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 "core/rendering/RenderEmbeddedObject.h"
27 #include "CSSValueKeywords.h"
28 #include "HTMLNames.h"
29 #include "core/html/HTMLIFrameElement.h"
30 #include "core/frame/Frame.h"
31 #include "core/page/Page.h"
32 #include "core/frame/Settings.h"
33 #include "core/plugins/PluginView.h"
34 #include "core/rendering/LayoutRectRecorder.h"
35 #include "core/rendering/PaintInfo.h"
36 #include "core/rendering/RenderTheme.h"
37 #include "core/rendering/RenderView.h"
38 #include "platform/fonts/Font.h"
39 #include "platform/fonts/FontSelector.h"
40 #include "platform/graphics/GraphicsContextStateSaver.h"
41 #include "platform/graphics/Path.h"
42 #include "platform/text/PlatformLocale.h"
43 #include "platform/text/TextRun.h"
47 using namespace HTMLNames;
49 static const float replacementTextRoundedRectHeight = 18;
50 static const float replacementTextRoundedRectLeftRightTextMargin = 6;
51 static const float replacementTextRoundedRectOpacity = 0.20f;
52 static const float replacementTextRoundedRectRadius = 5;
53 static const float replacementTextTextOpacity = 0.55f;
55 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
57 , m_hasFallbackContent(false)
58 , m_showsUnavailablePluginIndicator(false)
60 view()->frameView()->setIsVisuallyNonEmpty();
63 RenderEmbeddedObject::~RenderEmbeddedObject()
67 LayerType RenderEmbeddedObject::layerTypeRequired() const
69 LayerType type = RenderPart::layerTypeRequired();
73 return allowsAcceleratedCompositing() ? NormalLayer : NoLayer;
76 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
78 return widget() && widget()->isPluginView() && toPluginView(widget())->platformLayer();
81 static String unavailablePluginReplacementText(Node* node, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
83 Locale& locale = node ? toElement(node)->locale() : Locale::defaultLocale();
84 switch (pluginUnavailabilityReason) {
85 case RenderEmbeddedObject::PluginMissing:
86 return locale.queryString(blink::WebLocalizedString::MissingPluginText);
87 case RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy:
88 return locale.queryString(blink::WebLocalizedString::BlockedPluginText);
95 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
97 ASSERT(!m_showsUnavailablePluginIndicator);
98 m_showsUnavailablePluginIndicator = true;
99 m_pluginUnavailabilityReason = pluginUnavailabilityReason;
101 m_unavailablePluginReplacementText = unavailablePluginReplacementText(node(), pluginUnavailabilityReason);
104 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
106 return m_showsUnavailablePluginIndicator;
109 void RenderEmbeddedObject::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
111 Element* element = toElement(node());
112 if (!element || !element->isPluginElement())
115 RenderPart::paintContents(paintInfo, paintOffset);
118 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
120 if (showsUnavailablePluginIndicator()) {
121 RenderReplaced::paint(paintInfo, paintOffset);
125 RenderPart::paint(paintInfo, paintOffset);
128 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
130 if (!showsUnavailablePluginIndicator())
133 if (paintInfo.phase == PaintPhaseSelection)
136 GraphicsContext* context = paintInfo.context;
137 if (context->paintingDisabled())
140 FloatRect contentRect;
142 FloatRect replacementTextRect;
146 if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
149 GraphicsContextStateSaver stateSaver(*context);
150 context->clip(contentRect);
151 context->setAlpha(replacementTextRoundedRectOpacity);
152 context->setFillColor(Color::white);
153 context->fillPath(path);
155 const FontMetrics& fontMetrics = font.fontMetrics();
156 float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
157 float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
158 TextRunPaintInfo runInfo(run);
159 runInfo.bounds = replacementTextRect;
160 context->setAlpha(replacementTextTextOpacity);
161 context->setFillColor(Color::black);
162 context->drawBidiText(font, runInfo, FloatPoint(labelX, labelY));
165 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
167 contentRect = contentBoxRect();
168 contentRect.moveBy(roundedIntPoint(accumulatedOffset));
170 FontDescription fontDescription;
171 RenderTheme::theme().systemFont(CSSValueWebkitSmallControl, fontDescription);
172 fontDescription.setWeight(FontWeightBold);
173 Settings* settings = document().settings();
177 fontDescription.setComputedSize(fontDescription.specifiedSize());
178 font = Font(fontDescription);
181 run = TextRun(m_unavailablePluginReplacementText);
182 textWidth = font.width(run);
184 replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
185 float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
186 float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
187 replacementTextRect.setLocation(FloatPoint(x, y));
189 path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
194 void RenderEmbeddedObject::layout()
196 ASSERT(needsLayout());
198 LayoutSize oldSize = contentBoxRect().size();
199 LayoutRectRecorder recorder(*this);
201 updateLogicalWidth();
202 updateLogicalHeight();
204 RenderPart::layout();
207 addVisualEffectOverflow();
209 updateLayerTransform();
211 if (!widget() && frameView())
212 frameView()->addWidgetToUpdate(*this);
216 if (!canHaveChildren())
219 // This code copied from RenderMedia::layout().
220 RenderObject* child = m_children.firstChild();
225 RenderBox* childBox = toRenderBox(child);
230 LayoutSize newSize = contentBoxRect().size();
231 if (newSize == oldSize && !childBox->needsLayout())
234 // When calling layout() on a child node, a parent must either push a LayoutStateMaintainter, or
235 // instantiate LayoutStateDisabler. Since using a LayoutStateMaintainer is slightly more efficient,
236 // and this method will be called many times per second during playback, use a LayoutStateMaintainer:
237 LayoutStateMaintainer statePusher(view(), this, locationOffset(), hasTransform() || hasReflection() || style()->isFlippedBlocksWritingMode());
239 childBox->setLocation(LayoutPoint(borderLeft(), borderTop()) + LayoutSize(paddingLeft(), paddingTop()));
240 childBox->style()->setHeight(Length(newSize.height(), Fixed));
241 childBox->style()->setWidth(Length(newSize.width(), Fixed));
242 childBox->forceLayout();
248 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float)
253 bool RenderEmbeddedObject::canHaveChildren() const