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