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/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/Editor.h" | 10 #include "core/editing/Editor.h" |
11 #include "core/editing/FrameSelection.h" | 11 #include "core/editing/FrameSelection.h" |
12 #include "core/editing/VisibleSelection.h" | 12 #include "core/editing/VisibleSelection.h" |
13 #include "core/editing/VisibleUnits.h" | 13 #include "core/editing/VisibleUnits.h" |
14 #include "core/editing/commands/UndoStack.h" | 14 #include "core/editing/commands/UndoStack.h" |
15 #include "core/editing/commands/UndoStep.h" | 15 #include "core/editing/commands/UndoStep.h" |
16 #include "core/editing/iterators/CharacterIterator.h" | |
16 #include "core/editing/spellcheck/HotModeSpellCheckRequester.h" | 17 #include "core/editing/spellcheck/HotModeSpellCheckRequester.h" |
17 #include "core/editing/spellcheck/SpellCheckRequester.h" | 18 #include "core/editing/spellcheck/SpellCheckRequester.h" |
18 #include "core/editing/spellcheck/SpellChecker.h" | 19 #include "core/editing/spellcheck/SpellChecker.h" |
19 #include "core/frame/LocalFrame.h" | 20 #include "core/frame/LocalFrame.h" |
20 #include "platform/RuntimeEnabledFeatures.h" | 21 #include "platform/RuntimeEnabledFeatures.h" |
21 #include "platform/instrumentation/tracing/TraceEvent.h" | 22 #include "platform/instrumentation/tracing/TraceEvent.h" |
22 #include "wtf/CurrentTime.h" | 23 #include "wtf/CurrentTime.h" |
23 | 24 |
24 namespace blink { | 25 namespace blink { |
25 | 26 |
26 namespace { | 27 namespace { |
27 | 28 |
29 const int kColdModeChunkSize = 16384; | |
28 const int kColdModeTimerIntervalMS = 1000; | 30 const int kColdModeTimerIntervalMS = 1000; |
29 const int kConsecutiveColdModeTimerIntervalMS = 200; | 31 const int kConsecutiveColdModeTimerIntervalMS = 200; |
30 const int kRequestTimeoutMS = 200; | 32 const int kRequestTimeoutMS = 200; |
31 const int kInvalidHandle = -1; | 33 const int kInvalidHandle = -1; |
32 const int kDummyHandleForForcedInvocation = -2; | 34 const int kDummyHandleForForcedInvocation = -2; |
33 const double kForcedInvocationDeadlineSeconds = 10; | 35 const double kForcedInvocationDeadlineSeconds = 10; |
34 | 36 |
37 bool shouldCheckNodeInColdMode(Node& node) { | |
38 if (!node.isElementNode()) | |
39 return false; | |
40 const Position& position = Position::firstPositionInNode(&node); | |
41 if (!isEditablePosition(position)) | |
42 return false; | |
43 return SpellChecker::isSpellCheckingEnabledAt(position); | |
44 } | |
45 | |
35 } // namespace | 46 } // namespace |
36 | 47 |
37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} | 48 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} |
38 | 49 |
39 DEFINE_TRACE(IdleSpellCheckCallback) { | 50 DEFINE_TRACE(IdleSpellCheckCallback) { |
40 visitor->trace(m_frame); | 51 visitor->trace(m_frame); |
52 visitor->trace(m_nextNodeInColdMode); | |
41 IdleRequestCallback::trace(visitor); | 53 IdleRequestCallback::trace(visitor); |
42 SynchronousMutationObserver::trace(visitor); | 54 SynchronousMutationObserver::trace(visitor); |
43 } | 55 } |
44 | 56 |
45 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { | 57 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { |
46 return new IdleSpellCheckCallback(frame); | 58 return new IdleSpellCheckCallback(frame); |
47 } | 59 } |
48 | 60 |
49 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) | 61 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) |
50 : m_state(State::kInactive), | 62 : m_state(State::kInactive), |
51 m_idleCallbackHandle(kInvalidHandle), | 63 m_idleCallbackHandle(kInvalidHandle), |
52 m_needsMoreColdModeInvocationForTesting(false), | 64 m_needsMoreColdModeInvocationForTesting(false), |
53 m_frame(frame), | 65 m_frame(frame), |
54 m_lastProcessedUndoStepSequence(0), | 66 m_lastProcessedUndoStepSequence(0), |
67 m_lastCheckedDOMTreeVersionInColdMode(0), | |
55 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), | 68 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), |
56 this, | 69 this, |
57 &IdleSpellCheckCallback::coldModeTimerFired) {} | 70 &IdleSpellCheckCallback::coldModeTimerFired) {} |
58 | 71 |
59 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { | 72 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { |
60 return frame().spellChecker().spellCheckRequester(); | 73 return frame().spellChecker().spellCheckRequester(); |
61 } | 74 } |
62 | 75 |
63 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { | 76 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { |
64 return frame().spellChecker().isSpellCheckingEnabled(); | 77 return frame().spellChecker().isSpellCheckingEnabled(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 if (step->sequenceNumber() <= watermark) | 158 if (step->sequenceNumber() <= watermark) |
146 break; | 159 break; |
147 m_lastProcessedUndoStepSequence = | 160 m_lastProcessedUndoStepSequence = |
148 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); | 161 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); |
149 if (deadline->timeRemaining() == 0) | 162 if (deadline->timeRemaining() == 0) |
150 break; | 163 break; |
151 requester.checkSpellingAt(step->endingSelection().extent()); | 164 requester.checkSpellingAt(step->endingSelection().extent()); |
152 } | 165 } |
153 } | 166 } |
154 | 167 |
168 // TODO(xiaochengh): Deduplicate with SpellChecker::chunkAndMarkAllMisspellings. | |
169 void IdleSpellCheckCallback::chunkAndRequestFullCheckingFor( | |
170 const Element& editable) { | |
171 const EphemeralRange& fullRange = EphemeralRange::rangeOfContents(editable); | |
172 const int fullLength = TextIterator::rangeLength(fullRange.startPosition(), | |
173 fullRange.endPosition()); | |
174 | |
175 // Check the full content if it is short. | |
176 if (fullLength <= kColdModeChunkSize) { | |
177 SpellCheckRequest* fullRequest = SpellCheckRequest::create(fullRange); | |
178 spellCheckRequester().requestCheckingFor(fullRequest); | |
179 return; | |
180 } | |
181 | |
182 // TODO(xiaochengh): Figure out if this is going to cause performance issues. | |
183 // In that case, we need finer-grained control over request generation. | |
184 Position chunkStart = fullRange.startPosition(); | |
185 const int chunkLimit = fullLength / kColdModeChunkSize + 1; | |
186 for (int chunkIndex = 0; chunkIndex <= chunkLimit; ++chunkIndex) { | |
187 const Position& chunkEnd = | |
188 calculateCharacterSubrange( | |
189 EphemeralRange(chunkStart, fullRange.endPosition()), 0, | |
190 kColdModeChunkSize) | |
191 .endPosition(); | |
192 if (chunkEnd <= chunkStart) | |
193 break; | |
194 const EphemeralRange chunkRange(chunkStart, chunkEnd); | |
195 const EphemeralRange& checkRange = | |
196 chunkIndex >= 1 ? expandEndToSentenceBoundary(chunkRange) | |
197 : expandRangeToSentenceBoundary(chunkRange); | |
198 | |
199 SpellCheckRequest* chunkRequest = | |
200 SpellCheckRequest::create(checkRange, chunkIndex); | |
201 spellCheckRequester().requestCheckingFor(chunkRequest); | |
yosin_UTC9
2017/03/07 04:28:50
In following patch, we may want to make
SpellCheck
Xiaocheng
2017/03/07 06:46:54
Good catch. Will do.
| |
202 | |
203 chunkStart = checkRange.endPosition(); | |
204 } | |
205 } | |
206 | |
155 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { | 207 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { |
156 // TODO(xiaochengh): Implementation. | 208 TRACE_EVENT0("blink", "IdleSpellCheckCallback::coldModeInvocation"); |
209 | |
210 Node* body = frame().document()->body(); | |
211 if (!body) { | |
212 m_nextNodeInColdMode = nullptr; | |
213 m_lastCheckedDOMTreeVersionInColdMode = | |
214 frame().document()->domTreeVersion(); | |
215 return; | |
216 } | |
217 | |
218 // TODO(xiaochengh): Figure out if this has any performance impact. | |
219 frame().document()->updateStyleAndLayout(); | |
220 | |
221 if (m_lastCheckedDOMTreeVersionInColdMode != | |
222 frame().document()->domTreeVersion()) | |
223 m_nextNodeInColdMode = body; | |
224 | |
225 while (m_nextNodeInColdMode && deadline->timeRemaining() > 0) { | |
226 if (!shouldCheckNodeInColdMode(*m_nextNodeInColdMode)) { | |
227 m_nextNodeInColdMode = | |
228 FlatTreeTraversal::next(*m_nextNodeInColdMode, body); | |
229 continue; | |
230 } | |
231 | |
232 chunkAndRequestFullCheckingFor(toElement(*m_nextNodeInColdMode)); | |
233 m_nextNodeInColdMode = | |
234 FlatTreeTraversal::nextSkippingChildren(*m_nextNodeInColdMode, body); | |
235 } | |
236 | |
237 m_lastCheckedDOMTreeVersionInColdMode = frame().document()->domTreeVersion(); | |
157 } | 238 } |
158 | 239 |
159 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { | 240 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { |
160 if (m_needsMoreColdModeInvocationForTesting) { | 241 if (m_needsMoreColdModeInvocationForTesting) { |
161 m_needsMoreColdModeInvocationForTesting = false; | 242 m_needsMoreColdModeInvocationForTesting = false; |
162 return false; | 243 return false; |
163 } | 244 } |
164 | 245 |
165 // TODO(xiaochengh): Implementation. | 246 return !m_nextNodeInColdMode; |
166 return true; | |
167 } | 247 } |
168 | 248 |
169 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { | 249 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { |
170 DCHECK(frame().document()); | 250 DCHECK(frame().document()); |
171 DCHECK(frame().document()->isActive()); | 251 DCHECK(frame().document()->isActive()); |
172 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); | 252 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); |
173 m_idleCallbackHandle = kInvalidHandle; | 253 m_idleCallbackHandle = kInvalidHandle; |
174 | 254 |
175 if (!isSpellCheckingEnabled()) { | 255 if (!isSpellCheckingEnabled()) { |
176 deactivate(); | 256 deactivate(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 } | 309 } |
230 } | 310 } |
231 | 311 |
232 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { | 312 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { |
233 DCHECK(m_coldModeTimer.isActive()); | 313 DCHECK(m_coldModeTimer.isActive()); |
234 m_coldModeTimer.stop(); | 314 m_coldModeTimer.stop(); |
235 coldModeTimerFired(&m_coldModeTimer); | 315 coldModeTimerFired(&m_coldModeTimer); |
236 } | 316 } |
237 | 317 |
238 } // namespace blink | 318 } // namespace blink |
OLD | NEW |