autofill_manager.h revision 307db3d811ae8f73246af95b9bf4e4a5f82f65eb
1// Copyright (c) 2010 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 "chrome/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;
33class TabContentsObserver;
34
35#ifdef ANDROID
36class AutoFillHost;
37#endif
38
39namespace webkit_glue {
40struct FormData;
41class FormField;
42}  // namespace webkit_glue
43
44// Manages saving and restoring the user's personal information entered into web
45// forms.
46class AutoFillManager :
47#ifndef ANDROID
48                        public TabContentsObserver,
49#endif
50                        public AutoFillDownloadManager::Observer {
51 public:
52  explicit AutoFillManager(TabContents* tab_contents);
53  virtual ~AutoFillManager();
54
55  // Registers our browser prefs.
56  static void RegisterBrowserPrefs(PrefService* prefs);
57
58  // Registers our Enable/Disable AutoFill pref.
59  static void RegisterUserPrefs(PrefService* prefs);
60
61  // Returns the TabContents hosting this AutoFillManager.
62  TabContents* tab_contents() const { return tab_contents_; }
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();
117  AutoFillManager(TabContents* tab_contents,
118                  PersonalDataManager* personal_data);
119
120  void set_personal_data_manager(PersonalDataManager* personal_data) {
121    personal_data_ = personal_data;
122  }
123
124  const AutoFillMetrics* metric_logger() const {
125    return metric_logger_.get();
126  }
127  void set_metric_logger(const AutoFillMetrics* metric_logger);
128
129  ScopedVector<FormStructure>* form_structures() { return &form_structures_; }
130
131  // Maps GUIDs to and from IDs that are used to identify profiles and credit
132  // cards sent to and from the renderer process.
133  virtual int GUIDToID(const std::string& guid);
134  virtual const std::string IDToGUID(int id);
135
136  // Methods for packing and unpacking credit card and profile IDs for sending
137  // and receiving to and from the renderer process.
138  int PackGUIDs(const std::string& cc_guid, const std::string& profile_guid);
139  void UnpackGUIDs(int id, std::string* cc_guid, std::string* profile_guid);
140
141 private:
142  void OnFormSubmitted(const webkit_glue::FormData& form);
143  void OnFormsSeen(const std::vector<webkit_glue::FormData>& forms);
144#ifdef ANDROID
145  bool OnQueryFormFieldAutoFill(int query_id,
146                                const webkit_glue::FormData& form,
147                                const webkit_glue::FormField& field);
148#else
149  void OnQueryFormFieldAutoFill(int query_id,
150                                const webkit_glue::FormData& form,
151                                const webkit_glue::FormField& field);
152#endif
153  void OnFillAutoFillFormData(int query_id,
154                              const webkit_glue::FormData& form,
155                              const webkit_glue::FormField& field,
156                              int unique_id);
157  void OnShowAutoFillDialog();
158  void OnDidFillAutoFillFormData();
159  void OnDidShowAutoFillSuggestions();
160
161  // Fills |host| with the RenderViewHost for this tab.
162  // Returns false if AutoFill is disabled or if the host is unavailable.
163  bool GetHost(const std::vector<AutoFillProfile*>& profiles,
164               const std::vector<CreditCard*>& credit_cards,
165#ifdef ANDROID
166               AutoFillHost** host) WARN_UNUSED_RESULT;
167#else
168               RenderViewHost** host) WARN_UNUSED_RESULT;
169#endif
170
171  // Fills |form_structure| cached element corresponding to |form|.
172  // Returns false if the cached element was not found.
173  bool FindCachedForm(const webkit_glue::FormData& form,
174                      FormStructure** form_structure) WARN_UNUSED_RESULT;
175
176  // Fills |form_structure| and |autofill_field| with the cached elements
177  // corresponding to |form| and |field|. Returns false if the cached elements
178  // were not found.
179  bool FindCachedFormAndField(
180      const webkit_glue::FormData& form,
181      const webkit_glue::FormField& field,
182      FormStructure** form_structure,
183      AutoFillField** autofill_field) WARN_UNUSED_RESULT;
184
185  // Returns a list of values from the stored profiles that match |type| and the
186  // value of |field| and returns the labels of the matching profiles. |labels|
187  // is filled with the Profile label.
188  void GetProfileSuggestions(FormStructure* form,
189                             const webkit_glue::FormField& field,
190                             AutoFillType type,
191                             std::vector<string16>* values,
192                             std::vector<string16>* labels,
193                             std::vector<string16>* icons,
194                             std::vector<int>* unique_ids);
195
196  // Returns a list of values from the stored credit cards that match |type| and
197  // the value of |field| and returns the labels of the matching credit cards.
198  void GetCreditCardSuggestions(FormStructure* form,
199                                const webkit_glue::FormField& field,
200                                AutoFillType type,
201                                std::vector<string16>* values,
202                                std::vector<string16>* labels,
203                                std::vector<string16>* icons,
204                                std::vector<int>* unique_ids);
205
206  // Set |field| argument's value based on |type| and contents of the
207  // |credit_card|.
208  void FillCreditCardFormField(const CreditCard* credit_card,
209                               AutoFillType type,
210                               webkit_glue::FormField* field);
211
212  // Set |field| argument's value based on |type| and contents of the |profile|.
213  void FillFormField(const AutoFillProfile* profile,
214                     AutoFillType type,
215                     webkit_glue::FormField* field);
216
217  // Set |field| argument's value for phone/fax number based on contents of the
218  // |profile|. |type| is the type of the phone.
219  void FillPhoneNumberField(const AutoFillProfile* profile,
220                            AutoFillType type,
221                            webkit_glue::FormField* field);
222
223  // Parses the forms using heuristic matching and querying the AutoFill server.
224  void ParseForms(const std::vector<webkit_glue::FormData>& forms);
225
226  // Uses existing personal data to determine possible field types for the
227  // |submitted_form|.
228  void DeterminePossibleFieldTypesForUpload(FormStructure* submitted_form);
229
230  void LogMetricsAboutSubmittedForm(const webkit_glue::FormData& form,
231                                    const FormStructure* submitted_form);
232
233  // The TabContents hosting this AutoFillManager.
234  // Weak reference.
235  // May not be NULL.
236  TabContents* tab_contents_;
237
238  // The personal data manager, used to save and load personal data to/from the
239  // web database.  This is overridden by the AutoFillManagerTest.
240  // Weak reference.
241  // May be NULL.  NULL indicates OTR.
242  PersonalDataManager* personal_data_;
243
244  std::list<std::string> autofilled_forms_signatures_;
245  // Handles queries and uploads to AutoFill servers.
246  AutoFillDownloadManager download_manager_;
247
248  // Should be set to true in AutoFillManagerTest and other tests, false in
249  // AutoFillDownloadManagerTest and in non-test environment. Is false by
250  // default for the public constructor, and true by default for the test-only
251  // constructors.
252  bool disable_download_manager_requests_;
253
254  // For logging UMA metrics. Overridden by metrics tests.
255  scoped_ptr<const AutoFillMetrics> metric_logger_;
256
257  // Our copy of the form data.
258  ScopedVector<FormStructure> form_structures_;
259
260#ifdef ANDROID
261  // To minimize merge conflicts, we keep this pointer around, but never use it.
262  void* cc_infobar_;
263#else
264  // The InfoBar that asks for permission to store credit card information.
265  // Deletes itself when closed.
266  AutoFillCCInfoBarDelegate* cc_infobar_;
267#endif
268
269  // The imported credit card that should be saved if the user accepts the
270  // infobar.
271  scoped_ptr<const CreditCard> imported_credit_card_;
272
273  // GUID to ID mapping.  We keep two maps to convert back and forth.
274  std::map<std::string, int> guid_id_map_;
275  std::map<int, std::string> id_guid_map_;
276
277  friend class AutoFillManagerTest;
278  friend class FormStructureBrowserTest;
279  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillCreditCardForm);
280  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest,
281                           FillCreditCardFormNoYearNoMonth);
282  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillCreditCardFormYearNoMonth);
283  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillCreditCardFormNoYearMonth);
284  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillCreditCardFormYearMonth);
285  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillAddressForm);
286  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillAddressAndCreditCardForm);
287  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillFormWithMultipleSections);
288  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillAutoFilledForm);
289  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FillPhoneNumber);
290  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FormChangesRemoveField);
291  FRIEND_TEST_ALL_PREFIXES(AutoFillManagerTest, FormChangesAddField);
292  FRIEND_TEST_ALL_PREFIXES(AutoFillMetricsTest, QualityMetrics);
293  FRIEND_TEST_ALL_PREFIXES(AutoFillMetricsTest,
294                           NoQualityMetricsForNonAutoFillableForms);
295  FRIEND_TEST_ALL_PREFIXES(AutoFillMetricsTest, SaneMetricsWithCacheMismatch);
296  FRIEND_TEST_ALL_PREFIXES(AutoFillMetricsTest, QualityMetricsForFailure);
297  FRIEND_TEST_ALL_PREFIXES(AutoFillMetricsTest, QualityMetricsWithExperimentId);
298
299  DISALLOW_COPY_AND_ASSIGN(AutoFillManager);
300};
301
302#endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_MANAGER_H_
303