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