Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / component_extension_ime_manager_impl.cc
blobcc0c287dc5007e05e98cd1c896ec515d0d490af1
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 "chrome/browser/chromeos/input_method/component_extension_ime_manager_impl.h"
7 #include <algorithm>
9 #include "base/files/file_util.h"
10 #include "base/json/json_string_value_serializer.h"
11 #include "base/logging.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "base/sys_info.h"
15 #include "chrome/browser/extensions/component_loader.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/extensions/extension_constants.h"
21 #include "chrome/grit/browser_resources.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "extensions/common/extension_l10n_util.h"
27 #include "extensions/common/manifest_constants.h"
28 #include "ui/base/ime/chromeos/extension_ime_util.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
32 namespace chromeos {
34 namespace {
36 struct WhitelistedComponentExtensionIME {
37 const char* id;
38 int manifest_resource_id;
39 } whitelisted_component_extension[] = {
40 {// ChromeOS Hangul Input.
41 extension_ime_util::kHangulExtensionId, IDR_HANGUL_MANIFEST,
43 #if defined(GOOGLE_CHROME_BUILD)
44 {// Official Google XKB Input.
45 extension_ime_util::kXkbExtensionId, IDR_GOOGLE_XKB_MANIFEST,
47 {// Google input tools.
48 extension_ime_util::kT13nExtensionId, IDR_GOOGLE_INPUT_TOOLS_MANIFEST,
50 #else
51 {// Open-sourced ChromeOS xkb extension.
52 extension_ime_util::kXkbExtensionId, IDR_XKB_MANIFEST,
54 {// Open-sourced ChromeOS Keyboards extension.
55 extension_ime_util::kM17nExtensionId, IDR_M17N_MANIFEST,
57 {// Open-sourced Pinyin Chinese Input Method.
58 extension_ime_util::kChinesePinyinExtensionId, IDR_PINYIN_MANIFEST,
60 {// Open-sourced Zhuyin Chinese Input Method.
61 extension_ime_util::kChineseZhuyinExtensionId, IDR_ZHUYIN_MANIFEST,
63 {// Open-sourced Cangjie Chinese Input Method.
64 extension_ime_util::kChineseCangjieExtensionId, IDR_CANGJIE_MANIFEST,
66 {// Japanese Mozc Input.
67 extension_ime_util::kMozcExtensionId, IDR_MOZC_MANIFEST,
69 #endif
70 {// Braille hardware keyboard IME that works together with ChromeVox.
71 extension_misc::kBrailleImeExtensionId, IDR_BRAILLE_MANIFEST,
75 const struct InputMethodNameMap {
76 const char* message_name;
77 int resource_id;
78 bool operator<(const InputMethodNameMap& other) const {
79 return strcmp(message_name, other.message_name) < 0;
81 } kInputMethodNameMap[] = {
82 {"__MSG_INPUTMETHOD_ARRAY__", IDS_IME_NAME_INPUTMETHOD_ARRAY},
83 {"__MSG_INPUTMETHOD_CANGJIE__", IDS_IME_NAME_INPUTMETHOD_CANGJIE},
84 {"__MSG_INPUTMETHOD_DAYI__", IDS_IME_NAME_INPUTMETHOD_DAYI},
85 {"__MSG_INPUTMETHOD_HANGUL_2_SET__",
86 IDS_IME_NAME_INPUTMETHOD_HANGUL_2_SET},
87 {"__MSG_INPUTMETHOD_HANGUL_3_SET_390__",
88 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_390},
89 {"__MSG_INPUTMETHOD_HANGUL_3_SET_FINAL__",
90 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_FINAL},
91 {"__MSG_INPUTMETHOD_HANGUL_3_SET_NO_SHIFT__",
92 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_NO_SHIFT},
93 {"__MSG_INPUTMETHOD_HANGUL_AHNMATAE__",
94 IDS_IME_NAME_INPUTMETHOD_HANGUL_AHNMATAE},
95 {"__MSG_INPUTMETHOD_HANGUL_ROMAJA__",
96 IDS_IME_NAME_INPUTMETHOD_HANGUL_ROMAJA},
97 {"__MSG_INPUTMETHOD_HANGUL__", IDS_IME_NAME_INPUTMETHOD_HANGUL},
98 {"__MSG_INPUTMETHOD_MOZC_JP__", IDS_IME_NAME_INPUTMETHOD_MOZC_JP},
99 {"__MSG_INPUTMETHOD_MOZC_US__", IDS_IME_NAME_INPUTMETHOD_MOZC_US},
100 {"__MSG_INPUTMETHOD_PINYIN__", IDS_IME_NAME_INPUTMETHOD_PINYIN},
101 {"__MSG_INPUTMETHOD_QUICK__", IDS_IME_NAME_INPUTMETHOD_QUICK},
102 {"__MSG_INPUTMETHOD_TRADITIONAL_PINYIN__",
103 IDS_IME_NAME_INPUTMETHOD_TRADITIONAL_PINYIN},
104 {"__MSG_INPUTMETHOD_WUBI__", IDS_IME_NAME_INPUTMETHOD_WUBI},
105 {"__MSG_INPUTMETHOD_ZHUYIN__", IDS_IME_NAME_INPUTMETHOD_ZHUYIN},
106 {"__MSG_KEYBOARD_ARABIC__", IDS_IME_NAME_KEYBOARD_ARABIC},
107 {"__MSG_KEYBOARD_ARMENIAN_PHONETIC__",
108 IDS_IME_NAME_KEYBOARD_ARMENIAN_PHONETIC},
109 {"__MSG_KEYBOARD_BELARUSIAN__", IDS_IME_NAME_KEYBOARD_BELARUSIAN},
110 {"__MSG_KEYBOARD_BELGIAN__", IDS_IME_NAME_KEYBOARD_BELGIAN},
111 {"__MSG_KEYBOARD_BENGALI_PHONETIC__",
112 IDS_IME_NAME_KEYBOARD_BENGALI_PHONETIC},
113 {"__MSG_KEYBOARD_BRAZILIAN__", IDS_IME_NAME_KEYBOARD_BRAZILIAN},
114 {"__MSG_KEYBOARD_BULGARIAN_PHONETIC__",
115 IDS_IME_NAME_KEYBOARD_BULGARIAN_PHONETIC},
116 {"__MSG_KEYBOARD_BULGARIAN__", IDS_IME_NAME_KEYBOARD_BULGARIAN},
117 {"__MSG_KEYBOARD_CANADIAN_ENGLISH__",
118 IDS_IME_NAME_KEYBOARD_CANADIAN_ENGLISH},
119 {"__MSG_KEYBOARD_CANADIAN_FRENCH__",
120 IDS_IME_NAME_KEYBOARD_CANADIAN_FRENCH},
121 {"__MSG_KEYBOARD_CANADIAN_MULTILINGUAL__",
122 IDS_IME_NAME_KEYBOARD_CANADIAN_MULTILINGUAL},
123 {"__MSG_KEYBOARD_CATALAN__", IDS_IME_NAME_KEYBOARD_CATALAN},
124 {"__MSG_KEYBOARD_CROATIAN__", IDS_IME_NAME_KEYBOARD_CROATIAN},
125 {"__MSG_KEYBOARD_CZECH_QWERTY__", IDS_IME_NAME_KEYBOARD_CZECH_QWERTY},
126 {"__MSG_KEYBOARD_CZECH__", IDS_IME_NAME_KEYBOARD_CZECH},
127 {"__MSG_KEYBOARD_DANISH__", IDS_IME_NAME_KEYBOARD_DANISH},
128 {"__MSG_KEYBOARD_DEVANAGARI_PHONETIC__",
129 IDS_IME_NAME_KEYBOARD_DEVANAGARI_PHONETIC},
130 {"__MSG_KEYBOARD_ESTONIAN__", IDS_IME_NAME_KEYBOARD_ESTONIAN},
131 {"__MSG_KEYBOARD_ETHIOPIC__", IDS_IME_NAME_KEYBOARD_ETHIOPIC},
132 {"__MSG_KEYBOARD_FINNISH__", IDS_IME_NAME_KEYBOARD_FINNISH},
133 {"__MSG_KEYBOARD_FRENCH__", IDS_IME_NAME_KEYBOARD_FRENCH},
134 {"__MSG_KEYBOARD_GEORGIAN__", IDS_IME_NAME_KEYBOARD_GEORGIAN},
135 {"__MSG_KEYBOARD_GERMAN_NEO_2__", IDS_IME_NAME_KEYBOARD_GERMAN_NEO_2},
136 {"__MSG_KEYBOARD_GERMAN__", IDS_IME_NAME_KEYBOARD_GERMAN},
137 {"__MSG_KEYBOARD_GREEK__", IDS_IME_NAME_KEYBOARD_GREEK},
138 {"__MSG_KEYBOARD_GUJARATI_PHONETIC__",
139 IDS_IME_NAME_KEYBOARD_GUJARATI_PHONETIC},
140 {"__MSG_KEYBOARD_HEBREW__", IDS_IME_NAME_KEYBOARD_HEBREW},
141 {"__MSG_KEYBOARD_HUNGARIAN__", IDS_IME_NAME_KEYBOARD_HUNGARIAN},
142 {"__MSG_KEYBOARD_ICELANDIC__", IDS_IME_NAME_KEYBOARD_ICELANDIC},
143 {"__MSG_KEYBOARD_IRISH__", IDS_IME_NAME_KEYBOARD_IRISH},
144 {"__MSG_KEYBOARD_ITALIAN__", IDS_IME_NAME_KEYBOARD_ITALIAN},
145 {"__MSG_KEYBOARD_JAPANESE__", IDS_IME_NAME_KEYBOARD_JAPANESE},
146 {"__MSG_KEYBOARD_KANNADA_PHONETIC__",
147 IDS_IME_NAME_KEYBOARD_KANNADA_PHONETIC},
148 {"__MSG_KEYBOARD_KHMER__", IDS_IME_NAME_KEYBOARD_KHMER},
149 {"__MSG_KEYBOARD_LAO__", IDS_IME_NAME_KEYBOARD_LAO},
150 {"__MSG_KEYBOARD_LATIN_AMERICAN__", IDS_IME_NAME_KEYBOARD_LATIN_AMERICAN},
151 {"__MSG_KEYBOARD_LATVIAN__", IDS_IME_NAME_KEYBOARD_LATVIAN},
152 {"__MSG_KEYBOARD_LITHUANIAN__", IDS_IME_NAME_KEYBOARD_LITHUANIAN},
153 {"__MSG_KEYBOARD_MALAYALAM_PHONETIC__",
154 IDS_IME_NAME_KEYBOARD_MALAYALAM_PHONETIC},
155 {"__MSG_KEYBOARD_MALTESE__", IDS_IME_NAME_KEYBOARD_MALTESE},
156 {"__MSG_KEYBOARD_MONGOLIAN__", IDS_IME_NAME_KEYBOARD_MONGOLIAN},
157 {"__MSG_KEYBOARD_MYANMAR_MYANSAN__",
158 IDS_IME_NAME_KEYBOARD_MYANMAR_MYANSAN},
159 {"__MSG_KEYBOARD_MYANMAR__", IDS_IME_NAME_KEYBOARD_MYANMAR},
160 {"__MSG_KEYBOARD_NEPALI_INSCRIPT__",
161 IDS_IME_NAME_KEYBOARD_NEPALI_INSCRIPT},
162 {"__MSG_KEYBOARD_NEPALI_PHONETIC__",
163 IDS_IME_NAME_KEYBOARD_NEPALI_PHONETIC},
164 {"__MSG_KEYBOARD_NETHERLANDS__", IDS_IME_NAME_KEYBOARD_NETHERLANDS},
165 {"__MSG_KEYBOARD_NORWEGIAN__", IDS_IME_NAME_KEYBOARD_NORWEGIAN},
166 {"__MSG_KEYBOARD_PERSIAN__", IDS_IME_NAME_KEYBOARD_PERSIAN},
167 {"__MSG_KEYBOARD_POLISH__", IDS_IME_NAME_KEYBOARD_POLISH},
168 {"__MSG_KEYBOARD_PORTUGUESE__", IDS_IME_NAME_KEYBOARD_PORTUGUESE},
169 {"__MSG_KEYBOARD_ROMANIAN_STANDARD__",
170 IDS_IME_NAME_KEYBOARD_ROMANIAN_STANDARD},
171 {"__MSG_KEYBOARD_ROMANIAN__", IDS_IME_NAME_KEYBOARD_ROMANIAN},
172 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC_AATSEEL__",
173 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC_AATSEEL},
174 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC_YAZHERT__",
175 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC_YAZHERT},
176 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC__",
177 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC},
178 {"__MSG_KEYBOARD_RUSSIAN__", IDS_IME_NAME_KEYBOARD_RUSSIAN},
179 {"__MSG_KEYBOARD_SERBIAN__", IDS_IME_NAME_KEYBOARD_SERBIAN},
180 {"__MSG_KEYBOARD_SINHALA__", IDS_IME_NAME_KEYBOARD_SINHALA},
181 {"__MSG_KEYBOARD_SLOVAKIAN__", IDS_IME_NAME_KEYBOARD_SLOVAKIAN},
182 {"__MSG_KEYBOARD_SLOVENIAN__", IDS_IME_NAME_KEYBOARD_SLOVENIAN},
183 {"__MSG_KEYBOARD_SORANIKURDISH_AR__",
184 IDS_IME_NAME_KEYBOARD_SORANIKURDISH_AR},
185 {"__MSG_KEYBOARD_SORANIKURDISH_EN__",
186 IDS_IME_NAME_KEYBOARD_SORANIKURDISH_EN},
187 {"__MSG_KEYBOARD_SPANISH__", IDS_IME_NAME_KEYBOARD_SPANISH},
188 {"__MSG_KEYBOARD_SWEDISH__", IDS_IME_NAME_KEYBOARD_SWEDISH},
189 {"__MSG_KEYBOARD_SWISS_FRENCH__", IDS_IME_NAME_KEYBOARD_SWISS_FRENCH},
190 {"__MSG_KEYBOARD_SWISS__", IDS_IME_NAME_KEYBOARD_SWISS},
191 {"__MSG_KEYBOARD_TAMIL_INSCRIPT__", IDS_IME_NAME_KEYBOARD_TAMIL_INSCRIPT},
192 {"__MSG_KEYBOARD_TAMIL_ITRANS__", IDS_IME_NAME_KEYBOARD_TAMIL_ITRANS},
193 {"__MSG_KEYBOARD_TAMIL_PHONETIC__", IDS_IME_NAME_KEYBOARD_TAMIL_PHONETIC},
194 {"__MSG_KEYBOARD_TAMIL_TAMIL99__", IDS_IME_NAME_KEYBOARD_TAMIL_TAMIL99},
195 {"__MSG_KEYBOARD_TAMIL_TYPEWRITER__",
196 IDS_IME_NAME_KEYBOARD_TAMIL_TYPEWRITER},
197 {"__MSG_KEYBOARD_TELUGU_PHONETIC__",
198 IDS_IME_NAME_KEYBOARD_TELUGU_PHONETIC},
199 {"__MSG_KEYBOARD_THAI_KEDMANEE__", IDS_IME_NAME_KEYBOARD_THAI_KEDMANEE},
200 {"__MSG_KEYBOARD_THAI_PATTACHOTE__",
201 IDS_IME_NAME_KEYBOARD_THAI_PATTACHOTE},
202 {"__MSG_KEYBOARD_THAI_TIS__", IDS_IME_NAME_KEYBOARD_THAI_TIS},
203 {"__MSG_KEYBOARD_TURKISH__", IDS_IME_NAME_KEYBOARD_TURKISH},
204 {"__MSG_KEYBOARD_UKRAINIAN__", IDS_IME_NAME_KEYBOARD_UKRAINIAN},
205 {"__MSG_KEYBOARD_UK_DVORAK__", IDS_IME_NAME_KEYBOARD_UK_DVORAK},
206 {"__MSG_KEYBOARD_UK__", IDS_IME_NAME_KEYBOARD_UK},
207 {"__MSG_KEYBOARD_US_COLEMAK__", IDS_IME_NAME_KEYBOARD_US_COLEMAK},
208 {"__MSG_KEYBOARD_US_DVORAK__", IDS_IME_NAME_KEYBOARD_US_DVORAK},
209 {"__MSG_KEYBOARD_US_EXTENDED__", IDS_IME_NAME_KEYBOARD_US_EXTENDED},
210 {"__MSG_KEYBOARD_US_INTERNATIONAL__",
211 IDS_IME_NAME_KEYBOARD_US_INTERNATIONAL},
212 {"__MSG_KEYBOARD_US__", IDS_IME_NAME_KEYBOARD_US},
213 {"__MSG_KEYBOARD_VIETNAMESE_TCVN__",
214 IDS_IME_NAME_KEYBOARD_VIETNAMESE_TCVN},
215 {"__MSG_KEYBOARD_VIETNAMESE_TELEX__",
216 IDS_IME_NAME_KEYBOARD_VIETNAMESE_TELEX},
217 {"__MSG_KEYBOARD_VIETNAMESE_VIQR__",
218 IDS_IME_NAME_KEYBOARD_VIETNAMESE_VIQR},
219 {"__MSG_KEYBOARD_VIETNAMESE_VNI__", IDS_IME_NAME_KEYBOARD_VIETNAMESE_VNI},
220 {"__MSG_TRANSLITERATION_AM__", IDS_IME_NAME_TRANSLITERATION_AM},
221 {"__MSG_TRANSLITERATION_AR__", IDS_IME_NAME_TRANSLITERATION_AR},
222 {"__MSG_TRANSLITERATION_BN__", IDS_IME_NAME_TRANSLITERATION_BN},
223 {"__MSG_TRANSLITERATION_EL__", IDS_IME_NAME_TRANSLITERATION_EL},
224 {"__MSG_TRANSLITERATION_FA__", IDS_IME_NAME_TRANSLITERATION_FA},
225 {"__MSG_TRANSLITERATION_GU__", IDS_IME_NAME_TRANSLITERATION_GU},
226 {"__MSG_TRANSLITERATION_HE__", IDS_IME_NAME_TRANSLITERATION_HE},
227 {"__MSG_TRANSLITERATION_HI__", IDS_IME_NAME_TRANSLITERATION_HI},
228 {"__MSG_TRANSLITERATION_KN__", IDS_IME_NAME_TRANSLITERATION_KN},
229 {"__MSG_TRANSLITERATION_ML__", IDS_IME_NAME_TRANSLITERATION_ML},
230 {"__MSG_TRANSLITERATION_MR__", IDS_IME_NAME_TRANSLITERATION_MR},
231 {"__MSG_TRANSLITERATION_NE__", IDS_IME_NAME_TRANSLITERATION_NE},
232 {"__MSG_TRANSLITERATION_OR__", IDS_IME_NAME_TRANSLITERATION_OR},
233 {"__MSG_TRANSLITERATION_PA__", IDS_IME_NAME_TRANSLITERATION_PA},
234 {"__MSG_TRANSLITERATION_SA__", IDS_IME_NAME_TRANSLITERATION_SA},
235 {"__MSG_TRANSLITERATION_SR__", IDS_IME_NAME_TRANSLITERATION_SR},
236 {"__MSG_TRANSLITERATION_TA__", IDS_IME_NAME_TRANSLITERATION_TA},
237 {"__MSG_TRANSLITERATION_TE__", IDS_IME_NAME_TRANSLITERATION_TE},
238 {"__MSG_TRANSLITERATION_TI__", IDS_IME_NAME_TRANSLITERATION_TI},
239 {"__MSG_TRANSLITERATION_UR__", IDS_IME_NAME_TRANSLITERATION_UR},
242 const char kImePathKeyName[] = "ime_path";
244 extensions::ComponentLoader* GetComponentLoader(Profile* profile) {
245 extensions::ExtensionSystem* extension_system =
246 extensions::ExtensionSystem::Get(profile);
247 ExtensionService* extension_service = extension_system->extension_service();
248 return extension_service->component_loader();
251 void DoLoadExtension(Profile* profile,
252 const std::string& extension_id,
253 const std::string& manifest,
254 const base::FilePath& file_path) {
255 extensions::ExtensionSystem* extension_system =
256 extensions::ExtensionSystem::Get(profile);
257 ExtensionService* extension_service = extension_system->extension_service();
258 DCHECK(extension_service);
259 if (extension_service->GetExtensionById(extension_id, false))
260 return;
261 const std::string loaded_extension_id =
262 GetComponentLoader(profile)->Add(manifest, file_path);
263 DCHECK_EQ(loaded_extension_id, extension_id);
266 bool CheckFilePath(const base::FilePath* file_path) {
267 return base::PathExists(*file_path);
270 void OnFilePathChecked(Profile* profile,
271 const std::string* extension_id,
272 const std::string* manifest,
273 const base::FilePath* file_path,
274 bool result) {
275 if (result)
276 DoLoadExtension(profile, *extension_id, *manifest, *file_path);
277 else
278 LOG(ERROR) << "IME extension file path not exists: " << file_path->value();
281 } // namespace
283 ComponentExtensionIMEManagerImpl::ComponentExtensionIMEManagerImpl() {
284 ReadComponentExtensionsInfo(&component_extension_list_);
287 ComponentExtensionIMEManagerImpl::~ComponentExtensionIMEManagerImpl() {
290 std::vector<ComponentExtensionIME> ComponentExtensionIMEManagerImpl::ListIME() {
291 return component_extension_list_;
294 void ComponentExtensionIMEManagerImpl::Load(Profile* profile,
295 const std::string& extension_id,
296 const std::string& manifest,
297 const base::FilePath& file_path) {
298 // Check the existence of file path to avoid unnecessary extension loading
299 // and InputMethodEngine creation, so that the virtual keyboard web content
300 // url won't be override by IME component extensions.
301 base::FilePath* copied_file_path = new base::FilePath(file_path);
302 content::BrowserThread::PostTaskAndReplyWithResult(
303 content::BrowserThread::FILE,
304 FROM_HERE,
305 base::Bind(&CheckFilePath,
306 base::Unretained(copied_file_path)),
307 base::Bind(&OnFilePathChecked,
308 base::Unretained(profile),
309 base::Owned(new std::string(extension_id)),
310 base::Owned(new std::string(manifest)),
311 base::Owned(copied_file_path)));
314 void ComponentExtensionIMEManagerImpl::Unload(Profile* profile,
315 const std::string& extension_id,
316 const base::FilePath& file_path) {
317 // Remove(extension_id) does nothing when the extension has already been
318 // removed or not been registered.
319 GetComponentLoader(profile)->Remove(extension_id);
322 scoped_ptr<base::DictionaryValue> ComponentExtensionIMEManagerImpl::GetManifest(
323 const std::string& manifest_string) {
324 std::string error;
325 JSONStringValueDeserializer deserializer(manifest_string);
326 scoped_ptr<base::Value> manifest(deserializer.Deserialize(NULL, &error));
327 if (!manifest.get())
328 LOG(ERROR) << "Failed at getting manifest";
330 return scoped_ptr<base::DictionaryValue>(
331 static_cast<base::DictionaryValue*>(manifest.release())).Pass();
334 // static
335 bool ComponentExtensionIMEManagerImpl::ReadEngineComponent(
336 const ComponentExtensionIME& component_extension,
337 const base::DictionaryValue& dict,
338 ComponentExtensionEngine* out) {
339 DCHECK(out);
340 std::string type;
341 if (!dict.GetString(extensions::manifest_keys::kType, &type))
342 return false;
343 if (type != "ime")
344 return false;
345 if (!dict.GetString(extensions::manifest_keys::kId, &out->engine_id))
346 return false;
347 if (!dict.GetString(extensions::manifest_keys::kName, &out->display_name))
348 return false;
349 if (!dict.GetString(extensions::manifest_keys::kIndicator, &out->indicator))
350 out->indicator = "";
352 // Localizes the input method name.
353 if (out->display_name.find("__MSG_") == 0) {
354 const InputMethodNameMap* map = kInputMethodNameMap;
355 size_t map_size = arraysize(kInputMethodNameMap);
356 std::string name = StringToUpperASCII(out->display_name);
357 const InputMethodNameMap map_key = {name.c_str(), 0};
358 const InputMethodNameMap* p =
359 std::lower_bound(map, map + map_size, map_key);
360 if (p != map + map_size && name == p->message_name)
361 out->display_name = l10n_util::GetStringUTF8(p->resource_id);
363 DCHECK(out->display_name.find("__MSG_") == std::string::npos);
365 std::set<std::string> languages;
366 const base::Value* language_value = NULL;
367 if (dict.Get(extensions::manifest_keys::kLanguage, &language_value)) {
368 if (language_value->GetType() == base::Value::TYPE_STRING) {
369 std::string language_str;
370 language_value->GetAsString(&language_str);
371 languages.insert(language_str);
372 } else if (language_value->GetType() == base::Value::TYPE_LIST) {
373 const base::ListValue* language_list = NULL;
374 language_value->GetAsList(&language_list);
375 for (size_t j = 0; j < language_list->GetSize(); ++j) {
376 std::string language_str;
377 if (language_list->GetString(j, &language_str))
378 languages.insert(language_str);
382 DCHECK(!languages.empty());
383 out->language_codes.assign(languages.begin(), languages.end());
385 const base::ListValue* layouts = NULL;
386 if (!dict.GetList(extensions::manifest_keys::kLayouts, &layouts))
387 return false;
389 for (size_t i = 0; i < layouts->GetSize(); ++i) {
390 std::string buffer;
391 if (layouts->GetString(i, &buffer))
392 out->layouts.push_back(buffer);
395 std::string url_string;
396 if (dict.GetString(extensions::manifest_keys::kInputView,
397 &url_string)) {
398 GURL url = extensions::Extension::GetResourceURL(
399 extensions::Extension::GetBaseURLFromExtensionId(
400 component_extension.id),
401 url_string);
402 if (!url.is_valid())
403 return false;
404 out->input_view_url = url;
407 if (dict.GetString(extensions::manifest_keys::kOptionsPage,
408 &url_string)) {
409 GURL url = extensions::Extension::GetResourceURL(
410 extensions::Extension::GetBaseURLFromExtensionId(
411 component_extension.id),
412 url_string);
413 if (!url.is_valid())
414 return false;
415 out->options_page_url = url;
416 } else {
417 // Fallback to extension level options page.
418 out->options_page_url = component_extension.options_page_url;
421 return true;
424 // static
425 bool ComponentExtensionIMEManagerImpl::ReadExtensionInfo(
426 const base::DictionaryValue& manifest,
427 const std::string& extension_id,
428 ComponentExtensionIME* out) {
429 if (!manifest.GetString(extensions::manifest_keys::kDescription,
430 &out->description))
431 return false;
432 std::string path;
433 if (manifest.GetString(kImePathKeyName, &path))
434 out->path = base::FilePath(path);
435 std::string url_string;
436 if (manifest.GetString(extensions::manifest_keys::kOptionsPage,
437 &url_string)) {
438 GURL url = extensions::Extension::GetResourceURL(
439 extensions::Extension::GetBaseURLFromExtensionId(extension_id),
440 url_string);
441 if (!url.is_valid())
442 return false;
443 out->options_page_url = url;
445 // It's okay to return true on no option page and/or input view page case.
446 return true;
449 // static
450 void ComponentExtensionIMEManagerImpl::ReadComponentExtensionsInfo(
451 std::vector<ComponentExtensionIME>* out_imes) {
452 DCHECK(out_imes);
453 for (size_t i = 0; i < arraysize(whitelisted_component_extension); ++i) {
454 ComponentExtensionIME component_ime;
455 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
456 component_ime.manifest =
457 rb.GetRawDataResource(
458 whitelisted_component_extension[i].manifest_resource_id)
459 .as_string();
460 if (component_ime.manifest.empty())
461 continue;
463 scoped_ptr<base::DictionaryValue> manifest =
464 GetManifest(component_ime.manifest);
465 if (!manifest.get())
466 continue;
468 if (!ReadExtensionInfo(*manifest.get(),
469 whitelisted_component_extension[i].id,
470 &component_ime))
471 continue;
472 component_ime.id = whitelisted_component_extension[i].id;
474 if (!component_ime.path.IsAbsolute()) {
475 base::FilePath resources_path;
476 if (!PathService::Get(chrome::DIR_RESOURCES, &resources_path))
477 NOTREACHED();
478 component_ime.path = resources_path.Append(component_ime.path);
481 const base::ListValue* component_list;
482 if (!manifest->GetList(extensions::manifest_keys::kInputComponents,
483 &component_list))
484 continue;
486 for (size_t i = 0; i < component_list->GetSize(); ++i) {
487 const base::DictionaryValue* dictionary;
488 if (!component_list->GetDictionary(i, &dictionary))
489 continue;
491 ComponentExtensionEngine engine;
492 ReadEngineComponent(component_ime, *dictionary, &engine);
493 component_ime.engines.push_back(engine);
495 out_imes->push_back(component_ime);
499 } // namespace chromeos