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

Side by Side Diff: third_party/WebKit/Source/core/editing/spellcheck/IdleSpellCheckCallback.cpp

Issue 2701983002: Implement complete lifecycle transition for IdleSpellCheckCallback (Closed)
Patch Set: Add lifecycle transition unit tests Created 3 years, 10 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
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/editing/spellcheck/IdleSpellCheckCallback.h" 5 #include "core/editing/spellcheck/IdleSpellCheckCallback.h"
6 6
7 #include "core/dom/IdleRequestOptions.h" 7 #include "core/dom/IdleRequestOptions.h"
8 #include "core/dom/TaskRunnerHelper.h" 8 #include "core/dom/TaskRunnerHelper.h"
9 #include "core/editing/EditingUtilities.h" 9 #include "core/editing/EditingUtilities.h"
10 #include "core/editing/FrameSelection.h" 10 #include "core/editing/FrameSelection.h"
(...skipping 13 matching lines...) Expand all
24 #include "platform/instrumentation/tracing/TraceEvent.h" 24 #include "platform/instrumentation/tracing/TraceEvent.h"
25 #include "wtf/CurrentTime.h" 25 #include "wtf/CurrentTime.h"
26 26
27 namespace blink { 27 namespace blink {
28 28
29 namespace { 29 namespace {
30 30
31 const int kColdModeTimerIntervalMS = 1000; 31 const int kColdModeTimerIntervalMS = 1000;
32 const int kConsecutiveColdModeTimerIntervalMS = 200; 32 const int kConsecutiveColdModeTimerIntervalMS = 200;
33 const int kRequestTimeoutMS = 200; 33 const int kRequestTimeoutMS = 200;
34 const int kInvalidHandle = -1;
35 const int kDummyHandleForForcedInvocation = -2;
36 const double kForcedInvocationDeadlineSeconds = 10;
34 37
35 } // namespace 38 } // namespace
36 39
37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} 40 IdleSpellCheckCallback::~IdleSpellCheckCallback() {}
38 41
39 DEFINE_TRACE(IdleSpellCheckCallback) { 42 DEFINE_TRACE(IdleSpellCheckCallback) {
40 visitor->trace(m_frame); 43 visitor->trace(m_frame);
41 IdleRequestCallback::trace(visitor); 44 IdleRequestCallback::trace(visitor);
45 SynchronousMutationObserver::trace(visitor);
42 } 46 }
43 47
44 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { 48 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) {
45 return new IdleSpellCheckCallback(frame); 49 return new IdleSpellCheckCallback(frame);
46 } 50 }
47 51
48 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) 52 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame)
49 : m_state(State::kInactive), 53 : m_state(State::kInactive),
54 m_idleCallbackHandle(kInvalidHandle),
55 m_needsMoreColdModeInvocationForTesting(false),
50 m_frame(frame), 56 m_frame(frame),
51 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), 57 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame),
52 this, 58 this,
53 &IdleSpellCheckCallback::coldModeTimerFired) {} 59 &IdleSpellCheckCallback::coldModeTimerFired) {}
54 60
55 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { 61 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const {
56 // TODO(xiaochengh): decouple with SpellChecker after SpellCheckRequester is 62 // TODO(xiaochengh): decouple with SpellChecker after SpellCheckRequester is
57 // moved to IdleSpellCheckCallback. 63 // moved to IdleSpellCheckCallback.
58 return frame().spellChecker().spellCheckRequester(); 64 return frame().spellChecker().spellCheckRequester();
59 } 65 }
60 66
61 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { 67 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const {
62 // TODO(xiaochengh): decouple with SpellChecker. 68 // TODO(xiaochengh): decouple with SpellChecker.
63 return frame().spellChecker().isSpellCheckingEnabled(); 69 return frame().spellChecker().isSpellCheckingEnabled();
64 } 70 }
65 71
66 void IdleSpellCheckCallback::prepareForLeakDetection() { 72 void IdleSpellCheckCallback::prepareForLeakDetection() {
67 if (RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled()) 73 spellCheckRequester().prepareForLeakDetection();
68 spellCheckRequester().prepareForLeakDetection();
69 } 74 }
70 75
71 void IdleSpellCheckCallback::requestInvocation() { 76 void IdleSpellCheckCallback::requestInvocation() {
77 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle);
78
72 IdleRequestOptions options; 79 IdleRequestOptions options;
73 options.setTimeout(kRequestTimeoutMS); 80 options.setTimeout(kRequestTimeoutMS);
74 frame().document()->requestIdleCallback(this, options); 81 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options);
75 } 82 }
76 83
77 void IdleSpellCheckCallback::deactivate() { 84 void IdleSpellCheckCallback::deactivate() {
78 m_state = State::kInactive; 85 m_state = State::kInactive;
79 if (m_coldModeTimer.isActive()) 86 if (m_coldModeTimer.isActive())
80 m_coldModeTimer.stop(); 87 m_coldModeTimer.stop();
88 if (m_idleCallbackHandle != kInvalidHandle)
89 frame().document()->cancelIdleCallback(m_idleCallbackHandle);
90 m_idleCallbackHandle = kInvalidHandle;
81 } 91 }
82 92
83 void IdleSpellCheckCallback::setNeedsHotModeInvocation() { 93 void IdleSpellCheckCallback::setNeedsInvocation() {
84 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
85 return;
86
87 if (!isSpellCheckingEnabled()) { 94 if (!isSpellCheckingEnabled()) {
88 deactivate(); 95 deactivate();
89 return; 96 return;
90 } 97 }
91 98
99 if (m_state == State::kHotModeRequested)
100 return;
101
92 if (m_state == State::kColdModeTimerStarted) { 102 if (m_state == State::kColdModeTimerStarted) {
93 DCHECK(m_coldModeTimer.isActive()); 103 DCHECK(m_coldModeTimer.isActive());
94 m_coldModeTimer.stop(); 104 m_coldModeTimer.stop();
95 } 105 }
96 106
97 if (m_state != State::kColdModeRequested) 107 if (m_state != State::kColdModeRequested)
98 requestInvocation(); 108 requestInvocation();
99 m_state = State::kHotModeRequested; 109 m_state = State::kHotModeRequested;
100 } 110 }
101 111
102 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { 112 void IdleSpellCheckCallback::setNeedsColdModeInvocation() {
103 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
104 return;
105
106 if (!isSpellCheckingEnabled()) { 113 if (!isSpellCheckingEnabled()) {
107 deactivate(); 114 deactivate();
108 return; 115 return;
109 } 116 }
110 117
111 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && 118 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation &&
112 m_state != State::kInColdModeInvocation) 119 m_state != State::kInColdModeInvocation)
113 return; 120 return;
114 121
115 DCHECK(!m_coldModeTimer.isActive()); 122 DCHECK(!m_coldModeTimer.isActive());
(...skipping 18 matching lines...) Expand all
134 141
135 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { 142 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) {
136 // TODO(xiaochengh): Implementation. 143 // TODO(xiaochengh): Implementation.
137 } 144 }
138 145
139 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { 146 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) {
140 // TODO(xiaochengh): Implementation. 147 // TODO(xiaochengh): Implementation.
141 } 148 }
142 149
143 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { 150 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const {
151 if (m_needsMoreColdModeInvocationForTesting) {
152 m_needsMoreColdModeInvocationForTesting = false;
153 return false;
154 }
155
144 // TODO(xiaochengh): Implementation. 156 // TODO(xiaochengh): Implementation.
145 return true; 157 return true;
146 } 158 }
147 159
148 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { 160 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) {
149 DCHECK(frame().document()); 161 DCHECK(frame().document());
150 DCHECK(frame().document()->isActive()); 162 DCHECK(frame().document()->isActive());
163 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle);
164 m_idleCallbackHandle = kInvalidHandle;
151 165
152 if (!isSpellCheckingEnabled()) { 166 if (!isSpellCheckingEnabled()) {
153 deactivate(); 167 deactivate();
154 return; 168 return;
155 } 169 }
156 170
157 if (m_state == State::kHotModeRequested) { 171 if (m_state == State::kHotModeRequested) {
158 m_state = State::kInHotModeInvocation; 172 m_state = State::kInHotModeInvocation;
159 hotModeInvocation(deadline); 173 hotModeInvocation(deadline);
160 setNeedsColdModeInvocation(); 174 setNeedsColdModeInvocation();
161 } else if (m_state == State::kColdModeRequested) { 175 } else if (m_state == State::kColdModeRequested) {
162 m_state = State::kInColdModeInvocation; 176 m_state = State::kInColdModeInvocation;
163 coldModeInvocation(deadline); 177 coldModeInvocation(deadline);
164 if (coldModeFinishesFullDocument()) 178 if (coldModeFinishesFullDocument())
165 m_state = State::kInactive; 179 m_state = State::kInactive;
166 else 180 else
167 setNeedsColdModeInvocation(); 181 setNeedsColdModeInvocation();
168 } else { 182 } else {
169 NOTREACHED(); 183 NOTREACHED();
170 } 184 }
171 } 185 }
172 186
187 void IdleSpellCheckCallback::documentAttached(Document* document) {
188 setNeedsColdModeInvocation();
189 setContext(document);
190 }
191
192 void IdleSpellCheckCallback::contextDestroyed(Document*) {
193 deactivate();
194 }
195
196 void IdleSpellCheckCallback::forceInvocationForTesting() {
197 if (!isSpellCheckingEnabled())
198 return;
199
200 IdleDeadline* deadline =
201 IdleDeadline::create(kForcedInvocationDeadlineSeconds,
202 IdleDeadline::CallbackType::CalledWhenIdle);
203
204 switch (m_state) {
205 case State::kColdModeTimerStarted:
206 m_coldModeTimer.stop();
207 m_state = State::kColdModeRequested;
208 m_idleCallbackHandle = kDummyHandleForForcedInvocation;
209 handleEvent(deadline);
210 break;
211 case State::kHotModeRequested:
212 case State::kColdModeRequested:
213 frame().document()->cancelIdleCallback(m_idleCallbackHandle);
214 handleEvent(deadline);
215 break;
216 default:
217 NOTREACHED();
218 }
219 }
220
221 void IdleSpellCheckCallback::skipColdModeTimerForTesting() {
222 DCHECK(m_coldModeTimer.isActive());
223 m_coldModeTimer.stop();
224 coldModeTimerFired(&m_coldModeTimer);
225 }
226
173 } // namespace blink 227 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698