Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_manager_impl.h
blob84299b11bd554f2c802dd9fadb030f21f84d05cf
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 virtual scoped_refptr<InputMethodManager::State> Clone() const override;
74 virtual void AddInputMethodExtension(
75 const std::string& extension_id,
76 const InputMethodDescriptors& descriptors,
77 InputMethodEngineInterface* instance) override;
78 virtual void RemoveInputMethodExtension(
79 const std::string& extension_id) override;
80 virtual void ChangeInputMethod(const std::string& input_method_id,
81 bool show_message) override;
82 virtual bool EnableInputMethod(
83 const std::string& new_active_input_method_id) override;
84 virtual void EnableLoginLayouts(
85 const std::string& language_code,
86 const std::vector<std::string>& initial_layouts) override;
87 virtual void EnableLockScreenLayouts() override;
88 virtual void GetInputMethodExtensions(
89 InputMethodDescriptors* result) override;
90 virtual scoped_ptr<InputMethodDescriptors> GetActiveInputMethods()
91 const override;
92 virtual const std::vector<std::string>& GetActiveInputMethodIds()
93 const override;
94 virtual const InputMethodDescriptor* GetInputMethodFromId(
95 const std::string& input_method_id) const override;
96 virtual size_t GetNumActiveInputMethods() const override;
97 virtual void SetEnabledExtensionImes(
98 std::vector<std::string>* ids) override;
99 virtual void SetInputMethodLoginDefault() override;
100 virtual void SetInputMethodLoginDefaultFromVPD(
101 const std::string& locale,
102 const std::string& layout) override;
103 virtual bool CanCycleInputMethod() override;
104 virtual void SwitchToNextInputMethod() override;
105 virtual void SwitchToPreviousInputMethod() override;
106 virtual bool CanSwitchInputMethod(
107 const ui::Accelerator& accelerator) override;
108 virtual void SwitchInputMethod(const ui::Accelerator& accelerator) override;
109 virtual InputMethodDescriptor GetCurrentInputMethod() const override;
110 virtual bool ReplaceEnabledInputMethods(
111 const std::vector<std::string>& new_active_input_method_ids) override;
113 // ------------------------- Data members.
114 Profile* const profile;
116 // The input method which was/is selected.
117 InputMethodDescriptor previous_input_method;
118 InputMethodDescriptor current_input_method;
120 // The active input method ids cache.
121 std::vector<std::string> active_input_method_ids;
123 // The pending input method id for delayed 3rd party IME enabling.
124 std::string pending_input_method_id;
126 // The list of enabled extension IMEs.
127 std::vector<std::string> enabled_extension_imes;
129 // Extra input methods that have been explicitly added to the menu, such as
130 // those created by extension.
131 std::map<std::string, InputMethodDescriptor> extra_input_methods;
133 InputMethodManagerImpl* const manager_;
135 protected:
136 friend base::RefCounted<chromeos::input_method::InputMethodManager::State>;
137 virtual ~StateImpl();
140 // Constructs an InputMethodManager instance. The client is responsible for
141 // calling |SetUISessionState| in response to relevant changes in browser
142 // state.
143 InputMethodManagerImpl(scoped_ptr<InputMethodDelegate> delegate,
144 bool enable_extension_loading);
145 virtual ~InputMethodManagerImpl();
147 // Receives notification of an InputMethodManager::UISessionState transition.
148 void SetUISessionState(UISessionState new_ui_session);
150 // InputMethodManager override:
151 virtual UISessionState GetUISessionState() override;
152 virtual void AddObserver(InputMethodManager::Observer* observer) override;
153 virtual void AddCandidateWindowObserver(
154 InputMethodManager::CandidateWindowObserver* observer) override;
155 virtual void RemoveObserver(InputMethodManager::Observer* observer) override;
156 virtual void RemoveCandidateWindowObserver(
157 InputMethodManager::CandidateWindowObserver* observer) override;
158 virtual scoped_ptr<InputMethodDescriptors>
159 GetSupportedInputMethods() const override;
160 virtual void ActivateInputMethodMenuItem(const std::string& key) override;
161 virtual bool IsISOLevel5ShiftUsedByCurrentInputMethod() const override;
162 virtual bool IsAltGrUsedByCurrentInputMethod() const override;
164 virtual ImeKeyboard* GetImeKeyboard() override;
165 virtual InputMethodUtil* GetInputMethodUtil() override;
166 virtual ComponentExtensionIMEManager*
167 GetComponentExtensionIMEManager() override;
168 virtual bool IsLoginKeyboard(const std::string& layout) const override;
170 virtual bool MigrateInputMethods(
171 std::vector<std::string>* input_method_ids) override;
173 virtual scoped_refptr<InputMethodManager::State> CreateNewState(
174 Profile* profile) override;
176 virtual scoped_refptr<InputMethodManager::State> GetActiveIMEState() override;
177 virtual void SetState(
178 scoped_refptr<InputMethodManager::State> state) override;
180 // Sets |candidate_window_controller_|.
181 void SetCandidateWindowControllerForTesting(
182 CandidateWindowController* candidate_window_controller);
183 // Sets |keyboard_|.
184 void SetImeKeyboardForTesting(ImeKeyboard* keyboard);
185 // Initialize |component_extension_manager_|.
186 void InitializeComponentExtensionForTesting(
187 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
189 private:
190 friend class InputMethodManagerImplTest;
192 // CandidateWindowController::Observer overrides:
193 virtual void CandidateClicked(int index) override;
194 virtual void CandidateWindowOpened() override;
195 virtual void CandidateWindowClosed() override;
197 // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
198 // since they are not necessary to input a login password. Users are still
199 // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
200 // French).
201 void OnScreenLocked();
203 // Resumes the original state by activating input methods and/or changing the
204 // current input method as needed.
205 void OnScreenUnlocked();
207 // Returns true if the given input method config value is a string list
208 // that only contains an input method ID of a keyboard layout.
209 bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
211 // Creates and initializes |candidate_window_controller_| if it hasn't been
212 // done.
213 void MaybeInitializeCandidateWindowController();
215 // Returns Input Method that best matches given id.
216 const InputMethodDescriptor* LookupInputMethod(
217 const std::string& input_method_id,
218 StateImpl* state);
220 // Change system input method.
221 void ChangeInputMethodInternal(const InputMethodDescriptor& descriptor,
222 bool show_message,
223 bool notify_menu);
225 // Loads necessary component extensions.
226 // TODO(nona): Support dynamical unloading.
227 void LoadNecessaryComponentExtensions(StateImpl* state);
229 // Starts or stops the system input method framework as needed.
230 // (after list of enabled input methods has been updated).
231 // If state is active, active input method is updated.
232 void ReconfigureIMFramework(StateImpl* state);
234 // Record input method usage histograms.
235 void RecordInputMethodUsage(std::string input_method_id);
237 scoped_ptr<InputMethodDelegate> delegate_;
239 // The current UI session status.
240 UISessionState ui_session_;
242 // A list of objects that monitor the manager.
243 ObserverList<InputMethodManager::Observer> observers_;
244 ObserverList<CandidateWindowObserver> candidate_window_observers_;
246 scoped_refptr<StateImpl> state_;
248 // The candidate window. This will be deleted when the APP_TERMINATING
249 // message is sent.
250 scoped_ptr<CandidateWindowController> candidate_window_controller_;
252 // An object which provides miscellaneous input method utility functions. Note
253 // that |util_| is required to initialize |keyboard_|.
254 InputMethodUtil util_;
256 // An object which provides component extension ime management functions.
257 scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
259 // An object for switching XKB layouts and keyboard status like caps lock and
260 // auto-repeat interval.
261 scoped_ptr<ImeKeyboard> keyboard_;
264 // Whether load IME extensions.
265 bool enable_extension_loading_;
267 // The engine map from extension_id to an engine.
268 typedef std::map<std::string, InputMethodEngineInterface*> EngineMap;
269 EngineMap engine_map_;
271 // The map from input method id to the input method stat id.
272 // The stat id has the format: <category#><first char after prefix><index>
273 // For example, Chinese Simplified Pinyin IME has the stat id:
274 // 2,'p',1 -> 211201
275 // 2 means it in INPUT_METHOD_CATEGORY_ZH;
276 // 112 means the first char after prefix is 'p' of 'pinyin';
277 // 01 means it's the second pinyin as the first pinyin is for Traditional
278 // Chinese Pinyin IME. Note "zh-hant-t-i0-pinyin" < "zh-t-i0-pinyin".
279 std::map<std::string, int> stat_id_map_;
281 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
284 } // namespace input_method
285 } // namespace chromeos
287 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_