initial import
[vuplus_webkit] / Source / WebCore / plugins / IFrameShimSupport.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "IFrameShimSupport.h"
33
34 #include "Element.h"
35 #include "Frame.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"
42 #include "Widget.h"
43
44 #include <wtf/HashSet.h>
45
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).
48
49 namespace WebCore {
50
51 static void getObjectStack(const RenderObject* ro, Vector<const RenderObject*>* roStack)
52 {
53     roStack->clear();
54     while (ro) {
55         roStack->append(ro);
56         ro = ro->parent();
57     }
58 }
59
60 // Returns true if stack1 is at or above stack2
61 static bool iframeIsAbovePlugin(const Vector<const RenderObject*>& iframeZstack, const Vector<const RenderObject*>& pluginZstack)
62 {
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];
68
69         if (ro1 != ro2) {
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.
73
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();
78                 if (z1 > z2)
79                     return true;
80                 if (z1 < z2)
81                     return false;
82             }
83
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
88                 // the iframe.
89                 const RenderObject* pluginRenderObject = pluginZstack[0];
90                 const RenderObject* iframeRenderObject = iframeZstack[0];
91
92                 if (pluginRenderObject->style() && iframeRenderObject->style()) {
93                     if (pluginRenderObject->style()->zIndex() > iframeRenderObject->style()->zIndex())
94                         return false;
95                 }
96                 return true;
97             }
98
99             // Inspect the document order.  Later order means higher stacking.
100             const RenderObject* parent = ro1->parent();
101             if (!parent)
102                 return false;
103             ASSERT(parent == ro2->parent());
104
105             for (const RenderObject* ro = parent->firstChild(); ro; ro = ro->nextSibling()) {
106                 if (ro == ro1)
107                     return false;
108                 if (ro == ro2)
109                     return true;
110             }
111             ASSERT(false); // We should have seen ro1 and ro2 by now.
112             return false;
113         }
114     }
115     return true;
116 }
117
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)
124 {
125     RenderObject* pluginNode = element->renderer();
126     ASSERT(pluginNode);
127     if (!pluginNode->style())
128         return;
129     Vector<const RenderObject*> pluginZstack;
130     Vector<const RenderObject*> iframeZstack;
131     getObjectStack(pluginNode, &pluginZstack);
132
133     if (!parentWidget->isFrameView())
134         return;
135
136     FrameView* parentFrameView = static_cast<FrameView*>(parentWidget);
137
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())
142             continue;
143
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())
149             continue;
150
151         HTMLElement* element = frameView->frame()->ownerElement();
152         RenderObject* iframeRenderer = element->renderer();
153
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));
163             }
164         }
165     }
166 }
167
168 } // namespace WebCore