Remove the notion of inactive plug-ins
[WebKit-https.git] / Source / WebCore / rendering / RenderEmbeddedObject.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 2000 Simon Hausmann <hausmann@kde.org>
4  *           (C) 2000 Stefan Schimanski (1Stein@gmx.de)
5  * Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #include "config.h"
25 #include "RenderEmbeddedObject.h"
26
27 #include "CSSValueKeywords.h"
28 #include "Chrome.h"
29 #include "ChromeClient.h"
30 #include "Cursor.h"
31 #include "EventHandler.h"
32 #include "Font.h"
33 #include "FontSelector.h"
34 #include "Frame.h"
35 #include "FrameLoaderClient.h"
36 #include "GraphicsContext.h"
37 #include "HTMLEmbedElement.h"
38 #include "HTMLIFrameElement.h"
39 #include "HTMLNames.h"
40 #include "HTMLObjectElement.h"
41 #include "HTMLParamElement.h"
42 #include "HTMLPlugInElement.h"
43 #include "HitTestResult.h"
44 #include "LocalizedStrings.h"
45 #include "MIMETypeRegistry.h"
46 #include "MouseEvent.h"
47 #include "Page.h"
48 #include "PaintInfo.h"
49 #include "Path.h"
50 #include "PlatformMouseEvent.h"
51 #include "PluginViewBase.h"
52 #include "RenderTheme.h"
53 #include "RenderView.h"
54 #include "RenderWidgetProtector.h"
55 #include "Settings.h"
56 #include "Text.h"
57 #include "TextRun.h"
58 #include <wtf/StackStats.h>
59
60 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
61 #include "HTMLMediaElement.h"
62 #endif
63
64 namespace WebCore {
65
66 using namespace HTMLNames;
67     
68 static const float replacementTextRoundedRectHeight = 18;
69 static const float replacementTextRoundedRectLeftRightTextMargin = 6;
70 static const float replacementTextRoundedRectOpacity = 0.20f;
71 static const float replacementTextPressedRoundedRectOpacity = 0.65f;
72 static const float replacementTextRoundedRectRadius = 5;
73 static const float replacementTextTextOpacity = 0.55f;
74 static const float replacementTextPressedTextOpacity = 0.65f;
75
76 static const Color& replacementTextRoundedRectPressedColor()
77 {
78     static const Color lightGray(205, 205, 205);
79     return lightGray;
80 }
81     
82 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
83     : RenderPart(element)
84     , m_hasFallbackContent(false)
85     , m_showsUnavailablePluginIndicator(false)
86     , m_unavailablePluginIndicatorIsPressed(false)
87     , m_mouseDownWasInUnavailablePluginIndicator(false)
88 {
89     view()->frameView()->setIsVisuallyNonEmpty();
90 }
91
92 RenderEmbeddedObject::~RenderEmbeddedObject()
93 {
94     if (frameView())
95         frameView()->removeWidgetToUpdate(this);
96 }
97
98 #if USE(ACCELERATED_COMPOSITING)
99 bool RenderEmbeddedObject::requiresLayer() const
100 {
101     if (RenderPart::requiresLayer())
102         return true;
103     
104     return allowsAcceleratedCompositing();
105 }
106
107 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
108 {
109     return widget() && widget()->isPluginViewBase() && toPluginViewBase(widget())->platformLayer();
110 }
111 #endif
112
113 static String unavailablePluginReplacementText(RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
114 {
115     switch (pluginUnavailabilityReason) {
116     case RenderEmbeddedObject::PluginMissing:
117         return missingPluginText();
118     case RenderEmbeddedObject::PluginCrashed:
119         return crashedPluginText();
120     case RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy:
121         return blockedPluginByContentSecurityPolicyText();
122     case RenderEmbeddedObject::InsecurePluginVersion:
123         return insecurePluginVersionText();
124     }
125
126     ASSERT_NOT_REACHED();
127     return String();
128 }
129
130 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
131 {
132     ASSERT(!m_showsUnavailablePluginIndicator);
133     m_showsUnavailablePluginIndicator = true;
134     m_pluginUnavailabilityReason = pluginUnavailabilityReason;
135
136     m_unavailablePluginReplacementText = unavailablePluginReplacementText(pluginUnavailabilityReason);
137 }
138
139 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
140 {
141     return m_showsUnavailablePluginIndicator;
142 }
143
144 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsPressed(bool pressed)
145 {
146     if (m_unavailablePluginIndicatorIsPressed == pressed)
147         return;
148     
149     m_unavailablePluginIndicatorIsPressed = pressed;
150     repaint();
151 }
152
153 void RenderEmbeddedObject::paintSnapshotImage(PaintInfo& paintInfo, const LayoutPoint& paintOffset, Image* image)
154 {
155     LayoutUnit cWidth = contentWidth();
156     LayoutUnit cHeight = contentHeight();
157     if (!cWidth || !cHeight)
158         return;
159
160     GraphicsContext* context = paintInfo.context;
161     LayoutSize contentSize(cWidth, cHeight);
162     LayoutPoint contentLocation = location() + paintOffset;
163     contentLocation.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
164
165     LayoutRect rect(contentLocation, contentSize);
166     IntRect alignedRect = pixelSnappedIntRect(rect);
167     if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
168         return;
169
170     bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size());
171     context->drawImage(image, style()->colorSpace(), alignedRect, CompositeSourceOver, shouldRespectImageOrientation(), useLowQualityScaling);
172 }
173
174 void RenderEmbeddedObject::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
175 {
176     Element* element = toElement(node());
177     if (!element || !element->isPluginElement())
178         return;
179
180     HTMLPlugInElement* plugInElement = toHTMLPlugInElement(element);
181
182     if (plugInElement->displayState() > HTMLPlugInElement::DisplayingSnapshot) {
183         RenderPart::paintContents(paintInfo, paintOffset);
184         if (!plugInElement->isRestartedPlugin())
185             return;
186     }
187
188     if (!plugInElement->isPlugInImageElement())
189         return;
190
191     Image* snapshot = toHTMLPlugInImageElement(plugInElement)->snapshotImage();
192     if (snapshot)
193         paintSnapshotImage(paintInfo, paintOffset, snapshot);
194 }
195
196 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
197 {
198     Page* page = 0;
199     if (Frame* frame = this->frame())
200         page = frame->page();
201
202     if (showsUnavailablePluginIndicator()) {
203         if (page && paintInfo.phase == PaintPhaseForeground)
204             page->addRelevantUnpaintedObject(this, visualOverflowRect());
205         RenderReplaced::paint(paintInfo, paintOffset);
206         return;
207     }
208
209     if (page && paintInfo.phase == PaintPhaseForeground)
210         page->addRelevantRepaintedObject(this, visualOverflowRect());
211
212     RenderPart::paint(paintInfo, paintOffset);
213 }
214
215 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
216 {
217     if (!showsUnavailablePluginIndicator())
218         return;
219
220     if (paintInfo.phase == PaintPhaseSelection)
221         return;
222     
223     GraphicsContext* context = paintInfo.context;
224     if (context->paintingDisabled())
225         return;
226     
227     FloatRect contentRect;
228     Path path;
229     FloatRect replacementTextRect;
230     Font font;
231     TextRun run("");
232     float textWidth;
233     if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
234         return;
235     
236     GraphicsContextStateSaver stateSaver(*context);
237     context->clip(contentRect);
238     context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedRoundedRectOpacity : replacementTextRoundedRectOpacity);
239     context->setFillColor(m_unavailablePluginIndicatorIsPressed ? replacementTextRoundedRectPressedColor() : Color::white, style()->colorSpace());
240     context->fillPath(path);
241
242     const FontMetrics& fontMetrics = font.fontMetrics();
243     float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
244     float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
245     context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedTextOpacity : replacementTextTextOpacity);
246     context->setFillColor(Color::black, style()->colorSpace());
247     context->drawBidiText(font, run, FloatPoint(labelX, labelY));
248 }
249
250 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
251 {
252     contentRect = contentBoxRect();
253     contentRect.moveBy(roundedIntPoint(accumulatedOffset));
254     
255     FontDescription fontDescription;
256     RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
257     fontDescription.setWeight(FontWeightBold);
258     Settings* settings = document()->settings();
259     ASSERT(settings);
260     if (!settings)
261         return false;
262     fontDescription.setRenderingMode(settings->fontRenderingMode());
263     fontDescription.setComputedSize(fontDescription.specifiedSize());
264     font = Font(fontDescription, 0, 0);
265     font.update(0);
266
267     run = TextRun(m_unavailablePluginReplacementText);
268     textWidth = font.width(run);
269     
270     replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
271     float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
272     float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
273     replacementTextRect.setLocation(FloatPoint(x, y));
274     
275     path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
276
277     return true;
278 }
279
280 void RenderEmbeddedObject::layout()
281 {
282     StackStats::LayoutCheckPoint layoutCheckPoint;
283     ASSERT(needsLayout());
284
285     LayoutSize oldSize = contentBoxRect().size();
286
287     updateLogicalWidth();
288     updateLogicalHeight();
289
290     RenderPart::layout();
291
292     m_overflow.clear();
293     addVisualEffectOverflow();
294
295     updateLayerTransform();
296
297     bool wasMissingWidget = false;
298     if (!widget() && frameView() && canHaveWidget()) {
299         wasMissingWidget = true;
300         frameView()->addWidgetToUpdate(this);
301     }
302
303     setNeedsLayout(false);
304
305     LayoutSize newSize = contentBoxRect().size();
306
307     if (!wasMissingWidget && newSize.width() >= oldSize.width() && newSize.height() >= oldSize.height()) {
308         Element* element = toElement(node());
309         if (element && element->isPluginElement() && toHTMLPlugInElement(element)->isPlugInImageElement()) {
310             HTMLPlugInImageElement* plugInImageElement = toHTMLPlugInImageElement(element);
311             if (plugInImageElement->displayState() > HTMLPlugInElement::DisplayingSnapshot && plugInImageElement->snapshotDecision() == HTMLPlugInImageElement::MaySnapshotWhenResized && document()->view()) {
312                 plugInImageElement->setNeedsCheckForSizeChange();
313                 document()->view()->addWidgetToUpdate(this);
314             }
315         }
316     }
317
318     if (!canHaveChildren())
319         return;
320
321     // This code copied from RenderMedia::layout().
322     RenderObject* child = m_children.firstChild();
323
324     if (!child)
325         return;
326
327     RenderBox* childBox = toRenderBox(child);
328
329     if (!childBox)
330         return;
331     
332     if (newSize == oldSize && !childBox->needsLayout())
333         return;
334     
335     // When calling layout() on a child node, a parent must either push a LayoutStateMaintainter, or
336     // instantiate LayoutStateDisabler. Since using a LayoutStateMaintainer is slightly more efficient,
337     // and this method will be called many times per second during playback, use a LayoutStateMaintainer:
338     LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
339     
340     childBox->setLocation(LayoutPoint(borderLeft(), borderTop()) + LayoutSize(paddingLeft(), paddingTop()));
341     childBox->style()->setHeight(Length(newSize.height(), Fixed));
342     childBox->style()->setWidth(Length(newSize.width(), Fixed));
343     childBox->setNeedsLayout(true, MarkOnlyThis);
344     childBox->layout();
345     setChildNeedsLayout(false);
346     
347     statePusher.pop();
348 }
349
350 void RenderEmbeddedObject::viewCleared()
351 {
352     // This is required for <object> elements whose contents are rendered by WebCore (e.g. src="foo.html").
353     if (node() && widget() && widget()->isFrameView()) {
354         FrameView* view = toFrameView(widget());
355         int marginWidth = -1;
356         int marginHeight = -1;
357         if (node()->hasTagName(iframeTag)) {
358             HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
359             marginWidth = frame->marginWidth();
360             marginHeight = frame->marginHeight();
361         }
362         if (marginWidth != -1)
363             view->setMarginWidth(marginWidth);
364         if (marginHeight != -1)
365             view->setMarginHeight(marginHeight);
366     }
367 }
368
369 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
370 {
371     if (!RenderPart::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction))
372         return false;
373
374     if (!widget() || !widget()->isPluginViewBase())
375         return true;
376
377     PluginViewBase* view = toPluginViewBase(widget());
378     IntPoint roundedPoint = locationInContainer.roundedPoint();
379
380     if (Scrollbar* horizontalScrollbar = view->horizontalScrollbar()) {
381         if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
382             result.setScrollbar(horizontalScrollbar);
383             return true;
384         }
385     }
386
387     if (Scrollbar* verticalScrollbar = view->verticalScrollbar()) {
388         if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
389             result.setScrollbar(verticalScrollbar);
390             return true;
391         }
392     }
393
394     return true;
395 }
396
397 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Node**)
398 {
399     if (!widget() || !widget()->isPluginViewBase())
400         return false;
401
402     return toPluginViewBase(widget())->scroll(direction, granularity);
403 }
404
405 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
406 {
407     // Plugins don't expose a writing direction, so assuming horizontal LTR.
408     return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopNode);
409 }
410
411
412 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const LayoutPoint& point) const
413 {
414     FloatRect contentRect;
415     Path path;
416     FloatRect replacementTextRect;
417     Font font;
418     TextRun run("");
419     float textWidth;
420     return getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth)
421         && path.contains(point);
422 }
423
424 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(MouseEvent* event) const
425 {
426     return isInUnavailablePluginIndicator(roundedLayoutPoint(absoluteToLocal(event->absoluteLocation(), UseTransforms)));
427 }
428
429 static bool shouldUnavailablePluginMessageBeButton(Document* document, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
430 {
431     Page* page = document->page();
432     return page && page->chrome().client()->shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
433 }
434
435 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
436 {
437     if (!shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason))
438         return;
439     
440     if (!event->isMouseEvent())
441         return;
442     
443     MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
444     HTMLPlugInElement* element = toHTMLPlugInElement(node());
445     if (event->type() == eventNames().mousedownEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
446         m_mouseDownWasInUnavailablePluginIndicator = isInUnavailablePluginIndicator(mouseEvent);
447         if (m_mouseDownWasInUnavailablePluginIndicator) {
448             if (Frame* frame = document()->frame()) {
449                 frame->eventHandler()->setCapturingMouseEventsNode(element);
450                 element->setIsCapturingMouseEvents(true);
451             }
452             setUnavailablePluginIndicatorIsPressed(true);
453         }
454         event->setDefaultHandled();
455     }        
456     if (event->type() == eventNames().mouseupEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
457         if (m_unavailablePluginIndicatorIsPressed) {
458             if (Frame* frame = document()->frame()) {
459                 frame->eventHandler()->setCapturingMouseEventsNode(0);
460                 element->setIsCapturingMouseEvents(false);
461             }
462             setUnavailablePluginIndicatorIsPressed(false);
463         }
464         if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
465             if (Page* page = document()->page())
466                 page->chrome().client()->unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
467         }
468         m_mouseDownWasInUnavailablePluginIndicator = false;
469         event->setDefaultHandled();
470     }
471     if (event->type() == eventNames().mousemoveEvent) {
472         setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
473         event->setDefaultHandled();
474     }
475 }
476
477 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
478 {
479     if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
480         cursor = handCursor();
481         return SetCursor;
482     }
483     return RenderPart::getCursor(point, cursor);
484 }
485
486 bool RenderEmbeddedObject::canHaveChildren() const
487 {
488 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
489     if (!node())
490         return false;
491
492     if (toElement(node())->isMediaElement())
493         return true;
494 #endif
495
496     if (isSnapshottedPlugIn())
497         return true;
498
499     return false;
500 }
501
502 }