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 CHROMEOS_IME_INPUT_METHOD_MANAGER_H_
6#define CHROMEOS_IME_INPUT_METHOD_MANAGER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/memory/scoped_ptr.h"
13#include "chromeos/chromeos_export.h"
14#include "chromeos/ime/input_method_descriptor.h"
15#include "chromeos/ime/input_method_property.h"
16
17namespace ui {
18class Accelerator;
19}  // namespace ui
20
21namespace chromeos {
22class ComponentExtensionIMEManager;
23class InputMethodEngineInterface;
24namespace input_method {
25
26class InputMethodUtil;
27class XKeyboard;
28
29// This class manages input methodshandles.  Classes can add themselves as
30// observers. Clients can get an instance of this library class by:
31// InputMethodManager::Get().
32class CHROMEOS_EXPORT InputMethodManager {
33 public:
34  enum State {
35    STATE_LOGIN_SCREEN = 0,
36    STATE_BROWSER_SCREEN,
37    STATE_LOCK_SCREEN,
38    STATE_TERMINATING,
39  };
40
41  class Observer {
42   public:
43    virtual ~Observer() {}
44    // Called when the current input method is changed.  |show_message|
45    // indicates whether the user should be notified of this change.
46    virtual void InputMethodChanged(InputMethodManager* manager,
47                                    bool show_message) = 0;
48    // Called when the list of properties is changed.
49    virtual void InputMethodPropertyChanged(InputMethodManager* manager) = 0;
50  };
51
52  // CandidateWindowObserver is notified of events related to the candidate
53  // window.  The "suggestion window" used by IMEs such as ibus-mozc does not
54  // count as the candidate window (this may change if we later want suggestion
55  // window events as well).  These events also won't occur when the virtual
56  // keyboard is used, since it controls its own candidate window.
57  class CandidateWindowObserver {
58   public:
59    virtual ~CandidateWindowObserver() {}
60    // Called when the candidate window is opened.
61    virtual void CandidateWindowOpened(InputMethodManager* manager) = 0;
62    // Called when the candidate window is closed.
63    virtual void CandidateWindowClosed(InputMethodManager* manager) = 0;
64  };
65
66  virtual ~InputMethodManager() {}
67
68  // Gets the global instance of InputMethodManager. Initialize() must be called
69  // first.
70  static CHROMEOS_EXPORT InputMethodManager* Get();
71
72  // Sets the global instance. |instance| will be owned by the internal pointer
73  // and deleted by Shutdown().
74  // TODO(nona): Instanciate InputMethodManagerImpl inside of this function once
75  //             crbug.com/164375 is fixed.
76  static CHROMEOS_EXPORT void Initialize(InputMethodManager* instance);
77
78  // Destroy the global instance.
79  static CHROMEOS_EXPORT void Shutdown();
80
81  // Adds an observer to receive notifications of input method related
82  // changes as desribed in the Observer class above.
83  virtual void AddObserver(Observer* observer) = 0;
84  virtual void AddCandidateWindowObserver(
85      CandidateWindowObserver* observer) = 0;
86  virtual void RemoveObserver(Observer* observer) = 0;
87  virtual void RemoveCandidateWindowObserver(
88      CandidateWindowObserver* observer) = 0;
89
90  // Returns all input methods that are supported, including ones not active.
91  // This function never returns NULL. Note that input method extensions are NOT
92  // included in the result.
93  virtual scoped_ptr<InputMethodDescriptors>
94      GetSupportedInputMethods() const = 0;
95
96  // Returns the list of input methods we can select (i.e. active) including
97  // extension input methods.
98  virtual scoped_ptr<InputMethodDescriptors> GetActiveInputMethods() const = 0;
99
100  // Returns the list of input methods we can select (i.e. active) including
101  // extension input methods.
102  // The same as GetActiveInputMethods but returns reference to internal list.
103  virtual const std::vector<std::string>& GetActiveInputMethodIds() const = 0;
104
105  // Returns the number of active input methods including extension input
106  // methods.
107  virtual size_t GetNumActiveInputMethods() const = 0;
108
109  // Changes the current input method to |input_method_id|. If |input_method_id|
110  // is not active, switch to the first one in the active input method list.
111  virtual void ChangeInputMethod(const std::string& input_method_id) = 0;
112
113  // Enables keyboard layouts (e.g. US Qwerty, US Dvorak, French Azerty) that
114  // are necessary for the |language_code| and then switches to |initial_layout|
115  // if the string is not empty. For example, if |language_code| is "en-US", US
116  // Qwerty, US International, US Extended, US Dvorak, and US Colemak layouts
117  // would be enabled. Likewise, for Germany locale, US Qwerty which corresponds
118  // to the hardware keyboard layout and several keyboard layouts for Germany
119  // would be enabled.
120  // This method is for setting up i18n keyboard layouts for the login screen.
121  virtual void EnableLayouts(const std::string& language_code,
122                             const std::string& initial_layout) = 0;
123
124  // Activates the input method property specified by the |key|.
125  virtual void ActivateInputMethodProperty(const std::string& key) = 0;
126
127  // Updates the list of active input method IDs, and then starts or stops the
128  // system input method framework as needed.
129  virtual bool EnableInputMethods(
130      const std::vector<std::string>& new_active_input_method_ids) = 0;
131
132  // Adds one entry to the list of active input method IDs, and then starts or
133  // stops the system input method framework as needed.
134  virtual bool EnableInputMethod(
135      const std::string& new_active_input_method_id) = 0;
136
137  // Adds an input method extension. This function does not takes ownership of
138  // |instance|.
139  virtual void AddInputMethodExtension(
140      const std::string& id,
141      const std::string& name,
142      const std::vector<std::string>& layouts,
143      const std::vector<std::string>& languages,
144      const GURL& options_url,
145      const GURL& inputview_url,
146      InputMethodEngineInterface* instance) = 0;
147
148  // Removes an input method extension.
149  virtual void RemoveInputMethodExtension(const std::string& id) = 0;
150
151  // Returns a list of descriptors for all Input Method Extensions.
152  virtual void GetInputMethodExtensions(InputMethodDescriptors* result) = 0;
153
154  // Sets the list of extension IME ids which should be enabled.
155  virtual void SetEnabledExtensionImes(std::vector<std::string>* ids) = 0;
156
157  // Sets current input method to default (first owners, then hardware).
158  virtual void SetInputMethodDefault() = 0;
159
160  // Gets the descriptor of the input method which is currently selected.
161  virtual InputMethodDescriptor GetCurrentInputMethod() const = 0;
162
163  // Gets the list of input method properties. The list could be empty().
164  virtual InputMethodPropertyList GetCurrentInputMethodProperties() const = 0;
165
166  // Sets the list of input method properties. The list could be empty().
167  virtual void SetCurrentInputMethodProperties(
168      const InputMethodPropertyList& property_list) = 0;
169
170  // Returns an X keyboard object which could be used to change the current XKB
171  // layout, change the caps lock status, and set the auto repeat rate/interval.
172  virtual XKeyboard* GetXKeyboard() = 0;
173
174  // Returns an InputMethodUtil object.
175  virtual InputMethodUtil* GetInputMethodUtil() = 0;
176
177  // Returns a ComponentExtentionIMEManager object.
178  virtual ComponentExtensionIMEManager* GetComponentExtensionIMEManager() = 0;
179
180  // Switches the current input method (or keyboard layout) to the next one.
181  virtual bool SwitchToNextInputMethod() = 0;
182
183  // Switches the current input method (or keyboard layout) to the previous one.
184  virtual bool SwitchToPreviousInputMethod(
185      const ui::Accelerator& accelerator) = 0;
186
187  // Switches to an input method (or keyboard layout) which is associated with
188  // the |accelerator|.
189  virtual bool SwitchInputMethod(const ui::Accelerator& accelerator) = 0;
190
191  // If keyboard layout can be uset at login screen
192  virtual bool IsLoginKeyboard(const std::string& layout) const = 0;
193};
194
195}  // namespace input_method
196}  // namespace chromeos
197
198#endif  // CHROMEOS_IME_INPUT_METHOD_MANAGER_H_
199