Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_util.cc
blobce9106bb169c3ff7ba28747219cd08cf8dcc8439
1 // Copyright (c) 2012 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/input_method_util.h"
7 #include <algorithm>
8 #include <functional>
9 #include <map>
10 #include <utility>
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/common/extensions/extension_constants.h"
20 // TODO(nona): move this header from this file.
21 #include "chrome/grit/generated_resources.h"
23 #include "ui/base/ime/chromeos/component_extension_ime_manager.h"
24 #include "ui/base/ime/chromeos/extension_ime_util.h"
26 // For SetHardwareKeyboardLayoutForTesting.
27 #include "ui/base/ime/chromeos/fake_input_method_delegate.h"
28 #include "ui/base/ime/chromeos/input_method_delegate.h"
29 #include "ui/base/ime/chromeos/input_method_whitelist.h"
31 namespace {
33 // A mapping from an input method id to a resource id for a
34 // medium length language indicator.
35 // For those languages that want to display a slightly longer text in the
36 // "Your input method has changed to..." bubble than in the status tray.
37 // If an entry is not found in this table the short name is used.
38 const struct {
39 const char* engine_id;
40 const int resource_id;
41 } kMappingImeIdToMediumLenNameResourceId[] = {
42 { "hangul_2set", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
43 { "hangul_3set390", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
44 { "hangul_3setfinal", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
45 { "hangul_3setnoshift", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
46 { "hangul_3setromaja", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
47 { "zh-t-i0-pinyin", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED},
48 { "zh-t-i0-wubi-1986", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED },
49 { "zh-hant-t-i0-und", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
50 { "zh-hant-t-i0-cangjie-1987",
51 IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
52 { "zh-hant-t-i0-cangjie-1987-x-m0-simplified",
53 IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
54 { extension_misc::kBrailleImeEngineId,
55 IDS_LANGUAGES_MEDIUM_LEN_NAME_BRAILLE },
57 const size_t kMappingImeIdToMediumLenNameResourceIdLen =
58 arraysize(kMappingImeIdToMediumLenNameResourceId);
60 // Due to asynchronous initialization of component extension manager,
61 // GetFirstLogingInputMethodIds may miss component extension IMEs. To enable
62 // component extension IME as the first loging input method, we have to prepare
63 // component extension IME IDs.
64 // Note: empty layout means the rule applies for all layouts.
65 const struct {
66 const char* locale;
67 const char* layout;
68 const char* engine_id;
69 } kDefaultInputMethodRecommendation[] = {
70 { "ja", "jp", "nacl_mozc_jp" },
71 { "ja", "", "nacl_mozc_us" },
72 { "zh-CN", "", "zh-t-i0-pinyin" },
73 { "zh-TW", "", "zh-hant-t-i0-und" },
74 { "th", "", "vkd_th" },
75 { "vi", "", "vkd_vi_tcvn" },
76 { "ru", "", "xkb:ru::rus" },
79 // The engine ID map for migration. This migration is for input method IDs from
80 // VPD so it's NOT a temporary migration.
81 const char* const kEngineIdMigrationMap[][2] = {
82 {"ime:jp:mozc_jp", "nacl_mozc_jp"},
83 {"ime:jp:mozc_us", "nacl_mozc_us"},
84 {"ime:ko:hangul_2set", "hangul_2set"},
85 {"ime:ko:hangul", "hangul_2set"},
86 {"ime:zh-t:array", "zh-hant-t-i0-array-1992"},
87 {"ime:zh-t:cangjie", "zh-hant-t-i0-cangjie-1987"},
88 {"ime:zh-t:dayi", "zh-hant-t-i0-dayi-1988"},
89 {"ime:zh-t:pinyin", "zh-hant-t-i0-pinyin"},
90 {"ime:zh-t:quick", "zh-hant-t-i0-cangjie-1987-x-m0-simplified"},
91 {"ime:zh-t:zhuyin", "zh-hant-t-i0-und"},
92 {"ime:zh:pinyin", "zh-t-i0-pinyin"},
93 {"ime:zh:wubi", "zh-t-i0-wubi-1986"},
94 {"m17n:", "vkd_"},
95 {"t13n:am", "am-t-i0-und"},
96 {"t13n:ar", "ar-t-i0-und"},
97 {"t13n:bn", "bn-t-i0-und"},
98 {"t13n:el", "el-t-i0-und"},
99 {"t13n:fa", "fa-t-i0-und"},
100 {"t13n:gu", "gu-t-i0-und"},
101 {"t13n:he", "he-t-i0-und"},
102 {"t13n:hi", "hi-t-i0-und"},
103 {"t13n:kn", "kn-t-i0-und"},
104 {"t13n:ml", "ml-t-i0-und"},
105 {"t13n:mr", "mr-t-i0-und"},
106 {"t13n:ne", "ne-t-i0-und"},
107 {"t13n:or", "or-t-i0-und"},
108 {"t13n:pa", "pa-t-i0-und"},
109 {"t13n:sa", "sa-t-i0-und"},
110 {"t13n:sr", "sr-t-i0-und"},
111 {"t13n:ta", "ta-t-i0-und"},
112 {"t13n:te", "te-t-i0-und"},
113 {"t13n:ti", "ti-t-i0-und"},
114 {"t13n:ur", "ur-t-i0-und"},
117 const struct EnglishToResouceId {
118 const char* english_string_from_ibus;
119 int resource_id;
120 } kEnglishToResourceIdArray[] = {
121 // For xkb-layouts.
122 { "xkb:am:phonetic:arm", IDS_STATUSBAR_LAYOUT_ARMENIAN_PHONETIC },
123 { "xkb:be::fra", IDS_STATUSBAR_LAYOUT_BELGIUM },
124 { "xkb:be::ger", IDS_STATUSBAR_LAYOUT_BELGIUM },
125 { "xkb:be::nld", IDS_STATUSBAR_LAYOUT_BELGIUM },
126 { "xkb:bg::bul", IDS_STATUSBAR_LAYOUT_BULGARIA },
127 { "xkb:bg:phonetic:bul", IDS_STATUSBAR_LAYOUT_BULGARIA_PHONETIC },
128 { "xkb:br::por", IDS_STATUSBAR_LAYOUT_BRAZIL },
129 { "xkb:by::bel", IDS_STATUSBAR_LAYOUT_BELARUSIAN },
130 { "xkb:ca::fra", IDS_STATUSBAR_LAYOUT_CANADA },
131 { "xkb:ca:eng:eng", IDS_STATUSBAR_LAYOUT_CANADA_ENGLISH },
132 { "xkb:ca:multix:fra", IDS_STATUSBAR_LAYOUT_CANADIAN_MULTILINGUAL },
133 { "xkb:ch::ger", IDS_STATUSBAR_LAYOUT_SWITZERLAND },
134 { "xkb:ch:fr:fra", IDS_STATUSBAR_LAYOUT_SWITZERLAND_FRENCH },
135 { "xkb:cz::cze", IDS_STATUSBAR_LAYOUT_CZECHIA },
136 { "xkb:cz:qwerty:cze", IDS_STATUSBAR_LAYOUT_CZECHIA_QWERTY },
137 { "xkb:de::ger", IDS_STATUSBAR_LAYOUT_GERMANY },
138 { "xkb:de:neo:ger", IDS_STATUSBAR_LAYOUT_GERMANY_NEO2 },
139 { "xkb:dk::dan", IDS_STATUSBAR_LAYOUT_DENMARK },
140 { "xkb:ee::est", IDS_STATUSBAR_LAYOUT_ESTONIA },
141 { "xkb:es::spa", IDS_STATUSBAR_LAYOUT_SPAIN },
142 { "xkb:es:cat:cat", IDS_STATUSBAR_LAYOUT_SPAIN_CATALAN },
143 { "xkb:fi::fin", IDS_STATUSBAR_LAYOUT_FINLAND },
144 { "xkb:fr::fra", IDS_STATUSBAR_LAYOUT_FRANCE },
145 { "xkb:gb:dvorak:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM_DVORAK },
146 { "xkb:gb:extd:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM },
147 { "xkb:ge::geo", IDS_STATUSBAR_LAYOUT_GEORGIAN },
148 { "xkb:gr::gre", IDS_STATUSBAR_LAYOUT_GREECE },
149 { "xkb:hr::scr", IDS_STATUSBAR_LAYOUT_CROATIA },
150 { "xkb:hu::hun", IDS_STATUSBAR_LAYOUT_HUNGARY },
151 { "xkb:ie::ga", IDS_STATUSBAR_LAYOUT_IRISH },
152 { "xkb:il::heb", IDS_STATUSBAR_LAYOUT_ISRAEL },
153 { "xkb:is::ice", IDS_STATUSBAR_LAYOUT_ICELANDIC },
154 { "xkb:it::ita", IDS_STATUSBAR_LAYOUT_ITALY },
155 { "xkb:jp::jpn", IDS_STATUSBAR_LAYOUT_JAPAN },
156 { "xkb:latam::spa", IDS_STATUSBAR_LAYOUT_LATIN_AMERICAN },
157 { "xkb:lt::lit", IDS_STATUSBAR_LAYOUT_LITHUANIA },
158 { "xkb:lv:apostrophe:lav", IDS_STATUSBAR_LAYOUT_LATVIA },
159 { "xkb:mn::mon", IDS_STATUSBAR_LAYOUT_MONGOLIAN },
160 { "xkb:nl::nld", IDS_STATUSBAR_LAYOUT_NETHERLANDS },
161 { "xkb:no::nob", IDS_STATUSBAR_LAYOUT_NORWAY },
162 { "xkb:pl::pol", IDS_STATUSBAR_LAYOUT_POLAND },
163 { "xkb:pt::por", IDS_STATUSBAR_LAYOUT_PORTUGAL },
164 { "xkb:ro::rum", IDS_STATUSBAR_LAYOUT_ROMANIA },
165 { "xkb:rs::srp", IDS_STATUSBAR_LAYOUT_SERBIA },
166 { "xkb:ru::rus", IDS_STATUSBAR_LAYOUT_RUSSIA },
167 { "xkb:ru:phonetic:rus", IDS_STATUSBAR_LAYOUT_RUSSIA_PHONETIC },
168 { "xkb:se::swe", IDS_STATUSBAR_LAYOUT_SWEDEN },
169 { "xkb:si::slv", IDS_STATUSBAR_LAYOUT_SLOVENIA },
170 { "xkb:sk::slo", IDS_STATUSBAR_LAYOUT_SLOVAKIA },
171 { "xkb:tr::tur", IDS_STATUSBAR_LAYOUT_TURKEY },
172 { "xkb:ua::ukr", IDS_STATUSBAR_LAYOUT_UKRAINE },
173 { "xkb:us::eng", IDS_STATUSBAR_LAYOUT_USA },
174 { "xkb:us::fil", IDS_STATUSBAR_LAYOUT_USA },
175 { "xkb:us::ind", IDS_STATUSBAR_LAYOUT_USA },
176 { "xkb:us::msa", IDS_STATUSBAR_LAYOUT_USA },
177 { "xkb:us:altgr-intl:eng", IDS_STATUSBAR_LAYOUT_USA_EXTENDED },
178 { "xkb:us:colemak:eng", IDS_STATUSBAR_LAYOUT_USA_COLEMAK },
179 { "xkb:us:dvorak:eng", IDS_STATUSBAR_LAYOUT_USA_DVORAK },
180 { "xkb:us:intl:eng", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
181 { "xkb:us:intl:nld", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
182 { "xkb:us:intl:por", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
184 const size_t kEnglishToResourceIdArraySize =
185 arraysize(kEnglishToResourceIdArray);
187 } // namespace
189 namespace chromeos {
191 namespace input_method {
193 InputMethodUtil::InputMethodUtil(InputMethodDelegate* delegate)
194 : delegate_(delegate) {
195 InputMethodDescriptors default_input_methods;
196 default_input_methods.push_back(GetFallbackInputMethodDescriptor());
197 ResetInputMethods(default_input_methods);
199 // Initialize a map from English string to Chrome string resource ID as well.
200 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) {
201 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i];
202 const bool result = english_to_resource_id_.insert(std::make_pair(
203 map_entry.english_string_from_ibus, map_entry.resource_id)).second;
204 DCHECK(result) << "Duplicated string is found: "
205 << map_entry.english_string_from_ibus;
209 InputMethodUtil::~InputMethodUtil() {
212 bool InputMethodUtil::TranslateStringInternal(
213 const std::string& english_string, base::string16 *out_string) const {
214 DCHECK(out_string);
215 // |english_string| could be an input method id. So legacy xkb id is required
216 // to get the translated string.
217 std::string key_string = extension_ime_util::MaybeGetLegacyXkbId(
218 english_string);
219 HashType::const_iterator iter = english_to_resource_id_.find(key_string);
221 if (iter == english_to_resource_id_.end()) {
222 // TODO(yusukes): Write Autotest which checks if all display names and all
223 // property names for supported input methods are listed in the resource
224 // ID array (crosbug.com/4572).
225 LOG(ERROR) << "Resource ID is not found for: " << english_string
226 << ", " << key_string;
227 return false;
230 *out_string = delegate_->GetLocalizedString(iter->second);
231 return true;
234 base::string16 InputMethodUtil::TranslateString(
235 const std::string& english_string) const {
236 base::string16 localized_string;
237 if (TranslateStringInternal(english_string, &localized_string)) {
238 return localized_string;
240 return base::UTF8ToUTF16(english_string);
243 bool InputMethodUtil::IsValidInputMethodId(
244 const std::string& input_method_id) const {
245 // We can't check the component extension is whilelisted or not here because
246 // it might not be initialized.
247 return GetInputMethodDescriptorFromId(input_method_id) != NULL ||
248 extension_ime_util::IsComponentExtensionIME(input_method_id);
251 // static
252 bool InputMethodUtil::IsKeyboardLayout(const std::string& input_method_id) {
253 return StartsWithASCII(input_method_id, "xkb:", false) ||
254 extension_ime_util::IsKeyboardLayoutExtension(input_method_id);
257 std::string InputMethodUtil::GetKeyboardLayoutName(
258 const std::string& input_method_id) const {
259 InputMethodIdToDescriptorMap::const_iterator iter
260 = id_to_descriptor_.find(input_method_id);
261 return (iter == id_to_descriptor_.end()) ?
262 "" : iter->second.GetPreferredKeyboardLayout();
265 std::string InputMethodUtil::GetInputMethodDisplayNameFromId(
266 const std::string& input_method_id) const {
267 base::string16 display_name;
268 if (!extension_ime_util::IsExtensionIME(input_method_id) &&
269 TranslateStringInternal(input_method_id, &display_name)) {
270 return base::UTF16ToUTF8(display_name);
272 // Return an empty string if the display name is not found.
273 return "";
276 base::string16 InputMethodUtil::GetInputMethodShortName(
277 const InputMethodDescriptor& input_method) const {
278 // TODO(shuchen): remove this method, as the client can directly use
279 // input_method.GetIndicator().
280 return base::UTF8ToUTF16(input_method.GetIndicator());
283 base::string16 InputMethodUtil::GetInputMethodMediumName(
284 const InputMethodDescriptor& input_method) const {
285 // For the "Your input method has changed to..." bubble. In most cases
286 // it uses the same name as the short name, unless found in a table
287 // for medium length names.
288 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
289 if (extension_ime_util::GetInputMethodIDByEngineID(
290 kMappingImeIdToMediumLenNameResourceId[i].engine_id) ==
291 input_method.id()) {
292 return delegate_->GetLocalizedString(
293 kMappingImeIdToMediumLenNameResourceId[i].resource_id);
296 return GetInputMethodShortName(input_method);
299 base::string16 InputMethodUtil::GetInputMethodLongNameInternal(
300 const InputMethodDescriptor& input_method, bool short_name) const {
301 if (!input_method.name().empty() && !IsKeyboardLayout(input_method.id())) {
302 // If the descriptor has a name, use it.
303 return base::UTF8ToUTF16(input_method.name());
306 // We don't show language here. Name of keyboard layout or input method
307 // usually imply (or explicitly include) its language.
308 // Special case for German, French and Dutch: these languages have multiple
309 // keyboard layouts and share the same layout of keyboard (Belgian). We need
310 // to show explicitly the language for the layout.
311 DCHECK(!input_method.language_codes().empty());
312 const std::string language_code = input_method.language_codes().at(0);
314 base::string16 text = (short_name || input_method.name().empty())
315 ? TranslateString(input_method.id())
316 : base::UTF8ToUTF16(input_method.name());
317 if (language_code == "de" || language_code == "fr" || language_code == "nl") {
318 const base::string16 language_name = delegate_->GetDisplayLanguageName(
319 language_code);
320 text = language_name + base::UTF8ToUTF16(" - ") + text;
323 DCHECK(!text.empty());
324 return text;
328 base::string16 InputMethodUtil::GetInputMethodLongNameStripped(
329 const InputMethodDescriptor& input_method) const {
330 return GetInputMethodLongNameInternal(input_method, true /* short_name */);
333 base::string16 InputMethodUtil::GetInputMethodLongName(
334 const InputMethodDescriptor& input_method) const {
335 return GetInputMethodLongNameInternal(input_method, false /* short_name */);
338 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId(
339 const std::string& input_method_id) const {
340 InputMethodIdToDescriptorMap::const_iterator iter =
341 id_to_descriptor_.find(input_method_id);
342 if (iter == id_to_descriptor_.end())
343 return NULL;
344 return &(iter->second);
347 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode(
348 const std::string& normalized_language_code,
349 InputMethodType type,
350 std::vector<std::string>* out_input_method_ids) const {
351 return GetInputMethodIdsFromLanguageCodeInternal(
352 language_code_to_ids_,
353 normalized_language_code, type, out_input_method_ids);
356 bool InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal(
357 const std::multimap<std::string, std::string>& language_code_to_ids,
358 const std::string& normalized_language_code,
359 InputMethodType type,
360 std::vector<std::string>* out_input_method_ids) const {
361 DCHECK(out_input_method_ids);
362 out_input_method_ids->clear();
364 bool result = false;
365 std::pair<LanguageCodeToIdsMap::const_iterator,
366 LanguageCodeToIdsMap::const_iterator> range =
367 language_code_to_ids.equal_range(normalized_language_code);
368 for (LanguageCodeToIdsMap::const_iterator iter = range.first;
369 iter != range.second; ++iter) {
370 const std::string& input_method_id = iter->second;
371 if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) {
372 out_input_method_ids->push_back(input_method_id);
373 result = true;
376 if ((type == kAllInputMethods) && !result) {
377 DVLOG(1) << "Unknown language code: " << normalized_language_code;
379 return result;
382 void InputMethodUtil::GetFirstLoginInputMethodIds(
383 const std::string& language_code,
384 const InputMethodDescriptor& current_input_method,
385 std::vector<std::string>* out_input_method_ids) const {
386 out_input_method_ids->clear();
388 // First, add the current keyboard layout (one used on the login screen).
389 out_input_method_ids->push_back(current_input_method.id());
391 const std::string current_layout
392 = current_input_method.GetPreferredKeyboardLayout();
393 for (size_t i = 0; i < arraysize(kDefaultInputMethodRecommendation);
394 ++i) {
395 if (kDefaultInputMethodRecommendation[i].locale == language_code && (
396 !kDefaultInputMethodRecommendation[i].layout[0] ||
397 kDefaultInputMethodRecommendation[i].layout == current_layout)) {
398 out_input_method_ids->push_back(
399 extension_ime_util::GetInputMethodIDByEngineID(
400 kDefaultInputMethodRecommendation[i].engine_id));
401 return;
405 // Second, find the most popular input method associated with the
406 // current UI language. The input method IDs returned from
407 // GetInputMethodIdsFromLanguageCode() are sorted by popularity, hence
408 // our basic strategy is to pick the first one, but it's a bit more
409 // complicated as shown below.
410 std::string most_popular_id;
411 std::vector<std::string> input_method_ids;
412 // This returns the input methods sorted by popularity.
413 GetInputMethodIdsFromLanguageCode(
414 language_code, kAllInputMethods, &input_method_ids);
415 for (size_t i = 0; i < input_method_ids.size(); ++i) {
416 const std::string& input_method_id = input_method_ids[i];
417 // Pick the first one.
418 if (most_popular_id.empty())
419 most_popular_id = input_method_id;
421 // Check if there is one that matches the current keyboard layout, but
422 // not the current keyboard itself. This is useful if there are
423 // multiple keyboard layout choices for one input method. For
424 // instance, Mozc provides three choices: mozc (US keyboard), mozc-jp
425 // (JP keyboard), mozc-dv (Dvorak).
426 const InputMethodDescriptor* descriptor =
427 GetInputMethodDescriptorFromId(input_method_id);
428 if (descriptor &&
429 descriptor->id() != current_input_method.id() &&
430 descriptor->GetPreferredKeyboardLayout() ==
431 current_input_method.GetPreferredKeyboardLayout()) {
432 most_popular_id = input_method_id;
433 break;
436 // Add the most popular input method ID, if it's different from the
437 // current input method.
438 if (most_popular_id != current_input_method.id()) {
439 out_input_method_ids->push_back(most_popular_id);
443 void InputMethodUtil::GetLanguageCodesFromInputMethodIds(
444 const std::vector<std::string>& input_method_ids,
445 std::vector<std::string>* out_language_codes) const {
446 out_language_codes->clear();
448 for (size_t i = 0; i < input_method_ids.size(); ++i) {
449 const std::string& input_method_id = input_method_ids[i];
450 const InputMethodDescriptor* input_method =
451 GetInputMethodDescriptorFromId(input_method_id);
452 if (!input_method) {
453 DVLOG(1) << "Unknown input method ID: " << input_method_ids[i];
454 continue;
456 DCHECK(!input_method->language_codes().empty());
457 const std::string language_code = input_method->language_codes().at(0);
458 // Add it if it's not already present.
459 if (std::count(out_language_codes->begin(), out_language_codes->end(),
460 language_code) == 0) {
461 out_language_codes->push_back(language_code);
466 std::string InputMethodUtil::GetLanguageDefaultInputMethodId(
467 const std::string& language_code) {
468 std::vector<std::string> candidates;
469 GetInputMethodIdsFromLanguageCode(
470 language_code, input_method::kKeyboardLayoutsOnly, &candidates);
471 if (candidates.size())
472 return candidates.front();
474 return std::string();
477 std::string InputMethodUtil::MigrateInputMethod(
478 const std::string& input_method_id) {
479 std::string engine_id = input_method_id;
480 // Migrates some Engine IDs from VPD.
481 for (size_t j = 0; j < arraysize(kEngineIdMigrationMap); ++j) {
482 size_t pos = engine_id.find(kEngineIdMigrationMap[j][0]);
483 if (pos == 0) {
484 engine_id.replace(0,
485 strlen(kEngineIdMigrationMap[j][0]),
486 kEngineIdMigrationMap[j][1]);
487 break;
490 // Migrates the extension IDs.
491 std::string id =
492 extension_ime_util::GetInputMethodIDByEngineID(engine_id);
493 if (extension_ime_util::IsComponentExtensionIME(id)) {
494 std::string id_new = extension_ime_util::GetInputMethodIDByEngineID(
495 extension_ime_util::GetComponentIDByInputMethodID(id));
496 if (extension_ime_util::IsComponentExtensionIME(id_new))
497 id = id_new;
499 return id;
502 bool InputMethodUtil::MigrateInputMethods(
503 std::vector<std::string>* input_method_ids) {
504 bool rewritten = false;
505 std::vector<std::string>& ids = *input_method_ids;
506 for (size_t i = 0; i < ids.size(); ++i) {
507 std::string id = MigrateInputMethod(ids[i]);
508 if (id != ids[i]) {
509 ids[i] = id;
510 rewritten = true;
513 if (rewritten) {
514 // Removes the duplicates.
515 std::vector<std::string> new_ids;
516 for (size_t i = 0; i < ids.size(); ++i) {
517 if (std::find(new_ids.begin(), new_ids.end(), ids[i]) == new_ids.end())
518 new_ids.push_back(ids[i]);
520 ids.swap(new_ids);
522 return rewritten;
525 void InputMethodUtil::UpdateHardwareLayoutCache() {
526 DCHECK(thread_checker_.CalledOnValidThread());
527 hardware_layouts_.clear();
528 hardware_login_layouts_.clear();
529 if (cached_hardware_layouts_.empty())
530 Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",",
531 &cached_hardware_layouts_);
532 hardware_layouts_ = cached_hardware_layouts_;
533 MigrateInputMethods(&hardware_layouts_);
535 for (size_t i = 0; i < hardware_layouts_.size(); ++i) {
536 if (IsLoginKeyboard(hardware_layouts_[i]))
537 hardware_login_layouts_.push_back(hardware_layouts_[i]);
540 if (hardware_login_layouts_.empty()) {
541 // This is totally fine if |hardware_layouts_| is empty. The hardware
542 // keyboard layout is not stored if startup_manifest.json
543 // (OEM customization data) is not present (ex. Cr48 doen't have that file).
544 // So need to make sure |hardware_login_layouts_| is not empty, and
545 // |hardware_layouts_| contains at least one login layout.
546 std::string fallback_id = GetFallbackInputMethodDescriptor().id();
547 hardware_layouts_.insert(hardware_layouts_.begin(), fallback_id);
548 hardware_login_layouts_.push_back(fallback_id);
552 void InputMethodUtil::SetHardwareKeyboardLayoutForTesting(
553 const std::string& layout) {
554 delegate_->SetHardwareKeyboardLayoutForTesting(layout);
555 cached_hardware_layouts_.clear();
556 UpdateHardwareLayoutCache();
559 const std::vector<std::string>&
560 InputMethodUtil::GetHardwareInputMethodIds() {
561 DCHECK(thread_checker_.CalledOnValidThread());
562 UpdateHardwareLayoutCache();
563 return hardware_layouts_;
566 const std::vector<std::string>&
567 InputMethodUtil::GetHardwareLoginInputMethodIds() {
568 DCHECK(thread_checker_.CalledOnValidThread());
569 UpdateHardwareLayoutCache();
570 return hardware_login_layouts_;
573 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id)
574 const {
575 const InputMethodDescriptor* ime =
576 GetInputMethodDescriptorFromId(input_method_id);
577 return ime ? ime->is_login_keyboard() : false;
580 void InputMethodUtil::AppendInputMethods(const InputMethodDescriptors& imes) {
581 for (size_t i = 0; i < imes.size(); ++i) {
582 const InputMethodDescriptor& input_method = imes[i];
583 DCHECK(!input_method.language_codes().empty());
584 const std::vector<std::string>& language_codes =
585 input_method.language_codes();
586 id_to_descriptor_[input_method.id()] = input_method;
588 typedef LanguageCodeToIdsMap::const_iterator It;
589 for (size_t j = 0; j < language_codes.size(); ++j) {
590 std::pair<It, It> range =
591 language_code_to_ids_.equal_range(language_codes[j]);
592 It it = range.first;
593 for (; it != range.second; ++it) {
594 if (it->second == input_method.id())
595 break;
597 if (it == range.second)
598 language_code_to_ids_.insert(
599 std::make_pair(language_codes[j], input_method.id()));
604 void InputMethodUtil::ResetInputMethods(const InputMethodDescriptors& imes) {
605 // Clear the existing maps.
606 language_code_to_ids_.clear();
607 id_to_descriptor_.clear();
609 AppendInputMethods(imes);
612 void InputMethodUtil::InitXkbInputMethodsForTesting() {
613 cached_hardware_layouts_.clear();
614 ResetInputMethods(*(InputMethodWhitelist().GetSupportedInputMethods()));
617 const InputMethodUtil::InputMethodIdToDescriptorMap&
618 InputMethodUtil::GetIdToDesciptorMapForTesting() {
619 return id_to_descriptor_;
622 InputMethodDescriptor InputMethodUtil::GetFallbackInputMethodDescriptor() {
623 std::vector<std::string> layouts;
624 layouts.push_back("us");
625 std::vector<std::string> languages;
626 languages.push_back("en-US");
627 return InputMethodDescriptor(
628 extension_ime_util::GetInputMethodIDByEngineID("xkb:us::eng"),
630 "US",
631 layouts,
632 languages,
633 true, // login keyboard.
634 GURL(), // options page, not available.
635 GURL()); // input view page, not available.
638 } // namespace input_method
639 } // namespace chromeos