Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/dom/ElementVisibilityObserver.h" | |
| 6 | |
| 7 #include "core/dom/Element.h" | |
| 8 #include "core/dom/IntersectionObserverCallback.h" | |
| 9 #include "core/frame/LocalFrame.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 class ElementVisibilityObserver::ElementVisibilityCallback : public Intersection ObserverCallback { | |
| 14 public: | |
| 15 ElementVisibilityCallback(ElementVisibilityObserver* observer) | |
| 16 : m_observer(observer) { | |
| 17 } | |
| 18 | |
| 19 void handleEvent(const HeapVector<Member<IntersectionObserverEntry>>& entries, IntersectionObserver&) override { | |
| 20 DCHECK_EQ(entries.size(), 1u); | |
| 21 | |
| 22 bool isVisible = entries[0]->intersectionRatio() > 0.f; | |
| 23 (*m_observer->m_callback.get())(isVisible); | |
| 24 } | |
| 25 | |
| 26 ExecutionContext* getExecutionContext() const override { | |
| 27 return &m_observer->m_element->document(); | |
| 28 } | |
| 29 | |
| 30 DEFINE_INLINE_TRACE() { | |
| 31 IntersectionObserverCallback::trace(visitor); | |
| 32 visitor->trace(m_observer); | |
| 33 } | |
| 34 | |
| 35 private: | |
| 36 Member<ElementVisibilityObserver> m_observer; | |
| 37 }; | |
| 38 | |
| 39 ElementVisibilityObserver::ElementVisibilityObserver(Element* element, std::uniq ue_ptr<VisibilityCallback> callback) | |
| 40 : m_element(element) | |
| 41 , m_callback(std::move(callback)) | |
| 42 , m_intersectionObserverCallback(new ElementVisibilityCallback(this)) | |
| 43 { | |
| 44 } | |
| 45 | |
| 46 ElementVisibilityObserver::~ElementVisibilityObserver() = default; | |
| 47 | |
| 48 void ElementVisibilityObserver::start() | |
| 49 { | |
| 50 Node* root = nullptr; | |
|
Sami
2016/06/10 12:42:25
DCHECK(!m_intersectionObserver)?
mlamouri (slow - plz ping)
2016/06/10 15:14:01
Good idea!
| |
| 51 Frame* mainFrame = m_element->document().frame()->tree().top(); | |
| 52 if (mainFrame && mainFrame->isLocalFrame()) | |
| 53 root = toLocalFrame(mainFrame)->document(); | |
| 54 | |
| 55 m_intersectionObserver = new IntersectionObserver(*m_intersectionObserverCal lback, *root, Vector<Length>(), Vector<float>({0.f})); | |
| 56 m_intersectionObserver->observe(m_element); | |
| 57 } | |
| 58 | |
| 59 void ElementVisibilityObserver::stop() | |
| 60 { | |
|
Sami
2016/06/10 12:42:25
DCHECK(m_intersectionObserver)?
mlamouri (slow - plz ping)
2016/06/10 15:14:01
Done.
| |
| 61 m_intersectionObserver->unobserve(m_element); | |
| 62 m_intersectionObserver = nullptr; | |
| 63 } | |
| 64 | |
| 65 DEFINE_TRACE(ElementVisibilityObserver) | |
| 66 { | |
| 67 visitor->trace(m_element); | |
| 68 visitor->trace(m_intersectionObserver); | |
| 69 visitor->trace(m_intersectionObserverCallback); | |
| 70 } | |
| 71 | |
| 72 } // namespace blink | |
| OLD | NEW |