889a3f2e8c2a72260652a3896ef5cced77d102ed
[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 ActivateInputMethodProperty(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 InputMethodPropertyList
85       GetCurrentInputMethodProperties() const OVERRIDE;
86   virtual void SetCurrentInputMethodProperties(
87       const InputMethodPropertyList& property_list) OVERRIDE;
88
89   virtual XKeyboard* GetXKeyboard() OVERRIDE;
90   virtual InputMethodUtil* GetInputMethodUtil() OVERRIDE;
91   virtual ComponentExtensionIMEManager*
92       GetComponentExtensionIMEManager() OVERRIDE;
93   virtual bool IsLoginKeyboard(const std::string& layout) const OVERRIDE;
94
95   // Sets |candidate_window_controller_|.
96   void SetCandidateWindowControllerForTesting(
97       CandidateWindowController* candidate_window_controller);
98   // Sets |xkeyboard_|.
99   void SetXKeyboardForTesting(XKeyboard* xkeyboard);
100   // Initialize |component_extension_manager_|.
101   void InitializeComponentExtensionForTesting(
102       scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
103
104  private:
105   // Notifies observers that the property list is updated.
106   void PropertyChanged();
107
108   // CandidateWindowController::Observer overrides:
109   virtual void CandidateClicked(int index) OVERRIDE;
110   virtual void CandidateWindowOpened() OVERRIDE;
111   virtual void CandidateWindowClosed() OVERRIDE;
112
113   // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
114   // since they are not necessary to input a login password. Users are still
115   // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
116   // French).
117   void OnScreenLocked();
118
119   // Resumes the original state by activating input methods and/or changing the
120   // current input method as needed.
121   void OnScreenUnlocked();
122
123   // Returns true if |input_method_id| is in |active_input_method_ids_|.
124   bool InputMethodIsActivated(const std::string& input_method_id);
125
126   // Returns true if the given input method config value is a string list
127   // that only contains an input method ID of a keyboard layout.
128   bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
129
130   // Creates and initializes |candidate_window_controller_| if it hasn't been
131   // done.
132   void MaybeInitializeCandidateWindowController();
133
134   // If |current_input_method_id_| is not in |input_method_ids|, switch to
135   // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
136   // input_method_ids[N+1].
137   void SwitchToNextInputMethodInternal(
138       const std::vector<std::string>& input_method_ids,
139       const std::string& current_input_method_id);
140
141   // Change system input method.
142   // Returns true if the system input method is changed.
143   bool ChangeInputMethodInternal(const std::string& input_method_id,
144                                  bool show_message);
145
146   // Called when the ComponentExtensionIMEManagerDelegate is initialized.
147   void OnComponentExtensionInitialized(
148       scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
149   void InitializeComponentExtension();
150
151   // Loads necessary component extensions.
152   // TODO(nona): Support dynamical unloading.
153   void LoadNecessaryComponentExtensions();
154
155   // Adds new input method to given list if possible
156   bool EnableInputMethodImpl(
157       const std::string& input_method_id,
158       std::vector<std::string>* new_active_input_method_ids) const;
159
160   // Starts or stops the system input method framework as needed.
161   // (after list of enabled input methods has been updated)
162   void ReconfigureIMFramework();
163
164   scoped_ptr<InputMethodDelegate> delegate_;
165
166   // The current browser status.
167   State state_;
168
169   // A list of objects that monitor the manager.
170   ObserverList<InputMethodManager::Observer> observers_;
171   ObserverList<CandidateWindowObserver> candidate_window_observers_;
172
173   // The input method which was/is selected.
174   InputMethodDescriptor previous_input_method_;
175   InputMethodDescriptor current_input_method_;
176   // The active input method ids cache.
177   std::vector<std::string> active_input_method_ids_;
178
179   // The list of enabled extension IMEs.
180   std::vector<std::string> enabled_extension_imes_;
181
182   // For screen locker. When the screen is locked, |previous_input_method_|,
183   // |current_input_method_|, and |active_input_method_ids_| above are copied
184   // to these "saved" variables.
185   InputMethodDescriptor saved_previous_input_method_;
186   InputMethodDescriptor saved_current_input_method_;
187   std::vector<std::string> saved_active_input_method_ids_;
188
189   // Extra input methods that have been explicitly added to the menu, such as
190   // those created by extension.
191   std::map<std::string, InputMethodDescriptor> extra_input_methods_;
192
193   // Property list of the input method.  This is set by extension IMEs.
194   InputMethodPropertyList property_list_;
195
196   // The candidate window.  This will be deleted when the APP_TERMINATING
197   // message is sent.
198   scoped_ptr<CandidateWindowController> candidate_window_controller_;
199
200   // The object which can create an InputMethodDescriptor object.
201   InputMethodWhitelist whitelist_;
202
203   // An object which provides miscellaneous input method utility functions. Note
204   // that |util_| is required to initialize |xkeyboard_|.
205   InputMethodUtil util_;
206
207   // An object which provides component extension ime management functions.
208   scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
209
210   // An object for switching XKB layouts and keyboard status like caps lock and
211   // auto-repeat interval.
212   scoped_ptr<XKeyboard> xkeyboard_;
213
214   std::string pending_input_method_;
215
216   base::ThreadChecker thread_checker_;
217
218   base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
219
220   DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
221 };
222
223 }  // namespace input_method
224 }  // namespace chromeos
225
226 #endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_