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.
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.
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.
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.
25 #include "RenderEmbeddedObject.h"
28 #include "ChromeClient.h"
30 #include "CSSValueKeywords.h"
32 #include "FontSelector.h"
34 #include "FrameLoaderClient.h"
35 #include "GraphicsContext.h"
36 #include "HTMLEmbedElement.h"
37 #include "HTMLIFrameElement.h"
38 #include "HTMLNames.h"
39 #include "HTMLObjectElement.h"
40 #include "HTMLParamElement.h"
41 #include "HTMLPlugInElement.h"
42 #include "HitTestResult.h"
43 #include "LocalizedStrings.h"
44 #include "MIMETypeRegistry.h"
45 #include "MouseEvent.h"
47 #include "PaintInfo.h"
49 #include "PluginViewBase.h"
50 #include "RenderTheme.h"
51 #include "RenderView.h"
52 #include "RenderWidgetProtector.h"
57 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
58 #include "HTMLMediaElement.h"
63 using namespace HTMLNames;
65 static const float replacementTextRoundedRectHeight = 18;
66 static const float replacementTextRoundedRectLeftRightTextMargin = 6;
67 static const float replacementTextRoundedRectOpacity = 0.20f;
68 static const float replacementTextPressedRoundedRectOpacity = 0.65f;
69 static const float replacementTextRoundedRectRadius = 5;
70 static const float replacementTextTextOpacity = 0.55f;
71 static const float replacementTextPressedTextOpacity = 0.65f;
73 static const Color& replacementTextRoundedRectPressedColor()
75 static const Color lightGray(205, 205, 205);
79 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
81 , m_hasFallbackContent(false)
82 , m_showsUnavailablePluginIndicator(false)
83 , m_unavailablePluginIndicatorIsPressed(false)
84 , m_mouseDownWasInUnavailablePluginIndicator(false)
86 view()->frameView()->setIsVisuallyNonEmpty();
89 RenderEmbeddedObject::~RenderEmbeddedObject()
92 frameView()->removeWidgetToUpdate(this);
95 #if USE(ACCELERATED_COMPOSITING)
96 bool RenderEmbeddedObject::requiresLayer() const
98 if (RenderPart::requiresLayer())
101 return allowsAcceleratedCompositing();
104 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
106 return widget() && widget()->isPluginViewBase() && toPluginViewBase(widget())->platformLayer();
110 static String unavailablePluginReplacementText(RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
112 switch (pluginUnavailabilityReason) {
113 case RenderEmbeddedObject::PluginMissing:
114 return missingPluginText();
115 case RenderEmbeddedObject::PluginCrashed:
116 return crashedPluginText();
117 case RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy:
118 return blockedPluginByContentSecurityPolicyText();
119 case RenderEmbeddedObject::InsecurePluginVersion:
120 return insecurePluginVersionText();
121 case RenderEmbeddedObject::PluginInactive:
122 return inactivePluginText();
125 ASSERT_NOT_REACHED();
129 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
131 ASSERT(!m_showsUnavailablePluginIndicator);
132 m_showsUnavailablePluginIndicator = true;
133 m_pluginUnavailabilityReason = pluginUnavailabilityReason;
135 m_unavailablePluginReplacementText = unavailablePluginReplacementText(pluginUnavailabilityReason);
138 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
140 return m_showsUnavailablePluginIndicator;
143 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsPressed(bool pressed)
145 if (m_unavailablePluginIndicatorIsPressed == pressed)
148 m_unavailablePluginIndicatorIsPressed = pressed;
152 void RenderEmbeddedObject::paintSnapshotImage(PaintInfo& paintInfo, const LayoutPoint& paintOffset, Image* image)
154 LayoutUnit cWidth = contentWidth();
155 LayoutUnit cHeight = contentHeight();
156 if (!cWidth || !cHeight)
159 GraphicsContext* context = paintInfo.context;
160 LayoutSize contentSize(cWidth, cHeight);
161 LayoutPoint contentLocation = location() + paintOffset;
162 contentLocation.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
164 LayoutRect rect(contentLocation, contentSize);
165 IntRect alignedRect = pixelSnappedIntRect(rect);
166 if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
169 bool useLowQualityScaling = shouldPaintAtLowQuality(context, image, image, alignedRect.size());
170 context->drawImage(image, style()->colorSpace(), alignedRect, CompositeSourceOver, shouldRespectImageOrientation(), useLowQualityScaling);
173 void RenderEmbeddedObject::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
175 Element* element = toElement(node());
176 if (!element || !element->isPluginElement())
179 HTMLPlugInElement* plugInElement = toHTMLPlugInElement(element);
181 if (plugInElement->displayState() > HTMLPlugInElement::DisplayingSnapshot) {
182 RenderPart::paintContents(paintInfo, paintOffset);
183 if (!plugInElement->restartedPlugin())
187 if (!plugInElement->isPlugInImageElement())
190 Image* snapshot = toHTMLPlugInImageElement(plugInElement)->snapshotImage();
192 paintSnapshotImage(paintInfo, paintOffset, snapshot);
195 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
198 if (Frame* frame = this->frame())
199 page = frame->page();
201 if (showsUnavailablePluginIndicator()) {
202 if (page && paintInfo.phase == PaintPhaseForeground)
203 page->addRelevantUnpaintedObject(this, visualOverflowRect());
204 RenderReplaced::paint(paintInfo, paintOffset);
208 if (page && paintInfo.phase == PaintPhaseForeground)
209 page->addRelevantRepaintedObject(this, visualOverflowRect());
211 RenderPart::paint(paintInfo, paintOffset);
214 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
216 if (!showsUnavailablePluginIndicator())
219 if (paintInfo.phase == PaintPhaseSelection)
222 GraphicsContext* context = paintInfo.context;
223 if (context->paintingDisabled())
226 FloatRect contentRect;
228 FloatRect replacementTextRect;
232 if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
235 GraphicsContextStateSaver stateSaver(*context);
236 context->clip(contentRect);
237 context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedRoundedRectOpacity : replacementTextRoundedRectOpacity);
238 context->setFillColor(m_unavailablePluginIndicatorIsPressed ? replacementTextRoundedRectPressedColor() : Color::white, style()->colorSpace());
239 context->fillPath(path);
241 const FontMetrics& fontMetrics = font.fontMetrics();
242 float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
243 float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
244 context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedTextOpacity : replacementTextTextOpacity);
245 context->setFillColor(Color::black, style()->colorSpace());
246 context->drawBidiText(font, run, FloatPoint(labelX, labelY));
249 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
251 contentRect = contentBoxRect();
252 contentRect.moveBy(roundedIntPoint(accumulatedOffset));
254 FontDescription fontDescription;
255 RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
256 fontDescription.setWeight(FontWeightBold);
257 Settings* settings = document()->settings();
261 fontDescription.setRenderingMode(settings->fontRenderingMode());
262 fontDescription.setComputedSize(fontDescription.specifiedSize());
263 font = Font(fontDescription, 0, 0);
266 run = TextRun(m_unavailablePluginReplacementText);
267 textWidth = font.width(run);
269 replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
270 float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
271 float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
272 replacementTextRect.setLocation(FloatPoint(x, y));
274 path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
279 void RenderEmbeddedObject::layout()
281 StackStats::LayoutCheckPoint layoutCheckPoint;
282 ASSERT(needsLayout());
284 LayoutSize oldSize = contentBoxRect().size();
286 updateLogicalWidth();
287 updateLogicalHeight();
289 RenderPart::layout();
292 addVisualEffectOverflow();
294 updateLayerTransform();
296 bool wasMissingWidget = false;
297 if (!widget() && frameView() && canHaveWidget()) {
298 wasMissingWidget = true;
299 frameView()->addWidgetToUpdate(this);
302 setNeedsLayout(false);
304 LayoutSize newSize = contentBoxRect().size();
306 if (!wasMissingWidget && newSize.width() >= oldSize.width() && newSize.height() >= oldSize.height()) {
307 Element* element = toElement(node());
308 if (element && element->isPluginElement() && toHTMLPlugInElement(element)->isPlugInImageElement()) {
309 HTMLPlugInImageElement* plugInImageElement = toHTMLPlugInImageElement(element);
310 if (plugInImageElement->displayState() > HTMLPlugInElement::DisplayingSnapshot && plugInImageElement->snapshotDecision() == HTMLPlugInImageElement::MaySnapshotWhenResized && document()->view()) {
311 plugInImageElement->setNeedsCheckForSizeChange();
312 document()->view()->addWidgetToUpdate(this);
317 if (!canHaveChildren())
320 // This code copied from RenderMedia::layout().
321 RenderObject* child = m_children.firstChild();
326 RenderBox* childBox = toRenderBox(child);
331 if (newSize == oldSize && !childBox->needsLayout())
334 // When calling layout() on a child node, a parent must either push a LayoutStateMaintainter, or
335 // instantiate LayoutStateDisabler. Since using a LayoutStateMaintainer is slightly more efficient,
336 // and this method will be called many times per second during playback, use a LayoutStateMaintainer:
337 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
339 childBox->setLocation(LayoutPoint(borderLeft(), borderTop()) + LayoutSize(paddingLeft(), paddingTop()));
340 childBox->style()->setHeight(Length(newSize.height(), Fixed));
341 childBox->style()->setWidth(Length(newSize.width(), Fixed));
342 childBox->setNeedsLayout(true, MarkOnlyThis);
344 setChildNeedsLayout(false);
349 void RenderEmbeddedObject::viewCleared()
351 // This is required for <object> elements whose contents are rendered by WebCore (e.g. src="foo.html").
352 if (node() && widget() && widget()->isFrameView()) {
353 FrameView* view = toFrameView(widget());
354 int marginWidth = -1;
355 int marginHeight = -1;
356 if (node()->hasTagName(iframeTag)) {
357 HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
358 marginWidth = frame->marginWidth();
359 marginHeight = frame->marginHeight();
361 if (marginWidth != -1)
362 view->setMarginWidth(marginWidth);
363 if (marginHeight != -1)
364 view->setMarginHeight(marginHeight);
368 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
370 if (!RenderPart::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction))
373 if (!widget() || !widget()->isPluginViewBase())
376 PluginViewBase* view = toPluginViewBase(widget());
377 IntPoint roundedPoint = locationInContainer.roundedPoint();
379 if (Scrollbar* horizontalScrollbar = view->horizontalScrollbar()) {
380 if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
381 result.setScrollbar(horizontalScrollbar);
386 if (Scrollbar* verticalScrollbar = view->verticalScrollbar()) {
387 if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
388 result.setScrollbar(verticalScrollbar);
396 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Node**)
398 if (!widget() || !widget()->isPluginViewBase())
401 return toPluginViewBase(widget())->scroll(direction, granularity);
404 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
406 // Plugins don't expose a writing direction, so assuming horizontal LTR.
407 return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopNode);
411 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const LayoutPoint& point) const
413 FloatRect contentRect;
415 FloatRect replacementTextRect;
419 return getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth)
420 && path.contains(point);
423 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(MouseEvent* event) const
425 return isInUnavailablePluginIndicator(roundedLayoutPoint(absoluteToLocal(event->absoluteLocation(), UseTransforms)));
428 static bool shouldUnavailablePluginMessageBeButton(Document* document, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
430 Page* page = document->page();
431 return page && page->chrome()->client()->shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
434 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
436 if (!shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason))
439 if (!event->isMouseEvent())
442 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
443 HTMLPlugInElement* element = toHTMLPlugInElement(node());
444 if (event->type() == eventNames().mousedownEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
445 m_mouseDownWasInUnavailablePluginIndicator = isInUnavailablePluginIndicator(mouseEvent);
446 if (m_mouseDownWasInUnavailablePluginIndicator) {
447 if (Frame* frame = document()->frame()) {
448 frame->eventHandler()->setCapturingMouseEventsNode(element);
449 element->setIsCapturingMouseEvents(true);
451 setUnavailablePluginIndicatorIsPressed(true);
453 event->setDefaultHandled();
455 if (event->type() == eventNames().mouseupEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
456 if (m_unavailablePluginIndicatorIsPressed) {
457 if (Frame* frame = document()->frame()) {
458 frame->eventHandler()->setCapturingMouseEventsNode(0);
459 element->setIsCapturingMouseEvents(false);
461 setUnavailablePluginIndicatorIsPressed(false);
463 if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
464 if (Page* page = document()->page())
465 page->chrome()->client()->unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
467 m_mouseDownWasInUnavailablePluginIndicator = false;
468 event->setDefaultHandled();
470 if (event->type() == eventNames().mousemoveEvent) {
471 setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
472 event->setDefaultHandled();
476 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
478 if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
479 cursor = handCursor();
482 return RenderPart::getCursor(point, cursor);
485 bool RenderEmbeddedObject::canHaveChildren() const
487 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
491 if (toElement(node())->isMediaElement())
495 if (isSnapshottedPlugIn())