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