File: country_combobox_model.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (105 lines) | stat: -rw-r--r-- 3,747 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/autofill/country_combobox_model.h"

#include <algorithm>
#include <iterator>

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/personal_data_manager.h"
#include "ui/base/l10n/l10n_util_collator.h"
#include "ui/base/models/combobox_model_observer.h"

// TODO(rouslan): Remove this check. http://crbug.com/337587
#if defined(ENABLE_AUTOFILL_DIALOG)
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
#endif

namespace autofill {

CountryComboboxModel::CountryComboboxModel() {}

CountryComboboxModel::~CountryComboboxModel() {}

void CountryComboboxModel::SetCountries(
    const PersonalDataManager& manager,
    const base::Callback<bool(const std::string&)>& filter) {
  countries_.clear();

  // Insert the default country at the top as well as in the ordered list.
  std::string default_country_code =
      manager.GetDefaultCountryCodeForNewAddress();
  DCHECK(!default_country_code.empty());

  const std::string& app_locale = g_browser_process->GetApplicationLocale();
  if (filter.is_null() || filter.Run(default_country_code)) {
    countries_.push_back(new AutofillCountry(default_country_code, app_locale));
#if !defined(OS_ANDROID)
    // The separator item. On Android, there are separators after all items, so
    // this is unnecessary.
    countries_.push_back(NULL);
#endif
  }

  // The sorted list of countries.
  std::vector<std::string> available_countries;
  AutofillCountry::GetAvailableCountries(&available_countries);

#if defined(ENABLE_AUTOFILL_DIALOG)
  // Filter out the countries that do not have rules for address input and
  // validation.
  const std::vector<std::string>& addressinput_countries =
      ::i18n::addressinput::GetRegionCodes();
  std::vector<std::string> filtered_countries;
  filtered_countries.reserve(available_countries.size());
  std::set_intersection(available_countries.begin(),
                        available_countries.end(),
                        addressinput_countries.begin(),
                        addressinput_countries.end(),
                        std::back_inserter(filtered_countries));
  available_countries.swap(filtered_countries);
#endif

  std::vector<AutofillCountry*> sorted_countries;
  for (std::vector<std::string>::const_iterator it =
           available_countries.begin(); it != available_countries.end(); ++it) {
    if (filter.is_null() || filter.Run(*it))
      sorted_countries.push_back(new AutofillCountry(*it, app_locale));
  }

  l10n_util::SortStringsUsingMethod(app_locale,
                                    &sorted_countries,
                                    &AutofillCountry::name);
  countries_.insert(countries_.end(),
                    sorted_countries.begin(),
                    sorted_countries.end());
}

int CountryComboboxModel::GetItemCount() const {
  return countries_.size();
}

base::string16 CountryComboboxModel::GetItemAt(int index) {
  AutofillCountry* country = countries_[index];
  if (country)
    return countries_[index]->name();

  // The separator item. Implemented for platforms that don't yet support
  // IsItemSeparatorAt().
  return base::ASCIIToUTF16("---");
}

bool CountryComboboxModel::IsItemSeparatorAt(int index) {
  return !countries_[index];
}

std::string CountryComboboxModel::GetDefaultCountryCode() const {
  return countries_[GetDefaultIndex()]->country_code();
}

}  // namespace autofill