autofill_manager.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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_data_validation.h" 39#include "components/autofill/core/common/autofill_pref_names.h" 40#include "components/autofill/core/common/autofill_switches.h" 41#include "components/autofill/core/common/form_data.h" 42#include "components/autofill/core/common/form_data_predictions.h" 43#include "components/autofill/core/common/form_field_data.h" 44#include "components/autofill/core/common/password_form_fill_data.h" 45#include "components/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 234void AutofillManager::ShowAutofillSettings() { 235 manager_delegate_->ShowAutofillSettings(); 236} 237 238bool AutofillManager::OnFormSubmitted(const FormData& form, 239 const TimeTicks& timestamp) { 240 if (!IsValidFormData(form)) 241 return false; 242 243 // Let Autocomplete know as well. 244 autocomplete_history_manager_->OnFormSubmitted(form); 245 246 // Grab a copy of the form data. 247 scoped_ptr<FormStructure> submitted_form(new FormStructure(form)); 248 249 if (!ShouldUploadForm(*submitted_form)) 250 return false; 251 252 // Don't save data that was submitted through JavaScript. 253 if (!form.user_submitted) 254 return false; 255 256 // Ignore forms not present in our cache. These are typically forms with 257 // wonky JavaScript that also makes them not auto-fillable. 258 FormStructure* cached_submitted_form; 259 if (!FindCachedForm(form, &cached_submitted_form)) 260 return false; 261 262 submitted_form->UpdateFromCache(*cached_submitted_form); 263 if (submitted_form->IsAutofillable(true)) 264 ImportFormData(*submitted_form); 265 266 // Only upload server statistics and UMA metrics if at least some local data 267 // is available to use as a baseline. 268 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles(); 269 const std::vector<CreditCard*>& credit_cards = 270 personal_data_->GetCreditCards(); 271 if (!profiles.empty() || !credit_cards.empty()) { 272 // Copy the profile and credit card data, so that it can be accessed on a 273 // separate thread. 274 std::vector<AutofillProfile> copied_profiles; 275 copied_profiles.reserve(profiles.size()); 276 for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin(); 277 it != profiles.end(); ++it) { 278 copied_profiles.push_back(**it); 279 } 280 281 std::vector<CreditCard> copied_credit_cards; 282 copied_credit_cards.reserve(credit_cards.size()); 283 for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin(); 284 it != credit_cards.end(); ++it) { 285 copied_credit_cards.push_back(**it); 286 } 287 288 // Note that ownership of |submitted_form| is passed to the second task, 289 // using |base::Owned|. 290 FormStructure* raw_submitted_form = submitted_form.get(); 291 driver_->GetBlockingPool()->PostTaskAndReply( 292 FROM_HERE, 293 base::Bind(&DeterminePossibleFieldTypesForUpload, 294 copied_profiles, 295 copied_credit_cards, 296 app_locale_, 297 raw_submitted_form), 298 base::Bind(&AutofillManager::UploadFormDataAsyncCallback, 299 weak_ptr_factory_.GetWeakPtr(), 300 base::Owned(submitted_form.release()), 301 forms_loaded_timestamp_, 302 initial_interaction_timestamp_, 303 timestamp)); 304 } 305 306 return true; 307} 308 309void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms, 310 const TimeTicks& timestamp, 311 autofill::FormsSeenState state) { 312 if (!IsValidFormDataVector(forms)) 313 return; 314 315 bool is_post_document_load = state == autofill::DYNAMIC_FORMS_SEEN; 316 // If new forms were added dynamically, treat as a new page. 317 if (is_post_document_load) 318 Reset(); 319 320 if (!driver_->RendererIsAvailable()) 321 return; 322 323 bool enabled = IsAutofillEnabled(); 324 if (!has_logged_autofill_enabled_) { 325 metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled); 326 has_logged_autofill_enabled_ = true; 327 } 328 329 if (!enabled) 330 return; 331 332 forms_loaded_timestamp_ = timestamp; 333 ParseForms(forms); 334} 335 336void AutofillManager::OnTextFieldDidChange(const FormData& form, 337 const FormFieldData& field, 338 const TimeTicks& timestamp) { 339 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 340 return; 341 342 FormStructure* form_structure = NULL; 343 AutofillField* autofill_field = NULL; 344 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field)) 345 return; 346 347 if (!user_did_type_) { 348 user_did_type_ = true; 349 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE); 350 } 351 352 if (autofill_field->is_autofilled) { 353 autofill_field->is_autofilled = false; 354 metric_logger_->LogUserHappinessMetric( 355 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD); 356 357 if (!user_did_edit_autofilled_field_) { 358 user_did_edit_autofilled_field_ = true; 359 metric_logger_->LogUserHappinessMetric( 360 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE); 361 } 362 } 363 364 UpdateInitialInteractionTimestamp(timestamp); 365} 366 367void AutofillManager::OnQueryFormFieldAutofill(int query_id, 368 const FormData& form, 369 const FormFieldData& field, 370 const gfx::RectF& bounding_box, 371 bool display_warning) { 372 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 373 return; 374 375 std::vector<base::string16> values; 376 std::vector<base::string16> labels; 377 std::vector<base::string16> icons; 378 std::vector<int> unique_ids; 379 380 external_delegate_->OnQuery(query_id, 381 form, 382 field, 383 bounding_box, 384 display_warning); 385 FormStructure* form_structure = NULL; 386 AutofillField* autofill_field = NULL; 387 if (RefreshDataModels() && 388 driver_->RendererIsAvailable() && 389 GetCachedFormAndField(form, field, &form_structure, &autofill_field) && 390 // Don't send suggestions for forms that aren't auto-fillable. 391 form_structure->IsAutofillable(false)) { 392 AutofillType type = autofill_field->Type(); 393 bool is_filling_credit_card = (type.group() == CREDIT_CARD); 394 if (is_filling_credit_card) { 395 GetCreditCardSuggestions( 396 field, type, &values, &labels, &icons, &unique_ids); 397 } else { 398 GetProfileSuggestions( 399 form_structure, field, type, &values, &labels, &icons, &unique_ids); 400 } 401 402 DCHECK_EQ(values.size(), labels.size()); 403 DCHECK_EQ(values.size(), icons.size()); 404 DCHECK_EQ(values.size(), unique_ids.size()); 405 406 if (!values.empty()) { 407 // Don't provide Autofill suggestions when Autofill is disabled, and don't 408 // provide credit card suggestions for non-HTTPS pages. However, provide a 409 // warning to the user in these cases. 410 int warning = 0; 411 if (!form_structure->IsAutofillable(true)) 412 warning = IDS_AUTOFILL_WARNING_FORM_DISABLED; 413 else if (is_filling_credit_card && !FormIsHTTPS(*form_structure)) 414 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION; 415 if (warning) { 416 values.assign(1, l10n_util::GetStringUTF16(warning)); 417 labels.assign(1, base::string16()); 418 icons.assign(1, base::string16()); 419 unique_ids.assign(1, 420 blink::WebAutofillClient::MenuItemIDWarningMessage); 421 } else { 422 bool section_is_autofilled = 423 SectionIsAutofilled(*form_structure, form, 424 autofill_field->section()); 425 if (section_is_autofilled) { 426 // If the relevant section is auto-filled and the renderer is querying 427 // for suggestions, then the user is editing the value of a field. 428 // In this case, mimic autocomplete: don't display labels or icons, 429 // as that information is redundant. 430 labels.assign(labels.size(), base::string16()); 431 icons.assign(icons.size(), base::string16()); 432 } 433 434 // When filling credit card suggestions, the values and labels are 435 // typically obfuscated, which makes detecting duplicates hard. Since 436 // duplicates only tend to be a problem when filling address forms 437 // anyway, only don't de-dup credit card suggestions. 438 if (!is_filling_credit_card) 439 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids); 440 441 // The first time we show suggestions on this page, log the number of 442 // suggestions shown. 443 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) { 444 metric_logger_->LogAddressSuggestionsCount(values.size()); 445 has_logged_address_suggestions_count_ = true; 446 } 447 } 448 } 449 } 450 451 // Add the results from AutoComplete. They come back asynchronously, so we 452 // hand off what we generated and they will send the results back to the 453 // renderer. 454 autocomplete_history_manager_->OnGetAutocompleteSuggestions( 455 query_id, field.name, field.value, values, labels, icons, unique_ids); 456} 457 458void AutofillManager::OnFillAutofillFormData(int query_id, 459 const FormData& form, 460 const FormFieldData& field, 461 int unique_id) { 462 if (!IsValidFormData(form) || !IsValidFormFieldData(field)) 463 return; 464 465 const AutofillDataModel* data_model = NULL; 466 size_t variant = 0; 467 FormStructure* form_structure = NULL; 468 AutofillField* autofill_field = NULL; 469 // NOTE: RefreshDataModels may invalidate |data_model| because it causes the 470 // PersonalDataManager to reload Mac address book entries. Thus it must come 471 // before GetProfileOrCreditCard. 472 if (!RefreshDataModels() || 473 !driver_->RendererIsAvailable() || 474 !GetProfileOrCreditCard(unique_id, &data_model, &variant) || 475 !GetCachedFormAndField(form, field, &form_structure, &autofill_field)) 476 return; 477 478 DCHECK(form_structure); 479 DCHECK(autofill_field); 480 481 FormData result = form; 482 483 // If the relevant section is auto-filled, we should fill |field| but not the 484 // rest of the form. 485 if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) { 486 for (std::vector<FormFieldData>::iterator iter = result.fields.begin(); 487 iter != result.fields.end(); ++iter) { 488 if ((*iter) == field) { 489 base::string16 value = data_model->GetInfoForVariant( 490 autofill_field->Type(), variant, app_locale_); 491 AutofillField::FillFormField(*autofill_field, value, app_locale_, 492 &(*iter)); 493 // Mark the cached field as autofilled, so that we can detect when a 494 // user edits an autofilled field (for metrics). 495 autofill_field->is_autofilled = true; 496 break; 497 } 498 } 499 500 driver_->SendFormDataToRenderer(query_id, result); 501 return; 502 } 503 504 // Cache the field type for the field from which the user initiated autofill. 505 FieldTypeGroup initiating_group_type = autofill_field->Type().group(); 506 DCHECK_EQ(form_structure->field_count(), form.fields.size()); 507 for (size_t i = 0; i < form_structure->field_count(); ++i) { 508 if (form_structure->field(i)->section() != autofill_field->section()) 509 continue; 510 511 DCHECK_EQ(*form_structure->field(i), result.fields[i]); 512 513 const AutofillField* cached_field = form_structure->field(i); 514 FieldTypeGroup field_group_type = cached_field->Type().group(); 515 if (field_group_type != NO_GROUP) { 516 // If the field being filled is either 517 // (a) the field that the user initiated the fill from, or 518 // (b) part of the same logical unit, e.g. name or phone number, 519 // then take the multi-profile "variant" into account. 520 // Otherwise fill with the default (zeroth) variant. 521 size_t use_variant = 0; 522 if (result.fields[i] == field || 523 field_group_type == initiating_group_type) { 524 use_variant = variant; 525 } 526 base::string16 value = data_model->GetInfoForVariant( 527 cached_field->Type(), use_variant, app_locale_); 528 AutofillField::FillFormField(*cached_field, value, app_locale_, 529 &result.fields[i]); 530 // Mark the cached field as autofilled, so that we can detect when a user 531 // edits an autofilled field (for metrics). 532 form_structure->field(i)->is_autofilled = true; 533 } 534 } 535 536 autofilled_form_signatures_.push_front(form_structure->FormSignature()); 537 // Only remember the last few forms that we've seen, both to avoid false 538 // positives and to avoid wasting memory. 539 if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember) 540 autofilled_form_signatures_.pop_back(); 541 542 driver_->SendFormDataToRenderer(query_id, result); 543} 544 545void AutofillManager::OnDidPreviewAutofillFormData() { 546 if (test_delegate_) 547 test_delegate_->DidPreviewFormData(); 548} 549 550void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) { 551 if (test_delegate_) 552 test_delegate_->DidFillFormData(); 553 554 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL); 555 if (!user_did_autofill_) { 556 user_did_autofill_ = true; 557 metric_logger_->LogUserHappinessMetric( 558 AutofillMetrics::USER_DID_AUTOFILL_ONCE); 559 } 560 561 UpdateInitialInteractionTimestamp(timestamp); 562} 563 564void AutofillManager::OnDidShowAutofillSuggestions(bool is_new_popup) { 565 if (test_delegate_) 566 test_delegate_->DidShowSuggestions(); 567 568 if (is_new_popup) { 569 metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN); 570 571 if (!did_show_suggestions_) { 572 did_show_suggestions_ = true; 573 metric_logger_->LogUserHappinessMetric( 574 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE); 575 } 576 } 577} 578 579void AutofillManager::OnHideAutofillUI() { 580 if (!IsAutofillEnabled()) 581 return; 582 583 manager_delegate_->HideAutofillPopup(); 584} 585 586void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) { 587 const AutofillDataModel* data_model = NULL; 588 size_t variant = 0; 589 if (!GetProfileOrCreditCard(unique_id, &data_model, &variant)) { 590 NOTREACHED(); 591 return; 592 } 593 594 // TODO(csharp): If we are dealing with a variant only the variant should 595 // be deleted, instead of doing nothing. 596 // http://crbug.com/124211 597 if (variant != 0) 598 return; 599 600 personal_data_->RemoveByGUID(data_model->guid()); 601} 602 603void AutofillManager::RemoveAutocompleteEntry(const base::string16& name, 604 const base::string16& value) { 605 autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value); 606} 607 608const std::vector<FormStructure*>& AutofillManager::GetFormStructures() { 609 return form_structures_.get(); 610} 611 612void AutofillManager::SetTestDelegate( 613 autofill::AutofillManagerTestDelegate* delegate) { 614 test_delegate_ = delegate; 615} 616 617void AutofillManager::OnAddPasswordFormMapping( 618 const FormFieldData& username_field, 619 const PasswordFormFillData& fill_data) { 620 if (!IsValidFormFieldData(username_field) || 621 !IsValidPasswordFormFillData(fill_data)) 622 return; 623 624 external_delegate_->AddPasswordFormMapping(username_field, fill_data); 625} 626 627void AutofillManager::OnShowPasswordSuggestions( 628 const FormFieldData& field, 629 const gfx::RectF& bounds, 630 const std::vector<base::string16>& suggestions, 631 const std::vector<base::string16>& realms) { 632 if (!IsValidString16Vector(suggestions) || 633 !IsValidString16Vector(realms) || 634 suggestions.size() != realms.size()) 635 return; 636 637 external_delegate_->OnShowPasswordSuggestions(suggestions, 638 realms, 639 field, 640 bounds); 641} 642 643void AutofillManager::OnSetDataList(const std::vector<base::string16>& values, 644 const std::vector<base::string16>& labels) { 645 if (!IsValidString16Vector(values) || 646 !IsValidString16Vector(labels) || 647 values.size() != labels.size()) 648 return; 649 650 external_delegate_->SetCurrentDataListValues(values, labels); 651} 652 653void AutofillManager::OnLoadedServerPredictions( 654 const std::string& response_xml) { 655 // Parse and store the server predictions. 656 FormStructure::ParseQueryResponse(response_xml, 657 form_structures_.get(), 658 *metric_logger_); 659 660 // Forward form structures to the password generation manager to detect 661 // account creation forms. 662 manager_delegate_->DetectAccountCreationForms(form_structures_.get()); 663 664 // If the corresponding flag is set, annotate forms with the predicted types. 665 driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get()); 666} 667 668void AutofillManager::OnDidEndTextFieldEditing() { 669 external_delegate_->DidEndTextFieldEditing(); 670} 671 672bool AutofillManager::IsAutofillEnabled() const { 673 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled); 674} 675 676void AutofillManager::ImportFormData(const FormStructure& submitted_form) { 677 scoped_ptr<CreditCard> imported_credit_card; 678 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card)) 679 return; 680 681 // If credit card information was submitted, we need to confirm whether to 682 // save it. 683 if (imported_credit_card) { 684 manager_delegate_->ConfirmSaveCreditCard( 685 *metric_logger_, 686 base::Bind( 687 base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard), 688 base::Unretained(personal_data_), *imported_credit_card)); 689 } 690} 691 692// Note that |submitted_form| is passed as a pointer rather than as a reference 693// so that we can get memory management right across threads. Note also that we 694// explicitly pass in all the time stamps of interest, as the cached ones might 695// get reset before this method executes. 696void AutofillManager::UploadFormDataAsyncCallback( 697 const FormStructure* submitted_form, 698 const TimeTicks& load_time, 699 const TimeTicks& interaction_time, 700 const TimeTicks& submission_time) { 701 submitted_form->LogQualityMetrics(*metric_logger_, 702 load_time, 703 interaction_time, 704 submission_time); 705 706 if (submitted_form->ShouldBeCrowdsourced()) 707 UploadFormData(*submitted_form); 708} 709 710void AutofillManager::UploadFormData(const FormStructure& submitted_form) { 711 if (!download_manager_) 712 return; 713 714 // Check if the form is among the forms that were recently auto-filled. 715 bool was_autofilled = false; 716 std::string form_signature = submitted_form.FormSignature(); 717 for (std::list<std::string>::const_iterator it = 718 autofilled_form_signatures_.begin(); 719 it != autofilled_form_signatures_.end() && !was_autofilled; 720 ++it) { 721 if (*it == form_signature) 722 was_autofilled = true; 723 } 724 725 ServerFieldTypeSet non_empty_types; 726 personal_data_->GetNonEmptyTypes(&non_empty_types); 727 // Always add PASSWORD to |non_empty_types| so that if |submitted_form| 728 // contains a password field it will be uploaded to the server. If 729 // |submitted_form| doesn't contain a password field, there is no side 730 // effect from adding PASSWORD to |non_empty_types|. 731 non_empty_types.insert(autofill::PASSWORD); 732 733 download_manager_->StartUploadRequest(submitted_form, was_autofilled, 734 non_empty_types); 735} 736 737bool AutofillManager::UploadPasswordGenerationForm(const FormData& form) { 738 FormStructure form_structure(form); 739 740 if (!ShouldUploadForm(form_structure)) 741 return false; 742 743 if (!form_structure.ShouldBeCrowdsourced()) 744 return false; 745 746 // TODO(gcasto): Check that PasswordGeneration is enabled? 747 748 // Find the first password field to label. We don't try to label anything 749 // else. 750 bool found_password_field = false; 751 for (size_t i = 0; i < form_structure.field_count(); ++i) { 752 AutofillField* field = form_structure.field(i); 753 754 ServerFieldTypeSet types; 755 if (!found_password_field && field->form_control_type == "password") { 756 types.insert(ACCOUNT_CREATION_PASSWORD); 757 found_password_field = true; 758 } else { 759 types.insert(UNKNOWN_TYPE); 760 } 761 field->set_possible_types(types); 762 } 763 DCHECK(found_password_field); 764 765 // Only one field type should be present. 766 ServerFieldTypeSet available_field_types; 767 available_field_types.insert(ACCOUNT_CREATION_PASSWORD); 768 769 // Force uploading as these events are relatively rare and we want to make 770 // sure to receive them. It also makes testing easier if these requests 771 // always pass. 772 form_structure.set_upload_required(UPLOAD_REQUIRED); 773 774 if (!download_manager_) 775 return false; 776 777 return download_manager_->StartUploadRequest(form_structure, 778 false /* was_autofilled */, 779 available_field_types); 780} 781 782void AutofillManager::Reset() { 783 form_structures_.clear(); 784 has_logged_autofill_enabled_ = false; 785 has_logged_address_suggestions_count_ = false; 786 did_show_suggestions_ = false; 787 user_did_type_ = false; 788 user_did_autofill_ = false; 789 user_did_edit_autofilled_field_ = false; 790 forms_loaded_timestamp_ = TimeTicks(); 791 initial_interaction_timestamp_ = TimeTicks(); 792 external_delegate_->Reset(); 793} 794 795AutofillManager::AutofillManager(AutofillDriver* driver, 796 autofill::AutofillManagerDelegate* delegate, 797 PersonalDataManager* personal_data) 798 : driver_(driver), 799 manager_delegate_(delegate), 800 app_locale_("en-US"), 801 personal_data_(personal_data), 802 autocomplete_history_manager_( 803 new AutocompleteHistoryManager(driver, delegate)), 804 metric_logger_(new AutofillMetrics), 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 external_delegate_(NULL), 812 test_delegate_(NULL), 813 weak_ptr_factory_(this) { 814 DCHECK(driver_); 815 DCHECK(manager_delegate_); 816} 817 818void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) { 819 metric_logger_.reset(metric_logger); 820} 821 822bool AutofillManager::RefreshDataModels() const { 823 if (!IsAutofillEnabled()) 824 return false; 825 826 // No autofill data to return if the profiles are empty. 827 if (personal_data_->GetProfiles().empty() && 828 personal_data_->GetCreditCards().empty()) { 829 return false; 830 } 831 832 return true; 833} 834 835bool AutofillManager::GetProfileOrCreditCard( 836 int unique_id, 837 const AutofillDataModel** data_model, 838 size_t* variant) const { 839 // Unpack the |unique_id| into component parts. 840 GUIDPair credit_card_guid; 841 GUIDPair profile_guid; 842 UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid); 843 DCHECK(!base::IsValidGUID(credit_card_guid.first) || 844 !base::IsValidGUID(profile_guid.first)); 845 846 // Find the profile that matches the |profile_guid|, if one is specified. 847 // Otherwise find the credit card that matches the |credit_card_guid|, 848 // if specified. 849 if (base::IsValidGUID(profile_guid.first)) { 850 *data_model = personal_data_->GetProfileByGUID(profile_guid.first); 851 *variant = profile_guid.second; 852 } else if (base::IsValidGUID(credit_card_guid.first)) { 853 *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first); 854 *variant = credit_card_guid.second; 855 } 856 857 return !!*data_model; 858} 859 860bool AutofillManager::FindCachedForm(const FormData& form, 861 FormStructure** form_structure) const { 862 // Find the FormStructure that corresponds to |form|. 863 // Scan backward through the cached |form_structures_|, as updated versions of 864 // forms are added to the back of the list, whereas original versions of these 865 // forms might appear toward the beginning of the list. The communication 866 // protocol with the crowdsourcing server does not permit us to discard the 867 // original versions of the forms. 868 *form_structure = NULL; 869 for (std::vector<FormStructure*>::const_reverse_iterator iter = 870 form_structures_.rbegin(); 871 iter != form_structures_.rend(); ++iter) { 872 if (**iter == form) { 873 *form_structure = *iter; 874 875 // The same form might be cached with multiple field counts: in some 876 // cases, non-autofillable fields are filtered out, whereas in other cases 877 // they are not. To avoid thrashing the cache, keep scanning until we 878 // find a cached version with the same number of fields, if there is one. 879 if ((*iter)->field_count() == form.fields.size()) 880 break; 881 } 882 } 883 884 if (!(*form_structure)) 885 return false; 886 887 return true; 888} 889 890bool AutofillManager::GetCachedFormAndField(const FormData& form, 891 const FormFieldData& field, 892 FormStructure** form_structure, 893 AutofillField** autofill_field) { 894 // Find the FormStructure that corresponds to |form|. 895 // If we do not have this form in our cache but it is parseable, we'll add it 896 // in the call to |UpdateCachedForm()|. 897 if (!FindCachedForm(form, form_structure) && 898 !FormStructure(form).ShouldBeParsed(false)) { 899 return false; 900 } 901 902 // Update the cached form to reflect any dynamic changes to the form data, if 903 // necessary. 904 if (!UpdateCachedForm(form, *form_structure, form_structure)) 905 return false; 906 907 // No data to return if there are no auto-fillable fields. 908 if (!(*form_structure)->autofill_count()) 909 return false; 910 911 // Find the AutofillField that corresponds to |field|. 912 *autofill_field = NULL; 913 for (std::vector<AutofillField*>::const_iterator iter = 914 (*form_structure)->begin(); 915 iter != (*form_structure)->end(); ++iter) { 916 if ((**iter) == field) { 917 *autofill_field = *iter; 918 break; 919 } 920 } 921 922 // Even though we always update the cache, the field might not exist if the 923 // website disables autocomplete while the user is interacting with the form. 924 // See http://crbug.com/160476 925 return *autofill_field != NULL; 926} 927 928bool AutofillManager::UpdateCachedForm(const FormData& live_form, 929 const FormStructure* cached_form, 930 FormStructure** updated_form) { 931 bool needs_update = 932 (!cached_form || 933 live_form.fields.size() != cached_form->field_count()); 934 for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) { 935 needs_update = *cached_form->field(i) != live_form.fields[i]; 936 } 937 938 if (!needs_update) 939 return true; 940 941 if (form_structures_.size() >= kMaxFormCacheSize) 942 return false; 943 944 // Add the new or updated form to our cache. 945 form_structures_.push_back(new FormStructure(live_form)); 946 *updated_form = *form_structures_.rbegin(); 947 (*updated_form)->DetermineHeuristicTypes(*metric_logger_); 948 949 // If we have cached data, propagate it to the updated form. 950 if (cached_form) { 951 std::map<base::string16, const AutofillField*> cached_fields; 952 for (size_t i = 0; i < cached_form->field_count(); ++i) { 953 const AutofillField* field = cached_form->field(i); 954 cached_fields[field->unique_name()] = field; 955 } 956 957 for (size_t i = 0; i < (*updated_form)->field_count(); ++i) { 958 AutofillField* field = (*updated_form)->field(i); 959 std::map<base::string16, const AutofillField*>::iterator cached_field = 960 cached_fields.find(field->unique_name()); 961 if (cached_field != cached_fields.end()) { 962 field->set_server_type(cached_field->second->server_type()); 963 field->is_autofilled = cached_field->second->is_autofilled; 964 } 965 } 966 967 // Note: We _must not_ remove the original version of the cached form from 968 // the list of |form_structures_|. Otherwise, we break parsing of the 969 // crowdsourcing server's response to our query. 970 } 971 972 // Annotate the updated form with its predicted types. 973 std::vector<FormStructure*> forms(1, *updated_form); 974 driver_->SendAutofillTypePredictionsToRenderer(forms); 975 976 return true; 977} 978 979void AutofillManager::GetProfileSuggestions( 980 FormStructure* form, 981 const FormFieldData& field, 982 const AutofillType& type, 983 std::vector<base::string16>* values, 984 std::vector<base::string16>* labels, 985 std::vector<base::string16>* icons, 986 std::vector<int>* unique_ids) const { 987 std::vector<ServerFieldType> field_types(form->field_count()); 988 for (size_t i = 0; i < form->field_count(); ++i) { 989 field_types.push_back(form->field(i)->Type().GetStorableType()); 990 } 991 std::vector<GUIDPair> guid_pairs; 992 993 personal_data_->GetProfileSuggestions( 994 type, field.value, field.is_autofilled, field_types, 995 values, labels, icons, &guid_pairs); 996 997 for (size_t i = 0; i < guid_pairs.size(); ++i) { 998 unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0), 999 guid_pairs[i])); 1000 } 1001} 1002 1003void AutofillManager::GetCreditCardSuggestions( 1004 const FormFieldData& field, 1005 const AutofillType& type, 1006 std::vector<base::string16>* values, 1007 std::vector<base::string16>* labels, 1008 std::vector<base::string16>* icons, 1009 std::vector<int>* unique_ids) const { 1010 std::vector<GUIDPair> guid_pairs; 1011 personal_data_->GetCreditCardSuggestions( 1012 type, field.value, values, labels, icons, &guid_pairs); 1013 1014 for (size_t i = 0; i < guid_pairs.size(); ++i) { 1015 unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0))); 1016 } 1017} 1018 1019void AutofillManager::ParseForms(const std::vector<FormData>& forms) { 1020 std::vector<FormStructure*> non_queryable_forms; 1021 for (std::vector<FormData>::const_iterator iter = forms.begin(); 1022 iter != forms.end(); ++iter) { 1023 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); 1024 if (!form_structure->ShouldBeParsed(false)) 1025 continue; 1026 1027 form_structure->DetermineHeuristicTypes(*metric_logger_); 1028 1029 // Set aside forms with method GET or author-specified types, so that they 1030 // are not included in the query to the server. 1031 if (form_structure->ShouldBeCrowdsourced()) 1032 form_structures_.push_back(form_structure.release()); 1033 else 1034 non_queryable_forms.push_back(form_structure.release()); 1035 } 1036 1037 if (!form_structures_.empty() && download_manager_) { 1038 // Query the server if we have at least one of the forms were parsed. 1039 download_manager_->StartQueryRequest(form_structures_.get(), 1040 *metric_logger_); 1041 } 1042 1043 for (std::vector<FormStructure*>::const_iterator iter = 1044 non_queryable_forms.begin(); 1045 iter != non_queryable_forms.end(); ++iter) { 1046 form_structures_.push_back(*iter); 1047 } 1048 1049 if (!form_structures_.empty()) 1050 metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED); 1051 1052 // For the |non_queryable_forms|, we have all the field type info we're ever 1053 // going to get about them. For the other forms, we'll wait until we get a 1054 // response from the server. 1055 driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms); 1056} 1057 1058int AutofillManager::GUIDToID(const GUIDPair& guid) const { 1059 if (!base::IsValidGUID(guid.first)) 1060 return 0; 1061 1062 std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid); 1063 if (iter == guid_id_map_.end()) { 1064 int id = guid_id_map_.size() + 1; 1065 guid_id_map_[guid] = id; 1066 id_guid_map_[id] = guid; 1067 return id; 1068 } else { 1069 return iter->second; 1070 } 1071} 1072 1073const GUIDPair AutofillManager::IDToGUID(int id) const { 1074 if (id == 0) 1075 return GUIDPair(std::string(), 0); 1076 1077 std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id); 1078 if (iter == id_guid_map_.end()) { 1079 NOTREACHED(); 1080 return GUIDPair(std::string(), 0); 1081 } 1082 1083 return iter->second; 1084} 1085 1086// When sending IDs (across processes) to the renderer we pack credit card and 1087// profile IDs into a single integer. Credit card IDs are sent in the high 1088// word and profile IDs are sent in the low word. 1089int AutofillManager::PackGUIDs(const GUIDPair& cc_guid, 1090 const GUIDPair& profile_guid) const { 1091 int cc_id = GUIDToID(cc_guid); 1092 int profile_id = GUIDToID(profile_guid); 1093 1094 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max()); 1095 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max()); 1096 1097 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id; 1098} 1099 1100// When receiving IDs (across processes) from the renderer we unpack credit card 1101// and profile IDs from a single integer. Credit card IDs are stored in the 1102// high word and profile IDs are stored in the low word. 1103void AutofillManager::UnpackGUIDs(int id, 1104 GUIDPair* cc_guid, 1105 GUIDPair* profile_guid) const { 1106 int cc_id = id >> std::numeric_limits<unsigned short>::digits & 1107 std::numeric_limits<unsigned short>::max(); 1108 int profile_id = id & std::numeric_limits<unsigned short>::max(); 1109 1110 *cc_guid = IDToGUID(cc_id); 1111 *profile_guid = IDToGUID(profile_id); 1112} 1113 1114void AutofillManager::UpdateInitialInteractionTimestamp( 1115 const TimeTicks& interaction_timestamp) { 1116 if (initial_interaction_timestamp_.is_null() || 1117 interaction_timestamp < initial_interaction_timestamp_) { 1118 initial_interaction_timestamp_ = interaction_timestamp; 1119 } 1120} 1121 1122bool AutofillManager::ShouldUploadForm(const FormStructure& form) { 1123 if (!IsAutofillEnabled()) 1124 return false; 1125 1126 if (driver_->IsOffTheRecord()) 1127 return false; 1128 1129 // Disregard forms that we wouldn't ever autofill in the first place. 1130 if (!form.ShouldBeParsed(true)) 1131 return false; 1132 1133 return true; 1134} 1135 1136} // namespace autofill 1137