| 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 #ifndef IdleSpellCheckCallback_h | 5 #ifndef IdleSpellCheckCallback_h |
| 6 #define IdleSpellCheckCallback_h | 6 #define IdleSpellCheckCallback_h |
| 7 | 7 |
| 8 #include "core/dom/IdleRequestCallback.h" | 8 #include "core/dom/IdleRequestCallback.h" |
| 9 #include "core/dom/SynchronousMutationObserver.h" | |
| 10 #include "platform/Timer.h" | 9 #include "platform/Timer.h" |
| 11 | 10 |
| 12 namespace blink { | 11 namespace blink { |
| 13 | 12 |
| 14 class LocalFrame; | 13 class LocalFrame; |
| 15 class SpellCheckRequester; | 14 class SpellCheckRequester; |
| 16 | 15 |
| 17 // Main class for the implementation of idle time spell checker. | 16 // Main class for the implementation of idle time spell checker. |
| 18 class CORE_EXPORT IdleSpellCheckCallback final | 17 class CORE_EXPORT IdleSpellCheckCallback final : public IdleRequestCallback { |
| 19 : public IdleRequestCallback, | |
| 20 public SynchronousMutationObserver { | |
| 21 DISALLOW_COPY_AND_ASSIGN(IdleSpellCheckCallback); | |
| 22 USING_GARBAGE_COLLECTED_MIXIN(IdleSpellCheckCallback); | |
| 23 | |
| 24 public: | 18 public: |
| 25 static IdleSpellCheckCallback* create(LocalFrame&); | 19 static IdleSpellCheckCallback* create(LocalFrame&); |
| 26 ~IdleSpellCheckCallback() override; | 20 ~IdleSpellCheckCallback() override; |
| 27 | 21 |
| 22 // Transit to HotModeRequested, if possible. Called by operations that need |
| 23 // spell checker to follow up. |
| 24 // TODO(xiaochengh): Add proper call sites. |
| 25 void setNeedsHotModeInvocation(); |
| 26 |
| 27 // Transit to ColdModeTimerStarted, if possible. Sets up a timer, and requests |
| 28 // cold mode invocation if no critical operation occurs before timer firing. |
| 29 // TODO(xiaochengh): Add proper call sites. |
| 30 void setNeedsColdModeInvocation(); |
| 31 |
| 32 // Cleans everything up and makes the callback inactive. Should be called when |
| 33 // document is detached or spellchecking is globally disabled. |
| 34 // TODO(xiaochengh): Add proper call sites. |
| 35 void deactivate(); |
| 36 |
| 37 // Exposed for testing only. |
| 38 SpellCheckRequester& spellCheckRequester() const; |
| 39 |
| 40 DECLARE_VIRTUAL_TRACE(); |
| 41 |
| 42 private: |
| 43 explicit IdleSpellCheckCallback(LocalFrame&); |
| 44 void handleEvent(IdleDeadline*) override; |
| 45 |
| 46 LocalFrame& frame() const { return *m_frame; } |
| 47 |
| 28 enum class State { | 48 enum class State { |
| 29 kInactive, | 49 kInactive, |
| 30 kHotModeRequested, | 50 kHotModeRequested, |
| 31 kInHotModeInvocation, | 51 kInHotModeInvocation, |
| 32 kColdModeTimerStarted, | 52 kColdModeTimerStarted, |
| 33 kColdModeRequested, | 53 kColdModeRequested, |
| 34 kInColdModeInvocation | 54 kInColdModeInvocation |
| 35 }; | 55 }; |
| 36 | 56 |
| 37 State state() const { return m_state; } | |
| 38 | |
| 39 // Transit to HotModeRequested, if possible. Called by operations that need | |
| 40 // spell checker to follow up. | |
| 41 void setNeedsInvocation(); | |
| 42 | |
| 43 // Cleans everything up and makes the callback inactive. Should be called when | |
| 44 // document is detached or spellchecking is globally disabled. | |
| 45 void deactivate(); | |
| 46 | |
| 47 void documentAttached(Document*); | |
| 48 | |
| 49 // Exposed for testing only. | |
| 50 SpellCheckRequester& spellCheckRequester() const; | |
| 51 void forceInvocationForTesting(); | |
| 52 void setNeedsMoreColdModeInvocationForTesting() { | |
| 53 m_needsMoreColdModeInvocationForTesting = true; | |
| 54 } | |
| 55 void skipColdModeTimerForTesting(); | |
| 56 int idleCallbackHandle() const { return m_idleCallbackHandle; } | |
| 57 | |
| 58 DECLARE_VIRTUAL_TRACE(); | |
| 59 | |
| 60 private: | |
| 61 explicit IdleSpellCheckCallback(LocalFrame&); | |
| 62 void handleEvent(IdleDeadline*) override; | |
| 63 | |
| 64 LocalFrame& frame() const { return *m_frame; } | |
| 65 | |
| 66 // Returns whether spell checking is globally enabled. | 57 // Returns whether spell checking is globally enabled. |
| 67 bool isSpellCheckingEnabled() const; | 58 bool isSpellCheckingEnabled() const; |
| 68 | 59 |
| 69 // Calls requestIdleCallback with this IdleSpellCheckCallback. | 60 // Calls requestIdleCallback with this IdleSpellCheckCallback. |
| 70 void requestInvocation(); | 61 void requestInvocation(); |
| 71 | 62 |
| 72 // Functions for hot mode. | 63 // Functions for hot mode. |
| 73 void hotModeInvocation(IdleDeadline*); | 64 void hotModeInvocation(IdleDeadline*); |
| 74 | 65 |
| 75 // Transit to ColdModeTimerStarted, if possible. Sets up a timer, and requests | |
| 76 // cold mode invocation if no critical operation occurs before timer firing. | |
| 77 void setNeedsColdModeInvocation(); | |
| 78 | |
| 79 // Functions for cold mode. | 66 // Functions for cold mode. |
| 80 void coldModeTimerFired(TimerBase*); | 67 void coldModeTimerFired(TimerBase*); |
| 81 void coldModeInvocation(IdleDeadline*); | 68 void coldModeInvocation(IdleDeadline*); |
| 82 bool coldModeFinishesFullDocument() const; | 69 bool coldModeFinishesFullDocument() const; |
| 83 | 70 |
| 84 // Implements |SynchronousMutationObserver|. | |
| 85 void contextDestroyed(Document*) final; | |
| 86 | |
| 87 State m_state; | 71 State m_state; |
| 88 int m_idleCallbackHandle; | |
| 89 mutable bool m_needsMoreColdModeInvocationForTesting; | |
| 90 const Member<LocalFrame> m_frame; | 72 const Member<LocalFrame> m_frame; |
| 91 | 73 |
| 92 TaskRunnerTimer<IdleSpellCheckCallback> m_coldModeTimer; | 74 TaskRunnerTimer<IdleSpellCheckCallback> m_coldModeTimer; |
| 93 }; | 75 }; |
| 94 | 76 |
| 95 } // namespace blink | 77 } // namespace blink |
| 96 | 78 |
| 97 #endif // IdleSpellCheckCallback_h | 79 #endif // IdleSpellCheckCallback_h |
| OLD | NEW |