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 "CSSPropertyNames.h"
28#include "Frame.h"
29#include "FrameLoader.h"
30#include "FrameView.h"
31#include "HTMLImageLoader.h"
32#include "HTMLNames.h"
33#include "HTMLObjectElement.h"
34#include "HTMLParserIdioms.h"
35#include "PluginDocument.h"
36#include "RenderEmbeddedObject.h"
37#include "RenderWidget.h"
38#include "Settings.h"
39#include "SubframeLoader.h"
40#include <wtf/IsoMallocInlines.h>
41#include <wtf/Ref.h>
42
43namespace WebCore {
44
45WTF_MAKE_ISO_ALLOCATED_IMPL(HTMLEmbedElement);
46
47using namespace HTMLNames;
48
49inline HTMLEmbedElement::HTMLEmbedElement(const QualifiedName& tagName, Document& document)
50 : HTMLPlugInImageElement(tagName, document)
51{
52 ASSERT(hasTagName(embedTag));
53}
54
55Ref<HTMLEmbedElement> HTMLEmbedElement::create(const QualifiedName& tagName, Document& document)
56{
57 auto result = adoptRef(*new HTMLEmbedElement(tagName, document));
58 result->finishCreating();
59 return result;
60}
61
62Ref<HTMLEmbedElement> HTMLEmbedElement::create(Document& document)
63{
64 return create(embedTag, document);
65}
66
67static inline RenderWidget* findWidgetRenderer(const Node* node)
68{
69 if (!node->renderer()) {
70 do {
71 node = node->parentNode();
72 } while (node && !is<HTMLObjectElement>(*node));
73 }
74
75 if (node && is<RenderWidget>(node->renderer()))
76 return downcast<RenderWidget>(node->renderer());
77
78 return nullptr;
79}
80
81RenderWidget* HTMLEmbedElement::renderWidgetLoadingPlugin() const
82{
83 RefPtr<FrameView> view = document().view();
84 if (!view || (!view->layoutContext().isInRenderTreeLayout() && !view->isPainting())) {
85 // Needs to load the plugin immediatedly because this function is called
86 // when JavaScript code accesses the plugin.
87 // FIXME: <rdar://16893708> Check if dispatching events here is safe.
88 document().updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasks::Synchronously);
89 }
90 return findWidgetRenderer(this);
91}
92
93void HTMLEmbedElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStyleProperties& style)
94{
95 if (name == hiddenAttr) {
96 if (equalLettersIgnoringASCIICase(value, "yes") || equalLettersIgnoringASCIICase(value, "true")) {
97 addPropertyToPresentationAttributeStyle(style, CSSPropertyWidth, 0, CSSPrimitiveValue::CSS_PX);
98 addPropertyToPresentationAttributeStyle(style, CSSPropertyHeight, 0, CSSPrimitiveValue::CSS_PX);
99 }
100 } else
101 HTMLPlugInImageElement::collectStyleForPresentationAttribute(name, value, style);
102}
103
104static bool hasTypeOrSrc(const HTMLEmbedElement& embed)
105{
106 return embed.hasAttributeWithoutSynchronization(typeAttr) || embed.hasAttributeWithoutSynchronization(srcAttr);
107}
108
109void HTMLEmbedElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
110{
111 if (name == typeAttr) {
112 m_serviceType = value.string().left(value.find(';')).convertToASCIILowercase();
113 // FIXME: The only difference between this and HTMLObjectElement's corresponding
114 // code is that HTMLObjectElement does setNeedsWidgetUpdate(true). Consider moving
115 // this up to the HTMLPlugInImageElement to be shared.
116 if (renderer() && !hasTypeOrSrc(*this))
117 invalidateStyle();
118 } else if (name == codeAttr) {
119 m_url = stripLeadingAndTrailingHTMLSpaces(value);
120 // FIXME: Why no call to updateImageLoaderWithNewURLSoon?
121 // FIXME: If both code and src attributes are specified, last one parsed/changed wins. That can't be right!
122 } else if (name == srcAttr) {
123 m_url = stripLeadingAndTrailingHTMLSpaces(value);
124 updateImageLoaderWithNewURLSoon();
125 if (renderer() && !hasTypeOrSrc(*this))
126 invalidateStyle();
127 // FIXME: If both code and src attributes are specified, last one parsed/changed wins. That can't be right!
128 } else
129 HTMLPlugInImageElement::parseAttribute(name, value);
130}
131
132void HTMLEmbedElement::parametersForPlugin(Vector<String>& paramNames, Vector<String>& paramValues)
133{
134 if (!hasAttributes())
135 return;
136
137 for (const Attribute& attribute : attributesIterator()) {
138 paramNames.append(attribute.localName().string());
139 paramValues.append(attribute.value().string());
140 }
141}
142
143// FIXME: This should be unified with HTMLObjectElement::updateWidget and
144// moved down into HTMLPluginImageElement.cpp
145void HTMLEmbedElement::updateWidget(CreatePlugins createPlugins)
146{
147 ASSERT(!renderEmbeddedObject()->isPluginUnavailable());
148 ASSERT(needsWidgetUpdate());
149
150 if (m_url.isEmpty() && m_serviceType.isEmpty()) {
151 setNeedsWidgetUpdate(false);
152 return;
153 }
154
155 // Note these pass m_url and m_serviceType to allow better code sharing with
156 // <object> which modifies url and serviceType before calling these.
157 if (!allowedToLoadFrameURL(m_url)) {
158 setNeedsWidgetUpdate(false);
159 return;
160 }
161
162 // FIXME: It's sadness that we have this special case here.
163 // See http://trac.webkit.org/changeset/25128 and
164 // plugins/netscape-plugin-setwindow-size.html
165 if (createPlugins == CreatePlugins::No && wouldLoadAsPlugIn(m_url, m_serviceType))
166 return;
167
168 setNeedsWidgetUpdate(false);
169
170 // FIXME: These should be joined into a PluginParameters class.
171 Vector<String> paramNames;
172 Vector<String> paramValues;
173 parametersForPlugin(paramNames, paramValues);
174
175 Ref<HTMLEmbedElement> protectedThis(*this); // Loading the plugin might remove us from the document.
176 bool beforeLoadAllowedLoad = guardedDispatchBeforeLoadEvent(m_url);
177 if (!beforeLoadAllowedLoad) {
178 if (is<PluginDocument>(document())) {
179 // Plugins inside plugin documents load differently than other plugins. By the time
180 // we are here in a plugin document, the load of the plugin (which is the plugin document's
181 // main resource) has already started. We need to explicitly cancel the main resource load here.
182 downcast<PluginDocument>(document()).cancelManualPluginLoad();
183 }
184 return;
185 }
186 if (!renderer()) // Do not load the plugin if beforeload removed this element or its renderer.
187 return;
188
189 // beforeLoad could have changed the document. Make sure the URL is still safe to load.
190 if (!allowedToLoadFrameURL(m_url))
191 return;
192
193 // FIXME: beforeLoad could have detached the renderer! Just like in the <object> case above.
194 requestObject(m_url, m_serviceType, paramNames, paramValues);
195}
196
197bool HTMLEmbedElement::rendererIsNeeded(const RenderStyle& style)
198{
199 if (!hasTypeOrSrc(*this))
200 return false;
201
202 if (isImageType())
203 return HTMLPlugInImageElement::rendererIsNeeded(style);
204
205 // If my parent is an <object> and is not set to use fallback content, I
206 // should be ignored and not get a renderer.
207 RefPtr<ContainerNode> parent = parentNode();
208 if (is<HTMLObjectElement>(parent)) {
209 if (!parent->renderer())
210 return false;
211 if (!downcast<HTMLObjectElement>(*parent).useFallbackContent()) {
212 ASSERT(!parent->renderer()->isEmbeddedObject());
213 return false;
214 }
215 }
216
217#if ENABLE(DASHBOARD_SUPPORT)
218 // Workaround for <rdar://problem/6642221>.
219 if (document().settings().usesDashboardBackwardCompatibilityMode())
220 return true;
221#endif
222
223 return HTMLPlugInImageElement::rendererIsNeeded(style);
224}
225
226bool HTMLEmbedElement::isURLAttribute(const Attribute& attribute) const
227{
228 return attribute.name() == srcAttr || HTMLPlugInImageElement::isURLAttribute(attribute);
229}
230
231const AtomicString& HTMLEmbedElement::imageSourceURL() const
232{
233 return attributeWithoutSynchronization(srcAttr);
234}
235
236void HTMLEmbedElement::addSubresourceAttributeURLs(ListHashSet<URL>& urls) const
237{
238 HTMLPlugInImageElement::addSubresourceAttributeURLs(urls);
239
240 addSubresourceURL(urls, document().completeURL(attributeWithoutSynchronization(srcAttr)));
241}
242
243}
244