wallet_address.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 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#ifndef COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ADDRESS_H_ 6#define COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ADDRESS_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/strings/string16.h" 13#include "components/autofill/core/browser/phone_number_i18n.h" 14 15namespace base { 16class DictionaryValue; 17} 18 19namespace autofill { 20 21class AutofillProfile; 22class AutofillType; 23 24namespace wallet { 25 26// TODO(ahutter): This address is a lot like 27// components/autofill/core/browser/address.h. There should be a super 28// class that both extend from to clean up duplicated code. See 29// http://crbug.com/164463. 30 31// Address contains various address fields that have been populated from the 32// user's Online Wallet. It is loosely modeled as a subet of the OASIS 33// "extensible Address Language" (xAL); see 34// http://www.oasis-open.org/committees/ciq/download.shtml. 35class Address { 36 public: 37 // TODO(ahutter): Use additional fields (descriptive_name, is_post_box, 38 // is_valid, is_default) when SaveToWallet is implemented. 39 // See http://crbug.com/164284. 40 41 Address(); 42 43 // Using the raw info in |profile|, create a wallet::Address. 44 explicit Address(const AutofillProfile& profile); 45 46 Address(const std::string& country_name_code, 47 const base::string16& recipient_name, 48 const std::vector<base::string16>& street_address, 49 const base::string16& locality_name, 50 const base::string16& dependent_locality_name, 51 const base::string16& administrative_area_name, 52 const base::string16& postal_code_number, 53 const base::string16& sorting_code, 54 const base::string16& phone_number, 55 const std::string& object_id); 56 57 ~Address(); 58 59 // Returns an empty scoped_ptr if input is invalid or a valid address that is 60 // selectable for Google Wallet use. Does not require "id" in |dictionary|. 61 // IDs are not required for billing addresses. 62 static scoped_ptr<Address> CreateAddress( 63 const base::DictionaryValue& dictionary); 64 65 // TODO(ahutter): Make obvious in the function name that this public method 66 // only works for shipping address and assumes existance of "postal_address". 67 // Builds an Address from |dictionary|, which must have an "id" field. This 68 // function is designed for use with shipping addresses. The function may fail 69 // and return an empty pointer if its input is invalid. 70 static scoped_ptr<Address> CreateAddressWithID( 71 const base::DictionaryValue& dictionary); 72 73 // Returns an empty scoped_ptr if input in invalid or a valid address that 74 // can only be used for displaying to the user. 75 static scoped_ptr<Address> CreateDisplayAddress( 76 const base::DictionaryValue& dictionary); 77 78 // If an address is being upgraded, it will be sent to the server in a 79 // different format and with a few additional fields set, most importantly 80 // |object_id_|. 81 scoped_ptr<base::DictionaryValue> ToDictionaryWithID() const; 82 83 // Newly created addresses will not have an associated |object_id_| and are 84 // sent to the server in a slightly different format. 85 scoped_ptr<base::DictionaryValue> ToDictionaryWithoutID() const; 86 87 // Returns a string that summarizes this address, suitable for display to 88 // the user. 89 base::string16 DisplayName() const; 90 91 // Returns a string that could be used as a sub-label, suitable for display 92 // to the user together with DisplayName(). 93 base::string16 DisplayNameDetail() const; 94 95 // Returns the phone number as a string that is suitable for display to the 96 // user. 97 base::string16 DisplayPhoneNumber() const; 98 99 // Returns data appropriate for |type|. 100 base::string16 GetInfo(const AutofillType& type, 101 const std::string& app_locale) const; 102 103 const std::string& country_name_code() const { return country_name_code_; } 104 const base::string16& recipient_name() const { return recipient_name_; } 105 const std::vector<base::string16>& street_address() const { 106 return street_address_; 107 } 108 const base::string16& locality_name() const { return locality_name_; } 109 const base::string16& administrative_area_name() const { 110 return administrative_area_name_; 111 } 112 const base::string16& postal_code_number() const { 113 return postal_code_number_; 114 } 115 const base::string16& phone_number() const { return phone_number_; } 116 const std::string& object_id() const { return object_id_; } 117 bool is_complete_address() const { 118 return is_complete_address_; 119 } 120 121 void set_country_name_code(const std::string& country_name_code) { 122 country_name_code_ = country_name_code; 123 } 124 void set_recipient_name(const base::string16& recipient_name) { 125 recipient_name_ = recipient_name; 126 } 127 void set_street_address(const std::vector<base::string16>& street_address) { 128 street_address_ = street_address; 129 } 130 void set_locality_name(const base::string16& locality_name) { 131 locality_name_ = locality_name; 132 } 133 void set_dependent_locality_name( 134 const base::string16& dependent_locality_name) { 135 dependent_locality_name_ = dependent_locality_name; 136 } 137 void set_administrative_area_name( 138 const base::string16& administrative_area_name) { 139 administrative_area_name_ = administrative_area_name; 140 } 141 void set_postal_code_number(const base::string16& postal_code_number) { 142 postal_code_number_ = postal_code_number; 143 } 144 void set_sorting_code(const base::string16& sorting_code) { 145 sorting_code_ = sorting_code; 146 } 147 void SetPhoneNumber(const base::string16& phone_number); 148 void set_object_id(const std::string& object_id) { 149 object_id_ = object_id; 150 } 151 void set_is_complete_address(bool is_complete_address) { 152 is_complete_address_ = is_complete_address; 153 } 154 155 // Tests if this address exact matches |other|. |object_id| is ignored. 156 bool EqualsIgnoreID(const Address& other) const; 157 158 // Tests if this address exact matches |other| including |object_id|. 159 bool operator==(const Address& other) const; 160 bool operator!=(const Address& other) const; 161 162 private: 163 // Gets the street address on the given line (0-indexed). 164 base::string16 GetStreetAddressLine(size_t line) const; 165 166 // |country_name_code_| should be an ISO 3166-1-alpha-2 (two letter codes, as 167 // used in DNS). For example, "GB". 168 std::string country_name_code_; 169 170 // The recipient's name. For example "John Doe". 171 base::string16 recipient_name_; 172 173 // Address lines (arbitrarily many). 174 std::vector<base::string16> street_address_; 175 176 // Locality. This is something of a fuzzy term, but it generally refers to 177 // the city/town portion of an address. 178 // Examples: US city, IT comune, UK post town. 179 base::string16 locality_name_; 180 181 // Dependent locality is used in Korea and China. 182 // Example: a Chinese county under the authority of a prefecture-level city. 183 base::string16 dependent_locality_name_; 184 185 // Top-level administrative subdivision of this country. 186 // Examples: US state, IT region, UK constituent nation, JP prefecture. 187 // Note: this must be in short form, e.g. TX rather than Texas. 188 base::string16 administrative_area_name_; 189 190 // Despite the name, |postal_code_number_| values are frequently alphanumeric. 191 // Examples: "94043", "SW1W", "SW1W 9TQ". 192 base::string16 postal_code_number_; 193 194 // Sorting code, e.g. CEDEX in France. 195 base::string16 sorting_code_; 196 197 // A valid international phone number. If |phone_number_| is a user provided 198 // value, it should have been validated using libphonenumber by clients of 199 // this class before being set; see http://code.google.com/p/libphonenumber/. 200 base::string16 phone_number_; 201 202 // The parsed phone number. 203 i18n::PhoneObject phone_object_; 204 205 // Externalized Online Wallet id for this address. 206 std::string object_id_; 207 208 // Server's understanding of this address as complete address or not. 209 bool is_complete_address_; 210 211 // This class is intentionally copyable. 212 DISALLOW_ASSIGN(Address); 213}; 214 215} // namespace wallet 216} // namespace autofill 217 218#endif // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ADDRESS_H_ 219