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