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/TaskRunnerHelper.h" |
9 #include "platform/TraceEvent.h" | 9 #include "platform/TraceEvent.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 IntersectionObserverController* IntersectionObserverController::create(Document*
document) | 13 IntersectionObserverController* IntersectionObserverController::create(Document*
document) |
14 { | 14 { |
15 IntersectionObserverController* result = new IntersectionObserverController(
document); | 15 IntersectionObserverController* result = new IntersectionObserverController(
document); |
16 result->suspendIfNeeded(); | 16 result->suspendIfNeeded(); |
17 return result; | 17 return result; |
18 } | 18 } |
19 | 19 |
20 IntersectionObserverController::IntersectionObserverController(Document* documen
t) | 20 IntersectionObserverController::IntersectionObserverController(Document* documen
t) |
21 : ActiveDOMObject(document) | 21 : ActiveDOMObject(document) |
22 , m_callbackID(0) | 22 , m_weakPtrFactory(this) |
23 , m_callbackFiredWhileSuspended(false) | 23 , m_callbackFiredWhileSuspended(false) |
24 { | 24 { |
25 } | 25 } |
26 | 26 |
27 IntersectionObserverController::~IntersectionObserverController() { } | 27 IntersectionObserverController::~IntersectionObserverController() { } |
28 | 28 |
29 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int
ersectionObserver& observer) | 29 void IntersectionObserverController::scheduleIntersectionObserverForDelivery(Int
ersectionObserver& observer) |
30 { | 30 { |
31 m_pendingIntersectionObservers.add(&observer); | 31 m_pendingIntersectionObservers.add(&observer); |
32 if (m_callbackID) | 32 if (!m_weakPtrFactory.hasWeakPtrs()) { |
33 return; | 33 // TODO(ojan): These tasks decide whether to throttle a subframe, so the
y need to |
34 Document* document = toDocument(getExecutionContext()); | 34 // be unthrottled, but we should throttle all the other tasks (e.g. ones
coming from |
35 if (!document) | 35 // the web page). |
36 return; | 36 TaskRunnerHelper::get(TaskType::Unthrottled, |
37 IdleRequestOptions options; | 37 getExecutionContext())->postTask(BLINK_FROM_HERE, |
38 // The IntersectionObserver spec mandates that notifications be sent within
100ms. | 38 WTF::bind(&IntersectionObserverController::deliverIntersectionOb
servations, m_weakPtrFactory.createWeakPtr())); |
39 options.setTimeout(100); | 39 } |
40 m_callbackID = document->requestIdleCallback(this, options); | |
41 } | 40 } |
42 | 41 |
43 void IntersectionObserverController::resume() | 42 void IntersectionObserverController::resume() |
44 { | 43 { |
45 // If the callback fired while DOM objects were suspended, notifications mig
ht be late, so deliver | 44 // 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). | 45 // them right away (rather than waiting to fire again). |
47 if (m_callbackFiredWhileSuspended) { | 46 if (m_callbackFiredWhileSuspended) { |
48 m_callbackFiredWhileSuspended = false; | 47 m_callbackFiredWhileSuspended = false; |
49 deliverIntersectionObservations(); | 48 deliverIntersectionObservations(); |
50 } | 49 } |
51 } | 50 } |
52 | 51 |
53 void IntersectionObserverController::handleEvent(IdleDeadline*) | |
54 { | |
55 DCHECK(m_callbackID); | |
56 m_callbackID = 0; | |
57 deliverIntersectionObservations(); | |
58 } | |
59 | |
60 void IntersectionObserverController::deliverIntersectionObservations() | 52 void IntersectionObserverController::deliverIntersectionObservations() |
61 { | 53 { |
62 ExecutionContext* context = getExecutionContext(); | 54 ExecutionContext* context = getExecutionContext(); |
63 if (!context) { | 55 if (!context) { |
64 m_pendingIntersectionObservers.clear(); | 56 m_pendingIntersectionObservers.clear(); |
65 return; | 57 return; |
66 } | 58 } |
67 if (context->activeDOMObjectsAreSuspended()) { | 59 if (context->activeDOMObjectsAreSuspended()) { |
68 m_callbackFiredWhileSuspended = true; | 60 m_callbackFiredWhileSuspended = true; |
69 return; | 61 return; |
(...skipping 27 matching lines...) Expand all Loading... |
97 toRemove.append(observer); | 89 toRemove.append(observer); |
98 } | 90 } |
99 m_trackedIntersectionObservers.removeAll(toRemove); | 91 m_trackedIntersectionObservers.removeAll(toRemove); |
100 } | 92 } |
101 | 93 |
102 DEFINE_TRACE(IntersectionObserverController) | 94 DEFINE_TRACE(IntersectionObserverController) |
103 { | 95 { |
104 visitor->trace(m_trackedIntersectionObservers); | 96 visitor->trace(m_trackedIntersectionObservers); |
105 visitor->trace(m_pendingIntersectionObservers); | 97 visitor->trace(m_pendingIntersectionObservers); |
106 ActiveDOMObject::trace(visitor); | 98 ActiveDOMObject::trace(visitor); |
107 IdleRequestCallback::trace(visitor); | |
108 } | 99 } |
109 | 100 |
110 } // namespace blink | 101 } // namespace blink |
OLD | NEW |