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

Side by Side Diff: chrome/browser/autofill/wallet/wallet_address.cc

Issue 12434004: Move remaining Autofill code to //components/autofill. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months 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 (c) 2012 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/autofill/wallet/wallet_address.h"
6
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/autofill/autofill_country.h"
11
12 namespace autofill {
13 namespace wallet {
14
15 namespace {
16
17 Address* CreateAddressInternal(const base::DictionaryValue& dictionary,
18 const std::string& object_id) {
19 std::string country_name_code;
20 if (!dictionary.GetString("postal_address.country_name_code",
21 &country_name_code)) {
22 DLOG(ERROR) << "Response from Google Wallet missing country name";
23 return NULL;
24 }
25
26 string16 recipient_name;
27 if (!dictionary.GetString("postal_address.recipient_name",
28 &recipient_name)) {
29 DLOG(ERROR) << "Response from Google Wallet recipient name";
30 return NULL;
31 }
32
33 string16 postal_code_number;
34 if (!dictionary.GetString("postal_address.postal_code_number",
35 &postal_code_number)) {
36 DLOG(ERROR) << "Response from Google Wallet missing postal code number";
37 return NULL;
38 }
39
40 string16 phone_number;
41 if (!dictionary.GetString("phone_number", &phone_number))
42 DVLOG(1) << "Response from Google Wallet missing phone number";
43
44 string16 address_line_1;
45 string16 address_line_2;
46 const ListValue* address_line_list;
47 if (dictionary.GetList("postal_address.address_line", &address_line_list)) {
48 if (!address_line_list->GetString(0, &address_line_1))
49 DVLOG(1) << "Response from Google Wallet missing address line 1";
50 if (!address_line_list->GetString(1, &address_line_2))
51 DVLOG(1) << "Response from Google Wallet missing address line 2";
52 } else {
53 DVLOG(1) << "Response from Google Wallet missing address lines";
54 }
55
56 string16 locality_name;
57 if (!dictionary.GetString("postal_address.locality_name",
58 &locality_name)) {
59 DVLOG(1) << "Response from Google Wallet missing locality name";
60 }
61
62 string16 administrative_area_name;
63 if (!dictionary.GetString("postal_address.administrative_area_name",
64 &administrative_area_name)) {
65 DVLOG(1) << "Response from Google Wallet missing administrative area name";
66 }
67
68 return new Address(country_name_code,
69 recipient_name ,
70 address_line_1,
71 address_line_2,
72 locality_name,
73 administrative_area_name,
74 postal_code_number,
75 phone_number,
76 object_id);
77 }
78
79 } // namespace
80
81 Address::Address() {}
82
83 Address::Address(const std::string& country_name_code,
84 const string16& recipient_name,
85 const string16& address_line_1,
86 const string16& address_line_2,
87 const string16& locality_name,
88 const string16& administrative_area_name,
89 const string16& postal_code_number,
90 const string16& phone_number,
91 const std::string& object_id)
92 : country_name_code_(country_name_code),
93 recipient_name_(recipient_name),
94 address_line_1_(address_line_1),
95 address_line_2_(address_line_2),
96 locality_name_(locality_name),
97 administrative_area_name_(administrative_area_name),
98 postal_code_number_(postal_code_number),
99 phone_number_(phone_number),
100 object_id_(object_id) {}
101
102 Address::~Address() {}
103
104 scoped_ptr<base::DictionaryValue> Address::ToDictionaryWithID() const {
105 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
106
107 if (!object_id_.empty())
108 dict->SetString("id", object_id_);
109 dict->SetString("phone_number", phone_number_);
110 dict->Set("postal_address", ToDictionaryWithoutID().release());
111
112 return dict.Pass();
113 }
114
115 scoped_ptr<base::DictionaryValue> Address::ToDictionaryWithoutID() const {
116 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
117
118 scoped_ptr<base::ListValue> address_lines(new base::ListValue());
119 address_lines->AppendString(address_line_1_);
120 if (!address_line_2_.empty())
121 address_lines->AppendString(address_line_2_);
122 dict->Set("address_line", address_lines.release());
123
124 dict->SetString("country_name_code", country_name_code_);
125 dict->SetString("recipient_name", recipient_name_);
126 dict->SetString("locality_name", locality_name_);
127 dict->SetString("administrative_area_name",
128 administrative_area_name_);
129 dict->SetString("postal_code_number", postal_code_number_);
130
131 return dict.Pass();
132 }
133
134 string16 Address::DisplayName() const {
135 // TODO(estade): improve this stub implementation.
136 return recipient_name() + ASCIIToUTF16(", ") + address_line_1();
137 }
138
139 string16 Address::GetInfo(AutofillFieldType type) const {
140 switch (type) {
141 case NAME_FULL:
142 return recipient_name();
143
144 case ADDRESS_HOME_LINE1:
145 return address_line_1();
146
147 case ADDRESS_HOME_LINE2:
148 return address_line_2();
149
150 case ADDRESS_HOME_CITY:
151 return locality_name();
152
153 case ADDRESS_HOME_STATE:
154 return administrative_area_name();
155
156 case ADDRESS_HOME_ZIP:
157 return postal_code_number();
158
159 case ADDRESS_HOME_COUNTRY: {
160 AutofillCountry country(country_name_code(),
161 AutofillCountry::ApplicationLocale());
162 return country.name();
163 }
164
165 case PHONE_HOME_WHOLE_NUMBER:
166 return phone_number();
167
168 // TODO(estade): implement more.
169 default:
170 NOTREACHED();
171 return string16();
172 }
173 }
174
175 scoped_ptr<Address> Address::CreateAddressWithID(
176 const base::DictionaryValue& dictionary) {
177 std::string object_id;
178 if (!dictionary.GetString("id", &object_id)) {
179 DLOG(ERROR) << "Response from Google Wallet missing object id";
180 return scoped_ptr<Address>();
181 }
182 return scoped_ptr<Address>(CreateAddressInternal(dictionary, object_id));
183 }
184
185 scoped_ptr<Address> Address::CreateAddress(
186 const base::DictionaryValue& dictionary) {
187 std::string object_id;
188 dictionary.GetString("id", &object_id);
189 return scoped_ptr<Address>(CreateAddressInternal(dictionary, object_id));
190 }
191
192 scoped_ptr<Address> Address::CreateDisplayAddress(
193 const base::DictionaryValue& dictionary) {
194 std::string country_code;
195 if (!dictionary.GetString("country_code", &country_code)) {
196 DLOG(ERROR) << "Reponse from Google Wallet missing country code";
197 return scoped_ptr<Address>();
198 }
199
200 string16 name;
201 if (!dictionary.GetString("name", &name)) {
202 DLOG(ERROR) << "Reponse from Google Wallet missing name";
203 return scoped_ptr<Address>();
204 }
205
206 string16 postal_code;
207 if (!dictionary.GetString("postal_code", &postal_code)) {
208 DLOG(ERROR) << "Reponse from Google Wallet missing postal code";
209 return scoped_ptr<Address>();
210 }
211
212 string16 address1;
213 if (!dictionary.GetString("address1", &address1))
214 DVLOG(1) << "Reponse from Google Wallet missing address1";
215
216 string16 address2;
217 if (!dictionary.GetString("address2", &address2))
218 DVLOG(1) << "Reponse from Google Wallet missing address2";
219
220 string16 city;
221 if (!dictionary.GetString("city", &city))
222 DVLOG(1) << "Reponse from Google Wallet missing city";
223
224 string16 state;
225 if (!dictionary.GetString("state", &state))
226 DVLOG(1) << "Reponse from Google Wallet missing state";
227
228 string16 phone_number;
229 if (!dictionary.GetString("phone_number", &phone_number))
230 DVLOG(1) << "Reponse from Google Wallet missing phone number";
231
232 return scoped_ptr<Address>(new Address(country_code,
233 name,
234 address1,
235 address2,
236 city,
237 state,
238 postal_code,
239 phone_number,
240 std::string()));
241 }
242
243 bool Address::operator==(const Address& other) const {
244 return country_name_code_ == other.country_name_code_ &&
245 recipient_name_ == other.recipient_name_ &&
246 address_line_1_ == other.address_line_1_ &&
247 address_line_2_ == other.address_line_2_ &&
248 locality_name_ == other.locality_name_ &&
249 administrative_area_name_ == other.administrative_area_name_ &&
250 postal_code_number_ == other.postal_code_number_ &&
251 phone_number_ == other.phone_number_ &&
252 object_id_ == other.object_id_;
253 }
254
255 bool Address::operator!=(const Address& other) const {
256 return !(*this == other);
257 }
258
259 } // namespace wallet
260 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698