1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
/*
* Copyright (C) 2008, 2011, 2012 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "HTMLPlugInImageElement.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "HTMLImageLoader.h"
#include "HTMLNames.h"
#include "Image.h"
#include "MouseEvent.h"
#include "NodeRenderStyle.h"
#include "Page.h"
#include "RenderEmbeddedObject.h"
#include "RenderImage.h"
#include "RenderSnapshottedPlugIn.h"
#include "SecurityOrigin.h"
#include "Settings.h"
#include "StyleResolver.h"
namespace WebCore {
// This delay should not exceed the snapshot delay in PluginView.cpp
static const double simulatedMouseClickTimerDelay = .75;
HTMLPlugInImageElement::HTMLPlugInImageElement(const QualifiedName& tagName, Document* document, bool createdByParser, PreferPlugInsForImagesOption preferPlugInsForImagesOption)
: HTMLPlugInElement(tagName, document)
// m_needsWidgetUpdate(!createdByParser) allows HTMLObjectElement to delay
// widget updates until after all children are parsed. For HTMLEmbedElement
// this delay is unnecessary, but it is simpler to make both classes share
// the same codepath in this class.
, m_needsWidgetUpdate(!createdByParser)
, m_shouldPreferPlugInsForImages(preferPlugInsForImagesOption == ShouldPreferPlugInsForImages)
, m_needsDocumentActivationCallbacks(false)
, m_simulatedMouseClickTimer(this, &HTMLPlugInImageElement::simulatedMouseClickTimerFired, simulatedMouseClickTimerDelay)
{
setHasCustomCallbacks();
if (document->page()
&& document->page()->settings()->plugInSnapshottingEnabled()
&& !ScriptController::processingUserGesture())
setDisplayState(WaitingForSnapshot);
}
HTMLPlugInImageElement::~HTMLPlugInImageElement()
{
if (m_needsDocumentActivationCallbacks)
document()->unregisterForPageCacheSuspensionCallbacks(this);
}
RenderEmbeddedObject* HTMLPlugInImageElement::renderEmbeddedObject() const
{
// HTMLObjectElement and HTMLEmbedElement may return arbitrary renderers
// when using fallback content.
if (!renderer() || !renderer()->isEmbeddedObject())
return 0;
return toRenderEmbeddedObject(renderer());
}
bool HTMLPlugInImageElement::isImageType()
{
if (m_serviceType.isEmpty() && protocolIs(m_url, "data"))
m_serviceType = mimeTypeFromDataURL(m_url);
if (Frame* frame = document()->frame()) {
KURL completedURL = document()->completeURL(m_url);
return frame->loader()->client()->objectContentType(completedURL, m_serviceType, shouldPreferPlugInsForImages()) == ObjectContentImage;
}
return Image::supportsType(m_serviceType);
}
// We don't use m_url, as it may not be the final URL that the object loads,
// depending on <param> values.
bool HTMLPlugInImageElement::allowedToLoadFrameURL(const String& url)
{
KURL completeURL = document()->completeURL(url);
if (contentFrame() && protocolIsJavaScript(completeURL)
&& !document()->securityOrigin()->canAccess(contentDocument()->securityOrigin()))
return false;
return document()->frame()->isURLAllowed(completeURL);
}
// We don't use m_url, or m_serviceType as they may not be the final values
// that <object> uses depending on <param> values.
bool HTMLPlugInImageElement::wouldLoadAsNetscapePlugin(const String& url, const String& serviceType)
{
ASSERT(document());
ASSERT(document()->frame());
KURL completedURL;
if (!url.isEmpty())
completedURL = document()->completeURL(url);
FrameLoader* frameLoader = document()->frame()->loader();
ASSERT(frameLoader);
if (frameLoader->client()->objectContentType(completedURL, serviceType, shouldPreferPlugInsForImages()) == ObjectContentNetscapePlugin)
return true;
return false;
}
RenderObject* HTMLPlugInImageElement::createRenderer(RenderArena* arena, RenderStyle* style)
{
// Once a PlugIn Element creates its renderer, it needs to be told when the Document goes
// inactive or reactivates so it can clear the renderer before going into the page cache.
if (!m_needsDocumentActivationCallbacks) {
m_needsDocumentActivationCallbacks = true;
document()->registerForPageCacheSuspensionCallbacks(this);
}
// Fallback content breaks the DOM->Renderer class relationship of this
// class and all superclasses because createObject won't necessarily
// return a RenderEmbeddedObject, RenderPart or even RenderWidget.
if (useFallbackContent())
return RenderObject::createObject(this, style);
if (isImageType()) {
RenderImage* image = new (arena) RenderImage(this);
image->setImageResource(RenderImageResource::create());
return image;
}
if (document()->page() && document()->page()->settings()->plugInSnapshottingEnabled())
return new (arena) RenderSnapshottedPlugIn(this);
return new (arena) RenderEmbeddedObject(this);
}
bool HTMLPlugInImageElement::willRecalcStyle(StyleChange)
{
// FIXME: Why is this necessary? Manual re-attach is almost always wrong.
if (!useFallbackContent() && needsWidgetUpdate() && renderer() && !isImageType())
reattach();
return true;
}
void HTMLPlugInImageElement::attach()
{
suspendPostAttachCallbacks();
bool isImage = isImageType();
if (!isImage)
queuePostAttachCallback(&HTMLPlugInImageElement::updateWidgetCallback, this);
HTMLPlugInElement::attach();
if (isImage && renderer() && !useFallbackContent()) {
if (!m_imageLoader)
m_imageLoader = adoptPtr(new HTMLImageLoader(this));
m_imageLoader->updateFromElement();
}
resumePostAttachCallbacks();
}
void HTMLPlugInImageElement::detach()
{
// FIXME: Because of the insanity that is HTMLPlugInImageElement::recalcStyle,
// we can end up detaching during an attach() call, before we even have a
// renderer. In that case, don't mark the widget for update.
if (attached() && renderer() && !useFallbackContent())
// Update the widget the next time we attach (detaching destroys the plugin).
setNeedsWidgetUpdate(true);
HTMLPlugInElement::detach();
}
void HTMLPlugInImageElement::updateWidgetIfNecessary()
{
document()->updateStyleIfNeeded();
if (!needsWidgetUpdate() || useFallbackContent() || isImageType())
return;
if (!renderEmbeddedObject() || renderEmbeddedObject()->showsUnavailablePluginIndicator())
return;
updateWidget(CreateOnlyNonNetscapePlugins);
}
void HTMLPlugInImageElement::finishParsingChildren()
{
HTMLPlugInElement::finishParsingChildren();
if (useFallbackContent())
return;
setNeedsWidgetUpdate(true);
if (inDocument())
setNeedsStyleRecalc();
}
void HTMLPlugInImageElement::didMoveToNewDocument(Document* oldDocument)
{
if (m_needsDocumentActivationCallbacks) {
if (oldDocument)
oldDocument->unregisterForPageCacheSuspensionCallbacks(this);
document()->registerForPageCacheSuspensionCallbacks(this);
}
if (m_imageLoader)
m_imageLoader->elementDidMoveToNewDocument();
HTMLPlugInElement::didMoveToNewDocument(oldDocument);
}
void HTMLPlugInImageElement::documentWillSuspendForPageCache()
{
if (RenderStyle* renderStyle = this->renderStyle()) {
m_customStyleForPageCache = RenderStyle::clone(renderStyle);
m_customStyleForPageCache->setDisplay(NONE);
recalcStyle(Force);
}
HTMLPlugInElement::documentWillSuspendForPageCache();
}
void HTMLPlugInImageElement::documentDidResumeFromPageCache()
{
if (m_customStyleForPageCache) {
m_customStyleForPageCache = 0;
recalcStyle(Force);
}
HTMLPlugInElement::documentDidResumeFromPageCache();
}
PassRefPtr<RenderStyle> HTMLPlugInImageElement::customStyleForRenderer()
{
if (!m_customStyleForPageCache)
return document()->styleResolver()->styleForElement(this);
return m_customStyleForPageCache;
}
void HTMLPlugInImageElement::updateWidgetCallback(Node* n, unsigned)
{
static_cast<HTMLPlugInImageElement*>(n)->updateWidgetIfNecessary();
}
void HTMLPlugInImageElement::updateSnapshot(PassRefPtr<Image> image)
{
if (displayState() > WaitingForSnapshot || !renderer()->isSnapshottedPlugIn())
return;
toRenderSnapshottedPlugIn(renderer())->updateSnapshot(image);
setDisplayState(DisplayingSnapshot);
}
void HTMLPlugInImageElement::setPendingClickEvent(PassRefPtr<MouseEvent> event)
{
m_pendingClickEventFromSnapshot = event;
}
void HTMLPlugInImageElement::dispatchPendingMouseClick()
{
ASSERT(!m_simulatedMouseClickTimer.isActive());
m_simulatedMouseClickTimer.restart();
}
void HTMLPlugInImageElement::simulatedMouseClickTimerFired(DeferrableOneShotTimer<HTMLPlugInImageElement>*)
{
ASSERT(displayState() == PlayingWithPendingMouseClick);
ASSERT(m_pendingClickEventFromSnapshot);
dispatchSimulatedClick(m_pendingClickEventFromSnapshot.get(), SendMouseOverUpDownEvents, DoNotShowPressedLook);
setDisplayState(Playing);
m_pendingClickEventFromSnapshot = nullptr;
}
} // namespace WebCore
|