autofill_manager.h revision 58537e28ecd584eab876aee8be7156509866d23a
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 web_contents state. 106 content::WebContents* GetWebContents() const; 107 108 // Returns the present form structures seen by Autofill manager. 109 const std::vector<FormStructure*>& GetFormStructures(); 110 111 // Causes the dialog for request autocomplete feature to be shown. 112 virtual void ShowRequestAutocompleteDialog( 113 const FormData& form, 114 const GURL& source_url, 115 const base::Callback<void(const FormStructure*, 116 const std::string&)>& callback); 117 118 // Happens when the autocomplete dialog runs its callback when being closed. 119 void RequestAutocompleteDialogClosed(); 120 121 autofill::AutofillManagerDelegate* delegate() const { 122 return manager_delegate_; 123 } 124 125 const std::string& app_locale() const { return app_locale_; } 126 127 // Only for testing. 128 void SetTestDelegate(autofill::AutofillManagerTestDelegate* delegate); 129 130 void OnFormsSeen(const std::vector<FormData>& forms, 131 const base::TimeTicks& timestamp, 132 autofill::FormsSeenState state); 133 134 // Processes the submitted |form|, saving any new Autofill data and uploading 135 // the possible field types for the submitted fields to the crowdsourcing 136 // server. Returns false if this form is not relevant for Autofill. 137 bool OnFormSubmitted(const FormData& form, 138 const base::TimeTicks& timestamp); 139 140 void OnTextFieldDidChange(const FormData& form, 141 const FormFieldData& field, 142 const base::TimeTicks& timestamp); 143 144 // The |bounding_box| is a window relative value. 145 void OnQueryFormFieldAutofill(int query_id, 146 const FormData& form, 147 const FormFieldData& field, 148 const gfx::RectF& bounding_box, 149 bool display_warning); 150 void OnDidEndTextFieldEditing(); 151 void OnHideAutofillUI(); 152 void OnAddPasswordFormMapping( 153 const FormFieldData& form, 154 const PasswordFormFillData& fill_data); 155 void OnShowPasswordSuggestions( 156 const FormFieldData& field, 157 const gfx::RectF& bounds, 158 const std::vector<base::string16>& suggestions, 159 const std::vector<base::string16>& realms); 160 void OnSetDataList(const std::vector<base::string16>& values, 161 const std::vector<base::string16>& labels); 162 163 // Requests an interactive autocomplete UI be shown. 164 void OnRequestAutocomplete(const FormData& form, 165 const GURL& frame_url); 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 // Tell the renderer the current interactive autocomplete finished. 214 virtual void ReturnAutocompleteResult( 215 WebKit::WebFormElement::AutocompleteResult result, 216 const FormData& form_data); 217 218 private: 219 220 // AutofillDownloadManager::Observer: 221 virtual void OnLoadedServerPredictions( 222 const std::string& response_xml) OVERRIDE; 223 224 // Passes return data for an OnRequestAutocomplete call back to the page. 225 void ReturnAutocompleteData(const FormStructure* result, 226 const std::string& unused_transaction_id); 227 228 // Fills |host| with the RenderViewHost for this tab. 229 // Returns false if Autofill is disabled or if the host is unavailable. 230 bool GetHost(content::RenderViewHost** host) const WARN_UNUSED_RESULT; 231 232 // Unpacks |unique_id| and fills |form_group| and |variant| with the 233 // appropriate data source and variant index. Returns false if the unpacked 234 // id cannot be found. 235 bool GetProfileOrCreditCard(int unique_id, 236 const AutofillDataModel** data_model, 237 size_t* variant) const WARN_UNUSED_RESULT; 238 239 // Fills |form_structure| cached element corresponding to |form|. 240 // Returns false if the cached element was not found. 241 bool FindCachedForm(const FormData& form, 242 FormStructure** form_structure) const WARN_UNUSED_RESULT; 243 244 // Fills |form_structure| and |autofill_field| with the cached elements 245 // corresponding to |form| and |field|. This might have the side-effect of 246 // updating the cache. Returns false if the |form| is not autofillable, or if 247 // it is not already present in the cache and the cache is full. 248 bool GetCachedFormAndField(const FormData& form, 249 const FormFieldData& field, 250 FormStructure** form_structure, 251 AutofillField** autofill_field) WARN_UNUSED_RESULT; 252 253 // Re-parses |live_form| and adds the result to |form_structures_|. 254 // |cached_form| should be a pointer to the existing version of the form, or 255 // NULL if no cached version exists. The updated form is then written into 256 // |updated_form|. Returns false if the cache could not be updated. 257 bool UpdateCachedForm(const FormData& live_form, 258 const FormStructure* cached_form, 259 FormStructure** updated_form) WARN_UNUSED_RESULT; 260 261 // Returns a list of values from the stored profiles that match |type| and the 262 // value of |field| and returns the labels of the matching profiles. |labels| 263 // is filled with the Profile label. 264 void GetProfileSuggestions(FormStructure* form, 265 const FormFieldData& field, 266 const AutofillType& type, 267 std::vector<base::string16>* values, 268 std::vector<base::string16>* labels, 269 std::vector<base::string16>* icons, 270 std::vector<int>* unique_ids) const; 271 272 // Returns a list of values from the stored credit cards that match |type| and 273 // the value of |field| and returns the labels of the matching credit cards. 274 void GetCreditCardSuggestions(const FormFieldData& field, 275 const AutofillType& type, 276 std::vector<base::string16>* values, 277 std::vector<base::string16>* labels, 278 std::vector<base::string16>* icons, 279 std::vector<int>* unique_ids) const; 280 281 // Parses the forms using heuristic matching and querying the Autofill server. 282 void ParseForms(const std::vector<FormData>& forms); 283 284 // Imports the form data, submitted by the user, into |personal_data_|. 285 void ImportFormData(const FormStructure& submitted_form); 286 287 // If |initial_interaction_timestamp_| is unset or is set to a later time than 288 // |interaction_timestamp|, updates the cached timestamp. The latter check is 289 // needed because IPC messages can arrive out of order. 290 void UpdateInitialInteractionTimestamp( 291 const base::TimeTicks& interaction_timestamp); 292 293 // Provides driver-level context to the shared code of the component. Must 294 // outlive this object. 295 AutofillDriver* driver_; 296 297 autofill::AutofillManagerDelegate* const manager_delegate_; 298 299 std::string app_locale_; 300 301 // The personal data manager, used to save and load personal data to/from the 302 // web database. This is overridden by the AutofillManagerTest. 303 // Weak reference. 304 // May be NULL. NULL indicates OTR. 305 PersonalDataManager* personal_data_; 306 307 std::list<std::string> autofilled_form_signatures_; 308 309 // Handles queries and uploads to Autofill servers. Will be NULL if 310 // the download manager functionality is disabled. 311 scoped_ptr<AutofillDownloadManager> download_manager_; 312 313 // Handles single-field autocomplete form data. 314 scoped_ptr<AutocompleteHistoryManager> autocomplete_history_manager_; 315 316 // For logging UMA metrics. Overridden by metrics tests. 317 scoped_ptr<const AutofillMetrics> metric_logger_; 318 // Have we logged whether Autofill is enabled for this page load? 319 bool has_logged_autofill_enabled_; 320 // Have we logged an address suggestions count metric for this page? 321 bool has_logged_address_suggestions_count_; 322 // Have we shown Autofill suggestions at least once? 323 bool did_show_suggestions_; 324 // Has the user manually edited at least one form field among the autofillable 325 // ones? 326 bool user_did_type_; 327 // Has the user autofilled a form on this page? 328 bool user_did_autofill_; 329 // Has the user edited a field that was previously autofilled? 330 bool user_did_edit_autofilled_field_; 331 // When the page finished loading. 332 base::TimeTicks forms_loaded_timestamp_; 333 // When the user first interacted with a potentially fillable form on this 334 // page. 335 base::TimeTicks initial_interaction_timestamp_; 336 337 // Our copy of the form data. 338 ScopedVector<FormStructure> form_structures_; 339 340 // GUID to ID mapping. We keep two maps to convert back and forth. 341 mutable std::map<PersonalDataManager::GUIDPair, int> guid_id_map_; 342 mutable std::map<int, PersonalDataManager::GUIDPair> id_guid_map_; 343 344 // Delegate to perform external processing (display, selection) on 345 // our behalf. Weak. 346 AutofillExternalDelegate* external_delegate_; 347 348 // Delegate used in test to get notifications on certain events. 349 autofill::AutofillManagerTestDelegate* test_delegate_; 350 351 base::WeakPtrFactory<AutofillManager> weak_ptr_factory_; 352 353 friend class AutofillManagerTest; 354 friend class autofill::FormStructureBrowserTest; 355 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 356 DeterminePossibleFieldTypesForUpload); 357 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 358 DeterminePossibleFieldTypesForUploadStressTest); 359 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 360 DisabledAutofillDispatchesError); 361 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AddressSuggestionsCount); 362 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, AutofillIsEnabledAtPageLoad); 363 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, DeveloperEngagement); 364 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, FormFillDuration); 365 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, 366 NoQualityMetricsForNonAutofillableForms); 367 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetrics); 368 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsForFailure); 369 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, QualityMetricsWithExperimentId); 370 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, SaneMetricsWithCacheMismatch); 371 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, TestExternalDelegate); 372 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 373 TestTabContentsWithExternalDelegate); 374 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, 375 UserHappinessFormLoadAndSubmission); 376 FRIEND_TEST_ALL_PREFIXES(AutofillMetricsTest, UserHappinessFormInteraction); 377 FRIEND_TEST_ALL_PREFIXES(AutofillManagerTest, 378 FormSubmittedAutocompleteEnabled); 379 DISALLOW_COPY_AND_ASSIGN(AutofillManager); 380}; 381 382} // namespace autofill 383 384#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_MANAGER_H_ 385