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 "HTMLNames.h"
24 #include "HTMLPlugInElement.h"
25 #include "JSHTMLElement.h"
26 #include "runtime.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
pluginInstance(Node * node)38 static 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
getRuntimeObject(ExecState * exec,Node * node)52 static RuntimeObjectImp* getRuntimeObject(ExecState* exec, Node* node)
53 {
54 Instance* instance = pluginInstance(node);
55 if (!instance)
56 return 0;
57 return instance->createRuntimeObject(exec);
58 }
59
runtimeObjectGetter(ExecState * exec,const Identifier &,const PropertySlot & slot)60 JSValue runtimeObjectGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
61 {
62 JSHTMLElement* thisObj = static_cast<JSHTMLElement*>(asObject(slot.slotBase()));
63 HTMLElement* element = static_cast<HTMLElement*>(thisObj->impl());
64 RuntimeObjectImp* runtimeObject = getRuntimeObject(exec, element);
65 return runtimeObject ? runtimeObject : jsUndefined();
66 }
67
runtimeObjectPropertyGetter(ExecState * exec,const Identifier & propertyName,const PropertySlot & slot)68 JSValue runtimeObjectPropertyGetter(ExecState* exec, const Identifier& propertyName, const PropertySlot& slot)
69 {
70 JSHTMLElement* thisObj = static_cast<JSHTMLElement*>(asObject(slot.slotBase()));
71 HTMLElement* element = static_cast<HTMLElement*>(thisObj->impl());
72 RuntimeObjectImp* runtimeObject = getRuntimeObject(exec, element);
73 if (!runtimeObject)
74 return jsUndefined();
75 return runtimeObject->get(exec, propertyName);
76 }
77
runtimeObjectCustomGetOwnPropertySlot(ExecState * exec,const Identifier & propertyName,PropertySlot & slot,JSHTMLElement * element)78 bool runtimeObjectCustomGetOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot, JSHTMLElement* element)
79 {
80 RuntimeObjectImp* runtimeObject = getRuntimeObject(exec, element->impl());
81 if (!runtimeObject)
82 return false;
83 if (!runtimeObject->hasProperty(exec, propertyName))
84 return false;
85 slot.setCustom(element, runtimeObjectPropertyGetter);
86 return true;
87 }
88
runtimeObjectCustomPut(ExecState * exec,const Identifier & propertyName,JSValue value,HTMLElement * element,PutPropertySlot & slot)89 bool runtimeObjectCustomPut(ExecState* exec, const Identifier& propertyName, JSValue value, HTMLElement* element, PutPropertySlot& slot)
90 {
91 RuntimeObjectImp* runtimeObject = getRuntimeObject(exec, element);
92 if (!runtimeObject)
93 return 0;
94 if (!runtimeObject->hasProperty(exec, propertyName))
95 return false;
96 runtimeObject->put(exec, propertyName, value, slot);
97 return true;
98 }
99
callPlugin(ExecState * exec,JSObject * function,JSValue,const ArgList & args)100 static JSValue JSC_HOST_CALL callPlugin(ExecState* exec, JSObject* function, JSValue, const ArgList& args)
101 {
102 Instance* instance = pluginInstance(static_cast<JSHTMLElement*>(function)->impl());
103 instance->begin();
104 JSValue result = instance->invokeDefaultMethod(exec, args);
105 instance->end();
106 return result;
107 }
108
runtimeObjectGetCallData(HTMLElement * element,CallData & callData)109 CallType runtimeObjectGetCallData(HTMLElement* element, CallData& callData)
110 {
111 Instance* instance = pluginInstance(element);
112 if (!instance || !instance->supportsInvokeDefaultMethod())
113 return CallTypeNone;
114 callData.native.function = callPlugin;
115 return CallTypeHost;
116 }
117
118 } // namespace WebCore
119