WebCore/bindings/js/JSPluginElementFunctions.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
       
     3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
       
     4  *
       
     5  *  This library is free software; you can redistribute it and/or
       
     6  *  modify it under the terms of the GNU Lesser General Public
       
     7  *  License as published by the Free Software Foundation; either
       
     8  *  version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  *  This library is distributed in the hope that it will be useful,
       
    11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  *  Lesser General Public License for more details.
       
    14  *
       
    15  *  You should have received a copy of the GNU Lesser General Public
       
    16  *  License along with this library; if not, write to the Free Software
       
    17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
       
    18  */
       
    19 
       
    20 #include "config.h"
       
    21 #include "JSPluginElementFunctions.h"
       
    22 
       
    23 #include "Bridge.h"
       
    24 #include "HTMLNames.h"
       
    25 #include "HTMLPlugInElement.h"
       
    26 #include "JSHTMLElement.h"
       
    27 #include "runtime_object.h"
       
    28 
       
    29 using namespace JSC;
       
    30 
       
    31 namespace WebCore {
       
    32 
       
    33 using namespace Bindings;
       
    34 using namespace HTMLNames;
       
    35 
       
    36 // Runtime object support code for JSHTMLAppletElement, JSHTMLEmbedElement and JSHTMLObjectElement.
       
    37 
       
    38 Instance* pluginInstance(Node* node)
       
    39 {
       
    40     if (!node)
       
    41         return 0;
       
    42     if (!(node->hasTagName(objectTag) || node->hasTagName(embedTag) || node->hasTagName(appletTag)))
       
    43         return 0;
       
    44     HTMLPlugInElement* plugInElement = static_cast<HTMLPlugInElement*>(node);
       
    45     // The plugin element holds an owning reference, so we don't have to.
       
    46     Instance* instance = plugInElement->getInstance().get();
       
    47     if (!instance || !instance->rootObject())
       
    48         return 0;
       
    49     return instance;
       
    50 }
       
    51 
       
    52 static RuntimeObject* getRuntimeObject(ExecState* exec, Node* node)
       
    53 {
       
    54     Instance* instance = pluginInstance(node);
       
    55     if (!instance)
       
    56         return 0;
       
    57     return instance->createRuntimeObject(exec);
       
    58 }
       
    59 
       
    60 JSValue runtimeObjectPropertyGetter(ExecState* exec, JSValue slotBase, const Identifier& propertyName)
       
    61 {
       
    62     JSHTMLElement* thisObj = static_cast<JSHTMLElement*>(asObject(slotBase));
       
    63     HTMLElement* element = static_cast<HTMLElement*>(thisObj->impl());
       
    64     RuntimeObject* runtimeObject = getRuntimeObject(exec, element);
       
    65     if (!runtimeObject)
       
    66         return jsUndefined();
       
    67     return runtimeObject->get(exec, propertyName);
       
    68 }
       
    69 
       
    70 bool runtimeObjectCustomGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, JSHTMLElement* element)
       
    71 {
       
    72     RuntimeObject* runtimeObject = getRuntimeObject(exec, element->impl());
       
    73     if (!runtimeObject)
       
    74         return false;
       
    75     if (!runtimeObject->hasProperty(exec, propertyName))
       
    76         return false;
       
    77     slot.setCustom(element, runtimeObjectPropertyGetter);
       
    78     return true;
       
    79 }
       
    80 
       
    81 bool runtimeObjectCustomGetOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor, JSHTMLElement* element)
       
    82 {
       
    83     RuntimeObject* runtimeObject = getRuntimeObject(exec, element->impl());
       
    84     if (!runtimeObject)
       
    85         return false;
       
    86     if (!runtimeObject->hasProperty(exec, propertyName))
       
    87         return false;
       
    88     PropertySlot slot;
       
    89     slot.setCustom(element, runtimeObjectPropertyGetter);
       
    90     // While we don't know what the plugin allows, we do know that we prevent
       
    91     // enumeration or deletion of properties, so we mark plugin properties
       
    92     // as DontEnum | DontDelete
       
    93     descriptor.setDescriptor(slot.getValue(exec, propertyName), DontEnum | DontDelete);
       
    94     return true;
       
    95 }
       
    96 
       
    97 bool runtimeObjectCustomPut(ExecState* exec, const Identifier& propertyName, JSValue value, HTMLElement* element, PutPropertySlot& slot)
       
    98 {
       
    99     RuntimeObject* runtimeObject = getRuntimeObject(exec, element);
       
   100     if (!runtimeObject)
       
   101         return 0;
       
   102     if (!runtimeObject->hasProperty(exec, propertyName))
       
   103         return false;
       
   104     runtimeObject->put(exec, propertyName, value, slot);
       
   105     return true;
       
   106 }
       
   107 
       
   108 static EncodedJSValue JSC_HOST_CALL callPlugin(ExecState* exec)
       
   109 {
       
   110     Instance* instance = pluginInstance(static_cast<JSHTMLElement*>(exec->callee())->impl());
       
   111     instance->begin();
       
   112     JSValue result = instance->invokeDefaultMethod(exec);
       
   113     instance->end();
       
   114     return JSValue::encode(result);
       
   115 }
       
   116 
       
   117 CallType runtimeObjectGetCallData(HTMLElement* element, CallData& callData)
       
   118 {
       
   119     Instance* instance = pluginInstance(element);
       
   120     if (!instance || !instance->supportsInvokeDefaultMethod())
       
   121         return CallTypeNone;
       
   122     callData.native.function = callPlugin;
       
   123     return CallTypeHost;
       
   124 }
       
   125 
       
   126 } // namespace WebCore