Home | History | Annotate | Download | only in autofill
      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 #ifndef CHROME_BROWSER_UI_AUTOFILL_DATA_MODEL_WRAPPER_H_
      6 #define CHROME_BROWSER_UI_AUTOFILL_DATA_MODEL_WRAPPER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "base/strings/string16.h"
     12 #include "chrome/browser/ui/autofill/autofill_dialog_types.h"
     13 #include "components/autofill/content/browser/wallet/wallet_items.h"
     14 #include "components/autofill/core/browser/field_types.h"
     15 #include "components/autofill/core/browser/form_structure.h"
     16 
     17 namespace gfx {
     18 class Image;
     19 }
     20 
     21 namespace i18n {
     22 namespace addressinput {
     23 struct AddressData;
     24 }
     25 }
     26 
     27 namespace autofill {
     28 
     29 class AutofillDataModel;
     30 class AutofillProfile;
     31 class AutofillType;
     32 class CreditCard;
     33 class FormStructure;
     34 
     35 namespace wallet {
     36 class Address;
     37 class FullWallet;
     38 }
     39 
     40 // A glue class that allows uniform interactions with autocomplete data sources,
     41 // regardless of their type. Implementations are intended to be lightweight and
     42 // copyable, only holding weak references to their backing model.
     43 class DataModelWrapper {
     44  public:
     45   virtual ~DataModelWrapper();
     46 
     47   // Fills in |inputs| with the data that this model contains (|inputs| is an
     48   // out-param).
     49   void FillInputs(DetailInputs* inputs);
     50 
     51   // Returns the data for a specific autocomplete type in a format for filling
     52   // into a web form.
     53   virtual base::string16 GetInfo(const AutofillType& type) const = 0;
     54 
     55   // Returns the data for a specified type in a format optimized for displaying
     56   // to the user.
     57   virtual base::string16 GetInfoForDisplay(const AutofillType& type) const;
     58 
     59   // Returns the icon, if any, that represents this model.
     60   virtual gfx::Image GetIcon();
     61 
     62   // Gets text to display to the user to summarize this data source. The
     63   // default implementation assumes this is an address. Both params are required
     64   // to be non-NULL and will be filled in with text that is vertically compact
     65   // (but may take up a lot of horizontal space) and horizontally compact (but
     66   // may take up a lot of vertical space) respectively. The return value will
     67   // be true and the outparams will be filled in only if the data represented is
     68   // complete and valid.
     69   virtual bool GetDisplayText(base::string16* vertically_compact,
     70                               base::string16* horizontally_compact);
     71 
     72   // Returns the BCP 47 language code that should be used for formatting the
     73   // data for display.
     74   virtual const std::string& GetLanguageCode() const = 0;
     75 
     76   // Fills in |form_structure| with the data that this model contains. |inputs|
     77   // and |comparator| are used to determine whether each field in the
     78   // FormStructure should be filled in or left alone. Returns whether any fields
     79   // in |form_structure| were found to be matching.
     80   bool FillFormStructure(
     81       const std::vector<ServerFieldType>& types,
     82       const FormStructure::InputFieldComparator& compare,
     83       FormStructure* form_structure) const;
     84 
     85  protected:
     86   DataModelWrapper();
     87 
     88  private:
     89   DISALLOW_COPY_AND_ASSIGN(DataModelWrapper);
     90 };
     91 
     92 // A DataModelWrapper for Autofill profiles.
     93 class AutofillProfileWrapper : public DataModelWrapper {
     94  public:
     95   explicit AutofillProfileWrapper(const AutofillProfile* profile);
     96   AutofillProfileWrapper(const AutofillProfile* profile,
     97                          const AutofillType& variant_type,
     98                          size_t variant);
     99   virtual ~AutofillProfileWrapper();
    100 
    101   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    102   virtual base::string16 GetInfoForDisplay(const AutofillType& type) const
    103       OVERRIDE;
    104   virtual const std::string& GetLanguageCode() const OVERRIDE;
    105 
    106  protected:
    107   // Returns the variant that should be used when dealing with an element that
    108   // has the given |type|.
    109   size_t GetVariantForType(const AutofillType& type) const;
    110 
    111  private:
    112   const AutofillProfile* profile_;
    113 
    114   // The profile variant. |variant_| describes which variant of |variant_group_|
    115   // to use in the profile.
    116   FieldTypeGroup variant_group_;
    117   size_t variant_;
    118 
    119   DISALLOW_COPY_AND_ASSIGN(AutofillProfileWrapper);
    120 };
    121 
    122 // A DataModelWrapper specifically for shipping address profiles.
    123 class AutofillShippingAddressWrapper : public AutofillProfileWrapper {
    124  public:
    125   explicit AutofillShippingAddressWrapper(const AutofillProfile* profile);
    126   virtual ~AutofillShippingAddressWrapper();
    127 
    128   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    129 
    130  private:
    131   DISALLOW_COPY_AND_ASSIGN(AutofillShippingAddressWrapper);
    132 };
    133 
    134 // A DataModelWrapper specifically for Autofill CreditCard data.
    135 class AutofillCreditCardWrapper : public DataModelWrapper {
    136  public:
    137   explicit AutofillCreditCardWrapper(const CreditCard* card);
    138   virtual ~AutofillCreditCardWrapper();
    139 
    140   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    141   virtual gfx::Image GetIcon() OVERRIDE;
    142   virtual bool GetDisplayText(base::string16* vertically_compact,
    143                               base::string16* horizontally_compact) OVERRIDE;
    144   virtual const std::string& GetLanguageCode() const OVERRIDE;
    145 
    146  private:
    147   const CreditCard* card_;
    148 
    149   DISALLOW_COPY_AND_ASSIGN(AutofillCreditCardWrapper);
    150 };
    151 
    152 // A DataModelWrapper for Wallet addresses.
    153 class WalletAddressWrapper : public DataModelWrapper {
    154  public:
    155   explicit WalletAddressWrapper(const wallet::Address* address);
    156   virtual ~WalletAddressWrapper();
    157 
    158   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    159   virtual base::string16 GetInfoForDisplay(const AutofillType& type) const
    160       OVERRIDE;
    161   virtual bool GetDisplayText(base::string16* vertically_compact,
    162                               base::string16* horizontally_compact) OVERRIDE;
    163   virtual const std::string& GetLanguageCode() const OVERRIDE;
    164 
    165  private:
    166   const wallet::Address* address_;
    167 
    168   DISALLOW_COPY_AND_ASSIGN(WalletAddressWrapper);
    169 };
    170 
    171 // A DataModelWrapper for Wallet instruments.
    172 class WalletInstrumentWrapper : public DataModelWrapper {
    173  public:
    174   explicit WalletInstrumentWrapper(
    175       const wallet::WalletItems::MaskedInstrument* instrument);
    176   virtual ~WalletInstrumentWrapper();
    177 
    178   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    179   virtual base::string16 GetInfoForDisplay(const AutofillType& type) const
    180       OVERRIDE;
    181   virtual gfx::Image GetIcon() OVERRIDE;
    182   virtual bool GetDisplayText(base::string16* vertically_compact,
    183                               base::string16* horizontally_compact) OVERRIDE;
    184   virtual const std::string& GetLanguageCode() const OVERRIDE;
    185 
    186  private:
    187   const wallet::WalletItems::MaskedInstrument* instrument_;
    188 
    189   DISALLOW_COPY_AND_ASSIGN(WalletInstrumentWrapper);
    190 };
    191 
    192 // A DataModelWrapper for FullWallet billing data.
    193 class FullWalletBillingWrapper : public DataModelWrapper {
    194  public:
    195   explicit FullWalletBillingWrapper(wallet::FullWallet* full_wallet);
    196   virtual ~FullWalletBillingWrapper();
    197 
    198   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    199   virtual bool GetDisplayText(base::string16* vertically_compact,
    200                               base::string16* horizontally_compact) OVERRIDE;
    201   virtual const std::string& GetLanguageCode() const OVERRIDE;
    202 
    203  private:
    204   wallet::FullWallet* full_wallet_;
    205 
    206   DISALLOW_COPY_AND_ASSIGN(FullWalletBillingWrapper);
    207 };
    208 
    209 // A DataModelWrapper for FullWallet shipping data.
    210 class FullWalletShippingWrapper : public DataModelWrapper {
    211  public:
    212   explicit FullWalletShippingWrapper(wallet::FullWallet* full_wallet);
    213   virtual ~FullWalletShippingWrapper();
    214 
    215   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    216   virtual const std::string& GetLanguageCode() const OVERRIDE;
    217 
    218  private:
    219   wallet::FullWallet* full_wallet_;
    220 
    221   DISALLOW_COPY_AND_ASSIGN(FullWalletShippingWrapper);
    222 };
    223 
    224 // A DataModelWrapper for ::i18n::addressinput::AddressData objects.
    225 class I18nAddressDataWrapper : public DataModelWrapper {
    226  public:
    227   explicit I18nAddressDataWrapper(
    228       const ::i18n::addressinput::AddressData* address);
    229   virtual ~I18nAddressDataWrapper();
    230 
    231   virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
    232   virtual const std::string& GetLanguageCode() const OVERRIDE;
    233 
    234  private:
    235   const ::i18n::addressinput::AddressData* address_;
    236 
    237   DISALLOW_COPY_AND_ASSIGN(I18nAddressDataWrapper);
    238 };
    239 
    240 }  // namespace autofill
    241 
    242 #endif  // CHROME_BROWSER_UI_AUTOFILL_DATA_MODEL_WRAPPER_H_
    243