| 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 kHotModeRequestTimeoutMS = 200; | 32 const int kHotModeRequestTimeoutMS = 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 if (step->sequenceNumber() <= watermark) | 157 if (step->sequenceNumber() <= watermark) |
| 145 break; | 158 break; |
| 146 m_lastProcessedUndoStepSequence = | 159 m_lastProcessedUndoStepSequence = |
| 147 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); | 160 std::max(step->sequenceNumber(), m_lastProcessedUndoStepSequence); |
| 148 if (deadline->timeRemaining() == 0) | 161 if (deadline->timeRemaining() == 0) |
| 149 break; | 162 break; |
| 150 requester.checkSpellingAt(step->endingSelection().extent()); | 163 requester.checkSpellingAt(step->endingSelection().extent()); |
| 151 } | 164 } |
| 152 } | 165 } |
| 153 | 166 |
| 167 // TODO(xiaochengh): Deduplicate with SpellChecker::chunkAndMarkAllMisspellings. |
| 168 void IdleSpellCheckCallback::chunkAndRequestFullCheckingFor( |
| 169 const Element& editable) { |
| 170 const EphemeralRange& fullRange = EphemeralRange::rangeOfContents(editable); |
| 171 const int fullLength = TextIterator::rangeLength(fullRange.startPosition(), |
| 172 fullRange.endPosition()); |
| 173 |
| 174 // Check the full content if it is short. |
| 175 if (fullLength <= kColdModeChunkSize) { |
| 176 SpellCheckRequest* fullRequest = SpellCheckRequest::create(fullRange); |
| 177 spellCheckRequester().requestCheckingFor(fullRequest); |
| 178 return; |
| 179 } |
| 180 |
| 181 // TODO(xiaochengh): Figure out if this is going to cause performance issues. |
| 182 // In that case, we need finer-grained control over request generation. |
| 183 Position chunkStart = fullRange.startPosition(); |
| 184 const int chunkLimit = fullLength / kColdModeChunkSize + 1; |
| 185 for (int chunkIndex = 0; chunkIndex <= chunkLimit; ++chunkIndex) { |
| 186 const Position& chunkEnd = |
| 187 calculateCharacterSubrange( |
| 188 EphemeralRange(chunkStart, fullRange.endPosition()), 0, |
| 189 kColdModeChunkSize) |
| 190 .endPosition(); |
| 191 if (chunkEnd <= chunkStart) |
| 192 break; |
| 193 const EphemeralRange chunkRange(chunkStart, chunkEnd); |
| 194 const EphemeralRange& checkRange = |
| 195 chunkIndex >= 1 ? expandEndToSentenceBoundary(chunkRange) |
| 196 : expandRangeToSentenceBoundary(chunkRange); |
| 197 |
| 198 SpellCheckRequest* chunkRequest = |
| 199 SpellCheckRequest::create(checkRange, chunkIndex); |
| 200 spellCheckRequester().requestCheckingFor(chunkRequest); |
| 201 |
| 202 chunkStart = checkRange.endPosition(); |
| 203 } |
| 204 } |
| 205 |
| 154 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { | 206 void IdleSpellCheckCallback::coldModeInvocation(IdleDeadline* deadline) { |
| 155 // TODO(xiaochengh): Implementation. | 207 TRACE_EVENT0("blink", "IdleSpellCheckCallback::coldModeInvocation"); |
| 208 |
| 209 Node* body = frame().document()->body(); |
| 210 if (!body) { |
| 211 m_nextNodeInColdMode = nullptr; |
| 212 m_lastCheckedDOMTreeVersionInColdMode = |
| 213 frame().document()->domTreeVersion(); |
| 214 return; |
| 215 } |
| 216 |
| 217 // TODO(xiaochengh): Figure out if this has any performance impact. |
| 218 frame().document()->updateStyleAndLayout(); |
| 219 |
| 220 if (m_lastCheckedDOMTreeVersionInColdMode != |
| 221 frame().document()->domTreeVersion()) |
| 222 m_nextNodeInColdMode = body; |
| 223 |
| 224 while (m_nextNodeInColdMode && deadline->timeRemaining() > 0) { |
| 225 if (!shouldCheckNodeInColdMode(*m_nextNodeInColdMode)) { |
| 226 m_nextNodeInColdMode = |
| 227 FlatTreeTraversal::next(*m_nextNodeInColdMode, body); |
| 228 continue; |
| 229 } |
| 230 |
| 231 chunkAndRequestFullCheckingFor(toElement(*m_nextNodeInColdMode)); |
| 232 m_nextNodeInColdMode = |
| 233 FlatTreeTraversal::nextSkippingChildren(*m_nextNodeInColdMode, body); |
| 234 } |
| 235 |
| 236 m_lastCheckedDOMTreeVersionInColdMode = frame().document()->domTreeVersion(); |
| 156 } | 237 } |
| 157 | 238 |
| 158 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { | 239 bool IdleSpellCheckCallback::coldModeFinishesFullDocument() const { |
| 159 if (m_needsMoreColdModeInvocationForTesting) { | 240 if (m_needsMoreColdModeInvocationForTesting) { |
| 160 m_needsMoreColdModeInvocationForTesting = false; | 241 m_needsMoreColdModeInvocationForTesting = false; |
| 161 return false; | 242 return false; |
| 162 } | 243 } |
| 163 | 244 |
| 164 // TODO(xiaochengh): Implementation. | 245 return !m_nextNodeInColdMode; |
| 165 return true; | |
| 166 } | 246 } |
| 167 | 247 |
| 168 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { | 248 void IdleSpellCheckCallback::handleEvent(IdleDeadline* deadline) { |
| 169 DCHECK(frame().document()); | 249 DCHECK(frame().document()); |
| 170 DCHECK(frame().document()->isActive()); | 250 DCHECK(frame().document()->isActive()); |
| 171 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); | 251 DCHECK_NE(m_idleCallbackHandle, kInvalidHandle); |
| 172 m_idleCallbackHandle = kInvalidHandle; | 252 m_idleCallbackHandle = kInvalidHandle; |
| 173 | 253 |
| 174 if (!isSpellCheckingEnabled()) { | 254 if (!isSpellCheckingEnabled()) { |
| 175 deactivate(); | 255 deactivate(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 308 } |
| 229 } | 309 } |
| 230 | 310 |
| 231 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { | 311 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { |
| 232 DCHECK(m_coldModeTimer.isActive()); | 312 DCHECK(m_coldModeTimer.isActive()); |
| 233 m_coldModeTimer.stop(); | 313 m_coldModeTimer.stop(); |
| 234 coldModeTimerFired(&m_coldModeTimer); | 314 coldModeTimerFired(&m_coldModeTimer); |
| 235 } | 315 } |
| 236 | 316 |
| 237 } // namespace blink | 317 } // namespace blink |
| OLD | NEW |