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