| 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_address.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/autofill/autofill_country.h" | |
| 11 | |
| 12 namespace autofill { | |
| 13 namespace wallet { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 Address* CreateAddressInternal(const base::DictionaryValue& dictionary, | |
| 18 const std::string& object_id) { | |
| 19 std::string country_name_code; | |
| 20 if (!dictionary.GetString("postal_address.country_name_code", | |
| 21 &country_name_code)) { | |
| 22 DLOG(ERROR) << "Response from Google Wallet missing country name"; | |
| 23 return NULL; | |
| 24 } | |
| 25 | |
| 26 string16 recipient_name; | |
| 27 if (!dictionary.GetString("postal_address.recipient_name", | |
| 28 &recipient_name)) { | |
| 29 DLOG(ERROR) << "Response from Google Wallet recipient name"; | |
| 30 return NULL; | |
| 31 } | |
| 32 | |
| 33 string16 postal_code_number; | |
| 34 if (!dictionary.GetString("postal_address.postal_code_number", | |
| 35 &postal_code_number)) { | |
| 36 DLOG(ERROR) << "Response from Google Wallet missing postal code number"; | |
| 37 return NULL; | |
| 38 } | |
| 39 | |
| 40 string16 phone_number; | |
| 41 if (!dictionary.GetString("phone_number", &phone_number)) | |
| 42 DVLOG(1) << "Response from Google Wallet missing phone number"; | |
| 43 | |
| 44 string16 address_line_1; | |
| 45 string16 address_line_2; | |
| 46 const ListValue* address_line_list; | |
| 47 if (dictionary.GetList("postal_address.address_line", &address_line_list)) { | |
| 48 if (!address_line_list->GetString(0, &address_line_1)) | |
| 49 DVLOG(1) << "Response from Google Wallet missing address line 1"; | |
| 50 if (!address_line_list->GetString(1, &address_line_2)) | |
| 51 DVLOG(1) << "Response from Google Wallet missing address line 2"; | |
| 52 } else { | |
| 53 DVLOG(1) << "Response from Google Wallet missing address lines"; | |
| 54 } | |
| 55 | |
| 56 string16 locality_name; | |
| 57 if (!dictionary.GetString("postal_address.locality_name", | |
| 58 &locality_name)) { | |
| 59 DVLOG(1) << "Response from Google Wallet missing locality name"; | |
| 60 } | |
| 61 | |
| 62 string16 administrative_area_name; | |
| 63 if (!dictionary.GetString("postal_address.administrative_area_name", | |
| 64 &administrative_area_name)) { | |
| 65 DVLOG(1) << "Response from Google Wallet missing administrative area name"; | |
| 66 } | |
| 67 | |
| 68 return new Address(country_name_code, | |
| 69 recipient_name , | |
| 70 address_line_1, | |
| 71 address_line_2, | |
| 72 locality_name, | |
| 73 administrative_area_name, | |
| 74 postal_code_number, | |
| 75 phone_number, | |
| 76 object_id); | |
| 77 } | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 Address::Address() {} | |
| 82 | |
| 83 Address::Address(const std::string& country_name_code, | |
| 84 const string16& recipient_name, | |
| 85 const string16& address_line_1, | |
| 86 const string16& address_line_2, | |
| 87 const string16& locality_name, | |
| 88 const string16& administrative_area_name, | |
| 89 const string16& postal_code_number, | |
| 90 const string16& phone_number, | |
| 91 const std::string& object_id) | |
| 92 : country_name_code_(country_name_code), | |
| 93 recipient_name_(recipient_name), | |
| 94 address_line_1_(address_line_1), | |
| 95 address_line_2_(address_line_2), | |
| 96 locality_name_(locality_name), | |
| 97 administrative_area_name_(administrative_area_name), | |
| 98 postal_code_number_(postal_code_number), | |
| 99 phone_number_(phone_number), | |
| 100 object_id_(object_id) {} | |
| 101 | |
| 102 Address::~Address() {} | |
| 103 | |
| 104 // static | |
| 105 scoped_ptr<Address> Address::CreateAddressWithID( | |
| 106 const base::DictionaryValue& dictionary) { | |
| 107 std::string object_id; | |
| 108 if (!dictionary.GetString("id", &object_id)) { | |
| 109 DLOG(ERROR) << "Response from Google Wallet missing object id"; | |
| 110 return scoped_ptr<Address>(); | |
| 111 } | |
| 112 return scoped_ptr<Address>(CreateAddressInternal(dictionary, object_id)); | |
| 113 } | |
| 114 | |
| 115 // static | |
| 116 scoped_ptr<Address> Address::CreateAddress( | |
| 117 const base::DictionaryValue& dictionary) { | |
| 118 std::string object_id; | |
| 119 dictionary.GetString("id", &object_id); | |
| 120 return scoped_ptr<Address>(CreateAddressInternal(dictionary, object_id)); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 scoped_ptr<Address> Address::CreateDisplayAddress( | |
| 125 const base::DictionaryValue& dictionary) { | |
| 126 std::string country_code; | |
| 127 if (!dictionary.GetString("country_code", &country_code)) { | |
| 128 DLOG(ERROR) << "Reponse from Google Wallet missing country code"; | |
| 129 return scoped_ptr<Address>(); | |
| 130 } | |
| 131 | |
| 132 string16 name; | |
| 133 if (!dictionary.GetString("name", &name)) { | |
| 134 DLOG(ERROR) << "Reponse from Google Wallet missing name"; | |
| 135 return scoped_ptr<Address>(); | |
| 136 } | |
| 137 | |
| 138 string16 postal_code; | |
| 139 if (!dictionary.GetString("postal_code", &postal_code)) { | |
| 140 DLOG(ERROR) << "Reponse from Google Wallet missing postal code"; | |
| 141 return scoped_ptr<Address>(); | |
| 142 } | |
| 143 | |
| 144 string16 address1; | |
| 145 if (!dictionary.GetString("address1", &address1)) | |
| 146 DVLOG(1) << "Reponse from Google Wallet missing address1"; | |
| 147 | |
| 148 string16 address2; | |
| 149 if (!dictionary.GetString("address2", &address2)) | |
| 150 DVLOG(1) << "Reponse from Google Wallet missing address2"; | |
| 151 | |
| 152 string16 city; | |
| 153 if (!dictionary.GetString("city", &city)) | |
| 154 DVLOG(1) << "Reponse from Google Wallet missing city"; | |
| 155 | |
| 156 string16 state; | |
| 157 if (!dictionary.GetString("state", &state)) | |
| 158 DVLOG(1) << "Reponse from Google Wallet missing state"; | |
| 159 | |
| 160 string16 phone_number; | |
| 161 if (!dictionary.GetString("phone_number", &phone_number)) | |
| 162 DVLOG(1) << "Reponse from Google Wallet missing phone number"; | |
| 163 | |
| 164 return scoped_ptr<Address>(new Address(country_code, | |
| 165 name, | |
| 166 address1, | |
| 167 address2, | |
| 168 city, | |
| 169 state, | |
| 170 postal_code, | |
| 171 phone_number, | |
| 172 std::string())); | |
| 173 } | |
| 174 | |
| 175 scoped_ptr<base::DictionaryValue> Address::ToDictionaryWithID() const { | |
| 176 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 177 | |
| 178 if (!object_id_.empty()) | |
| 179 dict->SetString("id", object_id_); | |
| 180 dict->SetString("phone_number", phone_number_); | |
| 181 dict->Set("postal_address", ToDictionaryWithoutID().release()); | |
| 182 | |
| 183 return dict.Pass(); | |
| 184 } | |
| 185 | |
| 186 scoped_ptr<base::DictionaryValue> Address::ToDictionaryWithoutID() const { | |
| 187 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | |
| 188 | |
| 189 scoped_ptr<base::ListValue> address_lines(new base::ListValue()); | |
| 190 address_lines->AppendString(address_line_1_); | |
| 191 if (!address_line_2_.empty()) | |
| 192 address_lines->AppendString(address_line_2_); | |
| 193 dict->Set("address_line", address_lines.release()); | |
| 194 | |
| 195 dict->SetString("country_name_code", country_name_code_); | |
| 196 dict->SetString("recipient_name", recipient_name_); | |
| 197 dict->SetString("locality_name", locality_name_); | |
| 198 dict->SetString("administrative_area_name", | |
| 199 administrative_area_name_); | |
| 200 dict->SetString("postal_code_number", postal_code_number_); | |
| 201 | |
| 202 return dict.Pass(); | |
| 203 } | |
| 204 | |
| 205 string16 Address::DisplayName() const { | |
| 206 // TODO(estade): improve this stub implementation. | |
| 207 return recipient_name() + ASCIIToUTF16(", ") + address_line_1(); | |
| 208 } | |
| 209 | |
| 210 string16 Address::GetInfo(AutofillFieldType type) const { | |
| 211 switch (type) { | |
| 212 case NAME_FULL: | |
| 213 return recipient_name(); | |
| 214 | |
| 215 case ADDRESS_HOME_LINE1: | |
| 216 return address_line_1(); | |
| 217 | |
| 218 case ADDRESS_HOME_LINE2: | |
| 219 return address_line_2(); | |
| 220 | |
| 221 case ADDRESS_HOME_CITY: | |
| 222 return locality_name(); | |
| 223 | |
| 224 case ADDRESS_HOME_STATE: | |
| 225 return administrative_area_name(); | |
| 226 | |
| 227 case ADDRESS_HOME_ZIP: | |
| 228 return postal_code_number(); | |
| 229 | |
| 230 case ADDRESS_HOME_COUNTRY: { | |
| 231 AutofillCountry country(country_name_code(), | |
| 232 AutofillCountry::ApplicationLocale()); | |
| 233 return country.name(); | |
| 234 } | |
| 235 | |
| 236 case PHONE_HOME_WHOLE_NUMBER: | |
| 237 return phone_number(); | |
| 238 | |
| 239 // TODO(estade): implement more. | |
| 240 default: | |
| 241 NOTREACHED(); | |
| 242 return string16(); | |
| 243 } | |
| 244 } | |
| 245 | |
| 246 bool Address::operator==(const Address& other) const { | |
| 247 return country_name_code_ == other.country_name_code_ && | |
| 248 recipient_name_ == other.recipient_name_ && | |
| 249 address_line_1_ == other.address_line_1_ && | |
| 250 address_line_2_ == other.address_line_2_ && | |
| 251 locality_name_ == other.locality_name_ && | |
| 252 administrative_area_name_ == other.administrative_area_name_ && | |
| 253 postal_code_number_ == other.postal_code_number_ && | |
| 254 phone_number_ == other.phone_number_ && | |
| 255 object_id_ == other.object_id_; | |
| 256 } | |
| 257 | |
| 258 bool Address::operator!=(const Address& other) const { | |
| 259 return !(*this == other); | |
| 260 } | |
| 261 | |
| 262 } // namespace wallet | |
| 263 } // namespace autofill | |
| OLD | NEW |