ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_manager_impl.h
blobf0f7a2d2885e418fe0a53cb136d780315054d1a9
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/thread_checker.h"
15 #include "chrome/browser/chromeos/input_method/candidate_window_controller.h"
16 #include "chrome/browser/chromeos/input_method/input_method_util.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "ui/base/ime/chromeos/input_method_manager.h"
19 #include "ui/base/ime/chromeos/input_method_whitelist.h"
21 namespace chromeos {
22 class ComponentExtensionIMEManager;
23 class ComponentExtensionIMEManagerDelegate;
24 class InputMethodEngine;
25 namespace input_method {
26 class InputMethodDelegate;
27 class ImeKeyboard;
29 // The implementation of InputMethodManager.
30 class InputMethodManagerImpl : public InputMethodManager,
31 public CandidateWindowController::Observer {
32 public:
33 class StateImpl : public InputMethodManager::State {
34 public:
35 StateImpl(InputMethodManagerImpl* manager, Profile* profile);
37 // Init new state as a copy of other.
38 void InitFrom(const StateImpl& other);
40 // Returns true if (manager_->state_ == this).
41 bool IsActive() const;
43 // Returns human-readable dump (for debug).
44 std::string Dump() const;
46 // Adds new input method to given list if possible
47 bool EnableInputMethodImpl(
48 const std::string& input_method_id,
49 std::vector<std::string>* new_active_input_method_ids) const;
51 // Returns true if |input_method_id| is in |active_input_method_ids|.
52 bool InputMethodIsActivated(const std::string& input_method_id) const;
54 // If |current_input_methodid_| is not in |input_method_ids|, switch to
55 // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
56 // input_method_ids[N+1].
57 void SwitchToNextInputMethodInternal(
58 const std::vector<std::string>& input_method_ids,
59 const std::string& current_input_methodid);
61 // Returns the IDs of the subset of input methods which are active and are
62 // associated with |accelerator|. For example,
63 // { "mozc-hangul", "xkb:kr:kr104:kor" } is returned for
64 // ui::VKEY_DBE_SBCSCHAR if the two input methods are active.
65 void GetCandidateInputMethodsForAccelerator(
66 const ui::Accelerator& accelerator,
67 std::vector<std::string>* out_candidate_ids);
69 // Returns true if given input method requires pending extension.
70 bool MethodAwaitsExtensionLoad(const std::string& input_method_id) const;
72 // InputMethodManager::State overrides.
73 scoped_refptr<InputMethodManager::State> Clone() const override;
74 void AddInputMethodExtension(const std::string& extension_id,
75 const InputMethodDescriptors& descriptors,
76 InputMethodEngineInterface* instance) override;
77 void RemoveInputMethodExtension(const std::string& extension_id) override;
78 void ChangeInputMethod(const std::string& input_method_id,
79 bool show_message) override;
80 bool EnableInputMethod(
81 const std::string& new_active_input_method_id) override;
82 void EnableLoginLayouts(
83 const std::string& language_code,
84 const std::vector<std::string>& initial_layouts) override;
85 void EnableLockScreenLayouts() override;
86 void GetInputMethodExtensions(InputMethodDescriptors* result) override;
87 scoped_ptr<InputMethodDescriptors> GetActiveInputMethods() const override;
88 const std::vector<std::string>& GetActiveInputMethodIds() const override;
89 const InputMethodDescriptor* GetInputMethodFromId(
90 const std::string& input_method_id) const override;
91 size_t GetNumActiveInputMethods() const override;
92 void SetEnabledExtensionImes(std::vector<std::string>* ids) override;
93 void SetInputMethodLoginDefault() override;
94 void SetInputMethodLoginDefaultFromVPD(const std::string& locale,
95 const std::string& layout) override;
96 bool CanCycleInputMethod() override;
97 void SwitchToNextInputMethod() override;
98 void SwitchToPreviousInputMethod() override;
99 bool CanSwitchInputMethod(const ui::Accelerator& accelerator) override;
100 void SwitchInputMethod(const ui::Accelerator& accelerator) override;
101 InputMethodDescriptor GetCurrentInputMethod() const override;
102 bool ReplaceEnabledInputMethods(
103 const std::vector<std::string>& new_active_input_method_ids) override;
105 // ------------------------- Data members.
106 Profile* const profile;
108 // The input method which was/is selected.
109 InputMethodDescriptor previous_input_method;
110 InputMethodDescriptor current_input_method;
112 // The active input method ids cache.
113 std::vector<std::string> active_input_method_ids;
115 // The pending input method id for delayed 3rd party IME enabling.
116 std::string pending_input_method_id;
118 // The list of enabled extension IMEs.
119 std::vector<std::string> enabled_extension_imes;
121 // Extra input methods that have been explicitly added to the menu, such as
122 // those created by extension.
123 std::map<std::string, InputMethodDescriptor> extra_input_methods;
125 InputMethodManagerImpl* const manager_;
127 protected:
128 friend base::RefCounted<chromeos::input_method::InputMethodManager::State>;
129 ~StateImpl() override;
132 // Constructs an InputMethodManager instance. The client is responsible for
133 // calling |SetUISessionState| in response to relevant changes in browser
134 // state.
135 InputMethodManagerImpl(scoped_ptr<InputMethodDelegate> delegate,
136 bool enable_extension_loading);
137 ~InputMethodManagerImpl() override;
139 // Receives notification of an InputMethodManager::UISessionState transition.
140 void SetUISessionState(UISessionState new_ui_session);
142 // InputMethodManager override:
143 UISessionState GetUISessionState() override;
144 void AddObserver(InputMethodManager::Observer* observer) override;
145 void AddCandidateWindowObserver(
146 InputMethodManager::CandidateWindowObserver* observer) override;
147 void RemoveObserver(InputMethodManager::Observer* observer) override;
148 void RemoveCandidateWindowObserver(
149 InputMethodManager::CandidateWindowObserver* observer) override;
150 scoped_ptr<InputMethodDescriptors> GetSupportedInputMethods() const override;
151 void ActivateInputMethodMenuItem(const std::string& key) override;
152 bool IsISOLevel5ShiftUsedByCurrentInputMethod() const override;
153 bool IsAltGrUsedByCurrentInputMethod() const override;
155 ImeKeyboard* GetImeKeyboard() override;
156 InputMethodUtil* GetInputMethodUtil() override;
157 ComponentExtensionIMEManager* GetComponentExtensionIMEManager() override;
158 bool IsLoginKeyboard(const std::string& layout) const override;
160 bool MigrateInputMethods(std::vector<std::string>* input_method_ids) override;
162 scoped_refptr<InputMethodManager::State> CreateNewState(
163 Profile* profile) override;
165 scoped_refptr<InputMethodManager::State> GetActiveIMEState() override;
166 void SetState(scoped_refptr<InputMethodManager::State> state) override;
168 // Sets |candidate_window_controller_|.
169 void SetCandidateWindowControllerForTesting(
170 CandidateWindowController* candidate_window_controller);
171 // Sets |keyboard_|.
172 void SetImeKeyboardForTesting(ImeKeyboard* keyboard);
173 // Initialize |component_extension_manager_|.
174 void InitializeComponentExtensionForTesting(
175 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
177 private:
178 friend class InputMethodManagerImplTest;
180 // CandidateWindowController::Observer overrides:
181 void CandidateClicked(int index) override;
182 void CandidateWindowOpened() override;
183 void CandidateWindowClosed() override;
185 // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
186 // since they are not necessary to input a login password. Users are still
187 // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
188 // French).
189 void OnScreenLocked();
191 // Resumes the original state by activating input methods and/or changing the
192 // current input method as needed.
193 void OnScreenUnlocked();
195 // Returns true if the given input method config value is a string list
196 // that only contains an input method ID of a keyboard layout.
197 bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
199 // Creates and initializes |candidate_window_controller_| if it hasn't been
200 // done.
201 void MaybeInitializeCandidateWindowController();
203 // Returns Input Method that best matches given id.
204 const InputMethodDescriptor* LookupInputMethod(
205 const std::string& input_method_id,
206 StateImpl* state);
208 // Change system input method.
209 void ChangeInputMethodInternal(const InputMethodDescriptor& descriptor,
210 Profile* profile,
211 bool show_message,
212 bool notify_menu);
214 // Loads necessary component extensions.
215 // TODO(nona): Support dynamical unloading.
216 void LoadNecessaryComponentExtensions(StateImpl* state);
218 // Starts or stops the system input method framework as needed.
219 // (after list of enabled input methods has been updated).
220 // If state is active, active input method is updated.
221 void ReconfigureIMFramework(StateImpl* state);
223 // Record input method usage histograms.
224 void RecordInputMethodUsage(std::string input_method_id);
226 scoped_ptr<InputMethodDelegate> delegate_;
228 // The current UI session status.
229 UISessionState ui_session_;
231 // A list of objects that monitor the manager.
232 base::ObserverList<InputMethodManager::Observer> observers_;
233 base::ObserverList<CandidateWindowObserver> candidate_window_observers_;
235 scoped_refptr<StateImpl> state_;
237 // The candidate window. This will be deleted when the APP_TERMINATING
238 // message is sent.
239 scoped_ptr<CandidateWindowController> candidate_window_controller_;
241 // An object which provides miscellaneous input method utility functions. Note
242 // that |util_| is required to initialize |keyboard_|.
243 InputMethodUtil util_;
245 // An object which provides component extension ime management functions.
246 scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
248 // An object for switching XKB layouts and keyboard status like caps lock and
249 // auto-repeat interval.
250 scoped_ptr<ImeKeyboard> keyboard_;
253 // Whether load IME extensions.
254 bool enable_extension_loading_;
256 // The engine map from extension_id to an engine.
257 typedef std::map<std::string, InputMethodEngineInterface*> EngineMap;
258 typedef std::map<Profile*, EngineMap, ProfileCompare> ProfileEngineMap;
259 ProfileEngineMap engine_map_;
261 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
264 } // namespace input_method
265 } // namespace chromeos
267 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_