OLD | NEW |
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 "ui/base/ime/chromeos/input_method_util.h" | 5 #include "ui/base/ime/chromeos/input_method_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <functional> | 10 #include <functional> |
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 | 371 |
372 namespace input_method { | 372 namespace input_method { |
373 | 373 |
374 InputMethodUtil::InputMethodUtil(InputMethodDelegate* delegate) | 374 InputMethodUtil::InputMethodUtil(InputMethodDelegate* delegate) |
375 : delegate_(delegate) { | 375 : delegate_(delegate) { |
376 InputMethodDescriptors default_input_methods; | 376 InputMethodDescriptors default_input_methods; |
377 default_input_methods.push_back(GetFallbackInputMethodDescriptor()); | 377 default_input_methods.push_back(GetFallbackInputMethodDescriptor()); |
378 ResetInputMethods(default_input_methods); | 378 ResetInputMethods(default_input_methods); |
379 | 379 |
380 // Initialize a map from English string to Chrome string resource ID as well. | 380 // Initialize a map from English string to Chrome string resource ID as well. |
| 381 // Since this array is write-once, initialize a flat map in one step with a |
| 382 // given vector storage. |
| 383 // |
| 384 // TODO(brettw) this could be optimized further to binary search in the |
| 385 // static data, avoiding this up-front cost. |
| 386 std::vector<EnglishToIDMap::value_type> map_storage; |
| 387 map_storage.reserve(kEnglishToResourceIdArraySize); |
381 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) { | 388 for (size_t i = 0; i < kEnglishToResourceIdArraySize; ++i) { |
382 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i]; | 389 const EnglishToResouceId& map_entry = kEnglishToResourceIdArray[i]; |
383 const bool result = english_to_resource_id_.insert(std::make_pair( | 390 map_storage.emplace_back(map_entry.english_string_from_ibus, |
384 map_entry.english_string_from_ibus, map_entry.resource_id)).second; | 391 map_entry.resource_id); |
385 DCHECK(result) << "Duplicated string is found: " | |
386 << map_entry.english_string_from_ibus; | |
387 } | 392 } |
| 393 |
| 394 english_to_resource_id_ = |
| 395 EnglishToIDMap(std::move(map_storage), base::KEEP_FIRST_OF_DUPES); |
| 396 DCHECK(english_to_resource_id_.size() == kEnglishToResourceIdArraySize) |
| 397 << "Duplicate string is found"; |
388 } | 398 } |
389 | 399 |
390 InputMethodUtil::~InputMethodUtil() {} | 400 InputMethodUtil::~InputMethodUtil() {} |
391 | 401 |
392 std::string InputMethodUtil::GetLocalizedDisplayName( | 402 std::string InputMethodUtil::GetLocalizedDisplayName( |
393 const InputMethodDescriptor& descriptor) const { | 403 const InputMethodDescriptor& descriptor) const { |
394 // Localizes the input method name. | 404 // Localizes the input method name. |
395 const std::string& disp = descriptor.name(); | 405 const std::string& disp = descriptor.name(); |
396 if (base::StartsWith(disp, "__MSG_", base::CompareCase::SENSITIVE)) { | 406 if (base::StartsWith(disp, "__MSG_", base::CompareCase::SENSITIVE)) { |
397 const InputMethodNameMap* map = kInputMethodNameMap; | 407 const InputMethodNameMap* map = kInputMethodNameMap; |
398 size_t map_size = arraysize(kInputMethodNameMap); | 408 size_t map_size = arraysize(kInputMethodNameMap); |
399 std::string name = base::ToUpperASCII(disp); | 409 std::string name = base::ToUpperASCII(disp); |
400 const InputMethodNameMap map_key = {name.c_str(), 0}; | 410 const InputMethodNameMap map_key = {name.c_str(), 0}; |
401 const InputMethodNameMap* p = | 411 const InputMethodNameMap* p = |
402 std::lower_bound(map, map + map_size, map_key); | 412 std::lower_bound(map, map + map_size, map_key); |
403 if (p != map + map_size && name == p->message_name) | 413 if (p != map + map_size && name == p->message_name) |
404 return l10n_util::GetStringUTF8(p->resource_id); | 414 return l10n_util::GetStringUTF8(p->resource_id); |
405 } | 415 } |
406 return disp; | 416 return disp; |
407 } | 417 } |
408 | 418 |
409 bool InputMethodUtil::TranslateStringInternal( | 419 bool InputMethodUtil::TranslateStringInternal( |
410 const std::string& english_string, base::string16 *out_string) const { | 420 const std::string& english_string, base::string16 *out_string) const { |
411 DCHECK(out_string); | 421 DCHECK(out_string); |
412 // |english_string| could be an input method id. So legacy xkb id is required | 422 // |english_string| could be an input method id. So legacy xkb id is required |
413 // to get the translated string. | 423 // to get the translated string. |
414 std::string key_string = extension_ime_util::MaybeGetLegacyXkbId( | 424 std::string key_string = extension_ime_util::MaybeGetLegacyXkbId( |
415 english_string); | 425 english_string); |
416 HashType::const_iterator iter = english_to_resource_id_.find(key_string); | 426 auto iter = english_to_resource_id_.find(key_string); |
417 | 427 |
418 if (iter == english_to_resource_id_.end()) { | 428 if (iter == english_to_resource_id_.end()) { |
419 // TODO(yusukes): Write Autotest which checks if all display names and all | 429 // TODO(yusukes): Write Autotest which checks if all display names and all |
420 // property names for supported input methods are listed in the resource | 430 // property names for supported input methods are listed in the resource |
421 // ID array (crosbug.com/4572). | 431 // ID array (crosbug.com/4572). |
422 LOG(ERROR) << "Resource ID is not found for: " << english_string | 432 LOG(ERROR) << "Resource ID is not found for: " << english_string |
423 << ", " << key_string; | 433 << ", " << key_string; |
424 return false; | 434 return false; |
425 } | 435 } |
426 | 436 |
(...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 "US", | 832 "US", |
823 layouts, | 833 layouts, |
824 languages, | 834 languages, |
825 true, // login keyboard. | 835 true, // login keyboard. |
826 GURL(), // options page, not available. | 836 GURL(), // options page, not available. |
827 GURL()); // input view page, not available. | 837 GURL()); // input view page, not available. |
828 } | 838 } |
829 | 839 |
830 } // namespace input_method | 840 } // namespace input_method |
831 } // namespace chromeos | 841 } // namespace chromeos |
OLD | NEW |