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 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. 20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "web/PageOverlayList.h" 31 32 #include "public/web/WebPageOverlay.h" 33 #include "web/PageOverlay.h" 34 #include "web/WebViewImpl.h" 35 36 namespace blink { 37 38 PassOwnPtr<PageOverlayList> PageOverlayList::create(WebViewImpl* viewImpl) 39 { 40 return adoptPtr(new PageOverlayList(viewImpl)); 41 } 42 43 PageOverlayList::PageOverlayList(WebViewImpl* viewImpl) 44 : m_viewImpl(viewImpl) 45 { 46 } 47 48 PageOverlayList::~PageOverlayList() 49 { 50 } 51 52 bool PageOverlayList::add(WebPageOverlay* overlay, int zOrder) 53 { 54 bool added = false; 55 size_t index = find(overlay); 56 if (index == WTF::kNotFound) { 57 OwnPtr<PageOverlay> pageOverlay = PageOverlay::create(m_viewImpl, overlay); 58 m_pageOverlays.append(pageOverlay.release()); 59 index = m_pageOverlays.size() - 1; 60 added = true; 61 } 62 63 PageOverlay* pageOverlay = m_pageOverlays[index].get(); 64 pageOverlay->setZOrder(zOrder); 65 66 // Adjust page overlay list order based on their z-order numbers. We first 67 // check if we need to move the overlay up and do so if needed. Otherwise, 68 // check if we need to move it down. 69 bool zOrderChanged = false; 70 for (size_t i = index; i + 1 < m_pageOverlays.size(); ++i) { 71 if (m_pageOverlays[i]->zOrder() >= m_pageOverlays[i + 1]->zOrder()) { 72 m_pageOverlays[i].swap(m_pageOverlays[i + 1]); 73 zOrderChanged = true; 74 } 75 } 76 77 if (!zOrderChanged) { 78 for (size_t i = index; i >= 1; --i) { 79 if (m_pageOverlays[i]->zOrder() < m_pageOverlays[i - 1]->zOrder()) { 80 m_pageOverlays[i].swap(m_pageOverlays[i - 1]); 81 zOrderChanged = true; 82 } 83 } 84 } 85 86 // If we did move the overlay, that means z-order is changed and we need to 87 // update overlay layers' z-order. Otherwise, just update current overlay. 88 if (zOrderChanged) { 89 for (size_t i = 0; i < m_pageOverlays.size(); ++i) 90 m_pageOverlays[i]->clear(); 91 update(); 92 } else 93 pageOverlay->update(); 94 95 return added; 96 } 97 98 bool PageOverlayList::remove(WebPageOverlay* overlay) 99 { 100 size_t index = find(overlay); 101 if (index == WTF::kNotFound) 102 return false; 103 104 m_pageOverlays[index]->clear(); 105 m_pageOverlays.remove(index); 106 return true; 107 } 108 109 void PageOverlayList::update() 110 { 111 for (size_t i = 0; i < m_pageOverlays.size(); ++i) 112 m_pageOverlays[i]->update(); 113 } 114 115 void PageOverlayList::paintWebFrame(GraphicsContext& gc) 116 { 117 for (size_t i = 0; i < m_pageOverlays.size(); ++i) 118 m_pageOverlays[i]->paintWebFrame(gc); 119 } 120 121 size_t PageOverlayList::find(WebPageOverlay* overlay) 122 { 123 for (size_t i = 0; i < m_pageOverlays.size(); ++i) { 124 if (m_pageOverlays[i]->overlay() == overlay) 125 return i; 126 } 127 return WTF::kNotFound; 128 } 129 130 size_t PageOverlayList::findGraphicsLayer(GraphicsLayer* layer) 131 { 132 for (size_t i = 0; i < m_pageOverlays.size(); ++i) { 133 if (m_pageOverlays[i]->graphicsLayer() == layer) 134 return i; 135 } 136 return WTF::kNotFound; 137 } 138 139 } // namespace blink 140