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