summaryrefslogtreecommitdiffstats
path: root/chromeos/ime/component_extension_ime_manager.h
blob: 23a5d891a75949c69693962a79f40b017ddb7b74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_IME_COMPONENT_EXTENSION_IME_MANAGER_H_
#define CHROMEOS_IME_COMPONENT_EXTENSION_IME_MANAGER_H_

#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/ime/input_method_descriptor.h"

namespace chromeos {

// Represents an engine in component extension IME.
struct CHROMEOS_EXPORT ComponentExtensionEngine {
  ComponentExtensionEngine();
  ~ComponentExtensionEngine();
  std::string engine_id;  // The engine id.
  std::string display_name;  // The display name.
  std::string language_code;  // The engine's language(ex. "en").
  std::string description;  // The engine description.
  std::vector<std::string> layouts;  // The list of keyboard layout of engine.
};

// Represents a component extension IME.
// TODO(nona): Use GURL for |option_page_url| instead of string.
struct CHROMEOS_EXPORT ComponentExtensionIME {
  ComponentExtensionIME();
  ~ComponentExtensionIME();
  std::string id;  // extension id.
  std::string manifest;  // the contents of manifest.json
  std::string description;  // description of extension.
  GURL options_page_url; // an URL to option page.
  base::FilePath path;
  std::vector<ComponentExtensionEngine> engines;
};

// Provides an interface to list/load/unload for component extension IME.
class CHROMEOS_EXPORT ComponentExtensionIMEManagerDelegate {
 public:
  ComponentExtensionIMEManagerDelegate();
  virtual ~ComponentExtensionIMEManagerDelegate();

  // Lists installed component extension IMEs.
  virtual std::vector<ComponentExtensionIME> ListIME() = 0;

  // Loads component extension IME associated with |extension_id|.
  // Returns false if it fails, otherwise returns true.
  virtual bool Load(const std::string& extension_id,
                    const std::string& manifest,
                    const base::FilePath& path) = 0;

  // Unloads component extension IME associated with |extension_id|.
  // Returns false if it fails, otherwise returns true;
  virtual bool Unload(const std::string& extension_id,
                      const base::FilePath& path) = 0;
};

// This class manages component extension input method.
class CHROMEOS_EXPORT ComponentExtensionIMEManager {
 public:
  class Observer {
   public:
    // Called when the initialization is done.
    virtual void OnInitialized() = 0;
  };

  ComponentExtensionIMEManager();
  virtual ~ComponentExtensionIMEManager();

  // Initializes component extension manager. This function create internal
  // mapping between input method id and engine components. This function must
  // be called before using any other function.
  void Initialize(scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate);

  // Returns true if the initialization is done, otherwise returns false.
  bool IsInitialized();

  // Loads |input_method_id| component extension IME. This function returns true
  // on success. This function is safe to call multiple times. Returns false if
  // already corresponding component extension is loaded.
  bool LoadComponentExtensionIME(const std::string& input_method_id);

  // Unloads |input_method_id| component extension IME. This function returns
  // true on success. This function is safe to call multiple times. Returns
  // false if already corresponding component extension is unloaded.
  bool UnloadComponentExtensionIME(const std::string& input_method_id);

  // Returns true if |input_method_id| is component extension ime id. Note that
  // this function does not check the |input_method_id| is really whitelisted
  // one or not. If you want to check |input_method_id| is whitelisted component
  // extension ime, please use IsWhitelisted instead.
  static bool IsComponentExtensionIMEId(const std::string& input_method_id);

  // Returns true if |input_method_id| is whitelisted component extension input
  // method.
  bool IsWhitelisted(const std::string& input_method_id);

  // Returns true if |extension_id| is whitelisted component extension.
  bool IsWhitelistedExtension(const std::string& extension_id);

  // Returns InputMethodId. This function returns empty string if |extension_id|
  // and |engine_id| is not a whitelisted component extention IME.
  std::string GetId(const std::string& extension_id,
                    const std::string& engine_id);

  // Returns localized name of |input_method_id|.
  std::string GetName(const std::string& input_method_id);

  // Returns localized description of |input_method_id|.
  std::string GetDescription(const std::string& input_method_id);

  // Returns list of input method id associated with |language|.
  std::vector<std::string> ListIMEByLanguage(const std::string& language);

  // Returns all IME as InputMethodDescriptors.
  input_method::InputMethodDescriptors GetAllIMEAsInputMethodDescriptor();

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 protected:
  // Returns InputMethodId for |engine_id| in |extension_id|. This function does
  // not check |extension_id| is component one or |engine_id| is really a member
  // of |extension_id|. Do not use this function outside from this class, just
  // for protected for unit testing.
  static std::string GetComponentExtensionIMEId(const std::string& extension_id,
                                                const std::string& engine_id);

 private:
  // Finds ComponentExtensionIME and EngineDescription associated with
  // |input_method_id|. This function retruns true if it is found, otherwise
  // returns false. |out_extension| and |out_engine| can be NULL.
  bool FindEngineEntry(const std::string& input_method_id,
                       ComponentExtensionIME* out_extension,
                       ComponentExtensionEngine* out_engine);
  scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate_;

  std::vector<ComponentExtensionIME> component_extension_imes_;

  ObserverList<Observer> observers_;

  bool is_initialized_;

  DISALLOW_COPY_AND_ASSIGN(ComponentExtensionIMEManager);
};

}  // namespace chromeos

#endif  // CHROMEOS_IME_COMPONENT_EXTENSION_IME_MANAGER_H_