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.
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.
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.
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.
24 #include "core/html/HTMLPlugInElement.h"
26 #include "CSSPropertyNames.h"
27 #include "HTMLNames.h"
28 #include "bindings/v8/ScriptController.h"
29 #include "bindings/v8/npruntime_impl.h"
30 #include "core/dom/Document.h"
31 #include "core/dom/Node.h"
32 #include "core/dom/PostAttachCallbacks.h"
33 #include "core/dom/shadow/ShadowRoot.h"
34 #include "core/events/Event.h"
35 #include "core/frame/ContentSecurityPolicy.h"
36 #include "core/frame/Frame.h"
37 #include "core/html/HTMLImageLoader.h"
38 #include "core/html/PluginDocument.h"
39 #include "core/html/shadow/HTMLContentElement.h"
40 #include "core/loader/FrameLoaderClient.h"
41 #include "core/page/EventHandler.h"
42 #include "core/page/Page.h"
43 #include "core/frame/Settings.h"
44 #include "core/plugins/PluginView.h"
45 #include "core/rendering/RenderEmbeddedObject.h"
46 #include "core/rendering/RenderImage.h"
47 #include "core/rendering/RenderWidget.h"
48 #include "platform/Logging.h"
49 #include "platform/MIMETypeFromURL.h"
50 #include "platform/MIMETypeRegistry.h"
51 #include "platform/Widget.h"
52 #include "platform/plugins/PluginData.h"
56 using namespace HTMLNames;
58 HTMLPlugInElement::HTMLPlugInElement(const QualifiedName& tagName, Document& doc, bool createdByParser, PreferPlugInsForImagesOption preferPlugInsForImagesOption)
59 : HTMLFrameOwnerElement(tagName, doc)
60 , m_isDelayingLoadEvent(false)
62 , m_isCapturingMouseEvents(false)
63 , m_inBeforeLoadEventHandler(false)
64 // m_needsWidgetUpdate(!createdByParser) allows HTMLObjectElement to delay
65 // widget updates until after all children are parsed. For HTMLEmbedElement
66 // this delay is unnecessary, but it is simpler to make both classes share
67 // the same codepath in this class.
68 , m_needsWidgetUpdate(!createdByParser)
69 , m_shouldPreferPlugInsForImages(preferPlugInsForImagesOption == ShouldPreferPlugInsForImages)
70 , m_displayState(Playing)
72 setHasCustomStyleCallbacks();
75 HTMLPlugInElement::~HTMLPlugInElement()
77 ASSERT(!m_pluginWrapper); // cleared in detach()
78 ASSERT(!m_isDelayingLoadEvent);
81 _NPN_ReleaseObject(m_NPObject);
86 bool HTMLPlugInElement::canProcessDrag() const
88 return pluginWidget() && pluginWidget()->isPluginView() && toPluginView(pluginWidget())->canProcessDrag();
91 bool HTMLPlugInElement::willRespondToMouseClickEvents()
93 if (isDisabledFormControl())
95 RenderObject* r = renderer();
96 return r && (r->isEmbeddedObject() || r->isWidget());
99 void HTMLPlugInElement::removeAllEventListeners()
101 HTMLFrameOwnerElement::removeAllEventListeners();
102 if (RenderWidget* renderer = existingRenderWidget()) {
103 if (Widget* widget = renderer->widget())
104 widget->eventListenersRemoved();
108 void HTMLPlugInElement::didMoveToNewDocument(Document& oldDocument)
111 m_imageLoader->elementDidMoveToNewDocument();
112 HTMLFrameOwnerElement::didMoveToNewDocument(oldDocument);
115 void HTMLPlugInElement::attach(const AttachContext& context)
117 HTMLFrameOwnerElement::attach(context);
119 if (!renderer() || useFallbackContent())
123 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
124 m_imageLoader->updateFromElement();
125 } else if (needsWidgetUpdate()
126 && renderEmbeddedObject()
127 && !renderEmbeddedObject()->showsUnavailablePluginIndicator()
128 && !wouldLoadAsNetscapePlugin(m_url, m_serviceType)
129 && !m_isDelayingLoadEvent) {
130 m_isDelayingLoadEvent = true;
131 document().incrementLoadEventDelayCount();
135 void HTMLPlugInElement::updateWidget()
137 RefPtr<HTMLPlugInElement> protector(this);
138 updateWidgetInternal();
139 if (m_isDelayingLoadEvent) {
140 m_isDelayingLoadEvent = false;
141 document().decrementLoadEventDelayCount();
145 void HTMLPlugInElement::detach(const AttachContext& context)
147 // Update the widget the next time we attach (detaching destroys the plugin).
148 // FIXME: None of this "needsWidgetUpdate" related code looks right.
149 if (renderer() && !useFallbackContent())
150 setNeedsWidgetUpdate(true);
151 if (m_isDelayingLoadEvent) {
152 m_isDelayingLoadEvent = false;
153 document().decrementLoadEventDelayCount();
158 if (m_isCapturingMouseEvents) {
159 if (Frame* frame = document().frame())
160 frame->eventHandler().setCapturingMouseEventsNode(0);
161 m_isCapturingMouseEvents = false;
165 _NPN_ReleaseObject(m_NPObject);
169 HTMLFrameOwnerElement::detach(context);
172 RenderObject* HTMLPlugInElement::createRenderer(RenderStyle* style)
174 // Fallback content breaks the DOM->Renderer class relationship of this
175 // class and all superclasses because createObject won't necessarily
176 // return a RenderEmbeddedObject, RenderPart or even RenderWidget.
177 if (useFallbackContent())
178 return RenderObject::createObject(this, style);
181 RenderImage* image = new RenderImage(this);
182 image->setImageResource(RenderImageResource::create());
186 return new RenderEmbeddedObject(this);
189 void HTMLPlugInElement::willRecalcStyle(StyleRecalcChange)
191 // FIXME: Why is this necessary? Manual re-attach is almost always wrong.
192 if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType())
196 void HTMLPlugInElement::finishParsingChildren()
198 HTMLFrameOwnerElement::finishParsingChildren();
199 if (useFallbackContent())
202 setNeedsWidgetUpdate(true);
204 setNeedsStyleRecalc();
207 void HTMLPlugInElement::resetInstance()
209 m_pluginWrapper.clear();
212 SharedPersistent<v8::Object>* HTMLPlugInElement::pluginWrapper()
214 Frame* frame = document().frame();
218 // If the host dynamically turns off JavaScript (or Java) we will still
219 // return the cached allocated Bindings::Instance. Not supporting this
221 if (!m_pluginWrapper) {
222 if (Widget* widget = pluginWidget())
223 m_pluginWrapper = frame->script().createPluginWrapper(widget);
225 return m_pluginWrapper.get();
228 bool HTMLPlugInElement::dispatchBeforeLoadEvent(const String& sourceURL)
230 // FIXME: Our current plug-in loading design can't guarantee the following
231 // assertion is true, since plug-in loading can be initiated during layout,
232 // and synchronous layout can be initiated in a beforeload event handler!
233 // See <http://webkit.org/b/71264>.
234 // ASSERT(!m_inBeforeLoadEventHandler);
235 m_inBeforeLoadEventHandler = true;
236 bool beforeLoadAllowedLoad = HTMLFrameOwnerElement::dispatchBeforeLoadEvent(sourceURL);
237 m_inBeforeLoadEventHandler = false;
238 return beforeLoadAllowedLoad;
241 Widget* HTMLPlugInElement::pluginWidget() const
243 if (m_inBeforeLoadEventHandler) {
244 // The plug-in hasn't loaded yet, and it makes no sense to try to load
245 // if beforeload handler happened to touch the plug-in element. That
246 // would recursively call beforeload for the same element.
250 if (RenderWidget* renderWidget = renderWidgetForJSBindings())
251 return renderWidget->widget();
255 bool HTMLPlugInElement::isPresentationAttribute(const QualifiedName& name) const
257 if (name == widthAttr || name == heightAttr || name == vspaceAttr || name == hspaceAttr || name == alignAttr)
259 return HTMLFrameOwnerElement::isPresentationAttribute(name);
262 void HTMLPlugInElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
264 if (name == widthAttr) {
265 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
266 } else if (name == heightAttr) {
267 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
268 } else if (name == vspaceAttr) {
269 addHTMLLengthToStyle(style, CSSPropertyMarginTop, value);
270 addHTMLLengthToStyle(style, CSSPropertyMarginBottom, value);
271 } else if (name == hspaceAttr) {
272 addHTMLLengthToStyle(style, CSSPropertyMarginLeft, value);
273 addHTMLLengthToStyle(style, CSSPropertyMarginRight, value);
274 } else if (name == alignAttr) {
275 applyAlignmentAttributeToStyle(value, style);
277 HTMLFrameOwnerElement::collectStyleForPresentationAttribute(name, value, style);
281 void HTMLPlugInElement::defaultEventHandler(Event* event)
283 // Firefox seems to use a fake event listener to dispatch events to plug-in
284 // (tested with mouse events only). This is observable via different order
285 // of events - in Firefox, event listeners specified in HTML attributes
286 // fires first, then an event gets dispatched to plug-in, and only then
287 // other event listeners fire. Hopefully, this difference does not matter in
290 // FIXME: Mouse down and scroll events are passed down to plug-in via custom
291 // code in EventHandler; these code paths should be united.
293 RenderObject* r = renderer();
294 if (!r || !r->isWidget())
296 if (r->isEmbeddedObject()) {
297 if (toRenderEmbeddedObject(r)->showsUnavailablePluginIndicator())
299 if (displayState() < Playing)
302 RefPtr<Widget> widget = toRenderWidget(r)->widget();
305 widget->handleEvent(event);
306 if (event->defaultHandled())
308 HTMLFrameOwnerElement::defaultEventHandler(event);
311 RenderWidget* HTMLPlugInElement::renderWidgetForJSBindings() const
313 // Needs to load the plugin immediatedly because this function is called
314 // when JavaScript code accesses the plugin.
315 // FIXME: Check if dispatching events here is safe.
316 document().updateLayoutIgnorePendingStylesheets(Document::RunPostLayoutTasksSynchronously);
317 return existingRenderWidget();
320 bool HTMLPlugInElement::isKeyboardFocusable() const
322 if (!document().isActive())
324 return pluginWidget() && pluginWidget()->isPluginView() && toPluginView(pluginWidget())->supportsKeyboardFocus();
327 bool HTMLPlugInElement::isPluginElement() const
332 bool HTMLPlugInElement::rendererIsFocusable() const
334 if (HTMLFrameOwnerElement::supportsFocus() && HTMLFrameOwnerElement::rendererIsFocusable())
337 if (useFallbackContent() || !renderer() || !renderer()->isEmbeddedObject())
339 return !toRenderEmbeddedObject(renderer())->showsUnavailablePluginIndicator();
342 NPObject* HTMLPlugInElement::getNPObject()
344 ASSERT(document().frame());
346 m_NPObject = document().frame()->script().createScriptObjectForPluginElement(this);
350 bool HTMLPlugInElement::isImageType()
352 if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
353 m_serviceType = mimeTypeFromDataURL(m_url);
355 if (Frame* frame = document().frame()) {
356 KURL completedURL = document().completeURL(m_url);
357 return frame->loader().client()->objectContentType(completedURL, m_serviceType, shouldPreferPlugInsForImages()) == ObjectContentImage;
360 return Image::supportsType(m_serviceType);
363 const String HTMLPlugInElement::loadedMimeType() const
365 String mimeType = m_serviceType;
366 if (mimeType.isEmpty())
367 mimeType = mimeTypeFromURL(m_loadedUrl);
371 RenderEmbeddedObject* HTMLPlugInElement::renderEmbeddedObject() const
373 // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
374 // when using fallback content.
375 if (!renderer() || !renderer()->isEmbeddedObject())
377 return toRenderEmbeddedObject(renderer());
380 // We don't use m_url, as it may not be the final URL that the object loads,
381 // depending on <param> values.
382 bool HTMLPlugInElement::allowedToLoadFrameURL(const String& url)
384 KURL completeURL = document().completeURL(url);
385 if (contentFrame() && protocolIsJavaScript(completeURL)
386 && !document().securityOrigin()->canAccess(contentDocument()->securityOrigin()))
388 return document().frame()->isURLAllowed(completeURL);
391 // We don't use m_url, or m_serviceType as they may not be the final values
392 // that <object> uses depending on <param> values.
393 bool HTMLPlugInElement::wouldLoadAsNetscapePlugin(const String& url, const String& serviceType)
395 ASSERT(document().frame());
398 completedURL = document().completeURL(url);
399 return document().frame()->loader().client()->objectContentType(completedURL, serviceType, shouldPreferPlugInsForImages()) == ObjectContentNetscapePlugin;
402 bool HTMLPlugInElement::requestObject(const String& url, const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues)
404 if (url.isEmpty() && mimeType.isEmpty())
407 // FIXME: None of this code should use renderers!
408 RenderEmbeddedObject* renderer = renderEmbeddedObject();
413 KURL completedURL = document().completeURL(url);
414 if (!pluginIsLoadable(completedURL, mimeType))
418 if (shouldUsePlugin(completedURL, mimeType, renderer->hasFallbackContent(), useFallback))
419 return loadPlugin(completedURL, mimeType, paramNames, paramValues, useFallback);
421 // If the plug-in element already contains a subframe,
422 // loadOrRedirectSubframe will re-use it. Otherwise, it will create a new
423 // frame and set it as the RenderPart's widget, causing what was previously
424 // in the widget to be torn down.
425 return loadOrRedirectSubframe(completedURL, getNameAttribute(), true);
428 bool HTMLPlugInElement::loadPlugin(const KURL& url, const String& mimeType, const Vector<String>& paramNames, const Vector<String>& paramValues, bool useFallback)
430 Frame* frame = document().frame();
432 if (!frame->loader().allowPlugins(AboutToInstantiatePlugin))
435 RenderEmbeddedObject* renderer = renderEmbeddedObject();
436 // FIXME: This code should not depend on renderer!
437 if (!renderer || useFallback)
440 WTF_LOG(Plugins, "%p Plug-in URL: %s", this, m_url.utf8().data());
441 WTF_LOG(Plugins, " Loaded URL: %s", url.string().utf8().data());
444 IntSize contentSize = roundedIntSize(LayoutSize(renderer->contentWidth(), renderer->contentHeight()));
445 bool loadManually = document().isPluginDocument() && !document().containsPlugins() && toPluginDocument(document()).shouldLoadPluginManually();
446 RefPtr<Widget> widget = frame->loader().client()->createPlugin(contentSize, this, url, paramNames, paramValues, mimeType, loadManually);
449 if (!renderer->showsUnavailablePluginIndicator())
450 renderer->setPluginUnavailabilityReason(RenderEmbeddedObject::PluginMissing);
454 renderer->setWidget(widget);
455 document().setContainsPlugins();
456 setNeedsStyleRecalc(LocalStyleChange, StyleChangeFromRenderer);
460 bool HTMLPlugInElement::shouldUsePlugin(const KURL& url, const String& mimeType, bool hasFallback, bool& useFallback)
462 // Allow other plug-ins to win over QuickTime because if the user has
463 // installed a plug-in that can handle TIFF (which QuickTime can also
464 // handle) they probably intended to override QT.
465 if (document().frame()->page() && (mimeType == "image/tiff" || mimeType == "image/tif" || mimeType == "image/x-tiff")) {
466 const PluginData* pluginData = document().frame()->page()->pluginData();
467 String pluginName = pluginData ? pluginData->pluginNameForMimeType(mimeType) : String();
468 if (!pluginName.isEmpty() && !pluginName.contains("QuickTime", false))
472 ObjectContentType objectType = document().frame()->loader().client()->objectContentType(url, mimeType, shouldPreferPlugInsForImages());
473 // If an object's content can't be handled and it has no fallback, let
474 // it be handled as a plugin to show the broken plugin icon.
475 useFallback = objectType == ObjectContentNone && hasFallback;
476 return objectType == ObjectContentNone || objectType == ObjectContentNetscapePlugin || objectType == ObjectContentOtherPlugin;
480 void HTMLPlugInElement::dispatchErrorEvent()
482 if (document().isPluginDocument() && document().ownerElement())
483 document().ownerElement()->dispatchEvent(Event::create(EventTypeNames::error));
485 dispatchEvent(Event::create(EventTypeNames::error));
488 bool HTMLPlugInElement::pluginIsLoadable(const KURL& url, const String& mimeType)
490 Frame* frame = document().frame();
491 Settings* settings = frame->settings();
495 if (MIMETypeRegistry::isJavaAppletMIMEType(mimeType) && !settings->javaEnabled())
498 if (document().isSandboxed(SandboxPlugins))
501 if (!document().securityOrigin()->canDisplay(url)) {
502 FrameLoader::reportLocalLoadFailed(frame, url.string());
506 AtomicString declaredMimeType = document().isPluginDocument() && document().ownerElement() ?
507 document().ownerElement()->fastGetAttribute(HTMLNames::typeAttr) :
508 fastGetAttribute(HTMLNames::typeAttr);
509 if (!document().contentSecurityPolicy()->allowObjectFromSource(url)
510 || !document().contentSecurityPolicy()->allowPluginType(mimeType, declaredMimeType, url)) {
511 renderEmbeddedObject()->setPluginUnavailabilityReason(RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy);
515 return frame->loader().mixedContentChecker()->canRunInsecureContent(document().securityOrigin(), url);
518 void HTMLPlugInElement::didAddUserAgentShadowRoot(ShadowRoot&)
520 userAgentShadowRoot()->appendChild(HTMLContentElement::create(document()));
523 void HTMLPlugInElement::didAddShadowRoot(ShadowRoot& root)
525 if (root.isOldestAuthorShadowRoot())
526 lazyReattachIfAttached();
529 bool HTMLPlugInElement::useFallbackContent() const
531 return hasAuthorShadowRoot();