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() { | 67 void IdleSpellCheckCallback::requestInvocation(int timeoutMS) { |
yosin_UTC9
2017/03/07 08:15:30
Should we use base::TimeDeltta, available in WTF/T
Xiaocheng
2017/03/07 21:27:44
I still prefer just an |int|, as IdleRequestOption
| |
68 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle); | 68 DCHECK_EQ(m_idleCallbackHandle, kInvalidHandle); |
69 | 69 |
70 IdleRequestOptions options; | 70 IdleRequestOptions options; |
71 options.setTimeout(kRequestTimeoutMS); | 71 if (timeoutMS > 0) |
72 options.setTimeout(timeoutMS); | |
72 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options); | 73 m_idleCallbackHandle = frame().document()->requestIdleCallback(this, options); |
73 } | 74 } |
74 | 75 |
75 void IdleSpellCheckCallback::deactivate() { | 76 void IdleSpellCheckCallback::deactivate() { |
76 m_state = State::kInactive; | 77 m_state = State::kInactive; |
77 if (m_coldModeTimer.isActive()) | 78 if (m_coldModeTimer.isActive()) |
78 m_coldModeTimer.stop(); | 79 m_coldModeTimer.stop(); |
79 if (m_idleCallbackHandle != kInvalidHandle) | 80 if (m_idleCallbackHandle != kInvalidHandle) |
80 frame().document()->cancelIdleCallback(m_idleCallbackHandle); | 81 frame().document()->cancelIdleCallback(m_idleCallbackHandle); |
81 m_idleCallbackHandle = kInvalidHandle; | 82 m_idleCallbackHandle = kInvalidHandle; |
82 } | 83 } |
83 | 84 |
84 void IdleSpellCheckCallback::setNeedsInvocation() { | 85 void IdleSpellCheckCallback::setNeedsInvocation() { |
85 if (!isSpellCheckingEnabled()) { | 86 if (!isSpellCheckingEnabled()) { |
86 deactivate(); | 87 deactivate(); |
87 return; | 88 return; |
88 } | 89 } |
89 | 90 |
90 if (m_state == State::kHotModeRequested) | 91 if (m_state == State::kHotModeRequested) |
91 return; | 92 return; |
92 | 93 |
93 if (m_state == State::kColdModeTimerStarted) { | 94 if (m_state == State::kColdModeTimerStarted) { |
94 DCHECK(m_coldModeTimer.isActive()); | 95 DCHECK(m_coldModeTimer.isActive()); |
95 m_coldModeTimer.stop(); | 96 m_coldModeTimer.stop(); |
96 } | 97 } |
97 | 98 |
98 if (m_state != State::kColdModeRequested) | 99 if (m_state == State::kColdModeRequested) { |
99 requestInvocation(); | 100 frame().document()->cancelIdleCallback(m_idleCallbackHandle); |
101 m_idleCallbackHandle = kInvalidHandle; | |
102 } | |
103 | |
104 requestInvocation(kHotModeRequestTimeoutMS); | |
100 m_state = State::kHotModeRequested; | 105 m_state = State::kHotModeRequested; |
101 } | 106 } |
102 | 107 |
103 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { | 108 void IdleSpellCheckCallback::setNeedsColdModeInvocation() { |
104 if (!isSpellCheckingEnabled()) { | 109 if (!isSpellCheckingEnabled()) { |
105 deactivate(); | 110 deactivate(); |
106 return; | 111 return; |
107 } | 112 } |
108 | 113 |
109 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && | 114 if (m_state != State::kInactive && m_state != State::kInHotModeInvocation && |
110 m_state != State::kInColdModeInvocation) | 115 m_state != State::kInColdModeInvocation) |
111 return; | 116 return; |
112 | 117 |
113 DCHECK(!m_coldModeTimer.isActive()); | 118 DCHECK(!m_coldModeTimer.isActive()); |
114 int intervalMS = m_state == State::kInColdModeInvocation | 119 int intervalMS = m_state == State::kInColdModeInvocation |
115 ? kConsecutiveColdModeTimerIntervalMS | 120 ? kConsecutiveColdModeTimerIntervalMS |
116 : kColdModeTimerIntervalMS; | 121 : kColdModeTimerIntervalMS; |
117 m_coldModeTimer.startOneShot(intervalMS / 1000.0, BLINK_FROM_HERE); | 122 m_coldModeTimer.startOneShot(intervalMS / 1000.0, BLINK_FROM_HERE); |
118 m_state = State::kColdModeTimerStarted; | 123 m_state = State::kColdModeTimerStarted; |
119 } | 124 } |
120 | 125 |
121 void IdleSpellCheckCallback::coldModeTimerFired(TimerBase*) { | 126 void IdleSpellCheckCallback::coldModeTimerFired(TimerBase*) { |
122 DCHECK_EQ(State::kColdModeTimerStarted, m_state); | 127 DCHECK_EQ(State::kColdModeTimerStarted, m_state); |
123 | 128 |
124 if (!isSpellCheckingEnabled()) { | 129 if (!isSpellCheckingEnabled()) { |
125 deactivate(); | 130 deactivate(); |
126 return; | 131 return; |
127 } | 132 } |
128 | 133 |
129 requestInvocation(); | 134 requestInvocation(0); |
130 m_state = State::kColdModeRequested; | 135 m_state = State::kColdModeRequested; |
131 } | 136 } |
132 | 137 |
133 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { | 138 void IdleSpellCheckCallback::hotModeInvocation(IdleDeadline* deadline) { |
134 TRACE_EVENT0("blink", "IdleSpellCheckCallback::hotModeInvocation"); | 139 TRACE_EVENT0("blink", "IdleSpellCheckCallback::hotModeInvocation"); |
135 | 140 |
136 // TODO(xiaochengh): Figure out if this has any performance impact. | 141 // TODO(xiaochengh): Figure out if this has any performance impact. |
137 frame().document()->updateStyleAndLayout(); | 142 frame().document()->updateStyleAndLayout(); |
138 | 143 |
139 HotModeSpellCheckRequester requester(spellCheckRequester()); | 144 HotModeSpellCheckRequester requester(spellCheckRequester()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 } | 234 } |
230 } | 235 } |
231 | 236 |
232 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { | 237 void IdleSpellCheckCallback::skipColdModeTimerForTesting() { |
233 DCHECK(m_coldModeTimer.isActive()); | 238 DCHECK(m_coldModeTimer.isActive()); |
234 m_coldModeTimer.stop(); | 239 m_coldModeTimer.stop(); |
235 coldModeTimerFired(&m_coldModeTimer); | 240 coldModeTimerFired(&m_coldModeTimer); |
236 } | 241 } |
237 | 242 |
238 } // namespace blink | 243 } // namespace blink |
OLD | NEW |