Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: Source/core/dom/IntersectionObserver.cpp

Issue 1330633003: Intersection Observer first draft Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Include new files as well. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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, PassOwnPtrWillBeRawPtr<IntersectionObserverCallback> callback)
22 {
23 ASSERT(isMainThread());
24 return adoptRefWillBeNoop(new IntersectionObserver(document, callback));
25 }
26
27 IntersectionObserver::IntersectionObserver(Document* document, PassOwnPtrWillBeR awPtr<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, const IntersectionObserverIn it& observerInit, ExceptionState& exceptionState)
38 {
39 if (!m_document) {
40 exceptionState.throwTypeError("Window may be destroyed? Document is inva lid.");
41 return;
42 }
43
44 target->registerIntersectionObserver(this);
45 m_targets.append(target);
46 }
47
48 void IntersectionObserver::disconnect()
49 {
50 m_entries.clear();
51 for (auto target : m_targets) {
52 if (target)
53 target->unregisterIntersectionObserver(this);
54 }
55 m_targets.clear();
56 }
57
58 IntersectionObserverEntryVector IntersectionObserver::takeRecords()
59 {
60 IntersectionObserverEntryVector entries;
61 entries.swap(m_entries);
62 return entries;
63 }
64
65 void IntersectionObserver::enqueueIntersectionObserverEntry(PassRefPtrWillBeRawP tr<IntersectionObserverEntry> entry)
66 {
67 ASSERT(isMainThread());
68 m_entries.append(entry);
69 if (m_document)
70 m_document->activateIntersectionObserver(*this);
71 }
72
73 bool IntersectionObserver::shouldBeSuspended() const
74 {
75 return m_callback->executionContext() && m_callback->executionContext()->act iveDOMObjectsAreSuspended();
76 }
77
78 void IntersectionObserver::deliver()
79 {
80 ASSERT(!shouldBeSuspended());
81
82 if (m_entries.isEmpty())
83 return;
84
85 IntersectionObserverEntryVector entries;
86 entries.swap(m_entries);
87 m_callback->handleEvent(entries, this);
88 }
89
90 DEFINE_TRACE(IntersectionObserver)
91 {
92 visitor->trace(m_callback);
93 visitor->trace(m_document);
94 visitor->trace(m_entries);
95 visitor->trace(m_targets);
96 }
97
98 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698