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