Home | History | Annotate | Download | only in wallet
      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 
     15 namespace base {
     16 class DictionaryValue;
     17 }
     18 
     19 namespace autofill {
     20 
     21 class AutofillProfile;
     22 class AutofillType;
     23 
     24 namespace 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.
     35 class 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 base::string16& address_line_1,
     49           const base::string16& address_line_2,
     50           const base::string16& locality_name,
     51           const base::string16& administrative_area_name,
     52           const base::string16& postal_code_number,
     53           const base::string16& phone_number,
     54           const std::string& object_id);
     55 
     56   ~Address();
     57 
     58   // Returns an empty scoped_ptr if input is invalid or a valid address that is
     59   // selectable for Google Wallet use. Does not require "id" in |dictionary|.
     60   // IDs are not required for billing addresses.
     61   static scoped_ptr<Address> CreateAddress(
     62       const base::DictionaryValue& dictionary);
     63 
     64   // TODO(ahutter): Make obvious in the function name that this public method
     65   // only works for shipping address and assumes existance of "postal_address".
     66   // Builds an Address from |dictionary|, which must have an "id" field. This
     67   // function is designed for use with shipping addresses. The function may fail
     68   // and return an empty pointer if its input is invalid.
     69   static scoped_ptr<Address> CreateAddressWithID(
     70       const base::DictionaryValue& dictionary);
     71 
     72   // Returns an empty scoped_ptr if input in invalid or a valid address that
     73   // can only be used for displaying to the user.
     74   static scoped_ptr<Address> CreateDisplayAddress(
     75       const base::DictionaryValue& dictionary);
     76 
     77   // If an address is being upgraded, it will be sent to the server in a
     78   // different format and with a few additional fields set, most importantly
     79   // |object_id_|.
     80   scoped_ptr<base::DictionaryValue> ToDictionaryWithID() const;
     81 
     82   // Newly created addresses will not have an associated |object_id_| and are
     83   // sent to the server in a slightly different format.
     84   scoped_ptr<base::DictionaryValue> ToDictionaryWithoutID() const;
     85 
     86   // Returns a string that summarizes this address, suitable for display to
     87   // the user.
     88   base::string16 DisplayName() const;
     89 
     90   // Returns a string that could be used as a sub-label, suitable for display
     91   // to the user together with DisplayName().
     92   base::string16 DisplayNameDetail() const;
     93 
     94   // Returns the phone number as a string that is suitable for display to the
     95   // user.
     96   base::string16 DisplayPhoneNumber() const;
     97 
     98   // Returns data appropriate for |type|.
     99   base::string16 GetInfo(const AutofillType& type,
    100                          const std::string& app_locale) const;
    101 
    102   const std::string& country_name_code() const { return country_name_code_; }
    103   const base::string16& recipient_name() const { return recipient_name_; }
    104   const base::string16& address_line_1() const { return address_line_1_; }
    105   const base::string16& address_line_2() const { return address_line_2_; }
    106   const base::string16& locality_name() const { return locality_name_; }
    107   const base::string16& administrative_area_name() const {
    108     return administrative_area_name_;
    109   }
    110   const base::string16& postal_code_number() const {
    111     return postal_code_number_;
    112   }
    113   const base::string16& phone_number() const { return phone_number_; }
    114   const std::string& object_id() const { return object_id_; }
    115   bool is_complete_address() const {
    116     return is_complete_address_;
    117   }
    118 
    119   void set_country_name_code(const std::string& country_name_code) {
    120     country_name_code_ = country_name_code;
    121   }
    122   void set_recipient_name(const base::string16& recipient_name) {
    123     recipient_name_ = recipient_name;
    124   }
    125   void set_address_line_1(const base::string16& address_line_1) {
    126     address_line_1_ = address_line_1;
    127   }
    128   void set_address_line_2(const base::string16& address_line_2) {
    129     address_line_2_ = address_line_2;
    130   }
    131   void set_locality_name(const base::string16& locality_name) {
    132     locality_name_ = locality_name;
    133   }
    134   void set_administrative_area_name(
    135       const base::string16& administrative_area_name) {
    136     administrative_area_name_ = administrative_area_name;
    137   }
    138   void set_postal_code_number(const base::string16& postal_code_number) {
    139     postal_code_number_ = postal_code_number;
    140   }
    141   void SetPhoneNumber(const base::string16& phone_number);
    142   void set_object_id(const std::string& object_id) {
    143     object_id_ = object_id;
    144   }
    145   void set_is_complete_address(bool is_complete_address) {
    146     is_complete_address_ = is_complete_address;
    147   }
    148 
    149   // Tests if this address exact matches |other|. |object_id| is ignored.
    150   bool EqualsIgnoreID(const Address& other) const;
    151 
    152   // Tests if this address exact matches |other| including |object_id|.
    153   bool operator==(const Address& other) const;
    154   bool operator!=(const Address& other) const;
    155 
    156  private:
    157   // |country_name_code_| should be an ISO 3166-1-alpha-2 (two letter codes, as
    158   // used in DNS). For example, "GB".
    159   std::string country_name_code_;
    160 
    161   // The recipient's name. For example "John Doe".
    162   base::string16 recipient_name_;
    163 
    164   // |address_line_1| and |address_line_2| correspond to the "AddressLine"
    165   // elements in xAL, which are used to hold unstructured text.
    166   base::string16 address_line_1_;
    167   base::string16 address_line_2_;
    168 
    169   // Locality.  This is something of a fuzzy term, but it generally refers to
    170   // the city/town portion of an address.  In regions of the world where
    171   // localities are not well defined or do not fit into this structure well
    172   // (for example, Japan and China), leave locality_name empty and use
    173   // |address_line_2|.
    174   // Examples: US city, IT comune, UK post town.
    175   base::string16 locality_name_;
    176 
    177   // Top-level administrative subdivision of this country.
    178   // Examples: US state, IT region, UK constituent nation, JP prefecture.
    179   // Note: this must be in short form, e.g. TX rather than Texas.
    180   base::string16 administrative_area_name_;
    181 
    182   // Despite the name, |postal_code_number_| values are frequently alphanumeric.
    183   // Examples: "94043", "SW1W", "SW1W 9TQ".
    184   base::string16 postal_code_number_;
    185 
    186   // A valid international phone number. If |phone_number_| is a user provided
    187   // value, it should have been validated using libphonenumber by clients of
    188   // this class before being set; see http://code.google.com/p/libphonenumber/.
    189   base::string16 phone_number_;
    190 
    191   // The parsed phone number.
    192   i18n::PhoneObject phone_object_;
    193 
    194   // Externalized Online Wallet id for this address.
    195   std::string object_id_;
    196 
    197   // Server's understanding of this address as complete address or not.
    198   bool is_complete_address_;
    199 
    200   // This class is intentionally copyable.
    201   DISALLOW_ASSIGN(Address);
    202 };
    203 
    204 }  // namespace wallet
    205 }  // namespace autofill
    206 
    207 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_WALLET_WALLET_ADDRESS_H_
    208