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.
5 #include "components/autofill/core/browser/autofill_manager.h"
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_client.h"
25 #include "components/autofill/core/browser/autofill_data_model.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_test_delegate.h"
29 #include "components/autofill/core/browser/autofill_metrics.h"
30 #include "components/autofill/core/browser/autofill_profile.h"
31 #include "components/autofill/core/browser/autofill_type.h"
32 #include "components/autofill/core/browser/credit_card.h"
33 #include "components/autofill/core/browser/form_structure.h"
34 #include "components/autofill/core/browser/personal_data_manager.h"
35 #include "components/autofill/core/browser/phone_number.h"
36 #include "components/autofill/core/browser/phone_number_i18n.h"
37 #include "components/autofill/core/browser/popup_item_ids.h"
38 #include "components/autofill/core/common/autofill_data_validation.h"
39 #include "components/autofill/core/common/autofill_pref_names.h"
40 #include "components/autofill/core/common/autofill_switches.h"
41 #include "components/autofill/core/common/form_data.h"
42 #include "components/autofill/core/common/form_data_predictions.h"
43 #include "components/autofill/core/common/form_field_data.h"
44 #include "components/autofill/core/common/password_form_fill_data.h"
45 #include "components/pref_registry/pref_registry_syncable.h"
46 #include "grit/components_strings.h"
47 #include "ui/base/l10n/l10n_util.h"
48 #include "ui/gfx/rect.h"
53 typedef PersonalDataManager::GUIDPair GUIDPair;
55 using base::TimeTicks;
59 // We only send a fraction of the forms to upload server.
60 // The rate for positive/negative matches potentially could be different.
61 const double kAutofillPositiveUploadRateDefaultValue = 0.20;
62 const double kAutofillNegativeUploadRateDefaultValue = 0.20;
64 const size_t kMaxRecentFormSignaturesToRemember = 3;
66 // Set a conservative upper bound on the number of forms we are willing to
67 // cache, simply to prevent unbounded memory consumption.
68 const size_t kMaxFormCacheSize = 100;
70 // Removes duplicate suggestions whilst preserving their original order.
71 void RemoveDuplicateSuggestions(std::vector<base::string16>* values,
72 std::vector<base::string16>* labels,
73 std::vector<base::string16>* icons,
74 std::vector<int>* unique_ids) {
75 DCHECK_EQ(values->size(), labels->size());
76 DCHECK_EQ(values->size(), icons->size());
77 DCHECK_EQ(values->size(), unique_ids->size());
79 std::set<std::pair<base::string16, base::string16> > seen_suggestions;
80 std::vector<base::string16> values_copy;
81 std::vector<base::string16> labels_copy;
82 std::vector<base::string16> icons_copy;
83 std::vector<int> unique_ids_copy;
85 for (size_t i = 0; i < values->size(); ++i) {
86 const std::pair<base::string16, base::string16> suggestion(
87 (*values)[i], (*labels)[i]);
88 if (seen_suggestions.insert(suggestion).second) {
89 values_copy.push_back((*values)[i]);
90 labels_copy.push_back((*labels)[i]);
91 icons_copy.push_back((*icons)[i]);
92 unique_ids_copy.push_back((*unique_ids)[i]);
96 values->swap(values_copy);
97 labels->swap(labels_copy);
98 icons->swap(icons_copy);
99 unique_ids->swap(unique_ids_copy);
102 // Precondition: |form_structure| and |form| should correspond to the same
103 // logical form. Returns true if any field in the given |section| within |form|
105 bool SectionIsAutofilled(const FormStructure& form_structure,
106 const FormData& form,
107 const std::string& section) {
108 DCHECK_EQ(form_structure.field_count(), form.fields.size());
109 for (size_t i = 0; i < form_structure.field_count(); ++i) {
110 if (form_structure.field(i)->section() == section &&
111 form.fields[i].is_autofilled) {
119 bool FormIsHTTPS(const FormStructure& form) {
120 // TODO(blundell): Change this to use a constant once crbug.com/306258 is
122 return form.source_url().SchemeIs("https");
125 // Uses the existing personal data in |profiles| and |credit_cards| to determine
126 // possible field types for the |submitted_form|. This is potentially
127 // expensive -- on the order of 50ms even for a small set of |stored_data|.
128 // Hence, it should not run on the UI thread -- to avoid locking up the UI --
129 // nor on the IO thread -- to avoid blocking IPC calls.
130 void DeterminePossibleFieldTypesForUpload(
131 const std::vector<AutofillProfile>& profiles,
132 const std::vector<CreditCard>& credit_cards,
133 const std::string& app_locale,
134 FormStructure* submitted_form) {
135 // For each field in the |submitted_form|, extract the value. Then for each
136 // profile or credit card, identify any stored types that match the value.
137 for (size_t i = 0; i < submitted_form->field_count(); ++i) {
138 AutofillField* field = submitted_form->field(i);
139 ServerFieldTypeSet matching_types;
141 base::string16 value;
142 base::TrimWhitespace(field->value, base::TRIM_ALL, &value);
143 for (std::vector<AutofillProfile>::const_iterator it = profiles.begin();
144 it != profiles.end(); ++it) {
145 it->GetMatchingTypes(value, app_locale, &matching_types);
147 for (std::vector<CreditCard>::const_iterator it = credit_cards.begin();
148 it != credit_cards.end(); ++it) {
149 it->GetMatchingTypes(value, app_locale, &matching_types);
152 if (matching_types.empty())
153 matching_types.insert(UNKNOWN_TYPE);
155 field->set_possible_types(matching_types);
161 AutofillManager::AutofillManager(
162 AutofillDriver* driver,
163 AutofillClient* client,
164 const std::string& app_locale,
165 AutofillDownloadManagerState enable_download_manager)
168 app_locale_(app_locale),
169 personal_data_(client->GetPersonalDataManager()),
170 autocomplete_history_manager_(
171 new AutocompleteHistoryManager(driver, client)),
172 metric_logger_(new AutofillMetrics),
173 has_logged_autofill_enabled_(false),
174 has_logged_address_suggestions_count_(false),
175 did_show_suggestions_(false),
176 user_did_type_(false),
177 user_did_autofill_(false),
178 user_did_edit_autofilled_field_(false),
179 external_delegate_(NULL),
180 test_delegate_(NULL),
181 weak_ptr_factory_(this) {
182 if (enable_download_manager == ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {
183 download_manager_.reset(
184 new AutofillDownloadManager(driver, client_->GetPrefs(), this));
188 AutofillManager::~AutofillManager() {}
191 void AutofillManager::RegisterProfilePrefs(
192 user_prefs::PrefRegistrySyncable* registry) {
193 registry->RegisterBooleanPref(
194 prefs::kAutofillEnabled,
196 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
197 #if defined(OS_MACOSX)
198 registry->RegisterBooleanPref(
199 prefs::kAutofillAuxiliaryProfilesEnabled,
201 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
202 #else // defined(OS_MACOSX)
203 registry->RegisterBooleanPref(
204 prefs::kAutofillAuxiliaryProfilesEnabled,
206 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
207 #endif // defined(OS_MACOSX)
208 #if defined(OS_MACOSX)
209 registry->RegisterBooleanPref(
210 prefs::kAutofillMacAddressBookQueried,
212 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
213 #endif // defined(OS_MACOSX)
214 registry->RegisterDoublePref(
215 prefs::kAutofillPositiveUploadRate,
216 kAutofillPositiveUploadRateDefaultValue,
217 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
218 registry->RegisterDoublePref(
219 prefs::kAutofillNegativeUploadRate,
220 kAutofillNegativeUploadRateDefaultValue,
221 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
223 #if defined(OS_MACOSX) && !defined(OS_IOS)
224 registry->RegisterBooleanPref(
225 prefs::kAutofillUseMacAddressBook,
227 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
228 registry->RegisterIntegerPref(
229 prefs::kAutofillMacAddressBookShowedCount,
231 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
232 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
235 #if defined(OS_MACOSX) && !defined(OS_IOS)
236 void AutofillManager::MigrateUserPrefs(PrefService* prefs) {
237 const PrefService::Preference* pref =
238 prefs->FindPreference(prefs::kAutofillUseMacAddressBook);
240 // If the pref is not its default value, then the migration has already been
242 if (!pref->IsDefaultValue())
245 // Whether Chrome has already tried to access the user's Address Book.
246 const PrefService::Preference* pref_accessed =
247 prefs->FindPreference(prefs::kAutofillMacAddressBookQueried);
248 // Whether the user wants to use the Address Book to populate Autofill.
249 const PrefService::Preference* pref_enabled =
250 prefs->FindPreference(prefs::kAutofillAuxiliaryProfilesEnabled);
252 if (pref_accessed->IsDefaultValue() && pref_enabled->IsDefaultValue()) {
253 // This is likely a new user. Reset the default value to prevent the
254 // migration from happening again.
255 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook,
256 prefs->GetBoolean(prefs::kAutofillUseMacAddressBook));
262 bool success = pref_accessed->GetValue()->GetAsBoolean(&accessed);
264 success = pref_enabled->GetValue()->GetAsBoolean(&enabled);
267 prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, accessed && enabled);
269 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
271 void AutofillManager::SetExternalDelegate(AutofillExternalDelegate* delegate) {
272 // TODO(jrg): consider passing delegate into the ctor. That won't
273 // work if the delegate has a pointer to the AutofillManager, but
274 // future directions may not need such a pointer.
275 external_delegate_ = delegate;
276 autocomplete_history_manager_->SetExternalDelegate(delegate);
279 void AutofillManager::ShowAutofillSettings() {
280 client_->ShowAutofillSettings();
283 #if defined(OS_MACOSX) && !defined(OS_IOS)
284 bool AutofillManager::ShouldShowAccessAddressBookSuggestion(
285 const FormData& form,
286 const FormFieldData& field) {
287 if (!personal_data_ || !field.should_autocomplete)
290 FormStructure* form_structure = NULL;
291 AutofillField* autofill_field = NULL;
292 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
295 if (!form_structure->IsAutofillable())
298 return personal_data_->ShouldShowAccessAddressBookSuggestion(
299 autofill_field->Type());
302 bool AutofillManager::AccessAddressBook() {
305 return personal_data_->AccessAddressBook();
308 void AutofillManager::ShowedAccessAddressBookPrompt() {
311 return personal_data_->ShowedAccessAddressBookPrompt();
314 int AutofillManager::AccessAddressBookPromptCount() {
317 return personal_data_->AccessAddressBookPromptCount();
319 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
321 bool AutofillManager::OnFormSubmitted(const FormData& form,
322 const TimeTicks& timestamp) {
323 if (!IsValidFormData(form))
326 // Let Autocomplete know as well.
327 autocomplete_history_manager_->OnFormSubmitted(form);
329 // Grab a copy of the form data.
330 scoped_ptr<FormStructure> submitted_form(new FormStructure(form));
332 if (!ShouldUploadForm(*submitted_form))
335 // Don't save data that was submitted through JavaScript.
336 if (!form.user_submitted)
339 // Ignore forms not present in our cache. These are typically forms with
340 // wonky JavaScript that also makes them not auto-fillable.
341 FormStructure* cached_submitted_form;
342 if (!FindCachedForm(form, &cached_submitted_form))
345 submitted_form->UpdateFromCache(*cached_submitted_form);
346 if (submitted_form->IsAutofillable())
347 ImportFormData(*submitted_form);
349 // Only upload server statistics and UMA metrics if at least some local data
350 // is available to use as a baseline.
351 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
352 const std::vector<CreditCard*>& credit_cards =
353 personal_data_->GetCreditCards();
354 if (!profiles.empty() || !credit_cards.empty()) {
355 // Copy the profile and credit card data, so that it can be accessed on a
357 std::vector<AutofillProfile> copied_profiles;
358 copied_profiles.reserve(profiles.size());
359 for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin();
360 it != profiles.end(); ++it) {
361 copied_profiles.push_back(**it);
364 std::vector<CreditCard> copied_credit_cards;
365 copied_credit_cards.reserve(credit_cards.size());
366 for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin();
367 it != credit_cards.end(); ++it) {
368 copied_credit_cards.push_back(**it);
371 // Note that ownership of |submitted_form| is passed to the second task,
372 // using |base::Owned|.
373 FormStructure* raw_submitted_form = submitted_form.get();
374 driver_->GetBlockingPool()->PostTaskAndReply(
376 base::Bind(&DeterminePossibleFieldTypesForUpload,
381 base::Bind(&AutofillManager::UploadFormDataAsyncCallback,
382 weak_ptr_factory_.GetWeakPtr(),
383 base::Owned(submitted_form.release()),
384 forms_loaded_timestamps_[form],
385 initial_interaction_timestamp_,
392 void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms,
393 const TimeTicks& timestamp) {
394 if (!IsValidFormDataVector(forms))
397 if (!driver_->RendererIsAvailable())
400 bool enabled = IsAutofillEnabled();
401 if (!has_logged_autofill_enabled_) {
402 metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled);
403 has_logged_autofill_enabled_ = true;
409 for (size_t i = 0; i < forms.size(); ++i) {
410 forms_loaded_timestamps_[forms[i]] = timestamp;
416 void AutofillManager::OnTextFieldDidChange(const FormData& form,
417 const FormFieldData& field,
418 const TimeTicks& timestamp) {
419 if (!IsValidFormData(form) || !IsValidFormFieldData(field))
422 FormStructure* form_structure = NULL;
423 AutofillField* autofill_field = NULL;
424 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
427 if (!user_did_type_) {
428 user_did_type_ = true;
429 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE);
432 if (autofill_field->is_autofilled) {
433 autofill_field->is_autofilled = false;
434 metric_logger_->LogUserHappinessMetric(
435 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD);
437 if (!user_did_edit_autofilled_field_) {
438 user_did_edit_autofilled_field_ = true;
439 metric_logger_->LogUserHappinessMetric(
440 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE);
444 UpdateInitialInteractionTimestamp(timestamp);
447 void AutofillManager::OnQueryFormFieldAutofill(int query_id,
448 const FormData& form,
449 const FormFieldData& field,
450 const gfx::RectF& bounding_box,
451 bool display_warning) {
452 if (!IsValidFormData(form) || !IsValidFormFieldData(field))
455 std::vector<base::string16> values;
456 std::vector<base::string16> labels;
457 std::vector<base::string16> icons;
458 std::vector<int> unique_ids;
460 external_delegate_->OnQuery(query_id,
465 FormStructure* form_structure = NULL;
466 AutofillField* autofill_field = NULL;
467 if (RefreshDataModels() &&
468 driver_->RendererIsAvailable() &&
469 GetCachedFormAndField(form, field, &form_structure, &autofill_field) &&
470 // Don't send suggestions for forms that aren't auto-fillable.
471 form_structure->IsAutofillable()) {
472 AutofillType type = autofill_field->Type();
473 bool is_filling_credit_card = (type.group() == CREDIT_CARD);
474 if (is_filling_credit_card) {
475 GetCreditCardSuggestions(
476 field, type, &values, &labels, &icons, &unique_ids);
478 GetProfileSuggestions(*form_structure,
487 DCHECK_EQ(values.size(), labels.size());
488 DCHECK_EQ(values.size(), icons.size());
489 DCHECK_EQ(values.size(), unique_ids.size());
491 if (!values.empty()) {
492 // Don't provide Autofill suggestions when Autofill is disabled, and don't
493 // provide credit card suggestions for non-HTTPS pages. However, provide a
494 // warning to the user in these cases.
496 if (is_filling_credit_card && !FormIsHTTPS(*form_structure)) {
497 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION;
500 values.assign(1, l10n_util::GetStringUTF16(warning));
501 labels.assign(1, base::string16());
502 icons.assign(1, base::string16());
503 unique_ids.assign(1, POPUP_ITEM_ID_WARNING_MESSAGE);
505 bool section_is_autofilled =
506 SectionIsAutofilled(*form_structure, form,
507 autofill_field->section());
508 if (section_is_autofilled) {
509 // If the relevant section is auto-filled and the renderer is querying
510 // for suggestions, then the user is editing the value of a field.
511 // In this case, mimic autocomplete: don't display labels or icons,
512 // as that information is redundant.
513 labels.assign(labels.size(), base::string16());
514 icons.assign(icons.size(), base::string16());
517 // When filling credit card suggestions, the values and labels are
518 // typically obfuscated, which makes detecting duplicates hard. Since
519 // duplicates only tend to be a problem when filling address forms
520 // anyway, only don't de-dup credit card suggestions.
521 if (!is_filling_credit_card)
522 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
524 // The first time we show suggestions on this page, log the number of
525 // suggestions shown.
526 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
527 metric_logger_->LogAddressSuggestionsCount(values.size());
528 has_logged_address_suggestions_count_ = true;
534 if (field.should_autocomplete) {
535 // Add the results from AutoComplete. They come back asynchronously, so we
536 // hand off what we generated and they will send the results back to the
538 autocomplete_history_manager_->OnGetAutocompleteSuggestions(
539 query_id, field.name, field.value, field.form_control_type, values,
540 labels, icons, unique_ids);
542 // Autocomplete is disabled for this field; only pass back Autofill
544 autocomplete_history_manager_->CancelPendingQuery();
545 external_delegate_->OnSuggestionsReturned(
546 query_id, values, labels, icons, unique_ids);
550 void AutofillManager::FillOrPreviewForm(
551 AutofillDriver::RendererFormDataAction action,
553 const FormData& form,
554 const FormFieldData& field,
556 if (!IsValidFormData(form) || !IsValidFormFieldData(field))
559 const AutofillDataModel* data_model = NULL;
561 FormStructure* form_structure = NULL;
562 AutofillField* autofill_field = NULL;
563 bool is_credit_card = false;
564 // NOTE: RefreshDataModels may invalidate |data_model| because it causes the
565 // PersonalDataManager to reload Mac address book entries. Thus it must come
566 // before GetProfileOrCreditCard.
567 if (!RefreshDataModels() ||
568 !driver_->RendererIsAvailable() ||
569 !GetProfileOrCreditCard(
570 unique_id, &data_model, &variant, &is_credit_card) ||
571 !GetCachedFormAndField(form, field, &form_structure, &autofill_field))
574 DCHECK(form_structure);
575 DCHECK(autofill_field);
577 FormData result = form;
579 base::string16 profile_full_name;
580 std::string profile_language_code;
581 if (!is_credit_card) {
582 profile_full_name = data_model->GetInfo(
583 AutofillType(NAME_FULL), app_locale_);
584 profile_language_code =
585 static_cast<const AutofillProfile*>(data_model)->language_code();
588 // If the relevant section is auto-filled, we should fill |field| but not the
590 if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) {
591 for (std::vector<FormFieldData>::iterator iter = result.fields.begin();
592 iter != result.fields.end(); ++iter) {
593 if ((*iter) == field) {
594 base::string16 value = data_model->GetInfoForVariant(
595 autofill_field->Type(), variant, app_locale_);
596 if (AutofillField::FillFormField(*autofill_field,
598 profile_language_code,
601 // Mark the cached field as autofilled, so that we can detect when a
602 // user edits an autofilled field (for metrics).
603 autofill_field->is_autofilled = true;
605 // Mark the field as autofilled when a non-empty value is assigned to
606 // it. This allows the renderer to distinguish autofilled fields from
607 // fields with non-empty values, such as select-one fields.
608 iter->is_autofilled = true;
610 if (!is_credit_card && !value.empty())
611 client_->DidFillOrPreviewField(value, profile_full_name);
617 driver_->SendFormDataToRenderer(query_id, action, result);
621 // Cache the field type for the field from which the user initiated autofill.
622 FieldTypeGroup initiating_group_type = autofill_field->Type().group();
623 DCHECK_EQ(form_structure->field_count(), form.fields.size());
624 for (size_t i = 0; i < form_structure->field_count(); ++i) {
625 if (form_structure->field(i)->section() != autofill_field->section())
628 DCHECK_EQ(*form_structure->field(i), result.fields[i]);
630 const AutofillField* cached_field = form_structure->field(i);
631 FieldTypeGroup field_group_type = cached_field->Type().group();
632 if (field_group_type != NO_GROUP) {
633 // If the field being filled is either
634 // (a) the field that the user initiated the fill from, or
635 // (b) part of the same logical unit, e.g. name or phone number,
636 // then take the multi-profile "variant" into account.
637 // Otherwise fill with the default (zeroth) variant.
638 size_t use_variant = 0;
639 if (result.fields[i] == field ||
640 field_group_type == initiating_group_type) {
641 use_variant = variant;
643 base::string16 value = data_model->GetInfoForVariant(
644 cached_field->Type(), use_variant, app_locale_);
646 // Must match ForEachMatchingFormField() in form_autofill_util.cc.
647 // Only notify autofilling of empty fields and the field that initiated
648 // the filling (note that "select-one" controls may not be empty but will
649 // still be autofilled).
653 (result.fields[i] == field ||
654 result.fields[i].form_control_type == "select-one" ||
655 result.fields[i].value.empty());
656 if (AutofillField::FillFormField(*cached_field,
658 profile_language_code,
660 &result.fields[i])) {
661 // Mark the cached field as autofilled, so that we can detect when a
662 // user edits an autofilled field (for metrics).
663 form_structure->field(i)->is_autofilled = true;
665 // Mark the field as autofilled when a non-empty value is assigned to
666 // it. This allows the renderer to distinguish autofilled fields from
667 // fields with non-empty values, such as select-one fields.
668 result.fields[i].is_autofilled = true;
671 client_->DidFillOrPreviewField(value, profile_full_name);
676 autofilled_form_signatures_.push_front(form_structure->FormSignature());
677 // Only remember the last few forms that we've seen, both to avoid false
678 // positives and to avoid wasting memory.
679 if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember)
680 autofilled_form_signatures_.pop_back();
682 driver_->SendFormDataToRenderer(query_id, action, result);
685 void AutofillManager::OnDidPreviewAutofillFormData() {
687 test_delegate_->DidPreviewFormData();
690 void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) {
692 test_delegate_->DidFillFormData();
694 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL);
695 if (!user_did_autofill_) {
696 user_did_autofill_ = true;
697 metric_logger_->LogUserHappinessMetric(
698 AutofillMetrics::USER_DID_AUTOFILL_ONCE);
701 UpdateInitialInteractionTimestamp(timestamp);
704 void AutofillManager::DidShowSuggestions(bool is_new_popup) {
706 test_delegate_->DidShowSuggestions();
709 metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN);
711 if (!did_show_suggestions_) {
712 did_show_suggestions_ = true;
713 metric_logger_->LogUserHappinessMetric(
714 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE);
719 void AutofillManager::OnHidePopup() {
720 if (!IsAutofillEnabled())
723 autocomplete_history_manager_->CancelPendingQuery();
724 client_->HideAutofillPopup();
727 void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) {
728 const AutofillDataModel* data_model = NULL;
730 bool unused_is_credit_card = false;
731 if (!GetProfileOrCreditCard(
732 unique_id, &data_model, &variant, &unused_is_credit_card)) {
737 // TODO(csharp): If we are dealing with a variant only the variant should
738 // be deleted, instead of doing nothing.
739 // http://crbug.com/124211
743 personal_data_->RemoveByGUID(data_model->guid());
746 void AutofillManager::RemoveAutocompleteEntry(const base::string16& name,
747 const base::string16& value) {
748 autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value);
751 const std::vector<FormStructure*>& AutofillManager::GetFormStructures() {
752 return form_structures_.get();
755 void AutofillManager::SetTestDelegate(AutofillManagerTestDelegate* delegate) {
756 test_delegate_ = delegate;
759 void AutofillManager::OnSetDataList(const std::vector<base::string16>& values,
760 const std::vector<base::string16>& labels) {
761 if (!IsValidString16Vector(values) ||
762 !IsValidString16Vector(labels) ||
763 values.size() != labels.size())
766 external_delegate_->SetCurrentDataListValues(values, labels);
769 void AutofillManager::OnLoadedServerPredictions(
770 const std::string& response_xml) {
771 // Parse and store the server predictions.
772 FormStructure::ParseQueryResponse(response_xml,
773 form_structures_.get(),
776 // Forward form structures to the password generation manager to detect
777 // account creation forms.
778 client_->DetectAccountCreationForms(form_structures_.get());
780 // If the corresponding flag is set, annotate forms with the predicted types.
781 driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
784 void AutofillManager::OnDidEndTextFieldEditing() {
785 external_delegate_->DidEndTextFieldEditing();
788 bool AutofillManager::IsAutofillEnabled() const {
789 return client_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
792 void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
793 scoped_ptr<CreditCard> imported_credit_card;
794 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
797 // If credit card information was submitted, we need to confirm whether to
799 if (imported_credit_card) {
800 client_->ConfirmSaveCreditCard(
803 base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard),
804 base::Unretained(personal_data_),
805 *imported_credit_card));
809 // Note that |submitted_form| is passed as a pointer rather than as a reference
810 // so that we can get memory management right across threads. Note also that we
811 // explicitly pass in all the time stamps of interest, as the cached ones might
812 // get reset before this method executes.
813 void AutofillManager::UploadFormDataAsyncCallback(
814 const FormStructure* submitted_form,
815 const TimeTicks& load_time,
816 const TimeTicks& interaction_time,
817 const TimeTicks& submission_time) {
818 submitted_form->LogQualityMetrics(*metric_logger_,
823 if (submitted_form->ShouldBeCrowdsourced())
824 UploadFormData(*submitted_form);
827 void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
828 if (!download_manager_)
831 // Check if the form is among the forms that were recently auto-filled.
832 bool was_autofilled = false;
833 std::string form_signature = submitted_form.FormSignature();
834 for (std::list<std::string>::const_iterator it =
835 autofilled_form_signatures_.begin();
836 it != autofilled_form_signatures_.end() && !was_autofilled;
838 if (*it == form_signature)
839 was_autofilled = true;
842 ServerFieldTypeSet non_empty_types;
843 personal_data_->GetNonEmptyTypes(&non_empty_types);
845 download_manager_->StartUploadRequest(submitted_form, was_autofilled,
849 bool AutofillManager::UploadPasswordForm(
850 const FormData& form,
851 const ServerFieldType& password_type) {
852 FormStructure form_structure(form);
854 if (!ShouldUploadForm(form_structure))
857 if (!form_structure.ShouldBeCrowdsourced())
860 // Find the first password field to label. We don't try to label anything
862 bool found_password_field = false;
863 for (size_t i = 0; i < form_structure.field_count(); ++i) {
864 AutofillField* field = form_structure.field(i);
866 ServerFieldTypeSet types;
867 if (!found_password_field && field->form_control_type == "password") {
868 types.insert(password_type);
869 found_password_field = true;
871 types.insert(UNKNOWN_TYPE);
873 field->set_possible_types(types);
875 DCHECK(found_password_field);
877 // Only one field type should be present.
878 ServerFieldTypeSet available_field_types;
879 available_field_types.insert(password_type);
881 // Force uploading as these events are relatively rare and we want to make
882 // sure to receive them. It also makes testing easier if these requests
884 form_structure.set_upload_required(UPLOAD_REQUIRED);
886 if (!download_manager_)
889 return download_manager_->StartUploadRequest(form_structure,
890 false /* was_autofilled */,
891 available_field_types);
894 void AutofillManager::Reset() {
895 form_structures_.clear();
896 has_logged_autofill_enabled_ = false;
897 has_logged_address_suggestions_count_ = false;
898 did_show_suggestions_ = false;
899 user_did_type_ = false;
900 user_did_autofill_ = false;
901 user_did_edit_autofilled_field_ = false;
902 forms_loaded_timestamps_.clear();
903 initial_interaction_timestamp_ = TimeTicks();
904 external_delegate_->Reset();
907 AutofillManager::AutofillManager(AutofillDriver* driver,
908 AutofillClient* client,
909 PersonalDataManager* personal_data)
912 app_locale_("en-US"),
913 personal_data_(personal_data),
914 autocomplete_history_manager_(
915 new AutocompleteHistoryManager(driver, client)),
916 metric_logger_(new AutofillMetrics),
917 has_logged_autofill_enabled_(false),
918 has_logged_address_suggestions_count_(false),
919 did_show_suggestions_(false),
920 user_did_type_(false),
921 user_did_autofill_(false),
922 user_did_edit_autofilled_field_(false),
923 external_delegate_(NULL),
924 test_delegate_(NULL),
925 weak_ptr_factory_(this) {
930 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
931 metric_logger_.reset(metric_logger);
934 bool AutofillManager::RefreshDataModels() const {
935 if (!IsAutofillEnabled())
938 // No autofill data to return if the profiles are empty.
939 if (personal_data_->GetProfiles().empty() &&
940 personal_data_->GetCreditCards().empty()) {
947 bool AutofillManager::GetProfileOrCreditCard(
949 const AutofillDataModel** data_model,
951 bool* is_credit_card) const {
952 // Unpack the |unique_id| into component parts.
953 GUIDPair credit_card_guid;
954 GUIDPair profile_guid;
955 UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
956 DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
957 !base::IsValidGUID(profile_guid.first));
958 *is_credit_card = false;
960 // Find the profile that matches the |profile_guid|, if one is specified.
961 // Otherwise find the credit card that matches the |credit_card_guid|,
963 if (base::IsValidGUID(profile_guid.first)) {
964 *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
965 *variant = profile_guid.second;
966 } else if (base::IsValidGUID(credit_card_guid.first)) {
967 *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
968 *variant = credit_card_guid.second;
969 *is_credit_card = true;
972 return !!*data_model;
975 bool AutofillManager::FindCachedForm(const FormData& form,
976 FormStructure** form_structure) const {
977 // Find the FormStructure that corresponds to |form|.
978 // Scan backward through the cached |form_structures_|, as updated versions of
979 // forms are added to the back of the list, whereas original versions of these
980 // forms might appear toward the beginning of the list. The communication
981 // protocol with the crowdsourcing server does not permit us to discard the
982 // original versions of the forms.
983 *form_structure = NULL;
984 for (std::vector<FormStructure*>::const_reverse_iterator iter =
985 form_structures_.rbegin();
986 iter != form_structures_.rend(); ++iter) {
987 if (**iter == form) {
988 *form_structure = *iter;
990 // The same form might be cached with multiple field counts: in some
991 // cases, non-autofillable fields are filtered out, whereas in other cases
992 // they are not. To avoid thrashing the cache, keep scanning until we
993 // find a cached version with the same number of fields, if there is one.
994 if ((*iter)->field_count() == form.fields.size())
999 if (!(*form_structure))
1005 bool AutofillManager::GetCachedFormAndField(const FormData& form,
1006 const FormFieldData& field,
1007 FormStructure** form_structure,
1008 AutofillField** autofill_field) {
1009 // Find the FormStructure that corresponds to |form|.
1010 // If we do not have this form in our cache but it is parseable, we'll add it
1011 // in the call to |UpdateCachedForm()|.
1012 if (!FindCachedForm(form, form_structure) &&
1013 !FormStructure(form).ShouldBeParsed()) {
1017 // Update the cached form to reflect any dynamic changes to the form data, if
1019 if (!UpdateCachedForm(form, *form_structure, form_structure))
1022 // No data to return if there are no auto-fillable fields.
1023 if (!(*form_structure)->autofill_count())
1026 // Find the AutofillField that corresponds to |field|.
1027 *autofill_field = NULL;
1028 for (std::vector<AutofillField*>::const_iterator iter =
1029 (*form_structure)->begin();
1030 iter != (*form_structure)->end(); ++iter) {
1031 if ((**iter) == field) {
1032 *autofill_field = *iter;
1037 // Even though we always update the cache, the field might not exist if the
1038 // website disables autocomplete while the user is interacting with the form.
1039 // See http://crbug.com/160476
1040 return *autofill_field != NULL;
1043 bool AutofillManager::UpdateCachedForm(const FormData& live_form,
1044 const FormStructure* cached_form,
1045 FormStructure** updated_form) {
1048 live_form.fields.size() != cached_form->field_count());
1049 for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
1050 needs_update = *cached_form->field(i) != live_form.fields[i];
1056 if (form_structures_.size() >= kMaxFormCacheSize)
1059 // Add the new or updated form to our cache.
1060 form_structures_.push_back(new FormStructure(live_form));
1061 *updated_form = *form_structures_.rbegin();
1062 (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
1064 // If we have cached data, propagate it to the updated form.
1066 std::map<base::string16, const AutofillField*> cached_fields;
1067 for (size_t i = 0; i < cached_form->field_count(); ++i) {
1068 const AutofillField* field = cached_form->field(i);
1069 cached_fields[field->unique_name()] = field;
1072 for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
1073 AutofillField* field = (*updated_form)->field(i);
1074 std::map<base::string16, const AutofillField*>::iterator cached_field =
1075 cached_fields.find(field->unique_name());
1076 if (cached_field != cached_fields.end()) {
1077 field->set_server_type(cached_field->second->server_type());
1078 field->is_autofilled = cached_field->second->is_autofilled;
1082 // Note: We _must not_ remove the original version of the cached form from
1083 // the list of |form_structures_|. Otherwise, we break parsing of the
1084 // crowdsourcing server's response to our query.
1087 // Annotate the updated form with its predicted types.
1088 std::vector<FormStructure*> forms(1, *updated_form);
1089 driver_->SendAutofillTypePredictionsToRenderer(forms);
1094 void AutofillManager::GetProfileSuggestions(
1095 const FormStructure& form,
1096 const FormFieldData& field,
1097 const AutofillField& autofill_field,
1098 std::vector<base::string16>* values,
1099 std::vector<base::string16>* labels,
1100 std::vector<base::string16>* icons,
1101 std::vector<int>* unique_ids) const {
1102 std::vector<ServerFieldType> field_types(form.field_count());
1103 for (size_t i = 0; i < form.field_count(); ++i) {
1104 field_types.push_back(form.field(i)->Type().GetStorableType());
1106 std::vector<GUIDPair> guid_pairs;
1108 personal_data_->GetProfileSuggestions(
1109 autofill_field.Type(), field.value, field.is_autofilled, field_types,
1110 base::Callback<bool(const AutofillProfile&)>(),
1111 values, labels, icons, &guid_pairs);
1113 // Adjust phone number to display in prefix/suffix case.
1114 if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) {
1115 for (size_t i = 0; i < values->size(); ++i) {
1116 (*values)[i] = AutofillField::GetPhoneNumberValue(
1117 autofill_field, (*values)[i], field);
1121 for (size_t i = 0; i < guid_pairs.size(); ++i) {
1122 unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1127 void AutofillManager::GetCreditCardSuggestions(
1128 const FormFieldData& field,
1129 const AutofillType& type,
1130 std::vector<base::string16>* values,
1131 std::vector<base::string16>* labels,
1132 std::vector<base::string16>* icons,
1133 std::vector<int>* unique_ids) const {
1134 std::vector<GUIDPair> guid_pairs;
1135 personal_data_->GetCreditCardSuggestions(
1136 type, field.value, values, labels, icons, &guid_pairs);
1138 for (size_t i = 0; i < guid_pairs.size(); ++i) {
1139 unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1143 void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1144 std::vector<FormStructure*> non_queryable_forms;
1145 for (std::vector<FormData>::const_iterator iter = forms.begin();
1146 iter != forms.end(); ++iter) {
1147 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter));
1148 if (!form_structure->ShouldBeParsed())
1151 form_structure->DetermineHeuristicTypes(*metric_logger_);
1153 // Set aside forms with method GET or author-specified types, so that they
1154 // are not included in the query to the server.
1155 if (form_structure->ShouldBeCrowdsourced())
1156 form_structures_.push_back(form_structure.release());
1158 non_queryable_forms.push_back(form_structure.release());
1161 if (!form_structures_.empty() && download_manager_) {
1162 // Query the server if we have at least one of the forms were parsed.
1163 download_manager_->StartQueryRequest(form_structures_.get(),
1167 for (std::vector<FormStructure*>::const_iterator iter =
1168 non_queryable_forms.begin();
1169 iter != non_queryable_forms.end(); ++iter) {
1170 form_structures_.push_back(*iter);
1173 if (!form_structures_.empty())
1174 metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1176 // For the |non_queryable_forms|, we have all the field type info we're ever
1177 // going to get about them. For the other forms, we'll wait until we get a
1178 // response from the server.
1179 driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1182 int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1183 if (!base::IsValidGUID(guid.first))
1186 std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1187 if (iter == guid_id_map_.end()) {
1188 int id = guid_id_map_.size() + 1;
1189 guid_id_map_[guid] = id;
1190 id_guid_map_[id] = guid;
1193 return iter->second;
1197 const GUIDPair AutofillManager::IDToGUID(int id) const {
1199 return GUIDPair(std::string(), 0);
1201 std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1202 if (iter == id_guid_map_.end()) {
1204 return GUIDPair(std::string(), 0);
1207 return iter->second;
1210 // When sending IDs (across processes) to the renderer we pack credit card and
1211 // profile IDs into a single integer. Credit card IDs are sent in the high
1212 // word and profile IDs are sent in the low word.
1213 int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1214 const GUIDPair& profile_guid) const {
1215 int cc_id = GUIDToID(cc_guid);
1216 int profile_id = GUIDToID(profile_guid);
1218 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1219 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1221 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1224 // When receiving IDs (across processes) from the renderer we unpack credit card
1225 // and profile IDs from a single integer. Credit card IDs are stored in the
1226 // high word and profile IDs are stored in the low word.
1227 void AutofillManager::UnpackGUIDs(int id,
1229 GUIDPair* profile_guid) const {
1230 int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1231 std::numeric_limits<unsigned short>::max();
1232 int profile_id = id & std::numeric_limits<unsigned short>::max();
1234 *cc_guid = IDToGUID(cc_id);
1235 *profile_guid = IDToGUID(profile_id);
1238 void AutofillManager::UpdateInitialInteractionTimestamp(
1239 const TimeTicks& interaction_timestamp) {
1240 if (initial_interaction_timestamp_.is_null() ||
1241 interaction_timestamp < initial_interaction_timestamp_) {
1242 initial_interaction_timestamp_ = interaction_timestamp;
1246 bool AutofillManager::ShouldUploadForm(const FormStructure& form) {
1247 if (!IsAutofillEnabled())
1250 if (driver_->IsOffTheRecord())
1253 // Disregard forms that we wouldn't ever autofill in the first place.
1254 if (!form.ShouldBeParsed())
1260 } // namespace autofill