tizen beta release
[framework/web/webkit-efl.git] / Source / WebCore / html / HTMLEmbedElement.cpp
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, 2008, 2009, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23
24 #include "config.h"
25 #include "HTMLEmbedElement.h"
26
27 #include "Attribute.h"
28 #include "CSSPropertyNames.h"
29 #include "DocumentLoader.h"
30 #include "Frame.h"
31 #include "HTMLDocument.h"
32 #include "HTMLImageLoader.h"
33 #include "HTMLNames.h"
34 #include "HTMLObjectElement.h"
35 #include "HTMLParserIdioms.h"
36 #include "MainResourceLoader.h"
37 #include "PluginDocument.h"
38 #include "RenderEmbeddedObject.h"
39 #include "RenderImage.h"
40 #include "RenderWidget.h"
41 #include "Settings.h"
42
43 namespace WebCore {
44
45 using namespace HTMLNames;
46
47 inline HTMLEmbedElement::HTMLEmbedElement(const QualifiedName& tagName, Document* document, bool createdByParser)
48     : HTMLPlugInImageElement(tagName, document, createdByParser, ShouldPreferPlugInsForImages)
49 {
50     ASSERT(hasTagName(embedTag));
51 }
52
53 PassRefPtr<HTMLEmbedElement> HTMLEmbedElement::create(const QualifiedName& tagName, Document* document, bool createdByParser)
54 {
55     return adoptRef(new HTMLEmbedElement(tagName, document, createdByParser));
56 }
57
58 static inline RenderWidget* findWidgetRenderer(const Node* n) 
59 {
60     if (!n->renderer())
61         do
62             n = n->parentNode();
63         while (n && !n->hasTagName(objectTag));
64
65     if (n && n->renderer() && n->renderer()->isWidget())
66         return toRenderWidget(n->renderer());
67
68     return 0;
69 }
70
71 RenderWidget* HTMLEmbedElement::renderWidgetForJSBindings()
72 {
73     document()->updateLayoutIgnorePendingStylesheets();
74     return findWidgetRenderer(this);
75 }
76
77 bool HTMLEmbedElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
78 {
79     if (attrName == hiddenAttr) {
80         result = eUniversal;
81         return false;
82     }
83         
84     return HTMLPlugInImageElement::mapToEntry(attrName, result);
85 }
86
87 void HTMLEmbedElement::parseMappedAttribute(Attribute* attr)
88 {
89     const AtomicString& value = attr->value();
90   
91     if (attr->name() == typeAttr) {
92         m_serviceType = value.string().lower();
93         size_t pos = m_serviceType.find(";");
94         if (pos != notFound)
95             m_serviceType = m_serviceType.left(pos);
96         if (!isImageType() && m_imageLoader)
97             m_imageLoader.clear();
98     } else if (attr->name() == codeAttr)
99         m_url = stripLeadingAndTrailingHTMLSpaces(value.string());
100     else if (attr->name() == srcAttr) {
101         m_url = stripLeadingAndTrailingHTMLSpaces(value.string());
102         if (renderer() && isImageType()) {
103             if (!m_imageLoader)
104                 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
105             m_imageLoader->updateFromElementIgnoringPreviousError();
106         }
107     } else if (attr->name() == hiddenAttr) {
108         if (equalIgnoringCase(value.string(), "yes") || equalIgnoringCase(value.string(), "true")) {
109             // FIXME: Not dynamic, since we add this but don't remove it, but it may be OK for now
110             // that this rarely-used attribute won't work properly if you remove it.
111             addCSSLength(attr, CSSPropertyWidth, "0");
112             addCSSLength(attr, CSSPropertyHeight, "0");
113         }
114     } else if (attr->name() == nameAttr) {
115         if (inDocument() && document()->isHTMLDocument()) {
116             HTMLDocument* document = static_cast<HTMLDocument*>(this->document());
117             document->removeNamedItem(m_name);
118             document->addNamedItem(value);
119         }
120         m_name = value;
121     } else
122         HTMLPlugInImageElement::parseMappedAttribute(attr);
123 }
124
125 void HTMLEmbedElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues)
126 {
127     NamedNodeMap* attributes = this->attributes(true);
128     if (!attributes)
129         return;
130
131     for (unsigned i = 0; i < attributes->length(); ++i) {
132         Attribute* it = attributes->attributeItem(i);
133         paramNames.append(it->localName().string());
134         paramValues.append(it->value().string());
135     }
136 }
137
138 // FIXME: This should be unified with HTMLObjectElement::updateWidget and
139 // moved down into HTMLPluginImageElement.cpp
140 void HTMLEmbedElement::updateWidget(PluginCreationOption pluginCreationOption)
141 {
142     ASSERT(!renderEmbeddedObject()->pluginCrashedOrWasMissing());
143     // FIXME: We should ASSERT(needsWidgetUpdate()), but currently
144     // FrameView::updateWidget() calls updateWidget(false) without checking if
145     // the widget actually needs updating!
146     setNeedsWidgetUpdate(false);
147
148     if (m_url.isEmpty() && m_serviceType.isEmpty())
149         return;
150
151     // Note these pass m_url and m_serviceType to allow better code sharing with
152     // <object> which modifies url and serviceType before calling these.
153     if (!allowedToLoadFrameURL(m_url))
154         return;
155     // FIXME: It's sadness that we have this special case here.
156     //        See http://trac.webkit.org/changeset/25128 and
157     //        plugins/netscape-plugin-setwindow-size.html
158     if (pluginCreationOption == CreateOnlyNonNetscapePlugins && wouldLoadAsNetscapePlugin(m_url, m_serviceType))
159         return;
160
161     // FIXME: These should be joined into a PluginParameters class.
162     Vector<String> paramNames;
163     Vector<String> paramValues;
164     parametersForPlugin(paramNames, paramValues);
165
166     ASSERT(!m_inBeforeLoadEventHandler);
167     m_inBeforeLoadEventHandler = true;
168     bool beforeLoadAllowedLoad = dispatchBeforeLoadEvent(m_url);
169     m_inBeforeLoadEventHandler = false;
170
171     if (!beforeLoadAllowedLoad) {
172         if (document()->isPluginDocument()) {
173             // Plugins inside plugin documents load differently than other plugins. By the time
174             // we are here in a plugin document, the load of the plugin (which is the plugin document's
175             // main resource) has already started. We need to explicitly cancel the main resource load here.
176             toPluginDocument(document())->cancelManualPluginLoad();
177         }
178         return;
179     }
180
181     RefPtr<HTMLEmbedElement> protect(this); // Loading the plugin might remove us from the document.
182     SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
183     // FIXME: beforeLoad could have detached the renderer!  Just like in the <object> case above.
184     loader->requestObject(this, m_url, getAttribute(nameAttr), m_serviceType, paramNames, paramValues);
185 }
186
187 bool HTMLEmbedElement::rendererIsNeeded(const NodeRenderingContext& context)
188 {
189     if (isImageType())
190         return HTMLPlugInImageElement::rendererIsNeeded(context);
191
192     Frame* frame = document()->frame();
193     if (!frame)
194         return false;
195
196     // If my parent is an <object> and is not set to use fallback content, I
197     // should be ignored and not get a renderer.
198     ContainerNode* p = parentNode();
199     if (p && p->hasTagName(objectTag)) {
200         ASSERT(p->renderer());
201         if (!static_cast<HTMLObjectElement*>(p)->useFallbackContent()) {
202             ASSERT(!p->renderer()->isEmbeddedObject());
203             return false;
204         }
205     }
206
207 #if ENABLE(DASHBOARD_SUPPORT)
208     // Workaround for <rdar://problem/6642221>. 
209     if (Settings* settings = frame->settings()) {
210         if (settings->usesDashboardBackwardCompatibilityMode())
211             return true;
212     }
213 #endif
214
215     return HTMLPlugInImageElement::rendererIsNeeded(context);
216 }
217
218 void HTMLEmbedElement::insertedIntoDocument()
219 {
220     HTMLPlugInImageElement::insertedIntoDocument();
221     if (!inDocument())
222         return;
223
224     if (document()->isHTMLDocument())
225         static_cast<HTMLDocument*>(document())->addNamedItem(m_name);
226
227     String width = getAttribute(widthAttr);
228     String height = getAttribute(heightAttr);
229     if (!width.isEmpty() || !height.isEmpty()) {
230         Node* n = parentNode();
231         while (n && !n->hasTagName(objectTag))
232             n = n->parentNode();
233         if (n) {
234             if (!width.isEmpty())
235                 static_cast<HTMLObjectElement*>(n)->setAttribute(widthAttr, width);
236             if (!height.isEmpty())
237                 static_cast<HTMLObjectElement*>(n)->setAttribute(heightAttr, height);
238         }
239     }
240 }
241
242 void HTMLEmbedElement::removedFromDocument()
243 {
244     if (document()->isHTMLDocument())
245         static_cast<HTMLDocument*>(document())->removeNamedItem(m_name);
246
247     HTMLPlugInImageElement::removedFromDocument();
248 }
249
250 void HTMLEmbedElement::attributeChanged(Attribute* attr, bool preserveDecls)
251 {
252     HTMLPlugInImageElement::attributeChanged(attr, preserveDecls);
253
254     if ((attr->name() == widthAttr || attr->name() == heightAttr) && !attr->isEmpty()) {
255         ContainerNode* n = parentNode();
256         while (n && !n->hasTagName(objectTag))
257             n = n->parentNode();
258         if (n)
259             static_cast<HTMLObjectElement*>(n)->setAttribute(attr->name(), attr->value());
260     }
261 }
262
263 bool HTMLEmbedElement::isURLAttribute(Attribute* attr) const
264 {
265     return attr->name() == srcAttr || HTMLPlugInImageElement::isURLAttribute(attr);
266 }
267
268 const QualifiedName& HTMLEmbedElement::imageSourceAttributeName() const
269 {
270     return srcAttr;
271 }
272
273 void HTMLEmbedElement::addSubresourceAttributeURLs(ListHashSet<KURL>& urls) const
274 {
275     HTMLPlugInImageElement::addSubresourceAttributeURLs(urls);
276
277     addSubresourceURL(urls, document()->completeURL(getAttribute(srcAttr)));
278 }
279
280 #if ENABLE(MICRODATA)
281 String HTMLEmbedElement::itemValueText() const
282 {
283     return getURLAttribute(srcAttr);
284 }
285
286 void HTMLEmbedElement::setItemValueText(const String& value, ExceptionCode& ec)
287 {
288     setAttribute(srcAttr, value, ec);
289 }
290 #endif
291
292 }