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

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

Issue 2701983002: Implement complete lifecycle transition for IdleSpellCheckCallback (Closed)
Patch Set: Fix compiler error Created 3 years, 9 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 return frame().spellChecker().spellCheckRequester(); 62 return frame().spellChecker().spellCheckRequester();
57 } 63 }
58 64
59 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { 65 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const {
60 return frame().spellChecker().isSpellCheckingEnabled(); 66 return frame().spellChecker().isSpellCheckingEnabled();
61 } 67 }
62 68
63 void IdleSpellCheckCallback::requestInvocation() { 69 void IdleSpellCheckCallback::requestInvocation() {
70 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle);
71
64 IdleRequestOptions options; 72 IdleRequestOptions options;
65 options.setTimeout(kRequestTimeoutMS); 73 options.setTimeout(kRequestTimeoutMS);
66 frame().document()->requestIdleCallback(this, options); 74 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options);
67 } 75 }
68 76
69 void IdleSpellCheckCallback::deactivate() { 77 void IdleSpellCheckCallback::deactivate() {
70 m_state = State::kInactive; 78 m_state = State::kInactive;
71 if (m_coldModeTimer.isActive()) 79 if (m_coldModeTimer.isActive())
72 m_coldModeTimer.stop(); 80 m_coldModeTimer.stop();
81 if (m_idleCallbackHandle != kInvalidHandle)
82 frame().document()->cancelIdleCallback(m_idleCallbackHandle);
83 m_idleCallbackHandle = kInvalidHandle;
73 } 84 }
74 85
75 void IdleSpellCheckCallback::setNeedsHotModeInvocation() { 86 void IdleSpellCheckCallback::setNeedsInvocation() {
76 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
77 return;
78
79 if (!isSpellCheckingEnabled()) { 87 if (!isSpellCheckingEnabled()) {
80 deactivate(); 88 deactivate();
81 return; 89 return;
82 } 90 }
83 91
92 if (m_state == State::kHotModeRequested)
93 return;
94
84 if (m_state == State::kColdModeTimerStarted) { 95 if (m_state == State::kColdModeTimerStarted) {
85 DCHECK(m_coldModeTimer.isActive()); 96 DCHECK(m_coldModeTimer.isActive());
86 m_coldModeTimer.stop(); 97 m_coldModeTimer.stop();
87 } 98 }
88 99
89 if (m_state != State::kColdModeRequested) 100 if (m_state != State::kColdModeRequested)
90 requestInvocation(); 101 requestInvocation();
91 m_state = State::kHotModeRequested; 102 m_state = State::kHotModeRequested;
92 } 103 }
93 104
94 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { 105 void IdleSpellCheckCallback::setNeedsColdModeInvocation() {
95 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
96 return;
97
98 if (!isSpellCheckingEnabled()) { 106 if (!isSpellCheckingEnabled()) {
99 deactivate(); 107 deactivate();
100 return; 108 return;
101 } 109 }
102 110
103 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && 111 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation &&
104 m_state != State::kInColdModeInvocation) 112 m_state != State::kInColdModeInvocation)
105 return; 113 return;
106 114
107 DCHECK(!m_coldModeTimer.isActive()); 115 DCHECK(!m_coldModeTimer.isActive());
(...skipping 18 matching lines...) Expand all
126 134
127 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { 135 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) {
128 // TODO(xiaochengh): Implementation. 136 // TODO(xiaochengh): Implementation.
129 } 137 }
130 138
131 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { 139 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) {
132 // TODO(xiaochengh): Implementation. 140 // TODO(xiaochengh): Implementation.
133 } 141 }
134 142
135 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { 143 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const {
144 if (m_needsMoreColdModeInvocationForTesting) {
145 m_needsMoreColdModeInvocationForTesting = false;
146 return false;
147 }
148
136 // TODO(xiaochengh): Implementation. 149 // TODO(xiaochengh): Implementation.
137 return true; 150 return true;
138 } 151 }
139 152
140 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { 153 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) {
141 DCHECK(frame().document()); 154 DCHECK(frame().document());
142 DCHECK(frame().document()->isActive()); 155 DCHECK(frame().document()->isActive());
156 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle);
157 m_idleCallbackHandle = kInvalidHandle;
143 158
144 if (!isSpellCheckingEnabled()) { 159 if (!isSpellCheckingEnabled()) {
145 deactivate(); 160 deactivate();
146 return; 161 return;
147 } 162 }
148 163
149 if (m_state == State::kHotModeRequested) { 164 if (m_state == State::kHotModeRequested) {
150 m_state = State::kInHotModeInvocation; 165 m_state = State::kInHotModeInvocation;
151 hotModeInvocation(deadline); 166 hotModeInvocation(deadline);
152 setNeedsColdModeInvocation(); 167 setNeedsColdModeInvocation();
153 } else if (m_state == State::kColdModeRequested) { 168 } else if (m_state == State::kColdModeRequested) {
154 m_state = State::kInColdModeInvocation; 169 m_state = State::kInColdModeInvocation;
155 coldModeInvocation(deadline); 170 coldModeInvocation(deadline);
156 if (coldModeFinishesFullDocument()) 171 if (coldModeFinishesFullDocument())
157 m_state = State::kInactive; 172 m_state = State::kInactive;
158 else 173 else
159 setNeedsColdModeInvocation(); 174 setNeedsColdModeInvocation();
160 } else { 175 } else {
161 NOTREACHED(); 176 NOTREACHED();
162 } 177 }
163 } 178 }
164 179
180 void IdleSpellCheckCallback::documentAttached(Document* document) {
181 setNeedsColdModeInvocation();
182 setContext(document);
183 }
184
185 void IdleSpellCheckCallback::contextDestroyed(Document*) {
186 deactivate();
187 }
188
189 void IdleSpellCheckCallback::forceInvocationForTesting() {
190 if (!isSpellCheckingEnabled())
191 return;
192
193 IdleDeadline* deadline =
194 IdleDeadline::create(kForcedInvocationDeadlineSeconds,
195 IdleDeadline::CallbackType::CalledWhenIdle);
196
197 switch (m_state) {
198 case State::kColdModeTimerStarted:
199 m_coldModeTimer.stop();
200 m_state = State::kColdModeRequested;
201 m_idleCallbackHandle = kDummyHandleForForcedInvocation;
202 handleEvent(deadline);
203 break;
204 case State::kHotModeRequested:
205 case State::kColdModeRequested:
206 frame().document()->cancelIdleCallback(m_idleCallbackHandle);
207 handleEvent(deadline);
208 break;
209 case State::kInactive:
210 case State::kInHotModeInvocation:
211 case State::kInColdModeInvocation:
212 NOTREACHED();
213 }
214 }
215
216 void IdleSpellCheckCallback::skipColdModeTimerForTesting() {
217 DCHECK(m_coldModeTimer.isActive());
218 m_coldModeTimer.stop();
219 coldModeTimerFired(&m_coldModeTimer);
220 }
221
165 } // namespace blink 222 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698