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/message_loop/message_loop.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/strings/string16.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/sequenced_worker_pool.h"
24 #include "components/autofill/core/browser/autocomplete_history_manager.h"
25 #include "components/autofill/core/browser/autofill_client.h"
26 #include "components/autofill/core/browser/autofill_data_model.h"
27 #include "components/autofill/core/browser/autofill_external_delegate.h"
28 #include "components/autofill/core/browser/autofill_field.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/field_types.h"
35 #include "components/autofill/core/browser/form_structure.h"
36 #include "components/autofill/core/browser/personal_data_manager.h"
37 #include "components/autofill/core/browser/phone_number.h"
38 #include "components/autofill/core/browser/phone_number_i18n.h"
39 #include "components/autofill/core/browser/popup_item_ids.h"
40 #include "components/autofill/core/common/autofill_data_validation.h"
41 #include "components/autofill/core/common/autofill_pref_names.h"
42 #include "components/autofill/core/common/autofill_switches.h"
43 #include "components/autofill/core/common/form_data.h"
44 #include "components/autofill/core/common/form_data_predictions.h"
45 #include "components/autofill/core/common/form_field_data.h"
46 #include "components/autofill/core/common/password_form_fill_data.h"
47 #include "components/pref_registry/pref_registry_syncable.h"
48 #include "grit/components_strings.h"
49 #include "ui/base/l10n/l10n_util.h"
50 #include "ui/gfx/geometry/rect.h"
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
<Suggestion
>* suggestions
) {
72 std::set
<std::pair
<base::string16
, base::string16
>> seen_suggestions
;
74 for (int i
= 0; i
< static_cast<int>(suggestions
->size()); ++i
) {
75 if (!seen_suggestions
.insert(std::make_pair(
76 (*suggestions
)[i
].value
, (*suggestions
)[i
].label
)).second
) {
77 // Duplicate found, delete it.
78 suggestions
->erase(suggestions
->begin() + i
);
84 // Precondition: |form_structure| and |form| should correspond to the same
85 // logical form. Returns true if any field in the given |section| within |form|
87 bool SectionIsAutofilled(const FormStructure
& form_structure
,
89 const std::string
& section
) {
90 DCHECK_EQ(form_structure
.field_count(), form
.fields
.size());
91 for (size_t i
= 0; i
< form_structure
.field_count(); ++i
) {
92 if (form_structure
.field(i
)->section() == section
&&
93 form
.fields
[i
].is_autofilled
) {
101 bool FormIsHTTPS(const FormStructure
& form
) {
102 return form
.source_url().SchemeIs(url::kHttpsScheme
);
105 // Uses the existing personal data in |profiles| and |credit_cards| to determine
106 // possible field types for the |submitted_form|. This is potentially
107 // expensive -- on the order of 50ms even for a small set of |stored_data|.
108 // Hence, it should not run on the UI thread -- to avoid locking up the UI --
109 // nor on the IO thread -- to avoid blocking IPC calls.
110 void DeterminePossibleFieldTypesForUpload(
111 const std::vector
<AutofillProfile
>& profiles
,
112 const std::vector
<CreditCard
>& credit_cards
,
113 const std::string
& app_locale
,
114 FormStructure
* submitted_form
) {
115 // For each field in the |submitted_form|, extract the value. Then for each
116 // profile or credit card, identify any stored types that match the value.
117 for (size_t i
= 0; i
< submitted_form
->field_count(); ++i
) {
118 AutofillField
* field
= submitted_form
->field(i
);
119 ServerFieldTypeSet matching_types
;
121 base::string16 value
;
122 base::TrimWhitespace(field
->value
, base::TRIM_ALL
, &value
);
123 for (std::vector
<AutofillProfile
>::const_iterator it
= profiles
.begin();
124 it
!= profiles
.end(); ++it
) {
125 it
->GetMatchingTypes(value
, app_locale
, &matching_types
);
127 for (std::vector
<CreditCard
>::const_iterator it
= credit_cards
.begin();
128 it
!= credit_cards
.end(); ++it
) {
129 it
->GetMatchingTypes(value
, app_locale
, &matching_types
);
132 if (matching_types
.empty())
133 matching_types
.insert(UNKNOWN_TYPE
);
135 field
->set_possible_types(matching_types
);
141 AutofillManager::AutofillManager(
142 AutofillDriver
* driver
,
143 AutofillClient
* client
,
144 const std::string
& app_locale
,
145 AutofillDownloadManagerState enable_download_manager
)
148 real_pan_client_(driver
->GetURLRequestContext(), this),
149 app_locale_(app_locale
),
150 personal_data_(client
->GetPersonalDataManager()),
151 autocomplete_history_manager_(
152 new AutocompleteHistoryManager(driver
, client
)),
153 address_form_event_logger_(
154 new AutofillMetrics::FormEventLogger(false /* is_for_credit_card */)),
155 credit_card_form_event_logger_(
156 new AutofillMetrics::FormEventLogger(true /* is_for_credit_card */)),
157 has_logged_autofill_enabled_(false),
158 has_logged_address_suggestions_count_(false),
159 did_show_suggestions_(false),
160 user_did_type_(false),
161 user_did_autofill_(false),
162 user_did_edit_autofilled_field_(false),
163 external_delegate_(NULL
),
164 test_delegate_(NULL
),
165 weak_ptr_factory_(this) {
166 if (enable_download_manager
== ENABLE_AUTOFILL_DOWNLOAD_MANAGER
) {
167 download_manager_
.reset(
168 new AutofillDownloadManager(driver
, client_
->GetPrefs(), this));
172 AutofillManager::~AutofillManager() {}
175 void AutofillManager::RegisterProfilePrefs(
176 user_prefs::PrefRegistrySyncable
* registry
) {
177 registry
->RegisterBooleanPref(
178 prefs::kAutofillEnabled
,
180 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
181 registry
->RegisterBooleanPref(
182 prefs::kAutofillWalletSyncExperimentEnabled
,
184 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
185 // TODO(estade): Should this be syncable?
186 registry
->RegisterBooleanPref(
187 prefs::kAutofillWalletImportEnabled
,
189 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
190 // This choice is made on a per-device basis, so it's not syncable.
191 registry
->RegisterBooleanPref(
192 prefs::kAutofillWalletImportStorageCheckboxState
,
194 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
195 #if defined(OS_MACOSX)
196 registry
->RegisterBooleanPref(
197 prefs::kAutofillAuxiliaryProfilesEnabled
,
199 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF
);
200 #else // defined(OS_MACOSX)
201 registry
->RegisterBooleanPref(
202 prefs::kAutofillAuxiliaryProfilesEnabled
,
204 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
205 #endif // defined(OS_MACOSX)
206 #if defined(OS_MACOSX)
207 registry
->RegisterBooleanPref(
208 prefs::kAutofillMacAddressBookQueried
,
210 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
211 #endif // defined(OS_MACOSX)
212 registry
->RegisterDoublePref(
213 prefs::kAutofillPositiveUploadRate
,
214 kAutofillPositiveUploadRateDefaultValue
,
215 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
216 registry
->RegisterDoublePref(
217 prefs::kAutofillNegativeUploadRate
,
218 kAutofillNegativeUploadRateDefaultValue
,
219 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
221 #if defined(OS_MACOSX) && !defined(OS_IOS)
222 registry
->RegisterBooleanPref(
223 prefs::kAutofillUseMacAddressBook
,
225 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
226 registry
->RegisterIntegerPref(
227 prefs::kAutofillMacAddressBookShowedCount
,
229 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
230 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
233 #if defined(OS_MACOSX) && !defined(OS_IOS)
234 void AutofillManager::MigrateUserPrefs(PrefService
* prefs
) {
235 const PrefService::Preference
* pref
=
236 prefs
->FindPreference(prefs::kAutofillUseMacAddressBook
);
238 // If the pref is not its default value, then the migration has already been
240 if (!pref
->IsDefaultValue())
243 // Whether Chrome has already tried to access the user's Address Book.
244 const PrefService::Preference
* pref_accessed
=
245 prefs
->FindPreference(prefs::kAutofillMacAddressBookQueried
);
246 // Whether the user wants to use the Address Book to populate Autofill.
247 const PrefService::Preference
* pref_enabled
=
248 prefs
->FindPreference(prefs::kAutofillAuxiliaryProfilesEnabled
);
250 if (pref_accessed
->IsDefaultValue() && pref_enabled
->IsDefaultValue()) {
251 // This is likely a new user. Reset the default value to prevent the
252 // migration from happening again.
253 prefs
->SetBoolean(prefs::kAutofillUseMacAddressBook
,
254 prefs
->GetBoolean(prefs::kAutofillUseMacAddressBook
));
260 bool success
= pref_accessed
->GetValue()->GetAsBoolean(&accessed
);
262 success
= pref_enabled
->GetValue()->GetAsBoolean(&enabled
);
265 prefs
->SetBoolean(prefs::kAutofillUseMacAddressBook
, accessed
&& enabled
);
267 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
269 void AutofillManager::SetExternalDelegate(AutofillExternalDelegate
* delegate
) {
270 // TODO(jrg): consider passing delegate into the ctor. That won't
271 // work if the delegate has a pointer to the AutofillManager, but
272 // future directions may not need such a pointer.
273 external_delegate_
= delegate
;
274 autocomplete_history_manager_
->SetExternalDelegate(delegate
);
277 void AutofillManager::ShowAutofillSettings() {
278 client_
->ShowAutofillSettings();
281 #if defined(OS_MACOSX) && !defined(OS_IOS)
282 bool AutofillManager::ShouldShowAccessAddressBookSuggestion(
283 const FormData
& form
,
284 const FormFieldData
& field
) {
285 AutofillField
* autofill_field
= GetAutofillField(form
, field
);
286 return autofill_field
&&
287 personal_data_
->ShouldShowAccessAddressBookSuggestion(
288 autofill_field
->Type());
291 bool AutofillManager::AccessAddressBook() {
294 return personal_data_
->AccessAddressBook();
297 void AutofillManager::ShowedAccessAddressBookPrompt() {
300 return personal_data_
->ShowedAccessAddressBookPrompt();
303 int AutofillManager::AccessAddressBookPromptCount() {
306 return personal_data_
->AccessAddressBookPromptCount();
308 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
310 bool AutofillManager::ShouldShowScanCreditCard(const FormData
& form
,
311 const FormFieldData
& field
) {
312 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
313 autofill::switches::kEnableCreditCardScan
)) {
317 if (!client_
->HasCreditCardScanFeature())
320 AutofillField
* autofill_field
= GetAutofillField(form
, field
);
321 if (!autofill_field
||
322 autofill_field
->Type().GetStorableType() != CREDIT_CARD_NUMBER
) {
326 static const int kShowScanCreditCardMaxValueLength
= 6;
327 return field
.value
.size() <= kShowScanCreditCardMaxValueLength
&&
328 base::ContainsOnlyChars(CreditCard::StripSeparators(field
.value
),
329 base::ASCIIToUTF16("0123456789"));
332 bool AutofillManager::OnFormSubmitted(const FormData
& form
,
333 const TimeTicks
& timestamp
) {
334 if (!IsValidFormData(form
))
337 // Let Autocomplete know as well.
338 autocomplete_history_manager_
->OnFormSubmitted(form
);
340 // Grab a copy of the form data.
341 scoped_ptr
<FormStructure
> submitted_form(new FormStructure(form
));
343 if (!ShouldUploadForm(*submitted_form
))
346 // Don't save data that was submitted through JavaScript.
347 if (!form
.user_submitted
)
350 // Ignore forms not present in our cache. These are typically forms with
351 // wonky JavaScript that also makes them not auto-fillable.
352 FormStructure
* cached_submitted_form
;
353 if (!FindCachedForm(form
, &cached_submitted_form
))
356 submitted_form
->UpdateFromCache(*cached_submitted_form
);
357 if (submitted_form
->IsAutofillable())
358 ImportFormData(*submitted_form
);
360 address_form_event_logger_
->OnDidSubmitForm();
361 credit_card_form_event_logger_
->OnDidSubmitForm();
363 // Only upload server statistics and UMA metrics if at least some local data
364 // is available to use as a baseline.
365 const std::vector
<AutofillProfile
*>& profiles
= personal_data_
->GetProfiles();
366 if (submitted_form
->IsAutofillable()) {
367 AutofillMetrics::LogNumberOfProfilesAtAutofillableFormSubmission(
368 personal_data_
->GetProfiles().size());
370 const std::vector
<CreditCard
*>& credit_cards
=
371 personal_data_
->GetCreditCards();
372 if (!profiles
.empty() || !credit_cards
.empty()) {
373 // Copy the profile and credit card data, so that it can be accessed on a
375 std::vector
<AutofillProfile
> copied_profiles
;
376 copied_profiles
.reserve(profiles
.size());
377 for (std::vector
<AutofillProfile
*>::const_iterator it
= profiles
.begin();
378 it
!= profiles
.end(); ++it
) {
379 copied_profiles
.push_back(**it
);
382 std::vector
<CreditCard
> copied_credit_cards
;
383 copied_credit_cards
.reserve(credit_cards
.size());
384 for (std::vector
<CreditCard
*>::const_iterator it
= credit_cards
.begin();
385 it
!= credit_cards
.end(); ++it
) {
386 copied_credit_cards
.push_back(**it
);
389 // Note that ownership of |submitted_form| is passed to the second task,
390 // using |base::Owned|.
391 FormStructure
* raw_submitted_form
= submitted_form
.get();
392 driver_
->GetBlockingPool()->PostTaskAndReply(
394 base::Bind(&DeterminePossibleFieldTypesForUpload
,
399 base::Bind(&AutofillManager::UploadFormDataAsyncCallback
,
400 weak_ptr_factory_
.GetWeakPtr(),
401 base::Owned(submitted_form
.release()),
402 forms_loaded_timestamps_
[form
],
403 initial_interaction_timestamp_
,
410 void AutofillManager::OnFormsSeen(const std::vector
<FormData
>& forms
,
411 const TimeTicks
& timestamp
) {
412 if (!IsValidFormDataVector(forms
))
415 if (!driver_
->RendererIsAvailable())
418 bool enabled
= IsAutofillEnabled();
419 if (!has_logged_autofill_enabled_
) {
420 AutofillMetrics::LogIsAutofillEnabledAtPageLoad(enabled
);
421 has_logged_autofill_enabled_
= true;
427 for (size_t i
= 0; i
< forms
.size(); ++i
) {
428 forms_loaded_timestamps_
[forms
[i
]] = timestamp
;
434 void AutofillManager::OnTextFieldDidChange(const FormData
& form
,
435 const FormFieldData
& field
,
436 const TimeTicks
& timestamp
) {
437 if (!IsValidFormData(form
) || !IsValidFormFieldData(field
))
440 FormStructure
* form_structure
= NULL
;
441 AutofillField
* autofill_field
= NULL
;
442 if (!GetCachedFormAndField(form
, field
, &form_structure
, &autofill_field
))
445 if (!user_did_type_
) {
446 user_did_type_
= true;
447 AutofillMetrics::LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE
);
450 if (autofill_field
->is_autofilled
) {
451 autofill_field
->is_autofilled
= false;
452 AutofillMetrics::LogUserHappinessMetric(
453 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD
);
455 if (!user_did_edit_autofilled_field_
) {
456 user_did_edit_autofilled_field_
= true;
457 AutofillMetrics::LogUserHappinessMetric(
458 AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE
);
462 UpdateInitialInteractionTimestamp(timestamp
);
465 void AutofillManager::OnQueryFormFieldAutofill(int query_id
,
466 const FormData
& form
,
467 const FormFieldData
& field
,
468 const gfx::RectF
& bounding_box
,
469 bool display_warning
) {
470 if (!IsValidFormData(form
) || !IsValidFormFieldData(field
))
473 std::vector
<Suggestion
> suggestions
;
475 external_delegate_
->OnQuery(query_id
,
481 // Need to refresh models before using the form_event_loggers.
482 bool is_autofill_possible
= RefreshDataModels();
484 FormStructure
* form_structure
= NULL
;
485 AutofillField
* autofill_field
= NULL
;
486 bool got_autofillable_form
=
487 GetCachedFormAndField(form
, field
, &form_structure
, &autofill_field
) &&
488 // Don't send suggestions or track forms that aren't auto-fillable.
489 form_structure
->IsAutofillable();
491 // Logging interactions of forms that are autofillable.
492 if (got_autofillable_form
) {
493 if (autofill_field
->Type().group() == CREDIT_CARD
)
494 credit_card_form_event_logger_
->OnDidInteractWithAutofillableForm();
496 address_form_event_logger_
->OnDidInteractWithAutofillableForm();
499 if (is_autofill_possible
&&
500 driver_
->RendererIsAvailable() &&
501 got_autofillable_form
) {
502 AutofillType type
= autofill_field
->Type();
503 bool is_filling_credit_card
= (type
.group() == CREDIT_CARD
);
504 if (is_filling_credit_card
) {
505 suggestions
= GetCreditCardSuggestions(field
, type
);
508 GetProfileSuggestions(*form_structure
, field
, *autofill_field
);
510 if (!suggestions
.empty()) {
511 // Don't provide Autofill suggestions when Autofill is disabled, and don't
512 // provide credit card suggestions for non-HTTPS pages. However, provide a
513 // warning to the user in these cases.
515 if (is_filling_credit_card
&& !FormIsHTTPS(*form_structure
)) {
516 warning
= IDS_AUTOFILL_WARNING_INSECURE_CONNECTION
;
519 Suggestion
warning_suggestion(l10n_util::GetStringUTF16(warning
));
520 warning_suggestion
.frontend_id
= POPUP_ITEM_ID_WARNING_MESSAGE
;
521 suggestions
.assign(1, warning_suggestion
);
523 bool section_is_autofilled
=
524 SectionIsAutofilled(*form_structure
, form
,
525 autofill_field
->section());
526 if (section_is_autofilled
) {
527 // If the relevant section is auto-filled and the renderer is querying
528 // for suggestions, then the user is editing the value of a field.
529 // In this case, mimic autocomplete: don't display labels or icons,
530 // as that information is redundant.
531 for (size_t i
= 0; i
< suggestions
.size(); i
++) {
532 suggestions
[i
].label
= base::string16();
533 suggestions
[i
].icon
= base::string16();
537 // When filling credit card suggestions, the values and labels are
538 // typically obfuscated, which makes detecting duplicates hard. Since
539 // duplicates only tend to be a problem when filling address forms
540 // anyway, only don't de-dup credit card suggestions.
541 if (!is_filling_credit_card
)
542 RemoveDuplicateSuggestions(&suggestions
);
544 // The first time we show suggestions on this page, log the number of
545 // suggestions available.
546 // TODO(mathp): Differentiate between number of suggestions available
547 // (current metric) and number shown to the user.
548 if (!has_logged_address_suggestions_count_
&& !section_is_autofilled
) {
549 AutofillMetrics::LogAddressSuggestionsCount(suggestions
.size());
550 has_logged_address_suggestions_count_
= true;
556 if (field
.should_autocomplete
) {
557 // Add the results from AutoComplete. They come back asynchronously, so we
558 // hand off what we generated and they will send the results back to the
560 autocomplete_history_manager_
->OnGetAutocompleteSuggestions(
561 query_id
, field
.name
, field
.value
, field
.form_control_type
,
564 // Autocomplete is disabled for this field; only pass back Autofill
566 autocomplete_history_manager_
->CancelPendingQuery();
567 external_delegate_
->OnSuggestionsReturned(
568 query_id
, suggestions
);
572 void AutofillManager::FillOrPreviewCreditCardForm(
573 AutofillDriver::RendererFormDataAction action
,
575 const FormData
& form
,
576 const FormFieldData
& field
,
577 const CreditCard
& credit_card
,
579 if (action
== AutofillDriver::FORM_DATA_ACTION_FILL
) {
580 if (credit_card
.record_type() == CreditCard::MASKED_SERVER_CARD
) {
581 unmasking_card_
= credit_card
;
582 unmasking_query_id_
= query_id
;
583 unmasking_form_
= form
;
584 unmasking_field_
= field
;
585 real_pan_client_
.Prepare();
586 client()->ShowUnmaskPrompt(unmasking_card_
,
587 weak_ptr_factory_
.GetWeakPtr());
588 credit_card_form_event_logger_
->OnDidSelectMaskedServerCardSuggestion();
591 credit_card_form_event_logger_
->OnDidFillSuggestion(credit_card
);
594 FillOrPreviewDataModelForm(action
, query_id
, form
, field
, credit_card
,
595 variant
, true /* is_credit_card */);
598 void AutofillManager::FillOrPreviewProfileForm(
599 AutofillDriver::RendererFormDataAction action
,
601 const FormData
& form
,
602 const FormFieldData
& field
,
603 const AutofillProfile
& profile
,
605 if (action
== AutofillDriver::FORM_DATA_ACTION_FILL
)
606 address_form_event_logger_
->OnDidFillSuggestion(profile
);
608 FillOrPreviewDataModelForm(action
, query_id
, form
, field
, profile
, variant
,
609 false /* is_credit_card */);
612 void AutofillManager::FillOrPreviewForm(
613 AutofillDriver::RendererFormDataAction action
,
615 const FormData
& form
,
616 const FormFieldData
& field
,
618 if (!IsValidFormData(form
) || !IsValidFormFieldData(field
))
621 // NOTE: RefreshDataModels may invalidate |data_model| because it causes the
622 // PersonalDataManager to reload Mac address book entries. Thus it must come
623 // before GetProfile or GetCreditCard.
624 if (!RefreshDataModels() || !driver_
->RendererIsAvailable())
628 const CreditCard
* credit_card
= nullptr;
629 const AutofillProfile
* profile
= nullptr;
630 if (GetCreditCard(unique_id
, &credit_card
))
631 FillOrPreviewCreditCardForm(action
, query_id
, form
, field
, *credit_card
, 0);
632 else if (GetProfile(unique_id
, &profile
, &variant
))
633 FillOrPreviewProfileForm(action
, query_id
, form
, field
, *profile
, variant
);
636 void AutofillManager::FillCreditCardForm(int query_id
,
637 const FormData
& form
,
638 const FormFieldData
& field
,
639 const CreditCard
& credit_card
) {
640 if (!IsValidFormData(form
) || !IsValidFormFieldData(field
) ||
641 !driver_
->RendererIsAvailable()) {
645 FillOrPreviewDataModelForm(AutofillDriver::FORM_DATA_ACTION_FILL
, query_id
,
646 form
, field
, credit_card
, 0, true);
649 void AutofillManager::OnDidPreviewAutofillFormData() {
651 test_delegate_
->DidPreviewFormData();
654 void AutofillManager::OnDidFillAutofillFormData(const TimeTicks
& timestamp
) {
656 test_delegate_
->DidFillFormData();
658 AutofillMetrics::LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL
);
659 if (!user_did_autofill_
) {
660 user_did_autofill_
= true;
661 AutofillMetrics::LogUserHappinessMetric(
662 AutofillMetrics::USER_DID_AUTOFILL_ONCE
);
665 UpdateInitialInteractionTimestamp(timestamp
);
668 void AutofillManager::DidShowSuggestions(bool is_new_popup
,
669 const FormData
& form
,
670 const FormFieldData
& field
) {
672 test_delegate_
->DidShowSuggestions();
673 FormStructure
* form_structure
= NULL
;
674 AutofillField
* autofill_field
= NULL
;
675 if (!GetCachedFormAndField(form
, field
, &form_structure
, &autofill_field
))
679 AutofillMetrics::LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN
);
681 if (!did_show_suggestions_
) {
682 did_show_suggestions_
= true;
683 AutofillMetrics::LogUserHappinessMetric(
684 AutofillMetrics::SUGGESTIONS_SHOWN_ONCE
);
687 if (autofill_field
->Type().group() == CREDIT_CARD
)
688 credit_card_form_event_logger_
->OnDidShowSuggestions();
690 address_form_event_logger_
->OnDidShowSuggestions();
694 void AutofillManager::OnHidePopup() {
695 if (!IsAutofillEnabled())
698 autocomplete_history_manager_
->CancelPendingQuery();
699 client_
->HideAutofillPopup();
702 void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id
) {
705 const CreditCard
* credit_card
= nullptr;
706 const AutofillProfile
* profile
= nullptr;
707 if (GetCreditCard(unique_id
, &credit_card
)) {
708 guid
= credit_card
->guid();
709 } else if (GetProfile(unique_id
, &profile
, &variant
)) {
710 guid
= profile
->guid();
716 // TODO(csharp): If we are dealing with a variant only the variant should
717 // be deleted, instead of doing nothing.
718 // http://crbug.com/124211
722 personal_data_
->RemoveByGUID(guid
);
725 void AutofillManager::RemoveAutocompleteEntry(const base::string16
& name
,
726 const base::string16
& value
) {
727 autocomplete_history_manager_
->OnRemoveAutocompleteEntry(name
, value
);
730 bool AutofillManager::IsShowingUnmaskPrompt() {
731 return unmasking_card_
.Compare(CreditCard()) != 0;
734 const std::vector
<FormStructure
*>& AutofillManager::GetFormStructures() {
735 return form_structures_
.get();
738 void AutofillManager::SetTestDelegate(AutofillManagerTestDelegate
* delegate
) {
739 test_delegate_
= delegate
;
742 void AutofillManager::OnSetDataList(const std::vector
<base::string16
>& values
,
743 const std::vector
<base::string16
>& labels
) {
744 if (!IsValidString16Vector(values
) ||
745 !IsValidString16Vector(labels
) ||
746 values
.size() != labels
.size())
749 external_delegate_
->SetCurrentDataListValues(values
, labels
);
752 void AutofillManager::OnLoadedServerPredictions(
753 const std::string
& response_xml
) {
754 // Parse and store the server predictions.
755 FormStructure::ParseQueryResponse(response_xml
, form_structures_
.get());
757 // Forward form structures to the password generation manager to detect
758 // account creation forms.
759 driver_
->DetectAccountCreationForms(form_structures_
.get());
761 // If the corresponding flag is set, annotate forms with the predicted types.
762 driver_
->SendAutofillTypePredictionsToRenderer(form_structures_
.get());
765 void AutofillManager::OnUnmaskResponse(const UnmaskResponse
& response
) {
766 unmask_response_
= response
;
767 real_pan_client_
.UnmaskCard(unmasking_card_
, response
);
770 void AutofillManager::OnUnmaskPromptClosed() {
771 real_pan_client_
.CancelRequest();
772 driver_
->RendererShouldClearPreviewedForm();
773 unmasking_card_
= CreditCard();
774 unmask_response_
= UnmaskResponse();
777 IdentityProvider
* AutofillManager::GetIdentityProvider() {
778 return client()->GetIdentityProvider();
781 void AutofillManager::OnDidGetRealPan(AutofillClient::GetRealPanResult result
,
782 const std::string
& real_pan
) {
783 if (!real_pan
.empty()) {
784 DCHECK_EQ(AutofillClient::SUCCESS
, result
);
785 credit_card_form_event_logger_
->OnDidFillSuggestion(unmasking_card_
);
786 unmasking_card_
.set_record_type(CreditCard::FULL_SERVER_CARD
);
787 unmasking_card_
.SetNumber(base::UTF8ToUTF16(real_pan
));
788 if (unmask_response_
.should_store_pan
)
789 personal_data_
->UpdateServerCreditCard(unmasking_card_
);
791 FillCreditCardForm(unmasking_query_id_
, unmasking_form_
, unmasking_field_
,
795 client()->OnUnmaskVerificationResult(result
);
798 void AutofillManager::OnDidEndTextFieldEditing() {
799 external_delegate_
->DidEndTextFieldEditing();
802 bool AutofillManager::IsAutofillEnabled() const {
803 return client_
->GetPrefs()->GetBoolean(prefs::kAutofillEnabled
);
806 void AutofillManager::ImportFormData(const FormStructure
& submitted_form
) {
807 scoped_ptr
<CreditCard
> imported_credit_card
;
808 if (!personal_data_
->ImportFormData(submitted_form
, &imported_credit_card
))
811 // If credit card information was submitted, we need to confirm whether to
813 if (imported_credit_card
) {
814 client_
->ConfirmSaveCreditCard(
816 base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard
),
817 base::Unretained(personal_data_
),
818 *imported_credit_card
));
822 // Note that |submitted_form| is passed as a pointer rather than as a reference
823 // so that we can get memory management right across threads. Note also that we
824 // explicitly pass in all the time stamps of interest, as the cached ones might
825 // get reset before this method executes.
826 void AutofillManager::UploadFormDataAsyncCallback(
827 const FormStructure
* submitted_form
,
828 const TimeTicks
& load_time
,
829 const TimeTicks
& interaction_time
,
830 const TimeTicks
& submission_time
) {
831 submitted_form
->LogQualityMetrics(load_time
, interaction_time
,
834 if (submitted_form
->ShouldBeCrowdsourced())
835 UploadFormData(*submitted_form
);
838 void AutofillManager::UploadFormData(const FormStructure
& submitted_form
) {
839 if (!download_manager_
)
842 // Check if the form is among the forms that were recently auto-filled.
843 bool was_autofilled
= false;
844 std::string form_signature
= submitted_form
.FormSignature();
845 for (std::list
<std::string
>::const_iterator it
=
846 autofilled_form_signatures_
.begin();
847 it
!= autofilled_form_signatures_
.end() && !was_autofilled
;
849 if (*it
== form_signature
)
850 was_autofilled
= true;
853 ServerFieldTypeSet non_empty_types
;
854 personal_data_
->GetNonEmptyTypes(&non_empty_types
);
856 download_manager_
->StartUploadRequest(submitted_form
, was_autofilled
,
860 bool AutofillManager::UploadPasswordForm(
861 const FormData
& form
,
862 const ServerFieldType
& password_type
) {
863 FormStructure
form_structure(form
);
865 if (!ShouldUploadForm(form_structure
))
868 if (!form_structure
.ShouldBeCrowdsourced())
871 // Find the first password field to label. We don't try to label anything
873 bool found_password_field
= false;
874 for (size_t i
= 0; i
< form_structure
.field_count(); ++i
) {
875 AutofillField
* field
= form_structure
.field(i
);
877 ServerFieldTypeSet types
;
878 if (!found_password_field
&& field
->form_control_type
== "password") {
879 types
.insert(password_type
);
880 found_password_field
= true;
882 types
.insert(UNKNOWN_TYPE
);
884 field
->set_possible_types(types
);
886 DCHECK(found_password_field
);
888 // Only one field type should be present.
889 ServerFieldTypeSet available_field_types
;
890 available_field_types
.insert(password_type
);
892 // Force uploading as these events are relatively rare and we want to make
893 // sure to receive them. It also makes testing easier if these requests
895 form_structure
.set_upload_required(UPLOAD_REQUIRED
);
897 if (!download_manager_
)
900 return download_manager_
->StartUploadRequest(form_structure
,
901 false /* was_autofilled */,
902 available_field_types
);
905 void AutofillManager::Reset() {
906 form_structures_
.clear();
907 address_form_event_logger_
.reset(
908 new AutofillMetrics::FormEventLogger(false /* is_for_credit_card */));
909 credit_card_form_event_logger_
.reset(
910 new AutofillMetrics::FormEventLogger(true /* is_for_credit_card */));
911 has_logged_autofill_enabled_
= false;
912 has_logged_address_suggestions_count_
= false;
913 did_show_suggestions_
= false;
914 user_did_type_
= false;
915 user_did_autofill_
= false;
916 user_did_edit_autofilled_field_
= false;
917 unmasking_card_
= CreditCard();
918 unmasking_query_id_
= -1;
919 unmasking_form_
= FormData();
920 unmasking_field_
= FormFieldData();
921 forms_loaded_timestamps_
.clear();
922 initial_interaction_timestamp_
= TimeTicks();
923 external_delegate_
->Reset();
926 AutofillManager::AutofillManager(AutofillDriver
* driver
,
927 AutofillClient
* client
,
928 PersonalDataManager
* personal_data
)
931 real_pan_client_(driver
->GetURLRequestContext(), this),
932 app_locale_("en-US"),
933 personal_data_(personal_data
),
934 autocomplete_history_manager_(
935 new AutocompleteHistoryManager(driver
, client
)),
936 address_form_event_logger_(
937 new AutofillMetrics::FormEventLogger(false /* is_for_credit_card */)),
938 credit_card_form_event_logger_(
939 new AutofillMetrics::FormEventLogger(true /* is_for_credit_card */)),
940 has_logged_autofill_enabled_(false),
941 has_logged_address_suggestions_count_(false),
942 did_show_suggestions_(false),
943 user_did_type_(false),
944 user_did_autofill_(false),
945 user_did_edit_autofilled_field_(false),
946 unmasking_query_id_(-1),
947 external_delegate_(NULL
),
948 test_delegate_(NULL
),
949 weak_ptr_factory_(this) {
954 bool AutofillManager::RefreshDataModels() {
955 if (!IsAutofillEnabled())
958 // No autofill data to return if the profiles are empty.
959 const std::vector
<AutofillProfile
*>& profiles
=
960 personal_data_
->GetProfiles();
961 const std::vector
<CreditCard
*>& credit_cards
=
962 personal_data_
->GetCreditCards();
964 // Updating the FormEventLoggers for addresses and credit cards.
966 bool is_server_data_available
= false;
967 bool is_local_data_available
= false;
968 for (CreditCard
* credit_card
: credit_cards
) {
969 if (credit_card
->record_type() == CreditCard::LOCAL_CARD
)
970 is_local_data_available
= true;
972 is_server_data_available
= true;
974 credit_card_form_event_logger_
->set_is_server_data_available(
975 is_server_data_available
);
976 credit_card_form_event_logger_
->set_is_local_data_available(
977 is_local_data_available
);
980 bool is_server_data_available
= false;
981 bool is_local_data_available
= false;
982 for (AutofillProfile
* profile
: profiles
) {
983 if (profile
->record_type() == AutofillProfile::LOCAL_PROFILE
)
984 is_local_data_available
= true;
986 is_server_data_available
= true;
988 address_form_event_logger_
->set_is_server_data_available(
989 is_server_data_available
);
990 address_form_event_logger_
->set_is_local_data_available(
991 is_local_data_available
);
994 if (profiles
.empty() && credit_cards
.empty())
1000 bool AutofillManager::IsCreditCard(int unique_id
) {
1001 // Unpack the |unique_id| into component parts.
1002 SuggestionBackendID credit_card_id
;
1003 SuggestionBackendID profile_id
;
1004 SplitFrontendID(unique_id
, &credit_card_id
, &profile_id
);
1005 DCHECK(!base::IsValidGUID(credit_card_id
.guid
) ||
1006 !base::IsValidGUID(profile_id
.guid
));
1007 return base::IsValidGUID(credit_card_id
.guid
);
1010 bool AutofillManager::GetProfile(int unique_id
,
1011 const AutofillProfile
** profile
,
1013 // Unpack the |unique_id| into component parts.
1014 SuggestionBackendID credit_card_id
;
1015 SuggestionBackendID profile_id
;
1016 SplitFrontendID(unique_id
, &credit_card_id
, &profile_id
);
1018 if (base::IsValidGUID(profile_id
.guid
)) {
1019 *profile
= personal_data_
->GetProfileByGUID(profile_id
.guid
);
1020 *variant
= profile_id
.variant
;
1025 bool AutofillManager::GetCreditCard(int unique_id
,
1026 const CreditCard
** credit_card
) {
1027 // Unpack the |unique_id| into component parts.
1028 SuggestionBackendID credit_card_id
;
1029 SuggestionBackendID profile_id
;
1030 SplitFrontendID(unique_id
, &credit_card_id
, &profile_id
);
1031 *credit_card
= NULL
;
1032 if (base::IsValidGUID(credit_card_id
.guid
))
1033 *credit_card
= personal_data_
->GetCreditCardByGUID(credit_card_id
.guid
);
1034 return !!*credit_card
;
1037 void AutofillManager::FillOrPreviewDataModelForm(
1038 AutofillDriver::RendererFormDataAction action
,
1040 const FormData
& form
,
1041 const FormFieldData
& field
,
1042 const AutofillDataModel
& data_model
,
1044 bool is_credit_card
) {
1045 FormStructure
* form_structure
= NULL
;
1046 AutofillField
* autofill_field
= NULL
;
1047 if (!GetCachedFormAndField(form
, field
, &form_structure
, &autofill_field
))
1050 DCHECK(form_structure
);
1051 DCHECK(autofill_field
);
1053 FormData result
= form
;
1055 base::string16 profile_full_name
;
1056 std::string profile_language_code
;
1057 if (!is_credit_card
) {
1058 profile_full_name
= data_model
.GetInfo(
1059 AutofillType(NAME_FULL
), app_locale_
);
1060 profile_language_code
=
1061 static_cast<const AutofillProfile
*>(&data_model
)->language_code();
1064 // If the relevant section is auto-filled, we should fill |field| but not the
1065 // rest of the form.
1066 if (SectionIsAutofilled(*form_structure
, form
, autofill_field
->section())) {
1067 for (std::vector
<FormFieldData
>::iterator iter
= result
.fields
.begin();
1068 iter
!= result
.fields
.end(); ++iter
) {
1069 if (iter
->SameFieldAs(field
)) {
1070 base::string16 value
= data_model
.GetInfoForVariant(
1071 autofill_field
->Type(), variant
, app_locale_
);
1072 if (AutofillField::FillFormField(*autofill_field
,
1074 profile_language_code
,
1077 // Mark the cached field as autofilled, so that we can detect when a
1078 // user edits an autofilled field (for metrics).
1079 autofill_field
->is_autofilled
= true;
1081 // Mark the field as autofilled when a non-empty value is assigned to
1082 // it. This allows the renderer to distinguish autofilled fields from
1083 // fields with non-empty values, such as select-one fields.
1084 iter
->is_autofilled
= true;
1086 if (!is_credit_card
&& !value
.empty())
1087 client_
->DidFillOrPreviewField(value
, profile_full_name
);
1093 // Note that this may invalidate |data_model|, particularly if it is a Mac
1094 // address book entry.
1095 if (action
== AutofillDriver::FORM_DATA_ACTION_FILL
)
1096 personal_data_
->RecordUseOf(data_model
);
1098 driver_
->SendFormDataToRenderer(query_id
, action
, result
);
1102 // Cache the field type for the field from which the user initiated autofill.
1103 FieldTypeGroup initiating_group_type
= autofill_field
->Type().group();
1104 DCHECK_EQ(form_structure
->field_count(), form
.fields
.size());
1105 for (size_t i
= 0; i
< form_structure
->field_count(); ++i
) {
1106 if (form_structure
->field(i
)->section() != autofill_field
->section())
1109 DCHECK(form_structure
->field(i
)->SameFieldAs(result
.fields
[i
]));
1111 const AutofillField
* cached_field
= form_structure
->field(i
);
1112 FieldTypeGroup field_group_type
= cached_field
->Type().group();
1114 if (field_group_type
== NO_GROUP
)
1117 // If the field being filled is either
1118 // (a) the field that the user initiated the fill from, or
1119 // (b) part of the same logical unit, e.g. name or phone number,
1120 // then take the multi-profile "variant" into account.
1121 // Otherwise fill with the default (zeroth) variant.
1122 size_t use_variant
= 0;
1123 if (result
.fields
[i
].SameFieldAs(field
) ||
1124 field_group_type
== initiating_group_type
) {
1125 use_variant
= variant
;
1127 base::string16 value
= data_model
.GetInfoForVariant(
1128 cached_field
->Type(), use_variant
, app_locale_
);
1129 if (is_credit_card
&&
1130 cached_field
->Type().GetStorableType() ==
1131 CREDIT_CARD_VERIFICATION_CODE
) {
1132 // If this is |unmasking_card_|, |unmask_response_.cvc| should be
1133 // non-empty and vice versa.
1134 value
= unmask_response_
.cvc
;
1135 DCHECK_EQ(&unmasking_card_
== &data_model
, !value
.empty());
1138 // Must match ForEachMatchingFormField() in form_autofill_util.cc.
1139 // Only notify autofilling of empty fields and the field that initiated
1140 // the filling (note that "select-one" controls may not be empty but will
1141 // still be autofilled).
1142 bool should_notify
=
1145 (result
.fields
[i
].SameFieldAs(field
) ||
1146 result
.fields
[i
].form_control_type
== "select-one" ||
1147 result
.fields
[i
].value
.empty());
1148 if (AutofillField::FillFormField(*cached_field
,
1150 profile_language_code
,
1152 &result
.fields
[i
])) {
1153 // Mark the cached field as autofilled, so that we can detect when a
1154 // user edits an autofilled field (for metrics).
1155 form_structure
->field(i
)->is_autofilled
= true;
1157 // Mark the field as autofilled when a non-empty value is assigned to
1158 // it. This allows the renderer to distinguish autofilled fields from
1159 // fields with non-empty values, such as select-one fields.
1160 result
.fields
[i
].is_autofilled
= true;
1163 client_
->DidFillOrPreviewField(value
, profile_full_name
);
1167 autofilled_form_signatures_
.push_front(form_structure
->FormSignature());
1168 // Only remember the last few forms that we've seen, both to avoid false
1169 // positives and to avoid wasting memory.
1170 if (autofilled_form_signatures_
.size() > kMaxRecentFormSignaturesToRemember
)
1171 autofilled_form_signatures_
.pop_back();
1173 // Note that this may invalidate |data_model|, particularly if it is a Mac
1174 // address book entry.
1175 if (action
== AutofillDriver::FORM_DATA_ACTION_FILL
)
1176 personal_data_
->RecordUseOf(data_model
);
1178 driver_
->SendFormDataToRenderer(query_id
, action
, result
);
1181 bool AutofillManager::FindCachedForm(const FormData
& form
,
1182 FormStructure
** form_structure
) const {
1183 // Find the FormStructure that corresponds to |form|.
1184 // Scan backward through the cached |form_structures_|, as updated versions of
1185 // forms are added to the back of the list, whereas original versions of these
1186 // forms might appear toward the beginning of the list. The communication
1187 // protocol with the crowdsourcing server does not permit us to discard the
1188 // original versions of the forms.
1189 *form_structure
= NULL
;
1190 for (std::vector
<FormStructure
*>::const_reverse_iterator iter
=
1191 form_structures_
.rbegin();
1192 iter
!= form_structures_
.rend(); ++iter
) {
1193 if (**iter
== form
) {
1194 *form_structure
= *iter
;
1196 // The same form might be cached with multiple field counts: in some
1197 // cases, non-autofillable fields are filtered out, whereas in other cases
1198 // they are not. To avoid thrashing the cache, keep scanning until we
1199 // find a cached version with the same number of fields, if there is one.
1200 if ((*iter
)->field_count() == form
.fields
.size())
1205 if (!(*form_structure
))
1211 bool AutofillManager::GetCachedFormAndField(const FormData
& form
,
1212 const FormFieldData
& field
,
1213 FormStructure
** form_structure
,
1214 AutofillField
** autofill_field
) {
1215 // Find the FormStructure that corresponds to |form|.
1216 // If we do not have this form in our cache but it is parseable, we'll add it
1217 // in the call to |UpdateCachedForm()|.
1218 if (!FindCachedForm(form
, form_structure
) &&
1219 !FormStructure(form
).ShouldBeParsed()) {
1223 // Update the cached form to reflect any dynamic changes to the form data, if
1225 if (!UpdateCachedForm(form
, *form_structure
, form_structure
))
1228 // No data to return if there are no auto-fillable fields.
1229 if (!(*form_structure
)->autofill_count())
1232 // Find the AutofillField that corresponds to |field|.
1233 *autofill_field
= NULL
;
1234 for (AutofillField
* current
: **form_structure
) {
1235 if (current
->SameFieldAs(field
)) {
1236 *autofill_field
= current
;
1241 // Even though we always update the cache, the field might not exist if the
1242 // website disables autocomplete while the user is interacting with the form.
1243 // See http://crbug.com/160476
1244 return *autofill_field
!= NULL
;
1247 AutofillField
* AutofillManager::GetAutofillField(const FormData
& form
,
1248 const FormFieldData
& field
) {
1249 if (!personal_data_
)
1252 FormStructure
* form_structure
= NULL
;
1253 AutofillField
* autofill_field
= NULL
;
1254 if (!GetCachedFormAndField(form
, field
, &form_structure
, &autofill_field
))
1257 if (!form_structure
->IsAutofillable())
1260 return autofill_field
;
1263 bool AutofillManager::UpdateCachedForm(const FormData
& live_form
,
1264 const FormStructure
* cached_form
,
1265 FormStructure
** updated_form
) {
1268 live_form
.fields
.size() != cached_form
->field_count());
1269 for (size_t i
= 0; !needs_update
&& i
< cached_form
->field_count(); ++i
)
1270 needs_update
= !cached_form
->field(i
)->SameFieldAs(live_form
.fields
[i
]);
1275 if (form_structures_
.size() >= kMaxFormCacheSize
)
1278 // Add the new or updated form to our cache.
1279 form_structures_
.push_back(new FormStructure(live_form
));
1280 *updated_form
= *form_structures_
.rbegin();
1281 (*updated_form
)->DetermineHeuristicTypes();
1283 // If we have cached data, propagate it to the updated form.
1285 std::map
<base::string16
, const AutofillField
*> cached_fields
;
1286 for (size_t i
= 0; i
< cached_form
->field_count(); ++i
) {
1287 const AutofillField
* field
= cached_form
->field(i
);
1288 cached_fields
[field
->unique_name()] = field
;
1291 for (size_t i
= 0; i
< (*updated_form
)->field_count(); ++i
) {
1292 AutofillField
* field
= (*updated_form
)->field(i
);
1293 std::map
<base::string16
, const AutofillField
*>::iterator cached_field
=
1294 cached_fields
.find(field
->unique_name());
1295 if (cached_field
!= cached_fields
.end()) {
1296 field
->set_server_type(cached_field
->second
->server_type());
1297 field
->is_autofilled
= cached_field
->second
->is_autofilled
;
1301 // Note: We _must not_ remove the original version of the cached form from
1302 // the list of |form_structures_|. Otherwise, we break parsing of the
1303 // crowdsourcing server's response to our query.
1306 // Annotate the updated form with its predicted types.
1307 std::vector
<FormStructure
*> forms(1, *updated_form
);
1308 driver_
->SendAutofillTypePredictionsToRenderer(forms
);
1313 std::vector
<Suggestion
> AutofillManager::GetProfileSuggestions(
1314 const FormStructure
& form
,
1315 const FormFieldData
& field
,
1316 const AutofillField
& autofill_field
) const {
1317 std::vector
<ServerFieldType
> field_types(form
.field_count());
1318 for (size_t i
= 0; i
< form
.field_count(); ++i
) {
1319 field_types
.push_back(form
.field(i
)->Type().GetStorableType());
1322 std::vector
<Suggestion
> suggestions
= personal_data_
->GetProfileSuggestions(
1323 autofill_field
.Type(), field
.value
, field
.is_autofilled
, field_types
);
1325 // Adjust phone number to display in prefix/suffix case.
1326 if (autofill_field
.Type().GetStorableType() == PHONE_HOME_NUMBER
) {
1327 for (size_t i
= 0; i
< suggestions
.size(); ++i
) {
1328 suggestions
[i
].value
= AutofillField::GetPhoneNumberValue(
1329 autofill_field
, suggestions
[i
].value
, field
);
1333 for (size_t i
= 0; i
< suggestions
.size(); ++i
) {
1334 suggestions
[i
].frontend_id
=
1335 MakeFrontendID(SuggestionBackendID(), suggestions
[i
].backend_id
);
1340 std::vector
<Suggestion
> AutofillManager::GetCreditCardSuggestions(
1341 const FormFieldData
& field
,
1342 const AutofillType
& type
) const {
1343 std::vector
<Suggestion
> suggestions
=
1344 personal_data_
->GetCreditCardSuggestions(type
, field
.value
);
1345 for (size_t i
= 0; i
< suggestions
.size(); i
++) {
1346 suggestions
[i
].frontend_id
=
1347 MakeFrontendID(suggestions
[i
].backend_id
, SuggestionBackendID());
1352 void AutofillManager::ParseForms(const std::vector
<FormData
>& forms
) {
1353 std::vector
<FormStructure
*> non_queryable_forms
;
1354 for (std::vector
<FormData
>::const_iterator iter
= forms
.begin();
1355 iter
!= forms
.end(); ++iter
) {
1356 scoped_ptr
<FormStructure
> form_structure(new FormStructure(*iter
));
1357 if (!form_structure
->ShouldBeParsed())
1360 form_structure
->DetermineHeuristicTypes();
1362 if (form_structure
->ShouldBeCrowdsourced())
1363 form_structures_
.push_back(form_structure
.release());
1365 non_queryable_forms
.push_back(form_structure
.release());
1368 if (!form_structures_
.empty() && download_manager_
) {
1369 // Query the server if at least one of the forms was parsed.
1370 download_manager_
->StartQueryRequest(form_structures_
.get());
1373 for (std::vector
<FormStructure
*>::const_iterator iter
=
1374 non_queryable_forms
.begin();
1375 iter
!= non_queryable_forms
.end(); ++iter
) {
1376 form_structures_
.push_back(*iter
);
1379 if (!form_structures_
.empty())
1380 AutofillMetrics::LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED
);
1382 // For the |non_queryable_forms|, we have all the field type info we're ever
1383 // going to get about them. For the other forms, we'll wait until we get a
1384 // response from the server.
1385 driver_
->SendAutofillTypePredictionsToRenderer(non_queryable_forms
);
1388 int AutofillManager::BackendIDToInt(
1389 const SuggestionBackendID
& backend_id
) const {
1390 if (!base::IsValidGUID(backend_id
.guid
))
1393 const auto found
= backend_to_int_map_
.find(backend_id
);
1394 if (found
== backend_to_int_map_
.end()) {
1395 // Unknown one, make a new entry.
1396 int int_id
= backend_to_int_map_
.size() + 1;
1397 backend_to_int_map_
[backend_id
] = int_id
;
1398 int_to_backend_map_
[int_id
] = backend_id
;
1401 return found
->second
;
1404 SuggestionBackendID
AutofillManager::IntToBackendID(int int_id
) const {
1406 return SuggestionBackendID();
1408 const auto found
= int_to_backend_map_
.find(int_id
);
1409 if (found
== int_to_backend_map_
.end()) {
1411 return SuggestionBackendID();
1413 return found
->second
;
1416 // When sending IDs (across processes) to the renderer we pack credit card and
1417 // profile IDs into a single integer. Credit card IDs are sent in the high
1418 // word and profile IDs are sent in the low word.
1419 int AutofillManager::MakeFrontendID(
1420 const SuggestionBackendID
& cc_backend_id
,
1421 const SuggestionBackendID
& profile_backend_id
) const {
1422 int cc_int_id
= BackendIDToInt(cc_backend_id
);
1423 int profile_int_id
= BackendIDToInt(profile_backend_id
);
1425 // Should fit in signed 16-bit integers. We use 16-bits each when combining
1426 // below, and negative frontend IDs have special meaning so we can never use
1428 DCHECK(cc_int_id
<= std::numeric_limits
<int16_t>::max());
1429 DCHECK(profile_int_id
<= std::numeric_limits
<int16_t>::max());
1431 // Put CC in the high half of the bits.
1432 return (cc_int_id
<< std::numeric_limits
<uint16_t>::digits
) | profile_int_id
;
1435 // When receiving IDs (across processes) from the renderer we unpack credit card
1436 // and profile IDs from a single integer. Credit card IDs are stored in the
1437 // high word and profile IDs are stored in the low word.
1438 void AutofillManager::SplitFrontendID(
1440 SuggestionBackendID
* cc_backend_id
,
1441 SuggestionBackendID
* profile_backend_id
) const {
1442 int cc_int_id
= (frontend_id
>> std::numeric_limits
<uint16_t>::digits
) &
1443 std::numeric_limits
<uint16_t>::max();
1444 int profile_int_id
= frontend_id
& std::numeric_limits
<uint16_t>::max();
1446 *cc_backend_id
= IntToBackendID(cc_int_id
);
1447 *profile_backend_id
= IntToBackendID(profile_int_id
);
1450 void AutofillManager::UpdateInitialInteractionTimestamp(
1451 const TimeTicks
& interaction_timestamp
) {
1452 if (initial_interaction_timestamp_
.is_null() ||
1453 interaction_timestamp
< initial_interaction_timestamp_
) {
1454 initial_interaction_timestamp_
= interaction_timestamp
;
1458 bool AutofillManager::ShouldUploadForm(const FormStructure
& form
) {
1459 if (!IsAutofillEnabled())
1462 if (driver_
->IsOffTheRecord())
1465 // Disregard forms that we wouldn't ever autofill in the first place.
1466 if (!form
.ShouldBeParsed())
1472 } // namespace autofill