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