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