Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 descriptive_name; | |
| 42 if (!dictionary.GetString("descriptive_name", &descriptive_name)) | |
| 43 DVLOG(1) << "Response from Google Wallet missing descriptive name"; | |
| 44 | |
| 45 std::string type_string; | |
| 46 Type type; | |
| 47 if (dictionary.GetString("type", &type_string)) { | |
| 48 type = TypeFromString(type_string); | |
| 49 } else { | |
| 50 DLOG(ERROR) << "Response from Google Wallet missing card type"; | |
| 51 return NULL; | |
|
Dan Beam
2012/12/06 21:59:24
nit: I'd personally group all critical failures at
Dan Beam
2012/12/06 22:00:14
whooops! I meant group all critical failures at th
ahutter
2012/12/06 22:44:19
Done.
| |
| 52 } | |
| 53 | |
| 54 std::vector<std::string> supported_currencies; | |
| 55 const ListValue* supported_currency_list; | |
| 56 if (dictionary.GetList("supported_currency", &supported_currency_list)) { | |
| 57 for (size_t i = 0; i < supported_currency_list->GetSize(); i++) { | |
| 58 std::string currency; | |
| 59 if (supported_currency_list->GetString(i, ¤cy)) | |
| 60 supported_currencies.push_back(currency); | |
| 61 } | |
| 62 } else { | |
| 63 DVLOG(1) << "Response from Google Wallet missing supported currency"; | |
| 64 } | |
| 65 | |
| 66 std::string last_four_digits; | |
| 67 if (!dictionary.GetString("last_four_digits", &last_four_digits)) { | |
| 68 DLOG(ERROR) << "Response from Google Wallet missing last four digits"; | |
| 69 return NULL; | |
| 70 } | |
| 71 | |
| 72 int expiration_month; | |
| 73 if (!dictionary.GetInteger("expiration_month", &expiration_month)) | |
| 74 DVLOG(1) << "Response from Google Wallet missing expiration month"; | |
| 75 | |
| 76 int expiration_year; | |
| 77 if (!dictionary.GetInteger("expiration_year", &expiration_year)) | |
| 78 DVLOG(1) << "Response from Google Wallet missing expiration year"; | |
| 79 | |
| 80 std::string brand; | |
| 81 if (!dictionary.GetString("brand", &brand)) | |
| 82 DVLOG(1) << "Response from Google Wallet missing brand"; | |
| 83 | |
| 84 std::string status_string; | |
| 85 Status status; | |
| 86 if (dictionary.GetString("status", &status_string)) { | |
| 87 status = StatusFromString(status_string); | |
| 88 } else { | |
| 89 DLOG(ERROR) << "Response from Google Wallet missing status"; | |
| 90 return NULL; | |
| 91 } | |
| 92 | |
| 93 std::string object_id; | |
| 94 if (!dictionary.GetString("object_id", &object_id)) { | |
| 95 DLOG(ERROR) << "Response from Google Wallet missing object id"; | |
| 96 return NULL; | |
| 97 } | |
| 98 | |
| 99 const DictionaryValue* address_dict; | |
| 100 if (!dictionary.GetDictionary("billing_address", &address_dict)) { | |
| 101 DLOG(ERROR) << "Response from Google wallet missing address"; | |
| 102 return NULL; | |
| 103 } | |
| 104 Address* address = Address::CreateDisplayAddress(*address_dict); | |
| 105 | |
| 106 if (!address) { | |
| 107 DLOG(ERROR) << "Response from Google wallet contained malformed address"; | |
| 108 return NULL; | |
| 109 } | |
| 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::vector<std::string> required_action; | |
| 257 const ListValue* required_action_list; | |
| 258 if (dictionary.GetList("required_action", &required_action_list)) { | |
| 259 for (size_t i = 0; i < required_action_list->GetSize(); i++) { | |
| 260 std::string action; | |
| 261 if (required_action_list->GetString(i, &action)) | |
| 262 required_action.push_back(action); | |
| 263 } | |
| 264 } else { | |
| 265 DVLOG(1) << "Response from Google wallet missing required actions"; | |
| 266 } | |
| 267 | |
| 268 std::string google_transaction_id; | |
| 269 if (!dictionary.GetString("google_transaction_id", &google_transaction_id)) { | |
| 270 DLOG(ERROR) << "Response from Google wallet missing google transaction id"; | |
| 271 return NULL; | |
| 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* instruments; | |
| 288 if (dictionary.GetList("instrument", &instruments)) { | |
| 289 for (size_t i = 0; i < instruments->GetSize(); i++) { | |
| 290 const DictionaryValue* instrument_dict; | |
| 291 if (instruments->GetDictionary(i, &instrument_dict)) { | |
| 292 scoped_ptr<MaskedInstrument> instrument( | |
| 293 MaskedInstrument::CreateMaskedInstrument(*instrument_dict)); | |
| 294 if (instrument.get()) | |
| 295 wallet_items->AddInstrument(instrument.release()); | |
| 296 else | |
| 297 DLOG(ERROR) << "Malformed instrument in response from Google Wallet"; | |
| 298 } | |
| 299 } | |
| 300 } else { | |
| 301 DVLOG(1) << "Response from Google wallet missing instruments"; | |
| 302 } | |
| 303 | |
| 304 const ListValue* addresses; | |
| 305 if (dictionary.GetList("address", &addresses)) { | |
| 306 for (size_t i = 0; i < addresses->GetSize(); i++) { | |
| 307 const DictionaryValue* address_dict; | |
| 308 if (addresses->GetDictionary(i, &address_dict)) { | |
| 309 scoped_ptr<Address> address( | |
| 310 Address::CreateAddressWithID(*address_dict)); | |
| 311 if (address.get()) | |
| 312 wallet_items->AddAddress(address.release()); | |
| 313 else | |
| 314 DLOG(ERROR) << "Malformed address in response from Google Wallet"; | |
| 315 } | |
| 316 } | |
| 317 } else { | |
| 318 DVLOG(1) << "Response from Google wallet missing addresses"; | |
| 319 } | |
| 320 | |
| 321 const ListValue* legal_docs; | |
| 322 if (dictionary.GetList("required_legal_document", &legal_docs)) { | |
| 323 for (size_t i = 0; i < legal_docs->GetSize(); i++) { | |
| 324 const DictionaryValue* legal_doc_dict; | |
| 325 if (legal_docs->GetDictionary(i, &legal_doc_dict)) { | |
| 326 scoped_ptr<LegalDocument> legal_doc( | |
| 327 LegalDocument::CreateLegalDocument(*legal_doc_dict)); | |
| 328 if (legal_doc.get()) { | |
| 329 wallet_items->AddLegalDocument(legal_doc.release()); | |
| 330 } else { | |
| 331 DLOG(ERROR) << "Malformed legal document in response from " | |
| 332 "Google wallet"; | |
| 333 return NULL; | |
| 334 } | |
| 335 } | |
| 336 } | |
| 337 } else { | |
| 338 DVLOG(1) << "Response from Google wallet missing legal docs"; | |
| 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 } | |
| 351 bool WalletItems::operator!=(const WalletItems& other) const { | |
| 352 return !(*this == other); | |
| 353 } | |
| 354 | |
| 355 } // end wallet namespace | |
| OLD | NEW |