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

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

Issue 2720663002: Revert of Implement complete lifecycle transition for IdleSpellCheckCallback (Closed)
Patch Set: 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;
37 34
38 } // namespace 35 } // namespace
39 36
40 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} 37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {}
41 38
42 DEFINE_TRACE(IdleSpellCheckCallback) { 39 DEFINE_TRACE(IdleSpellCheckCallback) {
43 visitor->trace(m_frame); 40 visitor->trace(m_frame);
44 IdleRequestCallback::trace(visitor); 41 IdleRequestCallback::trace(visitor);
45 SynchronousMutationObserver::trace(visitor);
46 } 42 }
47 43
48 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { 44 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) {
49 return new IdleSpellCheckCallback(frame); 45 return new IdleSpellCheckCallback(frame);
50 } 46 }
51 47
52 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) 48 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame)
53 : m_state(State::kInactive), 49 : m_state(State::kInactive),
54 m_idleCallbackHandle(kInvalidHandle),
55 m_needsMoreColdModeInvocationForTesting(false),
56 m_frame(frame), 50 m_frame(frame),
57 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), 51 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame),
58 this, 52 this,
59 &IdleSpellCheckCallback::coldModeTimerFired) {} 53 &IdleSpellCheckCallback::coldModeTimerFired) {}
60 54
61 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { 55 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const {
62 return frame().spellChecker().spellCheckRequester(); 56 return frame().spellChecker().spellCheckRequester();
63 } 57 }
64 58
65 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { 59 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const {
66 return frame().spellChecker().isSpellCheckingEnabled(); 60 return frame().spellChecker().isSpellCheckingEnabled();
67 } 61 }
68 62
69 void IdleSpellCheckCallback::requestInvocation() { 63 void IdleSpellCheckCallback::requestInvocation() {
70 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle);
71
72 IdleRequestOptions options; 64 IdleRequestOptions options;
73 options.setTimeout(kRequestTimeoutMS); 65 options.setTimeout(kRequestTimeoutMS);
74 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options); 66 frame().document()->requestIdleCallback(this, options);
75 } 67 }
76 68
77 void IdleSpellCheckCallback::deactivate() { 69 void IdleSpellCheckCallback::deactivate() {
78 m_state = State::kInactive; 70 m_state = State::kInactive;
79 if (m_coldModeTimer.isActive()) 71 if (m_coldModeTimer.isActive())
80 m_coldModeTimer.stop(); 72 m_coldModeTimer.stop();
81 if (m_idleCallbackHandle != kInvalidHandle)
82 frame().document()->cancelIdleCallback(m_idleCallbackHandle);
83 m_idleCallbackHandle = kInvalidHandle;
84 } 73 }
85 74
86 void IdleSpellCheckCallback::setNeedsInvocation() { 75 void IdleSpellCheckCallback::setNeedsHotModeInvocation() {
76 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
77 return;
78
87 if (!isSpellCheckingEnabled()) { 79 if (!isSpellCheckingEnabled()) {
88 deactivate(); 80 deactivate();
89 return; 81 return;
90 } 82 }
91 83
92 if (m_state == State::kHotModeRequested)
93 return;
94
95 if (m_state == State::kColdModeTimerStarted) { 84 if (m_state == State::kColdModeTimerStarted) {
96 DCHECK(m_coldModeTimer.isActive()); 85 DCHECK(m_coldModeTimer.isActive());
97 m_coldModeTimer.stop(); 86 m_coldModeTimer.stop();
98 } 87 }
99 88
100 if (m_state != State::kColdModeRequested) 89 if (m_state != State::kColdModeRequested)
101 requestInvocation(); 90 requestInvocation();
102 m_state = State::kHotModeRequested; 91 m_state = State::kHotModeRequested;
103 } 92 }
104 93
105 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { 94 void IdleSpellCheckCallback::setNeedsColdModeInvocation() {
95 if (!RuntimeEnabledFeatures::idleTimeSpellCheckingEnabled())
96 return;
97
106 if (!isSpellCheckingEnabled()) { 98 if (!isSpellCheckingEnabled()) {
107 deactivate(); 99 deactivate();
108 return; 100 return;
109 } 101 }
110 102
111 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && 103 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation &&
112 m_state != State::kInColdModeInvocation) 104 m_state != State::kInColdModeInvocation)
113 return; 105 return;
114 106
115 DCHECK(!m_coldModeTimer.isActive()); 107 DCHECK(!m_coldModeTimer.isActive());
(...skipping 18 matching lines...) Expand all
134 126
135 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { 127 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) {
136 // TODO(xiaochengh): Implementation. 128 // TODO(xiaochengh): Implementation.
137 } 129 }
138 130
139 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { 131 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) {
140 // TODO(xiaochengh): Implementation. 132 // TODO(xiaochengh): Implementation.
141 } 133 }
142 134
143 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { 135 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const {
144 if (m_needsMoreColdModeInvocationForTesting) {
145 m_needsMoreColdModeInvocationForTesting = false;
146 return false;
147 }
148
149 // TODO(xiaochengh): Implementation. 136 // TODO(xiaochengh): Implementation.
150 return true; 137 return true;
151 } 138 }
152 139
153 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { 140 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) {
154 DCHECK(frame().document()); 141 DCHECK(frame().document());
155 DCHECK(frame().document()->isActive()); 142 DCHECK(frame().document()->isActive());
156 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle);
157 m_idleCallbackHandle = kInvalidHandle;
158 143
159 if (!isSpellCheckingEnabled()) { 144 if (!isSpellCheckingEnabled()) {
160 deactivate(); 145 deactivate();
161 return; 146 return;
162 } 147 }
163 148
164 if (m_state == State::kHotModeRequested) { 149 if (m_state == State::kHotModeRequested) {
165 m_state = State::kInHotModeInvocation; 150 m_state = State::kInHotModeInvocation;
166 hotModeInvocation(deadline); 151 hotModeInvocation(deadline);
167 setNeedsColdModeInvocation(); 152 setNeedsColdModeInvocation();
168 } else if (m_state == State::kColdModeRequested) { 153 } else if (m_state == State::kColdModeRequested) {
169 m_state = State::kInColdModeInvocation; 154 m_state = State::kInColdModeInvocation;
170 coldModeInvocation(deadline); 155 coldModeInvocation(deadline);
171 if (coldModeFinishesFullDocument()) 156 if (coldModeFinishesFullDocument())
172 m_state = State::kInactive; 157 m_state = State::kInactive;
173 else 158 else
174 setNeedsColdModeInvocation(); 159 setNeedsColdModeInvocation();
175 } else { 160 } else {
176 NOTREACHED(); 161 NOTREACHED();
177 } 162 }
178 } 163 }
179 164
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
222 } // namespace blink 165 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698