initial import
[vuplus_webkit] / Source / WebCore / html / HTMLPlugInImageElement.cpp
1 /*
2  * Copyright (C) 2008, 2011 Apple Inc. All rights reserved.
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20
21 #include "config.h"
22 #include "HTMLPlugInImageElement.h"
23
24 #include "Frame.h"
25 #include "FrameLoader.h"
26 #include "FrameLoaderClient.h"
27 #include "HTMLImageLoader.h"
28 #include "HTMLNames.h"
29 #include "Image.h"
30 #include "Page.h"
31 #include "RenderEmbeddedObject.h"
32 #include "RenderImage.h"
33
34 namespace WebCore {
35
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)
44 {
45     setHasCustomWillOrDidRecalcStyle();
46 }
47
48 RenderEmbeddedObject* HTMLPlugInImageElement::renderEmbeddedObject() const
49 {
50     // HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
51     // when using fallback content.
52     if (!renderer() || !renderer()->isEmbeddedObject())
53         return 0;
54     return toRenderEmbeddedObject(renderer());
55 }
56
57 bool HTMLPlugInImageElement::isImageType()
58 {
59     if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
60         m_serviceType = mimeTypeFromDataURL(m_url);
61
62     if (Frame* frame = document()->frame()) {
63         KURL completedURL = document()->completeURL(m_url);
64         return frame->loader()->client()->objectContentType(completedURL, m_serviceType, shouldPreferPlugInsForImages()) == ObjectContentImage;
65     }
66
67     return Image::supportsType(m_serviceType);
68 }
69
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)
73 {
74     ASSERT(document());
75     ASSERT(document()->frame());
76     if (document()->frame()->page()->frameCount() >= Page::maxNumberOfFrames)
77         return false;
78
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)
86                 return false;
87             foundSelfReference = true;
88         }
89     }
90     return true;
91 }
92
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)
96 {
97     ASSERT(document());
98     ASSERT(document()->frame());
99     KURL completedURL;
100     if (!url.isEmpty())
101         completedURL = document()->completeURL(url);
102
103     FrameLoader* frameLoader = document()->frame()->loader();
104     ASSERT(frameLoader);
105     if (frameLoader->client()->objectContentType(completedURL, serviceType, shouldPreferPlugInsForImages()) == ObjectContentNetscapePlugin)
106         return true;
107     return false;
108 }
109
110 RenderObject* HTMLPlugInImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
111 {
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);
117     if (isImageType()) {
118         RenderImage* image = new (arena) RenderImage(this);
119         image->setImageResource(RenderImageResource::create());
120         return image;
121     }
122     return new (arena) RenderEmbeddedObject(this);
123 }
124
125 bool HTMLPlugInImageElement::willRecalcStyle(StyleChange)
126 {
127     // FIXME: Why is this necessary?  Manual re-attach is almost always wrong.
128     if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType())
129         reattach();
130     return true;
131 }
132
133 void HTMLPlugInImageElement::attach()
134 {
135     bool isImage = isImageType();
136     
137     if (!isImage)
138         queuePostAttachCallback(&HTMLPlugInImageElement::updateWidgetCallback, this);
139
140     HTMLPlugInElement::attach();
141
142     if (isImage && renderer() && !useFallbackContent()) {
143         if (!m_imageLoader)
144             m_imageLoader = adoptPtr(new HTMLImageLoader(this));
145         m_imageLoader->updateFromElement();
146     }
147 }
148     
149 void HTMLPlugInImageElement::detach()
150 {
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();
158 }
159
160 void HTMLPlugInImageElement::updateWidgetIfNecessary()
161 {
162     document()->updateStyleIfNeeded();
163
164     if (!needsWidgetUpdate() || useFallbackContent() || isImageType())
165         return;
166
167     if (!renderEmbeddedObject() || renderEmbeddedObject()->pluginCrashedOrWasMissing())
168         return;
169
170     updateWidget(CreateOnlyNonNetscapePlugins);
171 }
172
173 void HTMLPlugInImageElement::finishParsingChildren()
174 {
175     HTMLPlugInElement::finishParsingChildren();
176     if (useFallbackContent())
177         return;
178     
179     setNeedsWidgetUpdate(true);
180     if (inDocument())
181         setNeedsStyleRecalc();    
182 }
183
184 void HTMLPlugInImageElement::willMoveToNewOwnerDocument()
185 {
186     if (m_imageLoader)
187         m_imageLoader->elementWillMoveToNewOwnerDocument();
188     HTMLPlugInElement::willMoveToNewOwnerDocument();
189 }
190
191 void HTMLPlugInImageElement::updateWidgetCallback(Node* n, unsigned)
192 {
193     static_cast<HTMLPlugInImageElement*>(n)->updateWidgetIfNecessary();
194 }
195
196 } // namespace WebCore