Chromium Code Reviews| 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 |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} | 39 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} |
| 38 | 40 |
| 39 DEFINE_TRACE(IdleSpellCheckCallback) { | 41 DEFINE_TRACE(IdleSpellCheckCallback) { |
| 40 visitor->trace(m_frame); | 42 visitor->trace(m_frame); |
| 43 visitor->trace(m_nextNodeInColdMode); | |
| 41 IdleRequestCallback::trace(visitor); | 44 IdleRequestCallback::trace(visitor); |
| 42 SynchronousMutationObserver::trace(visitor); | 45 SynchronousMutationObserver::trace(visitor); |
| 43 } | 46 } |
| 44 | 47 |
| 45 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { | 48 IdleSpellCheckCallback* IdleSpellCheckCallback::create(LocalFrame& frame) { |
| 46 return new IdleSpellCheckCallback(frame); | 49 return new IdleSpellCheckCallback(frame); |
| 47 } | 50 } |
| 48 | 51 |
| 49 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) | 52 IdleSpellCheckCallback::IdleSpellCheckCallback(LocalFrame& frame) |
| 50 : m_state(State::kInactive), | 53 : m_state(State::kInactive), |
| 51 m_idleCallbackHandle(kInvalidHandle), | 54 m_idleCallbackHandle(kInvalidHandle), |
| 52 m_needsMoreColdModeInvocationForTesting(false), | 55 m_needsMoreColdModeInvocationForTesting(false), |
| 53 m_frame(frame), | 56 m_frame(frame), |
| 54 m_lastProcessedUndoStepSequence(0), | 57 m_lastProcessedUndoStepSequence(0), |
| 58 m_lastCheckedDOMTreeVersionInColdMode(0), | |
| 55 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), | 59 m_coldModeTimer(TaskRunnerHelper::get(TaskType::UnspecedTimer, &frame), |
| 56 this, | 60 this, |
| 57 &IdleSpellCheckCallback::coldModeTimerFired) {} | 61 &IdleSpellCheckCallback::coldModeTimerFired) {} |
| 58 | 62 |
| 59 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { | 63 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { |
| 60 return frame().spellChecker().spellCheckRequester(); | 64 return frame().spellChecker().spellCheckRequester(); |
| 61 } | 65 } |
| 62 | 66 |
| 63 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { | 67 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { |
| 64 return frame().spellChecker().isSpellCheckingEnabled(); | 68 return frame().spellChecker().isSpellCheckingEnabled(); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 if (step->sequenceNumber() <= watermark) | 149 if (step->sequenceNumber() <= watermark) |
| 146 break; | 150 break; |
| 147 m_lastProcessedUndoStepSequence = | 151 m_lastProcessedUndoStepSequence = |
| 148 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); | 152 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); |
| 149 if (deadline->timeRemaining() == 0) | 153 if (deadline->timeRemaining() == 0) |
| 150 break; | 154 break; |
| 151 requester.checkSpellingAt(step->endingSelection().extent()); | 155 requester.checkSpellingAt(step->endingSelection().extent()); |
| 152 } | 156 } |
| 153 } | 157 } |
| 154 | 158 |
| 159 // TODO(xiaochengh): Deduplicate with SpellChecker::chunkAndMarkAllMisspellings. | |
| 160 void IdleSpellCheckCallback::chunkAndRequestFullCheckingFor( | |
| 161 const Element& editable) { | |
| 162 const EphemeralRange& fullRange = EphemeralRange::rangeOfContents(editable); | |
| 163 int fullLength = TextIterator::rangeLength(fullRange.startPosition(), | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/int/const int/
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 164 fullRange.endPosition()); | |
| 165 | |
| 166 // Check the full content if it is short. | |
| 167 if (fullLength <= kColdModeChunkSize) { | |
| 168 SpellCheckRequest* fullRequest = SpellCheckRequest::create(fullRange); | |
| 169 spellCheckRequester().requestCheckingFor(fullRequest); | |
| 170 return; | |
| 171 } | |
| 172 | |
| 173 // TODO(xiaochengh): Figure out if this is going to cause performance issues. | |
| 174 // In that case, we need finer-grained control over request generation. | |
| 175 Position chunkStart = fullRange.startPosition(); | |
| 176 int chunkLimit = fullLength / kColdModeChunkSize + 1; | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/int/const int/
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 177 for (int requestNum = 0; requestNum <= chunkLimit; ++requestNum) { | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/requestNum/chunkNumber/
or
nit: s/requestNu
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 178 Position chunkEnd = calculateCharacterSubrange( | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/Position/const Position/
Xiaocheng
2017/03/07 04:20:29
Done.
| |
| 179 EphemeralRange(chunkStart, fullRange.endPosition()), | |
| 180 0, kColdModeChunkSize) | |
| 181 .endPosition(); | |
| 182 if (chunkEnd <= chunkStart) | |
| 183 break; | |
| 184 EphemeralRange chunkRange(chunkStart, chunkEnd); | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/EphemeralRange/const EphmeralRange/
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 185 EphemeralRange checkRange = requestNum | |
|
yosin_UTC9
2017/03/07 01:47:03
nit: s/EphmeralRange/const EphemeralRange&/ to avo
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 186 ? expandEndToSentenceBoundary(chunkRange) | |
| 187 : expandRangeToSentenceBoundary(chunkRange); | |
| 188 | |
| 189 SpellCheckRequest* chunkRequest = | |
| 190 SpellCheckRequest::create(checkRange, requestNum); | |
| 191 spellCheckRequester().requestCheckingFor(chunkRequest); | |
| 192 | |
| 193 chunkStart = checkRange.endPosition(); | |
| 194 } | |
| 195 } | |
| 196 | |
| 155 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { | 197 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { |
| 156 // TODO(xiaochengh): Implementation. | 198 TRACE_EVENT0("blink", "IdleSpellCheckCallback::coldModeInvocation"); |
| 199 | |
| 200 // TODO(xiaochengh): Figure out if this has any performance impact. | |
| 201 frame().document()->updateStyleAndLayout(); | |
| 202 | |
| 203 Node* body = frame().document()->body(); | |
|
yosin_UTC9
2017/03/07 01:47:03
|body| can be null, e.g. no BODY element.
We may w
Xiaocheng
2017/03/07 04:20:30
Done.
| |
| 204 if (m_lastCheckedDOMTreeVersionInColdMode != | |
| 205 frame().document()->domTreeVersion()) | |
| 206 m_nextNodeInColdMode = body; | |
| 207 | |
| 208 while (m_nextNodeInColdMode && deadline->timeRemaining() > 0) { | |
| 209 const Position& refPosition = | |
| 210 Position::firstPositionInNode(m_nextNodeInColdMode); | |
| 211 if (m_nextNodeInColdMode->isElementNode() && | |
| 212 isEditablePosition(refPosition) && | |
| 213 SpellChecker::isSpellCheckingEnabledAt(refPosition)) { | |
| 214 chunkAndRequestFullCheckingFor(toElement(*m_nextNodeInColdMode)); | |
| 215 m_nextNodeInColdMode = | |
| 216 FlatTreeTraversal::nextSkippingChildren(*m_nextNodeInColdMode, body); | |
| 217 } else { | |
|
yosin_UTC9
2017/03/07 01:47:03
Prefer early-continue style to shallow indentation
Xiaocheng
2017/03/07 04:20:29
Done by introducing |shouldCheckNodeInColdMode()|
| |
| 218 m_nextNodeInColdMode = | |
| 219 FlatTreeTraversal::next(*m_nextNodeInColdMode, body); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 m_lastCheckedDOMTreeVersionInColdMode = frame().document()->domTreeVersion(); | |
| 157 } | 224 } |
| 158 | 225 |
| 159 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { | 226 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { |
| 160 if (m_needsMoreColdModeInvocationForTesting) { | 227 if (m_needsMoreColdModeInvocationForTesting) { |
| 161 m_needsMoreColdModeInvocationForTesting = false; | 228 m_needsMoreColdModeInvocationForTesting = false; |
| 162 return false; | 229 return false; |
| 163 } | 230 } |
| 164 | 231 |
| 165 // TODO(xiaochengh): Implementation. | 232 return !m_nextNodeInColdMode; |
| 166 return true; | |
| 167 } | 233 } |
| 168 | 234 |
| 169 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { | 235 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { |
| 170 DCHECK(frame().document()); | 236 DCHECK(frame().document()); |
| 171 DCHECK(frame().document()->isActive()); | 237 DCHECK(frame().document()->isActive()); |
| 172 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); | 238 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); |
| 173 m_idleCallbackHandle = kInvalidHandle; | 239 m_idleCallbackHandle = kInvalidHandle; |
| 174 | 240 |
| 175 if (!isSpellCheckingEnabled()) { | 241 if (!isSpellCheckingEnabled()) { |
| 176 deactivate(); | 242 deactivate(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 } | 295 } |
| 230 } | 296 } |
| 231 | 297 |
| 232 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { | 298 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { |
| 233 DCHECK(m_coldModeTimer.isActive()); | 299 DCHECK(m_coldModeTimer.isActive()); |
| 234 m_coldModeTimer.stop(); | 300 m_coldModeTimer.stop(); |
| 235 coldModeTimerFired(&m_coldModeTimer); | 301 coldModeTimerFired(&m_coldModeTimer); |
| 236 } | 302 } |
| 237 | 303 |
| 238 } // namespace blink | 304 } // namespace blink |
| OLD | NEW |