autofill_manager.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
1// Copyright 2013 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 COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_MANAGER_H_ 6#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_MANAGER_H_ 7 8#include <list> 9#include <map> 10#include <string> 11#include <vector> 12 13#include "base/basictypes.h" 14#include "base/callback_forward.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/memory/weak_ptr.h" 20#include "base/strings/string16.h" 21#include "base/time/time.h" 22#include "components/autofill/core/browser/autocomplete_history_manager.h" 23#include "components/autofill/core/browser/autofill_download.h" 24#include "components/autofill/core/browser/autofill_driver.h" 25#include "components/autofill/core/browser/autofill_manager_delegate.h" 26#include "components/autofill/core/browser/form_structure.h" 27#include "components/autofill/core/browser/personal_data_manager.h" 28#include "components/autofill/core/common/form_data.h" 29#include "components/autofill/core/common/forms_seen_state.h" 30 31class GURL; 32 33namespace content { 34class RenderViewHost; 35class WebContents; 36} 37 38namespace gfx { 39class Rect; 40class RectF; 41} 42 43namespace user_prefs { 44class PrefRegistrySyncable; 45} 46 47namespace autofill { 48 49class AutofillDataModel; 50class AutofillDownloadManager; 51class AutofillExternalDelegate; 52class AutofillField; 53class AutofillManagerDelegate; 54class AutofillManagerTestDelegate; 55class AutofillMetrics; 56class AutofillProfile; 57class AutofillType; 58class CreditCard; 59class FormStructureBrowserTest; 60 61struct FormData; 62struct FormFieldData; 63struct PasswordFormFillData; 64 65// Manages saving and restoring the user's personal information entered into web 66// forms. 67class AutofillManager : public AutofillDownloadManager::Observer { 68 public: 69 enum AutofillDownloadManagerState { 70 ENABLE_AUTOFILL_DOWNLOAD_MANAGER, 71 DISABLE_AUTOFILL_DOWNLOAD_MANAGER, 72 }; 73 74 // Registers our Enable/Disable Autofill pref. 75 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 76 77 AutofillManager(AutofillDriver* driver, 78 autofill::AutofillManagerDelegate* delegate, 79 const std::string& app_locale, 80 AutofillDownloadManagerState enable_download_manager); 81 virtual ~AutofillManager(); 82 83 // Sets an external delegate. 84 void SetExternalDelegate(AutofillExternalDelegate* delegate); 85 86 void ShowAutofillSettings(); 87 88 // Called from our external delegate so they cannot be private. 89 virtual void FillOrPreviewForm(AutofillDriver::RendererFormDataAction action, 90 int query_id, 91 const FormData& form, 92 const FormFieldData& field, 93 int unique_id); 94 void DidShowSuggestions(bool is_new_popup); 95 void OnDidFillAutofillFormData(const base::TimeTicks& timestamp); 96 void OnDidPreviewAutofillFormData(); 97 98 // Remove the credit card or Autofill profile that matches |unique_id| 99 // from the database. 100 void RemoveAutofillProfileOrCreditCard(int unique_id); 101 102 // Remove the specified Autocomplete entry. 103 void RemoveAutocompleteEntry(const base::string16& name, 104 const base::string16& value); 105 106 // Returns the present form structures seen by Autofill manager. 107 const std::vector<FormStructure*>& GetFormStructures(); 108 109 // Happens when the autocomplete dialog runs its callback when being closed. 110 void RequestAutocompleteDialogClosed(); 111 112 autofill::AutofillManagerDelegate* delegate() const { 113 return manager_delegate_; 114 } 115 116 const std::string& app_locale() const { return app_locale_; } 117 118 // Only for testing. 119 void SetTestDelegate(autofill::AutofillManagerTestDelegate* delegate); 120 121 void OnFormsSeen(const std::vector<FormData>& forms, 122 const base::TimeTicks& timestamp, 123 autofill::FormsSeenState state); 124 125 // Processes the submitted |form|, saving any new Autofill data and uploading 126 // the possible field types for the submitted fields to the crowdsourcing 127 // server. Returns false if this form is not relevant for Autofill. 128 bool OnFormSubmitted(const FormData& form, 129 const base::TimeTicks& timestamp); 130 131 void OnTextFieldDidChange(const FormData& form, 132 const FormFieldData& field, 133 const base::TimeTicks& timestamp); 134 135 // The |bounding_box| is a window relative value. 136 void OnQueryFormFieldAutofill(int query_id, 137 const FormData& form, 138 const FormFieldData& field, 139 const gfx::RectF& bounding_box, 140 bool display_warning); 141 void OnDidEndTextFieldEditing(); 142 void OnHidePopup(); 143 void OnSetDataList(const std::vector<base::string16>& values, 144 const std::vector<base::string16>& labels); 145 146 // Try and upload |form|. This differs from OnFormSubmitted() in a few ways. 147 // - This function will only label the first <input type="password"> field 148 // as ACCOUNT_CREATION_PASSWORD. Other fields will stay unlabeled, as they 149 // should have been labeled during the upload for OnFormSubmitted(). 150 // - This function does not assume that |form| is being uploaded during 151 // the same browsing session as it was originally submitted (as we may 152 // not have the necessary information to classify the form at that time) 153 // so it bypasses the cache and doesn't log the same quality UMA metrics. 154 bool UploadPasswordGenerationForm(const FormData& form); 155 156 // Resets cache. 157 virtual void Reset(); 158 159 // Returns the value of the AutofillEnabled pref. 160 virtual bool IsAutofillEnabled() const; 161 162 protected: 163 // Test code should prefer to use this constructor. 164 AutofillManager(AutofillDriver* driver, 165 autofill::AutofillManagerDelegate* delegate, 166 PersonalDataManager* personal_data); 167 168 // Uploads the form data to the Autofill server. 169 virtual void UploadFormData(const FormStructure& submitted_form); 170 171 // Logs quality metrics for the |submitted_form| and uploads the form data 172 // to the crowdsourcing server, if appropriate. 173 virtual void UploadFormDataAsyncCallback( 174 const FormStructure* submitted_form, 175 const base::TimeTicks& load_time, 176 const base::TimeTicks& interaction_time, 177 const base::TimeTicks& submission_time); 178 179 // Maps GUIDs to and from IDs that are used to identify profiles and credit 180 // cards sent to and from the renderer process. 181 virtual int GUIDToID(const PersonalDataManager::GUIDPair& guid) const; 182 virtual const PersonalDataManager::GUIDPair IDToGUID(int id) const; 183 184 // Methods for packing and unpacking credit card and profile IDs for sending 185 // and receiving to and from the renderer process. 186 int PackGUIDs(const PersonalDataManager::GUIDPair& cc_guid, 187 const PersonalDataManager::GUIDPair& profile_guid) const; 188 void UnpackGUIDs(int id, 189 PersonalDataManager::GUIDPair* cc_guid, 190 PersonalDataManager::GUIDPair* profile_guid) const; 191 192 const AutofillMetrics* metric_logger() const { return metric_logger_.get(); } 193 void set_metric_logger(const AutofillMetrics* metric_logger); 194 195 ScopedVector<FormStructure>* form_structures() { return &form_structures_; } 196 197 // Exposed for testing. 198 AutofillExternalDelegate* external_delegate() { 199 return external_delegate_; 200 } 201 202 private: 203 // AutofillDownloadManager::Observer: 204 virtual void OnLoadedServerPredictions( 205 const std::string& response_xml) OVERRIDE; 206 207 // Returns false if Autofill is disabled or if no Autofill data is available. 208 bool RefreshDataModels() const; 209 210 // Unpacks |unique_id| and fills |form_group| and |variant| with the 211 // appropriate data source and variant index. Sets |is_credit_card| to true 212 // if |data_model| points to a CreditCard data model, false if it's a 213 // profile data model. 214 // Returns false if the unpacked id cannot be found. 215 bool GetProfileOrCreditCard(int unique_id, 216 const AutofillDataModel** data_model, 217 size_t* variant, 218 bool* is_credit_card) const WARN_UNUSED_RESULT; 219 220 // Fills |form_structure| cached element corresponding to |form|. 221 // Returns false if the cached element was not found. 222 bool FindCachedForm(const FormData& form, 223 FormStructure** form_structure) const WARN_UNUSED_RESULT; 224 225 // Fills |form_structure| and |autofill_field| with the cached elements 226 // corresponding to |form| and |field|. This might have the side-effect of 227 // updating the cache. Returns false if the |form| is not autofillable, or if 228 // it is not already present in the cache and the cache is full. 229 bool GetCachedFormAndField(const FormData& form, 230 const FormFieldData& field, 231 FormStructure** form_structure, 232 AutofillField** autofill_field) WARN_UNUSED_RESULT; 233 234 // Re-parses |live_form| and adds the result to |form_structures_|. 235 // |cached_form| should be a pointer to the existing version of the form, or 236 // NULL if no cached version exists. The updated form is then written into 237 // |updated_form|. Returns false if the cache could not be updated. 238 bool UpdateCachedForm(const FormData& live_form, 239 const FormStructure* cached_form, 240 FormStructure** updated_form) WARN_UNUSED_RESULT; 241 242 // Returns a list of values from the stored profiles that match |type| and the 243 // value of |field| and returns the labels of the matching profiles. |labels| 244 // is filled with the Profile label. 245 void GetProfileSuggestions(FormStructure* form, 246 const FormFieldData& field, 247 const AutofillType& type, 248 std::vector<base::string16>* values, 249 std::vector<base::string16>* labels, 250 std::vector<base::string16>* icons, 251 std::vector<int>* unique_ids) const; 252 253 // Returns a list of values from the stored credit cards that match |type| and 254 // the value of |field| and returns the labels of the matching credit cards. 255 void GetCreditCardSuggestions(const FormFieldData& field, 256 const AutofillType& type, 257 std::vector<base::string16>* values, 258 std::vector<base::string16>* labels, 259 std::vector<base::string16>* icons, 260 std::vector<int>* unique_ids) const; 261 262 // Parses the forms using heuristic matching and querying the Autofill server. 263 void ParseForms(const std::vector<FormData>& forms); 264 265 // Imports the form data, submitted by the user, into |personal_data_|. 266 void ImportFormData(const FormStructure& submitted_form); 267 268 // If |initial_interaction_timestamp_| is unset or is set to a later time than 269 // |interaction_timestamp|, updates the cached timestamp. The latter check is 270 // needed because IPC messages can arrive out of order. 271 void UpdateInitialInteractionTimestamp( 272 const base::TimeTicks& interaction_timestamp); 273 274 // Shared code to determine if |form| should be uploaded. 275 bool ShouldUploadForm(const FormStructure& form); 276 277 // Provides driver-level context to the shared code of the component. Must 278 // outlive this object. 279 AutofillDriver* driver_; 280 281 autofill::AutofillManagerDelegate* const manager_delegate_; 282 283 std::string app_locale_; 284 285 // The personal data manager, used to save and load personal data to/from the 286 // web database. This is overridden by the AutofillManagerTest. 287 // Weak reference. 288 // May be NULL. NULL indicates OTR. 289 PersonalDataManager* personal_data_; 290 291 std::list<std::string> autofilled_form_signatures_; 292 293 // Handles queries and uploads to Autofill servers. Will be NULL if 294 // the download manager functionality is disabled. 295 scoped_ptr<AutofillDownloadManager> download_manager_; 296 297 // Handles single-field autocomplete form data. 298 scoped_ptr<AutocompleteHistoryManager> autocomplete_history_manager_; 299 300 // For logging UMA metrics. Overridden by metrics tests. 301 scoped_ptr<const AutofillMetrics> metric_logger_; 302 // Have we logged whether Autofill is enabled for this page load? 303 bool has_logged_autofill_enabled_; 304 // Have we logged an address suggestions count metric for this page? 305 bool has_logged_address_suggestions_count_; 306 // Have we shown Autofill suggestions at least once? 307 bool did_show_suggestions_; 308 // Has the user manually edited at least one form field among the autofillable 309 // ones? 310 bool user_did_type_; 311 // Has the user autofilled a form on this page? 312 bool user_did_autofill_; 313 // Has the user edited a field that was previously autofilled? 314 bool user_did_edit_autofilled_field_; 315 // When the page finished loading. 316 base::TimeTicks forms_loaded_timestamp_; 317 // When the user first interacted with a potentially fillable form on this 318 // page. 319 base::TimeTicks initial_interaction_timestamp_; 320 321 // Our copy of the form data. 322 ScopedVector<FormStructure> form_structures_; 323 324 // GUID to ID mapping. We keep two maps to convert back and forth. 325 mutable std::map<PersonalDataManager::GUIDPair, int> guid_id_map_; 326 mutable std::map<int, PersonalDataManager::GUIDPair> id_guid_map_; 327 328 // Delegate to perform external processing (display, selection) on 329 // our behalf. Weak. 330 AutofillExternalDelegate* external_delegate_; 331 332 // Delegate used in test to get notifications on certain events. 333 autofill::AutofillManagerTestDelegate* test_delegate_; 334 335 base::WeakPtrFactory<AutofillManager> weak_ptr_factory_; 336 337 friend class AutofillManagerTest; 338 friend class autofill::FormStructureBrowserTest; 339 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 340 DeterminePossibleFieldTypesForUpload); 341 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 342 DeterminePossibleFieldTypesForUploadStressTest); 343 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 344 DisabledAutofillDispatchesError); 345 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AddressSuggestionsCount); 346 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtPageLoad); 347 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, DeveloperEngagement); 348 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, FormFillDuration); 349 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, 350 NoQualityMetricsForNonAutofillableForms); 351 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetrics); 352 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsForFailure); 353 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsWithExperimentId); 354 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, SaneMetricsWithCacheMismatch); 355 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, TestExternalDelegate); 356 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 357 TestTabContentsWithExternalDelegate); 358 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, 359 UserHappinessFormLoadAndSubmission); 360 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, UserHappinessFormInteraction); 361 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 362 FormSubmittedAutocompleteEnabled); 363 DISALLOW_COPY_AND_ASSIGN(AutofillManager); 364}; 365 366} // namespace autofill 367 368#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_MANAGER_H_ 369