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