| 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 #include "googleurl/src/gurl.h" | |
| 10 #include "grit/webkit_resources.h" | |
| 11 #include "ui/base/resource/resource_bundle.h" | |
| 12 #include "ui/gfx/image/image.h" | |
| 13 | |
| 14 namespace autofill { | |
| 15 namespace wallet { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const char kLegalDocumentUrl[] = | |
| 20 "https://wallet.google.com/customer/gadget/legaldocument.html?docId="; | |
| 21 | |
| 22 WalletItems::MaskedInstrument::Type | |
| 23 TypeFromString(const std::string& type_string) { | |
| 24 if (type_string == "VISA") | |
| 25 return WalletItems::MaskedInstrument::VISA; | |
| 26 if (type_string == "MASTER_CARD") | |
| 27 return WalletItems::MaskedInstrument::MASTER_CARD; | |
| 28 if (type_string == "AMEX") | |
| 29 return WalletItems::MaskedInstrument::AMEX; | |
| 30 if (type_string == "DISCOVER") | |
| 31 return WalletItems::MaskedInstrument::DISCOVER; | |
| 32 if (type_string == "SOLO") | |
| 33 return WalletItems::MaskedInstrument::SOLO; | |
| 34 if (type_string == "MAESTRO") | |
| 35 return WalletItems::MaskedInstrument::MAESTRO; | |
| 36 if (type_string == "SWITCH") | |
| 37 return WalletItems::MaskedInstrument::SWITCH; | |
| 38 return WalletItems::MaskedInstrument::UNKNOWN; | |
| 39 } | |
| 40 | |
| 41 WalletItems::MaskedInstrument::Status | |
| 42 StatusFromString(const std::string& status_string) { | |
| 43 if (status_string == "PENDING") | |
| 44 return WalletItems::MaskedInstrument::PENDING; | |
| 45 if (status_string == "VALID") | |
| 46 return WalletItems::MaskedInstrument::VALID; | |
| 47 if (status_string == "DECLINED") | |
| 48 return WalletItems::MaskedInstrument::DECLINED; | |
| 49 if (status_string == "UNSUPPORTED_COUNTRY") | |
| 50 return WalletItems::MaskedInstrument::UNSUPPORTED_COUNTRY; | |
| 51 if (status_string == "EXPIRED") | |
| 52 return WalletItems::MaskedInstrument::EXPIRED; | |
| 53 if (status_string == "BILLING_INCOMPLETE") | |
| 54 return WalletItems::MaskedInstrument::BILLING_INCOMPLETE; | |
| 55 return WalletItems::MaskedInstrument::INAPPLICABLE; | |
| 56 } | |
| 57 | |
| 58 } // anonymous namespace | |
| 59 | |
| 60 WalletItems::MaskedInstrument::MaskedInstrument( | |
| 61 const string16& descriptive_name, | |
| 62 const WalletItems::MaskedInstrument::Type& type, | |
| 63 const std::vector<string16>& supported_currencies, | |
| 64 const string16& last_four_digits, | |
| 65 int expiration_month, | |
| 66 int expiration_year, | |
| 67 scoped_ptr<Address> address, | |
| 68 const WalletItems::MaskedInstrument::Status& status, | |
| 69 const std::string& object_id) | |
| 70 : descriptive_name_(descriptive_name), | |
| 71 type_(type), | |
| 72 supported_currencies_(supported_currencies), | |
| 73 last_four_digits_(last_four_digits), | |
| 74 expiration_month_(expiration_month), | |
| 75 expiration_year_(expiration_year), | |
| 76 address_(address.Pass()), | |
| 77 status_(status), | |
| 78 object_id_(object_id) { | |
| 79 DCHECK(address_.get()); | |
| 80 } | |
| 81 | |
| 82 WalletItems::MaskedInstrument::~MaskedInstrument() {} | |
| 83 | |
| 84 scoped_ptr<WalletItems::MaskedInstrument> | |
| 85 WalletItems::MaskedInstrument::CreateMaskedInstrument( | |
| 86 const base::DictionaryValue& dictionary) { | |
| 87 std::string type_string; | |
| 88 Type type; | |
| 89 if (dictionary.GetString("type", &type_string)) { | |
| 90 type = TypeFromString(type_string); | |
| 91 } else { | |
| 92 DLOG(ERROR) << "Response from Google Wallet missing card type"; | |
| 93 return scoped_ptr<MaskedInstrument>(); | |
| 94 } | |
| 95 | |
| 96 string16 last_four_digits; | |
| 97 if (!dictionary.GetString("last_four_digits", &last_four_digits)) { | |
| 98 DLOG(ERROR) << "Response from Google Wallet missing last four digits"; | |
| 99 return scoped_ptr<MaskedInstrument>(); | |
| 100 } | |
| 101 | |
| 102 std::string status_string; | |
| 103 Status status; | |
| 104 if (dictionary.GetString("status", &status_string)) { | |
| 105 status = StatusFromString(status_string); | |
| 106 } else { | |
| 107 DLOG(ERROR) << "Response from Google Wallet missing status"; | |
| 108 return scoped_ptr<MaskedInstrument>(); | |
| 109 } | |
| 110 | |
| 111 std::string object_id; | |
| 112 if (!dictionary.GetString("object_id", &object_id)) { | |
| 113 DLOG(ERROR) << "Response from Google Wallet missing object id"; | |
| 114 return scoped_ptr<MaskedInstrument>(); | |
| 115 } | |
| 116 | |
| 117 const DictionaryValue* address_dict; | |
| 118 if (!dictionary.GetDictionary("billing_address", &address_dict)) { | |
| 119 DLOG(ERROR) << "Response from Google wallet missing address"; | |
| 120 return scoped_ptr<MaskedInstrument>(); | |
| 121 } | |
| 122 scoped_ptr<Address> address = Address::CreateDisplayAddress(*address_dict); | |
| 123 | |
| 124 if (!address.get()) { | |
| 125 DLOG(ERROR) << "Response from Google wallet contained malformed address"; | |
| 126 return scoped_ptr<MaskedInstrument>(); | |
| 127 } | |
| 128 | |
| 129 std::vector<string16> supported_currencies; | |
| 130 const ListValue* supported_currency_list; | |
| 131 if (dictionary.GetList("supported_currency", &supported_currency_list)) { | |
| 132 for (size_t i = 0; i < supported_currency_list->GetSize(); ++i) { | |
| 133 string16 currency; | |
| 134 if (supported_currency_list->GetString(i, ¤cy)) | |
| 135 supported_currencies.push_back(currency); | |
| 136 } | |
| 137 } else { | |
| 138 DVLOG(1) << "Response from Google Wallet missing supported currency"; | |
| 139 } | |
| 140 | |
| 141 int expiration_month; | |
| 142 if (!dictionary.GetInteger("expiration_month", &expiration_month)) | |
| 143 DVLOG(1) << "Response from Google Wallet missing expiration month"; | |
| 144 | |
| 145 int expiration_year; | |
| 146 if (!dictionary.GetInteger("expiration_year", &expiration_year)) | |
| 147 DVLOG(1) << "Response from Google Wallet missing expiration year"; | |
| 148 | |
| 149 string16 descriptive_name; | |
| 150 if (!dictionary.GetString("descriptive_name", &descriptive_name)) | |
| 151 DVLOG(1) << "Response from Google Wallet missing descriptive name"; | |
| 152 | |
| 153 return scoped_ptr<MaskedInstrument>(new MaskedInstrument(descriptive_name, | |
| 154 type, | |
| 155 supported_currencies, | |
| 156 last_four_digits, | |
| 157 expiration_month, | |
| 158 expiration_year, | |
| 159 address.Pass(), | |
| 160 status, | |
| 161 object_id)); | |
| 162 } | |
| 163 | |
| 164 bool WalletItems::MaskedInstrument::operator==( | |
| 165 const WalletItems::MaskedInstrument& other) const { | |
| 166 if (descriptive_name_ != other.descriptive_name_) | |
| 167 return false; | |
| 168 if (type_ != other.type_) | |
| 169 return false; | |
| 170 if (supported_currencies_ != other.supported_currencies_) | |
| 171 return false; | |
| 172 if (last_four_digits_ != other.last_four_digits_) | |
| 173 return false; | |
| 174 if (expiration_month_ != other.expiration_month_) | |
| 175 return false; | |
| 176 if (expiration_year_ != other.expiration_year_) | |
| 177 return false; | |
| 178 if (address_.get()) { | |
| 179 if (other.address_.get()) { | |
| 180 if (*address_.get() != *other.address_.get()) | |
| 181 return false; | |
| 182 } else { | |
| 183 return false; | |
| 184 } | |
| 185 } else if (other.address_.get()) { | |
| 186 return false; | |
| 187 } | |
| 188 if (status_ != other.status_) | |
| 189 return false; | |
| 190 if (object_id_ != other.object_id_) | |
| 191 return false; | |
| 192 return true; | |
| 193 } | |
| 194 | |
| 195 bool WalletItems::MaskedInstrument::operator!=( | |
| 196 const WalletItems::MaskedInstrument& other) const { | |
| 197 return !(*this == other); | |
| 198 } | |
| 199 | |
| 200 bool WalletItems::HasRequiredAction(RequiredAction action) const { | |
| 201 DCHECK(ActionAppliesToWalletItems(action)); | |
| 202 return std::find(required_actions_.begin(), | |
| 203 required_actions_.end(), | |
| 204 action) != required_actions_.end(); | |
| 205 } | |
| 206 | |
| 207 const gfx::Image& WalletItems::MaskedInstrument::CardIcon() const { | |
| 208 int idr = 0; | |
| 209 switch (type_) { | |
| 210 case AMEX: | |
| 211 idr = IDR_AUTOFILL_CC_AMEX; | |
| 212 break; | |
| 213 | |
| 214 case DISCOVER: | |
| 215 idr = IDR_AUTOFILL_CC_DISCOVER; | |
| 216 break; | |
| 217 | |
| 218 case MASTER_CARD: | |
| 219 idr = IDR_AUTOFILL_CC_MASTERCARD; | |
| 220 break; | |
| 221 | |
| 222 case SOLO: | |
| 223 idr = IDR_AUTOFILL_CC_SOLO; | |
| 224 break; | |
| 225 | |
| 226 case VISA: | |
| 227 idr = IDR_AUTOFILL_CC_VISA; | |
| 228 break; | |
| 229 | |
| 230 case MAESTRO: | |
| 231 case SWITCH: | |
| 232 case UNKNOWN: | |
| 233 idr = IDR_AUTOFILL_CC_GENERIC; | |
| 234 break; | |
| 235 } | |
| 236 | |
| 237 return ResourceBundle::GetSharedInstance().GetImageNamed(idr); | |
| 238 } | |
| 239 | |
| 240 WalletItems::LegalDocument::LegalDocument(const std::string& document_id, | |
| 241 const std::string& display_name) | |
| 242 : document_id_(document_id), | |
| 243 display_name_(display_name) {} | |
| 244 | |
| 245 WalletItems::LegalDocument::~LegalDocument() {} | |
| 246 | |
| 247 scoped_ptr<WalletItems::LegalDocument> | |
| 248 WalletItems::LegalDocument::CreateLegalDocument( | |
| 249 const base::DictionaryValue& dictionary) { | |
| 250 std::string document_id; | |
| 251 if (!dictionary.GetString("legal_document_id", &document_id)) { | |
| 252 DLOG(ERROR) << "Response from Google Wallet missing legal document id"; | |
| 253 return scoped_ptr<LegalDocument>(); | |
| 254 } | |
| 255 | |
| 256 std::string display_name; | |
| 257 if (!dictionary.GetString("display_name", &display_name)) { | |
| 258 DLOG(ERROR) << "Response from Google Wallet missing display name"; | |
| 259 return scoped_ptr<LegalDocument>(); | |
| 260 } | |
| 261 | |
| 262 return scoped_ptr<LegalDocument>(new LegalDocument(document_id, | |
| 263 display_name)); | |
| 264 } | |
| 265 | |
| 266 GURL WalletItems::LegalDocument::GetUrl() { | |
| 267 return GURL(kLegalDocumentUrl + document_id_); | |
| 268 } | |
| 269 | |
| 270 bool WalletItems::LegalDocument::operator==(const LegalDocument& other) const { | |
| 271 return document_id_ == other.document_id_ && | |
| 272 display_name_ == other.display_name_; | |
| 273 } | |
| 274 | |
| 275 bool WalletItems::LegalDocument::operator!=(const LegalDocument& other) const { | |
| 276 return !(*this == other); | |
| 277 } | |
| 278 | |
| 279 WalletItems::WalletItems(const std::vector<RequiredAction>& required_actions, | |
| 280 const std::string& google_transaction_id, | |
| 281 const std::string& default_instrument_id, | |
| 282 const std::string& default_address_id, | |
| 283 const std::string& obfuscated_gaia_id) | |
| 284 : required_actions_(required_actions), | |
| 285 google_transaction_id_(google_transaction_id), | |
| 286 default_instrument_id_(default_instrument_id), | |
| 287 default_address_id_(default_address_id), | |
| 288 obfuscated_gaia_id_(obfuscated_gaia_id) {} | |
| 289 | |
| 290 WalletItems::~WalletItems() {} | |
| 291 | |
| 292 scoped_ptr<WalletItems> | |
| 293 WalletItems::CreateWalletItems(const base::DictionaryValue& dictionary) { | |
| 294 std::vector<RequiredAction> required_action; | |
| 295 const ListValue* required_action_list; | |
| 296 if (dictionary.GetList("required_action", &required_action_list)) { | |
| 297 for (size_t i = 0; i < required_action_list->GetSize(); ++i) { | |
| 298 std::string action_string; | |
| 299 if (required_action_list->GetString(i, &action_string)) { | |
| 300 RequiredAction action = ParseRequiredActionFromString(action_string); | |
| 301 if (!ActionAppliesToWalletItems(action)) { | |
| 302 DLOG(ERROR) << "Response from Google wallet with bad required action:" | |
| 303 " \"" << action_string << "\""; | |
| 304 return scoped_ptr<WalletItems>(); | |
| 305 } | |
| 306 required_action.push_back(action); | |
| 307 } | |
| 308 } | |
| 309 } else { | |
| 310 DVLOG(1) << "Response from Google wallet missing required actions"; | |
| 311 } | |
| 312 | |
| 313 std::string google_transaction_id; | |
| 314 if (!dictionary.GetString("google_transaction_id", &google_transaction_id) && | |
| 315 required_action.empty()) { | |
| 316 DLOG(ERROR) << "Response from Google wallet missing google transaction id"; | |
| 317 return scoped_ptr<WalletItems>(); | |
| 318 } | |
| 319 | |
| 320 std::string default_instrument_id; | |
| 321 if (!dictionary.GetString("default_instrument_id", &default_instrument_id)) | |
| 322 DVLOG(1) << "Response from Google wallet missing default instrument id"; | |
| 323 | |
| 324 std::string default_address_id; | |
| 325 if (!dictionary.GetString("default_address_id", &default_address_id)) | |
| 326 DVLOG(1) << "Response from Google wallet missing default_address_id"; | |
| 327 | |
| 328 std::string obfuscated_gaia_id; | |
| 329 if (!dictionary.GetString("obfuscated_gaia_id", &obfuscated_gaia_id)) | |
| 330 DVLOG(1) << "Response from Google wallet missing obfuscated gaia id"; | |
| 331 | |
| 332 scoped_ptr<WalletItems> wallet_items(new WalletItems(required_action, | |
| 333 google_transaction_id, | |
| 334 default_instrument_id, | |
| 335 default_address_id, | |
| 336 obfuscated_gaia_id)); | |
| 337 | |
| 338 const ListValue* legal_docs; | |
| 339 if (dictionary.GetList("required_legal_document", &legal_docs)) { | |
| 340 for (size_t i = 0; i < legal_docs->GetSize(); ++i) { | |
| 341 const DictionaryValue* legal_doc_dict; | |
| 342 if (legal_docs->GetDictionary(i, &legal_doc_dict)) { | |
| 343 scoped_ptr<LegalDocument> legal_doc( | |
| 344 LegalDocument::CreateLegalDocument(*legal_doc_dict)); | |
| 345 if (legal_doc.get()) { | |
| 346 wallet_items->AddLegalDocument(legal_doc.Pass()); | |
| 347 } else { | |
| 348 DLOG(ERROR) << "Malformed legal document in response from " | |
| 349 "Google wallet"; | |
| 350 return scoped_ptr<WalletItems>(); | |
| 351 } | |
| 352 } | |
| 353 } | |
| 354 } else { | |
| 355 DVLOG(1) << "Response from Google wallet missing legal docs"; | |
| 356 } | |
| 357 | |
| 358 const ListValue* instruments; | |
| 359 if (dictionary.GetList("instrument", &instruments)) { | |
| 360 for (size_t i = 0; i < instruments->GetSize(); ++i) { | |
| 361 const DictionaryValue* instrument_dict; | |
| 362 if (instruments->GetDictionary(i, &instrument_dict)) { | |
| 363 scoped_ptr<MaskedInstrument> instrument( | |
| 364 MaskedInstrument::CreateMaskedInstrument(*instrument_dict)); | |
| 365 if (instrument.get()) | |
| 366 wallet_items->AddInstrument(instrument.Pass()); | |
| 367 else | |
| 368 DLOG(ERROR) << "Malformed instrument in response from Google Wallet"; | |
| 369 } | |
| 370 } | |
| 371 } else { | |
| 372 DVLOG(1) << "Response from Google wallet missing instruments"; | |
| 373 } | |
| 374 | |
| 375 const ListValue* addresses; | |
| 376 if (dictionary.GetList("address", &addresses)) { | |
| 377 for (size_t i = 0; i < addresses->GetSize(); ++i) { | |
| 378 const DictionaryValue* address_dict; | |
| 379 if (addresses->GetDictionary(i, &address_dict)) { | |
| 380 scoped_ptr<Address> address( | |
| 381 Address::CreateAddressWithID(*address_dict)); | |
| 382 if (address.get()) | |
| 383 wallet_items->AddAddress(address.Pass()); | |
| 384 else | |
| 385 DLOG(ERROR) << "Malformed address in response from Google Wallet"; | |
| 386 } | |
| 387 } | |
| 388 } else { | |
| 389 DVLOG(1) << "Response from Google wallet missing addresses"; | |
| 390 } | |
| 391 | |
| 392 return wallet_items.Pass(); | |
| 393 } | |
| 394 | |
| 395 bool WalletItems::operator==(const WalletItems& other) const { | |
| 396 // TODO(ahutter): Check scoped vector equality. | |
| 397 return google_transaction_id_ == other.google_transaction_id_ && | |
| 398 default_instrument_id_ == other.default_instrument_id_ && | |
| 399 default_address_id_ == other.default_address_id_ && | |
| 400 required_actions_ == other.required_actions_ && | |
| 401 obfuscated_gaia_id_ == other.obfuscated_gaia_id_; | |
| 402 } | |
| 403 | |
| 404 bool WalletItems::operator!=(const WalletItems& other) const { | |
| 405 return !(*this == other); | |
| 406 } | |
| 407 | |
| 408 } // namespace wallet | |
| 409 } // namespace autofill | |
| OLD | NEW |