| Index: chrome/browser/autofill/wallet/wallet_items.cc
|
| diff --git a/chrome/browser/autofill/wallet/wallet_items.cc b/chrome/browser/autofill/wallet/wallet_items.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..73b2b9d8f9f6954e9bef73a1b1faeb1d5b3b2c18
|
| --- /dev/null
|
| +++ b/chrome/browser/autofill/wallet/wallet_items.cc
|
| @@ -0,0 +1,353 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/autofill/wallet/wallet_items.h"
|
| +
|
| +#include "base/logging.h"
|
| +#include "base/values.h"
|
| +
|
| +namespace wallet {
|
| +
|
| +WalletItems::MaskedInstrument::MaskedInstrument(
|
| + const std::string& descriptive_name,
|
| + WalletItems::MaskedInstrument::Type type,
|
| + std::vector<std::string> supported_currencies,
|
| + const std::string& last_four_digits,
|
| + int expiration_month,
|
| + int expiration_year,
|
| + const std::string& brand,
|
| + Address* address,
|
| + WalletItems::MaskedInstrument::Status status,
|
| + const std::string& object_id)
|
| + : descriptive_name_(descriptive_name),
|
| + type_(type),
|
| + supported_currencies_(supported_currencies),
|
| + last_four_digits_(last_four_digits),
|
| + expiration_month_(expiration_month),
|
| + expiration_year_(expiration_year),
|
| + brand_(brand),
|
| + address_(address),
|
| + status_(status),
|
| + object_id_(object_id) {}
|
| +
|
| +WalletItems::MaskedInstrument::~MaskedInstrument() {}
|
| +
|
| +WalletItems::MaskedInstrument* WalletItems::MaskedInstrument::FromDictionary(
|
| + base::DictionaryValue* dictionary) {
|
| + DCHECK(dictionary);
|
| + std::string descriptive_name;
|
| + if (!dictionary->GetString("descriptive_name", &descriptive_name))
|
| + VLOG(1) << "Response from Google Wallet missing descriptive name";
|
| + std::string type_string;
|
| + Type type;
|
| + if (dictionary->GetString("type", &type_string)) {
|
| + type = TypeFromString(type_string);
|
| + } else {
|
| + LOG(ERROR) << "Response from Google Wallet missing card type";
|
| + return NULL;
|
| + }
|
| + std::vector<std::string> supported_currencies;
|
| + ListValue* supported_currency_list;
|
| + if (dictionary->GetList("supported_currency", &supported_currency_list)) {
|
| + for (size_t i = 0; i < supported_currency_list->GetSize(); i++) {
|
| + std::string currency;
|
| + if (supported_currency_list->GetString(i, ¤cy))
|
| + supported_currencies.push_back(currency);
|
| + }
|
| + } else {
|
| + VLOG(1) << "Response from Google Wallet missing supported currency";
|
| + }
|
| + std::string last_four_digits;
|
| + if (!dictionary->GetString("last_four_digits", &last_four_digits)) {
|
| + LOG(ERROR) << "Response from Google Wallet missing last four digits";
|
| + return NULL;
|
| + }
|
| + int expiration_month;
|
| + if (!dictionary->GetInteger("expiration_month", &expiration_month))
|
| + VLOG(1) << "Response from Google Wallet missing expiration month";
|
| + int expiration_year;
|
| + if (!dictionary->GetInteger("expiration_year", &expiration_year))
|
| + VLOG(1) << "Response from Google Wallet missing expiration year";
|
| + std::string brand;
|
| + if (!dictionary->GetString("brand", &brand))
|
| + VLOG(1) << "Response from Google Wallet missing brand";
|
| + std::string status_string;
|
| + Status status;
|
| + if (dictionary->GetString("status", &status_string)) {
|
| + status = StatusFromString(status_string);
|
| + } else {
|
| + LOG(ERROR) << "Response from Google Wallet missing status";
|
| + return NULL;
|
| + }
|
| + std::string object_id;
|
| + if (!dictionary->GetString("object_id", &object_id)) {
|
| + LOG(ERROR) << "Response from Google Wallet missing object id";
|
| + return NULL;
|
| + }
|
| + DictionaryValue* address_dict;
|
| + if (!dictionary->GetDictionary("billing_address", &address_dict)) {
|
| + LOG(ERROR) << "Response from Google wallet missing address";
|
| + return NULL;
|
| + }
|
| + scoped_ptr<Address> address = Address::FromClientDictionary(address_dict);
|
| + if (!address.get()) {
|
| + LOG(ERROR) << "Response from Google wallet contained malformed address";
|
| + return NULL;
|
| + }
|
| +
|
| + return new MaskedInstrument(descriptive_name,
|
| + type,
|
| + supported_currencies,
|
| + last_four_digits,
|
| + expiration_month,
|
| + expiration_year,
|
| + brand,
|
| + address.release(),
|
| + status,
|
| + object_id);
|
| +}
|
| +
|
| +bool WalletItems::MaskedInstrument::operator==(
|
| + const WalletItems::MaskedInstrument& other) const {
|
| + if (descriptive_name_.compare(other.descriptive_name_) != 0)
|
| + return false;
|
| + if (type_ != other.type_)
|
| + return false;
|
| + if (supported_currencies_ != other.supported_currencies_)
|
| + return false;
|
| + if (last_four_digits_.compare(other.last_four_digits_) != 0)
|
| + return false;
|
| + if (expiration_month_ != other.expiration_month_)
|
| + return false;
|
| + if (expiration_year_ != other.expiration_year_)
|
| + return false;
|
| + if (brand_.compare(other.brand_) != 0)
|
| + return false;
|
| + if (address_.get()) {
|
| + if (other.address_.get()) {
|
| + if (*address_.get() != *other.address_.get())
|
| + return false;
|
| + } else {
|
| + return false;
|
| + }
|
| + } else if (other.address_.get()) {
|
| + return false;
|
| + }
|
| + if (status_ != other.status_)
|
| + return false;
|
| + if (object_id_.compare(other.object_id_) != 0)
|
| + return false;
|
| + return true;
|
| +}
|
| +
|
| +bool WalletItems::MaskedInstrument::operator!=(
|
| + const WalletItems::MaskedInstrument& other) const {
|
| + return !(*this == other);
|
| +}
|
| +
|
| +WalletItems::MaskedInstrument::Type
|
| +WalletItems::MaskedInstrument::TypeFromString(
|
| + const std::string& type_string) {
|
| + if (type_string.compare("VISA") == 0)
|
| + return VISA;
|
| + if (type_string.compare("MASTER_CARD") == 0)
|
| + return MASTER_CARD;
|
| + if (type_string.compare("AMEX") == 0)
|
| + return AMEX;
|
| + if (type_string.compare("DISCOVER") == 0)
|
| + return DISCOVER;
|
| + if (type_string.compare("SOLO") == 0)
|
| + return SOLO;
|
| + if (type_string.compare("MAESTRO") == 0)
|
| + return MAESTRO;
|
| + if (type_string.compare("SWITCH") == 0)
|
| + return SWITCH;
|
| + return UNKNOWN;
|
| +}
|
| +
|
| +WalletItems::MaskedInstrument::Status
|
| +WalletItems::MaskedInstrument::StatusFromString(
|
| + const std::string& status_string) {
|
| + if (status_string.compare("PENDING") == 0)
|
| + return PENDING;
|
| + if (status_string.compare("VALID") == 0)
|
| + return VALID;
|
| + if (status_string.compare("DECLINED") == 0)
|
| + return DECLINED;
|
| + if (status_string.compare("UNSUPPORTED_COUNTRY") == 0)
|
| + return UNSUPPORTED_COUNTRY;
|
| + if (status_string.compare("EXPIRED") == 0)
|
| + return EXPIRED;
|
| + if (status_string.compare("BILLING_INCOMPLETE") == 0)
|
| + return BILLING_INCOMPLETE;
|
| + return INAPPLICABLE;
|
| +}
|
| +
|
| +WalletItems::LegalDocument::LegalDocument(const std::string& document_id,
|
| + const std::string& display_name,
|
| + const std::string& document_body)
|
| + : document_id_(document_id),
|
| + display_name_(display_name),
|
| + document_body_(document_body) {}
|
| +
|
| +WalletItems::LegalDocument::~LegalDocument() {}
|
| +
|
| +WalletItems::LegalDocument* WalletItems::LegalDocument::FromDictionary(
|
| + base::DictionaryValue* dictionary) {
|
| + DCHECK(dictionary);
|
| +
|
| + std::string document_id;
|
| + if (!dictionary->GetString("legal_document_id", &document_id)) {
|
| + LOG(ERROR) << "Response from Google Wallet missing legal document id";
|
| + return NULL;
|
| + }
|
| + std::string display_name;
|
| + if (!dictionary->GetString("display_name", &display_name)) {
|
| + LOG(ERROR) << "Response from Google Wallet missing display name";
|
| + return NULL;
|
| + }
|
| + std::string document_body;
|
| + if (!dictionary->GetString("document", &document_body)) {
|
| + LOG(ERROR) << "Response from Google Wallet missing document body";
|
| + return NULL;
|
| + }
|
| + return new WalletItems::LegalDocument(document_id,
|
| + display_name,
|
| + document_body);
|
| +}
|
| +
|
| +bool WalletItems::LegalDocument::operator==(const LegalDocument& other) const {
|
| + if (document_id_.compare(other.document_id_) != 0)
|
| + return false;
|
| + if (display_name_.compare(other.display_name_) != 0)
|
| + return false;
|
| + if (document_body_.compare(other.document_body_) != 0)
|
| + return false;
|
| + return true;
|
| +}
|
| +
|
| +bool WalletItems::LegalDocument::operator!=(const LegalDocument& other) const {
|
| + return !(*this == other);
|
| +}
|
| +
|
| +WalletItems::WalletItems(const std::vector<std::string> required_actions,
|
| + const std::string& google_transaction_id,
|
| + const std::string& default_instrument_id,
|
| + const std::string& default_address_id)
|
| + : required_actions_(required_actions),
|
| + google_transaction_id_(google_transaction_id),
|
| + default_instrument_id_(default_instrument_id),
|
| + default_address_id_(default_address_id) {}
|
| +
|
| +WalletItems::~WalletItems() {}
|
| +
|
| +scoped_ptr<WalletItems> WalletItems::FromDictionary(
|
| + base::DictionaryValue* dictionary) {
|
| + DCHECK(dictionary);
|
| +
|
| + std::vector<std::string> required_action;
|
| + ListValue* required_action_list;
|
| + if (dictionary->GetList("required_action", &required_action_list)) {
|
| + for (size_t i = 0; i < required_action_list->GetSize(); i++) {
|
| + std::string action;
|
| + if (required_action_list->GetString(i, &action))
|
| + required_action.push_back(action);
|
| + }
|
| + if (required_action.size() > 0) {
|
| + return scoped_ptr<WalletItems>(new WalletItems(required_action,
|
| + "",
|
| + "",
|
| + ""));
|
| + }
|
| + } else {
|
| + VLOG(1) << "Response from Google wallet missing required actions";
|
| + }
|
| + std::string google_transaction_id;
|
| + if (!dictionary->GetString("google_transaction_id", &google_transaction_id)) {
|
| + LOG(ERROR) << "Response from Google wallet missing google transaction id";
|
| + return scoped_ptr<WalletItems>();
|
| + }
|
| + std::string default_instrument_id;
|
| + if (!dictionary->GetString("default_instrument_id", &default_instrument_id)) {
|
| + LOG(ERROR) << "Response from Google wallet missing default instrument id";
|
| + return scoped_ptr<WalletItems>();
|
| + }
|
| + std::string default_address_id;
|
| + if (!dictionary->GetString("default_address_id", &default_address_id))
|
| + VLOG(1) << "Response from Google wallet missing default_address_id";
|
| +
|
| + WalletItems* wallet_items = new WalletItems(required_action,
|
| + google_transaction_id,
|
| + default_instrument_id,
|
| + default_address_id);
|
| +
|
| + ListValue* instruments;
|
| + if (dictionary->GetList("instrument", &instruments)) {
|
| + for (size_t i = 0; i < instruments->GetSize(); i++) {
|
| + DictionaryValue* instrument_dict;
|
| + if (instruments->GetDictionary(i, &instrument_dict)) {
|
| + MaskedInstrument* instrument =
|
| + MaskedInstrument::FromDictionary(instrument_dict);
|
| + if (instrument) {
|
| + wallet_items->AddInstrument(instrument);
|
| + }
|
| + // TODO(ahutter): Should I consider malformed input an error?
|
| + }
|
| + }
|
| + } else {
|
| + VLOG(1) << "Response from Google wallet missing instruments";
|
| + }
|
| + ListValue* addresses;
|
| + if (dictionary->GetList("address", &addresses)) {
|
| + for (size_t i = 0; i < addresses->GetSize(); i++) {
|
| + DictionaryValue* address_dict;
|
| + if (addresses->GetDictionary(i, &address_dict)) {
|
| + scoped_ptr<Address> address =
|
| + Address::FromWalletDictionary(address_dict);
|
| + if (address.get()) {
|
| + wallet_items->AddAddress(address.release());
|
| + }
|
| + // TODO(ahutter): Should I consider malformed input an error?
|
| + }
|
| + }
|
| + } else {
|
| + VLOG(1) << "Response from Google wallet missing addresses";
|
| + }
|
| + ListValue* legal_docs;
|
| + if (dictionary->GetList("required_legal_document", &legal_docs)) {
|
| + for (size_t i = 0; i < legal_docs->GetSize(); i++) {
|
| + DictionaryValue* legal_doc_dict;
|
| + if (legal_docs->GetDictionary(i, &legal_doc_dict)) {
|
| + LegalDocument* legal_doc =
|
| + LegalDocument::FromDictionary(legal_doc_dict);
|
| + if (legal_doc) {
|
| + wallet_items->AddLegalDocument(legal_doc);
|
| + }
|
| + // TODO(ahutter): Should I consider malformed input an error?
|
| + }
|
| + }
|
| + } else {
|
| + VLOG(1) << "Response from Google wallet missing legal docs";
|
| + }
|
| +
|
| + return scoped_ptr<WalletItems>(wallet_items);
|
| +}
|
| +
|
| +bool WalletItems::operator==(const WalletItems& other) const {
|
| + if (google_transaction_id_.compare(other.google_transaction_id_) != 0)
|
| + return false;
|
| + if (default_instrument_id_.compare(other.default_instrument_id_))
|
| + return false;
|
| + if (default_address_id_.compare(other.default_address_id_))
|
| + return false;
|
| + if (required_actions_ != required_actions_)
|
| + return false;
|
| + // TODO(ahutter): how do i check the scoped vector equality...?
|
| + return true;
|
| +}
|
| +bool WalletItems::operator!=(const WalletItems& other) const {
|
| + return !(*this == other);
|
| +}
|
| +
|
| +} // end wallet namespace
|
|
|