autofill_manager.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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#include "components/autofill/core/browser/autofill_manager.h" 6 7#include <stddef.h> 8 9#include <limits> 10#include <map> 11#include <set> 12#include <utility> 13 14#include "base/bind.h" 15#include "base/command_line.h" 16#include "base/guid.h" 17#include "base/logging.h" 18#include "base/prefs/pref_service.h" 19#include "base/strings/string16.h" 20#include "base/strings/string_util.h" 21#include "base/strings/utf_string_conversions.h" 22#include "base/threading/sequenced_worker_pool.h" 23#include "components/autofill/core/browser/autocomplete_history_manager.h" 24#include "components/autofill/core/browser/autofill_data_model.h" 25#include "components/autofill/core/browser/autofill_external_delegate.h" 26#include "components/autofill/core/browser/autofill_field.h" 27#include "components/autofill/core/browser/autofill_manager_delegate.h" 28#include "components/autofill/core/browser/autofill_manager_test_delegate.h" 29#include "components/autofill/core/browser/autofill_metrics.h" 30#include "components/autofill/core/browser/autofill_profile.h" 31#include "components/autofill/core/browser/autofill_type.h" 32#include "components/autofill/core/browser/credit_card.h" 33#include "components/autofill/core/browser/form_structure.h" 34#include "components/autofill/core/browser/personal_data_manager.h" 35#include "components/autofill/core/browser/phone_number.h" 36#include "components/autofill/core/browser/phone_number_i18n.h" 37#include "components/autofill/core/browser/popup_item_ids.h" 38#include "components/autofill/core/common/autofill_data_validation.h" 39#include "components/autofill/core/common/autofill_pref_names.h" 40#include "components/autofill/core/common/autofill_switches.h" 41#include "components/autofill/core/common/form_data.h" 42#include "components/autofill/core/common/form_data_predictions.h" 43#include "components/autofill/core/common/form_field_data.h" 44#include "components/autofill/core/common/password_form_fill_data.h" 45#include "components/pref_registry/pref_registry_syncable.h" 46#include "grit/components_strings.h" 47#include "ui/base/l10n/l10n_util.h" 48#include "ui/gfx/rect.h" 49#include "url/gurl.h" 50 51namespace autofill { 52 53typedef PersonalDataManager::GUIDPair GUIDPair; 54 55using base::TimeTicks; 56 57namespace { 58 59// We only send a fraction of the forms to upload server. 60// The rate for positive/negative matches potentially could be different. 61const double kAutofillPositiveUploadRateDefaultValue = 0.20; 62const double kAutofillNegativeUploadRateDefaultValue = 0.20; 63 64const size_t kMaxRecentFormSignaturesToRemember = 3; 65 66// Set a conservative upper bound on the number of forms we are willing to 67// cache, simply to prevent unbounded memory consumption. 68const size_t kMaxFormCacheSize = 100; 69 70// Removes duplicate suggestions whilst preserving their original order. 71void RemoveDuplicateSuggestions(std::vector<base::string16>* values, 72 std::vector<base::string16>* labels, 73 std::vector<base::string16>* icons, 74 std::vector<int>* unique_ids) { 75 DCHECK_EQ(values->size(), labels->size()); 76 DCHECK_EQ(values->size(), icons->size()); 77 DCHECK_EQ(values->size(), unique_ids->size()); 78 79 std::set<std::pair<base::string16, base::string16> > seen_suggestions; 80 std::vector<base::string16> values_copy; 81 std::vector<base::string16> labels_copy; 82 std::vector<base::string16> icons_copy; 83 std::vector<int> unique_ids_copy; 84 85 for (size_t i = 0; i < values->size(); ++i) { 86 const std::pair<base::string16, base::string16> suggestion( 87 (*values)[i], (*labels)[i]); 88 if (seen_suggestions.insert(suggestion).second) { 89 values_copy.push_back((*values)[i]); 90 labels_copy.push_back((*labels)[i]); 91 icons_copy.push_back((*icons)[i]); 92 unique_ids_copy.push_back((*unique_ids)[i]); 93 } 94 } 95 96 values->swap(values_copy); 97 labels->swap(labels_copy); 98 icons->swap(icons_copy); 99 unique_ids->swap(unique_ids_copy); 100} 101 102// Precondition: |form_structure| and |form| should correspond to the same 103// logical form. Returns true if any field in the given |section| within |form| 104// is auto-filled. 105bool SectionIsAutofilled(const FormStructure& form_structure, 106 const FormData& form, 107 const std::string& section) { 108 DCHECK_EQ(form_structure.field_count(), form.fields.size()); 109 for (size_t i = 0; i < form_structure.field_count(); ++i) { 110 if (form_structure.field(i)->section() == section && 111 form.fields[i].is_autofilled) { 112 return true; 113 } 114 } 115 116 return false; 117} 118 119bool FormIsHTTPS(const FormStructure& form) { 120 // TODO(blundell): Change this to use a constant once crbug.com/306258 is 121 // fixed. 122 return form.source_url().SchemeIs("https"); 123} 124 125// Uses the existing personal data in |profiles| and |credit_cards| to determine 126// possible field types for the |submitted_form|. This is potentially 127// expensive -- on the order of 50ms even for a small set of |stored_data|. 128// Hence, it should not run on the UI thread -- to avoid locking up the UI -- 129// nor on the IO thread -- to avoid blocking IPC calls. 130void DeterminePossibleFieldTypesForUpload( 131 const std::vector<AutofillProfile>& profiles, 132 const std::vector<CreditCard>& credit_cards, 133 const std::string& app_locale, 134 FormStructure* submitted_form) { 135 // For each field in the |submitted_form|, extract the value. Then for each 136 // profile or credit card, identify any stored types that match the value. 137 for (size_t i = 0; i < submitted_form->field_count(); ++i) { 138 AutofillField* field = submitted_form->field(i); 139 ServerFieldTypeSet matching_types; 140 141 // If it's a password field, set the type directly. 142 if (field->form_control_type == "password") { 143 matching_types.insert(autofill::PASSWORD); 144 } else { 145 base::string16 value; 146 base::TrimWhitespace(field->value, base::TRIM_ALL, &value); 147 for (std::vector<AutofillProfile>::const_iterator it = profiles.begin(); 148 it != profiles.end(); ++it) { 149 it->GetMatchingTypes(value, app_locale, &matching_types); 150 } 151 for (std::vector<CreditCard>::const_iterator it = credit_cards.begin(); 152 it != credit_cards.end(); ++it) { 153 it->GetMatchingTypes(value, app_locale, &matching_types); 154 } 155 } 156 157 if (matching_types.empty()) 158 matching_types.insert(UNKNOWN_TYPE); 159 160 field->set_possible_types(matching_types); 161 } 162} 163 164} // namespace 165 166AutofillManager::AutofillManager( 167 AutofillDriver* driver, 168 autofill::AutofillManagerDelegate* delegate, 169 const std::string& app_locale, 170 AutofillDownloadManagerState enable_download_manager) 171 : driver_(driver), 172 manager_delegate_(delegate), 173 app_locale_(app_locale), 174 personal_data_(delegate->GetPersonalDataManager()), 175 autocomplete_history_manager_( 176 new AutocompleteHistoryManager(driver, delegate)), 177 metric_logger_(new AutofillMetrics), 178 has_logged_autofill_enabled_(false), 179 has_logged_address_suggestions_count_(false), 180 did_show_suggestions_(false), 181 user_did_type_(false), 182 user_did_autofill_(false), 183 user_did_edit_autofilled_field_(false), 184 external_delegate_(NULL), 185 test_delegate_(NULL), 186 weak_ptr_factory_(this) { 187 if (enable_download_manager == ENABLE_AUTOFILL_DOWNLOAD_MANAGER) { 188 download_manager_.reset( 189 new AutofillDownloadManager(driver, 190 manager_delegate_->GetPrefs(), 191 this)); 192 } 193} 194 195AutofillManager::~AutofillManager() {} 196 197// static 198void AutofillManager::RegisterProfilePrefs( 199 user_prefs::PrefRegistrySyncable* registry) { 200 registry->RegisterBooleanPref( 201 prefs::kAutofillEnabled, 202 true, 203 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 204#if defined(OS_MACOSX) || defined(OS_ANDROID) 205 registry->RegisterBooleanPref( 206 prefs::kAutofillAuxiliaryProfilesEnabled, 207 true, 208 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); 209#else // defined(OS_MACOSX) || defined(OS_ANDROID) 210 registry->RegisterBooleanPref( 211 prefs::kAutofillAuxiliaryProfilesEnabled, 212 false, 213 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 214#endif // defined(OS_MACOSX) || defined(OS_ANDROID) 215#if defined(OS_MACOSX) 216 registry->RegisterBooleanPref( 217 prefs::kAutofillMacAddressBookQueried, 218 false, 219 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 220#endif // defined(OS_MACOSX) 221 registry->RegisterDoublePref( 222 prefs::kAutofillPositiveUploadRate, 223 kAutofillPositiveUploadRateDefaultValue, 224 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 225 registry->RegisterDoublePref( 226 prefs::kAutofillNegativeUploadRate, 227 kAutofillNegativeUploadRateDefaultValue, 228 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 229 230#if defined(OS_MACOSX) && !defined(OS_IOS) 231 registry->RegisterBooleanPref( 232 prefs::kAutofillUseMacAddressBook, 233 false, 234 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 235#endif // defined(OS_MACOSX) && !defined(OS_IOS) 236} 237 238#if defined(OS_MACOSX) && !defined(OS_IOS) 239void AutofillManager::MigrateUserPrefs(PrefService* prefs) { 240 const PrefService::Preference* pref = 241 prefs->FindPreference(prefs::kAutofillUseMacAddressBook); 242 243 // If the pref is not its default value, then the migration has already been 244 // performed. 245 if (!pref->IsDefaultValue()) 246 return; 247 248 // Whether Chrome has already tried to access the user's Address Book. 249 const PrefService::Preference* pref_accessed = 250 prefs->FindPreference(prefs::kAutofillMacAddressBookQueried); 251 // Whether the user wants to use the Address Book to populate Autofill. 252 const PrefService::Preference* pref_enabled = 253 prefs->FindPreference(prefs::kAutofillAuxiliaryProfilesEnabled); 254 255 if (pref_accessed->IsDefaultValue() && pref_enabled->IsDefaultValue()) { 256 // This is likely a new user. Reset the default value to prevent the 257 // migration from happening again. 258 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, 259 prefs->GetBoolean(prefs::kAutofillUseMacAddressBook)); 260 return; 261 } 262 263 bool accessed; 264 bool enabled; 265 bool success = pref_accessed->GetValue()->GetAsBoolean(&accessed); 266 DCHECK(success); 267 success = pref_enabled->GetValue()->GetAsBoolean(&enabled); 268 DCHECK(success); 269 270 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, accessed && enabled); 271} 272#endif // defined(OS_MACOSX) && !defined(OS_IOS) 273 274void AutofillManager::SetExternalDelegate(AutofillExternalDelegate* delegate) { 275 // TODO(jrg): consider passing delegate into the ctor. That won't 276 // work if the delegate has a pointer to the AutofillManager, but 277 // future directions may not need such a pointer. 278 external_delegate_ = delegate; 279 autocomplete_history_manager_->SetExternalDelegate(delegate); 280} 281 282void AutofillManager::ShowAutofillSettings() { 283 manager_delegate_->ShowAutofillSettings(); 284} 285 286#if defined(OS_MACOSX) && !defined(OS_IOS) 287bool AutofillManager::ShouldShowAccessAddressBookSuggestion( 288 const FormData& form, 289 const FormFieldData& field) { 290 if (!personal_data_) 291 return false; 292 FormStructure* form_structure = NULL; 293 AutofillField* autofill_field = NULL; 294 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field)) 295 return false; 296 297 return personal_data_->ShouldShowAccessAddressBookSuggestion( 298 autofill_field->Type()); 299} 300 301bool AutofillManager::AccessAddressBook() { 302 if (!personal_data_) 303 return false; 304 return personal_data_->AccessAddressBook(); 305} 306#endif // defined(OS_MACOSX) && !defined(OS_IOS) 307 308bool AutofillManager::OnFormSubmitted(const FormData& form, 309 const TimeTicks& timestamp) { 310 if (!IsValidFormData(form)) 311 return false; 312 313 // Let Autocomplete know as well. 314 autocomplete_history_manager_->OnFormSubmitted(form); 315 316 // Grab a copy of the form data. 317 scoped_ptr<FormStructure> submitted_form(new FormStructure(form)); 318 319 if (!ShouldUploadForm(*submitted_form)) 320 return false; 321 322 // Don't save data that was submitted through JavaScript. 323 if (!form.user_submitted) 324 return false; 325 326 // Ignore forms not present in our cache. These are typically forms with 327 // wonky JavaScript that also makes them not auto-fillable. 328 FormStructure* cached_submitted_form; 329 if (!FindCachedForm(form, &cached_submitted_form)) 330 return false; 331 332 submitted_form->UpdateFromCache(*cached_submitted_form); 333 if (submitted_form->IsAutofillable(true)) 334 ImportFormData(*submitted_form); 335 336 // Only upload server statistics and UMA metrics if at least some local data 337 // is available to use as a baseline. 338 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); 339 const std::vector<CreditCard*>& credit_cards = 340 personal_data_->GetCreditCards(); 341 if (!profiles.empty() || !credit_cards.empty()) { 342 // Copy the profile and credit card data, so that it can be accessed on a 343 // separate thread. 344 std::vector<AutofillProfile> copied_profiles; 345 copied_profiles.reserve(profiles.size()); 346 for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin(); 347 it != profiles.end(); ++it) { 348 copied_profiles.push_back(**it); 349 } 350 351 std::vector<CreditCard> copied_credit_cards; 352 copied_credit_cards.reserve(credit_cards.size()); 353 for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin(); 354 it != credit_cards.end(); ++it) { 355 copied_credit_cards.push_back(**it); 356 } 357 358 // Note that ownership of |submitted_form| is passed to the second task, 359 // using |base::Owned|. 360 FormStructure* raw_submitted_form = submitted_form.get(); 361 driver_->GetBlockingPool()->PostTaskAndReply( 362 FROM_HERE, 363 base::Bind(&DeterminePossibleFieldTypesForUpload, 364 copied_profiles, 365 copied_credit_cards, 366 app_locale_, 367 raw_submitted_form), 368 base::Bind(&AutofillManager::UploadFormDataAsyncCallback, 369 weak_ptr_factory_.GetWeakPtr(), 370 base::Owned(submitted_form.release()), 371 forms_loaded_timestamps_[form], 372 initial_interaction_timestamp_, 373 timestamp)); 374 } 375 376 return true; 377} 378 379void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms, 380 const TimeTicks& timestamp) { 381 if (!IsValidFormDataVector(forms)) 382 return; 383 384 if (!driver_->RendererIsAvailable()) 385 return; 386 387 bool enabled = IsAutofillEnabled(); 388 if (!has_logged_autofill_enabled_) { 389 metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled); 390 has_logged_autofill_enabled_ = true; 391 } 392 393 if (!enabled) 394 return; 395 396 for (size_t i = 0; i < forms.size(); ++i) { 397 forms_loaded_timestamps_[forms[i]] = timestamp; 398 } 399 400 ParseForms(forms); 401} 402 403void AutofillManager::OnTextFieldDidChange(const FormData& form, 404 const FormFieldData& field, 405 const TimeTicks& timestamp) { 406 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 407 return; 408 409 FormStructure* form_structure = NULL; 410 AutofillField* autofill_field = NULL; 411 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field)) 412 return; 413 414 if (!user_did_type_) { 415 user_did_type_ = true; 416 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE); 417 } 418 419 if (autofill_field->is_autofilled) { 420 autofill_field->is_autofilled = false; 421 metric_logger_->LogUserHappinessMetric( 422 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD); 423 424 if (!user_did_edit_autofilled_field_) { 425 user_did_edit_autofilled_field_ = true; 426 metric_logger_->LogUserHappinessMetric( 427 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE); 428 } 429 } 430 431 UpdateInitialInteractionTimestamp(timestamp); 432} 433 434void AutofillManager::OnQueryFormFieldAutofill(int query_id, 435 const FormData& form, 436 const FormFieldData& field, 437 const gfx::RectF& bounding_box, 438 bool display_warning) { 439 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 440 return; 441 442 std::vector<base::string16> values; 443 std::vector<base::string16> labels; 444 std::vector<base::string16> icons; 445 std::vector<int> unique_ids; 446 447 external_delegate_->OnQuery(query_id, 448 form, 449 field, 450 bounding_box, 451 display_warning); 452 FormStructure* form_structure = NULL; 453 AutofillField* autofill_field = NULL; 454 if (RefreshDataModels() && 455 driver_->RendererIsAvailable() && 456 GetCachedFormAndField(form, field, &form_structure, &autofill_field) && 457 // Don't send suggestions for forms that aren't auto-fillable. 458 form_structure->IsAutofillable(false)) { 459 AutofillType type = autofill_field->Type(); 460 bool is_filling_credit_card = (type.group() == CREDIT_CARD); 461 if (is_filling_credit_card) { 462 GetCreditCardSuggestions( 463 field, type, &values, &labels, &icons, &unique_ids); 464 } else { 465 GetProfileSuggestions( 466 form_structure, field, type, &values, &labels, &icons, &unique_ids); 467 } 468 469 DCHECK_EQ(values.size(), labels.size()); 470 DCHECK_EQ(values.size(), icons.size()); 471 DCHECK_EQ(values.size(), unique_ids.size()); 472 473 if (!values.empty()) { 474 // Don't provide Autofill suggestions when Autofill is disabled, and don't 475 // provide credit card suggestions for non-HTTPS pages. However, provide a 476 // warning to the user in these cases. 477 int warning = 0; 478 if (!form_structure->IsAutofillable(true)) 479 warning = IDS_AUTOFILL_WARNING_FORM_DISABLED; 480 else if (is_filling_credit_card && !FormIsHTTPS(*form_structure)) 481 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION; 482 if (warning) { 483 values.assign(1, l10n_util::GetStringUTF16(warning)); 484 labels.assign(1, base::string16()); 485 icons.assign(1, base::string16()); 486 unique_ids.assign(1, POPUP_ITEM_ID_WARNING_MESSAGE); 487 } else { 488 bool section_is_autofilled = 489 SectionIsAutofilled(*form_structure, form, 490 autofill_field->section()); 491 if (section_is_autofilled) { 492 // If the relevant section is auto-filled and the renderer is querying 493 // for suggestions, then the user is editing the value of a field. 494 // In this case, mimic autocomplete: don't display labels or icons, 495 // as that information is redundant. 496 labels.assign(labels.size(), base::string16()); 497 icons.assign(icons.size(), base::string16()); 498 } 499 500 // When filling credit card suggestions, the values and labels are 501 // typically obfuscated, which makes detecting duplicates hard. Since 502 // duplicates only tend to be a problem when filling address forms 503 // anyway, only don't de-dup credit card suggestions. 504 if (!is_filling_credit_card) 505 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); 506 507 // The first time we show suggestions on this page, log the number of 508 // suggestions shown. 509 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) { 510 metric_logger_->LogAddressSuggestionsCount(values.size()); 511 has_logged_address_suggestions_count_ = true; 512 } 513 } 514 } 515 } 516 517 // Add the results from AutoComplete. They come back asynchronously, so we 518 // hand off what we generated and they will send the results back to the 519 // renderer. 520 autocomplete_history_manager_->OnGetAutocompleteSuggestions( 521 query_id, field.name, field.value, field.form_control_type, values, 522 labels, icons, unique_ids); 523} 524 525void AutofillManager::FillOrPreviewForm( 526 AutofillDriver::RendererFormDataAction action, 527 int query_id, 528 const FormData& form, 529 const FormFieldData& field, 530 int unique_id) { 531 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 532 return; 533 534 const AutofillDataModel* data_model = NULL; 535 size_t variant = 0; 536 FormStructure* form_structure = NULL; 537 AutofillField* autofill_field = NULL; 538 bool is_credit_card = false; 539 // NOTE: RefreshDataModels may invalidate |data_model| because it causes the 540 // PersonalDataManager to reload Mac address book entries. Thus it must come 541 // before GetProfileOrCreditCard. 542 if (!RefreshDataModels() || 543 !driver_->RendererIsAvailable() || 544 !GetProfileOrCreditCard( 545 unique_id, &data_model, &variant, &is_credit_card) || 546 !GetCachedFormAndField(form, field, &form_structure, &autofill_field)) 547 return; 548 549 DCHECK(form_structure); 550 DCHECK(autofill_field); 551 552 FormData result = form; 553 554 base::string16 profile_full_name; 555 if (!is_credit_card) { 556 profile_full_name = data_model->GetInfo( 557 AutofillType(NAME_FULL), app_locale_); 558 } 559 560 // If the relevant section is auto-filled, we should fill |field| but not the 561 // rest of the form. 562 if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) { 563 for (std::vector<FormFieldData>::iterator iter = result.fields.begin(); 564 iter != result.fields.end(); ++iter) { 565 if ((*iter) == field) { 566 base::string16 value = data_model->GetInfoForVariant( 567 autofill_field->Type(), variant, app_locale_); 568 if (AutofillField::FillFormField( 569 *autofill_field, value, app_locale_, &(*iter))) { 570 // Mark the cached field as autofilled, so that we can detect when a 571 // user edits an autofilled field (for metrics). 572 autofill_field->is_autofilled = true; 573 574 if (!is_credit_card && !value.empty()) 575 manager_delegate_->DidFillOrPreviewField(value, profile_full_name); 576 } 577 break; 578 } 579 } 580 581 driver_->SendFormDataToRenderer(query_id, action, result); 582 return; 583 } 584 585 // Cache the field type for the field from which the user initiated autofill. 586 FieldTypeGroup initiating_group_type = autofill_field->Type().group(); 587 DCHECK_EQ(form_structure->field_count(), form.fields.size()); 588 for (size_t i = 0; i < form_structure->field_count(); ++i) { 589 if (form_structure->field(i)->section() != autofill_field->section()) 590 continue; 591 592 DCHECK_EQ(*form_structure->field(i), result.fields[i]); 593 594 const AutofillField* cached_field = form_structure->field(i); 595 FieldTypeGroup field_group_type = cached_field->Type().group(); 596 if (field_group_type != NO_GROUP) { 597 // If the field being filled is either 598 // (a) the field that the user initiated the fill from, or 599 // (b) part of the same logical unit, e.g. name or phone number, 600 // then take the multi-profile "variant" into account. 601 // Otherwise fill with the default (zeroth) variant. 602 size_t use_variant = 0; 603 if (result.fields[i] == field || 604 field_group_type == initiating_group_type) { 605 use_variant = variant; 606 } 607 base::string16 value = data_model->GetInfoForVariant( 608 cached_field->Type(), use_variant, app_locale_); 609 610 // Must match ForEachMatchingFormField() in form_autofill_util.cc. 611 // Only notify autofilling of empty fields and the field that initiated 612 // the filling (note that "select-one" controls may not be empty but will 613 // still be autofilled). 614 bool should_notify = 615 !is_credit_card && 616 !value.empty() && 617 (result.fields[i] == field || 618 result.fields[i].form_control_type == "select-one" || 619 result.fields[i].value.empty()); 620 if (AutofillField::FillFormField( 621 *cached_field, value, app_locale_, &result.fields[i])) { 622 // Mark the cached field as autofilled, so that we can detect when a 623 // user edits an autofilled field (for metrics). 624 form_structure->field(i)->is_autofilled = true; 625 626 // Mark the field as autofilled when a non-empty value is assigned to 627 // it. This allows the renderer to distinguish autofilled fields from 628 // fields with non-empty values, such as select-one fields. 629 result.fields[i].is_autofilled = true; 630 631 if (should_notify) 632 manager_delegate_->DidFillOrPreviewField(value, profile_full_name); 633 } 634 } 635 } 636 637 autofilled_form_signatures_.push_front(form_structure->FormSignature()); 638 // Only remember the last few forms that we've seen, both to avoid false 639 // positives and to avoid wasting memory. 640 if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember) 641 autofilled_form_signatures_.pop_back(); 642 643 driver_->SendFormDataToRenderer(query_id, action, result); 644} 645 646void AutofillManager::OnDidPreviewAutofillFormData() { 647 if (test_delegate_) 648 test_delegate_->DidPreviewFormData(); 649} 650 651void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) { 652 if (test_delegate_) 653 test_delegate_->DidFillFormData(); 654 655 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL); 656 if (!user_did_autofill_) { 657 user_did_autofill_ = true; 658 metric_logger_->LogUserHappinessMetric( 659 AutofillMetrics::USER_DID_AUTOFILL_ONCE); 660 } 661 662 UpdateInitialInteractionTimestamp(timestamp); 663} 664 665void AutofillManager::DidShowSuggestions(bool is_new_popup) { 666 if (test_delegate_) 667 test_delegate_->DidShowSuggestions(); 668 669 if (is_new_popup) { 670 metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN); 671 672 if (!did_show_suggestions_) { 673 did_show_suggestions_ = true; 674 metric_logger_->LogUserHappinessMetric( 675 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE); 676 } 677 } 678} 679 680void AutofillManager::OnHidePopup() { 681 if (!IsAutofillEnabled()) 682 return; 683 684 manager_delegate_->HideAutofillPopup(); 685} 686 687void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) { 688 const AutofillDataModel* data_model = NULL; 689 size_t variant = 0; 690 bool unused_is_credit_card = false; 691 if (!GetProfileOrCreditCard( 692 unique_id, &data_model, &variant, &unused_is_credit_card)) { 693 NOTREACHED(); 694 return; 695 } 696 697 // TODO(csharp): If we are dealing with a variant only the variant should 698 // be deleted, instead of doing nothing. 699 // http://crbug.com/124211 700 if (variant != 0) 701 return; 702 703 personal_data_->RemoveByGUID(data_model->guid()); 704} 705 706void AutofillManager::RemoveAutocompleteEntry(const base::string16& name, 707 const base::string16& value) { 708 autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value); 709} 710 711const std::vector<FormStructure*>& AutofillManager::GetFormStructures() { 712 return form_structures_.get(); 713} 714 715void AutofillManager::SetTestDelegate( 716 autofill::AutofillManagerTestDelegate* delegate) { 717 test_delegate_ = delegate; 718} 719 720void AutofillManager::OnSetDataList(const std::vector<base::string16>& values, 721 const std::vector<base::string16>& labels) { 722 if (!IsValidString16Vector(values) || 723 !IsValidString16Vector(labels) || 724 values.size() != labels.size()) 725 return; 726 727 external_delegate_->SetCurrentDataListValues(values, labels); 728} 729 730void AutofillManager::OnLoadedServerPredictions( 731 const std::string& response_xml) { 732 // Parse and store the server predictions. 733 FormStructure::ParseQueryResponse(response_xml, 734 form_structures_.get(), 735 *metric_logger_); 736 737 // Forward form structures to the password generation manager to detect 738 // account creation forms. 739 manager_delegate_->DetectAccountCreationForms(form_structures_.get()); 740 741 // If the corresponding flag is set, annotate forms with the predicted types. 742 driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get()); 743} 744 745void AutofillManager::OnDidEndTextFieldEditing() { 746 external_delegate_->DidEndTextFieldEditing(); 747} 748 749bool AutofillManager::IsAutofillEnabled() const { 750 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); 751} 752 753void AutofillManager::ImportFormData(const FormStructure& submitted_form) { 754 scoped_ptr<CreditCard> imported_credit_card; 755 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card)) 756 return; 757 758 // If credit card information was submitted, we need to confirm whether to 759 // save it. 760 if (imported_credit_card) { 761 manager_delegate_->ConfirmSaveCreditCard( 762 *metric_logger_, 763 base::Bind( 764 base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard), 765 base::Unretained(personal_data_), *imported_credit_card)); 766 } 767} 768 769// Note that |submitted_form| is passed as a pointer rather than as a reference 770// so that we can get memory management right across threads. Note also that we 771// explicitly pass in all the time stamps of interest, as the cached ones might 772// get reset before this method executes. 773void AutofillManager::UploadFormDataAsyncCallback( 774 const FormStructure* submitted_form, 775 const TimeTicks& load_time, 776 const TimeTicks& interaction_time, 777 const TimeTicks& submission_time) { 778 submitted_form->LogQualityMetrics(*metric_logger_, 779 load_time, 780 interaction_time, 781 submission_time); 782 783 if (submitted_form->ShouldBeCrowdsourced()) 784 UploadFormData(*submitted_form); 785} 786 787void AutofillManager::UploadFormData(const FormStructure& submitted_form) { 788 if (!download_manager_) 789 return; 790 791 // Check if the form is among the forms that were recently auto-filled. 792 bool was_autofilled = false; 793 std::string form_signature = submitted_form.FormSignature(); 794 for (std::list<std::string>::const_iterator it = 795 autofilled_form_signatures_.begin(); 796 it != autofilled_form_signatures_.end() && !was_autofilled; 797 ++it) { 798 if (*it == form_signature) 799 was_autofilled = true; 800 } 801 802 ServerFieldTypeSet non_empty_types; 803 personal_data_->GetNonEmptyTypes(&non_empty_types); 804 // Always add PASSWORD to |non_empty_types| so that if |submitted_form| 805 // contains a password field it will be uploaded to the server. If 806 // |submitted_form| doesn't contain a password field, there is no side 807 // effect from adding PASSWORD to |non_empty_types|. 808 non_empty_types.insert(autofill::PASSWORD); 809 810 download_manager_->StartUploadRequest(submitted_form, was_autofilled, 811 non_empty_types); 812} 813 814bool AutofillManager::UploadPasswordGenerationForm(const FormData& form) { 815 FormStructure form_structure(form); 816 817 if (!ShouldUploadForm(form_structure)) 818 return false; 819 820 if (!form_structure.ShouldBeCrowdsourced()) 821 return false; 822 823 // TODO(gcasto): Check that PasswordGeneration is enabled? 824 825 // Find the first password field to label. We don't try to label anything 826 // else. 827 bool found_password_field = false; 828 for (size_t i = 0; i < form_structure.field_count(); ++i) { 829 AutofillField* field = form_structure.field(i); 830 831 ServerFieldTypeSet types; 832 if (!found_password_field && field->form_control_type == "password") { 833 types.insert(ACCOUNT_CREATION_PASSWORD); 834 found_password_field = true; 835 } else { 836 types.insert(UNKNOWN_TYPE); 837 } 838 field->set_possible_types(types); 839 } 840 DCHECK(found_password_field); 841 842 // Only one field type should be present. 843 ServerFieldTypeSet available_field_types; 844 available_field_types.insert(ACCOUNT_CREATION_PASSWORD); 845 846 // Force uploading as these events are relatively rare and we want to make 847 // sure to receive them. It also makes testing easier if these requests 848 // always pass. 849 form_structure.set_upload_required(UPLOAD_REQUIRED); 850 851 if (!download_manager_) 852 return false; 853 854 return download_manager_->StartUploadRequest(form_structure, 855 false /* was_autofilled */, 856 available_field_types); 857} 858 859void AutofillManager::Reset() { 860 form_structures_.clear(); 861 has_logged_autofill_enabled_ = false; 862 has_logged_address_suggestions_count_ = false; 863 did_show_suggestions_ = false; 864 user_did_type_ = false; 865 user_did_autofill_ = false; 866 user_did_edit_autofilled_field_ = false; 867 forms_loaded_timestamps_.clear(); 868 initial_interaction_timestamp_ = TimeTicks(); 869 external_delegate_->Reset(); 870} 871 872AutofillManager::AutofillManager(AutofillDriver* driver, 873 autofill::AutofillManagerDelegate* delegate, 874 PersonalDataManager* personal_data) 875 : driver_(driver), 876 manager_delegate_(delegate), 877 app_locale_("en-US"), 878 personal_data_(personal_data), 879 autocomplete_history_manager_( 880 new AutocompleteHistoryManager(driver, delegate)), 881 metric_logger_(new AutofillMetrics), 882 has_logged_autofill_enabled_(false), 883 has_logged_address_suggestions_count_(false), 884 did_show_suggestions_(false), 885 user_did_type_(false), 886 user_did_autofill_(false), 887 user_did_edit_autofilled_field_(false), 888 external_delegate_(NULL), 889 test_delegate_(NULL), 890 weak_ptr_factory_(this) { 891 DCHECK(driver_); 892 DCHECK(manager_delegate_); 893} 894 895void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { 896 metric_logger_.reset(metric_logger); 897} 898 899bool AutofillManager::RefreshDataModels() const { 900 if (!IsAutofillEnabled()) 901 return false; 902 903 // No autofill data to return if the profiles are empty. 904 if (personal_data_->GetProfiles().empty() && 905 personal_data_->GetCreditCards().empty()) { 906 return false; 907 } 908 909 return true; 910} 911 912bool AutofillManager::GetProfileOrCreditCard( 913 int unique_id, 914 const AutofillDataModel** data_model, 915 size_t* variant, 916 bool* is_credit_card) const { 917 // Unpack the |unique_id| into component parts. 918 GUIDPair credit_card_guid; 919 GUIDPair profile_guid; 920 UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid); 921 DCHECK(!base::IsValidGUID(credit_card_guid.first) || 922 !base::IsValidGUID(profile_guid.first)); 923 *is_credit_card = false; 924 925 // Find the profile that matches the |profile_guid|, if one is specified. 926 // Otherwise find the credit card that matches the |credit_card_guid|, 927 // if specified. 928 if (base::IsValidGUID(profile_guid.first)) { 929 *data_model = personal_data_->GetProfileByGUID(profile_guid.first); 930 *variant = profile_guid.second; 931 } else if (base::IsValidGUID(credit_card_guid.first)) { 932 *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first); 933 *variant = credit_card_guid.second; 934 *is_credit_card = true; 935 } 936 937 return !!*data_model; 938} 939 940bool AutofillManager::FindCachedForm(const FormData& form, 941 FormStructure** form_structure) const { 942 // Find the FormStructure that corresponds to |form|. 943 // Scan backward through the cached |form_structures_|, as updated versions of 944 // forms are added to the back of the list, whereas original versions of these 945 // forms might appear toward the beginning of the list. The communication 946 // protocol with the crowdsourcing server does not permit us to discard the 947 // original versions of the forms. 948 *form_structure = NULL; 949 for (std::vector<FormStructure*>::const_reverse_iterator iter = 950 form_structures_.rbegin(); 951 iter != form_structures_.rend(); ++iter) { 952 if (**iter == form) { 953 *form_structure = *iter; 954 955 // The same form might be cached with multiple field counts: in some 956 // cases, non-autofillable fields are filtered out, whereas in other cases 957 // they are not. To avoid thrashing the cache, keep scanning until we 958 // find a cached version with the same number of fields, if there is one. 959 if ((*iter)->field_count() == form.fields.size()) 960 break; 961 } 962 } 963 964 if (!(*form_structure)) 965 return false; 966 967 return true; 968} 969 970bool AutofillManager::GetCachedFormAndField(const FormData& form, 971 const FormFieldData& field, 972 FormStructure** form_structure, 973 AutofillField** autofill_field) { 974 // Find the FormStructure that corresponds to |form|. 975 // If we do not have this form in our cache but it is parseable, we'll add it 976 // in the call to |UpdateCachedForm()|. 977 if (!FindCachedForm(form, form_structure) && 978 !FormStructure(form).ShouldBeParsed(false)) { 979 return false; 980 } 981 982 // Update the cached form to reflect any dynamic changes to the form data, if 983 // necessary. 984 if (!UpdateCachedForm(form, *form_structure, form_structure)) 985 return false; 986 987 // No data to return if there are no auto-fillable fields. 988 if (!(*form_structure)->autofill_count()) 989 return false; 990 991 // Find the AutofillField that corresponds to |field|. 992 *autofill_field = NULL; 993 for (std::vector<AutofillField*>::const_iterator iter = 994 (*form_structure)->begin(); 995 iter != (*form_structure)->end(); ++iter) { 996 if ((**iter) == field) { 997 *autofill_field = *iter; 998 break; 999 } 1000 } 1001 1002 // Even though we always update the cache, the field might not exist if the 1003 // website disables autocomplete while the user is interacting with the form. 1004 // See http://crbug.com/160476 1005 return *autofill_field != NULL; 1006} 1007 1008bool AutofillManager::UpdateCachedForm(const FormData& live_form, 1009 const FormStructure* cached_form, 1010 FormStructure** updated_form) { 1011 bool needs_update = 1012 (!cached_form || 1013 live_form.fields.size() != cached_form->field_count()); 1014 for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) { 1015 needs_update = *cached_form->field(i) != live_form.fields[i]; 1016 } 1017 1018 if (!needs_update) 1019 return true; 1020 1021 if (form_structures_.size() >= kMaxFormCacheSize) 1022 return false; 1023 1024 // Add the new or updated form to our cache. 1025 form_structures_.push_back(new FormStructure(live_form)); 1026 *updated_form = *form_structures_.rbegin(); 1027 (*updated_form)->DetermineHeuristicTypes(*metric_logger_); 1028 1029 // If we have cached data, propagate it to the updated form. 1030 if (cached_form) { 1031 std::map<base::string16, const AutofillField*> cached_fields; 1032 for (size_t i = 0; i < cached_form->field_count(); ++i) { 1033 const AutofillField* field = cached_form->field(i); 1034 cached_fields[field->unique_name()] = field; 1035 } 1036 1037 for (size_t i = 0; i < (*updated_form)->field_count(); ++i) { 1038 AutofillField* field = (*updated_form)->field(i); 1039 std::map<base::string16, const AutofillField*>::iterator cached_field = 1040 cached_fields.find(field->unique_name()); 1041 if (cached_field != cached_fields.end()) { 1042 field->set_server_type(cached_field->second->server_type()); 1043 field->is_autofilled = cached_field->second->is_autofilled; 1044 } 1045 } 1046 1047 // Note: We _must not_ remove the original version of the cached form from 1048 // the list of |form_structures_|. Otherwise, we break parsing of the 1049 // crowdsourcing server's response to our query. 1050 } 1051 1052 // Annotate the updated form with its predicted types. 1053 std::vector<FormStructure*> forms(1, *updated_form); 1054 driver_->SendAutofillTypePredictionsToRenderer(forms); 1055 1056 return true; 1057} 1058 1059void AutofillManager::GetProfileSuggestions( 1060 FormStructure* form, 1061 const FormFieldData& field, 1062 const AutofillType& type, 1063 std::vector<base::string16>* values, 1064 std::vector<base::string16>* labels, 1065 std::vector<base::string16>* icons, 1066 std::vector<int>* unique_ids) const { 1067 std::vector<ServerFieldType> field_types(form->field_count()); 1068 for (size_t i = 0; i < form->field_count(); ++i) { 1069 field_types.push_back(form->field(i)->Type().GetStorableType()); 1070 } 1071 std::vector<GUIDPair> guid_pairs; 1072 1073 personal_data_->GetProfileSuggestions( 1074 type, field.value, field.is_autofilled, field_types, 1075 base::Callback<bool(const AutofillProfile&)>(), 1076 values, labels, icons, &guid_pairs); 1077 1078 for (size_t i = 0; i < guid_pairs.size(); ++i) { 1079 unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0), 1080 guid_pairs[i])); 1081 } 1082} 1083 1084void AutofillManager::GetCreditCardSuggestions( 1085 const FormFieldData& field, 1086 const AutofillType& type, 1087 std::vector<base::string16>* values, 1088 std::vector<base::string16>* labels, 1089 std::vector<base::string16>* icons, 1090 std::vector<int>* unique_ids) const { 1091 std::vector<GUIDPair> guid_pairs; 1092 personal_data_->GetCreditCardSuggestions( 1093 type, field.value, values, labels, icons, &guid_pairs); 1094 1095 for (size_t i = 0; i < guid_pairs.size(); ++i) { 1096 unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0))); 1097 } 1098} 1099 1100void AutofillManager::ParseForms(const std::vector<FormData>& forms) { 1101 std::vector<FormStructure*> non_queryable_forms; 1102 for (std::vector<FormData>::const_iterator iter = forms.begin(); 1103 iter != forms.end(); ++iter) { 1104 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); 1105 if (!form_structure->ShouldBeParsed(false)) 1106 continue; 1107 1108 form_structure->DetermineHeuristicTypes(*metric_logger_); 1109 1110 // Set aside forms with method GET or author-specified types, so that they 1111 // are not included in the query to the server. 1112 if (form_structure->ShouldBeCrowdsourced()) 1113 form_structures_.push_back(form_structure.release()); 1114 else 1115 non_queryable_forms.push_back(form_structure.release()); 1116 } 1117 1118 if (!form_structures_.empty() && download_manager_) { 1119 // Query the server if we have at least one of the forms were parsed. 1120 download_manager_->StartQueryRequest(form_structures_.get(), 1121 *metric_logger_); 1122 } 1123 1124 for (std::vector<FormStructure*>::const_iterator iter = 1125 non_queryable_forms.begin(); 1126 iter != non_queryable_forms.end(); ++iter) { 1127 form_structures_.push_back(*iter); 1128 } 1129 1130 if (!form_structures_.empty()) 1131 metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED); 1132 1133 // For the |non_queryable_forms|, we have all the field type info we're ever 1134 // going to get about them. For the other forms, we'll wait until we get a 1135 // response from the server. 1136 driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms); 1137} 1138 1139int AutofillManager::GUIDToID(const GUIDPair& guid) const { 1140 if (!base::IsValidGUID(guid.first)) 1141 return 0; 1142 1143 std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid); 1144 if (iter == guid_id_map_.end()) { 1145 int id = guid_id_map_.size() + 1; 1146 guid_id_map_[guid] = id; 1147 id_guid_map_[id] = guid; 1148 return id; 1149 } else { 1150 return iter->second; 1151 } 1152} 1153 1154const GUIDPair AutofillManager::IDToGUID(int id) const { 1155 if (id == 0) 1156 return GUIDPair(std::string(), 0); 1157 1158 std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id); 1159 if (iter == id_guid_map_.end()) { 1160 NOTREACHED(); 1161 return GUIDPair(std::string(), 0); 1162 } 1163 1164 return iter->second; 1165} 1166 1167// When sending IDs (across processes) to the renderer we pack credit card and 1168// profile IDs into a single integer. Credit card IDs are sent in the high 1169// word and profile IDs are sent in the low word. 1170int AutofillManager::PackGUIDs(const GUIDPair& cc_guid, 1171 const GUIDPair& profile_guid) const { 1172 int cc_id = GUIDToID(cc_guid); 1173 int profile_id = GUIDToID(profile_guid); 1174 1175 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); 1176 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); 1177 1178 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; 1179} 1180 1181// When receiving IDs (across processes) from the renderer we unpack credit card 1182// and profile IDs from a single integer. Credit card IDs are stored in the 1183// high word and profile IDs are stored in the low word. 1184void AutofillManager::UnpackGUIDs(int id, 1185 GUIDPair* cc_guid, 1186 GUIDPair* profile_guid) const { 1187 int cc_id = id >> std::numeric_limits<unsigned short>::digits & 1188 std::numeric_limits<unsigned short>::max(); 1189 int profile_id = id & std::numeric_limits<unsigned short>::max(); 1190 1191 *cc_guid = IDToGUID(cc_id); 1192 *profile_guid = IDToGUID(profile_id); 1193} 1194 1195void AutofillManager::UpdateInitialInteractionTimestamp( 1196 const TimeTicks& interaction_timestamp) { 1197 if (initial_interaction_timestamp_.is_null() || 1198 interaction_timestamp < initial_interaction_timestamp_) { 1199 initial_interaction_timestamp_ = interaction_timestamp; 1200 } 1201} 1202 1203bool AutofillManager::ShouldUploadForm(const FormStructure& form) { 1204 if (!IsAutofillEnabled()) 1205 return false; 1206 1207 if (driver_->IsOffTheRecord()) 1208 return false; 1209 1210 // Disregard forms that we wouldn't ever autofill in the first place. 1211 if (!form.ShouldBeParsed(true)) 1212 return false; 1213 1214 return true; 1215} 1216 1217} // namespace autofill 1218