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 "HitTestResult.h"
42 #include "LocalizedStrings.h"
43 #include "MIMETypeRegistry.h"
44 #include "MouseEvent.h"
46 #include "PaintInfo.h"
48 #include "PluginViewBase.h"
49 #include "RenderTheme.h"
50 #include "RenderView.h"
51 #include "RenderWidgetProtector.h"
58 using namespace HTMLNames;
60 static const float replacementTextRoundedRectHeight = 18;
61 static const float replacementTextRoundedRectLeftRightTextMargin = 6;
62 static const float replacementTextRoundedRectOpacity = 0.20f;
63 static const float replacementTextPressedRoundedRectOpacity = 0.65f;
64 static const float replacementTextRoundedRectRadius = 5;
65 static const float replacementTextTextOpacity = 0.55f;
66 static const float replacementTextPressedTextOpacity = 0.65f;
68 static const Color& replacementTextRoundedRectPressedColor()
70 static const Color lightGray(205, 205, 205);
74 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
76 , m_hasFallbackContent(false)
77 , m_showsUnavailablePluginIndicator(false)
78 , m_unavailablePluginIndicatorIsPressed(false)
79 , m_mouseDownWasInUnavailablePluginIndicator(false)
81 view()->frameView()->setIsVisuallyNonEmpty();
84 RenderEmbeddedObject::~RenderEmbeddedObject()
87 frameView()->removeWidgetToUpdate(this);
90 #if USE(ACCELERATED_COMPOSITING)
91 bool RenderEmbeddedObject::requiresLayer() const
93 if (RenderPart::requiresLayer())
96 return allowsAcceleratedCompositing();
99 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
101 return widget() && widget()->isPluginViewBase() && static_cast<PluginViewBase*>(widget())->platformLayer();
105 static String unavailablePluginReplacementText(RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
107 switch (pluginUnavailabilityReason) {
108 case RenderEmbeddedObject::PluginMissing:
109 return missingPluginText();
110 case RenderEmbeddedObject::PluginCrashed:
111 return crashedPluginText();
112 case RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy:
113 return blockedPluginByContentSecurityPolicyText();
114 case RenderEmbeddedObject::InsecurePluginVersion:
115 return insecurePluginVersionText();
118 ASSERT_NOT_REACHED();
122 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
124 ASSERT(!m_showsUnavailablePluginIndicator);
125 m_showsUnavailablePluginIndicator = true;
126 m_pluginUnavailabilityReason = pluginUnavailabilityReason;
128 m_unavailablePluginReplacementText = unavailablePluginReplacementText(pluginUnavailabilityReason);
131 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
133 return m_showsUnavailablePluginIndicator;
136 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsPressed(bool pressed)
138 if (m_unavailablePluginIndicatorIsPressed == pressed)
141 m_unavailablePluginIndicatorIsPressed = pressed;
145 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
148 if (Frame* frame = this->frame())
149 page = frame->page();
151 if (showsUnavailablePluginIndicator()) {
152 if (page && paintInfo.phase == PaintPhaseForeground)
153 page->addRelevantUnpaintedObject(this, visualOverflowRect());
154 RenderReplaced::paint(paintInfo, paintOffset);
158 if (page && paintInfo.phase == PaintPhaseForeground)
159 page->addRelevantRepaintedObject(this, visualOverflowRect());
161 RenderPart::paint(paintInfo, paintOffset);
164 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
166 if (!showsUnavailablePluginIndicator())
169 if (paintInfo.phase == PaintPhaseSelection)
172 GraphicsContext* context = paintInfo.context;
173 if (context->paintingDisabled())
176 FloatRect contentRect;
178 FloatRect replacementTextRect;
182 if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
185 GraphicsContextStateSaver stateSaver(*context);
186 context->clip(contentRect);
187 context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedRoundedRectOpacity : replacementTextRoundedRectOpacity);
188 context->setFillColor(m_unavailablePluginIndicatorIsPressed ? replacementTextRoundedRectPressedColor() : Color::white, style()->colorSpace());
189 context->fillPath(path);
191 #if ENABLE(TIZEN_PAINT_MISSING_PLUGIN_RECT)
192 context->setFillColor(Color::gray, style()->colorSpace());
193 context->fillRect(contentRect);
196 const FontMetrics& fontMetrics = font.fontMetrics();
197 float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
198 float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
199 context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedTextOpacity : replacementTextTextOpacity);
200 context->setFillColor(Color::black, style()->colorSpace());
201 context->drawBidiText(font, run, FloatPoint(labelX, labelY));
204 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
206 contentRect = contentBoxRect();
207 contentRect.moveBy(roundedIntPoint(accumulatedOffset));
209 FontDescription fontDescription;
210 RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
211 fontDescription.setWeight(FontWeightBold);
212 Settings* settings = document()->settings();
216 fontDescription.setRenderingMode(settings->fontRenderingMode());
217 fontDescription.setComputedSize(fontDescription.specifiedSize());
218 font = Font(fontDescription, 0, 0);
221 run = TextRun(m_unavailablePluginReplacementText);
222 textWidth = font.width(run);
224 replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
225 float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
226 float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
227 replacementTextRect.setLocation(FloatPoint(x, y));
229 path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
234 void RenderEmbeddedObject::layout()
236 ASSERT(needsLayout());
238 computeLogicalWidth();
239 computeLogicalHeight();
241 RenderPart::layout();
244 addVisualEffectOverflow();
246 updateLayerTransform();
248 if (!widget() && frameView())
249 frameView()->addWidgetToUpdate(this);
251 setNeedsLayout(false);
254 void RenderEmbeddedObject::viewCleared()
256 // This is required for <object> elements whose contents are rendered by WebCore (e.g. src="foo.html").
257 if (node() && widget() && widget()->isFrameView()) {
258 FrameView* view = static_cast<FrameView*>(widget());
259 int marginWidth = -1;
260 int marginHeight = -1;
261 if (node()->hasTagName(iframeTag)) {
262 HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
263 marginWidth = frame->marginWidth();
264 marginHeight = frame->marginHeight();
266 if (marginWidth != -1)
267 view->setMarginWidth(marginWidth);
268 if (marginHeight != -1)
269 view->setMarginHeight(marginHeight);
273 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
275 if (!RenderPart::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, hitTestAction))
278 if (!widget() || !widget()->isPluginViewBase())
281 PluginViewBase* view = static_cast<PluginViewBase*>(widget());
282 IntPoint roundedPoint = pointInContainer.roundedPoint();
284 if (Scrollbar* horizontalScrollbar = view->horizontalScrollbar()) {
285 if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
286 result.setScrollbar(horizontalScrollbar);
291 if (Scrollbar* verticalScrollbar = view->verticalScrollbar()) {
292 if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
293 result.setScrollbar(verticalScrollbar);
301 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Node**)
303 if (!widget() || !widget()->isPluginViewBase())
306 return static_cast<PluginViewBase*>(widget())->scroll(direction, granularity);
309 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
311 // Plugins don't expose a writing direction, so assuming horizontal LTR.
312 return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopNode);
316 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const LayoutPoint& point) const
318 FloatRect contentRect;
320 FloatRect replacementTextRect;
324 return getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth)
325 && path.contains(point);
328 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(MouseEvent* event) const
330 return isInUnavailablePluginIndicator(roundedLayoutPoint(absoluteToLocal(event->absoluteLocation(), false, true)));
333 static bool shouldUnavailablePluginMessageBeButton(Document* document, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
335 Page* page = document->page();
336 return page && page->chrome()->client()->shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
339 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
341 if (!shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason))
344 if (!event->isMouseEvent())
347 MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
348 HTMLPlugInElement* element = static_cast<HTMLPlugInElement*>(node());
349 if (event->type() == eventNames().mousedownEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
350 m_mouseDownWasInUnavailablePluginIndicator = isInUnavailablePluginIndicator(mouseEvent);
351 if (m_mouseDownWasInUnavailablePluginIndicator) {
352 if (Frame* frame = document()->frame()) {
353 frame->eventHandler()->setCapturingMouseEventsNode(element);
354 element->setIsCapturingMouseEvents(true);
356 setUnavailablePluginIndicatorIsPressed(true);
358 event->setDefaultHandled();
360 if (event->type() == eventNames().mouseupEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
361 if (m_unavailablePluginIndicatorIsPressed) {
362 if (Frame* frame = document()->frame()) {
363 frame->eventHandler()->setCapturingMouseEventsNode(0);
364 element->setIsCapturingMouseEvents(false);
366 setUnavailablePluginIndicatorIsPressed(false);
368 if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
369 if (Page* page = document()->page())
370 page->chrome()->client()->unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
372 m_mouseDownWasInUnavailablePluginIndicator = false;
373 event->setDefaultHandled();
375 if (event->type() == eventNames().mousemoveEvent) {
376 setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
377 event->setDefaultHandled();
381 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
383 if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
384 cursor = handCursor();
387 return RenderPart::getCursor(point, cursor);