OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "config.h" |
| 6 #include "core/dom/IntersectionObserver.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "core/dom/Document.h" |
| 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "core/dom/ExecutionContext.h" |
| 12 #include "core/dom/IntersectionObserverCallback.h" |
| 13 #include "core/dom/IntersectionObserverEntry.h" |
| 14 #include "core/dom/IntersectionObserverInit.h" |
| 15 #include "platform/Timer.h" |
| 16 #include "wtf/MainThread.h" |
| 17 #include <algorithm> |
| 18 |
| 19 namespace blink { |
| 20 |
| 21 PassRefPtrWillBeRawPtr<IntersectionObserver> IntersectionObserver::create(Docume
nt* document, const IntersectionObserverInit& observerInit, PassOwnPtrWillBeRawP
tr<IntersectionObserverCallback> callback) |
| 22 { |
| 23 ASSERT(isMainThread()); |
| 24 return adoptRefWillBeNoop(new IntersectionObserver(document, observerInit, c
allback)); |
| 25 } |
| 26 |
| 27 IntersectionObserver::IntersectionObserver(Document* document, const Intersectio
nObserverInit& observerInit, PassOwnPtrWillBeRawPtr<IntersectionObserverCallback
> callback) |
| 28 : m_callback(callback) |
| 29 , m_document(document) |
| 30 { |
| 31 } |
| 32 |
| 33 IntersectionObserver::~IntersectionObserver() |
| 34 { |
| 35 } |
| 36 |
| 37 void IntersectionObserver::observe(Element* target, ExceptionState& exceptionSta
te) |
| 38 { |
| 39 if (!m_document) { |
| 40 exceptionState.throwTypeError("Window may be destroyed? Document is inva
lid."); |
| 41 return; |
| 42 } |
| 43 if (!target) { |
| 44 exceptionState.throwTypeError("Invalid observation target (null)."); |
| 45 return; |
| 46 } |
| 47 |
| 48 target->registerIntersectionObserver(this); |
| 49 m_targets.append(target); |
| 50 } |
| 51 |
| 52 void IntersectionObserver::unobserve(Element* target, ExceptionState& exceptionS
tate) |
| 53 { |
| 54 if (!m_document) { |
| 55 exceptionState.throwTypeError("Window may be destroyed? Document is inva
lid."); |
| 56 return; |
| 57 } |
| 58 |
| 59 if (!target) |
| 60 return; |
| 61 |
| 62 size_t index = m_targets.find(target); |
| 63 if (index == WTF::kNotFound) |
| 64 return; |
| 65 |
| 66 target->unregisterIntersectionObserver(this); |
| 67 m_targets.remove(index); |
| 68 |
| 69 if (m_targets.isEmpty()) |
| 70 m_entries.clear(); |
| 71 } |
| 72 |
| 73 void IntersectionObserver::disconnect() |
| 74 { |
| 75 m_entries.clear(); |
| 76 for (auto target : m_targets) { |
| 77 if (target) |
| 78 target->unregisterIntersectionObserver(this); |
| 79 } |
| 80 m_targets.clear(); |
| 81 } |
| 82 |
| 83 IntersectionObserverEntryVector IntersectionObserver::takeRecords() |
| 84 { |
| 85 IntersectionObserverEntryVector entries; |
| 86 entries.swap(m_entries); |
| 87 return entries; |
| 88 } |
| 89 |
| 90 void IntersectionObserver::enqueueIntersectionObserverEntry(PassRefPtrWillBeRawP
tr<IntersectionObserverEntry> entry) |
| 91 { |
| 92 ASSERT(isMainThread()); |
| 93 m_entries.append(entry); |
| 94 if (m_document) |
| 95 m_document->activateIntersectionObserver(*this); |
| 96 } |
| 97 |
| 98 bool IntersectionObserver::shouldBeSuspended() const |
| 99 { |
| 100 return m_callback->executionContext() && m_callback->executionContext()->act
iveDOMObjectsAreSuspended(); |
| 101 } |
| 102 |
| 103 void IntersectionObserver::deliver() |
| 104 { |
| 105 ASSERT(!shouldBeSuspended()); |
| 106 |
| 107 if (m_entries.isEmpty()) |
| 108 return; |
| 109 |
| 110 IntersectionObserverEntryVector entries; |
| 111 entries.swap(m_entries); |
| 112 m_callback->handleEvent(entries, this); |
| 113 } |
| 114 |
| 115 DEFINE_TRACE(IntersectionObserver) |
| 116 { |
| 117 visitor->trace(m_callback); |
| 118 visitor->trace(m_document); |
| 119 visitor->trace(m_entries); |
| 120 visitor->trace(m_targets); |
| 121 } |
| 122 |
| 123 } // namespace blink |
OLD | NEW |