2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/plugins/PluginOcclusionSupport.h"
34 #include "core/HTMLNames.h"
35 #include "core/dom/Element.h"
36 #include "core/frame/FrameView.h"
37 #include "core/frame/LocalFrame.h"
38 #include "core/html/HTMLElement.h"
39 #include "core/html/HTMLFrameOwnerElement.h"
40 #include "core/rendering/RenderBox.h"
41 #include "core/rendering/RenderObject.h"
42 #include "platform/Widget.h"
43 #include "wtf/HashSet.h"
45 // This file provides a utility function to support rendering certain elements above plugins.
49 static void getObjectStack(const RenderObject* ro, Vector<const RenderObject*>* roStack)
58 // Returns true if stack1 is at or above stack2
59 static bool iframeIsAbovePlugin(const Vector<const RenderObject*>& iframeZstack, const Vector<const RenderObject*>& pluginZstack)
61 for (size_t i = 0; i < iframeZstack.size() && i < pluginZstack.size(); i++) {
62 // The root is at the end of these stacks. We want to iterate
63 // root-downwards so we index backwards from the end.
64 const RenderObject* ro1 = iframeZstack[iframeZstack.size() - 1 - i];
65 const RenderObject* ro2 = pluginZstack[pluginZstack.size() - 1 - i];
68 // When we find nodes in the stack that are not the same, then
69 // we've found the nodes just below the lowest comment ancestor.
70 // Determine which should be on top.
72 // See if z-index determines an order.
73 if (ro1->style() && ro2->style()) {
74 int z1 = ro1->style()->zIndex();
75 int z2 = ro2->style()->zIndex();
82 // If the plugin does not have an explicit z-index it stacks behind the iframe.
83 // This is for maintaining compatibility with IE.
84 if (ro2->style()->position() == StaticPosition) {
85 // The 0'th elements of these RenderObject arrays represent the plugin node and
87 const RenderObject* pluginRenderObject = pluginZstack[0];
88 const RenderObject* iframeRenderObject = iframeZstack[0];
90 if (pluginRenderObject->style() && iframeRenderObject->style()) {
91 if (pluginRenderObject->style()->zIndex() > iframeRenderObject->style()->zIndex())
97 // Inspect the document order. Later order means higher stacking.
98 const RenderObject* parent = ro1->parent();
101 ASSERT(parent == ro2->parent());
103 for (const RenderObject* ro = parent->slowFirstChild(); ro; ro = ro->nextSibling()) {
109 ASSERT(false); // We should have seen ro1 and ro2 by now.
116 static bool intersectsRect(const RenderObject* renderer, const IntRect& rect)
118 return renderer->absoluteBoundingBoxRectIgnoringTransforms().intersects(rect)
119 && (!renderer->style() || renderer->style()->visibility() == VISIBLE);
122 static void addToOcclusions(const RenderBox* renderer, Vector<IntRect>& occlusions)
124 IntPoint point = roundedIntPoint(renderer->localToAbsolute());
125 IntSize size(renderer->width(), renderer->height());
126 occlusions.append(IntRect(point, size));
129 static void addTreeToOcclusions(const RenderObject* renderer, const IntRect& frameRect, Vector<IntRect>& occlusions)
133 if (renderer->isBox() && intersectsRect(renderer, frameRect))
134 addToOcclusions(toRenderBox(renderer), occlusions);
135 for (RenderObject* child = renderer->slowFirstChild(); child; child = child->nextSibling())
136 addTreeToOcclusions(child, frameRect, occlusions);
139 static const Element* topLayerAncestor(const Element* element)
141 while (element && !element->isInTopLayer())
142 element = element->parentOrShadowHostElement();
146 // Return a set of rectangles that should not be overdrawn by the
147 // plugin ("cutouts"). This helps implement the "iframe shim"
148 // technique of overlaying a windowed plugin with content from the
149 // page. In a nutshell, iframe elements should occlude plugins when
150 // they occur higher in the stacking order.
151 void getPluginOcclusions(Element* element, Widget* parentWidget, const IntRect& frameRect, Vector<IntRect>& occlusions)
153 RenderObject* pluginNode = element->renderer();
155 if (!pluginNode->style())
157 Vector<const RenderObject*> pluginZstack;
158 Vector<const RenderObject*> iframeZstack;
159 getObjectStack(pluginNode, &pluginZstack);
161 if (!parentWidget->isFrameView())
164 FrameView* parentFrameView = toFrameView(parentWidget);
166 // Occlusions by iframes.
167 const FrameView::ChildrenWidgetSet* children = parentFrameView->children();
168 for (FrameView::ChildrenWidgetSet::const_iterator it = children->begin(); it != children->end(); ++it) {
169 // We only care about FrameView's because iframes show up as FrameViews.
170 if (!(*it)->isFrameView())
173 const FrameView* frameView = toFrameView(it->get());
174 // Check to make sure we can get both the element and the RenderObject
175 // for this FrameView, if we can't just move on to the next object.
176 // FIXME: Plugin occlusion by remote frames is probably broken.
177 HTMLElement* element = frameView->frame().deprecatedLocalOwner();
178 if (!element || !element->renderer())
181 RenderObject* iframeRenderer = element->renderer();
183 if (isHTMLIFrameElement(*element) && intersectsRect(iframeRenderer, frameRect)) {
184 getObjectStack(iframeRenderer, &iframeZstack);
185 if (iframeIsAbovePlugin(iframeZstack, pluginZstack))
186 addToOcclusions(toRenderBox(iframeRenderer), occlusions);
190 // Occlusions by top layer elements.
191 // FIXME: There's no handling yet for the interaction between top layer and
192 // iframes. For example, a plugin in the top layer will be occluded by an
193 // iframe. And a plugin inside an iframe in the top layer won't be respected
194 // as being in the top layer.
195 const Element* ancestor = topLayerAncestor(element);
196 Document* document = parentFrameView->frame().document();
197 const WillBeHeapVector<RefPtrWillBeMember<Element> >& elements = document->topLayerElements();
198 size_t start = ancestor ? elements.find(ancestor) + 1 : 0;
199 for (size_t i = start; i < elements.size(); ++i)
200 addTreeToOcclusions(elements[i]->renderer(), frameRect, occlusions);