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