Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / input_method_util.cc
blob2f6ddb85bde54aa13a57a61a9b8104ad29e86865
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 #include "ui/base/l10n/l10n_util.h"
33 namespace {
35 // A mapping from an input method id to a resource id for a
36 // medium length language indicator.
37 // For those languages that want to display a slightly longer text in the
38 // "Your input method has changed to..." bubble than in the status tray.
39 // If an entry is not found in this table the short name is used.
40 const struct {
41 const char* engine_id;
42 const int resource_id;
43 } kMappingImeIdToMediumLenNameResourceId[] = {
44 { "hangul_2set", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
45 { "hangul_3set390", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
46 { "hangul_3setfinal", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
47 { "hangul_3setnoshift", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
48 { "hangul_3setromaja", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
49 { "zh-t-i0-pinyin", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED},
50 { "zh-t-i0-wubi-1986", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_SIMPLIFIED },
51 { "zh-hant-t-i0-und", IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
52 { "zh-hant-t-i0-cangjie-1987",
53 IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
54 { "zh-hant-t-i0-cangjie-1987-x-m0-simplified",
55 IDS_LANGUAGES_MEDIUM_LEN_NAME_CHINESE_TRADITIONAL },
56 { extension_misc::kBrailleImeEngineId,
57 IDS_LANGUAGES_MEDIUM_LEN_NAME_BRAILLE },
59 const size_t kMappingImeIdToMediumLenNameResourceIdLen =
60 arraysize(kMappingImeIdToMediumLenNameResourceId);
62 // Due to asynchronous initialization of component extension manager,
63 // GetFirstLoginInputMethodIds may miss component extension IMEs. To enable
64 // component extension IME as the first loging input method, we have to prepare
65 // component extension IME IDs.
66 // Note: empty layout means the rule applies for all layouts.
67 const struct {
68 const char* locale;
69 const char* layout;
70 const char* engine_id;
71 } kDefaultInputMethodRecommendation[] = {
72 { "ja", "jp", "nacl_mozc_jp" },
73 { "ja", "", "nacl_mozc_us" },
74 { "zh-CN", "", "zh-t-i0-pinyin" },
75 { "zh-TW", "", "zh-hant-t-i0-und" },
76 { "th", "", "vkd_th" },
77 { "vi", "", "vkd_vi_tcvn" },
78 { "ru", "", "xkb:ru::rus" },
81 // The engine ID map for migration. This migration is for input method IDs from
82 // VPD so it's NOT a temporary migration.
83 const char* const kEngineIdMigrationMap[][2] = {
84 {"ime:jp:mozc_jp", "nacl_mozc_jp"},
85 {"ime:jp:mozc_us", "nacl_mozc_us"},
86 {"ime:ko:hangul_2set", "hangul_2set"},
87 {"ime:ko:hangul", "hangul_2set"},
88 {"ime:zh-t:array", "zh-hant-t-i0-array-1992"},
89 {"ime:zh-t:cangjie", "zh-hant-t-i0-cangjie-1987"},
90 {"ime:zh-t:dayi", "zh-hant-t-i0-dayi-1988"},
91 {"ime:zh-t:pinyin", "zh-hant-t-i0-pinyin"},
92 {"ime:zh-t:quick", "zh-hant-t-i0-cangjie-1987-x-m0-simplified"},
93 {"ime:zh-t:zhuyin", "zh-hant-t-i0-und"},
94 {"ime:zh:pinyin", "zh-t-i0-pinyin"},
95 {"ime:zh:wubi", "zh-t-i0-wubi-1986"},
96 {"m17n:", "vkd_"},
97 {"t13n:am", "am-t-i0-und"},
98 {"t13n:ar", "ar-t-i0-und"},
99 {"t13n:bn", "bn-t-i0-und"},
100 {"t13n:el", "el-t-i0-und"},
101 {"t13n:fa", "fa-t-i0-und"},
102 {"t13n:gu", "gu-t-i0-und"},
103 {"t13n:he", "he-t-i0-und"},
104 {"t13n:hi", "hi-t-i0-und"},
105 {"t13n:kn", "kn-t-i0-und"},
106 {"t13n:ml", "ml-t-i0-und"},
107 {"t13n:mr", "mr-t-i0-und"},
108 {"t13n:ne", "ne-t-i0-und"},
109 {"t13n:or", "or-t-i0-und"},
110 {"t13n:pa", "pa-t-i0-und"},
111 {"t13n:sa", "sa-t-i0-und"},
112 {"t13n:sr", "sr-t-i0-und"},
113 {"t13n:ta", "ta-t-i0-und"},
114 {"t13n:te", "te-t-i0-und"},
115 {"t13n:ti", "ti-t-i0-und"},
116 {"t13n:ur", "ur-t-i0-und"},
119 const struct EnglishToResouceId {
120 const char* english_string_from_ibus;
121 int resource_id;
122 } kEnglishToResourceIdArray[] = {
123 // For xkb-layouts.
124 { "xkb:am:phonetic:arm", IDS_STATUSBAR_LAYOUT_ARMENIAN_PHONETIC },
125 { "xkb:be::fra", IDS_STATUSBAR_LAYOUT_BELGIUM },
126 { "xkb:be::ger", IDS_STATUSBAR_LAYOUT_BELGIUM },
127 { "xkb:be::nld", IDS_STATUSBAR_LAYOUT_BELGIUM },
128 { "xkb:bg::bul", IDS_STATUSBAR_LAYOUT_BULGARIA },
129 { "xkb:bg:phonetic:bul", IDS_STATUSBAR_LAYOUT_BULGARIA_PHONETIC },
130 { "xkb:br::por", IDS_STATUSBAR_LAYOUT_BRAZIL },
131 { "xkb:by::bel", IDS_STATUSBAR_LAYOUT_BELARUSIAN },
132 { "xkb:ca::fra", IDS_STATUSBAR_LAYOUT_CANADA },
133 { "xkb:ca:eng:eng", IDS_STATUSBAR_LAYOUT_CANADA_ENGLISH },
134 { "xkb:ca:multix:fra", IDS_STATUSBAR_LAYOUT_CANADIAN_MULTILINGUAL },
135 { "xkb:ch::ger", IDS_STATUSBAR_LAYOUT_SWITZERLAND },
136 { "xkb:ch:fr:fra", IDS_STATUSBAR_LAYOUT_SWITZERLAND_FRENCH },
137 { "xkb:cz::cze", IDS_STATUSBAR_LAYOUT_CZECHIA },
138 { "xkb:cz:qwerty:cze", IDS_STATUSBAR_LAYOUT_CZECHIA_QWERTY },
139 { "xkb:de::ger", IDS_STATUSBAR_LAYOUT_GERMANY },
140 { "xkb:de:neo:ger", IDS_STATUSBAR_LAYOUT_GERMANY_NEO2 },
141 { "xkb:dk::dan", IDS_STATUSBAR_LAYOUT_DENMARK },
142 { "xkb:ee::est", IDS_STATUSBAR_LAYOUT_ESTONIA },
143 { "xkb:es::spa", IDS_STATUSBAR_LAYOUT_SPAIN },
144 { "xkb:es:cat:cat", IDS_STATUSBAR_LAYOUT_SPAIN_CATALAN },
145 { "xkb:fi::fin", IDS_STATUSBAR_LAYOUT_FINLAND },
146 { "xkb:fr::fra", IDS_STATUSBAR_LAYOUT_FRANCE },
147 { "xkb:gb:dvorak:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM_DVORAK },
148 { "xkb:gb:extd:eng", IDS_STATUSBAR_LAYOUT_UNITED_KINGDOM },
149 { "xkb:ge::geo", IDS_STATUSBAR_LAYOUT_GEORGIAN },
150 { "xkb:gr::gre", IDS_STATUSBAR_LAYOUT_GREECE },
151 { "xkb:hr::scr", IDS_STATUSBAR_LAYOUT_CROATIA },
152 { "xkb:hu::hun", IDS_STATUSBAR_LAYOUT_HUNGARY },
153 { "xkb:ie::ga", IDS_STATUSBAR_LAYOUT_IRISH },
154 { "xkb:il::heb", IDS_STATUSBAR_LAYOUT_ISRAEL },
155 { "xkb:is::ice", IDS_STATUSBAR_LAYOUT_ICELANDIC },
156 { "xkb:it::ita", IDS_STATUSBAR_LAYOUT_ITALY },
157 { "xkb:jp::jpn", IDS_STATUSBAR_LAYOUT_JAPAN },
158 { "xkb:latam::spa", IDS_STATUSBAR_LAYOUT_LATIN_AMERICAN },
159 { "xkb:lt::lit", IDS_STATUSBAR_LAYOUT_LITHUANIA },
160 { "xkb:lv:apostrophe:lav", IDS_STATUSBAR_LAYOUT_LATVIA },
161 { "xkb:mk::mkd", IDS_STATUSBAR_LAYOUT_MACEDONIAN },
162 { "xkb:mn::mon", IDS_STATUSBAR_LAYOUT_MONGOLIAN },
163 { "xkb:nl::nld", IDS_STATUSBAR_LAYOUT_NETHERLANDS },
164 { "xkb:no::nob", IDS_STATUSBAR_LAYOUT_NORWAY },
165 { "xkb:pl::pol", IDS_STATUSBAR_LAYOUT_POLAND },
166 { "xkb:pt::por", IDS_STATUSBAR_LAYOUT_PORTUGAL },
167 { "xkb:ro::rum", IDS_STATUSBAR_LAYOUT_ROMANIA },
168 { "xkb:rs::srp", IDS_STATUSBAR_LAYOUT_SERBIA },
169 { "xkb:ru::rus", IDS_STATUSBAR_LAYOUT_RUSSIA },
170 { "xkb:ru:phonetic:rus", IDS_STATUSBAR_LAYOUT_RUSSIA_PHONETIC },
171 { "xkb:se::swe", IDS_STATUSBAR_LAYOUT_SWEDEN },
172 { "xkb:si::slv", IDS_STATUSBAR_LAYOUT_SLOVENIA },
173 { "xkb:sk::slo", IDS_STATUSBAR_LAYOUT_SLOVAKIA },
174 { "xkb:tr::tur", IDS_STATUSBAR_LAYOUT_TURKEY },
175 { "xkb:ua::ukr", IDS_STATUSBAR_LAYOUT_UKRAINE },
176 { "xkb:us::eng", IDS_STATUSBAR_LAYOUT_USA },
177 { "xkb:us::fil", IDS_STATUSBAR_LAYOUT_USA },
178 { "xkb:us::ind", IDS_STATUSBAR_LAYOUT_USA },
179 { "xkb:us::msa", IDS_STATUSBAR_LAYOUT_USA },
180 { "xkb:us:altgr-intl:eng", IDS_STATUSBAR_LAYOUT_USA_EXTENDED },
181 { "xkb:us:colemak:eng", IDS_STATUSBAR_LAYOUT_USA_COLEMAK },
182 { "xkb:us:dvorak:eng", IDS_STATUSBAR_LAYOUT_USA_DVORAK },
183 { "xkb:us:dvp:eng", IDS_STATUSBAR_LAYOUT_USA_DVP },
184 { "xkb:us:intl:eng", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
185 { "xkb:us:intl:nld", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
186 { "xkb:us:intl:por", IDS_STATUSBAR_LAYOUT_USA_INTERNATIONAL },
188 const size_t kEnglishToResourceIdArraySize =
189 arraysize(kEnglishToResourceIdArray);
191 const struct InputMethodNameMap {
192 const char* message_name;
193 int resource_id;
194 bool operator<(const InputMethodNameMap& other) const {
195 return strcmp(message_name, other.message_name) < 0;
197 } kInputMethodNameMap[] = {
198 {"__MSG_INPUTMETHOD_ARRAY__", IDS_IME_NAME_INPUTMETHOD_ARRAY},
199 {"__MSG_INPUTMETHOD_CANGJIE__", IDS_IME_NAME_INPUTMETHOD_CANGJIE},
200 {"__MSG_INPUTMETHOD_DAYI__", IDS_IME_NAME_INPUTMETHOD_DAYI},
201 {"__MSG_INPUTMETHOD_HANGUL_2_SET__", IDS_IME_NAME_INPUTMETHOD_HANGUL_2_SET},
202 {"__MSG_INPUTMETHOD_HANGUL_3_SET_390__",
203 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_390},
204 {"__MSG_INPUTMETHOD_HANGUL_3_SET_FINAL__",
205 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_FINAL},
206 {"__MSG_INPUTMETHOD_HANGUL_3_SET_NO_SHIFT__",
207 IDS_IME_NAME_INPUTMETHOD_HANGUL_3_SET_NO_SHIFT},
208 {"__MSG_INPUTMETHOD_HANGUL_AHNMATAE__",
209 IDS_IME_NAME_INPUTMETHOD_HANGUL_AHNMATAE},
210 {"__MSG_INPUTMETHOD_HANGUL_ROMAJA__",
211 IDS_IME_NAME_INPUTMETHOD_HANGUL_ROMAJA},
212 {"__MSG_INPUTMETHOD_HANGUL__", IDS_IME_NAME_INPUTMETHOD_HANGUL},
213 {"__MSG_INPUTMETHOD_MOZC_JP__", IDS_IME_NAME_INPUTMETHOD_MOZC_JP},
214 {"__MSG_INPUTMETHOD_MOZC_US__", IDS_IME_NAME_INPUTMETHOD_MOZC_US},
215 {"__MSG_INPUTMETHOD_PINYIN__", IDS_IME_NAME_INPUTMETHOD_PINYIN},
216 {"__MSG_INPUTMETHOD_QUICK__", IDS_IME_NAME_INPUTMETHOD_QUICK},
217 {"__MSG_INPUTMETHOD_TRADITIONAL_PINYIN__",
218 IDS_IME_NAME_INPUTMETHOD_TRADITIONAL_PINYIN},
219 {"__MSG_INPUTMETHOD_WUBI__", IDS_IME_NAME_INPUTMETHOD_WUBI},
220 {"__MSG_INPUTMETHOD_ZHUYIN__", IDS_IME_NAME_INPUTMETHOD_ZHUYIN},
221 {"__MSG_KEYBOARD_ARABIC__", IDS_IME_NAME_KEYBOARD_ARABIC},
222 {"__MSG_KEYBOARD_ARMENIAN_PHONETIC__",
223 IDS_IME_NAME_KEYBOARD_ARMENIAN_PHONETIC},
224 {"__MSG_KEYBOARD_BELARUSIAN__", IDS_IME_NAME_KEYBOARD_BELARUSIAN},
225 {"__MSG_KEYBOARD_BELGIAN__", IDS_IME_NAME_KEYBOARD_BELGIAN},
226 {"__MSG_KEYBOARD_BENGALI_PHONETIC__",
227 IDS_IME_NAME_KEYBOARD_BENGALI_PHONETIC},
228 {"__MSG_KEYBOARD_BRAZILIAN__", IDS_IME_NAME_KEYBOARD_BRAZILIAN},
229 {"__MSG_KEYBOARD_BULGARIAN_PHONETIC__",
230 IDS_IME_NAME_KEYBOARD_BULGARIAN_PHONETIC},
231 {"__MSG_KEYBOARD_BULGARIAN__", IDS_IME_NAME_KEYBOARD_BULGARIAN},
232 {"__MSG_KEYBOARD_CANADIAN_ENGLISH__",
233 IDS_IME_NAME_KEYBOARD_CANADIAN_ENGLISH},
234 {"__MSG_KEYBOARD_CANADIAN_FRENCH__", IDS_IME_NAME_KEYBOARD_CANADIAN_FRENCH},
235 {"__MSG_KEYBOARD_CANADIAN_MULTILINGUAL__",
236 IDS_IME_NAME_KEYBOARD_CANADIAN_MULTILINGUAL},
237 {"__MSG_KEYBOARD_CATALAN__", IDS_IME_NAME_KEYBOARD_CATALAN},
238 {"__MSG_KEYBOARD_CROATIAN__", IDS_IME_NAME_KEYBOARD_CROATIAN},
239 {"__MSG_KEYBOARD_CZECH_QWERTY__", IDS_IME_NAME_KEYBOARD_CZECH_QWERTY},
240 {"__MSG_KEYBOARD_CZECH__", IDS_IME_NAME_KEYBOARD_CZECH},
241 {"__MSG_KEYBOARD_DANISH__", IDS_IME_NAME_KEYBOARD_DANISH},
242 {"__MSG_KEYBOARD_DEVANAGARI_PHONETIC__",
243 IDS_IME_NAME_KEYBOARD_DEVANAGARI_PHONETIC},
244 {"__MSG_KEYBOARD_ESTONIAN__", IDS_IME_NAME_KEYBOARD_ESTONIAN},
245 {"__MSG_KEYBOARD_ETHIOPIC__", IDS_IME_NAME_KEYBOARD_ETHIOPIC},
246 {"__MSG_KEYBOARD_FINNISH__", IDS_IME_NAME_KEYBOARD_FINNISH},
247 {"__MSG_KEYBOARD_FRENCH__", IDS_IME_NAME_KEYBOARD_FRENCH},
248 {"__MSG_KEYBOARD_GEORGIAN__", IDS_IME_NAME_KEYBOARD_GEORGIAN},
249 {"__MSG_KEYBOARD_GERMAN_NEO_2__", IDS_IME_NAME_KEYBOARD_GERMAN_NEO_2},
250 {"__MSG_KEYBOARD_GERMAN__", IDS_IME_NAME_KEYBOARD_GERMAN},
251 {"__MSG_KEYBOARD_GREEK__", IDS_IME_NAME_KEYBOARD_GREEK},
252 {"__MSG_KEYBOARD_GUJARATI_PHONETIC__",
253 IDS_IME_NAME_KEYBOARD_GUJARATI_PHONETIC},
254 {"__MSG_KEYBOARD_HEBREW__", IDS_IME_NAME_KEYBOARD_HEBREW},
255 {"__MSG_KEYBOARD_HUNGARIAN__", IDS_IME_NAME_KEYBOARD_HUNGARIAN},
256 {"__MSG_KEYBOARD_ICELANDIC__", IDS_IME_NAME_KEYBOARD_ICELANDIC},
257 {"__MSG_KEYBOARD_IRISH__", IDS_IME_NAME_KEYBOARD_IRISH},
258 {"__MSG_KEYBOARD_ITALIAN__", IDS_IME_NAME_KEYBOARD_ITALIAN},
259 {"__MSG_KEYBOARD_JAPANESE__", IDS_IME_NAME_KEYBOARD_JAPANESE},
260 {"__MSG_KEYBOARD_KANNADA_PHONETIC__",
261 IDS_IME_NAME_KEYBOARD_KANNADA_PHONETIC},
262 {"__MSG_KEYBOARD_KHMER__", IDS_IME_NAME_KEYBOARD_KHMER},
263 {"__MSG_KEYBOARD_LAO__", IDS_IME_NAME_KEYBOARD_LAO},
264 {"__MSG_KEYBOARD_LATIN_AMERICAN__", IDS_IME_NAME_KEYBOARD_LATIN_AMERICAN},
265 {"__MSG_KEYBOARD_LATVIAN__", IDS_IME_NAME_KEYBOARD_LATVIAN},
266 {"__MSG_KEYBOARD_LITHUANIAN__", IDS_IME_NAME_KEYBOARD_LITHUANIAN},
267 {"__MSG_KEYBOARD_MACEDONIAN__", IDS_IME_NAME_KEYBOARD_MACEDONIAN},
268 {"__MSG_KEYBOARD_MALAYALAM_PHONETIC__",
269 IDS_IME_NAME_KEYBOARD_MALAYALAM_PHONETIC},
270 {"__MSG_KEYBOARD_MALTESE__", IDS_IME_NAME_KEYBOARD_MALTESE},
271 {"__MSG_KEYBOARD_MONGOLIAN__", IDS_IME_NAME_KEYBOARD_MONGOLIAN},
272 {"__MSG_KEYBOARD_MYANMAR_MYANSAN__", IDS_IME_NAME_KEYBOARD_MYANMAR_MYANSAN},
273 {"__MSG_KEYBOARD_MYANMAR__", IDS_IME_NAME_KEYBOARD_MYANMAR},
274 {"__MSG_KEYBOARD_NEPALI_INSCRIPT__", IDS_IME_NAME_KEYBOARD_NEPALI_INSCRIPT},
275 {"__MSG_KEYBOARD_NEPALI_PHONETIC__", IDS_IME_NAME_KEYBOARD_NEPALI_PHONETIC},
276 {"__MSG_KEYBOARD_NETHERLANDS__", IDS_IME_NAME_KEYBOARD_NETHERLANDS},
277 {"__MSG_KEYBOARD_NORWEGIAN__", IDS_IME_NAME_KEYBOARD_NORWEGIAN},
278 {"__MSG_KEYBOARD_PERSIAN__", IDS_IME_NAME_KEYBOARD_PERSIAN},
279 {"__MSG_KEYBOARD_POLISH__", IDS_IME_NAME_KEYBOARD_POLISH},
280 {"__MSG_KEYBOARD_PORTUGUESE__", IDS_IME_NAME_KEYBOARD_PORTUGUESE},
281 {"__MSG_KEYBOARD_ROMANIAN_STANDARD__",
282 IDS_IME_NAME_KEYBOARD_ROMANIAN_STANDARD},
283 {"__MSG_KEYBOARD_ROMANIAN__", IDS_IME_NAME_KEYBOARD_ROMANIAN},
284 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC_AATSEEL__",
285 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC_AATSEEL},
286 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC_YAZHERT__",
287 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC_YAZHERT},
288 {"__MSG_KEYBOARD_RUSSIAN_PHONETIC__",
289 IDS_IME_NAME_KEYBOARD_RUSSIAN_PHONETIC},
290 {"__MSG_KEYBOARD_RUSSIAN__", IDS_IME_NAME_KEYBOARD_RUSSIAN},
291 {"__MSG_KEYBOARD_SERBIAN__", IDS_IME_NAME_KEYBOARD_SERBIAN},
292 {"__MSG_KEYBOARD_SINHALA__", IDS_IME_NAME_KEYBOARD_SINHALA},
293 {"__MSG_KEYBOARD_SLOVAKIAN__", IDS_IME_NAME_KEYBOARD_SLOVAKIAN},
294 {"__MSG_KEYBOARD_SLOVENIAN__", IDS_IME_NAME_KEYBOARD_SLOVENIAN},
295 {"__MSG_KEYBOARD_SORANIKURDISH_AR__",
296 IDS_IME_NAME_KEYBOARD_SORANIKURDISH_AR},
297 {"__MSG_KEYBOARD_SORANIKURDISH_EN__",
298 IDS_IME_NAME_KEYBOARD_SORANIKURDISH_EN},
299 {"__MSG_KEYBOARD_SPANISH__", IDS_IME_NAME_KEYBOARD_SPANISH},
300 {"__MSG_KEYBOARD_SWEDISH__", IDS_IME_NAME_KEYBOARD_SWEDISH},
301 {"__MSG_KEYBOARD_SWISS_FRENCH__", IDS_IME_NAME_KEYBOARD_SWISS_FRENCH},
302 {"__MSG_KEYBOARD_SWISS__", IDS_IME_NAME_KEYBOARD_SWISS},
303 {"__MSG_KEYBOARD_TAMIL_INSCRIPT__", IDS_IME_NAME_KEYBOARD_TAMIL_INSCRIPT},
304 {"__MSG_KEYBOARD_TAMIL_ITRANS__", IDS_IME_NAME_KEYBOARD_TAMIL_ITRANS},
305 {"__MSG_KEYBOARD_TAMIL_PHONETIC__", IDS_IME_NAME_KEYBOARD_TAMIL_PHONETIC},
306 {"__MSG_KEYBOARD_TAMIL_TAMIL99__", IDS_IME_NAME_KEYBOARD_TAMIL_TAMIL99},
307 {"__MSG_KEYBOARD_TAMIL_TYPEWRITER__",
308 IDS_IME_NAME_KEYBOARD_TAMIL_TYPEWRITER},
309 {"__MSG_KEYBOARD_TELUGU_PHONETIC__", IDS_IME_NAME_KEYBOARD_TELUGU_PHONETIC},
310 {"__MSG_KEYBOARD_THAI_KEDMANEE__", IDS_IME_NAME_KEYBOARD_THAI_KEDMANEE},
311 {"__MSG_KEYBOARD_THAI_PATTACHOTE__", IDS_IME_NAME_KEYBOARD_THAI_PATTACHOTE},
312 {"__MSG_KEYBOARD_THAI_TIS__", IDS_IME_NAME_KEYBOARD_THAI_TIS},
313 {"__MSG_KEYBOARD_TURKISH__", IDS_IME_NAME_KEYBOARD_TURKISH},
314 {"__MSG_KEYBOARD_UKRAINIAN__", IDS_IME_NAME_KEYBOARD_UKRAINIAN},
315 {"__MSG_KEYBOARD_UK_DVORAK__", IDS_IME_NAME_KEYBOARD_UK_DVORAK},
316 {"__MSG_KEYBOARD_UK__", IDS_IME_NAME_KEYBOARD_UK},
317 {"__MSG_KEYBOARD_US_COLEMAK__", IDS_IME_NAME_KEYBOARD_US_COLEMAK},
318 {"__MSG_KEYBOARD_US_DVORAK__", IDS_IME_NAME_KEYBOARD_US_DVORAK},
319 {"__MSG_KEYBOARD_US_DVP__", IDS_IME_NAME_KEYBOARD_US_DVP},
320 {"__MSG_KEYBOARD_US_EXTENDED__", IDS_IME_NAME_KEYBOARD_US_EXTENDED},
321 {"__MSG_KEYBOARD_US_INTERNATIONAL__",
322 IDS_IME_NAME_KEYBOARD_US_INTERNATIONAL},
323 {"__MSG_KEYBOARD_US__", IDS_IME_NAME_KEYBOARD_US},
324 {"__MSG_KEYBOARD_VIETNAMESE_TCVN__", IDS_IME_NAME_KEYBOARD_VIETNAMESE_TCVN},
325 {"__MSG_KEYBOARD_VIETNAMESE_TELEX__",
326 IDS_IME_NAME_KEYBOARD_VIETNAMESE_TELEX},
327 {"__MSG_KEYBOARD_VIETNAMESE_VIQR__", IDS_IME_NAME_KEYBOARD_VIETNAMESE_VIQR},
328 {"__MSG_KEYBOARD_VIETNAMESE_VNI__", IDS_IME_NAME_KEYBOARD_VIETNAMESE_VNI},
329 {"__MSG_TRANSLITERATION_AM__", IDS_IME_NAME_TRANSLITERATION_AM},
330 {"__MSG_TRANSLITERATION_AR__", IDS_IME_NAME_TRANSLITERATION_AR},
331 {"__MSG_TRANSLITERATION_BN__", IDS_IME_NAME_TRANSLITERATION_BN},
332 {"__MSG_TRANSLITERATION_EL__", IDS_IME_NAME_TRANSLITERATION_EL},
333 {"__MSG_TRANSLITERATION_FA__", IDS_IME_NAME_TRANSLITERATION_FA},
334 {"__MSG_TRANSLITERATION_GU__", IDS_IME_NAME_TRANSLITERATION_GU},
335 {"__MSG_TRANSLITERATION_HE__", IDS_IME_NAME_TRANSLITERATION_HE},
336 {"__MSG_TRANSLITERATION_HI__", IDS_IME_NAME_TRANSLITERATION_HI},
337 {"__MSG_TRANSLITERATION_KN__", IDS_IME_NAME_TRANSLITERATION_KN},
338 {"__MSG_TRANSLITERATION_ML__", IDS_IME_NAME_TRANSLITERATION_ML},
339 {"__MSG_TRANSLITERATION_MR__", IDS_IME_NAME_TRANSLITERATION_MR},
340 {"__MSG_TRANSLITERATION_NE__", IDS_IME_NAME_TRANSLITERATION_NE},
341 {"__MSG_TRANSLITERATION_OR__", IDS_IME_NAME_TRANSLITERATION_OR},
342 {"__MSG_TRANSLITERATION_PA__", IDS_IME_NAME_TRANSLITERATION_PA},
343 {"__MSG_TRANSLITERATION_SA__", IDS_IME_NAME_TRANSLITERATION_SA},
344 {"__MSG_TRANSLITERATION_SR__", IDS_IME_NAME_TRANSLITERATION_SR},
345 {"__MSG_TRANSLITERATION_TA__", IDS_IME_NAME_TRANSLITERATION_TA},
346 {"__MSG_TRANSLITERATION_TE__", IDS_IME_NAME_TRANSLITERATION_TE},
347 {"__MSG_TRANSLITERATION_TI__", IDS_IME_NAME_TRANSLITERATION_TI},
348 {"__MSG_TRANSLITERATION_UR__", IDS_IME_NAME_TRANSLITERATION_UR},
351 } // namespace
353 namespace chromeos {
355 namespace input_method {
357 InputMethodUtil::InputMethodUtil(InputMethodDelegate* delegate)
358 : delegate_(delegate) {
359 InputMethodDescriptors default_input_methods;
360 default_input_methods.push_back(GetFallbackInputMethodDescriptor());
361 ResetInputMethods(default_input_methods);
363 // Initialize a map from English string to Chrome string resource ID as well.
364 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) {
365 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i];
366 const bool result = english_to_resource_id_.insert(std::make_pair(
367 map_entry.english_string_from_ibus, map_entry.resource_id)).second;
368 DCHECK(result) << "Duplicated string is found: "
369 << map_entry.english_string_from_ibus;
373 InputMethodUtil::~InputMethodUtil() {
376 std::string InputMethodUtil::GetLocalizedDisplayName(
377 const InputMethodDescriptor& descriptor) const {
378 // Localizes the input method name.
379 const std::string& disp = descriptor.name();
380 if (disp.find("__MSG_") == 0) {
381 const InputMethodNameMap* map = kInputMethodNameMap;
382 size_t map_size = arraysize(kInputMethodNameMap);
383 std::string name = base::ToUpperASCII(disp);
384 const InputMethodNameMap map_key = {name.c_str(), 0};
385 const InputMethodNameMap* p =
386 std::lower_bound(map, map + map_size, map_key);
387 if (p != map + map_size && name == p->message_name)
388 return l10n_util::GetStringUTF8(p->resource_id);
390 return disp;
393 bool InputMethodUtil::TranslateStringInternal(
394 const std::string& english_string, base::string16 *out_string) const {
395 DCHECK(out_string);
396 // |english_string| could be an input method id. So legacy xkb id is required
397 // to get the translated string.
398 std::string key_string = extension_ime_util::MaybeGetLegacyXkbId(
399 english_string);
400 HashType::const_iterator iter = english_to_resource_id_.find(key_string);
402 if (iter == english_to_resource_id_.end()) {
403 // TODO(yusukes): Write Autotest which checks if all display names and all
404 // property names for supported input methods are listed in the resource
405 // ID array (crosbug.com/4572).
406 LOG(ERROR) << "Resource ID is not found for: " << english_string
407 << ", " << key_string;
408 return false;
411 *out_string = delegate_->GetLocalizedString(iter->second);
412 return true;
415 base::string16 InputMethodUtil::TranslateString(
416 const std::string& english_string) const {
417 base::string16 localized_string;
418 if (TranslateStringInternal(english_string, &localized_string)) {
419 return localized_string;
421 return base::UTF8ToUTF16(english_string);
424 bool InputMethodUtil::IsValidInputMethodId(
425 const std::string& input_method_id) const {
426 // We can't check the component extension is whilelisted or not here because
427 // it might not be initialized.
428 return GetInputMethodDescriptorFromId(input_method_id) != NULL ||
429 extension_ime_util::IsComponentExtensionIME(input_method_id);
432 // static
433 bool InputMethodUtil::IsKeyboardLayout(const std::string& input_method_id) {
434 return base::StartsWith(input_method_id, "xkb:",
435 base::CompareCase::INSENSITIVE_ASCII) ||
436 extension_ime_util::IsKeyboardLayoutExtension(input_method_id);
439 std::string InputMethodUtil::GetKeyboardLayoutName(
440 const std::string& input_method_id) const {
441 InputMethodIdToDescriptorMap::const_iterator iter
442 = id_to_descriptor_.find(input_method_id);
443 return (iter == id_to_descriptor_.end()) ?
444 "" : iter->second.GetPreferredKeyboardLayout();
447 std::string InputMethodUtil::GetInputMethodDisplayNameFromId(
448 const std::string& input_method_id) const {
449 base::string16 display_name;
450 if (!extension_ime_util::IsExtensionIME(input_method_id) &&
451 TranslateStringInternal(input_method_id, &display_name)) {
452 return base::UTF16ToUTF8(display_name);
454 const InputMethodDescriptor* descriptor =
455 GetInputMethodDescriptorFromId(input_method_id);
456 if (descriptor)
457 return GetLocalizedDisplayName(*descriptor);
458 // Return an empty string if the input method is not found.
459 return "";
462 base::string16 InputMethodUtil::GetInputMethodShortName(
463 const InputMethodDescriptor& input_method) const {
464 // TODO(shuchen): remove this method, as the client can directly use
465 // input_method.GetIndicator().
466 return base::UTF8ToUTF16(input_method.GetIndicator());
469 base::string16 InputMethodUtil::GetInputMethodMediumName(
470 const InputMethodDescriptor& input_method) const {
471 // For the "Your input method has changed to..." bubble. In most cases
472 // it uses the same name as the short name, unless found in a table
473 // for medium length names.
474 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
475 if (extension_ime_util::GetInputMethodIDByEngineID(
476 kMappingImeIdToMediumLenNameResourceId[i].engine_id) ==
477 input_method.id()) {
478 return delegate_->GetLocalizedString(
479 kMappingImeIdToMediumLenNameResourceId[i].resource_id);
482 return GetInputMethodShortName(input_method);
485 base::string16 InputMethodUtil::GetInputMethodLongNameInternal(
486 const InputMethodDescriptor& input_method, bool short_name) const {
487 std::string localized_display_name = GetLocalizedDisplayName(input_method);
488 if (!localized_display_name.empty() && !IsKeyboardLayout(input_method.id())) {
489 // If the descriptor has a name, use it.
490 return base::UTF8ToUTF16(localized_display_name);
493 // We don't show language here. Name of keyboard layout or input method
494 // usually imply (or explicitly include) its language.
495 // Special case for German, French and Dutch: these languages have multiple
496 // keyboard layouts and share the same layout of keyboard (Belgian). We need
497 // to show explicitly the language for the layout.
498 DCHECK(!input_method.language_codes().empty());
499 const std::string language_code = input_method.language_codes().at(0);
501 base::string16 text = (short_name || localized_display_name.empty())
502 ? TranslateString(input_method.id())
503 : base::UTF8ToUTF16(localized_display_name);
504 if (language_code == "de" || language_code == "fr" || language_code == "nl") {
505 const base::string16 language_name = delegate_->GetDisplayLanguageName(
506 language_code);
507 text = language_name + base::UTF8ToUTF16(" - ") + text;
510 DCHECK(!text.empty());
511 return text;
515 base::string16 InputMethodUtil::GetInputMethodLongNameStripped(
516 const InputMethodDescriptor& input_method) const {
517 return GetInputMethodLongNameInternal(input_method, true /* short_name */);
520 base::string16 InputMethodUtil::GetInputMethodLongName(
521 const InputMethodDescriptor& input_method) const {
522 return GetInputMethodLongNameInternal(input_method, false /* short_name */);
525 const InputMethodDescriptor* InputMethodUtil::GetInputMethodDescriptorFromId(
526 const std::string& input_method_id) const {
527 InputMethodIdToDescriptorMap::const_iterator iter =
528 id_to_descriptor_.find(input_method_id);
529 if (iter == id_to_descriptor_.end())
530 return NULL;
531 return &(iter->second);
534 bool InputMethodUtil::GetInputMethodIdsFromLanguageCode(
535 const std::string& normalized_language_code,
536 InputMethodType type,
537 std::vector<std::string>* out_input_method_ids) const {
538 return GetInputMethodIdsFromLanguageCodeInternal(
539 language_code_to_ids_,
540 normalized_language_code, type, out_input_method_ids);
543 bool InputMethodUtil::GetInputMethodIdsFromLanguageCodeInternal(
544 const std::multimap<std::string, std::string>& language_code_to_ids,
545 const std::string& normalized_language_code,
546 InputMethodType type,
547 std::vector<std::string>* out_input_method_ids) const {
548 DCHECK(out_input_method_ids);
549 out_input_method_ids->clear();
551 bool result = false;
552 std::pair<LanguageCodeToIdsMap::const_iterator,
553 LanguageCodeToIdsMap::const_iterator> range =
554 language_code_to_ids.equal_range(normalized_language_code);
555 for (LanguageCodeToIdsMap::const_iterator iter = range.first;
556 iter != range.second; ++iter) {
557 const std::string& input_method_id = iter->second;
558 if ((type == kAllInputMethods) || IsKeyboardLayout(input_method_id)) {
559 out_input_method_ids->push_back(input_method_id);
560 result = true;
563 if ((type == kAllInputMethods) && !result) {
564 DVLOG(1) << "Unknown language code: " << normalized_language_code;
566 return result;
569 void InputMethodUtil::GetFirstLoginInputMethodIds(
570 const std::string& language_code,
571 const InputMethodDescriptor& preferred_input_method,
572 std::vector<std::string>* out_input_method_ids) const {
573 out_input_method_ids->clear();
575 // First, add the preferred keyboard layout (e.g. one used on the login
576 // screen or set in UserContext when starting a public session).
577 out_input_method_ids->push_back(preferred_input_method.id());
579 const std::string current_layout
580 = preferred_input_method.GetPreferredKeyboardLayout();
581 for (size_t i = 0; i < arraysize(kDefaultInputMethodRecommendation);
582 ++i) {
583 if (kDefaultInputMethodRecommendation[i].locale == language_code && (
584 !kDefaultInputMethodRecommendation[i].layout[0] ||
585 kDefaultInputMethodRecommendation[i].layout == current_layout)) {
586 out_input_method_ids->push_back(
587 extension_ime_util::GetInputMethodIDByEngineID(
588 kDefaultInputMethodRecommendation[i].engine_id));
589 return;
593 std::vector<std::string> input_method_ids;
594 GetInputMethodIdsFromLanguageCode(
595 language_code, kAllInputMethods, &input_method_ids);
596 // Uses the first input method as the most popular one.
597 if (input_method_ids.size() > 0 &&
598 preferred_input_method.id() != input_method_ids[0]) {
599 out_input_method_ids->push_back(input_method_ids[0]);
603 void InputMethodUtil::GetLanguageCodesFromInputMethodIds(
604 const std::vector<std::string>& input_method_ids,
605 std::vector<std::string>* out_language_codes) const {
606 out_language_codes->clear();
608 for (size_t i = 0; i < input_method_ids.size(); ++i) {
609 const std::string& input_method_id = input_method_ids[i];
610 const InputMethodDescriptor* input_method =
611 GetInputMethodDescriptorFromId(input_method_id);
612 if (!input_method) {
613 DVLOG(1) << "Unknown input method ID: " << input_method_ids[i];
614 continue;
616 DCHECK(!input_method->language_codes().empty());
617 const std::string language_code = input_method->language_codes().at(0);
618 // Add it if it's not already present.
619 if (std::count(out_language_codes->begin(), out_language_codes->end(),
620 language_code) == 0) {
621 out_language_codes->push_back(language_code);
626 std::string InputMethodUtil::GetLanguageDefaultInputMethodId(
627 const std::string& language_code) {
628 std::vector<std::string> candidates;
629 GetInputMethodIdsFromLanguageCode(
630 language_code, input_method::kKeyboardLayoutsOnly, &candidates);
631 if (candidates.size())
632 return candidates.front();
634 return std::string();
637 std::string InputMethodUtil::MigrateInputMethod(
638 const std::string& input_method_id) {
639 std::string engine_id = input_method_id;
640 // Migrates some Engine IDs from VPD.
641 for (size_t j = 0; j < arraysize(kEngineIdMigrationMap); ++j) {
642 size_t pos = engine_id.find(kEngineIdMigrationMap[j][0]);
643 if (pos == 0) {
644 engine_id.replace(0,
645 strlen(kEngineIdMigrationMap[j][0]),
646 kEngineIdMigrationMap[j][1]);
647 break;
650 // Migrates the extension IDs.
651 std::string id =
652 extension_ime_util::GetInputMethodIDByEngineID(engine_id);
653 if (extension_ime_util::IsComponentExtensionIME(id)) {
654 std::string id_new = extension_ime_util::GetInputMethodIDByEngineID(
655 extension_ime_util::GetComponentIDByInputMethodID(id));
656 if (extension_ime_util::IsComponentExtensionIME(id_new))
657 id = id_new;
659 return id;
662 bool InputMethodUtil::MigrateInputMethods(
663 std::vector<std::string>* input_method_ids) {
664 bool rewritten = false;
665 std::vector<std::string>& ids = *input_method_ids;
666 for (size_t i = 0; i < ids.size(); ++i) {
667 std::string id = MigrateInputMethod(ids[i]);
668 if (id != ids[i]) {
669 ids[i] = id;
670 rewritten = true;
673 if (rewritten) {
674 // Removes the duplicates.
675 std::vector<std::string> new_ids;
676 for (size_t i = 0; i < ids.size(); ++i) {
677 if (std::find(new_ids.begin(), new_ids.end(), ids[i]) == new_ids.end())
678 new_ids.push_back(ids[i]);
680 ids.swap(new_ids);
682 return rewritten;
685 void InputMethodUtil::UpdateHardwareLayoutCache() {
686 DCHECK(thread_checker_.CalledOnValidThread());
687 hardware_layouts_.clear();
688 hardware_login_layouts_.clear();
689 if (cached_hardware_layouts_.empty()) {
690 cached_hardware_layouts_ =
691 base::SplitString(delegate_->GetHardwareKeyboardLayouts(), ",",
692 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
694 hardware_layouts_ = cached_hardware_layouts_;
695 MigrateInputMethods(&hardware_layouts_);
697 for (size_t i = 0; i < hardware_layouts_.size(); ++i) {
698 if (IsLoginKeyboard(hardware_layouts_[i]))
699 hardware_login_layouts_.push_back(hardware_layouts_[i]);
702 if (hardware_login_layouts_.empty()) {
703 // This is totally fine if |hardware_layouts_| is empty. The hardware
704 // keyboard layout is not stored if startup_manifest.json
705 // (OEM customization data) is not present (ex. Cr48 doen't have that file).
706 // So need to make sure |hardware_login_layouts_| is not empty, and
707 // |hardware_layouts_| contains at least one login layout.
708 std::string fallback_id = GetFallbackInputMethodDescriptor().id();
709 hardware_layouts_.insert(hardware_layouts_.begin(), fallback_id);
710 hardware_login_layouts_.push_back(fallback_id);
714 void InputMethodUtil::SetHardwareKeyboardLayoutForTesting(
715 const std::string& layout) {
716 delegate_->SetHardwareKeyboardLayoutForTesting(layout);
717 cached_hardware_layouts_.clear();
718 UpdateHardwareLayoutCache();
721 const std::vector<std::string>&
722 InputMethodUtil::GetHardwareInputMethodIds() {
723 DCHECK(thread_checker_.CalledOnValidThread());
724 UpdateHardwareLayoutCache();
725 return hardware_layouts_;
728 const std::vector<std::string>&
729 InputMethodUtil::GetHardwareLoginInputMethodIds() {
730 DCHECK(thread_checker_.CalledOnValidThread());
731 UpdateHardwareLayoutCache();
732 return hardware_login_layouts_;
735 bool InputMethodUtil::IsLoginKeyboard(const std::string& input_method_id)
736 const {
737 const InputMethodDescriptor* ime =
738 GetInputMethodDescriptorFromId(input_method_id);
739 return ime ? ime->is_login_keyboard() : false;
742 void InputMethodUtil::AppendInputMethods(const InputMethodDescriptors& imes) {
743 for (size_t i = 0; i < imes.size(); ++i) {
744 const InputMethodDescriptor& input_method = imes[i];
745 DCHECK(!input_method.language_codes().empty());
746 const std::vector<std::string>& language_codes =
747 input_method.language_codes();
748 id_to_descriptor_[input_method.id()] = input_method;
750 typedef LanguageCodeToIdsMap::const_iterator It;
751 for (size_t j = 0; j < language_codes.size(); ++j) {
752 std::pair<It, It> range =
753 language_code_to_ids_.equal_range(language_codes[j]);
754 It it = range.first;
755 for (; it != range.second; ++it) {
756 if (it->second == input_method.id())
757 break;
759 if (it == range.second)
760 language_code_to_ids_.insert(
761 std::make_pair(language_codes[j], input_method.id()));
766 void InputMethodUtil::ResetInputMethods(const InputMethodDescriptors& imes) {
767 // Clear the existing maps.
768 language_code_to_ids_.clear();
769 id_to_descriptor_.clear();
771 AppendInputMethods(imes);
774 void InputMethodUtil::InitXkbInputMethodsForTesting() {
775 cached_hardware_layouts_.clear();
776 ResetInputMethods(*(InputMethodWhitelist().GetSupportedInputMethods()));
779 const InputMethodUtil::InputMethodIdToDescriptorMap&
780 InputMethodUtil::GetIdToDesciptorMapForTesting() {
781 return id_to_descriptor_;
784 InputMethodDescriptor InputMethodUtil::GetFallbackInputMethodDescriptor() {
785 std::vector<std::string> layouts;
786 layouts.push_back("us");
787 std::vector<std::string> languages;
788 languages.push_back("en-US");
789 return InputMethodDescriptor(
790 extension_ime_util::GetInputMethodIDByEngineID("xkb:us::eng"),
792 "US",
793 layouts,
794 languages,
795 true, // login keyboard.
796 GURL(), // options page, not available.
797 GURL()); // input view page, not available.
800 } // namespace input_method
801 } // namespace chromeos