1 /**
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include "config.h"
24 #include "HTMLPlugInElement.h"
25
26 #include "CSSPropertyNames.h"
27 #include "Document.h"
28 #include "Frame.h"
29 #include "FrameLoader.h"
30 #include "FrameTree.h"
31 #include "HTMLNames.h"
32 #include "MappedAttribute.h"
33 #include "Page.h"
34 #include "RenderWidget.h"
35 #include "ScriptController.h"
36 #include "Settings.h"
37 #include "Widget.h"
38
39 #if ENABLE(NETSCAPE_PLUGIN_API)
40 #include "npruntime_impl.h"
41 #endif
42
43 namespace WebCore {
44
45 using namespace HTMLNames;
46
HTMLPlugInElement(const QualifiedName & tagName,Document * doc)47 HTMLPlugInElement::HTMLPlugInElement(const QualifiedName& tagName, Document* doc)
48 : HTMLFrameOwnerElement(tagName, doc)
49 #if ENABLE(NETSCAPE_PLUGIN_API)
50 , m_NPObject(0)
51 #endif
52 {
53 }
54
~HTMLPlugInElement()55 HTMLPlugInElement::~HTMLPlugInElement()
56 {
57 ASSERT(!m_instance); // cleared in detach()
58
59 #if ENABLE(NETSCAPE_PLUGIN_API)
60 if (m_NPObject) {
61 _NPN_ReleaseObject(m_NPObject);
62 m_NPObject = 0;
63 }
64 #endif
65 }
66
detach()67 void HTMLPlugInElement::detach()
68 {
69 m_instance.clear();
70 HTMLFrameOwnerElement::detach();
71 }
72
getInstance() const73 PassScriptInstance HTMLPlugInElement::getInstance() const
74 {
75 Frame* frame = document()->frame();
76 if (!frame)
77 return 0;
78
79 // If the host dynamically turns off JavaScript (or Java) we will still return
80 // the cached allocated Bindings::Instance. Not supporting this edge-case is OK.
81 if (m_instance)
82 return m_instance;
83
84 RenderWidget* renderWidget = renderWidgetForJSBindings();
85 if (renderWidget && renderWidget->widget())
86 m_instance = frame->script()->createScriptInstanceForWidget(renderWidget->widget());
87
88 return m_instance;
89 }
90
height() const91 String HTMLPlugInElement::height() const
92 {
93 return getAttribute(heightAttr);
94 }
95
setHeight(const String & value)96 void HTMLPlugInElement::setHeight(const String& value)
97 {
98 setAttribute(heightAttr, value);
99 }
100
width() const101 String HTMLPlugInElement::width() const
102 {
103 return getAttribute(widthAttr);
104 }
105
setWidth(const String & value)106 void HTMLPlugInElement::setWidth(const String& value)
107 {
108 setAttribute(widthAttr, value);
109 }
110
mapToEntry(const QualifiedName & attrName,MappedAttributeEntry & result) const111 bool HTMLPlugInElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
112 {
113 if (attrName == widthAttr ||
114 attrName == heightAttr ||
115 attrName == vspaceAttr ||
116 attrName == hspaceAttr) {
117 result = eUniversal;
118 return false;
119 }
120
121 if (attrName == alignAttr) {
122 result = eReplaced; // Share with <img> since the alignment behavior is the same.
123 return false;
124 }
125
126 return HTMLFrameOwnerElement::mapToEntry(attrName, result);
127 }
128
parseMappedAttribute(MappedAttribute * attr)129 void HTMLPlugInElement::parseMappedAttribute(MappedAttribute* attr)
130 {
131 if (attr->name() == widthAttr)
132 addCSSLength(attr, CSSPropertyWidth, attr->value());
133 else if (attr->name() == heightAttr)
134 addCSSLength(attr, CSSPropertyHeight, attr->value());
135 else if (attr->name() == vspaceAttr) {
136 addCSSLength(attr, CSSPropertyMarginTop, attr->value());
137 addCSSLength(attr, CSSPropertyMarginBottom, attr->value());
138 } else if (attr->name() == hspaceAttr) {
139 addCSSLength(attr, CSSPropertyMarginLeft, attr->value());
140 addCSSLength(attr, CSSPropertyMarginRight, attr->value());
141 } else if (attr->name() == alignAttr)
142 addHTMLAlignment(attr);
143 else
144 HTMLFrameOwnerElement::parseMappedAttribute(attr);
145 }
146
checkDTD(const Node * newChild)147 bool HTMLPlugInElement::checkDTD(const Node* newChild)
148 {
149 return newChild->hasTagName(paramTag) || HTMLFrameOwnerElement::checkDTD(newChild);
150 }
151
defaultEventHandler(Event * event)152 void HTMLPlugInElement::defaultEventHandler(Event* event)
153 {
154 RenderObject* r = renderer();
155 if (!r || !r->isWidget())
156 return;
157 Widget* widget = toRenderWidget(r)->widget();
158 if (!widget)
159 return;
160 widget->handleEvent(event);
161 }
162
163 #if ENABLE(NETSCAPE_PLUGIN_API)
164
getNPObject()165 NPObject* HTMLPlugInElement::getNPObject()
166 {
167 ASSERT(document()->frame());
168 if (!m_NPObject)
169 m_NPObject = document()->frame()->script()->createScriptObjectForPluginElement(this);
170 return m_NPObject;
171 }
172
173 #endif /* ENABLE(NETSCAPE_PLUGIN_API) */
174
updateWidgetCallback(Node * n)175 void HTMLPlugInElement::updateWidgetCallback(Node* n)
176 {
177 static_cast<HTMLPlugInElement*>(n)->updateWidget();
178 }
179
180 #if PLATFORM(ANDROID)
supportsFocus() const181 bool HTMLPlugInElement::supportsFocus() const
182 {
183 return true;
184 }
185 #endif
186
187 }
188