23ec79c9ed4e4739c52f29d5ad65ae91d2ffd0d7
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_manager_impl.h
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/input_method_util.h"
17 #include "chromeos/ime/input_method_manager.h"
18 #include "chromeos/ime/input_method_whitelist.h"
19
20 namespace chromeos {
21 class ComponentExtensionIMEManager;
22 class ComponentExtensionIMEManagerDelegate;
23 class InputMethodEngine;
24 namespace input_method {
25 class InputMethodDelegate;
26 class XKeyboard;
27
28 // The implementation of InputMethodManager.
29 class InputMethodManagerImpl : public InputMethodManager,
30                                public CandidateWindowController::Observer {
31  public:
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();
36
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);
42
43   // Receives notification of an InputMethodManager::State transition.
44   void SetState(State new_state);
45
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
58       OVERRIDE;
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)
68       OVERRIDE;
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;
86
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;
92
93   virtual bool MigrateXkbInputMethods(
94       std::vector<std::string>* input_method_ids) OVERRIDE;
95
96   // Sets |candidate_window_controller_|.
97   void SetCandidateWindowControllerForTesting(
98       CandidateWindowController* candidate_window_controller);
99   // Sets |xkeyboard_|.
100   void SetXKeyboardForTesting(XKeyboard* xkeyboard);
101   // Initialize |component_extension_manager_|.
102   void InitializeComponentExtensionForTesting(
103       scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
104
105  private:
106   // CandidateWindowController::Observer overrides:
107   virtual void CandidateClicked(int index) OVERRIDE;
108   virtual void CandidateWindowOpened() OVERRIDE;
109   virtual void CandidateWindowClosed() OVERRIDE;
110
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,
114   // French).
115   void OnScreenLocked();
116
117   // Resumes the original state by activating input methods and/or changing the
118   // current input method as needed.
119   void OnScreenUnlocked();
120
121   // Returns true if |input_method_id| is in |active_input_method_ids_|.
122   bool InputMethodIsActivated(const std::string& input_method_id);
123
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);
127
128   // Creates and initializes |candidate_window_controller_| if it hasn't been
129   // done.
130   void MaybeInitializeCandidateWindowController();
131
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);
138
139   // Change system input method.
140   // Returns true if the system input method is changed.
141   bool ChangeInputMethodInternal(const std::string& input_method_id,
142                                  bool show_message);
143
144   // Called when the ComponentExtensionIMEManagerDelegate is initialized.
145   void OnComponentExtensionInitialized(
146       scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
147   void InitializeComponentExtension();
148
149   // Loads necessary component extensions.
150   // TODO(nona): Support dynamical unloading.
151   void LoadNecessaryComponentExtensions();
152
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;
157
158   // Starts or stops the system input method framework as needed.
159   // (after list of enabled input methods has been updated)
160   void ReconfigureIMFramework();
161
162   scoped_ptr<InputMethodDelegate> delegate_;
163
164   // The current browser status.
165   State state_;
166
167   // A list of objects that monitor the manager.
168   ObserverList<InputMethodManager::Observer> observers_;
169   ObserverList<CandidateWindowObserver> candidate_window_observers_;
170
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_;
176
177   // The list of enabled extension IMEs.
178   std::vector<std::string> enabled_extension_imes_;
179
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_;
186
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_;
190
191   // The candidate window.  This will be deleted when the APP_TERMINATING
192   // message is sent.
193   scoped_ptr<CandidateWindowController> candidate_window_controller_;
194
195   // The object which can create an InputMethodDescriptor object.
196   InputMethodWhitelist whitelist_;
197
198   // An object which provides miscellaneous input method utility functions. Note
199   // that |util_| is required to initialize |xkeyboard_|.
200   InputMethodUtil util_;
201
202   // An object which provides component extension ime management functions.
203   scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
204
205   // An object for switching XKB layouts and keyboard status like caps lock and
206   // auto-repeat interval.
207   scoped_ptr<XKeyboard> xkeyboard_;
208
209   std::string pending_input_method_;
210
211   base::ThreadChecker thread_checker_;
212
213   base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
214
215   DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
216 };
217
218 }  // namespace input_method
219 }  // namespace chromeos
220
221 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_