Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / input_method / input_method_util.cc
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.
4
5 #include "chrome/browser/chromeos/input_method/input_method_util.h"
6
7 #include <algorithm>
8 #include <functional>
9 #include <map>
10 #include <utility>
11
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"
19 // TODO(nona): move this header from this file.
20 #include "chrome/grit/generated_resources.h"
21 #include "chromeos/ime/component_extension_ime_manager.h"
22 #include "chromeos/ime/extension_ime_util.h"
23 // For SetHardwareKeyboardLayoutForTesting.
24 #include "chromeos/ime/fake_input_method_delegate.h"
25 #include "chromeos/ime/input_method_delegate.h"
26 #include "chromeos/ime/input_method_whitelist.h"
27
28 namespace {
29
30 // A mapping from an input method id to a string for the language indicator. The
31 // mapping is necessary since some input methods belong to the same language.
32 // For example, both "xkb:us::eng" and "xkb:us:dvorak:eng" are for US English.
33 const struct {
34   const char* engine_id;
35   const char* indicator_text;
36 } kMappingFromIdToIndicatorText[] = {
37   // To distinguish from "xkb:jp::jpn"
38   // TODO(nona): Make following variables configurable. http://crbug.com/232260.
39   { "nacl_mozc_us", "\xe3\x81\x82" },
40   { "nacl_mozc_jp", "\xe3\x81\x82" },
41   // For simplified Chinese input methods
42   { "zh-t-i0-pinyin", "\xe6\x8b\xbc" },  // U+62FC
43   { "zh-t-i0-wubi-1986", "\xe4\xba\x94" }, // U+4E94
44   // For traditional Chinese input methods
45   { "zh-hant-t-i0-und", "\xE6\xB3\xA8" },  // U+9177
46   { "zh-hant-t-i0-cangjie-1987", "\xe5\x80\x89" },  // U+5009
47   { "zh-hant-t-i0-cangjie-1987-x-m0-simplified", "\xe9\x80\x9f" },  // U+901F
48   // For Hangul input method.
49   { "hangul_2set", "\xed\x95\x9c" },  // U+D55C
50   { "hangul_3set390", "\xed\x95\x9c" },  // U+D55C
51   { "hangul_3setfinal", "\xed\x95\x9c" },  // U+D55C
52   { "hangul_3setnoshift", "\xed\x95\x9c" },  // U+D55C
53   { "hangul_romaja", "\xed\x95\x9c" },  // U+D55C
54   { extension_misc::kBrailleImeEngineId,
55     // U+2803 U+2817 U+2807 (Unicode braille patterns for the letters 'brl' in
56     // English (and many other) braille codes.
57     "\xe2\xa0\x83\xe2\xa0\x97\xe2\xa0\x87" },
58 };
59
60 const size_t kMappingFromIdToIndicatorTextLen =
61     ARRAYSIZE_UNSAFE(kMappingFromIdToIndicatorText);
62
63 // A mapping from an input method id to a resource id for a
64 // medium length language indicator.
65 // For those languages that want to display a slightly longer text in the
66 // "Your input method has changed to..." bubble than in the status tray.
67 // If an entry is not found in this table the short name is used.
68 const struct {
69   const char* engine_id;
70   const int resource_id;
71 } kMappingImeIdToMediumLenNameResourceId[] = {
72   { "hangul_2set", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
73   { "hangul_3set390", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
74   { "hangul_3setfinal", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
75   { "hangul_3setnoshift", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
76   { "hangul_3setromaja", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
77   { "zh-t-i0-pinyin", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED},
78   { "zh-t-i0-wubi-1986", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED },
79   { "zh-hant-t-i0-und", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
80   { "zh-hant-t-i0-cangjie-1987",
81     IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
82   { "zh-hant-t-i0-cangjie-1987-x-m0-simplified",
83     IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
84   { extension_misc::kBrailleImeEngineId,
85     IDS_LANGUAGES_MEDIUM_LEN_NAME_BRAILLE },
86 };
87 const size_t kMappingImeIdToMediumLenNameResourceIdLen =
88     ARRAYSIZE_UNSAFE(kMappingImeIdToMediumLenNameResourceId);
89
90 // Due to asynchronous initialization of component extension manager,
91 // GetFirstLogingInputMethodIds may miss component extension IMEs. To enable
92 // component extension IME as the first loging input method, we have to prepare
93 // component extension IME IDs.
94 const struct {
95   const char* locale;
96   const char* layout;
97   const char* engine_id;
98 } kDefaultInputMethodRecommendation[] = {
99   { "ja", "us", "nacl_mozc_us" },
100   { "ja", "jp", "nacl_mozc_jp" },
101   { "zh-CN", "us", "zh-t-i0-pinyin" },
102   { "zh-TW", "us", "zh-hant-t-i0-und" },
103   { "th", "us", "vkd_th" },
104   { "vi", "us", "vkd_vi_tcvn" },
105 };
106
107 // The map from xkb layout to the indicator text.
108 // Refer to crbug.com/349829.
109 const char* const kXkbIndicators[][2] = {{"am", "AM"},
110                                          {"be", "BE"},
111                                          {"bg", "BG"},
112                                          {"bg(phonetic)", "BG"},
113                                          {"br", "BR"},
114                                          {"by", "BY"},
115                                          {"ca", "CA"},
116                                          {"ca(eng)", "CA"},
117                                          {"ca(multix)", "CA"},
118                                          {"ch", "CH"},
119                                          {"ch(fr)", "CH"},
120                                          {"cz", "CZ"},
121                                          {"cz(qwerty)", "CS"},
122                                          {"de", "DE"},
123                                          {"de(neo)", "NEO"},
124                                          {"dk", "DK"},
125                                          {"ee", "EE"},
126                                          {"es", "ES"},
127                                          {"es(cat)", "CAS"},
128                                          {"fi", "FI"},
129                                          {"fr", "FR"},
130                                          {"gb(dvorak)", "DV"},
131                                          {"gb(extd)", "GB"},
132                                          {"ge", "GE"},
133                                          {"gr", "GR"},
134                                          {"hr", "HR"},
135                                          {"hu", "HU"},
136                                          {"il", "IL"},
137                                          {"is", "IS"},
138                                          {"it", "IT"},
139                                          {"jp", "JA"},
140                                          {"latam", "LA"},
141                                          {"lt", "LT"},
142                                          {"lv(apostrophe)", "LV"},
143                                          {"mn", "MN"},
144                                          {"no", "NO"},
145                                          {"pl", "PL"},
146                                          {"pt", "PT"},
147                                          {"ro", "RO"},
148                                          {"rs", "RS"},
149                                          {"ru", "RU"},
150                                          {"ru(phonetic)", "RU"},
151                                          {"se", "SE"},
152                                          {"si", "SI"},
153                                          {"sk", "SK"},
154                                          {"tr", "TR"},
155                                          {"ua", "UA"},
156                                          {"us", "US"},
157                                          {"us(altgr-intl)", "EXTD"},
158                                          {"us(colemak)", "CO"},
159                                          {"us(dvorak)", "DV"},
160                                          {"us(intl)", "INTL"}, };
161
162 // The extension ID map for migration.
163 const char* const kExtensionIdMigrationMap[][2] = {
164   // Official Japanese IME extension ID.
165   {"fpfbhcjppmaeaijcidgiibchfbnhbelj", "gjaehgfemfahhmlgpdfknkhdnemmolop"},
166   // Official M17n keyboard extension ID.
167   {"habcdindjejkmepknlhkkloncjcpcnbf", "gjaehgfemfahhmlgpdfknkhdnemmolop"},
168 };
169
170 // The engine ID map for migration. This migration is for input method IDs from
171 // VPD so it's NOT a temporary migration.
172 const char* const kEngineIdMigrationMap[][2] = {
173   {"m17n:", "vkd_"},
174   {"ime:zh-t:quick", "zh-hant-t-i0-cangjie-1987-x-m0-simplified"},
175   {"ime:zh-t:zhuyin", "zh-hant-t-i0-und"},
176   {"ime:ko:hangul", "hangul_2set"},
177   {"ime:ko:hangul_2set", "hangul_2set"},
178 };
179
180 const size_t kExtensionIdLen = 32;
181
182 const struct EnglishToResouceId {
183   const char* english_string_from_ibus;
184   int resource_id;
185 } kEnglishToResourceIdArray[] = {
186   // For xkb-layouts.
187   { "xkb:am:phonetic:arm", IDS_STATUSBAR_LAYOUT_ARMENIAN_PHONETIC },
188   { "xkb:be::fra", IDS_STATUSBAR_LAYOUT_BELGIUM },
189   { "xkb:be::ger", IDS_STATUSBAR_LAYOUT_BELGIUM },
190   { "xkb:be::nld", IDS_STATUSBAR_LAYOUT_BELGIUM },
191   { "xkb:bg::bul", IDS_STATUSBAR_LAYOUT_BULGARIA },
192   { "xkb:bg:phonetic:bul", IDS_STATUSBAR_LAYOUT_BULGARIA_PHONETIC },
193   { "xkb:br::por", IDS_STATUSBAR_LAYOUT_BRAZIL },
194   { "xkb:by::bel", IDS_STATUSBAR_LAYOUT_BELARUSIAN },
195   { "xkb:ca::fra", IDS_STATUSBAR_LAYOUT_CANADA },
196   { "xkb:ca:eng:eng", IDS_STATUSBAR_LAYOUT_CANADA_ENGLISH },
197   { "xkb:ca:multix:fra", IDS_STATUSBAR_LAYOUT_CANADIAN_MULTILINGUAL },
198   { "xkb:ch::ger", IDS_STATUSBAR_LAYOUT_SWITZERLAND },
199   { "xkb:ch:fr:fra", IDS_STATUSBAR_LAYOUT_SWITZERLAND_FRENCH },
200   { "xkb:cz::cze", IDS_STATUSBAR_LAYOUT_CZECHIA },
201   { "xkb:cz:qwerty:cze", IDS_STATUSBAR_LAYOUT_CZECHIA_QWERTY },
202   { "xkb:de::ger", IDS_STATUSBAR_LAYOUT_GERMANY },
203   { "xkb:de:neo:ger", IDS_STATUSBAR_LAYOUT_GERMANY_NEO2 },
204   { "xkb:dk::dan", IDS_STATUSBAR_LAYOUT_DENMARK },
205   { "xkb:ee::est", IDS_STATUSBAR_LAYOUT_ESTONIA },
206   { "xkb:es::spa", IDS_STATUSBAR_LAYOUT_SPAIN },
207   { "xkb:es:cat:cat", IDS_STATUSBAR_LAYOUT_SPAIN_CATALAN },
208   { "xkb:fi::fin", IDS_STATUSBAR_LAYOUT_FINLAND },
209   { "xkb:fr::fra", IDS_STATUSBAR_LAYOUT_FRANCE },
210   { "xkb:gb:dvorak:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM_DVORAK },
211   { "xkb:gb:extd:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM },
212   { "xkb:ge::geo", IDS_STATUSBAR_LAYOUT_GEORGIAN },
213   { "xkb:gr::gre", IDS_STATUSBAR_LAYOUT_GREECE },
214   { "xkb:hr::scr", IDS_STATUSBAR_LAYOUT_CROATIA },
215   { "xkb:hu::hun", IDS_STATUSBAR_LAYOUT_HUNGARY },
216   { "xkb:ie::ga", IDS_STATUSBAR_LAYOUT_IRISH },
217   { "xkb:il::heb", IDS_STATUSBAR_LAYOUT_ISRAEL },
218   { "xkb:is::ice", IDS_STATUSBAR_LAYOUT_ICELANDIC },
219   { "xkb:it::ita", IDS_STATUSBAR_LAYOUT_ITALY },
220   { "xkb:jp::jpn", IDS_STATUSBAR_LAYOUT_JAPAN },
221   { "xkb:latam::spa", IDS_STATUSBAR_LAYOUT_LATIN_AMERICAN },
222   { "xkb:lt::lit", IDS_STATUSBAR_LAYOUT_LITHUANIA },
223   { "xkb:lv:apostrophe:lav", IDS_STATUSBAR_LAYOUT_LATVIA },
224   { "xkb:mn::mon", IDS_STATUSBAR_LAYOUT_MONGOLIAN },
225   { "xkb:nl::nld", IDS_STATUSBAR_LAYOUT_NETHERLANDS },
226   { "xkb:no::nob", IDS_STATUSBAR_LAYOUT_NORWAY },
227   { "xkb:pl::pol", IDS_STATUSBAR_LAYOUT_POLAND },
228   { "xkb:pt::por", IDS_STATUSBAR_LAYOUT_PORTUGAL },
229   { "xkb:ro::rum", IDS_STATUSBAR_LAYOUT_ROMANIA },
230   { "xkb:rs::srp", IDS_STATUSBAR_LAYOUT_SERBIA },
231   { "xkb:ru::rus", IDS_STATUSBAR_LAYOUT_RUSSIA },
232   { "xkb:ru:phonetic:rus", IDS_STATUSBAR_LAYOUT_RUSSIA_PHONETIC },
233   { "xkb:se::swe", IDS_STATUSBAR_LAYOUT_SWEDEN },
234   { "xkb:si::slv", IDS_STATUSBAR_LAYOUT_SLOVENIA },
235   { "xkb:sk::slo", IDS_STATUSBAR_LAYOUT_SLOVAKIA },
236   { "xkb:tr::tur", IDS_STATUSBAR_LAYOUT_TURKEY },
237   { "xkb:ua::ukr", IDS_STATUSBAR_LAYOUT_UKRAINE },
238   { "xkb:us::eng", IDS_STATUSBAR_LAYOUT_USA },
239   { "xkb:us::fil", IDS_STATUSBAR_LAYOUT_USA },
240   { "xkb:us::ind", IDS_STATUSBAR_LAYOUT_USA },
241   { "xkb:us::msa", IDS_STATUSBAR_LAYOUT_USA },
242   { "xkb:us:altgr-intl:eng", IDS_STATUSBAR_LAYOUT_USA_EXTENDED },
243   { "xkb:us:colemak:eng", IDS_STATUSBAR_LAYOUT_USA_COLEMAK },
244   { "xkb:us:dvorak:eng", IDS_STATUSBAR_LAYOUT_USA_DVORAK },
245   { "xkb:us:intl:eng", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
246   { "xkb:us:intl:nld", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
247   { "xkb:us:intl:por", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
248 };
249 const size_t kEnglishToResourceIdArraySize =
250     arraysize(kEnglishToResourceIdArray);
251
252 }  // namespace
253
254 namespace chromeos {
255
256 namespace input_method {
257
258 InputMethodUtil::InputMethodUtil(InputMethodDelegate* delegate)
259     : delegate_(delegate) {
260   InputMethodDescriptors default_input_methods;
261   default_input_methods.push_back(GetFallbackInputMethodDescriptor());
262   ResetInputMethods(default_input_methods);
263
264   // Initialize a map from English string to Chrome string resource ID as well.
265   for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) {
266     const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i];
267     const bool result = english_to_resource_id_.insert(std::make_pair(
268         map_entry.english_string_from_ibus, map_entry.resource_id)).second;
269     DCHECK(result) << "Duplicated string is found: "
270                    << map_entry.english_string_from_ibus;
271   }
272
273   // Initialize the map from xkb layout to indicator text.
274   for (size_t i = 0; i < arraysize(kXkbIndicators); ++i) {
275     xkb_layout_to_indicator_[kXkbIndicators[i][0]] = kXkbIndicators[i][1];
276   }
277 }
278
279 InputMethodUtil::~InputMethodUtil() {
280 }
281
282 bool InputMethodUtil::TranslateStringInternal(
283     const std::string& english_string, base::string16 *out_string) const {
284   DCHECK(out_string);
285   // |english_string| could be an input method id. So legacy xkb id is required
286   // to get the translated string.
287   std::string key_string = extension_ime_util::MaybeGetLegacyXkbId(
288       english_string);
289   HashType::const_iterator iter = english_to_resource_id_.find(key_string);
290
291   if (iter == english_to_resource_id_.end()) {
292     // TODO(yusukes): Write Autotest which checks if all display names and all
293     // property names for supported input methods are listed in the resource
294     // ID array (crosbug.com/4572).
295     LOG(ERROR) << "Resource ID is not found for: " << english_string
296                << ", " << key_string;
297     return false;
298   }
299
300   *out_string = delegate_->GetLocalizedString(iter->second);
301   return true;
302 }
303
304 base::string16 InputMethodUtil::TranslateString(
305     const std::string& english_string) const {
306   base::string16 localized_string;
307   if (TranslateStringInternal(english_string, &localized_string)) {
308     return localized_string;
309   }
310   return base::UTF8ToUTF16(english_string);
311 }
312
313 bool InputMethodUtil::IsValidInputMethodId(
314     const std::string& input_method_id) const {
315   // We can't check the component extension is whilelisted or not here because
316   // it might not be initialized.
317   return GetInputMethodDescriptorFromId(input_method_id) != NULL ||
318       extension_ime_util::IsComponentExtensionIME(input_method_id);
319 }
320
321 // static
322 bool InputMethodUtil::IsKeyboardLayout(const std::string& input_method_id) {
323   return StartsWithASCII(input_method_id, "xkb:", false) ||
324       extension_ime_util::IsKeyboardLayoutExtension(input_method_id);
325 }
326
327 std::string InputMethodUtil::GetKeyboardLayoutName(
328     const std::string& input_method_id) const {
329   InputMethodIdToDescriptorMap::const_iterator iter
330       = id_to_descriptor_.find(input_method_id);
331   return (iter == id_to_descriptor_.end()) ?
332       "" : iter->second.GetPreferredKeyboardLayout();
333 }
334
335 std::string InputMethodUtil::GetInputMethodDisplayNameFromId(
336     const std::string& input_method_id) const {
337   base::string16 display_name;
338   if (!extension_ime_util::IsExtensionIME(input_method_id) &&
339       TranslateStringInternal(input_method_id, &display_name)) {
340     return base::UTF16ToUTF8(display_name);
341   }
342   // Return an empty string if the display name is not found.
343   return "";
344 }
345
346 base::string16 InputMethodUtil::GetInputMethodShortName(
347     const InputMethodDescriptor& input_method) const {
348   // For the status area, we use two-letter, upper-case language code like
349   // "US" and "JP".
350
351   // Use the indicator string if set.
352   if (!input_method.indicator().empty()) {
353     return base::UTF8ToUTF16(input_method.indicator());
354   }
355
356   base::string16 text;
357   // Check special cases first.
358   for (size_t i = 0; i < kMappingFromIdToIndicatorTextLen; ++i) {
359     if (extension_ime_util::GetInputMethodIDByEngineID(
360         kMappingFromIdToIndicatorText[i].engine_id) == input_method.id()) {
361       text = base::UTF8ToUTF16(kMappingFromIdToIndicatorText[i].indicator_text);
362       break;
363     }
364   }
365
366   // Display the keyboard layout name when using a keyboard layout.
367   if (text.empty() && IsKeyboardLayout(input_method.id())) {
368     std::map<std::string, std::string>::const_iterator it =
369         xkb_layout_to_indicator_.find(GetKeyboardLayoutName(input_method.id()));
370     if (it != xkb_layout_to_indicator_.end())
371       text = base::UTF8ToUTF16(it->second);
372   }
373
374   // TODO(yusukes): Some languages have two or more input methods. For example,
375   // Thai has 3, Vietnamese has 4. If these input methods could be activated at
376   // the same time, we should do either of the following:
377   //   (1) Add mappings to |kMappingFromIdToIndicatorText|
378   //   (2) Add suffix (1, 2, ...) to |text| when ambiguous.
379
380   if (text.empty()) {
381     const size_t kMaxLanguageNameLen = 2;
382     DCHECK(!input_method.language_codes().empty());
383     const std::string language_code = input_method.language_codes().at(0);
384     text = StringToUpperASCII(base::UTF8ToUTF16(language_code)).substr(
385         0, kMaxLanguageNameLen);
386   }
387   DCHECK(!text.empty()) << input_method.id();
388   return text;
389 }
390
391 base::string16 InputMethodUtil::GetInputMethodMediumName(
392     const InputMethodDescriptor& input_method) const {
393   // For the "Your input method has changed to..." bubble. In most cases
394   // it uses the same name as the short name, unless found in a table
395   // for medium length names.
396   for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
397     if (extension_ime_util::GetInputMethodIDByEngineID(
398         kMappingImeIdToMediumLenNameResourceId[i].engine_id) ==
399         input_method.id()) {
400       return delegate_->GetLocalizedString(
401           kMappingImeIdToMediumLenNameResourceId[i].resource_id);
402     }
403   }
404   return GetInputMethodShortName(input_method);
405 }
406
407 base::string16 InputMethodUtil::GetInputMethodLongName(
408     const InputMethodDescriptor& input_method) const {
409   if (!input_method.name().empty() && !IsKeyboardLayout(input_method.id())) {
410     // If the descriptor has a name, use it.
411     return base::UTF8ToUTF16(input_method.name());
412   }
413
414   // We don't show language here.  Name of keyboard layout or input method
415   // usually imply (or explicitly include) its language.
416
417   // Special case for German, French and Dutch: these languages have multiple
418   // keyboard layouts and share the same layout of keyboard (Belgian). We need
419   // to show explicitly the language for the layout. For Arabic, Amharic, and
420   // Indic languages: they share "Standard Input Method".
421   const base::string16 standard_input_method_text =
422       delegate_->GetLocalizedString(
423           IDS_OPTIONS_SETTINGS_LANGUAGES_M17N_STANDARD_INPUT_METHOD);
424   DCHECK(!input_method.language_codes().empty());
425   const std::string language_code = input_method.language_codes().at(0);
426
427   base::string16 text = TranslateString(input_method.id());
428   if (text == standard_input_method_text ||
429              language_code == "de" ||
430              language_code == "fr" ||
431              language_code == "nl") {
432     const base::string16 language_name = delegate_->GetDisplayLanguageName(
433         language_code);
434
435     text = language_name + base::UTF8ToUTF16(" - ") + text;
436   }
437
438   DCHECK(!text.empty());
439   return text;
440 }
441
442 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId(
443     const std::string& input_method_id) const {
444   InputMethodIdToDescriptorMap::const_iterator iter =
445       id_to_descriptor_.find(input_method_id);
446   if (iter == id_to_descriptor_.end())
447     return NULL;
448   return &(iter->second);
449 }
450
451 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode(
452     const std::string& normalized_language_code,
453     InputMethodType type,
454     std::vector<std::string>* out_input_method_ids) const {
455   return GetInputMethodIdsFromLanguageCodeInternal(
456       language_code_to_ids_,
457       normalized_language_code, type, out_input_method_ids);
458 }
459
460 bool InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal(
461     const std::multimap<std::string, std::string>& language_code_to_ids,
462     const std::string& normalized_language_code,
463     InputMethodType type,
464     std::vector<std::string>* out_input_method_ids) const {
465   DCHECK(out_input_method_ids);
466   out_input_method_ids->clear();
467
468   bool result = false;
469   std::pair<LanguageCodeToIdsMap::const_iterator,
470       LanguageCodeToIdsMap::const_iterator> range =
471       language_code_to_ids.equal_range(normalized_language_code);
472   for (LanguageCodeToIdsMap::const_iterator iter = range.first;
473        iter != range.second; ++iter) {
474     const std::string& input_method_id = iter->second;
475     if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) {
476       out_input_method_ids->push_back(input_method_id);
477       result = true;
478     }
479   }
480   if ((type == kAllInputMethods) && !result) {
481     DVLOG(1) << "Unknown language code: " << normalized_language_code;
482   }
483   return result;
484 }
485
486 void InputMethodUtil::GetFirstLoginInputMethodIds(
487     const std::string& language_code,
488     const InputMethodDescriptor& current_input_method,
489     std::vector<std::string>* out_input_method_ids) const {
490   out_input_method_ids->clear();
491
492   // First, add the current keyboard layout (one used on the login screen).
493   out_input_method_ids->push_back(current_input_method.id());
494
495   const std::string current_layout
496       = current_input_method.GetPreferredKeyboardLayout();
497   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDefaultInputMethodRecommendation);
498        ++i) {
499     if (kDefaultInputMethodRecommendation[i].locale == language_code &&
500         kDefaultInputMethodRecommendation[i].layout == current_layout) {
501       out_input_method_ids->push_back(
502           extension_ime_util::GetInputMethodIDByEngineID(
503               kDefaultInputMethodRecommendation[i].engine_id));
504       return;
505     }
506   }
507
508   // Second, find the most popular input method associated with the
509   // current UI language. The input method IDs returned from
510   // GetInputMethodIdsFromLanguageCode() are sorted by popularity, hence
511   // our basic strategy is to pick the first one, but it's a bit more
512   // complicated as shown below.
513   std::string most_popular_id;
514   std::vector<std::string> input_method_ids;
515   // This returns the input methods sorted by popularity.
516   GetInputMethodIdsFromLanguageCode(
517       language_code, kAllInputMethods, &input_method_ids);
518   for (size_t i = 0; i < input_method_ids.size(); ++i) {
519     const std::string& input_method_id = input_method_ids[i];
520     // Pick the first one.
521     if (most_popular_id.empty())
522       most_popular_id = input_method_id;
523
524     // Check if there is one that matches the current keyboard layout, but
525     // not the current keyboard itself. This is useful if there are
526     // multiple keyboard layout choices for one input method. For
527     // instance, Mozc provides three choices: mozc (US keyboard), mozc-jp
528     // (JP keyboard), mozc-dv (Dvorak).
529     const InputMethodDescriptor* descriptor =
530         GetInputMethodDescriptorFromId(input_method_id);
531     if (descriptor &&
532         descriptor->id() != current_input_method.id() &&
533         descriptor->GetPreferredKeyboardLayout() ==
534         current_input_method.GetPreferredKeyboardLayout()) {
535       most_popular_id = input_method_id;
536       break;
537     }
538   }
539   // Add the most popular input method ID, if it's different from the
540   // current input method.
541   if (most_popular_id != current_input_method.id()) {
542     out_input_method_ids->push_back(most_popular_id);
543   }
544 }
545
546 void InputMethodUtil::GetLanguageCodesFromInputMethodIds(
547     const std::vector<std::string>& input_method_ids,
548     std::vector<std::string>* out_language_codes) const {
549   out_language_codes->clear();
550
551   for (size_t i = 0; i < input_method_ids.size(); ++i) {
552     const std::string& input_method_id = input_method_ids[i];
553     const InputMethodDescriptor* input_method =
554         GetInputMethodDescriptorFromId(input_method_id);
555     if (!input_method) {
556       DVLOG(1) << "Unknown input method ID: " << input_method_ids[i];
557       continue;
558     }
559     DCHECK(!input_method->language_codes().empty());
560     const std::string language_code = input_method->language_codes().at(0);
561     // Add it if it's not already present.
562     if (std::count(out_language_codes->begin(), out_language_codes->end(),
563                    language_code) == 0) {
564       out_language_codes->push_back(language_code);
565     }
566   }
567 }
568
569 std::string InputMethodUtil::GetLanguageDefaultInputMethodId(
570     const std::string& language_code) {
571   std::vector<std::string> candidates;
572   GetInputMethodIdsFromLanguageCode(
573       language_code, input_method::kKeyboardLayoutsOnly, &candidates);
574   if (candidates.size())
575     return candidates.front();
576
577   return std::string();
578 }
579
580 bool InputMethodUtil::MigrateInputMethods(
581     std::vector<std::string>* input_method_ids) {
582   bool rewritten = false;
583   std::vector<std::string>& ids = *input_method_ids;
584   for (size_t i = 0; i < ids.size(); ++i) {
585     std::string engine_id = ids[i];
586     // Migrates some Engine IDs from VPD.
587     for (size_t j = 0; j < arraysize(kEngineIdMigrationMap); ++j) {
588       size_t pos = engine_id.find(kEngineIdMigrationMap[j][0]);
589       if (pos == 0)
590         engine_id.replace(pos, strlen(kEngineIdMigrationMap[j][0]),
591                           kExtensionIdMigrationMap[j][1]);
592     }
593     std::string id =
594         extension_ime_util::GetInputMethodIDByEngineID(engine_id);
595     // Migrates old ime id's to new ones.
596     for (size_t j = 0; j < arraysize(kExtensionIdMigrationMap); ++j) {
597       size_t pos = id.find(kExtensionIdMigrationMap[j][0]);
598       if (pos != std::string::npos)
599         id.replace(pos, kExtensionIdLen, kExtensionIdMigrationMap[j][1]);
600       if (id != ids[i]) {
601         ids[i] = id;
602         rewritten = true;
603       }
604     }
605   }
606   if (rewritten) {
607     // Removes the duplicates.
608     std::vector<std::string> new_ids;
609     for (size_t i = 0; i < ids.size(); ++i) {
610       if (std::find(new_ids.begin(), new_ids.end(), ids[i]) == new_ids.end())
611         new_ids.push_back(ids[i]);
612     }
613     ids.swap(new_ids);
614   }
615   return rewritten;
616 }
617
618 void InputMethodUtil::UpdateHardwareLayoutCache() {
619   DCHECK(thread_checker_.CalledOnValidThread());
620   hardware_layouts_.clear();
621   hardware_login_layouts_.clear();
622   if (cached_hardware_layouts_.empty())
623     Tokenize(delegate_->GetHardwareKeyboardLayouts(), ",",
624              &cached_hardware_layouts_);
625   hardware_layouts_ = cached_hardware_layouts_;
626   MigrateInputMethods(&hardware_layouts_);
627
628   for (size_t i = 0; i < hardware_layouts_.size(); ++i) {
629     if (IsLoginKeyboard(hardware_layouts_[i]))
630       hardware_login_layouts_.push_back(hardware_layouts_[i]);
631   }
632   if (hardware_layouts_.empty()) {
633     // This is totally fine if it's empty. The hardware keyboard layout is
634     // not stored if startup_manifest.json (OEM customization data) is not
635     // present (ex. Cr48 doen't have that file).
636     hardware_layouts_.push_back(GetFallbackInputMethodDescriptor().id());
637   }
638
639   if (hardware_login_layouts_.empty())
640     hardware_login_layouts_.push_back(GetFallbackInputMethodDescriptor().id());
641 }
642
643 void InputMethodUtil::SetHardwareKeyboardLayoutForTesting(
644     const std::string& layout) {
645   delegate_->SetHardwareKeyboardLayoutForTesting(layout);
646   cached_hardware_layouts_.clear();
647   UpdateHardwareLayoutCache();
648 }
649
650 const std::vector<std::string>&
651     InputMethodUtil::GetHardwareInputMethodIds() {
652   DCHECK(thread_checker_.CalledOnValidThread());
653   UpdateHardwareLayoutCache();
654   return hardware_layouts_;
655 }
656
657 const std::vector<std::string>&
658     InputMethodUtil::GetHardwareLoginInputMethodIds() {
659   DCHECK(thread_checker_.CalledOnValidThread());
660   UpdateHardwareLayoutCache();
661   return hardware_login_layouts_;
662 }
663
664 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id)
665     const {
666   const InputMethodDescriptor* ime =
667       GetInputMethodDescriptorFromId(input_method_id);
668   return ime ? ime->is_login_keyboard() : false;
669 }
670
671 void InputMethodUtil::AppendInputMethods(const InputMethodDescriptors& imes) {
672   for (size_t i = 0; i < imes.size(); ++i) {
673     const InputMethodDescriptor& input_method = imes[i];
674     DCHECK(!input_method.language_codes().empty());
675     const std::vector<std::string>& language_codes =
676         input_method.language_codes();
677     id_to_descriptor_[input_method.id()] = input_method;
678
679     typedef LanguageCodeToIdsMap::const_iterator It;
680     for (size_t j = 0; j < language_codes.size(); ++j) {
681       std::pair<It, It> range =
682           language_code_to_ids_.equal_range(language_codes[j]);
683       It it = range.first;
684       for (; it != range.second; ++it) {
685         if (it->second == input_method.id())
686           break;
687       }
688       if (it == range.second)
689         language_code_to_ids_.insert(
690             std::make_pair(language_codes[j], input_method.id()));
691     }
692   }
693 }
694
695 void InputMethodUtil::ResetInputMethods(const InputMethodDescriptors& imes) {
696   // Clear the existing maps.
697   language_code_to_ids_.clear();
698   id_to_descriptor_.clear();
699
700   AppendInputMethods(imes);
701 }
702
703 void InputMethodUtil::InitXkbInputMethodsForTesting() {
704   cached_hardware_layouts_.clear();
705   ResetInputMethods(*(InputMethodWhitelist().GetSupportedInputMethods()));
706 }
707
708 const InputMethodUtil::InputMethodIdToDescriptorMap&
709 InputMethodUtil::GetIdToDesciptorMapForTesting() {
710   return id_to_descriptor_;
711 }
712
713 InputMethodDescriptor InputMethodUtil::GetFallbackInputMethodDescriptor() {
714   std::vector<std::string> layouts;
715   layouts.push_back("us");
716   std::vector<std::string> languages;
717   languages.push_back("en-US");
718   return InputMethodDescriptor(
719       extension_ime_util::GetInputMethodIDByEngineID("xkb:us::eng"),
720       "",
721       "US",
722       layouts,
723       languages,
724       true,  // login keyboard.
725       GURL(),  // options page, not available.
726       GURL()); // input view page, not available.
727 }
728
729 }  // namespace input_method
730 }  // namespace chromeos