input_method_manager_impl.h revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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 const std::vector<std::string>& GetActiveInputMethodIds() const
62      OVERRIDE;
63  virtual size_t GetNumActiveInputMethods() const OVERRIDE;
64  virtual void EnableLayouts(const std::string& language_code,
65                             const std::string& initial_layout) OVERRIDE;
66  virtual bool EnableInputMethods(
67      const std::vector<std::string>& new_active_input_method_ids) OVERRIDE;
68  virtual bool EnableInputMethod(const std::string& new_active_input_method_id)
69      OVERRIDE;
70  virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE;
71  virtual void ActivateInputMethodProperty(const std::string& key) OVERRIDE;
72  virtual void AddInputMethodExtension(
73      const std::string& id,
74      const std::string& name,
75      const std::vector<std::string>& layouts,
76      const std::vector<std::string>& languages,
77      const GURL& options_page,
78      InputMethodEngine* instance) OVERRIDE;
79  virtual void RemoveInputMethodExtension(const std::string& id) OVERRIDE;
80  virtual void GetInputMethodExtensions(
81      InputMethodDescriptors* result) OVERRIDE;
82  virtual void SetEnabledExtensionImes(std::vector<std::string>* ids) OVERRIDE;
83  virtual void SetInputMethodDefault() OVERRIDE;
84  virtual bool SwitchToNextInputMethod() OVERRIDE;
85  virtual bool SwitchToPreviousInputMethod(
86      const ui::Accelerator& accelerator) 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  virtual bool IsLoginKeyboard(const std::string& layout) const OVERRIDE;
96
97  // Sets |ibus_controller_|.
98  void SetIBusControllerForTesting(IBusController* ibus_controller);
99  // Sets |candidate_window_controller_|.
100  void SetCandidateWindowControllerForTesting(
101      CandidateWindowController* candidate_window_controller);
102  // Sets |xkeyboard_|.
103  void SetXKeyboardForTesting(XKeyboard* xkeyboard);
104  // Initialize |component_extension_manager_|.
105  void InitializeComponentExtensionForTesting(
106      scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
107
108 private:
109  // IBusController overrides:
110  virtual void PropertyChanged() OVERRIDE;
111
112  // IBusDaemonController overrides:
113  virtual void OnConnected() OVERRIDE;
114  virtual void OnDisconnected() OVERRIDE;
115
116
117  // CandidateWindowController::Observer overrides:
118  virtual void CandidateWindowOpened() OVERRIDE;
119  virtual void CandidateWindowClosed() OVERRIDE;
120
121  // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
122  // since they are not necessary to input a login password. Users are still
123  // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
124  // French).
125  void OnScreenLocked();
126
127  // Resumes the original state by activating input methods and/or changing the
128  // current input method as needed.
129  void OnScreenUnlocked();
130
131  // Returns true if |input_method_id| is in |active_input_method_ids_|.
132  bool InputMethodIsActivated(const std::string& input_method_id);
133
134  // Returns true if the given input method config value is a string list
135  // that only contains an input method ID of a keyboard layout.
136  bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
137
138  // Returns true if the connection to ibus-daemon is established.
139  bool IsIBusConnectionAlive();
140
141  // Creates and initializes |candidate_window_controller_| if it hasn't been
142  // done.
143  void MaybeInitializeCandidateWindowController();
144
145  // If |current_input_method_id_| is not in |input_method_ids|, switch to
146  // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
147  // input_method_ids[N+1].
148  void SwitchToNextInputMethodInternal(
149      const std::vector<std::string>& input_method_ids,
150      const std::string& current_input_method_id);
151
152  // Change system input method.
153  // Returns true if the system input method is changed.
154  bool ChangeInputMethodInternal(const std::string& input_method_id,
155                                 bool show_message);
156
157  // Called when the ComponentExtensionIMEManagerDelegate is initialized.
158  void OnComponentExtensionInitialized(
159      scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
160  void InitializeComponentExtension();
161
162  // Loads necessary component extensions.
163  // TODO(nona): Support dynamical unloading.
164  void LoadNecessaryComponentExtensions();
165
166  // Adds new input method to given list if possible
167  bool EnableInputMethodImpl(
168      const std::string& input_method_id,
169      std::vector<std::string>& new_active_input_method_ids) const;
170
171  // Starts or stops the system input method framework as needed.
172  // (after list of enabled input methods has been updated)
173  void ReconfigureIMFramework();
174
175  scoped_ptr<InputMethodDelegate> delegate_;
176
177  // The current browser status.
178  State state_;
179
180  // A list of objects that monitor the manager.
181  ObserverList<InputMethodManager::Observer> observers_;
182  ObserverList<CandidateWindowObserver> candidate_window_observers_;
183
184  // The input method which was/is selected.
185  InputMethodDescriptor previous_input_method_;
186  InputMethodDescriptor current_input_method_;
187  // The active input method ids cache.
188  std::vector<std::string> active_input_method_ids_;
189
190  // The list of enabled extension IMEs.
191  std::vector<std::string> enabled_extension_imes_;
192
193  // For screen locker. When the screen is locked, |previous_input_method_|,
194  // |current_input_method_|, and |active_input_method_ids_| above are copied
195  // to these "saved" variables.
196  InputMethodDescriptor saved_previous_input_method_;
197  InputMethodDescriptor saved_current_input_method_;
198  std::vector<std::string> saved_active_input_method_ids_;
199
200  // Extra input methods that have been explicitly added to the menu, such as
201  // those created by extension.
202  std::map<std::string, InputMethodDescriptor> extra_input_methods_;
203  std::map<std::string, InputMethodEngineIBus*> extra_input_method_instances_;
204
205  // The IBus controller is used to control the input method status and
206  // allow callbacks when the input method status changes.
207  scoped_ptr<IBusController> ibus_controller_;
208
209  // The candidate window.  This will be deleted when the APP_TERMINATING
210  // message is sent.
211  scoped_ptr<CandidateWindowController> candidate_window_controller_;
212
213  // The object which can create an InputMethodDescriptor object.
214  InputMethodWhitelist whitelist_;
215
216  // An object which provides miscellaneous input method utility functions. Note
217  // that |util_| is required to initialize |xkeyboard_|.
218  InputMethodUtil util_;
219
220  // An object which provides component extension ime management functions.
221  scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
222
223  // An object for switching XKB layouts and keyboard status like caps lock and
224  // auto-repeat interval.
225  scoped_ptr<XKeyboard> xkeyboard_;
226
227  std::string pending_input_method_;
228
229  base::ThreadChecker thread_checker_;
230
231  base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
232
233  DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
234};
235
236}  // namespace input_method
237}  // namespace chromeos
238
239#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
240