IFrameShimSupport.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "config.h"
  31. #include "IFrameShimSupport.h"
  32. #include "Element.h"
  33. #include "Frame.h"
  34. #include "FrameView.h"
  35. #include "HTMLElement.h"
  36. #include "HTMLFrameOwnerElement.h"
  37. #include "HTMLNames.h"
  38. #include "RenderBox.h"
  39. #include "RenderObject.h"
  40. #include "Widget.h"
  41. #include <wtf/HashSet.h>
  42. // This file provides plugin-related utility functions for iframe shims and is shared by platforms that inherit
  43. // from PluginView (e.g. Qt) and those that do not (e.g. Chromium).
  44. namespace WebCore {
  45. static void getObjectStack(const RenderObject* ro, Vector<const RenderObject*>* roStack)
  46. {
  47. roStack->clear();
  48. while (ro) {
  49. roStack->append(ro);
  50. ro = ro->parent();
  51. }
  52. }
  53. // Returns true if stack1 is at or above stack2
  54. static bool iframeIsAbovePlugin(const Vector<const RenderObject*>& iframeZstack, const Vector<const RenderObject*>& pluginZstack)
  55. {
  56. for (size_t i = 0; i < iframeZstack.size() && i < pluginZstack.size(); i++) {
  57. // The root is at the end of these stacks. We want to iterate
  58. // root-downwards so we index backwards from the end.
  59. const RenderObject* ro1 = iframeZstack[iframeZstack.size() - 1 - i];
  60. const RenderObject* ro2 = pluginZstack[pluginZstack.size() - 1 - i];
  61. if (ro1 != ro2) {
  62. // When we find nodes in the stack that are not the same, then
  63. // we've found the nodes just below the lowest comment ancestor.
  64. // Determine which should be on top.
  65. // See if z-index determines an order.
  66. if (ro1->style() && ro2->style()) {
  67. int z1 = ro1->style()->zIndex();
  68. int z2 = ro2->style()->zIndex();
  69. if (z1 > z2)
  70. return true;
  71. if (z1 < z2)
  72. return false;
  73. }
  74. // If the plugin does not have an explicit z-index it stacks behind the iframe.
  75. // This is for maintaining compatibility with IE.
  76. if (ro2->style()->position() == StaticPosition) {
  77. // The 0'th elements of these RenderObject arrays represent the plugin node and
  78. // the iframe.
  79. const RenderObject* pluginRenderObject = pluginZstack[0];
  80. const RenderObject* iframeRenderObject = iframeZstack[0];
  81. if (pluginRenderObject->style() && iframeRenderObject->style()) {
  82. if (pluginRenderObject->style()->zIndex() > iframeRenderObject->style()->zIndex())
  83. return false;
  84. }
  85. return true;
  86. }
  87. // Inspect the document order. Later order means higher stacking.
  88. const RenderObject* parent = ro1->parent();
  89. if (!parent)
  90. return false;
  91. ASSERT(parent == ro2->parent());
  92. for (const RenderObject* ro = parent->firstChild(); ro; ro = ro->nextSibling()) {
  93. if (ro == ro1)
  94. return false;
  95. if (ro == ro2)
  96. return true;
  97. }
  98. ASSERT(false); // We should have seen ro1 and ro2 by now.
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. // Return a set of rectangles that should not be overdrawn by the
  105. // plugin ("cutouts"). This helps implement the "iframe shim"
  106. // technique of overlaying a windowed plugin with content from the
  107. // page. In a nutshell, iframe elements should occlude plugins when
  108. // they occur higher in the stacking order.
  109. void getPluginOcclusions(Element* element, Widget* parentWidget, const IntRect& frameRect, Vector<IntRect>& occlusions)
  110. {
  111. RenderObject* pluginNode = element->renderer();
  112. ASSERT(pluginNode);
  113. if (!pluginNode->style())
  114. return;
  115. Vector<const RenderObject*> pluginZstack;
  116. Vector<const RenderObject*> iframeZstack;
  117. getObjectStack(pluginNode, &pluginZstack);
  118. if (!parentWidget->isFrameView())
  119. return;
  120. FrameView* parentFrameView = toFrameView(parentWidget);
  121. const HashSet<RefPtr<Widget> >* children = parentFrameView->children();
  122. for (HashSet<RefPtr<Widget> >::const_iterator it = children->begin(); it != children->end(); ++it) {
  123. // We only care about FrameView's because iframes show up as FrameViews.
  124. if (!(*it)->isFrameView())
  125. continue;
  126. const FrameView* frameView = toFrameView((*it).get());
  127. // Check to make sure we can get both the element and the RenderObject
  128. // for this FrameView, if we can't just move on to the next object.
  129. if (!frameView->frame() || !frameView->frame()->ownerElement()
  130. || !frameView->frame()->ownerElement()->renderer())
  131. continue;
  132. HTMLElement* element = frameView->frame()->ownerElement();
  133. RenderObject* iframeRenderer = element->renderer();
  134. if (element->hasTagName(HTMLNames::iframeTag)
  135. && iframeRenderer->absoluteBoundingBoxRectIgnoringTransforms().intersects(frameRect)
  136. && (!iframeRenderer->style() || iframeRenderer->style()->visibility() == VISIBLE)) {
  137. getObjectStack(iframeRenderer, &iframeZstack);
  138. if (iframeIsAbovePlugin(iframeZstack, pluginZstack)) {
  139. IntPoint point = roundedIntPoint(iframeRenderer->localToAbsolute());
  140. RenderBox* rbox = toRenderBox(iframeRenderer);
  141. IntSize size(rbox->width(), rbox->height());
  142. occlusions.append(IntRect(point, size));
  143. }
  144. }
  145. }
  146. }
  147. } // namespace WebCore