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 "IFrameShimSupport.h"
36 #include "FrameView.h"
37 #include "HTMLElement.h"
38 #include "HTMLFrameOwnerElement.h"
39 #include "HTMLNames.h"
40 #include "RenderBox.h"
41 #include "RenderObject.h"
44 #include <wtf/HashSet.h>
46 // This file provides plugin-related utility functions for iframe shims and is shared by platforms that inherit
47 // from PluginView (e.g. Qt) and those that do not (e.g. Chromium).
51 static void getObjectStack(const RenderObject* ro, Vector<const RenderObject*>* roStack)
60 // Returns true if stack1 is at or above stack2
61 static bool iframeIsAbovePlugin(const Vector<const RenderObject*>& iframeZstack, const Vector<const RenderObject*>& pluginZstack)
63 for (size_t i = 0; i < iframeZstack.size() && i < pluginZstack.size(); i++) {
64 // The root is at the end of these stacks. We want to iterate
65 // root-downwards so we index backwards from the end.
66 const RenderObject* ro1 = iframeZstack[iframeZstack.size() - 1 - i];
67 const RenderObject* ro2 = pluginZstack[pluginZstack.size() - 1 - i];
70 // When we find nodes in the stack that are not the same, then
71 // we've found the nodes just below the lowest comment ancestor.
72 // Determine which should be on top.
74 // See if z-index determines an order.
75 if (ro1->style() && ro2->style()) {
76 int z1 = ro1->style()->zIndex();
77 int z2 = ro2->style()->zIndex();
84 // If the plugin does not have an explicit z-index it stacks behind the iframe.
85 // This is for maintaining compatibility with IE.
86 if (ro2->style()->position() == StaticPosition) {
87 // The 0'th elements of these RenderObject arrays represent the plugin node and
89 const RenderObject* pluginRenderObject = pluginZstack[0];
90 const RenderObject* iframeRenderObject = iframeZstack[0];
92 if (pluginRenderObject->style() && iframeRenderObject->style()) {
93 if (pluginRenderObject->style()->zIndex() > iframeRenderObject->style()->zIndex())
99 // Inspect the document order. Later order means higher stacking.
100 const RenderObject* parent = ro1->parent();
103 ASSERT(parent == ro2->parent());
105 for (const RenderObject* ro = parent->firstChild(); ro; ro = ro->nextSibling()) {
111 ASSERT(false); // We should have seen ro1 and ro2 by now.
118 // Return a set of rectangles that should not be overdrawn by the
119 // plugin ("cutouts"). This helps implement the "iframe shim"
120 // technique of overlaying a windowed plugin with content from the
121 // page. In a nutshell, iframe elements should occlude plugins when
122 // they occur higher in the stacking order.
123 void getPluginOcclusions(Element* element, Widget* parentWidget, const IntRect& frameRect, Vector<IntRect>& occlusions)
125 RenderObject* pluginNode = element->renderer();
127 if (!pluginNode->style())
129 Vector<const RenderObject*> pluginZstack;
130 Vector<const RenderObject*> iframeZstack;
131 getObjectStack(pluginNode, &pluginZstack);
133 if (!parentWidget->isFrameView())
136 FrameView* parentFrameView = static_cast<FrameView*>(parentWidget);
138 const HashSet<RefPtr<Widget> >* children = parentFrameView->children();
139 for (HashSet<RefPtr<Widget> >::const_iterator it = children->begin(); it != children->end(); ++it) {
140 // We only care about FrameView's because iframes show up as FrameViews.
141 if (!(*it)->isFrameView())
144 const FrameView* frameView = static_cast<const FrameView*>((*it).get());
145 // Check to make sure we can get both the element and the RenderObject
146 // for this FrameView, if we can't just move on to the next object.
147 if (!frameView->frame() || !frameView->frame()->ownerElement()
148 || !frameView->frame()->ownerElement()->renderer())
151 HTMLElement* element = frameView->frame()->ownerElement();
152 RenderObject* iframeRenderer = element->renderer();
154 if (element->hasTagName(HTMLNames::iframeTag)
155 && iframeRenderer->absoluteBoundingBoxRect().intersects(frameRect)
156 && (!iframeRenderer->style() || iframeRenderer->style()->visibility() == VISIBLE)) {
157 getObjectStack(iframeRenderer, &iframeZstack);
158 if (iframeIsAbovePlugin(iframeZstack, pluginZstack)) {
159 IntPoint point = roundedIntPoint(iframeRenderer->localToAbsolute());
160 RenderBox* rbox = toRenderBox(iframeRenderer);
161 IntSize size(rbox->width(), rbox->height());
162 occlusions.append(IntRect(point, size));
168 } // namespace WebCore