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