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_data_model.h"
25 #include "components/autofill/core/browser/autofill_driver.h"
26 #include "components/autofill/core/browser/autofill_external_delegate.h"
27 #include "components/autofill/core/browser/autofill_field.h"
28 #include "components/autofill/core/browser/autofill_manager_delegate.h"
29 #include "components/autofill/core/browser/autofill_manager_test_delegate.h"
30 #include "components/autofill/core/browser/autofill_metrics.h"
31 #include "components/autofill/core/browser/autofill_profile.h"
32 #include "components/autofill/core/browser/autofill_type.h"
33 #include "components/autofill/core/browser/credit_card.h"
34 #include "components/autofill/core/browser/form_structure.h"
35 #include "components/autofill/core/browser/personal_data_manager.h"
36 #include "components/autofill/core/browser/phone_number.h"
37 #include "components/autofill/core/browser/phone_number_i18n.h"
38 #include "components/autofill/core/common/autofill_messages.h"
39 #include "components/autofill/core/common/autofill_pref_names.h"
40 #include "components/autofill/core/common/autofill_switches.h"
41 #include "components/autofill/core/common/form_data.h"
42 #include "components/autofill/core/common/form_data_predictions.h"
43 #include "components/autofill/core/common/form_field_data.h"
44 #include "components/autofill/core/common/password_form_fill_data.h"
45 #include "components/user_prefs/pref_registry_syncable.h"
46 #include "content/public/browser/browser_context.h"
47 #include "content/public/browser/render_view_host.h"
48 #include "content/public/browser/web_contents.h"
49 #include "grit/component_strings.h"
50 #include "third_party/WebKit/public/web/WebAutofillClient.h"
51 #include "ui/base/l10n/l10n_util.h"
52 #include "ui/gfx/rect.h"
57 typedef PersonalDataManager::GUIDPair GUIDPair;
59 using base::TimeTicks;
60 using content::RenderViewHost;
61 using WebKit::WebFormElement;
65 // We only send a fraction of the forms to upload server.
66 // The rate for positive/negative matches potentially could be different.
67 const double kAutofillPositiveUploadRateDefaultValue = 0.20;
68 const double kAutofillNegativeUploadRateDefaultValue = 0.20;
70 const size_t kMaxRecentFormSignaturesToRemember = 3;
72 // Set a conservative upper bound on the number of forms we are willing to
73 // cache, simply to prevent unbounded memory consumption.
74 const size_t kMaxFormCacheSize = 100;
76 // Removes duplicate suggestions whilst preserving their original order.
77 void RemoveDuplicateSuggestions(std::vector<base::string16>* values,
78 std::vector<base::string16>* labels,
79 std::vector<base::string16>* icons,
80 std::vector<int>* unique_ids) {
81 DCHECK_EQ(values->size(), labels->size());
82 DCHECK_EQ(values->size(), icons->size());
83 DCHECK_EQ(values->size(), unique_ids->size());
85 std::set<std::pair<base::string16, base::string16> > seen_suggestions;
86 std::vector<base::string16> values_copy;
87 std::vector<base::string16> labels_copy;
88 std::vector<base::string16> icons_copy;
89 std::vector<int> unique_ids_copy;
91 for (size_t i = 0; i < values->size(); ++i) {
92 const std::pair<base::string16, base::string16> suggestion(
93 (*values)[i], (*labels)[i]);
94 if (seen_suggestions.insert(suggestion).second) {
95 values_copy.push_back((*values)[i]);
96 labels_copy.push_back((*labels)[i]);
97 icons_copy.push_back((*icons)[i]);
98 unique_ids_copy.push_back((*unique_ids)[i]);
102 values->swap(values_copy);
103 labels->swap(labels_copy);
104 icons->swap(icons_copy);
105 unique_ids->swap(unique_ids_copy);
108 // Precondition: |form_structure| and |form| should correspond to the same
109 // logical form. Returns true if any field in the given |section| within |form|
111 bool SectionIsAutofilled(const FormStructure& form_structure,
112 const FormData& form,
113 const std::string& section) {
114 DCHECK_EQ(form_structure.field_count(), form.fields.size());
115 for (size_t i = 0; i < form_structure.field_count(); ++i) {
116 if (form_structure.field(i)->section() == section &&
117 form.fields[i].is_autofilled) {
125 bool FormIsHTTPS(const FormStructure& form) {
126 // TODO(blundell): Change this to use a constant once crbug.com/306258 is
128 return form.source_url().SchemeIs("https");
131 // Uses the existing personal data in |profiles| and |credit_cards| to determine
132 // possible field types for the |submitted_form|. This is potentially
133 // expensive -- on the order of 50ms even for a small set of |stored_data|.
134 // Hence, it should not run on the UI thread -- to avoid locking up the UI --
135 // nor on the IO thread -- to avoid blocking IPC calls.
136 void DeterminePossibleFieldTypesForUpload(
137 const std::vector<AutofillProfile>& profiles,
138 const std::vector<CreditCard>& credit_cards,
139 const std::string& app_locale,
140 FormStructure* submitted_form) {
141 // For each field in the |submitted_form|, extract the value. Then for each
142 // profile or credit card, identify any stored types that match the value.
143 for (size_t i = 0; i < submitted_form->field_count(); ++i) {
144 AutofillField* field = submitted_form->field(i);
145 ServerFieldTypeSet matching_types;
147 // If it's a password field, set the type directly.
148 if (field->form_control_type == "password") {
149 matching_types.insert(autofill::PASSWORD);
151 base::string16 value = CollapseWhitespace(field->value, false);
152 for (std::vector<AutofillProfile>::const_iterator it = profiles.begin();
153 it != profiles.end(); ++it) {
154 it->GetMatchingTypes(value, app_locale, &matching_types);
156 for (std::vector<CreditCard>::const_iterator it = credit_cards.begin();
157 it != credit_cards.end(); ++it) {
158 it->GetMatchingTypes(value, app_locale, &matching_types);
162 if (matching_types.empty())
163 matching_types.insert(UNKNOWN_TYPE);
165 field->set_possible_types(matching_types);
169 // Returns true if server returned known field types to one or more fields in
171 bool HasServerSpecifiedFieldTypes(const FormStructure& form_structure) {
172 for (size_t i = 0; i < form_structure.field_count(); ++i) {
173 if (form_structure.field(i)->server_type() != NO_SERVER_DATA)
181 AutofillManager::AutofillManager(
182 AutofillDriver* driver,
183 autofill::AutofillManagerDelegate* delegate,
184 const std::string& app_locale,
185 AutofillDownloadManagerState enable_download_manager)
187 manager_delegate_(delegate),
188 app_locale_(app_locale),
189 personal_data_(delegate->GetPersonalDataManager()),
190 autocomplete_history_manager_(
191 new AutocompleteHistoryManager(driver, delegate)),
192 metric_logger_(new AutofillMetrics),
193 has_logged_autofill_enabled_(false),
194 has_logged_address_suggestions_count_(false),
195 did_show_suggestions_(false),
196 user_did_type_(false),
197 user_did_autofill_(false),
198 user_did_edit_autofilled_field_(false),
199 external_delegate_(NULL),
200 test_delegate_(NULL),
201 weak_ptr_factory_(this) {
202 if (enable_download_manager == ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {
203 download_manager_.reset(
204 new AutofillDownloadManager(
205 driver->GetWebContents()->GetBrowserContext(), this));
209 AutofillManager::~AutofillManager() {}
212 void AutofillManager::RegisterProfilePrefs(
213 user_prefs::PrefRegistrySyncable* registry) {
214 registry->RegisterBooleanPref(
215 prefs::kAutofillEnabled,
217 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
218 #if defined(OS_MACOSX) || defined(OS_ANDROID)
219 registry->RegisterBooleanPref(
220 prefs::kAutofillAuxiliaryProfilesEnabled,
222 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
224 registry->RegisterBooleanPref(
225 prefs::kAutofillAuxiliaryProfilesEnabled,
227 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
229 registry->RegisterDoublePref(
230 prefs::kAutofillPositiveUploadRate,
231 kAutofillPositiveUploadRateDefaultValue,
232 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
233 registry->RegisterDoublePref(
234 prefs::kAutofillNegativeUploadRate,
235 kAutofillNegativeUploadRateDefaultValue,
236 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
239 void AutofillManager::SetExternalDelegate(AutofillExternalDelegate* delegate) {
240 // TODO(jrg): consider passing delegate into the ctor. That won't
241 // work if the delegate has a pointer to the AutofillManager, but
242 // future directions may not need such a pointer.
243 external_delegate_ = delegate;
244 autocomplete_history_manager_->SetExternalDelegate(delegate);
247 bool AutofillManager::OnFormSubmitted(const FormData& form,
248 const TimeTicks& timestamp) {
249 // Let Autocomplete know as well.
250 autocomplete_history_manager_->OnFormSubmitted(form);
252 // Grab a copy of the form data.
253 scoped_ptr<FormStructure> submitted_form(new FormStructure(form));
255 if (!ShouldUploadForm(*submitted_form))
258 // Don't save data that was submitted through JavaScript.
259 if (!form.user_submitted)
262 // Ignore forms not present in our cache. These are typically forms with
263 // wonky JavaScript that also makes them not auto-fillable.
264 FormStructure* cached_submitted_form;
265 if (!FindCachedForm(form, &cached_submitted_form))
268 submitted_form->UpdateFromCache(*cached_submitted_form);
269 if (submitted_form->IsAutofillable(true))
270 ImportFormData(*submitted_form);
272 // Only upload server statistics and UMA metrics if at least some local data
273 // is available to use as a baseline.
274 const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
275 const std::vector<CreditCard*>& credit_cards =
276 personal_data_->GetCreditCards();
277 if (!profiles.empty() || !credit_cards.empty()) {
278 // Copy the profile and credit card data, so that it can be accessed on a
280 std::vector<AutofillProfile> copied_profiles;
281 copied_profiles.reserve(profiles.size());
282 for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin();
283 it != profiles.end(); ++it) {
284 copied_profiles.push_back(**it);
287 std::vector<CreditCard> copied_credit_cards;
288 copied_credit_cards.reserve(credit_cards.size());
289 for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin();
290 it != credit_cards.end(); ++it) {
291 copied_credit_cards.push_back(**it);
294 // Note that ownership of |submitted_form| is passed to the second task,
295 // using |base::Owned|.
296 FormStructure* raw_submitted_form = submitted_form.get();
297 driver_->GetBlockingPool()->PostTaskAndReply(
299 base::Bind(&DeterminePossibleFieldTypesForUpload,
304 base::Bind(&AutofillManager::UploadFormDataAsyncCallback,
305 weak_ptr_factory_.GetWeakPtr(),
306 base::Owned(submitted_form.release()),
307 forms_loaded_timestamp_,
308 initial_interaction_timestamp_,
315 void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms,
316 const TimeTicks& timestamp,
317 autofill::FormsSeenState state) {
318 bool is_post_document_load = state == autofill::DYNAMIC_FORMS_SEEN;
319 // If new forms were added dynamically, treat as a new page.
320 if (is_post_document_load)
323 RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost();
327 bool enabled = IsAutofillEnabled();
328 if (!has_logged_autofill_enabled_) {
329 metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled);
330 has_logged_autofill_enabled_ = true;
336 forms_loaded_timestamp_ = timestamp;
340 void AutofillManager::OnTextFieldDidChange(const FormData& form,
341 const FormFieldData& field,
342 const TimeTicks& timestamp) {
343 FormStructure* form_structure = NULL;
344 AutofillField* autofill_field = NULL;
345 if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
348 if (!user_did_type_) {
349 user_did_type_ = true;
350 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE);
353 if (autofill_field->is_autofilled) {
354 autofill_field->is_autofilled = false;
355 metric_logger_->LogUserHappinessMetric(
356 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD);
358 if (!user_did_edit_autofilled_field_) {
359 user_did_edit_autofilled_field_ = true;
360 metric_logger_->LogUserHappinessMetric(
361 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE);
365 UpdateInitialInteractionTimestamp(timestamp);
368 void AutofillManager::OnQueryFormFieldAutofill(int query_id,
369 const FormData& form,
370 const FormFieldData& field,
371 const gfx::RectF& bounding_box,
372 bool display_warning) {
373 std::vector<base::string16> values;
374 std::vector<base::string16> labels;
375 std::vector<base::string16> icons;
376 std::vector<int> unique_ids;
378 external_delegate_->OnQuery(query_id,
384 RenderViewHost* host = NULL;
385 FormStructure* form_structure = NULL;
386 AutofillField* autofill_field = NULL;
387 if (GetHost(&host) &&
388 GetCachedFormAndField(form, field, &form_structure, &autofill_field) &&
389 // Don't send suggestions for forms that aren't auto-fillable.
390 form_structure->IsAutofillable(false)) {
391 AutofillType type = autofill_field->Type();
392 bool is_filling_credit_card = (type.group() == CREDIT_CARD);
393 if (is_filling_credit_card) {
394 GetCreditCardSuggestions(
395 field, type, &values, &labels, &icons, &unique_ids);
397 GetProfileSuggestions(
398 form_structure, field, type, &values, &labels, &icons, &unique_ids);
401 DCHECK_EQ(values.size(), labels.size());
402 DCHECK_EQ(values.size(), icons.size());
403 DCHECK_EQ(values.size(), unique_ids.size());
405 if (!values.empty()) {
406 // Don't provide Autofill suggestions when Autofill is disabled, and don't
407 // provide credit card suggestions for non-HTTPS pages. However, provide a
408 // warning to the user in these cases.
410 if (!form_structure->IsAutofillable(true))
411 warning = IDS_AUTOFILL_WARNING_FORM_DISABLED;
412 else if (is_filling_credit_card && !FormIsHTTPS(*form_structure))
413 warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION;
415 values.assign(1, l10n_util::GetStringUTF16(warning));
416 labels.assign(1, base::string16());
417 icons.assign(1, base::string16());
419 WebKit::WebAutofillClient::MenuItemIDWarningMessage);
421 bool section_is_autofilled =
422 SectionIsAutofilled(*form_structure, form,
423 autofill_field->section());
424 if (section_is_autofilled) {
425 // If the relevant section is auto-filled and the renderer is querying
426 // for suggestions, then the user is editing the value of a field.
427 // In this case, mimic autocomplete: don't display labels or icons,
428 // as that information is redundant.
429 labels.assign(labels.size(), base::string16());
430 icons.assign(icons.size(), base::string16());
433 // When filling credit card suggestions, the values and labels are
434 // typically obfuscated, which makes detecting duplicates hard. Since
435 // duplicates only tend to be a problem when filling address forms
436 // anyway, only don't de-dup credit card suggestions.
437 if (!is_filling_credit_card)
438 RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
440 // The first time we show suggestions on this page, log the number of
441 // suggestions shown.
442 if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
443 metric_logger_->LogAddressSuggestionsCount(values.size());
444 has_logged_address_suggestions_count_ = true;
450 // Add the results from AutoComplete. They come back asynchronously, so we
451 // hand off what we generated and they will send the results back to the
453 autocomplete_history_manager_->OnGetAutocompleteSuggestions(
454 query_id, field.name, field.value, values, labels, icons, unique_ids);
457 void AutofillManager::OnFillAutofillFormData(int query_id,
458 const FormData& form,
459 const FormFieldData& field,
461 RenderViewHost* host = NULL;
462 const AutofillDataModel* data_model = NULL;
464 FormStructure* form_structure = NULL;
465 AutofillField* autofill_field = NULL;
466 // NOTE: GetHost may invalidate |data_model| because it causes the
467 // PersonalDataManager to reload Mac address book entries. Thus it must
468 // come before GetProfileOrCreditCard.
469 if (!GetHost(&host) ||
470 !GetProfileOrCreditCard(unique_id, &data_model, &variant) ||
471 !GetCachedFormAndField(form, field, &form_structure, &autofill_field))
475 DCHECK(form_structure);
476 DCHECK(autofill_field);
478 FormData result = form;
480 // If the relevant section is auto-filled, we should fill |field| but not the
482 if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) {
483 for (std::vector<FormFieldData>::iterator iter = result.fields.begin();
484 iter != result.fields.end(); ++iter) {
485 if ((*iter) == field) {
486 base::string16 value = data_model->GetInfoForVariant(
487 autofill_field->Type(), variant, app_locale_);
488 AutofillField::FillFormField(*autofill_field, value, app_locale_,
490 // Mark the cached field as autofilled, so that we can detect when a
491 // user edits an autofilled field (for metrics).
492 autofill_field->is_autofilled = true;
497 driver_->SendFormDataToRenderer(query_id, result);
501 // Cache the field type for the field from which the user initiated autofill.
502 FieldTypeGroup initiating_group_type = autofill_field->Type().group();
503 DCHECK_EQ(form_structure->field_count(), form.fields.size());
504 for (size_t i = 0; i < form_structure->field_count(); ++i) {
505 if (form_structure->field(i)->section() != autofill_field->section())
508 DCHECK_EQ(*form_structure->field(i), result.fields[i]);
510 const AutofillField* cached_field = form_structure->field(i);
511 FieldTypeGroup field_group_type = cached_field->Type().group();
512 if (field_group_type != NO_GROUP) {
513 // If the field being filled is either
514 // (a) the field that the user initiated the fill from, or
515 // (b) part of the same logical unit, e.g. name or phone number,
516 // then take the multi-profile "variant" into account.
517 // Otherwise fill with the default (zeroth) variant.
518 size_t use_variant = 0;
519 if (result.fields[i] == field ||
520 field_group_type == initiating_group_type) {
521 use_variant = variant;
523 base::string16 value = data_model->GetInfoForVariant(
524 cached_field->Type(), use_variant, app_locale_);
525 AutofillField::FillFormField(*cached_field, value, app_locale_,
527 // Mark the cached field as autofilled, so that we can detect when a user
528 // edits an autofilled field (for metrics).
529 form_structure->field(i)->is_autofilled = true;
533 autofilled_form_signatures_.push_front(form_structure->FormSignature());
534 // Only remember the last few forms that we've seen, both to avoid false
535 // positives and to avoid wasting memory.
536 if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember)
537 autofilled_form_signatures_.pop_back();
539 driver_->SendFormDataToRenderer(query_id, result);
542 void AutofillManager::OnShowAutofillDialog() {
543 manager_delegate_->ShowAutofillSettings();
546 void AutofillManager::OnDidPreviewAutofillFormData() {
548 test_delegate_->DidPreviewFormData();
551 void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) {
553 test_delegate_->DidFillFormData();
555 metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL);
556 if (!user_did_autofill_) {
557 user_did_autofill_ = true;
558 metric_logger_->LogUserHappinessMetric(
559 AutofillMetrics::USER_DID_AUTOFILL_ONCE);
562 UpdateInitialInteractionTimestamp(timestamp);
565 void AutofillManager::OnDidShowAutofillSuggestions(bool is_new_popup) {
567 test_delegate_->DidShowSuggestions();
570 metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN);
572 if (!did_show_suggestions_) {
573 did_show_suggestions_ = true;
574 metric_logger_->LogUserHappinessMetric(
575 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE);
580 void AutofillManager::OnHideAutofillUI() {
581 if (!IsAutofillEnabled())
584 manager_delegate_->HideAutofillPopup();
587 void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) {
588 const AutofillDataModel* data_model = NULL;
590 if (!GetProfileOrCreditCard(unique_id, &data_model, &variant)) {
595 // TODO(csharp): If we are dealing with a variant only the variant should
596 // be deleted, instead of doing nothing.
597 // http://crbug.com/124211
601 personal_data_->RemoveByGUID(data_model->guid());
604 void AutofillManager::RemoveAutocompleteEntry(const base::string16& name,
605 const base::string16& value) {
606 autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value);
609 const std::vector<FormStructure*>& AutofillManager::GetFormStructures() {
610 return form_structures_.get();
613 void AutofillManager::SetTestDelegate(
614 autofill::AutofillManagerTestDelegate* delegate) {
615 test_delegate_ = delegate;
618 void AutofillManager::OnAddPasswordFormMapping(
619 const FormFieldData& form,
620 const PasswordFormFillData& fill_data) {
621 external_delegate_->AddPasswordFormMapping(form, fill_data);
624 void AutofillManager::OnShowPasswordSuggestions(
625 const FormFieldData& field,
626 const gfx::RectF& bounds,
627 const std::vector<base::string16>& suggestions,
628 const std::vector<base::string16>& realms) {
629 external_delegate_->OnShowPasswordSuggestions(suggestions,
635 void AutofillManager::OnSetDataList(const std::vector<base::string16>& values,
636 const std::vector<base::string16>& labels) {
637 if (values.size() != labels.size())
640 external_delegate_->SetCurrentDataListValues(values, labels);
643 void AutofillManager::OnRequestAutocomplete(
644 const FormData& form,
645 const GURL& frame_url) {
646 if (!IsAutofillEnabled()) {
647 ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorDisabled,
652 base::Callback<void(const FormStructure*)> callback =
653 base::Bind(&AutofillManager::ReturnAutocompleteData,
654 weak_ptr_factory_.GetWeakPtr());
655 ShowRequestAutocompleteDialog(form, frame_url, callback);
658 void AutofillManager::ReturnAutocompleteResult(
659 WebFormElement::AutocompleteResult result, const FormData& form_data) {
660 // driver_->GetWebContents() will be NULL when the interactive autocomplete
661 // is closed due to a tab or browser window closing.
662 if (!driver_->GetWebContents())
665 RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost();
669 host->Send(new AutofillMsg_RequestAutocompleteResult(host->GetRoutingID(),
674 void AutofillManager::ReturnAutocompleteData(const FormStructure* result) {
676 ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorCancel,
679 ReturnAutocompleteResult(WebFormElement::AutocompleteResultSuccess,
680 result->ToFormData());
684 void AutofillManager::OnLoadedServerPredictions(
685 const std::string& response_xml) {
686 // Parse and store the server predictions.
687 FormStructure::ParseQueryResponse(response_xml,
688 form_structures_.get(),
691 // Forward form structures to the password generation manager to detect
692 // account creation forms.
693 manager_delegate_->DetectAccountCreationForms(form_structures_.get());
695 // If the corresponding flag is set, annotate forms with the predicted types.
696 driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
699 void AutofillManager::OnDidEndTextFieldEditing() {
700 external_delegate_->DidEndTextFieldEditing();
703 bool AutofillManager::IsAutofillEnabled() const {
704 return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
707 void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
708 const CreditCard* imported_credit_card;
709 if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
712 // If credit card information was submitted, we need to confirm whether to
714 if (imported_credit_card) {
715 manager_delegate_->ConfirmSaveCreditCard(
717 *imported_credit_card,
719 base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard),
720 base::Unretained(personal_data_), *imported_credit_card));
724 // Note that |submitted_form| is passed as a pointer rather than as a reference
725 // so that we can get memory management right across threads. Note also that we
726 // explicitly pass in all the time stamps of interest, as the cached ones might
727 // get reset before this method executes.
728 void AutofillManager::UploadFormDataAsyncCallback(
729 const FormStructure* submitted_form,
730 const TimeTicks& load_time,
731 const TimeTicks& interaction_time,
732 const TimeTicks& submission_time) {
733 submitted_form->LogQualityMetrics(*metric_logger_,
738 if (submitted_form->ShouldBeCrowdsourced())
739 UploadFormData(*submitted_form);
742 void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
743 if (!download_manager_)
746 // Check if the form is among the forms that were recently auto-filled.
747 bool was_autofilled = false;
748 std::string form_signature = submitted_form.FormSignature();
749 for (std::list<std::string>::const_iterator it =
750 autofilled_form_signatures_.begin();
751 it != autofilled_form_signatures_.end() && !was_autofilled;
753 if (*it == form_signature)
754 was_autofilled = true;
757 ServerFieldTypeSet non_empty_types;
758 personal_data_->GetNonEmptyTypes(&non_empty_types);
759 // Always add PASSWORD to |non_empty_types| so that if |submitted_form|
760 // contains a password field it will be uploaded to the server. If
761 // |submitted_form| doesn't contain a password field, there is no side
762 // effect from adding PASSWORD to |non_empty_types|.
763 non_empty_types.insert(autofill::PASSWORD);
765 download_manager_->StartUploadRequest(submitted_form, was_autofilled,
769 bool AutofillManager::UploadPasswordGenerationForm(const FormData& form) {
770 FormStructure form_structure(form);
772 if (!ShouldUploadForm(form_structure))
775 if (!form_structure.ShouldBeCrowdsourced())
778 // TODO(gcasto): Check that PasswordGeneration is enabled?
780 // Find the first password field to label. We don't try to label anything
782 bool found_password_field = false;
783 for (size_t i = 0; i < form_structure.field_count(); ++i) {
784 AutofillField* field = form_structure.field(i);
786 ServerFieldTypeSet types;
787 if (!found_password_field && field->form_control_type == "password") {
788 types.insert(ACCOUNT_CREATION_PASSWORD);
789 found_password_field = true;
791 types.insert(UNKNOWN_TYPE);
793 field->set_possible_types(types);
795 DCHECK(found_password_field);
797 // Only one field type should be present.
798 ServerFieldTypeSet available_field_types;
799 available_field_types.insert(ACCOUNT_CREATION_PASSWORD);
801 // Force uploading as these events are relatively rare and we want to make
802 // sure to receive them. It also makes testing easier if these requests
804 form_structure.set_upload_required(UPLOAD_REQUIRED);
806 if (!download_manager_)
809 return download_manager_->StartUploadRequest(form_structure,
810 false /* was_autofilled */,
811 available_field_types);
814 void AutofillManager::Reset() {
815 form_structures_.clear();
816 has_logged_autofill_enabled_ = false;
817 has_logged_address_suggestions_count_ = false;
818 did_show_suggestions_ = false;
819 user_did_type_ = false;
820 user_did_autofill_ = false;
821 user_did_edit_autofilled_field_ = false;
822 forms_loaded_timestamp_ = TimeTicks();
823 initial_interaction_timestamp_ = TimeTicks();
824 external_delegate_->Reset();
827 AutofillManager::AutofillManager(AutofillDriver* driver,
828 autofill::AutofillManagerDelegate* delegate,
829 PersonalDataManager* personal_data)
831 manager_delegate_(delegate),
832 app_locale_("en-US"),
833 personal_data_(personal_data),
834 autocomplete_history_manager_(
835 new AutocompleteHistoryManager(driver, delegate)),
836 metric_logger_(new AutofillMetrics),
837 has_logged_autofill_enabled_(false),
838 has_logged_address_suggestions_count_(false),
839 did_show_suggestions_(false),
840 user_did_type_(false),
841 user_did_autofill_(false),
842 user_did_edit_autofilled_field_(false),
843 external_delegate_(NULL),
844 test_delegate_(NULL),
845 weak_ptr_factory_(this) {
847 DCHECK(driver_->GetWebContents());
848 DCHECK(manager_delegate_);
851 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
852 metric_logger_.reset(metric_logger);
855 bool AutofillManager::GetHost(RenderViewHost** host) const {
856 if (!IsAutofillEnabled())
859 // No autofill data to return if the profiles are empty.
860 if (personal_data_->GetProfiles().empty() &&
861 personal_data_->GetCreditCards().empty()) {
865 if (!driver_->RendererIsAvailable())
868 *host = driver_->GetWebContents()->GetRenderViewHost();
872 bool AutofillManager::GetProfileOrCreditCard(
874 const AutofillDataModel** data_model,
875 size_t* variant) const {
876 // Unpack the |unique_id| into component parts.
877 GUIDPair credit_card_guid;
878 GUIDPair profile_guid;
879 UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
880 DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
881 !base::IsValidGUID(profile_guid.first));
883 // Find the profile that matches the |profile_guid|, if one is specified.
884 // Otherwise find the credit card that matches the |credit_card_guid|,
886 if (base::IsValidGUID(profile_guid.first)) {
887 *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
888 *variant = profile_guid.second;
889 } else if (base::IsValidGUID(credit_card_guid.first)) {
890 *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
891 *variant = credit_card_guid.second;
894 return !!*data_model;
897 bool AutofillManager::FindCachedForm(const FormData& form,
898 FormStructure** form_structure) const {
899 // Find the FormStructure that corresponds to |form|.
900 // Scan backward through the cached |form_structures_|, as updated versions of
901 // forms are added to the back of the list, whereas original versions of these
902 // forms might appear toward the beginning of the list. The communication
903 // protocol with the crowdsourcing server does not permit us to discard the
904 // original versions of the forms.
905 *form_structure = NULL;
906 for (std::vector<FormStructure*>::const_reverse_iterator iter =
907 form_structures_.rbegin();
908 iter != form_structures_.rend(); ++iter) {
909 if (**iter == form) {
910 *form_structure = *iter;
912 // The same form might be cached with multiple field counts: in some
913 // cases, non-autofillable fields are filtered out, whereas in other cases
914 // they are not. To avoid thrashing the cache, keep scanning until we
915 // find a cached version with the same number of fields, if there is one.
916 if ((*iter)->field_count() == form.fields.size())
921 if (!(*form_structure))
927 bool AutofillManager::GetCachedFormAndField(const FormData& form,
928 const FormFieldData& field,
929 FormStructure** form_structure,
930 AutofillField** autofill_field) {
931 // Find the FormStructure that corresponds to |form|.
932 // If we do not have this form in our cache but it is parseable, we'll add it
933 // in the call to |UpdateCachedForm()|.
934 if (!FindCachedForm(form, form_structure) &&
935 !FormStructure(form).ShouldBeParsed(false)) {
939 // Update the cached form to reflect any dynamic changes to the form data, if
941 if (!UpdateCachedForm(form, *form_structure, form_structure))
944 // No data to return if there are no auto-fillable fields.
945 if (!(*form_structure)->autofill_count())
948 // Find the AutofillField that corresponds to |field|.
949 *autofill_field = NULL;
950 for (std::vector<AutofillField*>::const_iterator iter =
951 (*form_structure)->begin();
952 iter != (*form_structure)->end(); ++iter) {
953 if ((**iter) == field) {
954 *autofill_field = *iter;
959 // Even though we always update the cache, the field might not exist if the
960 // website disables autocomplete while the user is interacting with the form.
961 // See http://crbug.com/160476
962 return *autofill_field != NULL;
965 bool AutofillManager::UpdateCachedForm(const FormData& live_form,
966 const FormStructure* cached_form,
967 FormStructure** updated_form) {
970 live_form.fields.size() != cached_form->field_count());
971 for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
972 needs_update = *cached_form->field(i) != live_form.fields[i];
978 if (form_structures_.size() >= kMaxFormCacheSize)
981 // Add the new or updated form to our cache.
982 form_structures_.push_back(new FormStructure(live_form));
983 *updated_form = *form_structures_.rbegin();
984 (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
986 // If we have cached data, propagate it to the updated form.
988 std::map<base::string16, const AutofillField*> cached_fields;
989 for (size_t i = 0; i < cached_form->field_count(); ++i) {
990 const AutofillField* field = cached_form->field(i);
991 cached_fields[field->unique_name()] = field;
994 for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
995 AutofillField* field = (*updated_form)->field(i);
996 std::map<base::string16, const AutofillField*>::iterator cached_field =
997 cached_fields.find(field->unique_name());
998 if (cached_field != cached_fields.end()) {
999 field->set_server_type(cached_field->second->server_type());
1000 field->is_autofilled = cached_field->second->is_autofilled;
1004 // Note: We _must not_ remove the original version of the cached form from
1005 // the list of |form_structures_|. Otherwise, we break parsing of the
1006 // crowdsourcing server's response to our query.
1009 // Annotate the updated form with its predicted types.
1010 std::vector<FormStructure*> forms(1, *updated_form);
1011 driver_->SendAutofillTypePredictionsToRenderer(forms);
1016 void AutofillManager::GetProfileSuggestions(
1017 FormStructure* form,
1018 const FormFieldData& field,
1019 const AutofillType& type,
1020 std::vector<base::string16>* values,
1021 std::vector<base::string16>* labels,
1022 std::vector<base::string16>* icons,
1023 std::vector<int>* unique_ids) const {
1024 std::vector<ServerFieldType> field_types(form->field_count());
1025 for (size_t i = 0; i < form->field_count(); ++i) {
1026 field_types.push_back(form->field(i)->Type().GetStorableType());
1028 std::vector<GUIDPair> guid_pairs;
1030 personal_data_->GetProfileSuggestions(
1031 type, field.value, field.is_autofilled, field_types,
1032 values, labels, icons, &guid_pairs);
1034 for (size_t i = 0; i < guid_pairs.size(); ++i) {
1035 unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1040 void AutofillManager::GetCreditCardSuggestions(
1041 const FormFieldData& field,
1042 const AutofillType& type,
1043 std::vector<base::string16>* values,
1044 std::vector<base::string16>* labels,
1045 std::vector<base::string16>* icons,
1046 std::vector<int>* unique_ids) const {
1047 std::vector<GUIDPair> guid_pairs;
1048 personal_data_->GetCreditCardSuggestions(
1049 type, field.value, values, labels, icons, &guid_pairs);
1051 for (size_t i = 0; i < guid_pairs.size(); ++i) {
1052 unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1056 void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1057 std::vector<FormStructure*> non_queryable_forms;
1058 for (std::vector<FormData>::const_iterator iter = forms.begin();
1059 iter != forms.end(); ++iter) {
1060 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter));
1061 if (!form_structure->ShouldBeParsed(false))
1064 form_structure->DetermineHeuristicTypes(*metric_logger_);
1066 // Set aside forms with method GET or author-specified types, so that they
1067 // are not included in the query to the server.
1068 if (form_structure->ShouldBeCrowdsourced())
1069 form_structures_.push_back(form_structure.release());
1071 non_queryable_forms.push_back(form_structure.release());
1074 if (!form_structures_.empty() && download_manager_) {
1075 // Query the server if we have at least one of the forms were parsed.
1076 download_manager_->StartQueryRequest(form_structures_.get(),
1080 for (std::vector<FormStructure*>::const_iterator iter =
1081 non_queryable_forms.begin();
1082 iter != non_queryable_forms.end(); ++iter) {
1083 form_structures_.push_back(*iter);
1086 if (!form_structures_.empty())
1087 metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1089 // For the |non_queryable_forms|, we have all the field type info we're ever
1090 // going to get about them. For the other forms, we'll wait until we get a
1091 // response from the server.
1092 driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1095 int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1096 if (!base::IsValidGUID(guid.first))
1099 std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1100 if (iter == guid_id_map_.end()) {
1101 int id = guid_id_map_.size() + 1;
1102 guid_id_map_[guid] = id;
1103 id_guid_map_[id] = guid;
1106 return iter->second;
1110 const GUIDPair AutofillManager::IDToGUID(int id) const {
1112 return GUIDPair(std::string(), 0);
1114 std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1115 if (iter == id_guid_map_.end()) {
1117 return GUIDPair(std::string(), 0);
1120 return iter->second;
1123 // When sending IDs (across processes) to the renderer we pack credit card and
1124 // profile IDs into a single integer. Credit card IDs are sent in the high
1125 // word and profile IDs are sent in the low word.
1126 int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1127 const GUIDPair& profile_guid) const {
1128 int cc_id = GUIDToID(cc_guid);
1129 int profile_id = GUIDToID(profile_guid);
1131 DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1132 DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1134 return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1137 // When receiving IDs (across processes) from the renderer we unpack credit card
1138 // and profile IDs from a single integer. Credit card IDs are stored in the
1139 // high word and profile IDs are stored in the low word.
1140 void AutofillManager::UnpackGUIDs(int id,
1142 GUIDPair* profile_guid) const {
1143 int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1144 std::numeric_limits<unsigned short>::max();
1145 int profile_id = id & std::numeric_limits<unsigned short>::max();
1147 *cc_guid = IDToGUID(cc_id);
1148 *profile_guid = IDToGUID(profile_id);
1151 void AutofillManager::ShowRequestAutocompleteDialog(
1152 const FormData& form,
1153 const GURL& source_url,
1154 const base::Callback<void(const FormStructure*)>& callback) {
1155 manager_delegate_->ShowRequestAutocompleteDialog(
1156 form, source_url, callback);
1159 void AutofillManager::UpdateInitialInteractionTimestamp(
1160 const TimeTicks& interaction_timestamp) {
1161 if (initial_interaction_timestamp_.is_null() ||
1162 interaction_timestamp < initial_interaction_timestamp_) {
1163 initial_interaction_timestamp_ = interaction_timestamp;
1167 bool AutofillManager::ShouldUploadForm(const FormStructure& form) {
1168 if (!IsAutofillEnabled())
1171 if (driver_->GetWebContents()->GetBrowserContext()->IsOffTheRecord())
1174 // Disregard forms that we wouldn't ever autofill in the first place.
1175 if (!form.ShouldBeParsed(true))
1181 } // namespace autofill