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