Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/html/HTMLEmbedElement.h"
26
27 #include "core/CSSPropertyNames.h"
28 #include "core/HTMLNames.h"
29 #include "core/dom/Attribute.h"
30 #include "core/dom/ElementTraversal.h"
31 #include "core/dom/shadow/ShadowRoot.h"
32 #include "core/html/HTMLImageLoader.h"
33 #include "core/html/HTMLObjectElement.h"
34 #include "core/html/PluginDocument.h"
35 #include "core/html/parser/HTMLParserIdioms.h"
36 #include "core/rendering/RenderEmbeddedObject.h"
37 #include "core/rendering/RenderWidget.h"
38
39 namespace blink {
40
41 using namespace HTMLNames;
42
43 inline HTMLEmbedElement::HTMLEmbedElement(Document& document, bool createdByParser)
44     : HTMLPlugInElement(embedTag, document, createdByParser, ShouldPreferPlugInsForImages)
45 {
46     ScriptWrappable::init(this);
47 }
48
49 PassRefPtrWillBeRawPtr<HTMLEmbedElement> HTMLEmbedElement::create(Document& document, bool createdByParser)
50 {
51     RefPtrWillBeRawPtr<HTMLEmbedElement> element = adoptRefWillBeNoop(new HTMLEmbedElement(document, createdByParser));
52     element->ensureUserAgentShadowRoot();
53     return element.release();
54 }
55
56 static inline RenderWidget* findWidgetRenderer(const Node* n)
57 {
58     if (!n->renderer())
59         n = Traversal<HTMLObjectElement>::firstAncestor(*n);
60
61     if (n && n->renderer() && n->renderer()->isWidget())
62         return toRenderWidget(n->renderer());
63
64     return 0;
65 }
66
67 RenderWidget* HTMLEmbedElement::existingRenderWidget() const
68 {
69     return findWidgetRenderer(this);
70 }
71
72 bool HTMLEmbedElement::isPresentationAttribute(const QualifiedName& name) const
73 {
74     if (name == hiddenAttr)
75         return true;
76     return HTMLPlugInElement::isPresentationAttribute(name);
77 }
78
79 void HTMLEmbedElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
80 {
81     if (name == hiddenAttr) {
82         if (equalIgnoringCase(value, "yes") || equalIgnoringCase(value, "true")) {
83             addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, 0, CSSPrimitiveValue::CSS_PX);
84             addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, 0, CSSPrimitiveValue::CSS_PX);
85         }
86     } else {
87         HTMLPlugInElement::collectStyleForPresentationAttribute(name, value, style);
88     }
89 }
90
91 void HTMLEmbedElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
92 {
93     if (name == typeAttr) {
94         m_serviceType = value.string().lower();
95         size_t pos = m_serviceType.find(";");
96         if (pos != kNotFound)
97             m_serviceType = m_serviceType.left(pos);
98         if (!renderer())
99             requestPluginCreationWithoutRendererIfPossible();
100     } else if (name == codeAttr) {
101         m_url = stripLeadingAndTrailingHTMLSpaces(value);
102     } else if (name == srcAttr) {
103         m_url = stripLeadingAndTrailingHTMLSpaces(value);
104         if (renderer() && isImageType()) {
105             if (!m_imageLoader)
106                 m_imageLoader = HTMLImageLoader::create(this);
107             m_imageLoader->updateFromElement(ImageLoader::UpdateIgnorePreviousError);
108         }
109     } else {
110         HTMLPlugInElement::parseAttribute(name, value);
111     }
112 }
113
114 void HTMLEmbedElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues)
115 {
116     AttributeCollection attributes = this->attributes();
117     AttributeCollection::iterator end = attributes.end();
118     for (AttributeCollection::iterator it = attributes.begin(); it != end; ++it) {
119         paramNames.append(it->localName().string());
120         paramValues.append(it->value().string());
121     }
122 }
123
124 // FIXME: This should be unified with HTMLObjectElement::updateWidget and
125 // moved down into HTMLPluginElement.cpp
126 void HTMLEmbedElement::updateWidgetInternal()
127 {
128     ASSERT(!renderEmbeddedObject()->showsUnavailablePluginIndicator());
129     ASSERT(needsWidgetUpdate());
130     setNeedsWidgetUpdate(false);
131
132     if (m_url.isEmpty() && m_serviceType.isEmpty())
133         return;
134
135     // Note these pass m_url and m_serviceType to allow better code sharing with
136     // <object> which modifies url and serviceType before calling these.
137     if (!allowedToLoadFrameURL(m_url))
138         return;
139
140     // FIXME: These should be joined into a PluginParameters class.
141     Vector<String> paramNames;
142     Vector<String> paramValues;
143     parametersForPlugin(paramNames, paramValues);
144
145     RefPtrWillBeRawPtr<HTMLEmbedElement> protect(this); // Loading the plugin might remove us from the document.
146
147     // FIXME: Can we not have renderer here now that beforeload events are gone?
148     if (!renderer())
149         return;
150
151     requestObject(m_url, m_serviceType, paramNames, paramValues);
152 }
153
154 bool HTMLEmbedElement::rendererIsNeeded(const RenderStyle& style)
155 {
156     if (isImageType())
157         return HTMLPlugInElement::rendererIsNeeded(style);
158
159     LocalFrame* frame = document().frame();
160     if (!frame)
161         return false;
162
163     // If my parent is an <object> and is not set to use fallback content, I
164     // should be ignored and not get a renderer.
165     ContainerNode* p = parentNode();
166     if (isHTMLObjectElement(p)) {
167         ASSERT(p->renderer());
168         if (!toHTMLObjectElement(p)->useFallbackContent()) {
169             ASSERT(!p->renderer()->isEmbeddedObject());
170             return false;
171         }
172     }
173     return HTMLPlugInElement::rendererIsNeeded(style);
174 }
175
176 bool HTMLEmbedElement::isURLAttribute(const Attribute& attribute) const
177 {
178     return attribute.name() == srcAttr || HTMLPlugInElement::isURLAttribute(attribute);
179 }
180
181 const QualifiedName& HTMLEmbedElement::subResourceAttributeName() const
182 {
183     return srcAttr;
184 }
185
186 bool HTMLEmbedElement::isInteractiveContent() const
187 {
188     return true;
189 }
190
191 bool HTMLEmbedElement::isExposed() const
192 {
193     // http://www.whatwg.org/specs/web-apps/current-work/#exposed
194     for (HTMLObjectElement* object = Traversal<HTMLObjectElement>::firstAncestor(*this); object; object = Traversal<HTMLObjectElement>::firstAncestor(*object)) {
195         if (object->isExposed())
196             return false;
197     }
198     return true;
199 }
200
201 }