Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_manager_impl.h
blobb3b2d6895ff849d3618e4c152c14d3eca061db06
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.
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_
8 #include <map>
9 #include <string>
10 #include <vector>
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"
20 namespace chromeos {
21 class ComponentExtensionIMEManager;
22 class ComponentExtensionIMEManagerDelegate;
23 class InputMethodEngine;
24 namespace input_method {
25 class InputMethodDelegate;
26 class XKeyboard;
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();
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);
43 // Receives notification of an InputMethodManager::State transition.
44 void SetState(State new_state);
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 EnableLayouts(const std::string& language_code,
63 const std::string& initial_layout) OVERRIDE;
64 virtual bool EnableInputMethods(
65 const std::vector<std::string>& new_active_input_method_ids) OVERRIDE;
66 virtual bool EnableInputMethod(const std::string& new_active_input_method_id)
67 OVERRIDE;
68 virtual void ChangeInputMethod(const std::string& input_method_id) OVERRIDE;
69 virtual void ActivateInputMethodProperty(const std::string& key) OVERRIDE;
70 virtual void AddInputMethodExtension(
71 const std::string& id,
72 InputMethodEngineInterface* instance) OVERRIDE;
73 virtual void RemoveInputMethodExtension(const std::string& id) OVERRIDE;
74 virtual void GetInputMethodExtensions(
75 InputMethodDescriptors* result) OVERRIDE;
76 virtual void SetEnabledExtensionImes(std::vector<std::string>* ids) OVERRIDE;
77 virtual void SetInputMethodDefault() OVERRIDE;
78 virtual bool SwitchToNextInputMethod() OVERRIDE;
79 virtual bool SwitchToPreviousInputMethod(
80 const ui::Accelerator& accelerator) OVERRIDE;
81 virtual bool SwitchInputMethod(const ui::Accelerator& accelerator) OVERRIDE;
82 virtual InputMethodDescriptor GetCurrentInputMethod() const OVERRIDE;
83 virtual InputMethodPropertyList
84 GetCurrentInputMethodProperties() const OVERRIDE;
85 virtual void SetCurrentInputMethodProperties(
86 const InputMethodPropertyList& property_list) OVERRIDE;
88 virtual XKeyboard* GetXKeyboard() OVERRIDE;
89 virtual InputMethodUtil* GetInputMethodUtil() OVERRIDE;
90 virtual ComponentExtensionIMEManager*
91 GetComponentExtensionIMEManager() OVERRIDE;
92 virtual bool IsLoginKeyboard(const std::string& layout) const OVERRIDE;
94 // Sets |candidate_window_controller_|.
95 void SetCandidateWindowControllerForTesting(
96 CandidateWindowController* candidate_window_controller);
97 // Sets |xkeyboard_|.
98 void SetXKeyboardForTesting(XKeyboard* xkeyboard);
99 // Initialize |component_extension_manager_|.
100 void InitializeComponentExtensionForTesting(
101 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
103 private:
104 // Notifies observers that the property list is updated.
105 void PropertyChanged();
107 // CandidateWindowController::Observer overrides:
108 virtual void CandidateClicked(int index) OVERRIDE;
109 virtual void CandidateWindowOpened() OVERRIDE;
110 virtual void CandidateWindowClosed() OVERRIDE;
112 // Temporarily deactivates all input methods (e.g. Chinese, Japanese, Arabic)
113 // since they are not necessary to input a login password. Users are still
114 // able to use/switch active keyboard layouts (e.g. US qwerty, US dvorak,
115 // French).
116 void OnScreenLocked();
118 // Resumes the original state by activating input methods and/or changing the
119 // current input method as needed.
120 void OnScreenUnlocked();
122 // Returns true if |input_method_id| is in |active_input_method_ids_|.
123 bool InputMethodIsActivated(const std::string& input_method_id);
125 // Returns true if the given input method config value is a string list
126 // that only contains an input method ID of a keyboard layout.
127 bool ContainsOnlyKeyboardLayout(const std::vector<std::string>& value);
129 // Creates and initializes |candidate_window_controller_| if it hasn't been
130 // done.
131 void MaybeInitializeCandidateWindowController();
133 // If |current_input_method_id_| is not in |input_method_ids|, switch to
134 // input_method_ids[0]. If the ID is equal to input_method_ids[N], switch to
135 // input_method_ids[N+1].
136 void SwitchToNextInputMethodInternal(
137 const std::vector<std::string>& input_method_ids,
138 const std::string& current_input_method_id);
140 // Change system input method.
141 // Returns true if the system input method is changed.
142 bool ChangeInputMethodInternal(const std::string& input_method_id,
143 bool show_message);
145 // Called when the ComponentExtensionIMEManagerDelegate is initialized.
146 void OnComponentExtensionInitialized(
147 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
148 void InitializeComponentExtension();
150 // Loads necessary component extensions.
151 // TODO(nona): Support dynamical unloading.
152 void LoadNecessaryComponentExtensions();
154 // Adds new input method to given list if possible
155 bool EnableInputMethodImpl(
156 const std::string& input_method_id,
157 std::vector<std::string>* new_active_input_method_ids) const;
159 // Starts or stops the system input method framework as needed.
160 // (after list of enabled input methods has been updated)
161 void ReconfigureIMFramework();
163 scoped_ptr<InputMethodDelegate> delegate_;
165 // The current browser status.
166 State state_;
168 // A list of objects that monitor the manager.
169 ObserverList<InputMethodManager::Observer> observers_;
170 ObserverList<CandidateWindowObserver> candidate_window_observers_;
172 // The input method which was/is selected.
173 InputMethodDescriptor previous_input_method_;
174 InputMethodDescriptor current_input_method_;
175 // The active input method ids cache.
176 std::vector<std::string> active_input_method_ids_;
178 // The list of enabled extension IMEs.
179 std::vector<std::string> enabled_extension_imes_;
181 // For screen locker. When the screen is locked, |previous_input_method_|,
182 // |current_input_method_|, and |active_input_method_ids_| above are copied
183 // to these "saved" variables.
184 InputMethodDescriptor saved_previous_input_method_;
185 InputMethodDescriptor saved_current_input_method_;
186 std::vector<std::string> saved_active_input_method_ids_;
188 // Extra input methods that have been explicitly added to the menu, such as
189 // those created by extension.
190 std::map<std::string, InputMethodDescriptor> extra_input_methods_;
192 // Property list of the input method. This is set by extension IMEs.
193 InputMethodPropertyList property_list_;
195 // The candidate window. This will be deleted when the APP_TERMINATING
196 // message is sent.
197 scoped_ptr<CandidateWindowController> candidate_window_controller_;
199 // The object which can create an InputMethodDescriptor object.
200 InputMethodWhitelist whitelist_;
202 // An object which provides miscellaneous input method utility functions. Note
203 // that |util_| is required to initialize |xkeyboard_|.
204 InputMethodUtil util_;
206 // An object which provides component extension ime management functions.
207 scoped_ptr<ComponentExtensionIMEManager> component_extension_ime_manager_;
209 // An object for switching XKB layouts and keyboard status like caps lock and
210 // auto-repeat interval.
211 scoped_ptr<XKeyboard> xkeyboard_;
213 std::string pending_input_method_;
215 base::ThreadChecker thread_checker_;
217 base::WeakPtrFactory<InputMethodManagerImpl> weak_ptr_factory_;
219 DISALLOW_COPY_AND_ASSIGN(InputMethodManagerImpl);
222 } // namespace input_method
223 } // namespace chromeos
225 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_MANAGER_IMPL_H_