Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_manager_impl.h
blob8c930d5a2438edd861a2ef454da711249e10398e
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 bool show_message,
211 bool notify_menu);
213 // Loads necessary component extensions.
214 // TODO(nona): Support dynamical unloading.
215 void LoadNecessaryComponentExtensions(StateImpl* state);
217 // Starts or stops the system input method framework as needed.
218 // (after list of enabled input methods has been updated).
219 // If state is active, active input method is updated.
220 void ReconfigureIMFramework(StateImpl* state);
222 // Record input method usage histograms.
223 void RecordInputMethodUsage(std::string input_method_id);
225 scoped_ptr<InputMethodDelegate> delegate_;
227 // The current UI session status.
228 UISessionState ui_session_;
230 // A list of objects that monitor the manager.
231 ObserverList<InputMethodManager::Observer> observers_;
232 ObserverList<CandidateWindowObserver> candidate_window_observers_;
234 scoped_refptr<StateImpl> state_;
236 // The candidate window. This will be deleted when the APP_TERMINATING
237 // message is sent.
238 scoped_ptr<CandidateWindowController> candidate_window_controller_;
240 // An object which provides miscellaneous input method utility functions. Note
241 // that |util_| is required to initialize |keyboard_|.
242 InputMethodUtil util_;
244 // An object which provides component extension ime management functions.
245 scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
247 // An object for switching XKB layouts and keyboard status like caps lock and
248 // auto-repeat interval.
249 scoped_ptr<ImeKeyboard> keyboard_;
252 // Whether load IME extensions.
253 bool enable_extension_loading_;
255 // The engine map from extension_id to an engine.
256 typedef std::map<std::string, InputMethodEngineInterface*> EngineMap;
257 EngineMap engine_map_;
259 // The map from input method id to the input method stat id.
260 // The stat id has the format: <category#><first char after prefix><index>
261 // For example, Chinese Simplified Pinyin IME has the stat id:
262 // 2,'p',1 -> 211201
263 // 2 means it in INPUT_METHOD_CATEGORY_ZH;
264 // 112 means the first char after prefix is 'p' of 'pinyin';
265 // 01 means it's the second pinyin as the first pinyin is for Traditional
266 // Chinese Pinyin IME. Note "zh-hant-t-i0-pinyin" < "zh-t-i0-pinyin".
267 std::map<std::string, int> stat_id_map_;
269 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
272 } // namespace input_method
273 } // namespace chromeos
275 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_