summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/autofill_address_util.cc
blob: c232e0026513379e93b646177282b74d02c7b41f (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2017 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 "components/autofill/core/browser/autofill_address_util.h"

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "components/autofill/core/browser/autofill_country.h"
#include "components/autofill/core/browser/country_combobox_model.h"
#include "components/autofill/core/browser/field_types.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui_component.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/localization.h"
#include "ui/base/l10n/l10n_util.h"

using autofill::AutofillCountry;
using autofill::ServerFieldType;
using i18n::addressinput::AddressUiComponent;

namespace autofill {

// Dictionary keys for address components info.
const char kFieldTypeKey[] = "field";
const char kFieldLengthKey[] = "length";
const char kFieldNameKey[] = "name";

// Field names for the address components.
const char kFullNameField[] = "fullName";
const char kCompanyNameField[] = "companyName";
const char kAddressLineField[] = "addrLines";
const char kDependentLocalityField[] = "dependentLocality";
const char kCityField[] = "city";
const char kStateField[] = "state";
const char kPostalCodeField[] = "postalCode";
const char kSortingCodeField[] = "sortingCode";
const char kCountryField[] = "country";

// Address field length values.
const char kShortField[] = "short";
const char kLongField[] = "long";

// Fills |components| with the address UI components that should be used to
// input an address for |country_code| when UI BCP 47 language code is
// |ui_language_code|. If |components_language_code| is not NULL, then sets it
// to the BCP 47 language code that should be used to format the address for
// display.
void GetAddressComponents(const std::string& country_code,
                          const std::string& ui_language_code,
                          base::ListValue* address_components,
                          std::string* components_language_code) {
  DCHECK(address_components);

  i18n::addressinput::Localization localization;
  localization.SetGetter(l10n_util::GetStringUTF8);
  std::string not_used;
  std::vector<AddressUiComponent> components =
      i18n::addressinput::BuildComponents(
          country_code, localization, ui_language_code,
          components_language_code ? components_language_code : &not_used);
  if (components.empty()) {
    static const char kDefaultCountryCode[] = "US";
    components = i18n::addressinput::BuildComponents(
        kDefaultCountryCode, localization, ui_language_code,
        components_language_code ? components_language_code : &not_used);
  }
  DCHECK(!components.empty());

  base::ListValue* line = nullptr;
  for (size_t i = 0; i < components.size(); ++i) {
    if (i == 0 ||
        components[i - 1].length_hint == AddressUiComponent::HINT_LONG ||
        components[i].length_hint == AddressUiComponent::HINT_LONG) {
      line = new base::ListValue;
      address_components->Append(base::WrapUnique(line));
      // |line| is invalidated at this point, so it needs to be reset.
      address_components->GetList(address_components->GetSize() - 1, &line);
    }

    std::unique_ptr<base::DictionaryValue> component(new base::DictionaryValue);
    component->SetString(kFieldNameKey, components[i].name);

    switch (components[i].field) {
      case i18n::addressinput::COUNTRY:
        component->SetString(kFieldTypeKey, kCountryField);
        break;
      case i18n::addressinput::ADMIN_AREA:
        component->SetString(kFieldTypeKey, kStateField);
        break;
      case i18n::addressinput::LOCALITY:
        component->SetString(kFieldTypeKey, kCityField);
        break;
      case i18n::addressinput::DEPENDENT_LOCALITY:
        component->SetString(kFieldTypeKey, kDependentLocalityField);
        break;
      case i18n::addressinput::SORTING_CODE:
        component->SetString(kFieldTypeKey, kSortingCodeField);
        break;
      case i18n::addressinput::POSTAL_CODE:
        component->SetString(kFieldTypeKey, kPostalCodeField);
        break;
      case i18n::addressinput::STREET_ADDRESS:
        component->SetString(kFieldTypeKey, kAddressLineField);
        break;
      case i18n::addressinput::ORGANIZATION:
        component->SetString(kFieldTypeKey, kCompanyNameField);
        break;
      case i18n::addressinput::RECIPIENT:
        component->SetString(kFieldTypeKey, kFullNameField);
        break;
    }

    switch (components[i].length_hint) {
      case AddressUiComponent::HINT_LONG:
        component->SetString(kFieldLengthKey, kLongField);
        break;
      case AddressUiComponent::HINT_SHORT:
        component->SetString(kFieldLengthKey, kShortField);
        break;
    }

    line->Append(std::move(component));
  }
}

// Sets data related to the country <select>.
void SetCountryData(const PersonalDataManager& manager,
                    base::DictionaryValue* localized_strings,
                    const std::string& ui_language_code) {
  autofill::CountryComboboxModel model;
  model.SetCountries(manager, base::Callback<bool(const std::string&)>(),
                     ui_language_code);
  const std::vector<std::unique_ptr<autofill::AutofillCountry>>& countries =
      model.countries();
  localized_strings->SetString("defaultCountryCode",
                               countries.front()->country_code());

  // An ordered list of options to show in the <select>.
  std::unique_ptr<base::ListValue> country_list(new base::ListValue());
  for (size_t i = 0; i < countries.size(); ++i) {
    std::unique_ptr<base::DictionaryValue> option_details(
        new base::DictionaryValue());
    option_details->SetString("name", model.GetItemAt(i));
    option_details->SetString(
        "value", countries[i] ? countries[i]->country_code() : "separator");
    country_list->Append(std::move(option_details));
  }
  localized_strings->Set("autofillCountrySelectList", country_list.release());

  std::unique_ptr<base::ListValue> default_country_components(
      new base::ListValue);
  std::string default_country_language_code;
  GetAddressComponents(countries.front()->country_code(), ui_language_code,
                       default_country_components.get(),
                       &default_country_language_code);
  localized_strings->Set("autofillDefaultCountryComponents",
                         default_country_components.release());
  localized_strings->SetString("autofillDefaultCountryLanguageCode",
                               default_country_language_code);
}

}  // namespace autofill