input_method_manager_impl.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
6#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
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/ibus_controller.h"
17#include "chrome/browser/chromeos/input_method/input_method_util.h"
18#include "chromeos/ime/ibus_daemon_controller.h"
19#include "chromeos/ime/input_method_manager.h"
20#include "chromeos/ime/input_method_whitelist.h"
21
22namespace chromeos {
23class ComponentExtensionIMEManager;
24class ComponentExtensionIMEManagerDelegate;
25class InputMethodEngineIBus;
26namespace input_method {
27class InputMethodDelegate;
28class XKeyboard;
29
30// The implementation of InputMethodManager.
31class InputMethodManagerImpl : public InputMethodManager,
32                               public CandidateWindowController::Observer,
33                               public IBusController::Observer,
34                               public IBusDaemonController::Observer {
35 public:
36  // Constructs an InputMethodManager instance. The client is responsible for
37  // calling |SetState| in response to relevant changes in browser state.
38  explicit InputMethodManagerImpl(scoped_ptr<InputMethodDelegate> delegate);
39  virtual ~InputMethodManagerImpl();
40
41  // Attach IBusController, CandidateWindowController, and XKeyboard objects
42  // to the InputMethodManagerImpl object. You don't have to call this function
43  // if you attach them yourself (e.g. in unit tests) using the protected
44  // setters.
45  void Init(base::SequencedTaskRunner* ui_task_runner);
46
47  // Receives notification of an InputMethodManager::State transition.
48  void SetState(State new_state);
49
50  // InputMethodManager override:
51  virtual void AddObserver(InputMethodManager::Observer* observer) OVERRIDE;
52  virtual void AddCandidateWindowObserver(
53      InputMethodManager::CandidateWindowObserver* observer) OVERRIDE;
54  virtual void RemoveObserver(InputMethodManager::Observer* observer) OVERRIDE;
55  virtual void RemoveCandidateWindowObserver(
56      InputMethodManager::CandidateWindowObserver* observer) OVERRIDE;
57  virtual scoped_ptr<InputMethodDescriptors>
58      GetSupportedInputMethods() const OVERRIDE;
59  virtual scoped_ptr<InputMethodDescriptors>
60      GetActiveInputMethods() const OVERRIDE;
61  virtual size_t GetNumActiveInputMethods() const OVERRIDE;
62  virtual void EnableLayouts(const std::string& language_code,
63                             const std::string& initial_layout) OVERRIDE;
64  virtual bool EnableInputMethods(
65      const std::vector<std::string>& new_active_input_method_ids) OVERRIDE;
66  virtual bool MigrateOldInputMethods(
67      std::vector<std::string>* input_method_ids) OVERRIDE;
68  virtual bool SetInputMethodConfig(
69      const std::string& section,
70      const std::string& config_name,
71      const InputMethodConfigValue& value) OVERRIDE;
72  virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE;
73  virtual void ActivateInputMethodProperty(const std::string& key) OVERRIDE;
74  virtual void AddInputMethodExtension(
75      const std::string& id,
76      const std::string& name,
77      const std::vector<std::string>& layouts,
78      const std::vector<std::string>& languages,
79      const GURL& options_page,
80      InputMethodEngine* instance) OVERRIDE;
81  virtual void RemoveInputMethodExtension(const std::string& id) OVERRIDE;
82  virtual void GetInputMethodExtensions(
83      InputMethodDescriptors* result) OVERRIDE;
84  virtual void SetEnabledExtensionImes(std::vector<std::string>* ids) OVERRIDE;
85  virtual bool SwitchToNextInputMethod() OVERRIDE;
86  virtual bool SwitchToPreviousInputMethod() OVERRIDE;
87  virtual bool SwitchInputMethod(const ui::Accelerator& accelerator) OVERRIDE;
88  virtual InputMethodDescriptor GetCurrentInputMethod() const OVERRIDE;
89  virtual InputMethodPropertyList
90      GetCurrentInputMethodProperties() const OVERRIDE;
91  virtual XKeyboard* GetXKeyboard() OVERRIDE;
92  virtual InputMethodUtil* GetInputMethodUtil() OVERRIDE;
93  virtual ComponentExtensionIMEManager*
94      GetComponentExtensionIMEManager() OVERRIDE;
95
96  // Sets |ibus_controller_|.
97  void SetIBusControllerForTesting(IBusController* ibus_controller);
98  // Sets |candidate_window_controller_|.
99  void SetCandidateWindowControllerForTesting(
100      CandidateWindowController* candidate_window_controller);
101  // Sets |xkeyboard_|.
102  void SetXKeyboardForTesting(XKeyboard* xkeyboard);
103  // Initialize |component_extension_manager_|.
104  void InitializeComponentExtensionForTesting(
105      scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
106
107 private:
108  // IBusController overrides:
109  virtual void PropertyChanged() OVERRIDE;
110
111  // IBusDaemonController overrides:
112  virtual void OnConnected() OVERRIDE;
113  virtual void OnDisconnected() OVERRIDE;
114
115
116  // CandidateWindowController::Observer overrides:
117  virtual void CandidateWindowOpened() OVERRIDE;
118  virtual void CandidateWindowClosed() OVERRIDE;
119
120  // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
121  // since they are not necessary to input a login password. Users are still
122  // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
123  // French).
124  void OnScreenLocked();
125
126  // Resumes the original state by activating input methods and/or changing the
127  // current input method as needed.
128  void OnScreenUnlocked();
129
130  // Returns true if |input_method_id| is in |active_input_method_ids_|.
131  bool InputMethodIsActivated(const std::string& input_method_id);
132
133  // Returns true if the given input method config value is a string list
134  // that only contains an input method ID of a keyboard layout.
135  bool ContainOnlyKeyboardLayout(const std::vector<std::string>& value);
136
137  // Returns true if the connection to ibus-daemon is established.
138  bool IsIBusConnectionAlive();
139
140  // Creates and initializes |candidate_window_controller_| if it hasn't been
141  // done.
142  void MaybeInitializeCandidateWindowController();
143
144  // If |current_input_method_id_| is not in |input_method_ids|, switch to
145  // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
146  // input_method_ids[N+1].
147  void SwitchToNextInputMethodInternal(
148      const std::vector<std::string>& input_method_ids,
149      const std::string& current_input_method_id);
150
151  // Change system input method.
152  // Returns true if the system input method is changed.
153  bool ChangeInputMethodInternal(const std::string& input_method_id,
154                                 bool show_message);
155
156  // Called when the ComponentExtensionIMEManagerDelegate is initialized.
157  void OnComponentExtensionInitialized(
158      scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
159  void InitializeComponentExtension();
160
161  // Loads necessary component extensions.
162  // TODO(nona): Support dynamical unloading.
163  void LoadNecessaryComponentExtensions();
164
165  scoped_ptr<InputMethodDelegate> delegate_;
166
167  // The current browser status.
168  State state_;
169
170  // A list of objects that monitor the manager.
171  ObserverList<InputMethodManager::Observer> observers_;
172  ObserverList<CandidateWindowObserver> candidate_window_observers_;
173
174  // The input method which was/is selected.
175  InputMethodDescriptor previous_input_method_;
176  InputMethodDescriptor current_input_method_;
177  // The active input method ids cache.
178  std::vector<std::string> active_input_method_ids_;
179
180  // The list of enabled extension IMEs.
181  std::vector<std::string> enabled_extension_imes_;
182
183  // For screen locker. When the screen is locked, |previous_input_method_|,
184  // |current_input_method_|, and |active_input_method_ids_| above are copied
185  // to these "saved" variables.
186  InputMethodDescriptor saved_previous_input_method_;
187  InputMethodDescriptor saved_current_input_method_;
188  std::vector<std::string> saved_active_input_method_ids_;
189
190  // Extra input methods that have been explicitly added to the menu, such as
191  // those created by extension.
192  std::map<std::string, InputMethodDescriptor> extra_input_methods_;
193  std::map<std::string, InputMethodEngineIBus*> extra_input_method_instances_;
194
195  // The IBus controller is used to control the input method status and
196  // allow callbacks when the input method status changes.
197  scoped_ptr<IBusController> ibus_controller_;
198
199  // The candidate window.  This will be deleted when the APP_TERMINATING
200  // message is sent.
201  scoped_ptr<CandidateWindowController> candidate_window_controller_;
202
203  // The object which can create an InputMethodDescriptor object.
204  InputMethodWhitelist whitelist_;
205
206  // An object which provides miscellaneous input method utility functions. Note
207  // that |util_| is required to initialize |xkeyboard_|.
208  InputMethodUtil util_;
209
210  // An object which provides component extension ime management functions.
211  scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
212
213  // An object for switching XKB layouts and keyboard status like caps lock and
214  // auto-repeat interval.
215  scoped_ptr<XKeyboard> xkeyboard_;
216
217  std::string pending_input_method_;
218
219  base::ThreadChecker thread_checker_;
220
221  base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
222
223  DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
224};
225
226}  // namespace input_method
227}  // namespace chromeos
228
229#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
230