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