data_model_wrapper.h revision 58537e28ecd584eab876aee8be7156509866d23a
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
13namespace gfx {
14class Image;
15}
16
17namespace autofill {
18
19class AutofillDataModel;
20class AutofillProfile;
21class AutofillType;
22class CreditCard;
23class FormStructure;
24
25namespace wallet {
26class Address;
27class 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.
33class 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.
82class 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.
96class 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.
116class 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 shipping address profiles.
132class AutofillShippingAddressWrapper : public AutofillProfileWrapper {
133 public:
134  AutofillShippingAddressWrapper(const AutofillProfile* profile,
135                                 size_t variant);
136  virtual ~AutofillShippingAddressWrapper();
137
138  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
139
140 private:
141  DISALLOW_COPY_AND_ASSIGN(AutofillShippingAddressWrapper);
142};
143
144// A DataModelWrapper specifically for Autofill CreditCard data.
145class AutofillCreditCardWrapper : public AutofillDataModelWrapper {
146 public:
147  explicit AutofillCreditCardWrapper(const CreditCard* card);
148  virtual ~AutofillCreditCardWrapper();
149
150  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
151  virtual gfx::Image GetIcon() OVERRIDE;
152  virtual bool GetDisplayText(base::string16* vertically_compact,
153                              base::string16* horizontally_compact) OVERRIDE;
154
155 private:
156  const CreditCard* card_;
157
158  DISALLOW_COPY_AND_ASSIGN(AutofillCreditCardWrapper);
159};
160
161// A DataModelWrapper for Wallet addresses.
162class WalletAddressWrapper : public DataModelWrapper {
163 public:
164  explicit WalletAddressWrapper(const wallet::Address* address);
165  virtual ~WalletAddressWrapper();
166
167  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
168  virtual bool GetDisplayText(base::string16* vertically_compact,
169                              base::string16* horizontally_compact) OVERRIDE;
170
171 private:
172  const wallet::Address* address_;
173
174  DISALLOW_COPY_AND_ASSIGN(WalletAddressWrapper);
175};
176
177// A DataModelWrapper for Wallet instruments.
178class WalletInstrumentWrapper : public DataModelWrapper {
179 public:
180  explicit WalletInstrumentWrapper(
181      const wallet::WalletItems::MaskedInstrument* instrument);
182  virtual ~WalletInstrumentWrapper();
183
184  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
185  virtual gfx::Image GetIcon() OVERRIDE;
186  virtual bool GetDisplayText(base::string16* vertically_compact,
187                              base::string16* horizontally_compact) OVERRIDE;
188
189 private:
190  const wallet::WalletItems::MaskedInstrument* instrument_;
191
192  DISALLOW_COPY_AND_ASSIGN(WalletInstrumentWrapper);
193};
194
195// A DataModelWrapper for FullWallet billing data.
196class FullWalletBillingWrapper : public DataModelWrapper {
197 public:
198  explicit FullWalletBillingWrapper(wallet::FullWallet* full_wallet);
199  virtual ~FullWalletBillingWrapper();
200
201  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
202  virtual bool GetDisplayText(base::string16* vertically_compact,
203                              base::string16* horizontally_compact) OVERRIDE;
204
205 private:
206  wallet::FullWallet* full_wallet_;
207
208  DISALLOW_COPY_AND_ASSIGN(FullWalletBillingWrapper);
209};
210
211// A DataModelWrapper for FullWallet shipping data.
212class FullWalletShippingWrapper : public DataModelWrapper {
213 public:
214  explicit FullWalletShippingWrapper(wallet::FullWallet* full_wallet);
215  virtual ~FullWalletShippingWrapper();
216
217  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
218
219 private:
220  wallet::FullWallet* full_wallet_;
221
222  DISALLOW_COPY_AND_ASSIGN(FullWalletShippingWrapper);
223};
224
225// A DataModelWrapper to copy the output of one section to the input of another.
226class DetailOutputWrapper : public DataModelWrapper {
227 public:
228  explicit DetailOutputWrapper(const DetailOutputMap& outputs);
229  virtual ~DetailOutputWrapper();
230
231  virtual base::string16 GetInfo(const AutofillType& type) const OVERRIDE;
232
233 private:
234  const DetailOutputMap& outputs_;
235
236  DISALLOW_COPY_AND_ASSIGN(DetailOutputWrapper);
237};
238
239}  // namespace autofill
240
241#endif  // CHROME_BROWSER_UI_AUTOFILL_DATA_MODEL_WRAPPER_H_
242