RenderTheme does not need to be per-page
[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 "CSSValueKeywords.h"
28 #include "Chrome.h"
29 #include "ChromeClient.h"
30 #include "Cursor.h"
31 #include "EventHandler.h"
32 #include "EventNames.h"
33 #include "FontCascade.h"
34 #include "FontSelector.h"
35 #include "Frame.h"
36 #include "FrameLoaderClient.h"
37 #include "GraphicsContext.h"
38 #include "HTMLAppletElement.h"
39 #include "HTMLEmbedElement.h"
40 #include "HTMLNames.h"
41 #include "HTMLObjectElement.h"
42 #include "HTMLParamElement.h"
43 #include "HTMLPlugInElement.h"
44 #include "HitTestResult.h"
45 #include "LocalizedStrings.h"
46 #include "MouseEvent.h"
47 #include "Page.h"
48 #include "PaintInfo.h"
49 #include "Path.h"
50 #include "PlatformMouseEvent.h"
51 #include "PluginViewBase.h"
52 #include "RenderLayer.h"
53 #include "RenderTheme.h"
54 #include "RenderView.h"
55 #include "Settings.h"
56 #include "Text.h"
57 #include "TextRun.h"
58 #include <wtf/StackStats.h>
59
60 namespace WebCore {
61
62 using namespace HTMLNames;
63
64 static const float replacementTextRoundedRectHeight = 22;
65 static const float replacementTextRoundedRectLeftTextMargin = 10;
66 static const float replacementTextRoundedRectRightTextMargin = 10;
67 static const float replacementTextRoundedRectRightTextMarginWithArrow = 5;
68 static const float replacementTextRoundedRectTopTextMargin = -1;
69 static const float replacementTextRoundedRectRadius = 11;
70 static const float replacementArrowLeftMargin = -4;
71 static const float replacementArrowPadding = 4;
72 static const float replacementArrowCirclePadding = 3;
73
74 static const Color& replacementTextRoundedRectPressedColor()
75 {
76     static NeverDestroyed<Color> pressed(105, 105, 105, 242);
77     return pressed;
78 }
79
80 static const Color& replacementTextRoundedRectColor()
81 {
82     static NeverDestroyed<Color> standard(125, 125, 125, 242);
83     return standard;
84 }
85
86 static const Color& replacementTextColor()
87 {
88     static NeverDestroyed<Color> standard(240, 240, 240, 255);
89     return standard;
90 }
91
92 static const Color& unavailablePluginBorderColor()
93 {
94     static NeverDestroyed<Color> standard(255, 255, 255, 216);
95     return standard;
96 }
97
98 RenderEmbeddedObject::RenderEmbeddedObject(HTMLFrameOwnerElement& element, RenderStyle&& style)
99     : RenderWidget(element, WTFMove(style))
100     , m_isPluginUnavailable(false)
101     , m_unavailablePluginIndicatorIsPressed(false)
102     , m_mouseDownWasInUnavailablePluginIndicator(false)
103 {
104     // Actual size is not known yet, report the default intrinsic size.
105     view().frameView().incrementVisuallyNonEmptyPixelCount(roundedIntSize(intrinsicSize()));
106 }
107
108 RenderEmbeddedObject::~RenderEmbeddedObject()
109 {
110     // Do not add any code here. Add it to willBeDestroyed() instead.
111 }
112
113 void RenderEmbeddedObject::willBeDestroyed()
114 {
115     view().frameView().removeEmbeddedObjectToUpdate(*this);
116     RenderWidget::willBeDestroyed();
117 }
118
119 RenderPtr<RenderEmbeddedObject> RenderEmbeddedObject::createForApplet(HTMLAppletElement& applet, RenderStyle&& style)
120 {
121     auto renderer = createRenderer<RenderEmbeddedObject>(applet, WTFMove(style));
122     renderer->setInline(true);
123     return renderer;
124 }
125
126 bool RenderEmbeddedObject::requiresLayer() const
127 {
128     if (RenderWidget::requiresLayer())
129         return true;
130     
131     return allowsAcceleratedCompositing();
132 }
133
134 bool RenderEmbeddedObject::allowsAcceleratedCompositing() const
135 {
136 #if PLATFORM(IOS)
137     // The timing of layer creation is different on the phone, since the plugin can only be manipulated from the main thread.
138     return is<PluginViewBase>(widget()) && downcast<PluginViewBase>(*widget()).willProvidePluginLayer();
139 #else
140     return is<PluginViewBase>(widget()) && downcast<PluginViewBase>(*widget()).platformLayer();
141 #endif
142 }
143
144 #if !PLATFORM(IOS)
145 static String unavailablePluginReplacementText(RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
146 {
147     switch (pluginUnavailabilityReason) {
148     case RenderEmbeddedObject::PluginMissing:
149         return missingPluginText();
150     case RenderEmbeddedObject::PluginCrashed:
151         return crashedPluginText();
152     case RenderEmbeddedObject::PluginBlockedByContentSecurityPolicy:
153         return blockedPluginByContentSecurityPolicyText();
154     case RenderEmbeddedObject::InsecurePluginVersion:
155         return insecurePluginVersionText();
156     }
157
158     ASSERT_NOT_REACHED();
159     return String();
160 }
161 #endif
162
163 static bool shouldUnavailablePluginMessageBeButton(Page& page, RenderEmbeddedObject::PluginUnavailabilityReason pluginUnavailabilityReason)
164 {
165     return page.chrome().client().shouldUnavailablePluginMessageBeButton(pluginUnavailabilityReason);
166 }
167
168 void RenderEmbeddedObject::setPluginUnavailabilityReason(PluginUnavailabilityReason pluginUnavailabilityReason)
169 {
170 #if PLATFORM(IOS)
171     UNUSED_PARAM(pluginUnavailabilityReason);
172 #else
173     setPluginUnavailabilityReasonWithDescription(pluginUnavailabilityReason, unavailablePluginReplacementText(pluginUnavailabilityReason));
174 #endif
175 }
176
177 void RenderEmbeddedObject::setPluginUnavailabilityReasonWithDescription(PluginUnavailabilityReason pluginUnavailabilityReason, const String& description)
178 {
179 #if PLATFORM(IOS)
180     UNUSED_PARAM(pluginUnavailabilityReason);
181     UNUSED_PARAM(description);
182 #else
183     ASSERT(!m_isPluginUnavailable);
184     m_isPluginUnavailable = true;
185     m_pluginUnavailabilityReason = pluginUnavailabilityReason;
186
187     if (description.isEmpty())
188         m_unavailablePluginReplacementText = unavailablePluginReplacementText(pluginUnavailabilityReason);
189     else
190         m_unavailablePluginReplacementText = description;
191 #endif
192 }
193
194 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsPressed(bool pressed)
195 {
196     if (m_unavailablePluginIndicatorIsPressed == pressed)
197         return;
198     m_unavailablePluginIndicatorIsPressed = pressed;
199     repaint();
200 }
201
202 void RenderEmbeddedObject::paintSnapshotImage(PaintInfo& paintInfo, const LayoutPoint& paintOffset, Image& image)
203 {
204     LayoutUnit cWidth = contentWidth();
205     LayoutUnit cHeight = contentHeight();
206     if (!cWidth || !cHeight)
207         return;
208
209     GraphicsContext& context = paintInfo.context();
210     LayoutSize contentSize(cWidth, cHeight);
211     LayoutPoint contentLocation = location() + paintOffset;
212     contentLocation.move(borderLeft() + paddingLeft(), borderTop() + paddingTop());
213
214     LayoutRect rect(contentLocation, contentSize);
215     IntRect alignedRect = snappedIntRect(rect);
216     if (alignedRect.width() <= 0 || alignedRect.height() <= 0)
217         return;
218
219     InterpolationQuality interpolation = chooseInterpolationQuality(context, image, &image, alignedRect.size());
220     ImageOrientationDescription orientationDescription(shouldRespectImageOrientation(), style().imageOrientation());
221     context.drawImage(image, alignedRect, ImagePaintingOptions(orientationDescription, interpolation));
222 }
223
224 void RenderEmbeddedObject::paintContents(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
225 {
226     if (!is<HTMLPlugInElement>(frameOwnerElement()))
227         return;
228
229     HTMLPlugInElement& plugInElement = downcast<HTMLPlugInElement>(frameOwnerElement());
230
231     if (plugInElement.displayState() > HTMLPlugInElement::DisplayingSnapshot) {
232         RenderWidget::paintContents(paintInfo, paintOffset);
233         if (!plugInElement.isRestartedPlugin())
234             return;
235     }
236
237     if (!is<HTMLPlugInImageElement>(plugInElement))
238         return;
239
240     if (Image* snapshot = downcast<HTMLPlugInImageElement>(plugInElement).snapshotImage())
241         paintSnapshotImage(paintInfo, paintOffset, *snapshot);
242 }
243
244 void RenderEmbeddedObject::paint(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
245 {
246     // The relevant repainted object heuristic is not tuned for plugin documents.
247     bool countsTowardsRelevantObjects = !document().isPluginDocument() && paintInfo.phase == PaintPhaseForeground;
248
249     if (isPluginUnavailable()) {
250         if (countsTowardsRelevantObjects)
251             page().addRelevantUnpaintedObject(this, visualOverflowRect());
252         RenderReplaced::paint(paintInfo, paintOffset);
253         return;
254     }
255
256     if (countsTowardsRelevantObjects)
257         page().addRelevantRepaintedObject(this, visualOverflowRect());
258
259     RenderWidget::paint(paintInfo, paintOffset);
260 }
261
262 static void drawReplacementArrow(GraphicsContext& context, const FloatRect& insideRect)
263 {
264     GraphicsContextStateSaver stateSaver(context);
265
266     FloatRect rect(insideRect);
267     rect.inflate(-replacementArrowPadding);
268
269     FloatPoint center(rect.center());
270     FloatPoint arrowTip(rect.maxX(), center.y());
271
272     context.setStrokeThickness(2);
273     context.setLineCap(RoundCap);
274     context.setLineJoin(RoundJoin);
275
276     Path path;
277     path.moveTo(FloatPoint(rect.x(), center.y()));
278     path.addLineTo(arrowTip);
279     path.addLineTo(FloatPoint(center.x(), rect.y()));
280     path.moveTo(arrowTip);
281     path.addLineTo(FloatPoint(center.x(), rect.maxY()));
282     context.strokePath(path);
283 }
284
285 void RenderEmbeddedObject::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
286 {
287     if (!showsUnavailablePluginIndicator())
288         return;
289
290     if (paintInfo.phase == PaintPhaseSelection)
291         return;
292
293     GraphicsContext& context = paintInfo.context();
294     if (context.paintingDisabled())
295         return;
296
297     FloatRect contentRect;
298     FloatRect indicatorRect;
299     FloatRect replacementTextRect;
300     FloatRect arrowRect;
301     FontCascade font;
302     TextRun run(emptyString());
303     float textWidth;
304     if (!getReplacementTextGeometry(paintOffset, contentRect, indicatorRect, replacementTextRect, arrowRect, font, run, textWidth))
305         return;
306
307     Path background;
308     background.addRoundedRect(indicatorRect, FloatSize(replacementTextRoundedRectRadius, replacementTextRoundedRectRadius));
309
310     GraphicsContextStateSaver stateSaver(context);
311     context.clip(contentRect);
312     context.setFillColor(m_unavailablePluginIndicatorIsPressed ? replacementTextRoundedRectPressedColor() : replacementTextRoundedRectColor());
313     context.fillPath(background);
314
315     Path strokePath;
316     FloatRect strokeRect(indicatorRect);
317     strokeRect.inflate(1);
318     strokePath.addRoundedRect(strokeRect, FloatSize(replacementTextRoundedRectRadius + 1, replacementTextRoundedRectRadius + 1));
319
320     context.setStrokeColor(unavailablePluginBorderColor());
321     context.setStrokeThickness(2);
322     context.strokePath(strokePath);
323
324     const FontMetrics& fontMetrics = font.fontMetrics();
325     float labelX = roundf(replacementTextRect.location().x() + replacementTextRoundedRectLeftTextMargin);
326     float labelY = roundf(replacementTextRect.location().y() + (replacementTextRect.size().height() - fontMetrics.height()) / 2 + fontMetrics.ascent() + replacementTextRoundedRectTopTextMargin);
327     context.setFillColor(replacementTextColor());
328     context.drawBidiText(font, run, FloatPoint(labelX, labelY));
329
330     if (shouldUnavailablePluginMessageBeButton(page(), m_pluginUnavailabilityReason)) {
331         arrowRect.inflate(-replacementArrowCirclePadding);
332
333         context.beginTransparencyLayer(1.0);
334         context.setFillColor(replacementTextColor());
335         context.fillEllipse(arrowRect);
336
337         context.setCompositeOperation(CompositeClear);
338         drawReplacementArrow(context, arrowRect);
339         context.endTransparencyLayer();
340     }
341 }
342
343 void RenderEmbeddedObject::setUnavailablePluginIndicatorIsHidden(bool hidden)
344 {
345     auto newState = hidden ? UnavailablePluginIndicatorState::Hidden : UnavailablePluginIndicatorState::Visible;
346     if (m_isUnavailablePluginIndicatorState == newState)
347         return;
348     m_isUnavailablePluginIndicatorState = newState;
349     repaint();
350 }
351
352 bool RenderEmbeddedObject::getReplacementTextGeometry(const LayoutPoint& accumulatedOffset, FloatRect& contentRect, FloatRect& indicatorRect, FloatRect& replacementTextRect, FloatRect& arrowRect, FontCascade& font, TextRun& run, float& textWidth) const
353 {
354     bool includesArrow = shouldUnavailablePluginMessageBeButton(page(), m_pluginUnavailabilityReason);
355
356     contentRect = contentBoxRect();
357     contentRect.moveBy(roundedIntPoint(accumulatedOffset));
358
359     FontCascadeDescription fontDescription;
360     RenderTheme::singleton().systemFont(CSSValueWebkitSmallControl, fontDescription);
361     fontDescription.setWeight(boldWeightValue());
362     fontDescription.setRenderingMode(settings().fontRenderingMode());
363     fontDescription.setComputedSize(12);
364     font = FontCascade(fontDescription, 0, 0);
365     font.update(0);
366
367     run = TextRun(m_unavailablePluginReplacementText);
368     textWidth = font.width(run);
369
370     replacementTextRect.setSize(FloatSize(textWidth + replacementTextRoundedRectLeftTextMargin + (includesArrow ? replacementTextRoundedRectRightTextMarginWithArrow : replacementTextRoundedRectRightTextMargin), replacementTextRoundedRectHeight));
371     float x = (contentRect.size().width() / 2 - replacementTextRect.size().width() / 2) + contentRect.location().x();
372     float y = (contentRect.size().height() / 2 - replacementTextRect.size().height() / 2) + contentRect.location().y();
373     replacementTextRect.setLocation(FloatPoint(x, y));
374
375     indicatorRect = replacementTextRect;
376
377     // Expand the background rect to include the arrow, if it will be used.
378     if (includesArrow) {
379         arrowRect = indicatorRect;
380         arrowRect.setX(ceilf(arrowRect.maxX() + replacementArrowLeftMargin));
381         arrowRect.setWidth(arrowRect.height());
382         indicatorRect.unite(arrowRect);
383     }
384
385     return true;
386 }
387
388 LayoutRect RenderEmbeddedObject::unavailablePluginIndicatorBounds(const LayoutPoint& accumulatedOffset) const
389 {
390     FloatRect contentRect;
391     FloatRect indicatorRect;
392     FloatRect replacementTextRect;
393     FloatRect arrowRect;
394     FontCascade font;
395     TextRun run(emptyString());
396     float textWidth;
397     if (getReplacementTextGeometry(accumulatedOffset, contentRect, indicatorRect, replacementTextRect, arrowRect, font, run, textWidth))
398         return LayoutRect(indicatorRect);
399
400     return LayoutRect();
401 }
402
403 bool RenderEmbeddedObject::isReplacementObscured() const
404 {
405     // Return whether or not the replacement content for blocked plugins is accessible to the user.
406
407     // Check the opacity of each layer containing the element or its ancestors.
408     float opacity = 1.0;
409     for (RenderLayer* layer = enclosingLayer(); layer; layer = layer->parent()) {
410         opacity *= layer->renderer().style().opacity();
411         if (opacity < 0.1)
412             return true;
413     }
414
415     // Calculate the absolute rect for the blocked plugin replacement text.
416     IntRect absoluteBoundingBox = absoluteBoundingBoxRect();
417     LayoutPoint absoluteLocation(absoluteBoundingBox.location());
418     LayoutRect rect = unavailablePluginIndicatorBounds(absoluteLocation);
419     if (rect.isEmpty())
420         return true;
421
422     RenderView* rootRenderView = document().topDocument().renderView();
423     ASSERT(rootRenderView);
424     if (!rootRenderView)
425         return true;
426
427     // We should always start hit testing a clean tree.
428     view().frameView().updateLayoutAndStyleIfNeededRecursive();
429
430     HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active | HitTestRequest::IgnoreClipping | HitTestRequest::DisallowUserAgentShadowContent | HitTestRequest::AllowChildFrameContent);
431     HitTestResult result;
432     HitTestLocation location;
433     
434     IntRect rootViewRect = view().frameView().convertToRootView(snappedIntRect(rect));
435     LayoutUnit x = rootViewRect.x();
436     LayoutUnit y = rootViewRect.y();
437     LayoutUnit width = rootViewRect.width();
438     LayoutUnit height = rootViewRect.height();
439     
440     // Hit test the center and near the corners of the replacement text to ensure
441     // it is visible and is not masked by other elements.
442     bool hit = false;
443     location = LayoutPoint(x + width / 2, y + height / 2);
444     hit = rootRenderView->hitTest(request, location, result);
445     if (!hit || result.innerNode() != &frameOwnerElement())
446         return true;
447     
448     location = LayoutPoint(x, y);
449     hit = rootRenderView->hitTest(request, location, result);
450     if (!hit || result.innerNode() != &frameOwnerElement())
451         return true;
452     
453     location = LayoutPoint(x + width, y);
454     hit = rootRenderView->hitTest(request, location, result);
455     if (!hit || result.innerNode() != &frameOwnerElement())
456         return true;
457     
458     location = LayoutPoint(x + width, y + height);
459     hit = rootRenderView->hitTest(request, location, result);
460     if (!hit || result.innerNode() != &frameOwnerElement())
461         return true;
462     
463     location = LayoutPoint(x, y + height);
464     hit = rootRenderView->hitTest(request, location, result);
465     if (!hit || result.innerNode() != &frameOwnerElement())
466         return true;
467
468     return false;
469 }
470
471 void RenderEmbeddedObject::layout()
472 {
473     StackStats::LayoutCheckPoint layoutCheckPoint;
474     ASSERT(needsLayout());
475
476     LayoutSize oldSize = contentBoxRect().size();
477
478     updateLogicalWidth();
479     updateLogicalHeight();
480
481     RenderWidget::layout();
482
483     clearOverflow();
484     addVisualEffectOverflow();
485
486     updateLayerTransform();
487
488     bool wasMissingWidget = false;
489     if (!widget() && canHaveWidget()) {
490         wasMissingWidget = true;
491         view().frameView().addEmbeddedObjectToUpdate(*this);
492     }
493
494     clearNeedsLayout();
495
496     LayoutSize newSize = contentBoxRect().size();
497
498     if (!wasMissingWidget && newSize.width() >= oldSize.width() && newSize.height() >= oldSize.height()) {
499         HTMLFrameOwnerElement& element = frameOwnerElement();
500         if (is<HTMLPlugInImageElement>(element)) {
501             HTMLPlugInImageElement& plugInImageElement = downcast<HTMLPlugInImageElement>(element);
502             if (plugInImageElement.displayState() > HTMLPlugInElement::DisplayingSnapshot && plugInImageElement.snapshotDecision() == HTMLPlugInImageElement::MaySnapshotWhenResized) {
503                 plugInImageElement.setNeedsCheckForSizeChange();
504                 view().frameView().addEmbeddedObjectToUpdate(*this);
505             }
506         }
507     }
508
509     if (!canHaveChildren())
510         return;
511
512     // This code copied from RenderMedia::layout().
513     RenderObject* child = firstChild();
514
515     if (!child)
516         return;
517
518     auto& childBox = downcast<RenderBox>(*child);
519
520     if (newSize == oldSize && !childBox.needsLayout())
521         return;
522     
523     // When calling layout() on a child node, a parent must either push a LayoutStateMaintainter, or
524     // instantiate LayoutStateDisabler. Since using a LayoutStateMaintainer is slightly more efficient,
525     // and this method will be called many times per second during playback, use a LayoutStateMaintainer:
526     LayoutStateMaintainer statePusher(view(), *this, locationOffset(), hasTransform() || hasReflection() || style().isFlippedBlocksWritingMode());
527     
528     childBox.setLocation(LayoutPoint(borderLeft(), borderTop()) + LayoutSize(paddingLeft(), paddingTop()));
529     childBox.mutableStyle().setHeight(Length(newSize.height(), Fixed));
530     childBox.mutableStyle().setWidth(Length(newSize.width(), Fixed));
531     childBox.setNeedsLayout(MarkOnlyThis);
532     childBox.layout();
533     clearChildNeedsLayout();
534     
535     statePusher.pop();
536 }
537
538 bool RenderEmbeddedObject::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
539 {
540     if (!RenderWidget::nodeAtPoint(request, result, locationInContainer, accumulatedOffset, hitTestAction))
541         return false;
542
543     if (!is<PluginViewBase>(widget()))
544         return true;
545
546     PluginViewBase& view = downcast<PluginViewBase>(*widget());
547     IntPoint roundedPoint = locationInContainer.roundedPoint();
548
549     if (Scrollbar* horizontalScrollbar = view.horizontalScrollbar()) {
550         if (horizontalScrollbar->shouldParticipateInHitTesting() && horizontalScrollbar->frameRect().contains(roundedPoint)) {
551             result.setScrollbar(horizontalScrollbar);
552             return true;
553         }
554     }
555
556     if (Scrollbar* verticalScrollbar = view.verticalScrollbar()) {
557         if (verticalScrollbar->shouldParticipateInHitTesting() && verticalScrollbar->frameRect().contains(roundedPoint)) {
558             result.setScrollbar(verticalScrollbar);
559             return true;
560         }
561     }
562
563     return true;
564 }
565
566 bool RenderEmbeddedObject::scroll(ScrollDirection direction, ScrollGranularity granularity, float, Element**, RenderBox*, const IntPoint&)
567 {
568     if (!is<PluginViewBase>(widget()))
569         return false;
570
571     return downcast<PluginViewBase>(*widget()).scroll(direction, granularity);
572 }
573
574 bool RenderEmbeddedObject::logicalScroll(ScrollLogicalDirection direction, ScrollGranularity granularity, float multiplier, Element** stopElement)
575 {
576     // Plugins don't expose a writing direction, so assuming horizontal LTR.
577     return scroll(logicalToPhysical(direction, true, false), granularity, multiplier, stopElement);
578 }
579
580
581 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const FloatPoint& point) const
582 {
583     FloatRect contentRect;
584     FloatRect indicatorRect;
585     FloatRect replacementTextRect;
586     FloatRect arrowRect;
587     FontCascade font;
588     TextRun run(emptyString());
589     float textWidth;
590     return getReplacementTextGeometry(IntPoint(), contentRect, indicatorRect, replacementTextRect, arrowRect, font, run, textWidth)
591         && indicatorRect.contains(point);
592 }
593
594 bool RenderEmbeddedObject::isInUnavailablePluginIndicator(const MouseEvent& event) const
595 {
596     return isInUnavailablePluginIndicator(absoluteToLocal(event.absoluteLocation(), UseTransforms));
597 }
598
599 void RenderEmbeddedObject::handleUnavailablePluginIndicatorEvent(Event* event)
600 {
601     if (!shouldUnavailablePluginMessageBeButton(page(), m_pluginUnavailabilityReason))
602         return;
603
604     if (!is<MouseEvent>(*event))
605         return;
606
607     MouseEvent& mouseEvent = downcast<MouseEvent>(*event);
608     HTMLPlugInElement& element = downcast<HTMLPlugInElement>(frameOwnerElement());
609     if (mouseEvent.type() == eventNames().mousedownEvent && mouseEvent.button() == LeftButton) {
610         m_mouseDownWasInUnavailablePluginIndicator = isInUnavailablePluginIndicator(mouseEvent);
611         if (m_mouseDownWasInUnavailablePluginIndicator) {
612             frame().eventHandler().setCapturingMouseEventsElement(&element);
613             element.setIsCapturingMouseEvents(true);
614             setUnavailablePluginIndicatorIsPressed(true);
615         }
616         mouseEvent.setDefaultHandled();
617     }
618     if (mouseEvent.type() == eventNames().mouseupEvent && mouseEvent.button() == LeftButton) {
619         if (m_unavailablePluginIndicatorIsPressed) {
620             frame().eventHandler().setCapturingMouseEventsElement(nullptr);
621             element.setIsCapturingMouseEvents(false);
622             setUnavailablePluginIndicatorIsPressed(false);
623         }
624         if (m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent)) {
625             page().chrome().client().unavailablePluginButtonClicked(element, m_pluginUnavailabilityReason);
626         }
627         m_mouseDownWasInUnavailablePluginIndicator = false;
628         event->setDefaultHandled();
629     }
630     if (mouseEvent.type() == eventNames().mousemoveEvent) {
631         setUnavailablePluginIndicatorIsPressed(m_mouseDownWasInUnavailablePluginIndicator && isInUnavailablePluginIndicator(mouseEvent));
632         mouseEvent.setDefaultHandled();
633     }
634 }
635
636 CursorDirective RenderEmbeddedObject::getCursor(const LayoutPoint& point, Cursor& cursor) const
637 {
638     if (showsUnavailablePluginIndicator() && shouldUnavailablePluginMessageBeButton(page(), m_pluginUnavailabilityReason) && isInUnavailablePluginIndicator(point)) {
639         cursor = handCursor();
640         return SetCursor;
641     }
642     if (widget() && widget()->isPluginViewBase()) {
643         // A plug-in is responsible for setting the cursor when the pointer is over it.
644         return DoNotSetCursor;
645     }
646     return RenderWidget::getCursor(point, cursor);
647 }
648
649 bool RenderEmbeddedObject::canHaveChildren() const
650 {
651     if (isSnapshottedPlugIn())
652         return true;
653
654     return false;
655 }
656
657 }