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/IntersectionObserverController.h" | 5 #include "core/dom/IntersectionObserverController.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/IdleRequestOptions.h" | 8 #include "core/dom/IdleRequestOptions.h" |
9 #include "core/dom/TaskRunnerHelper.h" | |
9 #include "platform/TraceEvent.h" | 10 #include "platform/TraceEvent.h" |
11 #include "platform/scheduler/CancellableTaskFactory.h" | |
10 | 12 |
11 namespace blink { | 13 namespace blink { |
12 | 14 |
13 IntersectionObserverController* IntersectionObserverController::create(Document* document) | 15 IntersectionObserverController* IntersectionObserverController::create(Document* document) |
14 { | 16 { |
15 IntersectionObserverController* result = new IntersectionObserverController( document); | 17 IntersectionObserverController* result = new IntersectionObserverController( document); |
16 result->suspendIfNeeded(); | 18 result->suspendIfNeeded(); |
17 return result; | 19 return result; |
18 } | 20 } |
19 | 21 |
20 IntersectionObserverController::IntersectionObserverController(Document* documen t) | 22 IntersectionObserverController::IntersectionObserverController(Document* documen t) |
21 : ActiveDOMObject(document) | 23 : ActiveDOMObject(document) |
24 , m_lowLatencyNotificationTask(CancellableTaskFactory::create(this, &Interse ctionObserverController::deliverLowLatencyNotifications)) | |
22 , m_callbackID(0) | 25 , m_callbackID(0) |
23 , m_callbackFiredWhileSuspended(false) | 26 , m_callbackFiredWhileSuspended(false) |
27 , m_lowLatencyCallbackFiredWhileSuspended(false) | |
szager1
2016/08/30 23:57:11
I don't think you need m_lowLatencyCallbacksFiredW
Sami
2016/08/31 11:08:55
Done.
| |
24 { | 28 { |
25 } | 29 } |
26 | 30 |
27 IntersectionObserverController::~IntersectionObserverController() { } | 31 IntersectionObserverController::~IntersectionObserverController() { } |
28 | 32 |
29 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int ersectionObserver& observer) | 33 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int ersectionObserver& observer) |
30 { | 34 { |
35 if (observer.lowLatency()) { | |
szager1
2016/08/30 23:57:11
I think this would read better if you refactor it
Sami
2016/08/31 11:08:55
Good idea, done.
| |
36 m_pendingLowLatencyIntersectionObservers.add(&observer); | |
37 if (!m_lowLatencyNotificationTask->isPending()) | |
38 TaskRunnerHelper::get(TaskType::Unthrottled, getExecutionContext())- >postTask(BLINK_FROM_HERE, m_lowLatencyNotificationTask->cancelAndCreate()); | |
39 return; | |
40 } | |
31 m_pendingIntersectionObservers.add(&observer); | 41 m_pendingIntersectionObservers.add(&observer); |
32 if (m_callbackID) | 42 if (m_callbackID) |
33 return; | 43 return; |
34 Document* document = toDocument(getExecutionContext()); | 44 Document* document = toDocument(getExecutionContext()); |
35 if (!document) | 45 if (!document) |
36 return; | 46 return; |
37 IdleRequestOptions options; | 47 IdleRequestOptions options; |
38 // The IntersectionObserver spec mandates that notifications be sent within 100ms. | 48 // The IntersectionObserver spec mandates that notifications be sent within 100ms. |
39 options.setTimeout(100); | 49 options.setTimeout(100); |
40 m_callbackID = document->requestIdleCallback(this, options); | 50 m_callbackID = document->requestIdleCallback(this, options); |
41 } | 51 } |
42 | 52 |
43 void IntersectionObserverController::resume() | 53 void IntersectionObserverController::resume() |
44 { | 54 { |
45 // If the callback fired while DOM objects were suspended, notifications mig ht be late, so deliver | 55 // If the callback fired while DOM objects were suspended, notifications mig ht be late, so deliver |
46 // them right away (rather than waiting to fire again). | 56 // them right away (rather than waiting to fire again). |
57 if (m_lowLatencyCallbackFiredWhileSuspended) { | |
58 m_lowLatencyCallbackFiredWhileSuspended = false; | |
59 deliverLowLatencyNotifications(); | |
60 } | |
47 if (m_callbackFiredWhileSuspended) { | 61 if (m_callbackFiredWhileSuspended) { |
48 m_callbackFiredWhileSuspended = false; | 62 m_callbackFiredWhileSuspended = false; |
49 deliverIntersectionObservations(); | 63 deliverIntersectionObservations(); |
50 } | 64 } |
51 } | 65 } |
52 | 66 |
53 void IntersectionObserverController::handleEvent(IdleDeadline*) | 67 void IntersectionObserverController::handleEvent(IdleDeadline*) |
54 { | 68 { |
55 DCHECK(m_callbackID); | 69 DCHECK(m_callbackID); |
56 m_callbackID = 0; | 70 m_callbackID = 0; |
(...skipping 10 matching lines...) Expand all Loading... | |
67 if (context->activeDOMObjectsAreSuspended()) { | 81 if (context->activeDOMObjectsAreSuspended()) { |
68 m_callbackFiredWhileSuspended = true; | 82 m_callbackFiredWhileSuspended = true; |
69 return; | 83 return; |
70 } | 84 } |
71 HeapHashSet<Member<IntersectionObserver>> observers; | 85 HeapHashSet<Member<IntersectionObserver>> observers; |
72 m_pendingIntersectionObservers.swap(observers); | 86 m_pendingIntersectionObservers.swap(observers); |
73 for (auto& observer : observers) | 87 for (auto& observer : observers) |
74 observer->deliver(); | 88 observer->deliver(); |
75 } | 89 } |
76 | 90 |
91 void IntersectionObserverController::deliverLowLatencyNotifications() | |
92 { | |
93 ExecutionContext* context = getExecutionContext(); | |
94 if (!context) { | |
95 m_pendingLowLatencyIntersectionObservers.clear(); | |
96 return; | |
97 } | |
98 if (context->activeDOMObjectsAreSuspended()) { | |
99 m_lowLatencyCallbackFiredWhileSuspended = true; | |
100 return; | |
101 } | |
102 HeapHashSet<Member<IntersectionObserver>> observers; | |
103 m_pendingLowLatencyIntersectionObservers.swap(observers); | |
104 for (auto& observer : observers) | |
105 observer->deliver(); | |
106 } | |
107 | |
77 void IntersectionObserverController::computeTrackedIntersectionObservations() | 108 void IntersectionObserverController::computeTrackedIntersectionObservations() |
78 { | 109 { |
79 TRACE_EVENT0("blink", "IntersectionObserverController::computeTrackedInterse ctionObservations"); | 110 TRACE_EVENT0("blink", "IntersectionObserverController::computeTrackedInterse ctionObservations"); |
80 for (auto& observer : m_trackedIntersectionObservers) { | 111 for (auto& observer : m_trackedIntersectionObservers) { |
81 observer->computeIntersectionObservations(); | 112 observer->computeIntersectionObservations(); |
82 if (observer->hasEntries()) | 113 if (observer->hasEntries()) |
83 scheduleIntersectionObserverForDelivery(*observer); | 114 scheduleIntersectionObserverForDelivery(*observer); |
84 } | 115 } |
85 } | 116 } |
86 | 117 |
87 void IntersectionObserverController::addTrackedObserver(IntersectionObserver& ob server) | 118 void IntersectionObserverController::addTrackedObserver(IntersectionObserver& ob server) |
88 { | 119 { |
89 m_trackedIntersectionObservers.add(&observer); | 120 m_trackedIntersectionObservers.add(&observer); |
90 } | 121 } |
91 | 122 |
92 void IntersectionObserverController::removeTrackedObserversForRoot(const Node& r oot) | 123 void IntersectionObserverController::removeTrackedObserversForRoot(const Node& r oot) |
93 { | 124 { |
94 HeapVector<Member<IntersectionObserver>> toRemove; | 125 HeapVector<Member<IntersectionObserver>> toRemove; |
95 for (auto& observer : m_trackedIntersectionObservers) { | 126 for (auto& observer : m_trackedIntersectionObservers) { |
96 if (observer->rootNode() == &root) | 127 if (observer->rootNode() == &root) |
97 toRemove.append(observer); | 128 toRemove.append(observer); |
98 } | 129 } |
99 m_trackedIntersectionObservers.removeAll(toRemove); | 130 m_trackedIntersectionObservers.removeAll(toRemove); |
100 } | 131 } |
101 | 132 |
102 DEFINE_TRACE(IntersectionObserverController) | 133 DEFINE_TRACE(IntersectionObserverController) |
103 { | 134 { |
104 visitor->trace(m_trackedIntersectionObservers); | 135 visitor->trace(m_trackedIntersectionObservers); |
105 visitor->trace(m_pendingIntersectionObservers); | 136 visitor->trace(m_pendingIntersectionObservers); |
137 visitor->trace(m_pendingLowLatencyIntersectionObservers); | |
106 ActiveDOMObject::trace(visitor); | 138 ActiveDOMObject::trace(visitor); |
107 IdleRequestCallback::trace(visitor); | 139 IdleRequestCallback::trace(visitor); |
108 } | 140 } |
109 | 141 |
110 } // namespace blink | 142 } // namespace blink |
OLD | NEW |