Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "core/dom/ElementVisibilityObserver.h" | 5 #include "core/dom/ElementVisibilityObserver.h" |
| 6 | 6 |
| 7 #include "core/dom/Element.h" | 7 #include "core/dom/Element.h" |
| 8 #include "core/dom/IntersectionObserverEntry.h" | 8 #include "core/dom/IntersectionObserverEntry.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "wtf/Functional.h" | 10 #include "wtf/Functional.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 bool isInRemoteFrame(const Document& document) { | 16 bool isInRemoteFrame(const Document& document) { |
| 17 DCHECK(document.frame()); | 17 DCHECK(document.frame()); |
| 18 Frame* mainFrame = document.frame()->tree().top(); | 18 Frame* mainFrame = document.frame()->tree().top(); |
| 19 return !mainFrame || mainFrame->isRemoteFrame(); | 19 return !mainFrame || mainFrame->isRemoteFrame(); |
| 20 } | 20 } |
| 21 | 21 |
| 22 } // anonymous namespace | 22 } // anonymous namespace |
| 23 | 23 |
| 24 VisibilityObserverBase::VisibilityObserverBase(Element* element) | |
| 25 : m_element(element) {} | |
| 26 | |
| 27 VisibilityObserverBase::~VisibilityObserverBase() {} | |
| 28 | |
| 29 void VisibilityObserverBase::stop() { | |
| 30 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | |
| 31 // so |m_intersectionObserver| may be null at this point after start(). | |
| 32 // Replace this early return with DCHECK when this has been fixed. See | |
| 33 // https://crbug.com/615156 | |
| 34 if (!m_intersectionObserver) | |
| 35 return; | |
| 36 | |
| 37 m_intersectionObserver->disconnect(); | |
| 38 m_intersectionObserver = nullptr; | |
| 39 } | |
| 40 | |
| 41 DEFINE_TRACE(VisibilityObserverBase) { | |
| 42 visitor->trace(m_element); | |
| 43 visitor->trace(m_intersectionObserver); | |
| 44 } | |
| 45 | |
| 24 ElementVisibilityObserver::ElementVisibilityObserver( | 46 ElementVisibilityObserver::ElementVisibilityObserver( |
| 25 Element* element, | 47 Element* element, |
| 26 std::unique_ptr<VisibilityCallback> callback) | 48 std::unique_ptr<VisibilityCallback> callback) |
| 27 : m_element(element), m_callback(std::move(callback)) {} | 49 : VisibilityObserverBase(element), m_callback(std::move(callback)) {} |
| 28 | 50 |
| 29 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | 51 ElementVisibilityObserver::~ElementVisibilityObserver() {} |
| 30 | 52 |
| 31 void ElementVisibilityObserver::start() { | 53 void ElementVisibilityObserver::start() { |
| 32 ExecutionContext* context = m_element->getExecutionContext(); | 54 ExecutionContext* context = m_element->getExecutionContext(); |
| 33 DCHECK(context->isDocument()); | 55 DCHECK(context->isDocument()); |
| 34 Document& document = toDocument(*context); | 56 Document& document = toDocument(*context); |
| 35 | 57 |
| 36 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. | 58 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. |
| 37 // Remove this early return when it's fixed. See https://crbug.com/615156 | 59 // Remove this early return when it's fixed. See https://crbug.com/615156 |
| 38 if (isInRemoteFrame(document)) { | 60 if (isInRemoteFrame(document)) { |
| 39 m_element.release(); | 61 m_element.release(); |
| 40 return; | 62 return; |
| 41 } | 63 } |
| 42 | 64 |
| 43 DCHECK(!m_intersectionObserver); | 65 DCHECK(!m_intersectionObserver); |
| 44 m_intersectionObserver = IntersectionObserver::create( | 66 m_intersectionObserver = IntersectionObserver::create( |
| 45 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 67 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), |
| 46 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, | 68 &document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, |
| 47 wrapWeakPersistent(this))); | 69 wrapWeakPersistent(this))); |
| 48 DCHECK(m_intersectionObserver); | 70 DCHECK(m_intersectionObserver); |
| 49 m_intersectionObserver->setInitialState( | 71 m_intersectionObserver->setInitialState( |
| 50 IntersectionObserver::InitialState::kAuto); | 72 IntersectionObserver::InitialState::kAuto); |
| 51 m_intersectionObserver->observe(m_element.release()); | 73 m_intersectionObserver->observe(m_element.release()); |
| 52 } | 74 } |
| 53 | 75 |
| 54 void ElementVisibilityObserver::stop() { | |
| 55 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | |
| 56 // so |m_intersectionObserver| may be null at this point after start(). | |
| 57 // Replace this early return with DCHECK when this has been fixed. See | |
| 58 // https://crbug.com/615156 | |
| 59 if (!m_intersectionObserver) | |
| 60 return; | |
| 61 | |
| 62 m_intersectionObserver->disconnect(); | |
| 63 m_intersectionObserver = nullptr; | |
| 64 } | |
| 65 | |
| 66 void ElementVisibilityObserver::deliverObservationsForTesting() { | 76 void ElementVisibilityObserver::deliverObservationsForTesting() { |
| 67 m_intersectionObserver->deliver(); | 77 m_intersectionObserver->deliver(); |
| 68 } | 78 } |
| 69 | 79 |
| 70 DEFINE_TRACE(ElementVisibilityObserver) { | |
| 71 visitor->trace(m_element); | |
| 72 visitor->trace(m_intersectionObserver); | |
| 73 } | |
| 74 | |
| 75 void ElementVisibilityObserver::onVisibilityChanged( | 80 void ElementVisibilityObserver::onVisibilityChanged( |
| 76 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 81 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 77 bool isVisible = entries.last()->intersectionRatio() > 0.f; | 82 bool isVisible = entries.last()->intersectionRatio() > 0.f; |
| 78 (*m_callback.get())(isVisible); | 83 (*m_callback.get())(isVisible); |
| 79 } | 84 } |
| 80 | 85 |
| 86 ViewportIntersectionObserver::ViewportIntersectionObserver( | |
| 87 Element* element, | |
| 88 std::unique_ptr<RatioChangeCallback> callback) | |
| 89 : VisibilityObserverBase(element), m_callback(std::move(callback)) {} | |
| 90 | |
| 91 ViewportIntersectionObserver::~ViewportIntersectionObserver() {} | |
| 92 | |
| 93 void ViewportIntersectionObserver::start() { | |
| 94 ExecutionContext* context = m_element->getExecutionContext(); | |
| 95 DCHECK(context->isDocument()); | |
| 96 Document& document = toDocument(*context); | |
| 97 | |
| 98 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. | |
| 99 // Remove this early return when it's fixed. See https://crbug.com/615156 | |
| 100 if (isInRemoteFrame(document)) { | |
| 101 m_element.release(); | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 DCHECK(!m_intersectionObserver); | |
| 106 m_intersectionObserver = IntersectionObserver::create( | |
| 107 Vector<Length>(), &document, | |
| 108 WTF::bind(&ViewportIntersectionObserver::onVisibilityChanged, | |
| 109 wrapWeakPersistent(this))); | |
| 110 DCHECK(m_intersectionObserver); | |
| 111 m_intersectionObserver->setInitialState( | |
| 112 IntersectionObserver::InitialState::kAuto); | |
| 113 m_intersectionObserver->observe(m_element.release()); | |
| 114 } | |
| 115 | |
| 116 void ViewportIntersectionObserver::onVisibilityChanged( | |
| 117 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | |
| 118 IntersectionObserverEntry* entry = entries.last(); | |
| 119 WebRect rootRect; | |
| 120 if (entry && entry->rootBounds()) { | |
| 121 rootRect = | |
| 122 WebRect(entry->rootBounds()->top(), entry->rootBounds()->left(), | |
|
miu
2016/11/16 02:40:26
Please fix: top() and left() are reversed.
xjz
2016/11/16 07:45:55
Done. Thanks for pointing this out!
| |
| 123 entry->rootBounds()->width(), entry->rootBounds()->height()); | |
| 124 } | |
| 125 WebRect intersectRect; | |
| 126 if (entry && entry->intersectionRect()) { | |
| 127 intersectRect = WebRect(entry->intersectionRect()->top(), | |
|
miu
2016/11/16 02:40:26
ditto: top() and left() are reversed.
xjz
2016/11/16 07:45:55
Done.
| |
| 128 entry->intersectionRect()->left(), | |
| 129 entry->intersectionRect()->width(), | |
| 130 entry->intersectionRect()->height()); | |
| 131 } | |
| 132 | |
| 133 (*m_callback.get())(rootRect, intersectRect); | |
| 134 } | |
| 135 | |
| 81 } // namespace blink | 136 } // namespace blink |
| OLD | NEW |