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(Element* element) { | 16 bool isInRemoteFrame(Document* document) { |
|
esprehn
2016/10/11 22:06:45
reference then you don't need the DCHECK below?
| |
| 17 Frame* mainFrame = element->document().frame()->tree().top(); | 17 DCHECK(document); |
| 18 DCHECK(document->frame()); | |
| 19 Frame* mainFrame = document->frame()->tree().top(); | |
| 18 return !mainFrame || mainFrame->isRemoteFrame(); | 20 return !mainFrame || mainFrame->isRemoteFrame(); |
| 19 } | 21 } |
| 20 | 22 |
| 21 } // anonymous namespace | 23 } // anonymous namespace |
| 22 | 24 |
| 23 ElementVisibilityObserver::ElementVisibilityObserver( | 25 ElementVisibilityObserver::ElementVisibilityObserver( |
| 24 Element* element, | 26 Element* element, |
| 25 std::unique_ptr<VisibilityCallback> callback) | 27 std::unique_ptr<VisibilityCallback> callback) |
| 26 : m_element(element), m_callback(std::move(callback)) {} | 28 : m_element(element), m_callback(std::move(callback)) {} |
| 27 | 29 |
| 28 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | 30 ElementVisibilityObserver::~ElementVisibilityObserver() = default; |
| 29 | 31 |
| 30 void ElementVisibilityObserver::start() { | 32 void ElementVisibilityObserver::start() { |
| 33 ExecutionContext* context = m_element->getExecutionContext(); | |
| 34 DCHECK(context->isDocument()); | |
| 35 Document* document = toDocument(context); | |
|
esprehn
2016/10/11 22:06:45
Document& document = toDocument(*context);
| |
| 36 | |
| 31 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. | 37 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame. |
| 32 // Remove this early return when it's fixed. See https://crbug.com/615156 | 38 // Remove this early return when it's fixed. See https://crbug.com/615156 |
| 33 if (isInRemoteFrame(m_element)) { | 39 if (isInRemoteFrame(document)) { |
| 34 m_element.release(); | 40 m_element.release(); |
| 35 return; | 41 return; |
| 36 } | 42 } |
| 37 | 43 |
| 38 DCHECK(!m_intersectionObserver); | 44 DCHECK(!m_intersectionObserver); |
| 39 m_intersectionObserver = IntersectionObserver::create( | 45 m_intersectionObserver = IntersectionObserver::create( |
| 40 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), | 46 Vector<Length>(), Vector<float>({std::numeric_limits<float>::min()}), |
| 41 &m_element->document(), | 47 document, WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, |
| 42 WTF::bind(&ElementVisibilityObserver::onVisibilityChanged, | 48 wrapWeakPersistent(this))); |
| 43 wrapWeakPersistent(this))); | |
| 44 DCHECK(m_intersectionObserver); | 49 DCHECK(m_intersectionObserver); |
| 45 m_intersectionObserver->observe(m_element.release()); | 50 m_intersectionObserver->observe(m_element.release()); |
| 46 } | 51 } |
| 47 | 52 |
| 48 void ElementVisibilityObserver::stop() { | 53 void ElementVisibilityObserver::stop() { |
| 49 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, | 54 // TODO(zqzhang): IntersectionObserver does not work for RemoteFrame, |
| 50 // so |m_intersectionObserver| may be null at this point after start(). | 55 // so |m_intersectionObserver| may be null at this point after start(). |
| 51 // Replace this early return with DCHECK when this has been fixed. See | 56 // Replace this early return with DCHECK when this has been fixed. See |
| 52 // https://crbug.com/615156 | 57 // https://crbug.com/615156 |
| 53 if (!m_intersectionObserver) | 58 if (!m_intersectionObserver) |
| 54 return; | 59 return; |
| 55 | 60 |
| 56 m_intersectionObserver->disconnect(); | 61 m_intersectionObserver->disconnect(); |
| 57 m_intersectionObserver = nullptr; | 62 m_intersectionObserver = nullptr; |
| 58 } | 63 } |
| 59 | 64 |
| 60 DEFINE_TRACE(ElementVisibilityObserver) { | 65 DEFINE_TRACE(ElementVisibilityObserver) { |
| 61 visitor->trace(m_element); | 66 visitor->trace(m_element); |
| 62 visitor->trace(m_intersectionObserver); | 67 visitor->trace(m_intersectionObserver); |
| 63 } | 68 } |
| 64 | 69 |
| 65 void ElementVisibilityObserver::onVisibilityChanged( | 70 void ElementVisibilityObserver::onVisibilityChanged( |
| 66 const HeapVector<Member<IntersectionObserverEntry>>& entries) { | 71 const HeapVector<Member<IntersectionObserverEntry>>& entries) { |
| 67 bool isVisible = entries.last()->intersectionRatio() > 0.f; | 72 bool isVisible = entries.last()->intersectionRatio() > 0.f; |
| 68 (*m_callback.get())(isVisible); | 73 (*m_callback.get())(isVisible); |
| 69 } | 74 } |
| 70 | 75 |
| 71 } // namespace blink | 76 } // namespace blink |
| OLD | NEW |