Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / autofill_manager.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/autofill/core/browser/autofill_manager.h"
6
7 #include <stddef.h>
8
9 #include <limits>
10 #include <map>
11 #include <set>
12 #include <utility>
13
14 #include "base/bind.h"
15 #include "base/command_line.h"
16 #include "base/guid.h"
17 #include "base/logging.h"
18 #include "base/prefs/pref_service.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_util.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "base/threading/sequenced_worker_pool.h"
23 #include "components/autofill/core/browser/autocomplete_history_manager.h"
24 #include "components/autofill/core/browser/autofill_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"
49 #include "url/gurl.h"
50
51 namespace autofill {
52
53 typedef PersonalDataManager::GUIDPair GUIDPair;
54
55 using base::TimeTicks;
56
57 namespace {
58
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;
63
64 const size_t kMaxRecentFormSignaturesToRemember = 3;
65
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;
69
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());
78
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;
84
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]);
93     }
94   }
95
96   values->swap(values_copy);
97   labels->swap(labels_copy);
98   icons->swap(icons_copy);
99   unique_ids->swap(unique_ids_copy);
100 }
101
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|
104 // is auto-filled.
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) {
112       return true;
113     }
114   }
115
116   return false;
117 }
118
119 bool FormIsHTTPS(const FormStructure& form) {
120   // TODO(blundell): Change this to use a constant once crbug.com/306258 is
121   // fixed.
122   return form.source_url().SchemeIs("https");
123 }
124
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;
140
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);
146     }
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);
150     }
151
152     if (matching_types.empty())
153       matching_types.insert(UNKNOWN_TYPE);
154
155     field->set_possible_types(matching_types);
156   }
157 }
158
159 }  // namespace
160
161 AutofillManager::AutofillManager(
162     AutofillDriver* driver,
163     AutofillClient* client,
164     const std::string& app_locale,
165     AutofillDownloadManagerState enable_download_manager)
166     : driver_(driver),
167       client_(client),
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));
185   }
186 }
187
188 AutofillManager::~AutofillManager() {}
189
190 // static
191 void AutofillManager::RegisterProfilePrefs(
192     user_prefs::PrefRegistrySyncable* registry) {
193   registry->RegisterBooleanPref(
194       prefs::kAutofillEnabled,
195       true,
196       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
197 #if defined(OS_MACOSX)
198   registry->RegisterBooleanPref(
199       prefs::kAutofillAuxiliaryProfilesEnabled,
200       true,
201       user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
202 #else  // defined(OS_MACOSX)
203   registry->RegisterBooleanPref(
204       prefs::kAutofillAuxiliaryProfilesEnabled,
205       false,
206       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
207 #endif  // defined(OS_MACOSX)
208 #if defined(OS_MACOSX)
209   registry->RegisterBooleanPref(
210       prefs::kAutofillMacAddressBookQueried,
211       false,
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);
222
223 #if defined(OS_MACOSX) && !defined(OS_IOS)
224   registry->RegisterBooleanPref(
225       prefs::kAutofillUseMacAddressBook,
226       false,
227       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
228   registry->RegisterIntegerPref(
229       prefs::kAutofillMacAddressBookShowedCount,
230       0,
231       user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
232 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
233 }
234
235 #if defined(OS_MACOSX) && !defined(OS_IOS)
236 void AutofillManager::MigrateUserPrefs(PrefService* prefs) {
237   const PrefService::Preference* pref =
238       prefs->FindPreference(prefs::kAutofillUseMacAddressBook);
239
240   // If the pref is not its default value, then the migration has already been
241   // performed.
242   if (!pref->IsDefaultValue())
243     return;
244
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);
251
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));
257     return;
258   }
259
260   bool accessed;
261   bool enabled;
262   bool success = pref_accessed->GetValue()->GetAsBoolean(&accessed);
263   DCHECK(success);
264   success = pref_enabled->GetValue()->GetAsBoolean(&enabled);
265   DCHECK(success);
266
267   prefs->SetBoolean(prefs::kAutofillUseMacAddressBook, accessed && enabled);
268 }
269 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
270
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);
277 }
278
279 void AutofillManager::ShowAutofillSettings() {
280   client_->ShowAutofillSettings();
281 }
282
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)
288     return false;
289
290   FormStructure* form_structure = NULL;
291   AutofillField* autofill_field = NULL;
292   if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
293     return false;
294
295   if (!form_structure->IsAutofillable())
296     return false;
297
298   return personal_data_->ShouldShowAccessAddressBookSuggestion(
299       autofill_field->Type());
300 }
301
302 bool AutofillManager::AccessAddressBook() {
303   if (!personal_data_)
304     return false;
305   return personal_data_->AccessAddressBook();
306 }
307
308 void AutofillManager::ShowedAccessAddressBookPrompt() {
309   if (!personal_data_)
310     return;
311   return personal_data_->ShowedAccessAddressBookPrompt();
312 }
313
314 int AutofillManager::AccessAddressBookPromptCount() {
315   if (!personal_data_)
316     return 0;
317   return personal_data_->AccessAddressBookPromptCount();
318 }
319 #endif  // defined(OS_MACOSX) && !defined(OS_IOS)
320
321 bool AutofillManager::OnFormSubmitted(const FormData& form,
322                                       const TimeTicks& timestamp) {
323   if (!IsValidFormData(form))
324     return false;
325
326   // Let Autocomplete know as well.
327   autocomplete_history_manager_->OnFormSubmitted(form);
328
329   // Grab a copy of the form data.
330   scoped_ptr<FormStructure> submitted_form(new FormStructure(form));
331
332   if (!ShouldUploadForm(*submitted_form))
333     return false;
334
335   // Don't save data that was submitted through JavaScript.
336   if (!form.user_submitted)
337     return false;
338
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))
343     return false;
344
345   submitted_form->UpdateFromCache(*cached_submitted_form);
346   if (submitted_form->IsAutofillable())
347     ImportFormData(*submitted_form);
348
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
356     // separate thread.
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);
362     }
363
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);
369     }
370
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(
375         FROM_HERE,
376         base::Bind(&DeterminePossibleFieldTypesForUpload,
377                    copied_profiles,
378                    copied_credit_cards,
379                    app_locale_,
380                    raw_submitted_form),
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_,
386                    timestamp));
387   }
388
389   return true;
390 }
391
392 void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms,
393                                   const TimeTicks& timestamp) {
394   if (!IsValidFormDataVector(forms))
395     return;
396
397   if (!driver_->RendererIsAvailable())
398     return;
399
400   bool enabled = IsAutofillEnabled();
401   if (!has_logged_autofill_enabled_) {
402     metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled);
403     has_logged_autofill_enabled_ = true;
404   }
405
406   if (!enabled)
407     return;
408
409   for (size_t i = 0; i < forms.size(); ++i) {
410     forms_loaded_timestamps_[forms[i]] = timestamp;
411   }
412
413   ParseForms(forms);
414 }
415
416 void AutofillManager::OnTextFieldDidChange(const FormData& form,
417                                            const FormFieldData& field,
418                                            const TimeTicks& timestamp) {
419   if (!IsValidFormData(form) || !IsValidFormFieldData(field))
420     return;
421
422   FormStructure* form_structure = NULL;
423   AutofillField* autofill_field = NULL;
424   if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
425     return;
426
427   if (!user_did_type_) {
428     user_did_type_ = true;
429     metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE);
430   }
431
432   if (autofill_field->is_autofilled) {
433     autofill_field->is_autofilled = false;
434     metric_logger_->LogUserHappinessMetric(
435         AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD);
436
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);
441     }
442   }
443
444   UpdateInitialInteractionTimestamp(timestamp);
445 }
446
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))
453     return;
454
455   std::vector<base::string16> values;
456   std::vector<base::string16> labels;
457   std::vector<base::string16> icons;
458   std::vector<int> unique_ids;
459
460   external_delegate_->OnQuery(query_id,
461                               form,
462                               field,
463                               bounding_box,
464                               display_warning);
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);
477     } else {
478       GetProfileSuggestions(*form_structure,
479                             field,
480                             *autofill_field,
481                             &values,
482                             &labels,
483                             &icons,
484                             &unique_ids);
485     }
486
487     DCHECK_EQ(values.size(), labels.size());
488     DCHECK_EQ(values.size(), icons.size());
489     DCHECK_EQ(values.size(), unique_ids.size());
490
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.
495       int warning = 0;
496       if (is_filling_credit_card && !FormIsHTTPS(*form_structure))
497         warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION;
498       if (warning) {
499         values.assign(1, l10n_util::GetStringUTF16(warning));
500         labels.assign(1, base::string16());
501         icons.assign(1, base::string16());
502         unique_ids.assign(1, POPUP_ITEM_ID_WARNING_MESSAGE);
503       } else {
504         bool section_is_autofilled =
505             SectionIsAutofilled(*form_structure, form,
506                                 autofill_field->section());
507         if (section_is_autofilled) {
508           // If the relevant section is auto-filled and the renderer is querying
509           // for suggestions, then the user is editing the value of a field.
510           // In this case, mimic autocomplete: don't display labels or icons,
511           // as that information is redundant.
512           labels.assign(labels.size(), base::string16());
513           icons.assign(icons.size(), base::string16());
514         }
515
516         // When filling credit card suggestions, the values and labels are
517         // typically obfuscated, which makes detecting duplicates hard.  Since
518         // duplicates only tend to be a problem when filling address forms
519         // anyway, only don't de-dup credit card suggestions.
520         if (!is_filling_credit_card)
521           RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
522
523         // The first time we show suggestions on this page, log the number of
524         // suggestions shown.
525         if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
526           metric_logger_->LogAddressSuggestionsCount(values.size());
527           has_logged_address_suggestions_count_ = true;
528         }
529       }
530     }
531   }
532
533   // Add the results from AutoComplete.  They come back asynchronously, so we
534   // hand off what we generated and they will send the results back to the
535   // renderer.
536   autocomplete_history_manager_->OnGetAutocompleteSuggestions(
537       query_id, field.name, field.value, field.form_control_type, values,
538       labels, icons, unique_ids);
539 }
540
541 void AutofillManager::FillOrPreviewForm(
542     AutofillDriver::RendererFormDataAction action,
543     int query_id,
544     const FormData& form,
545     const FormFieldData& field,
546     int unique_id) {
547   if (!IsValidFormData(form) || !IsValidFormFieldData(field))
548     return;
549
550   const AutofillDataModel* data_model = NULL;
551   size_t variant = 0;
552   FormStructure* form_structure = NULL;
553   AutofillField* autofill_field = NULL;
554   bool is_credit_card = false;
555   // NOTE: RefreshDataModels may invalidate |data_model| because it causes the
556   // PersonalDataManager to reload Mac address book entries. Thus it must come
557   // before GetProfileOrCreditCard.
558   if (!RefreshDataModels() ||
559       !driver_->RendererIsAvailable() ||
560       !GetProfileOrCreditCard(
561           unique_id, &data_model, &variant, &is_credit_card) ||
562       !GetCachedFormAndField(form, field, &form_structure, &autofill_field))
563     return;
564
565   DCHECK(form_structure);
566   DCHECK(autofill_field);
567
568   FormData result = form;
569
570   base::string16 profile_full_name;
571   std::string profile_language_code;
572   if (!is_credit_card) {
573     profile_full_name = data_model->GetInfo(
574         AutofillType(NAME_FULL), app_locale_);
575     profile_language_code =
576         static_cast<const AutofillProfile*>(data_model)->language_code();
577   }
578
579   // If the relevant section is auto-filled, we should fill |field| but not the
580   // rest of the form.
581   if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) {
582     for (std::vector<FormFieldData>::iterator iter = result.fields.begin();
583          iter != result.fields.end(); ++iter) {
584       if ((*iter) == field) {
585         base::string16 value = data_model->GetInfoForVariant(
586             autofill_field->Type(), variant, app_locale_);
587         if (AutofillField::FillFormField(*autofill_field,
588                                          value,
589                                          profile_language_code,
590                                          app_locale_,
591                                          &(*iter))) {
592           // Mark the cached field as autofilled, so that we can detect when a
593           // user edits an autofilled field (for metrics).
594           autofill_field->is_autofilled = true;
595
596           // Mark the field as autofilled when a non-empty value is assigned to
597           // it. This allows the renderer to distinguish autofilled fields from
598           // fields with non-empty values, such as select-one fields.
599           iter->is_autofilled = true;
600
601           if (!is_credit_card && !value.empty())
602             client_->DidFillOrPreviewField(value, profile_full_name);
603         }
604         break;
605       }
606     }
607
608     driver_->SendFormDataToRenderer(query_id, action, result);
609     return;
610   }
611
612   // Cache the field type for the field from which the user initiated autofill.
613   FieldTypeGroup initiating_group_type = autofill_field->Type().group();
614   DCHECK_EQ(form_structure->field_count(), form.fields.size());
615   for (size_t i = 0; i < form_structure->field_count(); ++i) {
616     if (form_structure->field(i)->section() != autofill_field->section())
617       continue;
618
619     DCHECK_EQ(*form_structure->field(i), result.fields[i]);
620
621     const AutofillField* cached_field = form_structure->field(i);
622     FieldTypeGroup field_group_type = cached_field->Type().group();
623     if (field_group_type != NO_GROUP) {
624       // If the field being filled is either
625       //   (a) the field that the user initiated the fill from, or
626       //   (b) part of the same logical unit, e.g. name or phone number,
627       // then take the multi-profile "variant" into account.
628       // Otherwise fill with the default (zeroth) variant.
629       size_t use_variant = 0;
630       if (result.fields[i] == field ||
631           field_group_type == initiating_group_type) {
632         use_variant = variant;
633       }
634       base::string16 value = data_model->GetInfoForVariant(
635           cached_field->Type(), use_variant, app_locale_);
636
637       // Must match ForEachMatchingFormField() in form_autofill_util.cc.
638       // Only notify autofilling of empty fields and the field that initiated
639       // the filling (note that "select-one" controls may not be empty but will
640       // still be autofilled).
641       bool should_notify =
642           !is_credit_card &&
643           !value.empty() &&
644           (result.fields[i] == field ||
645            result.fields[i].form_control_type == "select-one" ||
646            result.fields[i].value.empty());
647       if (AutofillField::FillFormField(*cached_field,
648                                        value,
649                                        profile_language_code,
650                                        app_locale_,
651                                        &result.fields[i])) {
652         // Mark the cached field as autofilled, so that we can detect when a
653         // user edits an autofilled field (for metrics).
654         form_structure->field(i)->is_autofilled = true;
655
656         // Mark the field as autofilled when a non-empty value is assigned to
657         // it. This allows the renderer to distinguish autofilled fields from
658         // fields with non-empty values, such as select-one fields.
659         result.fields[i].is_autofilled = true;
660
661         if (should_notify)
662           client_->DidFillOrPreviewField(value, profile_full_name);
663       }
664     }
665   }
666
667   autofilled_form_signatures_.push_front(form_structure->FormSignature());
668   // Only remember the last few forms that we've seen, both to avoid false
669   // positives and to avoid wasting memory.
670   if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember)
671     autofilled_form_signatures_.pop_back();
672
673   driver_->SendFormDataToRenderer(query_id, action, result);
674 }
675
676 void AutofillManager::OnDidPreviewAutofillFormData() {
677   if (test_delegate_)
678     test_delegate_->DidPreviewFormData();
679 }
680
681 void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) {
682   if (test_delegate_)
683     test_delegate_->DidFillFormData();
684
685   metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL);
686   if (!user_did_autofill_) {
687     user_did_autofill_ = true;
688     metric_logger_->LogUserHappinessMetric(
689         AutofillMetrics::USER_DID_AUTOFILL_ONCE);
690   }
691
692   UpdateInitialInteractionTimestamp(timestamp);
693 }
694
695 void AutofillManager::DidShowSuggestions(bool is_new_popup) {
696   if (test_delegate_)
697     test_delegate_->DidShowSuggestions();
698
699   if (is_new_popup) {
700     metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN);
701
702     if (!did_show_suggestions_) {
703       did_show_suggestions_ = true;
704       metric_logger_->LogUserHappinessMetric(
705           AutofillMetrics::SUGGESTIONS_SHOWN_ONCE);
706     }
707   }
708 }
709
710 void AutofillManager::OnHidePopup() {
711   if (!IsAutofillEnabled())
712     return;
713
714   autocomplete_history_manager_->CancelPendingQuery();
715   client_->HideAutofillPopup();
716 }
717
718 void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) {
719   const AutofillDataModel* data_model = NULL;
720   size_t variant = 0;
721   bool unused_is_credit_card = false;
722   if (!GetProfileOrCreditCard(
723           unique_id, &data_model, &variant, &unused_is_credit_card)) {
724     NOTREACHED();
725     return;
726   }
727
728   // TODO(csharp): If we are dealing with a variant only the variant should
729   // be deleted, instead of doing nothing.
730   // http://crbug.com/124211
731   if (variant != 0)
732     return;
733
734   personal_data_->RemoveByGUID(data_model->guid());
735 }
736
737 void AutofillManager::RemoveAutocompleteEntry(const base::string16& name,
738                                               const base::string16& value) {
739   autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value);
740 }
741
742 const std::vector<FormStructure*>& AutofillManager::GetFormStructures() {
743   return form_structures_.get();
744 }
745
746 void AutofillManager::SetTestDelegate(AutofillManagerTestDelegate* delegate) {
747   test_delegate_ = delegate;
748 }
749
750 void AutofillManager::OnSetDataList(const std::vector<base::string16>& values,
751                                     const std::vector<base::string16>& labels) {
752   if (!IsValidString16Vector(values) ||
753       !IsValidString16Vector(labels) ||
754       values.size() != labels.size())
755     return;
756
757   external_delegate_->SetCurrentDataListValues(values, labels);
758 }
759
760 void AutofillManager::OnLoadedServerPredictions(
761     const std::string& response_xml) {
762   // Parse and store the server predictions.
763   FormStructure::ParseQueryResponse(response_xml,
764                                     form_structures_.get(),
765                                     *metric_logger_);
766
767   // Forward form structures to the password generation manager to detect
768   // account creation forms.
769   client_->DetectAccountCreationForms(form_structures_.get());
770
771   // If the corresponding flag is set, annotate forms with the predicted types.
772   driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
773 }
774
775 void AutofillManager::OnDidEndTextFieldEditing() {
776   external_delegate_->DidEndTextFieldEditing();
777 }
778
779 bool AutofillManager::IsAutofillEnabled() const {
780   return client_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
781 }
782
783 void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
784   scoped_ptr<CreditCard> imported_credit_card;
785   if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
786     return;
787
788   // If credit card information was submitted, we need to confirm whether to
789   // save it.
790   if (imported_credit_card) {
791     client_->ConfirmSaveCreditCard(
792         *metric_logger_,
793         base::Bind(
794             base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard),
795             base::Unretained(personal_data_),
796             *imported_credit_card));
797   }
798 }
799
800 // Note that |submitted_form| is passed as a pointer rather than as a reference
801 // so that we can get memory management right across threads.  Note also that we
802 // explicitly pass in all the time stamps of interest, as the cached ones might
803 // get reset before this method executes.
804 void AutofillManager::UploadFormDataAsyncCallback(
805     const FormStructure* submitted_form,
806     const TimeTicks& load_time,
807     const TimeTicks& interaction_time,
808     const TimeTicks& submission_time) {
809   submitted_form->LogQualityMetrics(*metric_logger_,
810                                     load_time,
811                                     interaction_time,
812                                     submission_time);
813
814   if (submitted_form->ShouldBeCrowdsourced())
815     UploadFormData(*submitted_form);
816 }
817
818 void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
819   if (!download_manager_)
820     return;
821
822   // Check if the form is among the forms that were recently auto-filled.
823   bool was_autofilled = false;
824   std::string form_signature = submitted_form.FormSignature();
825   for (std::list<std::string>::const_iterator it =
826            autofilled_form_signatures_.begin();
827        it != autofilled_form_signatures_.end() && !was_autofilled;
828        ++it) {
829     if (*it == form_signature)
830       was_autofilled = true;
831   }
832
833   ServerFieldTypeSet non_empty_types;
834   personal_data_->GetNonEmptyTypes(&non_empty_types);
835
836   download_manager_->StartUploadRequest(submitted_form, was_autofilled,
837                                         non_empty_types);
838 }
839
840 bool AutofillManager::UploadPasswordForm(
841     const FormData& form,
842     const ServerFieldType& password_type) {
843   FormStructure form_structure(form);
844
845   if (!ShouldUploadForm(form_structure))
846     return false;
847
848   if (!form_structure.ShouldBeCrowdsourced())
849     return false;
850
851   // Find the first password field to label. We don't try to label anything
852   // else.
853   bool found_password_field = false;
854   for (size_t i = 0; i < form_structure.field_count(); ++i) {
855     AutofillField* field = form_structure.field(i);
856
857     ServerFieldTypeSet types;
858     if (!found_password_field && field->form_control_type == "password") {
859       types.insert(password_type);
860       found_password_field = true;
861     } else {
862       types.insert(UNKNOWN_TYPE);
863     }
864     field->set_possible_types(types);
865   }
866   DCHECK(found_password_field);
867
868   // Only one field type should be present.
869   ServerFieldTypeSet available_field_types;
870   available_field_types.insert(password_type);
871
872   // Force uploading as these events are relatively rare and we want to make
873   // sure to receive them. It also makes testing easier if these requests
874   // always pass.
875   form_structure.set_upload_required(UPLOAD_REQUIRED);
876
877   if (!download_manager_)
878     return false;
879
880   return download_manager_->StartUploadRequest(form_structure,
881                                                false /* was_autofilled */,
882                                                available_field_types);
883 }
884
885 void AutofillManager::Reset() {
886   form_structures_.clear();
887   has_logged_autofill_enabled_ = false;
888   has_logged_address_suggestions_count_ = false;
889   did_show_suggestions_ = false;
890   user_did_type_ = false;
891   user_did_autofill_ = false;
892   user_did_edit_autofilled_field_ = false;
893   forms_loaded_timestamps_.clear();
894   initial_interaction_timestamp_ = TimeTicks();
895   external_delegate_->Reset();
896 }
897
898 AutofillManager::AutofillManager(AutofillDriver* driver,
899                                  AutofillClient* client,
900                                  PersonalDataManager* personal_data)
901     : driver_(driver),
902       client_(client),
903       app_locale_("en-US"),
904       personal_data_(personal_data),
905       autocomplete_history_manager_(
906           new AutocompleteHistoryManager(driver, client)),
907       metric_logger_(new AutofillMetrics),
908       has_logged_autofill_enabled_(false),
909       has_logged_address_suggestions_count_(false),
910       did_show_suggestions_(false),
911       user_did_type_(false),
912       user_did_autofill_(false),
913       user_did_edit_autofilled_field_(false),
914       external_delegate_(NULL),
915       test_delegate_(NULL),
916       weak_ptr_factory_(this) {
917   DCHECK(driver_);
918   DCHECK(client_);
919 }
920
921 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
922   metric_logger_.reset(metric_logger);
923 }
924
925 bool AutofillManager::RefreshDataModels() const {
926   if (!IsAutofillEnabled())
927     return false;
928
929   // No autofill data to return if the profiles are empty.
930   if (personal_data_->GetProfiles().empty() &&
931       personal_data_->GetCreditCards().empty()) {
932     return false;
933   }
934
935   return true;
936 }
937
938 bool AutofillManager::GetProfileOrCreditCard(
939     int unique_id,
940     const AutofillDataModel** data_model,
941     size_t* variant,
942     bool* is_credit_card) const {
943   // Unpack the |unique_id| into component parts.
944   GUIDPair credit_card_guid;
945   GUIDPair profile_guid;
946   UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
947   DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
948          !base::IsValidGUID(profile_guid.first));
949   *is_credit_card = false;
950
951   // Find the profile that matches the |profile_guid|, if one is specified.
952   // Otherwise find the credit card that matches the |credit_card_guid|,
953   // if specified.
954   if (base::IsValidGUID(profile_guid.first)) {
955     *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
956     *variant = profile_guid.second;
957   } else if (base::IsValidGUID(credit_card_guid.first)) {
958     *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
959     *variant = credit_card_guid.second;
960     *is_credit_card = true;
961   }
962
963   return !!*data_model;
964 }
965
966 bool AutofillManager::FindCachedForm(const FormData& form,
967                                      FormStructure** form_structure) const {
968   // Find the FormStructure that corresponds to |form|.
969   // Scan backward through the cached |form_structures_|, as updated versions of
970   // forms are added to the back of the list, whereas original versions of these
971   // forms might appear toward the beginning of the list.  The communication
972   // protocol with the crowdsourcing server does not permit us to discard the
973   // original versions of the forms.
974   *form_structure = NULL;
975   for (std::vector<FormStructure*>::const_reverse_iterator iter =
976            form_structures_.rbegin();
977        iter != form_structures_.rend(); ++iter) {
978     if (**iter == form) {
979       *form_structure = *iter;
980
981       // The same form might be cached with multiple field counts: in some
982       // cases, non-autofillable fields are filtered out, whereas in other cases
983       // they are not.  To avoid thrashing the cache, keep scanning until we
984       // find a cached version with the same number of fields, if there is one.
985       if ((*iter)->field_count() == form.fields.size())
986         break;
987     }
988   }
989
990   if (!(*form_structure))
991     return false;
992
993   return true;
994 }
995
996 bool AutofillManager::GetCachedFormAndField(const FormData& form,
997                                             const FormFieldData& field,
998                                             FormStructure** form_structure,
999                                             AutofillField** autofill_field) {
1000   // Find the FormStructure that corresponds to |form|.
1001   // If we do not have this form in our cache but it is parseable, we'll add it
1002   // in the call to |UpdateCachedForm()|.
1003   if (!FindCachedForm(form, form_structure) &&
1004       !FormStructure(form).ShouldBeParsed()) {
1005     return false;
1006   }
1007
1008   // Update the cached form to reflect any dynamic changes to the form data, if
1009   // necessary.
1010   if (!UpdateCachedForm(form, *form_structure, form_structure))
1011     return false;
1012
1013   // No data to return if there are no auto-fillable fields.
1014   if (!(*form_structure)->autofill_count())
1015     return false;
1016
1017   // Find the AutofillField that corresponds to |field|.
1018   *autofill_field = NULL;
1019   for (std::vector<AutofillField*>::const_iterator iter =
1020            (*form_structure)->begin();
1021        iter != (*form_structure)->end(); ++iter) {
1022     if ((**iter) == field) {
1023       *autofill_field = *iter;
1024       break;
1025     }
1026   }
1027
1028   // Even though we always update the cache, the field might not exist if the
1029   // website disables autocomplete while the user is interacting with the form.
1030   // See http://crbug.com/160476
1031   return *autofill_field != NULL;
1032 }
1033
1034 bool AutofillManager::UpdateCachedForm(const FormData& live_form,
1035                                        const FormStructure* cached_form,
1036                                        FormStructure** updated_form) {
1037   bool needs_update =
1038       (!cached_form ||
1039        live_form.fields.size() != cached_form->field_count());
1040   for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
1041     needs_update = *cached_form->field(i) != live_form.fields[i];
1042   }
1043
1044   if (!needs_update)
1045     return true;
1046
1047   if (form_structures_.size() >= kMaxFormCacheSize)
1048     return false;
1049
1050   // Add the new or updated form to our cache.
1051   form_structures_.push_back(new FormStructure(live_form));
1052   *updated_form = *form_structures_.rbegin();
1053   (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
1054
1055   // If we have cached data, propagate it to the updated form.
1056   if (cached_form) {
1057     std::map<base::string16, const AutofillField*> cached_fields;
1058     for (size_t i = 0; i < cached_form->field_count(); ++i) {
1059       const AutofillField* field = cached_form->field(i);
1060       cached_fields[field->unique_name()] = field;
1061     }
1062
1063     for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
1064       AutofillField* field = (*updated_form)->field(i);
1065       std::map<base::string16, const AutofillField*>::iterator cached_field =
1066           cached_fields.find(field->unique_name());
1067       if (cached_field != cached_fields.end()) {
1068         field->set_server_type(cached_field->second->server_type());
1069         field->is_autofilled = cached_field->second->is_autofilled;
1070       }
1071     }
1072
1073     // Note: We _must not_ remove the original version of the cached form from
1074     // the list of |form_structures_|.  Otherwise, we break parsing of the
1075     // crowdsourcing server's response to our query.
1076   }
1077
1078   // Annotate the updated form with its predicted types.
1079   std::vector<FormStructure*> forms(1, *updated_form);
1080   driver_->SendAutofillTypePredictionsToRenderer(forms);
1081
1082   return true;
1083 }
1084
1085 void AutofillManager::GetProfileSuggestions(
1086     const FormStructure& form,
1087     const FormFieldData& field,
1088     const AutofillField& autofill_field,
1089     std::vector<base::string16>* values,
1090     std::vector<base::string16>* labels,
1091     std::vector<base::string16>* icons,
1092     std::vector<int>* unique_ids) const {
1093   std::vector<ServerFieldType> field_types(form.field_count());
1094   for (size_t i = 0; i < form.field_count(); ++i) {
1095     field_types.push_back(form.field(i)->Type().GetStorableType());
1096   }
1097   std::vector<GUIDPair> guid_pairs;
1098
1099   personal_data_->GetProfileSuggestions(
1100       autofill_field.Type(), field.value, field.is_autofilled, field_types,
1101       base::Callback<bool(const AutofillProfile&)>(),
1102       values, labels, icons, &guid_pairs);
1103
1104   // Adjust phone number to display in prefix/suffix case.
1105   if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) {
1106     for (size_t i = 0; i < values->size(); ++i) {
1107       (*values)[i] = AutofillField::GetPhoneNumberValue(
1108           autofill_field, (*values)[i], field);
1109     }
1110   }
1111
1112   for (size_t i = 0; i < guid_pairs.size(); ++i) {
1113     unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1114                                     guid_pairs[i]));
1115   }
1116 }
1117
1118 void AutofillManager::GetCreditCardSuggestions(
1119     const FormFieldData& field,
1120     const AutofillType& type,
1121     std::vector<base::string16>* values,
1122     std::vector<base::string16>* labels,
1123     std::vector<base::string16>* icons,
1124     std::vector<int>* unique_ids) const {
1125   std::vector<GUIDPair> guid_pairs;
1126   personal_data_->GetCreditCardSuggestions(
1127       type, field.value, values, labels, icons, &guid_pairs);
1128
1129   for (size_t i = 0; i < guid_pairs.size(); ++i) {
1130     unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1131   }
1132 }
1133
1134 void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1135   std::vector<FormStructure*> non_queryable_forms;
1136   for (std::vector<FormData>::const_iterator iter = forms.begin();
1137        iter != forms.end(); ++iter) {
1138     scoped_ptr<FormStructure> form_structure(new FormStructure(*iter));
1139     if (!form_structure->ShouldBeParsed())
1140       continue;
1141
1142     form_structure->DetermineHeuristicTypes(*metric_logger_);
1143
1144     // Set aside forms with method GET or author-specified types, so that they
1145     // are not included in the query to the server.
1146     if (form_structure->ShouldBeCrowdsourced())
1147       form_structures_.push_back(form_structure.release());
1148     else
1149       non_queryable_forms.push_back(form_structure.release());
1150   }
1151
1152   if (!form_structures_.empty() && download_manager_) {
1153     // Query the server if we have at least one of the forms were parsed.
1154     download_manager_->StartQueryRequest(form_structures_.get(),
1155                                         *metric_logger_);
1156   }
1157
1158   for (std::vector<FormStructure*>::const_iterator iter =
1159            non_queryable_forms.begin();
1160        iter != non_queryable_forms.end(); ++iter) {
1161     form_structures_.push_back(*iter);
1162   }
1163
1164   if (!form_structures_.empty())
1165     metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1166
1167   // For the |non_queryable_forms|, we have all the field type info we're ever
1168   // going to get about them.  For the other forms, we'll wait until we get a
1169   // response from the server.
1170   driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1171 }
1172
1173 int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1174   if (!base::IsValidGUID(guid.first))
1175     return 0;
1176
1177   std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1178   if (iter == guid_id_map_.end()) {
1179     int id = guid_id_map_.size() + 1;
1180     guid_id_map_[guid] = id;
1181     id_guid_map_[id] = guid;
1182     return id;
1183   } else {
1184     return iter->second;
1185   }
1186 }
1187
1188 const GUIDPair AutofillManager::IDToGUID(int id) const {
1189   if (id == 0)
1190     return GUIDPair(std::string(), 0);
1191
1192   std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1193   if (iter == id_guid_map_.end()) {
1194     NOTREACHED();
1195     return GUIDPair(std::string(), 0);
1196   }
1197
1198   return iter->second;
1199 }
1200
1201 // When sending IDs (across processes) to the renderer we pack credit card and
1202 // profile IDs into a single integer.  Credit card IDs are sent in the high
1203 // word and profile IDs are sent in the low word.
1204 int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1205                                const GUIDPair& profile_guid) const {
1206   int cc_id = GUIDToID(cc_guid);
1207   int profile_id = GUIDToID(profile_guid);
1208
1209   DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1210   DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1211
1212   return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1213 }
1214
1215 // When receiving IDs (across processes) from the renderer we unpack credit card
1216 // and profile IDs from a single integer.  Credit card IDs are stored in the
1217 // high word and profile IDs are stored in the low word.
1218 void AutofillManager::UnpackGUIDs(int id,
1219                                   GUIDPair* cc_guid,
1220                                   GUIDPair* profile_guid) const {
1221   int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1222       std::numeric_limits<unsigned short>::max();
1223   int profile_id = id & std::numeric_limits<unsigned short>::max();
1224
1225   *cc_guid = IDToGUID(cc_id);
1226   *profile_guid = IDToGUID(profile_id);
1227 }
1228
1229 void AutofillManager::UpdateInitialInteractionTimestamp(
1230     const TimeTicks& interaction_timestamp) {
1231   if (initial_interaction_timestamp_.is_null() ||
1232       interaction_timestamp < initial_interaction_timestamp_) {
1233     initial_interaction_timestamp_ = interaction_timestamp;
1234   }
1235 }
1236
1237 bool AutofillManager::ShouldUploadForm(const FormStructure& form) {
1238   if (!IsAutofillEnabled())
1239     return false;
1240
1241   if (driver_->IsOffTheRecord())
1242     return false;
1243
1244   // Disregard forms that we wouldn't ever autofill in the first place.
1245   if (!form.ShouldBeParsed())
1246     return false;
1247
1248   return true;
1249 }
1250
1251 }  // namespace autofill