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 <map> 11 #include <string> 12 #include <vector> 13 14 #include "base/basictypes.h" 15 #include "base/compiler_specific.h" 16 #include "base/gtest_prod_util.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_vector.h" 19 #include "base/string16.h" 20 #include "chrome/browser/autofill/autofill_download.h" 21 #include "chrome/browser/autofill/field_types.h" 22 #include "chrome/browser/autofill/form_structure.h" 23 #ifndef ANDROID 24 #include "content/browser/tab_contents/navigation_controller.h" 25 #endif 26 #include "content/browser/tab_contents/tab_contents_observer.h" 27 28 class AutofillField; 29 class AutofillProfile; 30 class AutofillMetrics; 31 class CreditCard; 32 class PersonalDataManager; 33 class PrefService; 34 class RenderViewHost; 35 class TabContents; 36 37 #ifdef ANDROID 38 class AutoFillHost; 39 #endif 40 41 struct ViewHostMsg_FrameNavigate_Params; 42 43 namespace IPC { 44 class Message; 45 } 46 47 namespace webkit_glue { 48 struct FormData; 49 struct FormField; 50 } 51 52 // Manages saving and restoring the user's personal information entered into web 53 // forms. 54 class AutofillManager : public TabContentsObserver, 55 public AutofillDownloadManager::Observer { 56 public: 57 explicit AutofillManager(TabContents* tab_contents); 58 virtual ~AutofillManager(); 59 60 #ifndef ANDROID 61 // Registers our browser prefs. 62 static void RegisterBrowserPrefs(PrefService* prefs); 63 #endif 64 65 #ifndef ANDROID 66 // Registers our Enable/Disable Autofill pref. 67 static void RegisterUserPrefs(PrefService* prefs); 68 #endif 69 70 #ifndef ANDROID 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 #endif 77 78 // AutofillDownloadManager::Observer implementation: 79 virtual void OnLoadedAutofillHeuristics(const std::string& heuristic_xml); 80 virtual void OnUploadedAutofillHeuristics(const std::string& form_signature); 81 virtual void OnHeuristicsRequestError( 82 const std::string& form_signature, 83 AutofillDownloadManager::AutofillRequestType request_type, 84 int http_error); 85 86 // Returns the value of the AutofillEnabled pref. 87 virtual bool IsAutofillEnabled() const; 88 89 // Imports the form data, submitted by the user, into |personal_data_|. 90 void ImportFormData(const FormStructure& submitted_form); 91 92 // Uploads the form data to the Autofill server. 93 void UploadFormData(const FormStructure& submitted_form); 94 95 // Reset cache. 96 void Reset(); 97 98 #ifdef ANDROID 99 void OnFormsSeenWrapper(const std::vector<webkit_glue::FormData>& forms) { 100 OnFormsSeen(forms); 101 } 102 103 bool OnQueryFormFieldAutoFillWrapper(const webkit_glue::FormData& form, 104 const webkit_glue::FormField& field) { 105 return OnQueryFormFieldAutofill(0, form, field); 106 } 107 108 void OnFillAutoFillFormDataWrapper(int query_id, 109 const webkit_glue::FormData& form, 110 const webkit_glue::FormField& field, 111 int unique_id) { 112 OnFillAutofillFormData(query_id, form, field, unique_id); 113 } 114 #endif 115 116 protected: 117 // For tests: 118 119 // The string/int pair is composed of the guid string and variant index 120 // respectively. The variant index is an index into the multi-valued item 121 // (where applicable). 122 typedef std::pair<std::string, size_t> GUIDPair; 123 124 AutofillManager(TabContents* tab_contents, 125 PersonalDataManager* personal_data); 126 127 void set_personal_data_manager(PersonalDataManager* personal_data) { 128 personal_data_ = personal_data; 129 } 130 131 const AutofillMetrics* metric_logger() const { return metric_logger_.get(); } 132 void set_metric_logger(const AutofillMetrics* metric_logger); 133 134 ScopedVector<FormStructure>* form_structures() { return &form_structures_; } 135 136 // Maps GUIDs to and from IDs that are used to identify profiles and credit 137 // cards sent to and from the renderer process. 138 virtual int GUIDToID(const GUIDPair& guid); 139 virtual const GUIDPair IDToGUID(int id); 140 141 // Methods for packing and unpacking credit card and profile IDs for sending 142 // and receiving to and from the renderer process. 143 int PackGUIDs(const GUIDPair& cc_guid, const GUIDPair& profile_guid); 144 void UnpackGUIDs(int id, GUIDPair* cc_guid, GUIDPair* profile_guid); 145 146 private: 147 void OnFormSubmitted(const webkit_glue::FormData& form); 148 void OnFormsSeen(const std::vector<webkit_glue::FormData>& forms); 149 #ifdef ANDROID 150 bool 151 #else 152 void 153 #endif 154 OnQueryFormFieldAutofill(int query_id, 155 const webkit_glue::FormData& form, 156 const webkit_glue::FormField& field); 157 void OnFillAutofillFormData(int query_id, 158 const webkit_glue::FormData& form, 159 const webkit_glue::FormField& field, 160 int unique_id); 161 void OnShowAutofillDialog(); 162 void OnDidFillAutofillFormData(); 163 void OnDidShowAutofillSuggestions(); 164 165 // Fills |host| with the RenderViewHost for this tab. 166 // Returns false if Autofill is disabled or if the host is unavailable. 167 bool GetHost(const std::vector<AutofillProfile*>& profiles, 168 const std::vector<CreditCard*>& credit_cards, 169 #ifdef ANDROID 170 AutoFillHost** host) WARN_UNUSED_RESULT; 171 #else 172 RenderViewHost** host) const WARN_UNUSED_RESULT; 173 #endif 174 175 // Fills |form_structure| cached element corresponding to |form|. 176 // Returns false if the cached element was not found. 177 bool FindCachedForm(const webkit_glue::FormData& form, 178 FormStructure** form_structure) const WARN_UNUSED_RESULT; 179 180 // Fills |form_structure| and |autofill_field| with the cached elements 181 // corresponding to |form| and |field|. Returns false if the cached elements 182 // were not found. 183 bool FindCachedFormAndField( 184 const webkit_glue::FormData& form, 185 const webkit_glue::FormField& field, 186 FormStructure** form_structure, 187 AutofillField** autofill_field) WARN_UNUSED_RESULT; 188 189 // Returns a list of values from the stored profiles that match |type| and the 190 // value of |field| and returns the labels of the matching profiles. |labels| 191 // is filled with the Profile label. 192 void GetProfileSuggestions(FormStructure* form, 193 const webkit_glue::FormField& field, 194 AutofillFieldType type, 195 std::vector<string16>* values, 196 std::vector<string16>* labels, 197 std::vector<string16>* icons, 198 std::vector<int>* unique_ids); 199 200 // Returns a list of values from the stored credit cards that match |type| and 201 // the value of |field| and returns the labels of the matching credit cards. 202 void GetCreditCardSuggestions(FormStructure* form, 203 const webkit_glue::FormField& field, 204 AutofillFieldType type, 205 std::vector<string16>* values, 206 std::vector<string16>* labels, 207 std::vector<string16>* icons, 208 std::vector<int>* unique_ids); 209 210 // Set |field| argument's value based on |type| and contents of the 211 // |credit_card|. 212 void FillCreditCardFormField(const CreditCard* credit_card, 213 AutofillFieldType type, 214 webkit_glue::FormField* field); 215 216 // Set |field| argument's value based on |type| and contents of the |profile|. 217 // The |variant| parameter specifies which value in a multi-valued profile. 218 void FillFormField(const AutofillProfile* profile, 219 AutofillFieldType type, 220 size_t variant, 221 webkit_glue::FormField* field); 222 223 // Set |field| argument's value for phone/fax number based on contents of the 224 // |profile|. |type| is the type of the phone. 225 // The |variant| parameter specifies which value in a multi-valued profile. 226 void FillPhoneNumberField(const AutofillProfile* profile, 227 AutofillFieldType type, 228 size_t variant, 229 webkit_glue::FormField* field); 230 231 // Parses the forms using heuristic matching and querying the Autofill server. 232 void ParseForms(const std::vector<webkit_glue::FormData>& forms); 233 234 // Uses existing personal data to determine possible field types for the 235 // |submitted_form|. 236 void DeterminePossibleFieldTypesForUpload(FormStructure* submitted_form); 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 // Have we logged whether Autofill is enabled for this page load? 258 bool has_logged_autofill_enabled_; 259 260 // Have we logged an address suggestions count metric for this page? 261 bool has_logged_address_suggestions_count_; 262 263 // Our copy of the form data. 264 ScopedVector<FormStructure> form_structures_; 265 266 // GUID to ID mapping. We keep two maps to convert back and forth. 267 std::map<GUIDPair, int> guid_id_map_; 268 std::map<int, GUIDPair> id_guid_map_; 269 270 friend class AutofillManagerTest; 271 friend class FormStructureBrowserTest; 272 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardForm); 273 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 274 FillCreditCardFormNoYearNoMonth); 275 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormYearNoMonth); 276 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormNoYearMonth); 277 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillCreditCardFormYearMonth); 278 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillAddressForm); 279 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillAddressAndCreditCardForm); 280 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillFormWithMultipleSections); 281 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FillFormWithMultipleEmails); 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(AutofillManagerTest, FormSubmitted); 287 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, FormSubmittedServerTypes); 288 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AddressSuggestionsCount); 289 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtPageLoad); 290 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, 291 NoQualityMetricsForNonAutofillableForms); 292 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetrics); 293 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsForFailure); 294 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsWithExperimentId); 295 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, SaneMetricsWithCacheMismatch); 296 297 DISALLOW_COPY_AND_ASSIGN(AutofillManager); 298 }; 299 300 #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_MANAGER_H_ 301