1// Copyright 2013 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_COMPONENT_EXTENSION_IME_MANAGER_H_
6#define CHROMEOS_IME_COMPONENT_EXTENSION_IME_MANAGER_H_
7
8#include <set>
9
10#include "base/files/file_path.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/observer_list.h"
13#include "chromeos/chromeos_export.h"
14#include "chromeos/ime/input_method_descriptor.h"
15
16namespace chromeos {
17
18// Represents an engine in component extension IME.
19struct CHROMEOS_EXPORT ComponentExtensionEngine {
20  ComponentExtensionEngine();
21  ~ComponentExtensionEngine();
22  std::string engine_id;  // The engine id.
23  std::string display_name;  // The display name.
24  std::vector<std::string> language_codes;  // The engine's language(ex. "en").
25  std::string description;  // The engine description.
26  std::vector<std::string> layouts;  // The list of keyboard layout of engine.
27  GURL options_page_url; // an URL to option page.
28  GURL input_view_url; // an URL to input view page.
29};
30
31// Represents a component extension IME.
32struct CHROMEOS_EXPORT ComponentExtensionIME {
33  ComponentExtensionIME();
34  ~ComponentExtensionIME();
35  std::string id;  // extension id.
36  std::string manifest;  // the contents of manifest.json
37  std::string description;  // description of extension.
38  GURL options_page_url; // an URL to option page.
39  base::FilePath path;
40  std::vector<ComponentExtensionEngine> engines;
41};
42
43// Provides an interface to list/load/unload for component extension IME.
44class CHROMEOS_EXPORT ComponentExtensionIMEManagerDelegate {
45 public:
46  ComponentExtensionIMEManagerDelegate();
47  virtual ~ComponentExtensionIMEManagerDelegate();
48
49  // Lists installed component extension IMEs.
50  virtual std::vector<ComponentExtensionIME> ListIME() = 0;
51
52  // Loads component extension IME associated with |extension_id|.
53  // Returns false if it fails, otherwise returns true.
54  virtual bool Load(const std::string& extension_id,
55                    const std::string& manifest,
56                    const base::FilePath& path) = 0;
57
58  // Unloads component extension IME associated with |extension_id|.
59  virtual void Unload(const std::string& extension_id,
60                      const base::FilePath& path) = 0;
61};
62
63// This class manages component extension input method.
64class CHROMEOS_EXPORT ComponentExtensionIMEManager {
65 public:
66  class Observer {
67   public:
68    // Called when the initialization is done.
69    virtual void OnImeComponentExtensionInitialized() = 0;
70  };
71
72  ComponentExtensionIMEManager();
73  virtual ~ComponentExtensionIMEManager();
74
75  // Initializes component extension manager. This function create internal
76  // mapping between input method id and engine components. This function must
77  // be called before using any other function.
78  void Initialize(scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);
79
80  // Notifies the observers for the component extension IMEs are initialized.
81  void NotifyInitialized();
82
83  // Returns true if the initialization is done, otherwise returns false.
84  bool IsInitialized();
85
86  // Loads |input_method_id| component extension IME. This function returns true
87  // on success. This function is safe to call multiple times. Returns false if
88  // already corresponding component extension is loaded.
89  bool LoadComponentExtensionIME(const std::string& input_method_id);
90
91  // Unloads |input_method_id| component extension IME. This function returns
92  // true on success. This function is safe to call multiple times. Returns
93  // false if already corresponding component extension is unloaded.
94  bool UnloadComponentExtensionIME(const std::string& input_method_id);
95
96  // Returns true if |input_method_id| is whitelisted component extension input
97  // method.
98  bool IsWhitelisted(const std::string& input_method_id);
99
100  // Returns true if |extension_id| is whitelisted component extension.
101  bool IsWhitelistedExtension(const std::string& extension_id);
102
103  // Returns InputMethodId. This function returns empty string if |extension_id|
104  // and |engine_id| is not a whitelisted component extention IME.
105  std::string GetId(const std::string& extension_id,
106                    const std::string& engine_id);
107
108  // Returns localized name of |input_method_id|.
109  std::string GetName(const std::string& input_method_id);
110
111  // Returns localized description of |input_method_id|.
112  std::string GetDescription(const std::string& input_method_id);
113
114  // Returns list of input method id associated with |language|.
115  std::vector<std::string> ListIMEByLanguage(const std::string& language);
116
117  // Returns all IME as InputMethodDescriptors.
118  input_method::InputMethodDescriptors GetAllIMEAsInputMethodDescriptor();
119
120  // Returns all XKB keyboard IME as InputMethodDescriptors.
121  input_method::InputMethodDescriptors GetXkbIMEAsInputMethodDescriptor();
122
123  void AddObserver(Observer* observer);
124  void RemoveObserver(Observer* observer);
125
126 private:
127  // Finds ComponentExtensionIME and EngineDescription associated with
128  // |input_method_id|. This function retruns true if it is found, otherwise
129  // returns false. |out_extension| and |out_engine| can be NULL.
130  bool FindEngineEntry(const std::string& input_method_id,
131                       ComponentExtensionIME* out_extension,
132                       ComponentExtensionEngine* out_engine);
133
134  bool IsInLoginLayoutWhitelist(const std::vector<std::string>& layouts);
135
136  scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate_;
137
138  std::vector<ComponentExtensionIME> component_extension_imes_;
139
140  ObserverList<Observer> observers_;
141
142  bool is_initialized_;
143
144  bool was_initialization_notified_;
145
146  std::set<std::string> login_layout_set_;
147
148  DISALLOW_COPY_AND_ASSIGN(ComponentExtensionIMEManager);
149};
150
151}  // namespace chromeos
152
153#endif  // CHROMEOS_IME_COMPONENT_EXTENSION_IME_MANAGER_H_
154