Added WTF::StackStats mechanism.
[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 "Chrome.h"
28 #include "ChromeClient.h"
29 #include "Cursor.h"
30 #include "CSSValueKeywords.h"
31 #include "Font.h"
32 #include "FontSelector.h"
33 #include "Frame.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"
45 #include "Page.h"
46 #include "PaintInfo.h"
47 #include "Path.h"
48 #include "PluginViewBase.h"
49 #include "RenderTheme.h"
50 #include "RenderView.h"
51 #include "RenderWidgetProtector.h"
52 #include "Settings.h"
53 #include "Text.h"
54 #include "TextRun.h"
55
56 namespace WebCore {
57
58 using namespace HTMLNames;
59     
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;
67
68 static const Color& replacementTextRoundedRectPressedColor()
69 {
70     static const Color lightGray(205, 205, 205);
71     return lightGray;
72 }
73     
74 RenderEmbeddedObject::RenderEmbeddedObject(Element* element)
75     : RenderPart(element)
76     , m_hasFallbackContent(false)
77     , m_showsUnavailablePluginIndicator(false)
78     , m_unavailablePluginIndicatorIsPressed(false)
79     , m_mouseDownWasInUnavailablePluginIndicator(false)
80 {
81     view()->frameView()->setIsVisuallyNonEmpty();
82 }
83
84 RenderEmbeddedObject::~RenderEmbeddedObject()
85 {
86     if (frameView())
87         frameView()->removeWidgetToUpdate(this);
88 }
89
90 #if USE(ACCELERATED_COMPOSITING)
91 bool RenderEmbeddedObject::requiresLayer() const
92 {
93     if (RenderPart::requiresLayer())
94         return true;
95     
96     return allowsAcceleratedCompositing();
97 }
98
99 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
100 {
101     return widget() && widget()->isPluginViewBase() && static_cast<PluginViewBase*>(widget())->platformLayer();
102 }
103 #endif
104
105 static String unavailablePluginReplacementText(RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
106 {
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();
116     }
117
118     ASSERT_NOT_REACHED();
119     return String();
120 }
121
122 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
123 {
124     ASSERT(!m_showsUnavailablePluginIndicator);
125     m_showsUnavailablePluginIndicator = true;
126     m_pluginUnavailabilityReason = pluginUnavailabilityReason;
127
128     m_unavailablePluginReplacementText = unavailablePluginReplacementText(pluginUnavailabilityReason);
129 }
130
131 bool RenderEmbeddedObject::showsUnavailablePluginIndicator() const
132 {
133     return m_showsUnavailablePluginIndicator;
134 }
135
136 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsPressed(bool pressed)
137 {
138     if (m_unavailablePluginIndicatorIsPressed == pressed)
139         return;
140     
141     m_unavailablePluginIndicatorIsPressed = pressed;
142     repaint();
143 }
144
145 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
146 {
147     Page* page = 0;
148     if (Frame* frame = this->frame())
149         page = frame->page();
150
151     if (showsUnavailablePluginIndicator()) {
152         if (page && paintInfo.phase == PaintPhaseForeground)
153             page->addRelevantUnpaintedObject(this, visualOverflowRect());
154         RenderReplaced::paint(paintInfo, paintOffset);
155         return;
156     }
157
158     if (page && paintInfo.phase == PaintPhaseForeground)
159         page->addRelevantRepaintedObject(this, visualOverflowRect());
160
161     RenderPart::paint(paintInfo, paintOffset);
162 }
163
164 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
165 {
166     if (!showsUnavailablePluginIndicator())
167         return;
168
169     if (paintInfo.phase == PaintPhaseSelection)
170         return;
171     
172     GraphicsContext* context = paintInfo.context;
173     if (context->paintingDisabled())
174         return;
175     
176     FloatRect contentRect;
177     Path path;
178     FloatRect replacementTextRect;
179     Font font;
180     TextRun run("");
181     float textWidth;
182     if (!getReplacementTextGeometry(paintOffset, contentRect, path, replacementTextRect, font, run, textWidth))
183         return;
184     
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);
190
191     const FontMetrics& fontMetrics = font.fontMetrics();
192     float labelX = roundf(replacementTextRect.location().x() + (replacementTextRect.size().width() - textWidth) / 2);
193     float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent());
194     context->setAlpha(m_unavailablePluginIndicatorIsPressed ? replacementTextPressedTextOpacity : replacementTextTextOpacity);
195     context->setFillColor(Color::black, style()->colorSpace());
196     context->drawBidiText(font, run, FloatPoint(labelX, labelY));
197 }
198
199 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
200 {
201     contentRect = contentBoxRect();
202     contentRect.moveBy(roundedIntPoint(accumulatedOffset));
203     
204     FontDescription fontDescription;
205     RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
206     fontDescription.setWeight(FontWeightBold);
207     Settings* settings = document()->settings();
208     ASSERT(settings);
209     if (!settings)
210         return false;
211     fontDescription.setRenderingMode(settings->fontRenderingMode());
212     fontDescription.setComputedSize(fontDescription.specifiedSize());
213     font = Font(fontDescription, 0, 0);
214     font.update(0);
215
216     run = TextRun(m_unavailablePluginReplacementText);
217     textWidth = font.width(run);
218     
219     replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftRightTextMargin * 2, replacementTextRoundedRectHeight));
220     float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
221     float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
222     replacementTextRect.setLocation(FloatPoint(x, y));
223     
224     path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
225
226     return true;
227 }
228
229 void RenderEmbeddedObject::layout()
230 {
231     StackStats::LayoutCheckPoint layoutCheckPoint;
232     ASSERT(needsLayout());
233
234     updateLogicalWidth();
235     updateLogicalHeight();
236
237     RenderPart::layout();
238
239     m_overflow.clear();
240     addVisualEffectOverflow();
241
242     updateLayerTransform();
243
244     if (!widget() && frameView())
245         frameView()->addWidgetToUpdate(this);
246
247     setNeedsLayout(false);
248 }
249
250 void RenderEmbeddedObject::viewCleared()
251 {
252     // This is required for <object> elements whose contents are rendered by WebCore (e.g. src="foo.html").
253     if (node() && widget() && widget()->isFrameView()) {
254         FrameView* view = static_cast<FrameView*>(widget());
255         int marginWidth = -1;
256         int marginHeight = -1;
257         if (node()->hasTagName(iframeTag)) {
258             HTMLIFrameElement* frame = static_cast<HTMLIFrameElement*>(node());
259             marginWidth = frame->marginWidth();
260             marginHeight = frame->marginHeight();
261         }
262         if (marginWidth != -1)
263             view->setMarginWidth(marginWidth);
264         if (marginHeight != -1)
265             view->setMarginHeight(marginHeight);
266     }
267 }
268
269 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
270 {
271     if (!RenderPart::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction))
272         return false;
273
274     if (!widget() || !widget()->isPluginViewBase())
275         return true;
276
277     PluginViewBase* view = static_cast<PluginViewBase*>(widget());
278     IntPoint roundedPoint = locationInContainer.roundedPoint();
279
280     if (Scrollbar* horizontalScrollbar = view->horizontalScrollbar()) {
281         if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
282             result.setScrollbar(horizontalScrollbar);
283             return true;
284         }
285     }
286
287     if (Scrollbar* verticalScrollbar = view->verticalScrollbar()) {
288         if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
289             result.setScrollbar(verticalScrollbar);
290             return true;
291         }
292     }
293
294     return true;
295 }
296
297 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Node**)
298 {
299     if (!widget() || !widget()->isPluginViewBase())
300         return false;
301
302     return static_cast<PluginViewBase*>(widget())->scroll(direction, granularity);
303 }
304
305 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
306 {
307     // Plugins don't expose a writing direction, so assuming horizontal LTR.
308     return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopNode);
309 }
310
311
312 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const LayoutPoint& point) const
313 {
314     FloatRect contentRect;
315     Path path;
316     FloatRect replacementTextRect;
317     Font font;
318     TextRun run("");
319     float textWidth;
320     return getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth)
321         && path.contains(point);
322 }
323
324 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(MouseEvent* event) const
325 {
326     return isInUnavailablePluginIndicator(roundedLayoutPoint(absoluteToLocal(event->absoluteLocation(), UseTransforms | SnapOffsetForTransforms)));
327 }
328
329 static bool shouldUnavailablePluginMessageBeButton(Document* document, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
330 {
331     Page* page = document->page();
332     return page && page->chrome()->client()->shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
333 }
334
335 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
336 {
337     if (!shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason))
338         return;
339     
340     if (!event->isMouseEvent())
341         return;
342     
343     MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
344     HTMLPlugInElement* element = static_cast<HTMLPlugInElement*>(node());
345     if (event->type() == eventNames().mousedownEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
346         m_mouseDownWasInUnavailablePluginIndicator = isInUnavailablePluginIndicator(mouseEvent);
347         if (m_mouseDownWasInUnavailablePluginIndicator) {
348             if (Frame* frame = document()->frame()) {
349                 frame->eventHandler()->setCapturingMouseEventsNode(element);
350                 element->setIsCapturingMouseEvents(true);
351             }
352             setUnavailablePluginIndicatorIsPressed(true);
353         }
354         event->setDefaultHandled();
355     }        
356     if (event->type() == eventNames().mouseupEvent && static_cast<MouseEvent*>(event)->button() == LeftButton) {
357         if (m_unavailablePluginIndicatorIsPressed) {
358             if (Frame* frame = document()->frame()) {
359                 frame->eventHandler()->setCapturingMouseEventsNode(0);
360                 element->setIsCapturingMouseEvents(false);
361             }
362             setUnavailablePluginIndicatorIsPressed(false);
363         }
364         if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
365             if (Page* page = document()->page())
366                 page->chrome()->client()->unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
367         }
368         m_mouseDownWasInUnavailablePluginIndicator = false;
369         event->setDefaultHandled();
370     }
371     if (event->type() == eventNames().mousemoveEvent) {
372         setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
373         event->setDefaultHandled();
374     }
375 }
376
377 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
378 {
379     if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
380         cursor = handCursor();
381         return SetCursor;
382     }
383     return RenderPart::getCursor(point, cursor);
384 }
385
386 }