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

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_i18n_input.cc

Issue 63053003: Ask libaddressinput for address components to use in requestAutocomplete(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years 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
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
6
7 #include <algorithm>
8
9 #include "base/command_line.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "components/autofill/core/browser/autofill_country.h"
14 #include "components/autofill/core/browser/autofill_type.h"
15 #include "components/autofill/core/browser/field_types.h"
16 #include "grit/component_strings.h"
17 #include "grit/generated_resources.h"
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi eld.h"
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h"
20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui _component.h"
21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/localizati on.h"
22 #include "ui/base/l10n/l10n_util.h"
23
24 namespace autofill {
25 namespace i18ninput {
26
27 namespace {
28
29 using i18n::addressinput::AddressField;
30 using i18n::addressinput::AddressUiComponent;
31
32 ServerFieldType GetServerType(AddressField address_field, bool billing) {
33 switch (address_field) {
34 case i18n::addressinput::COUNTRY:
35 return billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
36 case i18n::addressinput::ADMIN_AREA:
37 return billing ? ADDRESS_BILLING_STATE : ADDRESS_HOME_STATE;
38 case i18n::addressinput::LOCALITY:
39 return billing ? ADDRESS_BILLING_CITY : ADDRESS_HOME_CITY;
40 case i18n::addressinput::DEPENDENT_LOCALITY:
41 return billing ? ADDRESS_BILLING_DEPENDENT_LOCALITY :
42 ADDRESS_HOME_DEPENDENT_LOCALITY;
43 case i18n::addressinput::POSTAL_CODE:
44 return billing ? ADDRESS_BILLING_ZIP : ADDRESS_HOME_ZIP;
45 case i18n::addressinput::SORTING_CODE:
46 return billing ? ADDRESS_BILLING_SORTING_CODE : ADDRESS_HOME_SORTING_CODE;
47 case i18n::addressinput::STREET_ADDRESS:
48 return billing ? ADDRESS_BILLING_LINE1 : ADDRESS_HOME_LINE1;
49 case i18n::addressinput::RECIPIENT:
50 return billing ? NAME_BILLING_FULL : NAME_FULL;
51 case i18n::addressinput::ORGANIZATION:
52 return COMPANY_NAME;
53 }
54 NOTREACHED();
55 return UNKNOWN_TYPE;
56 }
57
58 } // namespace
59
60 bool IsI18nAddressInputEnabled() {
61 return CommandLine::ForCurrentProcess()->HasSwitch(
62 ::switches::kEnableAutofillAddressInternationalization);
63 }
64
65 void BuildI18nInputsForCountry(AddressType address_type,
66 const std::string& country_code,
67 DetailInputs* inputs) {
68 i18n::addressinput::Localization localization;
69 // TODO(dbeam): figure out how to include libaddressinput's translations into
70 // some .pak file so I can call |SetGetter(&l10n_util::GetStringUTF8)| here.
71 std::vector<AddressUiComponent> components(
72 i18n::addressinput::BuildComponents(country_code, localization));
73
74 // Address row ids are 100-200. See common::BuildDetailInputs() for ranges.
75 int row_id = 100;
76 const bool billing = address_type == ADDRESS_TYPE_BILLING;
77
78 for (size_t i = 0; i < components.size(); ++i) {
79 const AddressUiComponent& component = components[i];
80 if (component.field == i18n::addressinput::ORGANIZATION) {
81 // TODO(dbeam): figure out when we actually need this.
82 continue;
83 }
84
85 if (component.length_hint == AddressUiComponent::HINT_LONG)
86 ++row_id;
87
88 ServerFieldType server_type = GetServerType(component.field, billing);
89 DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) };
90 inputs->push_back(input);
91
92 if (component.field == i18n::addressinput::STREET_ADDRESS) {
93 if (component.length_hint == AddressUiComponent::HINT_LONG)
94 ++row_id;
95
96 // TODO(dbeam): support more than 2 address lines. http://crbug.com/324889
97 ServerFieldType server_type =
98 billing ? ADDRESS_BILLING_LINE2 : ADDRESS_HOME_LINE2;
99 DetailInput input = { row_id, server_type, UTF8ToUTF16(component.name) };
100 inputs->push_back(input);
101 }
102
103 if (component.length_hint == AddressUiComponent::HINT_LONG)
104 ++row_id;
105 }
106
107 // Also add a country input so the user can switch countries.
108 ServerFieldType server_type =
109 billing ? ADDRESS_BILLING_COUNTRY : ADDRESS_HOME_COUNTRY;
110 base::string16 placeholder_text =
111 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_COUNTRY);
112 DetailInput input = { row_id + 1, server_type, placeholder_text };
113 inputs->push_back(input);
114 }
115
116 } // namespace i18ninput
117 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698