2 * Copyright (C) 2008, 2011 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"
28 #include "HTMLNames.h"
31 #include "RenderEmbeddedObject.h"
32 #include "RenderImage.h"
36 HTMLPlugInImageElement::HTMLPlugInImageElement(const QualifiedName& tagName, Document* document, bool createdByParser, PreferPlugInsForImagesOption preferPlugInsForImagesOption)
37 : HTMLPlugInElement(tagName, document)
38 // m_needsWidgetUpdate(!createdByParser) allows HTMLObjectElement to delay
39 // widget updates until after all children are parsed. For HTMLEmbedElement
40 // this delay is unnecessary, but it is simpler to make both classes share
41 // the same codepath in this class.
42 , m_needsWidgetUpdate(!createdByParser)
43 , m_shouldPreferPlugInsForImages(preferPlugInsForImagesOption == ShouldPreferPlugInsForImages)
45 setHasCustomWillOrDidRecalcStyle();
48 RenderEmbeddedObject* HTMLPlugInImageElement::renderEmbeddedObject() const
50 // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
51 // when using fallback content.
52 if (!renderer() || !renderer()->isEmbeddedObject())
54 return toRenderEmbeddedObject(renderer());
57 bool HTMLPlugInImageElement::isImageType()
59 if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
60 m_serviceType = mimeTypeFromDataURL(m_url);
62 if (Frame* frame = document()->frame()) {
63 KURL completedURL = document()->completeURL(m_url);
64 return frame->loader()->client()->objectContentType(completedURL, m_serviceType, shouldPreferPlugInsForImages()) == ObjectContentImage;
67 return Image::supportsType(m_serviceType);
70 // We don't use m_url, as it may not be the final URL that the object loads,
71 // depending on <param> values.
72 bool HTMLPlugInImageElement::allowedToLoadFrameURL(const String& url)
75 ASSERT(document()->frame());
76 if (document()->frame()->page()->frameCount() >= Page::maxNumberOfFrames)
79 // We allow one level of self-reference because some sites depend on that.
80 // But we don't allow more than one.
81 KURL completeURL = document()->completeURL(url);
82 bool foundSelfReference = false;
83 for (Frame* frame = document()->frame(); frame; frame = frame->tree()->parent()) {
84 if (equalIgnoringFragmentIdentifier(frame->document()->url(), completeURL)) {
85 if (foundSelfReference)
87 foundSelfReference = true;
93 // We don't use m_url, or m_serviceType as they may not be the final values
94 // that <object> uses depending on <param> values.
95 bool HTMLPlugInImageElement::wouldLoadAsNetscapePlugin(const String& url, const String& serviceType)
98 ASSERT(document()->frame());
101 completedURL = document()->completeURL(url);
103 FrameLoader* frameLoader = document()->frame()->loader();
105 if (frameLoader->client()->objectContentType(completedURL, serviceType, shouldPreferPlugInsForImages()) == ObjectContentNetscapePlugin)
110 RenderObject* HTMLPlugInImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
112 // Fallback content breaks the DOM->Renderer class relationship of this
113 // class and all superclasses because createObject won't necessarily
114 // return a RenderEmbeddedObject, RenderPart or even RenderWidget.
115 if (useFallbackContent())
116 return RenderObject::createObject(this, style);
118 RenderImage* image = new (arena) RenderImage(this);
119 image->setImageResource(RenderImageResource::create());
122 return new (arena) RenderEmbeddedObject(this);
125 bool HTMLPlugInImageElement::willRecalcStyle(StyleChange)
127 // FIXME: Why is this necessary? Manual re-attach is almost always wrong.
128 if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType())
133 void HTMLPlugInImageElement::attach()
135 bool isImage = isImageType();
138 queuePostAttachCallback(&HTMLPlugInImageElement::updateWidgetCallback, this);
140 HTMLPlugInElement::attach();
142 if (isImage && renderer() && !useFallbackContent()) {
144 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
145 m_imageLoader->updateFromElement();
149 void HTMLPlugInImageElement::detach()
151 // FIXME: Because of the insanity that is HTMLPlugInImageElement::recalcStyle,
152 // we can end up detaching during an attach() call, before we even have a
153 // renderer. In that case, don't mark the widget for update.
154 if (attached() && renderer() && !useFallbackContent())
155 // Update the widget the next time we attach (detaching destroys the plugin).
156 setNeedsWidgetUpdate(true);
157 HTMLPlugInElement::detach();
160 void HTMLPlugInImageElement::updateWidgetIfNecessary()
162 document()->updateStyleIfNeeded();
164 if (!needsWidgetUpdate() || useFallbackContent() || isImageType())
167 if (!renderEmbeddedObject() || renderEmbeddedObject()->pluginCrashedOrWasMissing())
170 updateWidget(CreateOnlyNonNetscapePlugins);
173 void HTMLPlugInImageElement::finishParsingChildren()
175 HTMLPlugInElement::finishParsingChildren();
176 if (useFallbackContent())
179 setNeedsWidgetUpdate(true);
181 setNeedsStyleRecalc();
184 void HTMLPlugInImageElement::willMoveToNewOwnerDocument()
187 m_imageLoader->elementWillMoveToNewOwnerDocument();
188 HTMLPlugInElement::willMoveToNewOwnerDocument();
191 void HTMLPlugInImageElement::updateWidgetCallback(Node* n, unsigned)
193 static_cast<HTMLPlugInImageElement*>(n)->updateWidgetIfNecessary();
196 } // namespace WebCore