Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: chrome/browser/chromeos/input_method/input_method_util.cc

Issue 608183002: Move all white-listed input method indicator text from InputMethodUtil to manifests of IME extensio… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/input_method/input_method_util.h" 5 #include "chrome/browser/chromeos/input_method/input_method_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <map> 9 #include <map>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_service.h" 14 #include "base/prefs/pref_service.h"
15 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "chrome/common/extensions/extension_constants.h" 18 #include "chrome/common/extensions/extension_constants.h"
19 // TODO(nona): move this header from this file. 19 // TODO(nona): move this header from this file.
20 #include "chrome/grit/generated_resources.h" 20 #include "chrome/grit/generated_resources.h"
21 #include "chromeos/ime/component_extension_ime_manager.h" 21 #include "chromeos/ime/component_extension_ime_manager.h"
22 #include "chromeos/ime/extension_ime_util.h" 22 #include "chromeos/ime/extension_ime_util.h"
23 // For SetHardwareKeyboardLayoutForTesting. 23 // For SetHardwareKeyboardLayoutForTesting.
24 #include "chromeos/ime/fake_input_method_delegate.h" 24 #include "chromeos/ime/fake_input_method_delegate.h"
25 #include "chromeos/ime/input_method_delegate.h" 25 #include "chromeos/ime/input_method_delegate.h"
26 #include "chromeos/ime/input_method_whitelist.h" 26 #include "chromeos/ime/input_method_whitelist.h"
27 27
28 namespace { 28 namespace {
29 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-pinyin", "\xe6\x8b\xbc" }, // U+62FC
46 { "zh-hant-t-i0-und", "\xE6\xB3\xA8" }, // U+9177
47 { "zh-hant-t-i0-cangjie-1987", "\xe5\x80\x89" }, // U+5009
48 { "zh-hant-t-i0-cangjie-1987-x-m0-simplified", "\xe9\x80\x9f" }, // U+901F
49 // For Hangul input method.
50 { "hangul_ahnmatae", "\xed\x95\x9c" }, // U+D55C
51 { "hangul_2set", "\xed\x95\x9c" }, // U+D55C
52 { "hangul_3set390", "\xed\x95\x9c" }, // U+D55C
53 { "hangul_3setfinal", "\xed\x95\x9c" }, // U+D55C
54 { "hangul_3setnoshift", "\xed\x95\x9c" }, // U+D55C
55 { "hangul_romaja", "\xed\x95\x9c" }, // U+D55C
56 { extension_misc::kBrailleImeEngineId,
57 // U+2803 U+2817 U+2807 (Unicode braille patterns for the letters 'brl' in
58 // English (and many other) braille codes.
59 "\xe2\xa0\x83\xe2\xa0\x97\xe2\xa0\x87" },
60 };
61
62 const size_t kMappingFromIdToIndicatorTextLen =
63 ARRAYSIZE_UNSAFE(kMappingFromIdToIndicatorText);
64
65 // A mapping from an input method id to a resource id for a 30 // A mapping from an input method id to a resource id for a
66 // medium length language indicator. 31 // medium length language indicator.
67 // For those languages that want to display a slightly longer text in the 32 // For those languages that want to display a slightly longer text in the
68 // "Your input method has changed to..." bubble than in the status tray. 33 // "Your input method has changed to..." bubble than in the status tray.
69 // If an entry is not found in this table the short name is used. 34 // If an entry is not found in this table the short name is used.
70 const struct { 35 const struct {
71 const char* engine_id; 36 const char* engine_id;
72 const int resource_id; 37 const int resource_id;
73 } kMappingImeIdToMediumLenNameResourceId[] = { 38 } kMappingImeIdToMediumLenNameResourceId[] = {
74 { "hangul_2set", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN }, 39 { "hangul_2set", IDS_LANGUAGES_MEDIUM_LEN_NAME_KOREAN },
(...skipping 24 matching lines...) Expand all
99 const char* engine_id; 64 const char* engine_id;
100 } kDefaultInputMethodRecommendation[] = { 65 } kDefaultInputMethodRecommendation[] = {
101 { "ja", "us", "nacl_mozc_us" }, 66 { "ja", "us", "nacl_mozc_us" },
102 { "ja", "jp", "nacl_mozc_jp" }, 67 { "ja", "jp", "nacl_mozc_jp" },
103 { "zh-CN", "us", "zh-t-i0-pinyin" }, 68 { "zh-CN", "us", "zh-t-i0-pinyin" },
104 { "zh-TW", "us", "zh-hant-t-i0-und" }, 69 { "zh-TW", "us", "zh-hant-t-i0-und" },
105 { "th", "us", "vkd_th" }, 70 { "th", "us", "vkd_th" },
106 { "vi", "us", "vkd_vi_tcvn" }, 71 { "vi", "us", "vkd_vi_tcvn" },
107 }; 72 };
108 73
109 // The map from xkb layout to the indicator text.
110 // Refer to crbug.com/349829.
111 const char* const kXkbIndicators[][2] = {{"am", "AM"},
112 {"be", "BE"},
113 {"bg", "BG"},
114 {"bg(phonetic)", "BG"},
115 {"br", "BR"},
116 {"by", "BY"},
117 {"ca", "CA"},
118 {"ca(eng)", "CA"},
119 {"ca(multix)", "CA"},
120 {"ch", "CH"},
121 {"ch(fr)", "CH"},
122 {"cz", "CZ"},
123 {"cz(qwerty)", "CS"},
124 {"de", "DE"},
125 {"de(neo)", "NEO"},
126 {"dk", "DK"},
127 {"ee", "EE"},
128 {"es", "ES"},
129 {"es(cat)", "CAS"},
130 {"fi", "FI"},
131 {"fr", "FR"},
132 {"gb(dvorak)", "DV"},
133 {"gb(extd)", "GB"},
134 {"ge", "GE"},
135 {"gr", "GR"},
136 {"hr", "HR"},
137 {"hu", "HU"},
138 {"il", "IL"},
139 {"is", "IS"},
140 {"it", "IT"},
141 {"jp", "JA"},
142 {"latam", "LA"},
143 {"lt", "LT"},
144 {"lv(apostrophe)", "LV"},
145 {"mn", "MN"},
146 {"no", "NO"},
147 {"pl", "PL"},
148 {"pt", "PT"},
149 {"ro", "RO"},
150 {"rs", "RS"},
151 {"ru", "RU"},
152 {"ru(phonetic)", "RU"},
153 {"se", "SE"},
154 {"si", "SI"},
155 {"sk", "SK"},
156 {"tr", "TR"},
157 {"ua", "UA"},
158 {"us", "US"},
159 {"us(altgr-intl)", "EXTD"},
160 {"us(colemak)", "CO"},
161 {"us(dvorak)", "DV"},
162 {"us(intl)", "INTL"}, };
163
164 // The extension ID map for migration. 74 // The extension ID map for migration.
165 const char* const kExtensionIdMigrationMap[][2] = { 75 const char* const kExtensionIdMigrationMap[][2] = {
166 // Official Japanese IME extension ID. 76 // Official Japanese IME extension ID.
167 {"fpfbhcjppmaeaijcidgiibchfbnhbelj", "gjaehgfemfahhmlgpdfknkhdnemmolop"}, 77 {"fpfbhcjppmaeaijcidgiibchfbnhbelj", "gjaehgfemfahhmlgpdfknkhdnemmolop"},
168 // Official M17n keyboard extension ID. 78 // Official M17n keyboard extension ID.
169 {"habcdindjejkmepknlhkkloncjcpcnbf", "gjaehgfemfahhmlgpdfknkhdnemmolop"}, 79 {"habcdindjejkmepknlhkkloncjcpcnbf", "gjaehgfemfahhmlgpdfknkhdnemmolop"},
170 }; 80 };
171 81
172 // The engine ID map for migration. This migration is for input method IDs from 82 // The engine ID map for migration. This migration is for input method IDs from
173 // VPD so it's NOT a temporary migration. 83 // VPD so it's NOT a temporary migration.
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 ResetInputMethods(default_input_methods); 202 ResetInputMethods(default_input_methods);
293 203
294 // Initialize a map from English string to Chrome string resource ID as well. 204 // Initialize a map from English string to Chrome string resource ID as well.
295 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) { 205 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) {
296 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i]; 206 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i];
297 const bool result = english_to_resource_id_.insert(std::make_pair( 207 const bool result = english_to_resource_id_.insert(std::make_pair(
298 map_entry.english_string_from_ibus, map_entry.resource_id)).second; 208 map_entry.english_string_from_ibus, map_entry.resource_id)).second;
299 DCHECK(result) << "Duplicated string is found: " 209 DCHECK(result) << "Duplicated string is found: "
300 << map_entry.english_string_from_ibus; 210 << map_entry.english_string_from_ibus;
301 } 211 }
302
303 // Initialize the map from xkb layout to indicator text.
304 for (size_t i = 0; i < arraysize(kXkbIndicators); ++i) {
305 xkb_layout_to_indicator_[kXkbIndicators[i][0]] = kXkbIndicators[i][1];
306 }
307 } 212 }
308 213
309 InputMethodUtil::~InputMethodUtil() { 214 InputMethodUtil::~InputMethodUtil() {
310 } 215 }
311 216
312 bool InputMethodUtil::TranslateStringInternal( 217 bool InputMethodUtil::TranslateStringInternal(
313 const std::string& english_string, base::string16 *out_string) const { 218 const std::string& english_string, base::string16 *out_string) const {
314 DCHECK(out_string); 219 DCHECK(out_string);
315 // |english_string| could be an input method id. So legacy xkb id is required 220 // |english_string| could be an input method id. So legacy xkb id is required
316 // to get the translated string. 221 // to get the translated string.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
368 if (!extension_ime_util::IsExtensionIME(input_method_id) && 273 if (!extension_ime_util::IsExtensionIME(input_method_id) &&
369 TranslateStringInternal(input_method_id, &display_name)) { 274 TranslateStringInternal(input_method_id, &display_name)) {
370 return base::UTF16ToUTF8(display_name); 275 return base::UTF16ToUTF8(display_name);
371 } 276 }
372 // Return an empty string if the display name is not found. 277 // Return an empty string if the display name is not found.
373 return ""; 278 return "";
374 } 279 }
375 280
376 base::string16 InputMethodUtil::GetInputMethodShortName( 281 base::string16 InputMethodUtil::GetInputMethodShortName(
377 const InputMethodDescriptor& input_method) const { 282 const InputMethodDescriptor& input_method) const {
378 // For the status area, we use two-letter, upper-case language code like 283 // TODO(shuchen): remove this method.
Peter Lundblad 2014/09/29 12:20:47 Does 'this method' refer to the one being called o
Shu Chen 2014/09/29 14:48:08 Done.
379 // "US" and "JP". 284 return base::UTF8ToUTF16(input_method.GetIndicator());
380
381 // Use the indicator string if set.
382 if (!input_method.indicator().empty()) {
383 return base::UTF8ToUTF16(input_method.indicator());
384 }
385
386 base::string16 text;
387 // Check special cases first.
388 for (size_t i = 0; i < kMappingFromIdToIndicatorTextLen; ++i) {
389 if (extension_ime_util::GetInputMethodIDByEngineID(
390 kMappingFromIdToIndicatorText[i].engine_id) == input_method.id()) {
391 text = base::UTF8ToUTF16(kMappingFromIdToIndicatorText[i].indicator_text);
392 break;
393 }
394 }
395
396 // Display the keyboard layout name when using a keyboard layout.
397 if (text.empty() && IsKeyboardLayout(input_method.id())) {
398 std::map<std::string, std::string>::const_iterator it =
399 xkb_layout_to_indicator_.find(GetKeyboardLayoutName(input_method.id()));
400 if (it != xkb_layout_to_indicator_.end())
401 text = base::UTF8ToUTF16(it->second);
402 }
403
404 // TODO(yusukes): Some languages have two or more input methods. For example,
405 // Thai has 3, Vietnamese has 4. If these input methods could be activated at
406 // the same time, we should do either of the following:
407 // (1) Add mappings to |kMappingFromIdToIndicatorText|
408 // (2) Add suffix (1, 2, ...) to |text| when ambiguous.
409
410 if (text.empty()) {
411 const size_t kMaxLanguageNameLen = 2;
412 DCHECK(!input_method.language_codes().empty());
413 const std::string language_code = input_method.language_codes().at(0);
414 text = StringToUpperASCII(base::UTF8ToUTF16(language_code)).substr(
415 0, kMaxLanguageNameLen);
416 }
417 DCHECK(!text.empty()) << input_method.id();
418 return text;
419 } 285 }
420 286
421 base::string16 InputMethodUtil::GetInputMethodMediumName( 287 base::string16 InputMethodUtil::GetInputMethodMediumName(
422 const InputMethodDescriptor& input_method) const { 288 const InputMethodDescriptor& input_method) const {
423 // For the "Your input method has changed to..." bubble. In most cases 289 // For the "Your input method has changed to..." bubble. In most cases
424 // it uses the same name as the short name, unless found in a table 290 // it uses the same name as the short name, unless found in a table
425 // for medium length names. 291 // for medium length names.
426 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) { 292 for (size_t i = 0; i < kMappingImeIdToMediumLenNameResourceIdLen; ++i) {
427 if (extension_ime_util::GetInputMethodIDByEngineID( 293 if (extension_ime_util::GetInputMethodIDByEngineID(
428 kMappingImeIdToMediumLenNameResourceId[i].engine_id) == 294 kMappingImeIdToMediumLenNameResourceId[i].engine_id) ==
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 "US", 620 "US",
755 layouts, 621 layouts,
756 languages, 622 languages,
757 true, // login keyboard. 623 true, // login keyboard.
758 GURL(), // options page, not available. 624 GURL(), // options page, not available.
759 GURL()); // input view page, not available. 625 GURL()); // input view page, not available.
760 } 626 }
761 627
762 } // namespace input_method 628 } // namespace input_method
763 } // namespace chromeos 629 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698