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          const std::string& language_code);
57
58  ~Address();
59
60  // Returns an empty scoped_ptr if input is invalid or a valid address that is
61  // selectable for Google Wallet use. Does not require "id" in |dictionary|.
62  // IDs are not required for billing addresses.
63  static scoped_ptr<Address> CreateAddress(
64      const base::DictionaryValue& dictionary);
65
66  // TODO(ahutter): Make obvious in the function name that this public method
67  // only works for shipping address and assumes existance of "postal_address".
68  // Builds an Address from |dictionary|, which must have an "id" field. This
69  // function is designed for use with shipping addresses. The function may fail
70  // and return an empty pointer if its input is invalid.
71  static scoped_ptr<Address> CreateAddressWithID(
72      const base::DictionaryValue& dictionary);
73
74  // Returns an empty scoped_ptr if input in invalid or a valid address that
75  // can only be used for displaying to the user.
76  static scoped_ptr<Address> CreateDisplayAddress(
77      const base::DictionaryValue& dictionary);
78
79  // If an address is being upgraded, it will be sent to the server in a
80  // different format and with a few additional fields set, most importantly
81  // |object_id_|.
82  scoped_ptr<base::DictionaryValue> ToDictionaryWithID() const;
83
84  // Newly created addresses will not have an associated |object_id_| and are
85  // sent to the server in a slightly different format.
86  scoped_ptr<base::DictionaryValue> ToDictionaryWithoutID() const;
87
88  // Returns a string that summarizes this address, suitable for display to
89  // the user.
90  base::string16 DisplayName() const;
91
92  // Returns a string that could be used as a sub-label, suitable for display
93  // to the user together with DisplayName().
94  base::string16 DisplayNameDetail() const;
95
96  // Returns the phone number as a string that is suitable for display to the
97  // user.
98  base::string16 DisplayPhoneNumber() const;
99
100  // Returns data appropriate for |type|.
101  base::string16 GetInfo(const AutofillType& type,
102                         const std::string& app_locale) const;
103
104  const std::string& country_name_code() const { return country_name_code_; }
105  const base::string16& recipient_name() const { return recipient_name_; }
106  const std::vector<base::string16>& street_address() const {
107    return street_address_;
108  }
109  const base::string16& locality_name() const { return locality_name_; }
110  const base::string16& administrative_area_name() const {
111    return administrative_area_name_;
112  }
113  const base::string16& postal_code_number() const {
114    return postal_code_number_;
115  }
116  const base::string16& phone_number() const { return phone_number_; }
117  const std::string& object_id() const { return object_id_; }
118  bool is_complete_address() const {
119    return is_complete_address_;
120  }
121  const std::string& language_code() const { return language_code_; }
122
123  void set_country_name_code(const std::string& country_name_code) {
124    country_name_code_ = country_name_code;
125  }
126  void set_recipient_name(const base::string16& recipient_name) {
127    recipient_name_ = recipient_name;
128  }
129  void set_street_address(const std::vector<base::string16>& street_address) {
130    street_address_ = street_address;
131  }
132  void set_locality_name(const base::string16& locality_name) {
133    locality_name_ = locality_name;
134  }
135  void set_dependent_locality_name(
136        const base::string16& dependent_locality_name) {
137    dependent_locality_name_ = dependent_locality_name;
138  }
139  void set_administrative_area_name(
140      const base::string16& administrative_area_name) {
141    administrative_area_name_ = administrative_area_name;
142  }
143  void set_postal_code_number(const base::string16& postal_code_number) {
144    postal_code_number_ = postal_code_number;
145  }
146  void set_sorting_code(const base::string16& sorting_code) {
147    sorting_code_ = sorting_code;
148  }
149  void SetPhoneNumber(const base::string16& phone_number);
150  void set_object_id(const std::string& object_id) {
151    object_id_ = object_id;
152  }
153  void set_is_complete_address(bool is_complete_address) {
154    is_complete_address_ = is_complete_address;
155  }
156  void set_language_code(const std::string& language_code) {
157    language_code_ = language_code;
158  }
159
160  // Tests if this address exact matches |other|. This method can be used to
161  // cull duplicates. It wouldn't make sense to have multiple identical street
162  // addresses with different identifiers or language codes (used for
163  // formatting). Therefore, |object_id| and |language_code| are ignored.
164  bool EqualsIgnoreID(const Address& other) const;
165
166  // Tests if this address exact matches |other| including |object_id| and
167  // |language_code|.
168  bool operator==(const Address& other) const;
169  bool operator!=(const Address& other) const;
170
171 private:
172  // Gets the street address on the given line (0-indexed).
173  base::string16 GetStreetAddressLine(size_t line) const;
174
175  // |country_name_code_| should be an ISO 3166-1-alpha-2 (two letter codes, as
176  // used in DNS). For example, "GB".
177  std::string country_name_code_;
178
179  // The recipient's name. For example "John Doe".
180  base::string16 recipient_name_;
181
182  // Address lines (arbitrarily many).
183  std::vector<base::string16> street_address_;
184
185  // Locality.  This is something of a fuzzy term, but it generally refers to
186  // the city/town portion of an address.
187  // Examples: US city, IT comune, UK post town.
188  base::string16 locality_name_;
189
190  // Dependent locality is used in Korea and China.
191  // Example: a Chinese county under the authority of a prefecture-level city.
192  base::string16 dependent_locality_name_;
193
194  // Top-level administrative subdivision of this country.
195  // Examples: US state, IT region, UK constituent nation, JP prefecture.
196  // Note: this must be in short form, e.g. TX rather than Texas.
197  base::string16 administrative_area_name_;
198
199  // Despite the name, |postal_code_number_| values are frequently alphanumeric.
200  // Examples: "94043", "SW1W", "SW1W 9TQ".
201  base::string16 postal_code_number_;
202
203  // Sorting code, e.g. CEDEX in France.
204  base::string16 sorting_code_;
205
206  // A valid international phone number. If |phone_number_| is a user provided
207  // value, it should have been validated using libphonenumber by clients of
208  // this class before being set; see http://code.google.com/p/libphonenumber/.
209  base::string16 phone_number_;
210
211  // The parsed phone number.
212  i18n::PhoneObject phone_object_;
213
214  // Externalized Online Wallet id for this address.
215  std::string object_id_;
216
217  // Server's understanding of this address as complete address or not.
218  bool is_complete_address_;
219
220  // The BCP 47 language code that can be used for formatting this address for
221  // display.
222  std::string language_code_;
223
224  // This class is intentionally copyable.
225  DISALLOW_ASSIGN(Address);
226};
227
228}  // namespace wallet
229}  // namespace autofill
230
231#endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ADDRESS_H_
232