9dac19598232353eddf077f5db0005d586b28ed7
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/RenderEmbeddedObject.h"
26
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"
44
45 namespace WebCore {
46
47 using namespace HTMLNames;
48
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;
54
55 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
56     : RenderPart(element)
57     , m_hasFallbackContent(false)
58     , m_showsUnavailablePluginIndicator(false)
59 {
60     view()->frameView()->setIsVisuallyNonEmpty();
61 }
62
63 RenderEmbeddedObject::~RenderEmbeddedObject()
64 {
65 }
66
67 LayerType RenderEmbeddedObject::layerTypeRequired() const
68 {
69     LayerType type = RenderPart::layerTypeRequired();
70     if (type != NoLayer)
71         return type;
72
73     return allowsAcceleratedCompositing() ? NormalLayer : NoLayer;
74 }
75
76 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
77 {
78     return widget() && widget()->isPluginView() && toPluginView(widget())->platformLayer();
79 }
80
81 static String unavailablePluginReplacementText(Node* node, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
82 {
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);
89     }
90
91     ASSERT_NOT_REACHED();
92     return String();
93 }
94
95 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
96 {
97     ASSERT(!m_showsUnavailablePluginIndicator);
98     m_showsUnavailablePluginIndicator = true;
99     m_pluginUnavailabilityReason = pluginUnavailabilityReason;
100
101     m_unavailablePluginReplacementText = unavailablePluginReplacementText(node(), pluginUnavailabilityReason);
102 }
103
104 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
105 {
106     return m_showsUnavailablePluginIndicator;
107 }
108
109 void RenderEmbeddedObject::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
110 {
111     Element* element = toElement(node());
112     if (!element || !element->isPluginElement())
113         return;
114
115     RenderPart::paintContents(paintInfo, paintOffset);
116 }
117
118 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
119 {
120     if (showsUnavailablePluginIndicator()) {
121         RenderReplaced::paint(paintInfo, paintOffset);
122         return;
123     }
124
125     RenderPart::paint(paintInfo, paintOffset);
126 }
127
128 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
129 {
130     if (!showsUnavailablePluginIndicator())
131         return;
132
133     if (paintInfo.phase == PaintPhaseSelection)
134         return;
135
136     GraphicsContext* context = paintInfo.context;
137     if (context->paintingDisabled())
138         return;
139
140     FloatRect contentRect;
141     Path path;
142     FloatRect replacementTextRect;
143     Font font;
144     TextRun run("");
145     float textWidth;
146     if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
147         return;
148
149     GraphicsContextStateSaver stateSaver(*context);
150     context->clip(contentRect);
151     context->setAlpha(replacementTextRoundedRectOpacity);
152     context->setFillColor(Color::white);
153     context->fillPath(path);
154
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));
163 }
164
165 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
166 {
167     contentRect = contentBoxRect();
168     contentRect.moveBy(roundedIntPoint(accumulatedOffset));
169
170     FontDescription fontDescription;
171     RenderTheme::theme().systemFont(CSSValueWebkitSmallControl, fontDescription);
172     fontDescription.setWeight(FontWeightBold);
173     Settings* settings = document().settings();
174     ASSERT(settings);
175     if (!settings)
176         return false;
177     fontDescription.setComputedSize(fontDescription.specifiedSize());
178     font = Font(fontDescription);
179     font.update(0);
180
181     run = TextRun(m_unavailablePluginReplacementText);
182     textWidth = font.width(run);
183
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));
188
189     path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
190
191     return true;
192 }
193
194 void RenderEmbeddedObject::layout()
195 {
196     ASSERT(needsLayout());
197
198     LayoutSize oldSize = contentBoxRect().size();
199     LayoutRectRecorder recorder(*this);
200
201     updateLogicalWidth();
202     updateLogicalHeight();
203
204     RenderPart::layout();
205
206     m_overflow.clear();
207     addVisualEffectOverflow();
208
209     updateLayerTransform();
210
211     if (!widget() && frameView())
212         frameView()->addWidgetToUpdate(*this);
213
214     clearNeedsLayout();
215
216     if (!canHaveChildren())
217         return;
218
219     // This code copied from RenderMedia::layout().
220     RenderObject* child = m_children.firstChild();
221
222     if (!child)
223         return;
224
225     RenderBox* childBox = toRenderBox(child);
226
227     if (!childBox)
228         return;
229
230     LayoutSize newSize = contentBoxRect().size();
231     if (newSize == oldSize && !childBox->needsLayout())
232         return;
233
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());
238
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();
243     clearNeedsLayout();
244
245     statePusher.pop();
246 }
247
248 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float)
249 {
250     return false;
251 }
252
253 bool RenderEmbeddedObject::canHaveChildren() const
254 {
255     return false;
256 }
257
258 }