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/spellcheck/HotModeSpellCheckRequester.h" | 16 #include "core/editing/spellcheck/HotModeSpellCheckRequester.h" |
17 #include "core/editing/spellcheck/SpellCheckRequester.h" | 17 #include "core/editing/spellcheck/SpellCheckRequester.h" |
18 #include "core/editing/spellcheck/SpellChecker.h" | 18 #include "core/editing/spellcheck/SpellChecker.h" |
19 #include "core/frame/LocalFrame.h" | 19 #include "core/frame/LocalFrame.h" |
20 #include "platform/RuntimeEnabledFeatures.h" | 20 #include "platform/RuntimeEnabledFeatures.h" |
21 #include "platform/instrumentation/tracing/TraceEvent.h" | 21 #include "platform/instrumentation/tracing/TraceEvent.h" |
22 #include "wtf/CurrentTime.h" | 22 #include "wtf/CurrentTime.h" |
23 | 23 |
24 namespace blink { | 24 namespace blink { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const int kColdModeTimerIntervalMS = 1000; | 28 const int kColdModeTimerIntervalMS = 1000; |
29 const int kConsecutiveColdModeTimerIntervalMS = 200; | 29 const int kConsecutiveColdModeTimerIntervalMS = 200; |
30 const int kRequestTimeoutMS = 200; | 30 const int kHotModeRequestTimeoutMS = 200; |
31 const int kInvalidHandle = -1; | 31 const int kInvalidHandle = -1; |
32 const int kDummyHandleForForcedInvocation = -2; | 32 const int kDummyHandleForForcedInvocation = -2; |
33 const double kForcedInvocationDeadlineSeconds = 10; | 33 const double kForcedInvocationDeadlineSeconds = 10; |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} | 37 IdleSpellCheckCallback::~IdleSpellCheckCallback() {} |
38 | 38 |
39 DEFINE_TRACE(IdleSpellCheckCallback) { | 39 DEFINE_TRACE(IdleSpellCheckCallback) { |
40 visitor->trace(m_frame); | 40 visitor->trace(m_frame); |
(...skipping 16 matching lines...) Expand all Loading... |
57 &IdleSpellCheckCallback::coldModeTimerFired) {} | 57 &IdleSpellCheckCallback::coldModeTimerFired) {} |
58 | 58 |
59 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { | 59 SpellCheckRequester& IdleSpellCheckCallback::spellCheckRequester() const { |
60 return frame().spellChecker().spellCheckRequester(); | 60 return frame().spellChecker().spellCheckRequester(); |
61 } | 61 } |
62 | 62 |
63 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { | 63 bool IdleSpellCheckCallback::isSpellCheckingEnabled() const { |
64 return frame().spellChecker().isSpellCheckingEnabled(); | 64 return frame().spellChecker().isSpellCheckingEnabled(); |
65 } | 65 } |
66 | 66 |
67 void IdleSpellCheckCallback::requestInvocation() { | |
68 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle); | |
69 | |
70 IdleRequestOptions options; | |
71 options.setTimeout(kRequestTimeoutMS); | |
72 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options); | |
73 } | |
74 | |
75 void IdleSpellCheckCallback::deactivate() { | 67 void IdleSpellCheckCallback::deactivate() { |
76 m_state = State::kInactive; | 68 m_state = State::kInactive; |
77 if (m_coldModeTimer.isActive()) | 69 if (m_coldModeTimer.isActive()) |
78 m_coldModeTimer.stop(); | 70 m_coldModeTimer.stop(); |
79 if (m_idleCallbackHandle != kInvalidHandle) | 71 if (m_idleCallbackHandle != kInvalidHandle) |
80 frame().document()->cancelIdleCallback(m_idleCallbackHandle); | 72 frame().document()->cancelIdleCallback(m_idleCallbackHandle); |
81 m_idleCallbackHandle = kInvalidHandle; | 73 m_idleCallbackHandle = kInvalidHandle; |
82 } | 74 } |
83 | 75 |
84 void IdleSpellCheckCallback::setNeedsInvocation() { | 76 void IdleSpellCheckCallback::setNeedsInvocation() { |
85 if (!isSpellCheckingEnabled()) { | 77 if (!isSpellCheckingEnabled()) { |
86 deactivate(); | 78 deactivate(); |
87 return; | 79 return; |
88 } | 80 } |
89 | 81 |
90 if (m_state == State::kHotModeRequested) | 82 if (m_state == State::kHotModeRequested) |
91 return; | 83 return; |
92 | 84 |
93 if (m_state == State::kColdModeTimerStarted) { | 85 if (m_state == State::kColdModeTimerStarted) { |
94 DCHECK(m_coldModeTimer.isActive()); | 86 DCHECK(m_coldModeTimer.isActive()); |
95 m_coldModeTimer.stop(); | 87 m_coldModeTimer.stop(); |
96 } | 88 } |
97 | 89 |
98 if (m_state != State::kColdModeRequested) | 90 if (m_state == State::kColdModeRequested) { |
99 requestInvocation(); | 91 frame().document()->cancelIdleCallback(m_idleCallbackHandle); |
| 92 m_idleCallbackHandle = kInvalidHandle; |
| 93 } |
| 94 |
| 95 IdleRequestOptions options; |
| 96 options.setTimeout(kHotModeRequestTimeoutMS); |
| 97 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options); |
100 m_state = State::kHotModeRequested; | 98 m_state = State::kHotModeRequested; |
101 } | 99 } |
102 | 100 |
103 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { | 101 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { |
104 if (!isSpellCheckingEnabled()) { | 102 if (!isSpellCheckingEnabled()) { |
105 deactivate(); | 103 deactivate(); |
106 return; | 104 return; |
107 } | 105 } |
108 | 106 |
109 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && | 107 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && |
110 m_state != State::kInColdModeInvocation) | 108 m_state != State::kInColdModeInvocation) |
111 return; | 109 return; |
112 | 110 |
113 DCHECK(!m_coldModeTimer.isActive()); | 111 DCHECK(!m_coldModeTimer.isActive()); |
114 int intervalMS = m_state == State::kInColdModeInvocation | 112 int intervalMS = m_state == State::kInColdModeInvocation |
115 ? kConsecutiveColdModeTimerIntervalMS | 113 ? kConsecutiveColdModeTimerIntervalMS |
116 : kColdModeTimerIntervalMS; | 114 : kColdModeTimerIntervalMS; |
117 m_coldModeTimer.startOneShot(intervalMS / 1000.0, BLINK_FROM_HERE); | 115 m_coldModeTimer.startOneShot(intervalMS / 1000.0, BLINK_FROM_HERE); |
118 m_state = State::kColdModeTimerStarted; | 116 m_state = State::kColdModeTimerStarted; |
119 } | 117 } |
120 | 118 |
121 void IdleSpellCheckCallback::coldModeTimerFired(TimerBase*) { | 119 void IdleSpellCheckCallback::coldModeTimerFired(TimerBase*) { |
122 DCHECK_EQ(State::kColdModeTimerStarted, m_state); | 120 DCHECK_EQ(State::kColdModeTimerStarted, m_state); |
123 | 121 |
124 if (!isSpellCheckingEnabled()) { | 122 if (!isSpellCheckingEnabled()) { |
125 deactivate(); | 123 deactivate(); |
126 return; | 124 return; |
127 } | 125 } |
128 | 126 |
129 requestInvocation(); | 127 m_idleCallbackHandle = |
| 128 frame().document()->requestIdleCallback(this, IdleRequestOptions()); |
130 m_state = State::kColdModeRequested; | 129 m_state = State::kColdModeRequested; |
131 } | 130 } |
132 | 131 |
133 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { | 132 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { |
134 TRACE_EVENT0("blink", "IdleSpellCheckCallback::hotModeInvocation"); | 133 TRACE_EVENT0("blink", "IdleSpellCheckCallback::hotModeInvocation"); |
135 | 134 |
136 // TODO(xiaochengh): Figure out if this has any performance impact. | 135 // TODO(xiaochengh): Figure out if this has any performance impact. |
137 frame().document()->updateStyleAndLayout(); | 136 frame().document()->updateStyleAndLayout(); |
138 | 137 |
139 HotModeSpellCheckRequester requester(spellCheckRequester()); | 138 HotModeSpellCheckRequester requester(spellCheckRequester()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 } | 228 } |
230 } | 229 } |
231 | 230 |
232 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { | 231 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { |
233 DCHECK(m_coldModeTimer.isActive()); | 232 DCHECK(m_coldModeTimer.isActive()); |
234 m_coldModeTimer.stop(); | 233 m_coldModeTimer.stop(); |
235 coldModeTimerFired(&m_coldModeTimer); | 234 coldModeTimerFired(&m_coldModeTimer); |
236 } | 235 } |
237 | 236 |
238 } // namespace blink | 237 } // namespace blink |
OLD | NEW |