File: country_combobox_model.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,373 bytes parent folder | download | duplicates (5)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/core/browser/ui/country_combobox_model.h"

#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>

#include "base/check.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/geo/autofill_country.h"
#include "components/autofill/core/browser/geo/country_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
#include "ui/base/l10n/l10n_util_collator.h"
#include "ui/base/models/combobox_model_observer.h"

namespace autofill {

CountryComboboxModel::CountryComboboxModel() = default;

CountryComboboxModel::~CountryComboboxModel() = default;

void CountryComboboxModel::SetCountries(
    const GeoIpCountryCode& geo_ip_country_code,
    const std::string& app_locale) {
  countries_.clear();

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

  countries_.push_back(
      std::make_unique<AutofillCountry>(default_country_code, app_locale));
#if !BUILDFLAG(IS_ANDROID)
  // The separator item. On Android, there are separators after all items, so
  // this is unnecessary.
  countries_.push_back(nullptr);
#endif

  // The sorted list of country codes.
  const std::vector<std::string>* available_countries =
      &CountryDataMap::GetInstance()->country_codes();

  // 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 = &filtered_countries;

  CountryVector sorted_countries;
  for (const auto& country_code : *available_countries) {
    sorted_countries.push_back(
        std::make_unique<AutofillCountry>(country_code, app_locale));
  }

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

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

std::u16string CountryComboboxModel::GetItemAt(size_t index) const {
  AutofillCountry* country = countries_[index].get();
  if (country)
    return countries_[index]->name();

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

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

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

}  // namespace autofill