autofill_manager.h revision 72348d7342b67133b80566824ec56ee7afff127f
1// Copyright (c) 2011 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_AUTOFILL_AUTOFILL_MANAGER_H_
6#define CHROME_BROWSER_AUTOFILL_AUTOFILL_MANAGER_H_
7#pragma once
8
9#include <list>
10#include <string>
11#include <vector>
12
13#include "base/gtest_prod_util.h"
14#include "base/scoped_ptr.h"
15#include "base/scoped_vector.h"
16#include "chrome/browser/autofill/autofill_dialog.h"
17#include "chrome/browser/autofill/autofill_download.h"
18#include "chrome/browser/autofill/personal_data_manager.h"
19#ifndef ANDROID
20#include "content/browser/tab_contents/tab_contents_observer.h"
21#endif
22
23#ifndef ANDROID
24class AutoFillCCInfoBarDelegate;
25#endif
26class AutoFillProfile;
27class AutofillMetrics;
28class CreditCard;
29class FormStructure;
30class PrefService;
31class RenderViewHost;
32class TabContents;
33
34#ifdef ANDROID
35class AutoFillHost;
36#endif
37
38namespace webkit_glue {
39struct FormData;
40class FormField;
41}  // namespace webkit_glue
42
43// Manages saving and restoring the user's personal information entered into web
44// forms.
45class AutofillManager :
46#ifndef ANDROID
47                        public TabContentsObserver,
48#endif
49                        public AutofillDownloadManager::Observer {
50 public:
51  explicit AutofillManager(TabContents* tab_contents);
52  virtual ~AutofillManager();
53
54#ifndef ANDROID
55  // Registers our browser prefs.
56  static void RegisterBrowserPrefs(PrefService* prefs);
57#endif
58
59#ifndef ANDROID
60  // Registers our Enable/Disable Autofill pref.
61  static void RegisterUserPrefs(PrefService* prefs);
62#endif
63
64#ifndef ANDROID
65  // TabContentsObserver implementation.
66  virtual void DidNavigateMainFramePostCommit(
67      const NavigationController::LoadCommittedDetails& details,
68      const ViewHostMsg_FrameNavigate_Params& params);
69  virtual bool OnMessageReceived(const IPC::Message& message);
70#endif
71
72  // Called by the AutoFillCCInfoBarDelegate when the user interacts with the
73  // infobar.
74  virtual void OnInfoBarClosed(bool should_save);
75
76  // AutofillDownloadManager::Observer implementation:
77  virtual void OnLoadedAutofillHeuristics(const std::string& heuristic_xml);
78  virtual void OnUploadedAutofillHeuristics(const std::string& form_signature);
79  virtual void OnHeuristicsRequestError(
80      const std::string& form_signature,
81      AutofillDownloadManager::AutofillRequestType request_type,
82      int http_error);
83
84  // Returns the value of the AutoFillEnabled pref.
85  virtual bool IsAutoFillEnabled() const;
86
87  // Imports the form data, submitted by the user, into |personal_data_|.
88  void ImportFormData(const FormStructure& submitted_form);
89
90  // Uploads the form data to the Autofill server.
91  void UploadFormData(const FormStructure& submitted_form);
92
93  // Reset cache.
94  void Reset();
95
96#ifdef ANDROID
97  void OnFormsSeenWrapper(const std::vector<webkit_glue::FormData>& forms) {
98    OnFormsSeen(forms);
99  }
100
101  bool OnQueryFormFieldAutoFillWrapper(const webkit_glue::FormData& form,
102                                       const webkit_glue::FormField& field) {
103    return OnQueryFormFieldAutoFill(0, form, field);
104  }
105
106  void OnFillAutoFillFormDataWrapper(int query_id,
107                                     const webkit_glue::FormData& form,
108                                     const webkit_glue::FormField& field,
109                                     int unique_id) {
110    OnFillAutoFillFormData(query_id, form, field, unique_id);
111  }
112#endif
113
114 protected:
115  // For tests.
116  AutofillManager(TabContents* tab_contents,
117                  PersonalDataManager* personal_data);
118
119  void set_personal_data_manager(PersonalDataManager* personal_data) {
120    personal_data_ = personal_data;
121  }
122
123  const AutofillMetrics* metric_logger() const {
124    return metric_logger_.get();
125  }
126  void set_metric_logger(const AutofillMetrics* metric_logger);
127
128  ScopedVector<FormStructure>* form_structures() { return &form_structures_; }
129
130  // Maps GUIDs to and from IDs that are used to identify profiles and credit
131  // cards sent to and from the renderer process.
132  virtual int GUIDToID(const std::string& guid);
133  virtual const std::string IDToGUID(int id);
134
135  // Methods for packing and unpacking credit card and profile IDs for sending
136  // and receiving to and from the renderer process.
137  int PackGUIDs(const std::string& cc_guid, const std::string& profile_guid);
138  void UnpackGUIDs(int id, std::string* cc_guid, std::string* profile_guid);
139
140 private:
141  void OnFormSubmitted(const webkit_glue::FormData& form);
142  void OnFormsSeen(const std::vector<webkit_glue::FormData>& forms);
143#ifdef ANDROID
144  bool OnQueryFormFieldAutoFill(int query_id,
145                                const webkit_glue::FormData& form,
146                                const webkit_glue::FormField& field);
147#else
148  void OnQueryFormFieldAutoFill(int query_id,
149                                const webkit_glue::FormData& form,
150                                const webkit_glue::FormField& field);
151#endif
152  void OnFillAutoFillFormData(int query_id,
153                              const webkit_glue::FormData& form,
154                              const webkit_glue::FormField& field,
155                              int unique_id);
156  void OnShowAutoFillDialog();
157  void OnDidFillAutoFillFormData();
158  void OnDidShowAutoFillSuggestions();
159
160  // Fills |host| with the RenderViewHost for this tab.
161  // Returns false if Autofill is disabled or if the host is unavailable.
162  bool GetHost(const std::vector<AutoFillProfile*>& profiles,
163               const std::vector<CreditCard*>& credit_cards,
164#ifdef ANDROID
165               AutoFillHost** host) WARN_UNUSED_RESULT;
166#else
167               RenderViewHost** host) WARN_UNUSED_RESULT;
168#endif
169
170  // Fills |form_structure| cached element corresponding to |form|.
171  // Returns false if the cached element was not found.
172  bool FindCachedForm(const webkit_glue::FormData& form,
173                      FormStructure** form_structure) WARN_UNUSED_RESULT;
174
175  // Fills |form_structure| and |autofill_field| with the cached elements
176  // corresponding to |form| and |field|. Returns false if the cached elements
177  // were not found.
178  bool FindCachedFormAndField(
179      const webkit_glue::FormData& form,
180      const webkit_glue::FormField& field,
181      FormStructure** form_structure,
182      AutofillField** autofill_field) WARN_UNUSED_RESULT;
183
184  // Returns a list of values from the stored profiles that match |type| and the
185  // value of |field| and returns the labels of the matching profiles. |labels|
186  // is filled with the Profile label.
187  void GetProfileSuggestions(FormStructure* form,
188                             const webkit_glue::FormField& field,
189                             AutofillType type,
190                             std::vector<string16>* values,
191                             std::vector<string16>* labels,
192                             std::vector<string16>* icons,
193                             std::vector<int>* unique_ids);
194
195  // Returns a list of values from the stored credit cards that match |type| and
196  // the value of |field| and returns the labels of the matching credit cards.
197  void GetCreditCardSuggestions(FormStructure* form,
198                                const webkit_glue::FormField& field,
199                                AutofillType type,
200                                std::vector<string16>* values,
201                                std::vector<string16>* labels,
202                                std::vector<string16>* icons,
203                                std::vector<int>* unique_ids);
204
205  // Set |field| argument's value based on |type| and contents of the
206  // |credit_card|.
207  void FillCreditCardFormField(const CreditCard* credit_card,
208                               AutofillType type,
209                               webkit_glue::FormField* field);
210
211  // Set |field| argument's value based on |type| and contents of the |profile|.
212  void FillFormField(const AutoFillProfile* profile,
213                     AutofillType type,
214                     webkit_glue::FormField* field);
215
216  // Set |field| argument's value for phone/fax number based on contents of the
217  // |profile|. |type| is the type of the phone.
218  void FillPhoneNumberField(const AutoFillProfile* profile,
219                            AutofillType type,
220                            webkit_glue::FormField* field);
221
222  // Parses the forms using heuristic matching and querying the Autofill server.
223  void ParseForms(const std::vector<webkit_glue::FormData>& forms);
224
225  // Uses existing personal data to determine possible field types for the
226  // |submitted_form|.
227  void DeterminePossibleFieldTypesForUpload(FormStructure* submitted_form);
228
229  void LogMetricsAboutSubmittedForm(const webkit_glue::FormData& form,
230                                    const FormStructure* submitted_form);
231
232  // The personal data manager, used to save and load personal data to/from the
233  // web database.  This is overridden by the AutofillManagerTest.
234  // Weak reference.
235  // May be NULL.  NULL indicates OTR.
236  PersonalDataManager* personal_data_;
237
238  std::list<std::string> autofilled_forms_signatures_;
239  // Handles queries and uploads to Autofill servers.
240  AutofillDownloadManager download_manager_;
241
242  // Should be set to true in AutofillManagerTest and other tests, false in
243  // AutofillDownloadManagerTest and in non-test environment. Is false by
244  // default for the public constructor, and true by default for the test-only
245  // constructors.
246  bool disable_download_manager_requests_;
247
248  // For logging UMA metrics. Overridden by metrics tests.
249  scoped_ptr<const AutofillMetrics> metric_logger_;
250
251  // Our copy of the form data.
252  ScopedVector<FormStructure> form_structures_;
253
254#ifdef ANDROID
255  // To minimize merge conflicts, we keep this pointer around, but never use it.
256  void* cc_infobar_;
257#else
258  // The InfoBar that asks for permission to store credit card information.
259  // Deletes itself when closed.
260  AutoFillCCInfoBarDelegate* cc_infobar_;
261#endif
262
263  // The imported credit card that should be saved if the user accepts the
264  // infobar.
265  scoped_ptr<const CreditCard> imported_credit_card_;
266
267  // GUID to ID mapping.  We keep two maps to convert back and forth.
268  std::map<std::string, int> guid_id_map_;
269  std::map<int, std::string> id_guid_map_;
270
271  friend class AutofillManagerTest;
272  friend class FormStructureBrowserTest;
273  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardForm);
274  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest,
275                           FillCreditCardFormNoYearNoMonth);
276  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormYearNoMonth);
277  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormNoYearMonth);
278  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormYearMonth);
279  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillAddressForm);
280  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillAddressAndCreditCardForm);
281  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillFormWithMultipleSections);
282  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillAutoFilledForm);
283  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillPhoneNumber);
284  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FormChangesRemoveField);
285  FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FormChangesAddField);
286  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetrics);
287  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest,
288                           NoQualityMetricsForNonAutofillableForms);
289  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, SaneMetricsWithCacheMismatch);
290  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsForFailure);
291  FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsWithExperimentId);
292
293  DISALLOW_COPY_AND_ASSIGN(AutofillManager);
294};
295
296#endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_MANAGER_H_
297