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