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_
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 "chromeos/ime/input_method_manager.h"
18 #include "chromeos/ime/input_method_whitelist.h"
21 class ComponentExtensionIMEManager;
22 class ComponentExtensionIMEManagerDelegate;
23 class InputMethodEngine;
24 namespace input_method {
25 class InputMethodDelegate;
28 // The implementation of InputMethodManager.
29 class InputMethodManagerImpl : public InputMethodManager,
30 public CandidateWindowController::Observer {
32 // Constructs an InputMethodManager instance. The client is responsible for
33 // calling |SetState| in response to relevant changes in browser state.
34 explicit InputMethodManagerImpl(scoped_ptr<InputMethodDelegate> delegate);
35 virtual ~InputMethodManagerImpl();
37 // Attach CandidateWindowController, and XKeyboard objects to the
38 // InputMethodManagerImpl object. You don't have to call this
39 // function if you attach them yourself (e.g. in unit tests) using
40 // the protected setters.
41 void Init(base::SequencedTaskRunner* ui_task_runner);
43 // Receives notification of an InputMethodManager::State transition.
44 void SetState(State new_state);
46 // InputMethodManager override:
47 virtual void AddObserver(InputMethodManager::Observer* observer) OVERRIDE;
48 virtual void AddCandidateWindowObserver(
49 InputMethodManager::CandidateWindowObserver* observer) OVERRIDE;
50 virtual void RemoveObserver(InputMethodManager::Observer* observer) OVERRIDE;
51 virtual void RemoveCandidateWindowObserver(
52 InputMethodManager::CandidateWindowObserver* observer) OVERRIDE;
53 virtual scoped_ptr<InputMethodDescriptors>
54 GetSupportedInputMethods() const OVERRIDE;
55 virtual scoped_ptr<InputMethodDescriptors>
56 GetActiveInputMethods() const OVERRIDE;
57 virtual const std::vector<std::string>& GetActiveInputMethodIds() const
59 virtual size_t GetNumActiveInputMethods() const OVERRIDE;
60 virtual const InputMethodDescriptor* GetInputMethodFromId(
61 const std::string& input_method_id) const OVERRIDE;
62 virtual void EnableLoginLayouts(
63 const std::string& language_code,
64 const std::vector<std::string>& initial_layouts) OVERRIDE;
65 virtual bool ReplaceEnabledInputMethods(
66 const std::vector<std::string>& new_active_input_method_ids) OVERRIDE;
67 virtual bool EnableInputMethod(const std::string& new_active_input_method_id)
69 virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE;
70 virtual void ActivateInputMethodMenuItem(const std::string& key) OVERRIDE;
71 virtual void AddInputMethodExtension(
72 const std::string& id,
73 InputMethodEngineInterface* instance) OVERRIDE;
74 virtual void RemoveInputMethodExtension(const std::string& id) OVERRIDE;
75 virtual void GetInputMethodExtensions(
76 InputMethodDescriptors* result) OVERRIDE;
77 virtual void SetEnabledExtensionImes(std::vector<std::string>* ids) OVERRIDE;
78 virtual void SetInputMethodLoginDefault() OVERRIDE;
79 virtual bool SwitchToNextInputMethod() OVERRIDE;
80 virtual bool SwitchToPreviousInputMethod(
81 const ui::Accelerator& accelerator) OVERRIDE;
82 virtual bool SwitchInputMethod(const ui::Accelerator& accelerator) OVERRIDE;
83 virtual InputMethodDescriptor GetCurrentInputMethod() const OVERRIDE;
84 virtual bool IsISOLevel5ShiftUsedByCurrentInputMethod() const OVERRIDE;
85 virtual bool IsAltGrUsedByCurrentInputMethod() const OVERRIDE;
87 virtual XKeyboard* GetXKeyboard() OVERRIDE;
88 virtual InputMethodUtil* GetInputMethodUtil() OVERRIDE;
89 virtual ComponentExtensionIMEManager*
90 GetComponentExtensionIMEManager() OVERRIDE;
91 virtual bool IsLoginKeyboard(const std::string& layout) const OVERRIDE;
93 virtual bool MigrateXkbInputMethods(
94 std::vector<std::string>* input_method_ids) OVERRIDE;
96 // Sets |candidate_window_controller_|.
97 void SetCandidateWindowControllerForTesting(
98 CandidateWindowController* candidate_window_controller);
100 void SetXKeyboardForTesting(XKeyboard* xkeyboard);
101 // Initialize |component_extension_manager_|.
102 void InitializeComponentExtensionForTesting(
103 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
106 // CandidateWindowController::Observer overrides:
107 virtual void CandidateClicked(int index) OVERRIDE;
108 virtual void CandidateWindowOpened() OVERRIDE;
109 virtual void CandidateWindowClosed() OVERRIDE;
111 // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
112 // since they are not necessary to input a login password. Users are still
113 // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
115 void OnScreenLocked();
117 // Resumes the original state by activating input methods and/or changing the
118 // current input method as needed.
119 void OnScreenUnlocked();
121 // Returns true if |input_method_id| is in |active_input_method_ids_|.
122 bool InputMethodIsActivated(const std::string& input_method_id);
124 // Returns true if the given input method config value is a string list
125 // that only contains an input method ID of a keyboard layout.
126 bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
128 // Creates and initializes |candidate_window_controller_| if it hasn't been
130 void MaybeInitializeCandidateWindowController();
132 // If |current_input_method_id_| is not in |input_method_ids|, switch to
133 // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
134 // input_method_ids[N+1].
135 void SwitchToNextInputMethodInternal(
136 const std::vector<std::string>& input_method_ids,
137 const std::string& current_input_method_id);
139 // Change system input method.
140 // Returns true if the system input method is changed.
141 bool ChangeInputMethodInternal(const std::string& input_method_id,
144 // Called when the ComponentExtensionIMEManagerDelegate is initialized.
145 void OnComponentExtensionInitialized(
146 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
147 void InitializeComponentExtension();
149 // Loads necessary component extensions.
150 // TODO(nona): Support dynamical unloading.
151 void LoadNecessaryComponentExtensions();
153 // Adds new input method to given list if possible
154 bool EnableInputMethodImpl(
155 const std::string& input_method_id,
156 std::vector<std::string>* new_active_input_method_ids) const;
158 // Starts or stops the system input method framework as needed.
159 // (after list of enabled input methods has been updated)
160 void ReconfigureIMFramework();
162 scoped_ptr<InputMethodDelegate> delegate_;
164 // The current browser status.
167 // A list of objects that monitor the manager.
168 ObserverList<InputMethodManager::Observer> observers_;
169 ObserverList<CandidateWindowObserver> candidate_window_observers_;
171 // The input method which was/is selected.
172 InputMethodDescriptor previous_input_method_;
173 InputMethodDescriptor current_input_method_;
174 // The active input method ids cache.
175 std::vector<std::string> active_input_method_ids_;
177 // The list of enabled extension IMEs.
178 std::vector<std::string> enabled_extension_imes_;
180 // For screen locker. When the screen is locked, |previous_input_method_|,
181 // |current_input_method_|, and |active_input_method_ids_| above are copied
182 // to these "saved" variables.
183 InputMethodDescriptor saved_previous_input_method_;
184 InputMethodDescriptor saved_current_input_method_;
185 std::vector<std::string> saved_active_input_method_ids_;
187 // Extra input methods that have been explicitly added to the menu, such as
188 // those created by extension.
189 std::map<std::string, InputMethodDescriptor> extra_input_methods_;
191 // The candidate window. This will be deleted when the APP_TERMINATING
193 scoped_ptr<CandidateWindowController> candidate_window_controller_;
195 // The object which can create an InputMethodDescriptor object.
196 InputMethodWhitelist whitelist_;
198 // An object which provides miscellaneous input method utility functions. Note
199 // that |util_| is required to initialize |xkeyboard_|.
200 InputMethodUtil util_;
202 // An object which provides component extension ime management functions.
203 scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
205 // An object for switching XKB layouts and keyboard status like caps lock and
206 // auto-repeat interval.
207 scoped_ptr<XKeyboard> xkeyboard_;
209 std::string pending_input_method_;
211 base::ThreadChecker thread_checker_;
213 base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
215 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
218 } // namespace input_method
219 } // namespace chromeos
221 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_