+static const char kFullNameField[] = "fullName";
+static const char kCompanyNameField[] = "companyName";
+static const char kAddressLineField[] = "addrLines";
+static const char kDependentLocalityField[] = "dependentLocality";
+static const char kCityField[] = "city";
+static const char kStateField[] = "state";
+static const char kPostalCodeField[] = "postalCode";
+static const char kSortingCodeField[] = "sortingCode";
+static const char kCountryField[] = "country";
+
+static const char kComponents[] = "components";
+static const char kLanguageCode[] = "languageCode";
+
+// 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);
+
+ std::vector<AddressUiComponent> components =
+ i18n::addressinput::BuildComponents(
+ country_code, ui_language_code, components_language_code);
+ if (components.empty()) {
+ static const char kDefaultCountryCode[] = "US";
+ components = i18n::addressinput::BuildComponents(
+ kDefaultCountryCode, ui_language_code, components_language_code);
+ }
+ DCHECK(!components.empty());
+
+ base::ListValue* line = NULL;
+ static const char kField[] = "field";
+ static const char kLength[] = "length";
+ 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(line);
+ }
+
+ scoped_ptr<base::DictionaryValue> component(new base::DictionaryValue);
+ component->SetString(
+ "name", l10n_util::GetStringUTF16(components[i].name_id));
+
+ switch (components[i].field) {
+ case i18n::addressinput::COUNTRY:
+ component->SetString(kField, kCountryField);
+ break;
+ case i18n::addressinput::ADMIN_AREA:
+ component->SetString(kField, kStateField);
+ break;
+ case i18n::addressinput::LOCALITY:
+ component->SetString(kField, kCityField);
+ break;
+ case i18n::addressinput::DEPENDENT_LOCALITY:
+ component->SetString(kField, kDependentLocalityField);
+ break;
+ case i18n::addressinput::SORTING_CODE:
+ component->SetString(kField, kSortingCodeField);
+ break;
+ case i18n::addressinput::POSTAL_CODE:
+ component->SetString(kField, kPostalCodeField);
+ break;
+ case i18n::addressinput::STREET_ADDRESS:
+ component->SetString(kField, kAddressLineField);
+ break;
+ case i18n::addressinput::ORGANIZATION:
+ component->SetString(kField, kCompanyNameField);
+ break;
+ case i18n::addressinput::RECIPIENT:
+ component->SetString(kField, kFullNameField);
+ component->SetString(
+ "placeholder",
+ l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_ADD_NAME));
+ break;
+ }
+
+ switch (components[i].length_hint) {
+ case AddressUiComponent::HINT_LONG:
+ component->SetString(kLength, "long");
+ break;
+ case AddressUiComponent::HINT_SHORT:
+ component->SetString(kLength, "short");
+ break;
+ }
+
+ line->Append(component.release());
+ }
+}
+