Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.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 #if ENABLE(TIZEN_PAINT_MISSING_PLUGIN_RECT)
192     context->setFillColor(Color::gray, style()->colorSpace());
193     context->fillRect(contentRect);
194 #endif
195
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));
202 }
203
204 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, Path& path, FloatRect& replacementTextRect, Font& font, TextRun& run, float& textWidth) const
205 {
206     contentRect = contentBoxRect();
207     contentRect.moveBy(roundedIntPoint(accumulatedOffset));
208     
209     FontDescription fontDescription;
210     RenderTheme::defaultTheme()->systemFont(CSSValueWebkitSmallControl, fontDescription);
211     fontDescription.setWeight(FontWeightBold);
212     Settings* settings = document()->settings();
213     ASSERT(settings);
214     if (!settings)
215         return false;
216     fontDescription.setRenderingMode(settings->fontRenderingMode());
217     fontDescription.setComputedSize(fontDescription.specifiedSize());
218     font = Font(fontDescription, 0, 0);
219     font.update(0);
220
221     run = TextRun(m_unavailablePluginReplacementText);
222     textWidth = font.width(run);
223     
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));
228     
229     path.addRoundedRect(replacementTextRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
230
231     return true;
232 }
233
234 void RenderEmbeddedObject::layout()
235 {
236     ASSERT(needsLayout());
237
238     computeLogicalWidth();
239     computeLogicalHeight();
240
241     RenderPart::layout();
242
243     m_overflow.clear();
244     addVisualEffectOverflow();
245
246     updateLayerTransform();
247
248     if (!widget() && frameView())
249         frameView()->addWidgetToUpdate(this);
250
251     setNeedsLayout(false);
252 }
253
254 void RenderEmbeddedObject::viewCleared()
255 {
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();
265         }
266         if (marginWidth != -1)
267             view->setMarginWidth(marginWidth);
268         if (marginHeight != -1)
269             view->setMarginHeight(marginHeight);
270     }
271 }
272
273 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestPoint& pointInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
274 {
275     if (!RenderPart::nodeAtPoint(request, result, pointInContainer, accumulatedOffset, hitTestAction))
276         return false;
277
278     if (!widget() || !widget()->isPluginViewBase())
279         return true;
280
281     PluginViewBase* view = static_cast<PluginViewBase*>(widget());
282     IntPoint roundedPoint = pointInContainer.roundedPoint();
283
284     if (Scrollbar* horizontalScrollbar = view->horizontalScrollbar()) {
285         if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
286             result.setScrollbar(horizontalScrollbar);
287             return true;
288         }
289     }
290
291     if (Scrollbar* verticalScrollbar = view->verticalScrollbar()) {
292         if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
293             result.setScrollbar(verticalScrollbar);
294             return true;
295         }
296     }
297
298     return true;
299 }
300
301 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Node**)
302 {
303     if (!widget() || !widget()->isPluginViewBase())
304         return false;
305
306     return static_cast<PluginViewBase*>(widget())->scroll(direction, granularity);
307 }
308
309 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Node** stopNode)
310 {
311     // Plugins don't expose a writing direction, so assuming horizontal LTR.
312     return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopNode);
313 }
314
315
316 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const LayoutPoint& point) const
317 {
318     FloatRect contentRect;
319     Path path;
320     FloatRect replacementTextRect;
321     Font font;
322     TextRun run("");
323     float textWidth;
324     return getReplacementTextGeometry(IntPoint(), contentRect, path, replacementTextRect, font, run, textWidth)
325         && path.contains(point);
326 }
327
328 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(MouseEvent* event) const
329 {
330     return isInUnavailablePluginIndicator(roundedLayoutPoint(absoluteToLocal(event->absoluteLocation(), false, true)));
331 }
332
333 static bool shouldUnavailablePluginMessageBeButton(Document* document, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
334 {
335     Page* page = document->page();
336     return page && page->chrome()->client()->shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
337 }
338
339 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
340 {
341     if (!shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason))
342         return;
343     
344     if (!event->isMouseEvent())
345         return;
346     
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);
355             }
356             setUnavailablePluginIndicatorIsPressed(true);
357         }
358         event->setDefaultHandled();
359     }        
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);
365             }
366             setUnavailablePluginIndicatorIsPressed(false);
367         }
368         if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
369             if (Page* page = document()->page())
370                 page->chrome()->client()->unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
371         }
372         m_mouseDownWasInUnavailablePluginIndicator = false;
373         event->setDefaultHandled();
374     }
375     if (event->type() == eventNames().mousemoveEvent) {
376         setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
377         event->setDefaultHandled();
378     }
379 }
380
381 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
382 {
383     if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(document(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
384         cursor = handCursor();
385         return SetCursor;
386     }
387     return RenderPart::getCursor(point, cursor);
388 }
389
390 }