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