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