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

Side by Side Diff: chrome/browser/autofill/autofill_country.cc

Issue 10310029: Reapply "Changes according to review comments" (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use StartsWith Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/autofill/autofill_country.h" 5 #include "chrome/browser/autofill/autofill_country.h"
6 6
7 #include <map> 7 #include <map>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 #include <utility> 10 #include <utility>
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 383
384 // Returns an ICU collator -- i.e. string comparator -- appropriate for the 384 // Returns an ICU collator -- i.e. string comparator -- appropriate for the
385 // given |locale|. 385 // given |locale|.
386 icu::Collator* GetCollatorForLocale(const std::string& locale); 386 icu::Collator* GetCollatorForLocale(const std::string& locale);
387 387
388 // Returns the ICU sort key corresponding to |str| for the given |collator|. 388 // Returns the ICU sort key corresponding to |str| for the given |collator|.
389 // Uses |buffer| as temporary storage, and might resize |buffer| as a side- 389 // Uses |buffer| as temporary storage, and might resize |buffer| as a side-
390 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if 390 // effect. |buffer_size| should specify the |buffer|'s size, and is updated if
391 // the |buffer| is resized. 391 // the |buffer| is resized.
392 const std::string GetSortKey(const icu::Collator& collator, 392 const std::string GetSortKey(const icu::Collator& collator,
393 const icu::UnicodeString& str, 393 const string16& str,
394 scoped_array<uint8_t>* buffer, 394 scoped_array<uint8_t>* buffer,
395 int32_t* buffer_size) const; 395 int32_t* buffer_size) const;
396 396
397 397
398 // Maps from common country names, including 2- and 3-letter country codes, 398 // Maps from common country names, including 2- and 3-letter country codes,
399 // to the corresponding 2-letter country codes. The keys are uppercase ASCII 399 // to the corresponding 2-letter country codes. The keys are uppercase ASCII
400 // strings. 400 // strings.
401 std::map<std::string, std::string> common_names_; 401 std::map<std::string, std::string> common_names_;
402 402
403 // The outer map keys are ICU locale identifiers. 403 // The outer map keys are ICU locale identifiers.
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 return GetCountryCodeForLocalizedName(country, "en_US"); 475 return GetCountryCodeForLocalizedName(country, "en_US");
476 } 476 }
477 477
478 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) { 478 void CountryNames::AddLocalizedNamesForLocale(const std::string& locale) {
479 // Nothing to do if we've previously added the localized names for the given 479 // Nothing to do if we've previously added the localized names for the given
480 // |locale|. 480 // |locale|.
481 if (locales_to_localized_names_.count(locale)) 481 if (locales_to_localized_names_.count(locale))
482 return; 482 return;
483 483
484 std::map<std::string, std::string> localized_names; 484 std::map<std::string, std::string> localized_names;
485
486 icu::Locale icu_locale(locale.c_str());
487 const icu::Collator* collator = GetCollatorForLocale(locale); 485 const icu::Collator* collator = GetCollatorForLocale(locale);
488
489 int32_t buffer_size = 1000; 486 int32_t buffer_size = 1000;
490 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]); 487 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
491 488
492 for (CountryDataMap::Iterator it = CountryDataMap::Begin(); 489 for (CountryDataMap::Iterator it = CountryDataMap::Begin();
493 it != CountryDataMap::End(); 490 it != CountryDataMap::End();
494 ++it) { 491 ++it) {
495 const std::string& country_code = it->first; 492 const std::string& country_code = it->first;
496 493 string16 country_name = l10n_util::GetDisplayNameForCountry(country_code,
497 icu::Locale country_locale(NULL, country_code.c_str()); 494 locale);
498 icu::UnicodeString country_name;
499 country_locale.getDisplayName(icu_locale, country_name);
500 std::string sort_key = GetSortKey(*collator, 495 std::string sort_key = GetSortKey(*collator,
501 country_name, 496 country_name,
502 &buffer, 497 &buffer,
503 &buffer_size); 498 &buffer_size);
504 499
505 localized_names.insert(std::make_pair(sort_key, country_code)); 500 localized_names.insert(std::make_pair(sort_key, country_code));
506 } 501 }
507 502
508 locales_to_localized_names_.insert(std::make_pair(locale, localized_names)); 503 locales_to_localized_names_.insert(std::make_pair(locale, localized_names));
509 } 504 }
510 505
511 const std::string CountryNames::GetCountryCodeForLocalizedName( 506 const std::string CountryNames::GetCountryCodeForLocalizedName(
512 const string16& country_name, 507 const string16& country_name,
513 const std::string& locale) { 508 const std::string& locale) {
514 AddLocalizedNamesForLocale(locale); 509 AddLocalizedNamesForLocale(locale);
515 510
516 icu::Collator* collator = GetCollatorForLocale(locale); 511 icu::Collator* collator = GetCollatorForLocale(locale);
517 512
518 // As recommended[1] by ICU, initialize the buffer size to four times the 513 // As recommended[1] by ICU, initialize the buffer size to four times the
519 // source string length. 514 // source string length.
520 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples 515 // [1] http://userguide.icu-project.org/collation/api#TOC-Examples
521 int32_t buffer_size = country_name.size() * 4; 516 int32_t buffer_size = country_name.size() * 4;
522 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]); 517 scoped_array<uint8_t> buffer(new uint8_t[buffer_size]);
523 std::string sort_key = GetSortKey(*collator, 518 std::string sort_key = GetSortKey(*collator,
524 country_name.c_str(), 519 country_name,
525 &buffer, 520 &buffer,
526 &buffer_size); 521 &buffer_size);
527 522
528 const std::map<std::string, std::string>& localized_names = 523 const std::map<std::string, std::string>& localized_names =
529 locales_to_localized_names_[locale]; 524 locales_to_localized_names_[locale];
530 std::map<std::string, std::string>::const_iterator result = 525 std::map<std::string, std::string>::const_iterator result =
531 localized_names.find(sort_key); 526 localized_names.find(sort_key);
532 527
533 if (result != localized_names.end()) 528 if (result != localized_names.end())
534 return result->second; 529 return result->second;
(...skipping 13 matching lines...) Expand all
548 ignored = U_ZERO_ERROR; 543 ignored = U_ZERO_ERROR;
549 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored); 544 collator->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, ignored);
550 545
551 collators_.insert(std::make_pair(locale, collator)); 546 collators_.insert(std::make_pair(locale, collator));
552 } 547 }
553 548
554 return collators_[locale]; 549 return collators_[locale];
555 } 550 }
556 551
557 const std::string CountryNames::GetSortKey(const icu::Collator& collator, 552 const std::string CountryNames::GetSortKey(const icu::Collator& collator,
558 const icu::UnicodeString& str, 553 const string16& str,
559 scoped_array<uint8_t>* buffer, 554 scoped_array<uint8_t>* buffer,
560 int32_t* buffer_size) const { 555 int32_t* buffer_size) const {
561 DCHECK(buffer); 556 DCHECK(buffer);
562 DCHECK(buffer_size); 557 DCHECK(buffer_size);
563 558
564 int32_t expected_size = collator.getSortKey(str, buffer->get(), *buffer_size); 559 icu::UnicodeString icu_str(str.c_str(), str.length());
560 int32_t expected_size = collator.getSortKey(icu_str, buffer->get(),
561 *buffer_size);
565 if (expected_size > *buffer_size) { 562 if (expected_size > *buffer_size) {
566 // If there wasn't enough space, grow the buffer and try again. 563 // If there wasn't enough space, grow the buffer and try again.
567 *buffer_size = expected_size; 564 *buffer_size = expected_size;
568 buffer->reset(new uint8_t[*buffer_size]); 565 buffer->reset(new uint8_t[*buffer_size]);
569 DCHECK(buffer->get()); 566 DCHECK(buffer->get());
570 567
571 expected_size = collator.getSortKey(str, buffer->get(), *buffer_size); 568 expected_size = collator.getSortKey(icu_str, buffer->get(), *buffer_size);
572 DCHECK_EQ(*buffer_size, expected_size); 569 DCHECK_EQ(*buffer_size, expected_size);
573 } 570 }
574 571
575 return std::string(reinterpret_cast<const char*>(buffer->get())); 572 return std::string(reinterpret_cast<const char*>(buffer->get()));
576 } 573 }
577 574
578 // Returns the country name corresponding to |country_code|, localized to the
579 // |display_locale|.
580 string16 GetDisplayName(const std::string& country_code,
581 const icu::Locale& display_locale) {
582 icu::Locale country_locale(NULL, country_code.c_str());
583 icu::UnicodeString name;
584 country_locale.getDisplayName(display_locale, name);
585
586 DCHECK_GT(name.length(), 0);
587 return string16(name.getBuffer(), name.length());
588 }
589
590 } // namespace 575 } // namespace
591 576
592 AutofillCountry::AutofillCountry(const std::string& country_code, 577 AutofillCountry::AutofillCountry(const std::string& country_code,
593 const std::string& locale) { 578 const std::string& locale) {
594 const CountryDataMap::Iterator result = CountryDataMap::Find(country_code); 579 const CountryDataMap::Iterator result = CountryDataMap::Find(country_code);
595 DCHECK(result != CountryDataMap::End()); 580 DCHECK(result != CountryDataMap::End());
596 const CountryData& data = result->second; 581 const CountryData& data = result->second;
597 582
598 country_code_ = country_code; 583 country_code_ = country_code;
599 name_ = GetDisplayName(country_code, icu::Locale(locale.c_str())); 584 name_ = l10n_util::GetDisplayNameForCountry(country_code, locale);
600 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id); 585 postal_code_label_ = l10n_util::GetStringUTF16(data.postal_code_label_id);
601 state_label_ = l10n_util::GetStringUTF16(data.state_label_id); 586 state_label_ = l10n_util::GetStringUTF16(data.state_label_id);
602 } 587 }
603 588
604 AutofillCountry::~AutofillCountry() { 589 AutofillCountry::~AutofillCountry() {
605 } 590 }
606 591
607 // static 592 // static
608 void AutofillCountry::GetAvailableCountries( 593 void AutofillCountry::GetAvailableCountries(
609 std::vector<std::string>* country_codes) { 594 std::vector<std::string>* country_codes) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 636
652 AutofillCountry::AutofillCountry(const std::string& country_code, 637 AutofillCountry::AutofillCountry(const std::string& country_code,
653 const string16& name, 638 const string16& name,
654 const string16& postal_code_label, 639 const string16& postal_code_label,
655 const string16& state_label) 640 const string16& state_label)
656 : country_code_(country_code), 641 : country_code_(country_code),
657 name_(name), 642 name_(name),
658 postal_code_label_(postal_code_label), 643 postal_code_label_(postal_code_label),
659 state_label_(state_label) { 644 state_label_(state_label) {
660 } 645 }
OLDNEW
« no previous file with comments | « build/android/gtest_filter/ui_unittests_disabled ('k') | testing/android/native_test_launcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698