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

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

Issue 11293078: Integrating Online Wallet into Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed linter issue Created 8 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 (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_items.h"
6
7 #include "base/logging.h"
8 #include "base/values.h"
9
10 namespace wallet {
11
12 WalletItems::MaskedInstrument::MaskedInstrument(
13 const std::string& descriptive_name,
14 const WalletItems::MaskedInstrument::Type& type,
15 std::vector<std::string> supported_currencies,
16 const std::string& last_four_digits,
17 int expiration_month,
18 int expiration_year,
19 const std::string& brand,
20 Address* address,
21 const WalletItems::MaskedInstrument::Status& status,
22 const std::string& object_id)
23 : descriptive_name_(descriptive_name),
24 type_(type),
25 supported_currencies_(supported_currencies),
26 last_four_digits_(last_four_digits),
27 expiration_month_(expiration_month),
28 expiration_year_(expiration_year),
29 brand_(brand),
30 address_(address),
31 status_(status),
32 object_id_(object_id) {
33 DCHECK(address_);
34 }
35
36 WalletItems::MaskedInstrument::~MaskedInstrument() {}
37
38 WalletItems::MaskedInstrument*
39 WalletItems::MaskedInstrument::CreateMaskedInstrument(
40 const base::DictionaryValue& dictionary) {
41 std::string type_string;
42 Type type;
43 if (dictionary.GetString("type", &type_string)) {
44 type = TypeFromString(type_string);
45 } else {
46 DLOG(ERROR) << "Response from Google Wallet missing card type";
47 return NULL;
48 }
49
50 std::string last_four_digits;
51 if (!dictionary.GetString("last_four_digits", &last_four_digits)) {
52 DLOG(ERROR) << "Response from Google Wallet missing last four digits";
53 return NULL;
54 }
55
56 std::string status_string;
57 Status status;
58 if (dictionary.GetString("status", &status_string)) {
59 status = StatusFromString(status_string);
60 } else {
61 DLOG(ERROR) << "Response from Google Wallet missing status";
62 return NULL;
63 }
64
65 std::string object_id;
66 if (!dictionary.GetString("object_id", &object_id)) {
67 DLOG(ERROR) << "Response from Google Wallet missing object id";
68 return NULL;
69 }
70
71 const DictionaryValue* address_dict;
72 if (!dictionary.GetDictionary("billing_address", &address_dict)) {
73 DLOG(ERROR) << "Response from Google wallet missing address";
74 return NULL;
75 }
76 Address* address = Address::CreateDisplayAddress(*address_dict);
77
78 if (!address) {
79 DLOG(ERROR) << "Response from Google wallet contained malformed address";
80 return NULL;
81 }
82
83 std::vector<std::string> supported_currencies;
84 const ListValue* supported_currency_list;
85 if (dictionary.GetList("supported_currency", &supported_currency_list)) {
86 for (size_t i = 0; i < supported_currency_list->GetSize(); i++) {
87 std::string currency;
88 if (supported_currency_list->GetString(i, &currency))
89 supported_currencies.push_back(currency);
90 }
91 } else {
92 DVLOG(1) << "Response from Google Wallet missing supported currency";
93 }
94
95 int expiration_month;
96 if (!dictionary.GetInteger("expiration_month", &expiration_month))
97 DVLOG(1) << "Response from Google Wallet missing expiration month";
98
99 int expiration_year;
100 if (!dictionary.GetInteger("expiration_year", &expiration_year))
101 DVLOG(1) << "Response from Google Wallet missing expiration year";
102
103 std::string brand;
104 if (!dictionary.GetString("brand", &brand))
105 DVLOG(1) << "Response from Google Wallet missing brand";
106
107 std::string descriptive_name;
108 if (!dictionary.GetString("descriptive_name", &descriptive_name))
109 DVLOG(1) << "Response from Google Wallet missing descriptive name";
110
111 return new MaskedInstrument(descriptive_name,
112 type,
113 supported_currencies,
114 last_four_digits,
115 expiration_month,
116 expiration_year,
117 brand,
118 address,
119 status,
120 object_id);
121 }
122
123 bool WalletItems::MaskedInstrument::operator==(
124 const WalletItems::MaskedInstrument& other) const {
125 if (descriptive_name_ != other.descriptive_name_)
126 return false;
127 if (type_ != other.type_)
128 return false;
129 if (supported_currencies_ != other.supported_currencies_)
130 return false;
131 if (last_four_digits_ != other.last_four_digits_)
132 return false;
133 if (expiration_month_ != other.expiration_month_)
134 return false;
135 if (expiration_year_ != other.expiration_year_)
136 return false;
137 if (brand_ != other.brand_)
138 return false;
139 if (address_.get()) {
140 if (other.address_.get()) {
141 if (*address_.get() != *other.address_.get())
142 return false;
143 } else {
144 return false;
145 }
146 } else if (other.address_.get()) {
147 return false;
148 }
149 if (status_ != other.status_)
150 return false;
151 if (object_id_ != other.object_id_)
152 return false;
153 return true;
154 }
155
156 bool WalletItems::MaskedInstrument::operator!=(
157 const WalletItems::MaskedInstrument& other) const {
158 return !(*this == other);
159 }
160
161 WalletItems::MaskedInstrument::Type
162 WalletItems::MaskedInstrument::TypeFromString(
163 const std::string& type_string) {
164 if (type_string == "VISA")
165 return VISA;
166 if (type_string == "MASTER_CARD")
167 return MASTER_CARD;
168 if (type_string == "AMEX")
169 return AMEX;
170 if (type_string == "DISCOVER")
171 return DISCOVER;
172 if (type_string == "SOLO")
173 return SOLO;
174 if (type_string == "MAESTRO")
175 return MAESTRO;
176 if (type_string == "SWITCH")
177 return SWITCH;
178 return UNKNOWN;
179 }
180
181 WalletItems::MaskedInstrument::Status
182 WalletItems::MaskedInstrument::StatusFromString(
183 const std::string& status_string) {
184 if (status_string == "PENDING")
185 return PENDING;
186 if (status_string == "VALID")
187 return VALID;
188 if (status_string == "DECLINED")
189 return DECLINED;
190 if (status_string == "UNSUPPORTED_COUNTRY")
191 return UNSUPPORTED_COUNTRY;
192 if (status_string == "EXPIRED")
193 return EXPIRED;
194 if (status_string == "BILLING_INCOMPLETE")
195 return BILLING_INCOMPLETE;
196 return INAPPLICABLE;
197 }
198
199 WalletItems::LegalDocument::LegalDocument(const std::string& document_id,
200 const std::string& display_name,
201 const std::string& document_body)
202 : document_id_(document_id),
203 display_name_(display_name),
204 document_body_(document_body) {}
205
206 WalletItems::LegalDocument::~LegalDocument() {}
207
208 WalletItems::LegalDocument* WalletItems::LegalDocument::CreateLegalDocument(
209 const base::DictionaryValue& dictionary) {
210 std::string document_id;
211 if (!dictionary.GetString("legal_document_id", &document_id)) {
212 DLOG(ERROR) << "Response from Google Wallet missing legal document id";
213 return NULL;
214 }
215
216 std::string display_name;
217 if (!dictionary.GetString("display_name", &display_name)) {
218 DLOG(ERROR) << "Response from Google Wallet missing display name";
219 return NULL;
220 }
221
222 std::string document_body;
223 if (!dictionary.GetString("document", &document_body)) {
224 DLOG(ERROR) << "Response from Google Wallet missing document body";
225 return NULL;
226 }
227
228 return new WalletItems::LegalDocument(document_id,
229 display_name,
230 document_body);
231 }
232
233 bool WalletItems::LegalDocument::operator==(const LegalDocument& other) const {
234 return document_id_ == other.document_id_ &&
235 display_name_ == other.display_name_ &&
236 document_body_ == other.document_body_;
237 }
238
239 bool WalletItems::LegalDocument::operator!=(const LegalDocument& other) const {
240 return !(*this == other);
241 }
242
243 WalletItems::WalletItems(const std::vector<std::string>& required_actions,
244 const std::string& google_transaction_id,
245 const std::string& default_instrument_id,
246 const std::string& default_address_id)
247 : required_actions_(required_actions),
248 google_transaction_id_(google_transaction_id),
249 default_instrument_id_(default_instrument_id),
250 default_address_id_(default_address_id) {}
251
252 WalletItems::~WalletItems() {}
253
254 WalletItems* WalletItems::CreateWalletItems(
255 const base::DictionaryValue& dictionary) {
256 std::string google_transaction_id;
257 if (!dictionary.GetString("google_transaction_id", &google_transaction_id)) {
258 DLOG(ERROR) << "Response from Google wallet missing google transaction id";
259 return NULL;
260 }
261
262 std::vector<std::string> required_action;
263 const ListValue* required_action_list;
264 if (dictionary.GetList("required_action", &required_action_list)) {
265 for (size_t i = 0; i < required_action_list->GetSize(); i++) {
266 std::string action;
267 if (required_action_list->GetString(i, &action))
268 required_action.push_back(action);
269 }
270 } else {
271 DVLOG(1) << "Response from Google wallet missing required actions";
272 }
273
274 std::string default_instrument_id;
275 if (!dictionary.GetString("default_instrument_id", &default_instrument_id))
276 DVLOG(1) << "Response from Google wallet missing default instrument id";
277
278 std::string default_address_id;
279 if (!dictionary.GetString("default_address_id", &default_address_id))
280 DVLOG(1) << "Response from Google wallet missing default_address_id";
281
282 scoped_ptr<WalletItems> wallet_items(new WalletItems(required_action,
283 google_transaction_id,
284 default_instrument_id,
285 default_address_id));
286
287 const ListValue* legal_docs;
288 if (dictionary.GetList("required_legal_document", &legal_docs)) {
289 for (size_t i = 0; i < legal_docs->GetSize(); i++) {
290 const DictionaryValue* legal_doc_dict;
291 if (legal_docs->GetDictionary(i, &legal_doc_dict)) {
292 scoped_ptr<LegalDocument> legal_doc(
293 LegalDocument::CreateLegalDocument(*legal_doc_dict));
294 if (legal_doc.get()) {
295 wallet_items->AddLegalDocument(legal_doc.release());
296 } else {
297 DLOG(ERROR) << "Malformed legal document in response from "
298 "Google wallet";
299 return NULL;
300 }
301 }
302 }
303 } else {
304 DVLOG(1) << "Response from Google wallet missing legal docs";
305 }
306
307 const ListValue* instruments;
308 if (dictionary.GetList("instrument", &instruments)) {
309 for (size_t i = 0; i < instruments->GetSize(); i++) {
310 const DictionaryValue* instrument_dict;
311 if (instruments->GetDictionary(i, &instrument_dict)) {
312 scoped_ptr<MaskedInstrument> instrument(
313 MaskedInstrument::CreateMaskedInstrument(*instrument_dict));
314 if (instrument.get())
315 wallet_items->AddInstrument(instrument.release());
316 else
317 DLOG(ERROR) << "Malformed instrument in response from Google Wallet";
318 }
319 }
320 } else {
321 DVLOG(1) << "Response from Google wallet missing instruments";
322 }
323
324 const ListValue* addresses;
325 if (dictionary.GetList("address", &addresses)) {
326 for (size_t i = 0; i < addresses->GetSize(); i++) {
327 const DictionaryValue* address_dict;
328 if (addresses->GetDictionary(i, &address_dict)) {
329 scoped_ptr<Address> address(
330 Address::CreateAddressWithID(*address_dict));
331 if (address.get())
332 wallet_items->AddAddress(address.release());
333 else
334 DLOG(ERROR) << "Malformed address in response from Google Wallet";
335 }
336 }
337 } else {
338 DVLOG(1) << "Response from Google wallet missing addresses";
339 }
340
341 return wallet_items.release();
342 }
343
344 bool WalletItems::operator==(const WalletItems& other) const {
345 // TODO(ahutter): Check scoped vector equality.
346 return google_transaction_id_ == other.google_transaction_id_ &&
347 default_instrument_id_ == other.default_instrument_id_ &&
348 default_address_id_ == other.default_address_id_ &&
349 required_actions_ == required_actions_;
350 }
Evan Stade 2012/12/07 19:57:44 \n
ahutter 2012/12/15 01:06:31 Done.
351 bool WalletItems::operator!=(const WalletItems& other) const {
352 return !(*this == other);
353 }
354
355 } // end wallet namespace
356
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698