Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / chromeos / ime / component_extension_ime_manager.cc
blobc557d090448a3573a4ff6f8e715c319f2597e5c7
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.
5 #include "base/logging.h"
6 #include "base/strings/string_util.h"
7 #include "chromeos/ime/component_extension_ime_manager.h"
9 namespace chromeos {
10 namespace {
11 const char* kComponentExtensionIMEPrefix = "_comp_ime_";
12 } // namespace
14 ComponentExtensionEngine::ComponentExtensionEngine() {
17 ComponentExtensionEngine::~ComponentExtensionEngine() {
20 ComponentExtensionIME::ComponentExtensionIME() {
23 ComponentExtensionIME::~ComponentExtensionIME() {
26 ComponentExtensionIMEManagerDelegate::ComponentExtensionIMEManagerDelegate() {
29 ComponentExtensionIMEManagerDelegate::~ComponentExtensionIMEManagerDelegate() {
32 ComponentExtensionIMEManager::ComponentExtensionIMEManager()
33 : is_initialized_(false) {
36 ComponentExtensionIMEManager::~ComponentExtensionIMEManager() {
39 void ComponentExtensionIMEManager::Initialize(
40 scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate) {
41 delegate_ = delegate.Pass();
42 component_extension_imes_ = delegate_->ListIME();
43 is_initialized_ = true;
44 FOR_EACH_OBSERVER(Observer, observers_, OnInitialized());
47 bool ComponentExtensionIMEManager::IsInitialized() {
48 return is_initialized_;
51 bool ComponentExtensionIMEManager::LoadComponentExtensionIME(
52 const std::string& input_method_id) {
53 ComponentExtensionIME ime;
54 if (FindEngineEntry(input_method_id, &ime, NULL))
55 return delegate_->Load(ime.id, ime.manifest, ime.path);
56 else
57 return false;
60 bool ComponentExtensionIMEManager::UnloadComponentExtensionIME(
61 const std::string& input_method_id) {
62 ComponentExtensionIME ime;
63 if (FindEngineEntry(input_method_id, &ime, NULL))
64 return delegate_->Unload(ime.id, ime.path);
65 else
66 return false;
69 // static
70 std::string ComponentExtensionIMEManager::GetComponentExtensionIMEId(
71 const std::string& extension_id,
72 const std::string& engine_id) {
73 return kComponentExtensionIMEPrefix + extension_id + engine_id;
76 // static
77 bool ComponentExtensionIMEManager::IsComponentExtensionIMEId(
78 const std::string& input_method_id) {
79 return StartsWithASCII(input_method_id,
80 kComponentExtensionIMEPrefix,
81 true); // Case sensitive.
84 bool ComponentExtensionIMEManager::IsWhitelisted(
85 const std::string& input_method_id) {
86 return IsComponentExtensionIMEId(input_method_id) &&
87 FindEngineEntry(input_method_id, NULL, NULL);
90 bool ComponentExtensionIMEManager::IsWhitelistedExtension(
91 const std::string& extension_id) {
92 for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
93 if (component_extension_imes_[i].id == extension_id)
94 return true;
96 return false;
99 std::string ComponentExtensionIMEManager::GetId(
100 const std::string& extension_id,
101 const std::string& engine_id) {
102 ComponentExtensionEngine engine;
103 const std::string& input_method_id =
104 GetComponentExtensionIMEId(extension_id, engine_id);
105 if (!FindEngineEntry(input_method_id, NULL, &engine))
106 return "";
107 return input_method_id;
110 std::string ComponentExtensionIMEManager::GetName(
111 const std::string& input_method_id) {
112 ComponentExtensionEngine engine;
113 if (!FindEngineEntry(input_method_id, NULL, &engine))
114 return "";
115 return engine.display_name;
118 std::string ComponentExtensionIMEManager::GetDescription(
119 const std::string& input_method_id) {
120 ComponentExtensionEngine engine;
121 if (!FindEngineEntry(input_method_id, NULL, &engine))
122 return "";
123 return engine.description;
126 std::vector<std::string> ComponentExtensionIMEManager::ListIMEByLanguage(
127 const std::string& language) {
128 std::vector<std::string> result;
129 for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
130 for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) {
131 if (component_extension_imes_[i].engines[j].language_code == language)
132 result.push_back(
133 GetComponentExtensionIMEId(
134 component_extension_imes_[i].id,
135 component_extension_imes_[i].engines[j].engine_id));
138 return result;
141 input_method::InputMethodDescriptors
142 ComponentExtensionIMEManager::GetAllIMEAsInputMethodDescriptor() {
143 input_method::InputMethodDescriptors result;
144 for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
145 for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) {
146 std::vector<std::string> languages;
147 languages.push_back(
148 component_extension_imes_[i].engines[j].language_code);
149 result.push_back(
150 input_method::InputMethodDescriptor(
151 GetComponentExtensionIMEId(
152 component_extension_imes_[i].id,
153 component_extension_imes_[i].engines[j].engine_id),
154 component_extension_imes_[i].engines[j].display_name,
155 component_extension_imes_[i].engines[j].layouts,
156 languages,
157 component_extension_imes_[i].options_page_url));
160 return result;
163 void ComponentExtensionIMEManager::AddObserver(Observer* observer) {
164 observers_.AddObserver(observer);
167 void ComponentExtensionIMEManager::RemoveObserver(Observer* observer) {
168 observers_.RemoveObserver(observer);
171 bool ComponentExtensionIMEManager::FindEngineEntry(
172 const std::string& input_method_id,
173 ComponentExtensionIME* out_extension,
174 ComponentExtensionEngine* out_engine) {
175 if (!IsComponentExtensionIMEId(input_method_id))
176 return false;
177 for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
178 const std::string extension_id = component_extension_imes_[i].id;
179 const std::vector<ComponentExtensionEngine>& engines =
180 component_extension_imes_[i].engines;
182 for (size_t j = 0; j < engines.size(); ++j) {
183 const std::string trial_ime_id = GetComponentExtensionIMEId(
184 extension_id, engines[j].engine_id);
185 if (trial_ime_id != input_method_id)
186 continue;
188 if (out_extension)
189 *out_extension = component_extension_imes_[i];
190 if (out_engine)
191 *out_engine = component_extension_imes_[i].engines[j];
192 return true;
195 return false;
198 } // namespace chromeos