2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "HTMLPlugInImageElement.h"
25 #include "FrameLoader.h"
26 #include "FrameLoaderClient.h"
27 #include "HTMLImageLoader.h"
30 #include "RenderEmbeddedObject.h"
31 #include "RenderImage.h"
35 HTMLPlugInImageElement::HTMLPlugInImageElement(const QualifiedName& tagName, Document* document, bool createdByParser)
36 : HTMLPlugInElement(tagName, document)
37 // m_needsWidgetUpdate(!createdByParser) allows HTMLObjectElement to delay
38 // widget updates until after all children are parsed. For HTMLEmbedElement
39 // this delay is unnecessary, but it is simpler to make both classes share
40 // the same codepath in this class.
41 , m_needsWidgetUpdate(!createdByParser)
45 RenderEmbeddedObject* HTMLPlugInImageElement::renderEmbeddedObject() const
47 // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
48 // when using fallback content.
49 if (!renderer() || !renderer()->isEmbeddedObject())
51 return toRenderEmbeddedObject(renderer());
54 bool HTMLPlugInImageElement::isImageType()
56 if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
57 m_serviceType = mimeTypeFromDataURL(m_url);
59 if (Frame* frame = document()->frame()) {
60 KURL completedURL = frame->loader()->completeURL(m_url);
61 return frame->loader()->client()->objectContentType(completedURL, m_serviceType) == ObjectContentImage;
64 return Image::supportsType(m_serviceType);
67 // We don't use m_url, as it may not be the final URL that the object loads,
68 // depending on <param> values.
69 bool HTMLPlugInImageElement::allowedToLoadFrameURL(const String& url)
72 ASSERT(document()->frame());
73 if (document()->frame()->page()->frameCount() >= Page::maxNumberOfFrames)
76 // We allow one level of self-reference because some sites depend on that.
77 // But we don't allow more than one.
78 KURL completeURL = document()->completeURL(url);
79 bool foundSelfReference = false;
80 for (Frame* frame = document()->frame(); frame; frame = frame->tree()->parent()) {
81 if (equalIgnoringFragmentIdentifier(frame->document()->url(), completeURL)) {
82 if (foundSelfReference)
84 foundSelfReference = true;
90 // We don't use m_url, or m_serviceType as they may not be the final values
91 // that <object> uses depending on <param> values.
92 bool HTMLPlugInImageElement::wouldLoadAsNetscapePlugin(const String& url, const String& serviceType)
95 ASSERT(document()->frame());
96 FrameLoader* frameLoader = document()->frame()->loader();
100 completedURL = frameLoader->completeURL(url);
102 if (frameLoader->client()->objectContentType(completedURL, serviceType) == ObjectContentNetscapePlugin)
107 RenderObject* HTMLPlugInImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
109 // Fallback content breaks the DOM->Renderer class relationship of this
110 // class and all superclasses because createObject won't necessarily
111 // return a RenderEmbeddedObject, RenderPart or even RenderWidget.
112 if (useFallbackContent())
113 return RenderObject::createObject(this, style);
115 RenderImage* image = new (arena) RenderImage(this);
116 image->setImageResource(RenderImageResource::create());
119 return new (arena) RenderEmbeddedObject(this);
122 void HTMLPlugInImageElement::recalcStyle(StyleChange ch)
124 // FIXME: Why is this necessary? Manual re-attach is almost always wrong.
125 if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType()) {
129 HTMLPlugInElement::recalcStyle(ch);
132 void HTMLPlugInImageElement::attach()
134 bool isImage = isImageType();
137 queuePostAttachCallback(&HTMLPlugInImageElement::updateWidgetCallback, this);
139 HTMLPlugInElement::attach();
141 if (isImage && renderer() && !useFallbackContent()) {
143 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
144 m_imageLoader->updateFromElement();
148 void HTMLPlugInImageElement::detach()
150 // FIXME: Because of the insanity that is HTMLPlugInImageElement::recalcStyle,
151 // we can end up detaching during an attach() call, before we even have a
152 // renderer. In that case, don't mark the widget for update.
153 if (attached() && renderer() && !useFallbackContent())
154 // Update the widget the next time we attach (detaching destroys the plugin).
155 setNeedsWidgetUpdate(true);
156 HTMLPlugInElement::detach();
159 void HTMLPlugInImageElement::updateWidgetIfNecessary()
161 document()->updateStyleIfNeeded();
163 if (!needsWidgetUpdate() || useFallbackContent() || isImageType())
166 if (!renderEmbeddedObject() || renderEmbeddedObject()->pluginCrashedOrWasMissing())
172 void HTMLPlugInImageElement::finishParsingChildren()
174 HTMLPlugInElement::finishParsingChildren();
175 if (useFallbackContent())
178 setNeedsWidgetUpdate(true);
180 setNeedsStyleRecalc();
183 void HTMLPlugInImageElement::willMoveToNewOwnerDocument()
186 m_imageLoader->elementWillMoveToNewOwnerDocument();
187 HTMLPlugInElement::willMoveToNewOwnerDocument();
190 void HTMLPlugInImageElement::updateWidgetCallback(Node* n)
192 static_cast<HTMLPlugInImageElement*>(n)->updateWidgetIfNecessary();
195 } // namespace WebCore