Update To 11.40.268.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       }
499       if (warning) {
500         values.assign(1, l10n_util::GetStringUTF16(warning));
501         labels.assign(1, base::string16());
502         icons.assign(1, base::string16());
503         unique_ids.assign(1, POPUP_ITEM_ID_WARNING_MESSAGE);
504       } else {
505         bool section_is_autofilled =
506             SectionIsAutofilled(*form_structure, form,
507                                 autofill_field->section());
508         if (section_is_autofilled) {
509           // If the relevant section is auto-filled and the renderer is querying
510           // for suggestions, then the user is editing the value of a field.
511           // In this case, mimic autocomplete: don't display labels or icons,
512           // as that information is redundant.
513           labels.assign(labels.size(), base::string16());
514           icons.assign(icons.size(), base::string16());
515         }
516
517         // When filling credit card suggestions, the values and labels are
518         // typically obfuscated, which makes detecting duplicates hard.  Since
519         // duplicates only tend to be a problem when filling address forms
520         // anyway, only don't de-dup credit card suggestions.
521         if (!is_filling_credit_card)
522           RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
523
524         // The first time we show suggestions on this page, log the number of
525         // suggestions shown.
526         if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
527           metric_logger_->LogAddressSuggestionsCount(values.size());
528           has_logged_address_suggestions_count_ = true;
529         }
530       }
531     }
532   }
533
534   if (field.should_autocomplete) {
535     // Add the results from AutoComplete.  They come back asynchronously, so we
536     // hand off what we generated and they will send the results back to the
537     // renderer.
538     autocomplete_history_manager_->OnGetAutocompleteSuggestions(
539         query_id, field.name, field.value, field.form_control_type, values,
540         labels, icons, unique_ids);
541   } else {
542     // Autocomplete is disabled for this field; only pass back Autofill
543     // suggestions.
544     autocomplete_history_manager_->CancelPendingQuery();
545     external_delegate_->OnSuggestionsReturned(
546         query_id, values, labels, icons, unique_ids);
547   }
548 }
549
550 void AutofillManager::FillOrPreviewForm(
551     AutofillDriver::RendererFormDataAction action,
552     int query_id,
553     const FormData& form,
554     const FormFieldData& field,
555     int unique_id) {
556   if (!IsValidFormData(form) || !IsValidFormFieldData(field))
557     return;
558
559   const AutofillDataModel* data_model = NULL;
560   size_t variant = 0;
561   FormStructure* form_structure = NULL;
562   AutofillField* autofill_field = NULL;
563   bool is_credit_card = false;
564   // NOTE: RefreshDataModels may invalidate |data_model| because it causes the
565   // PersonalDataManager to reload Mac address book entries. Thus it must come
566   // before GetProfileOrCreditCard.
567   if (!RefreshDataModels() ||
568       !driver_->RendererIsAvailable() ||
569       !GetProfileOrCreditCard(
570           unique_id, &data_model, &variant, &is_credit_card) ||
571       !GetCachedFormAndField(form, field, &form_structure, &autofill_field))
572     return;
573
574   DCHECK(form_structure);
575   DCHECK(autofill_field);
576
577   FormData result = form;
578
579   base::string16 profile_full_name;
580   std::string profile_language_code;
581   if (!is_credit_card) {
582     profile_full_name = data_model->GetInfo(
583         AutofillType(NAME_FULL), app_locale_);
584     profile_language_code =
585         static_cast<const AutofillProfile*>(data_model)->language_code();
586   }
587
588   // If the relevant section is auto-filled, we should fill |field| but not the
589   // rest of the form.
590   if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) {
591     for (std::vector<FormFieldData>::iterator iter = result.fields.begin();
592          iter != result.fields.end(); ++iter) {
593       if ((*iter) == field) {
594         base::string16 value = data_model->GetInfoForVariant(
595             autofill_field->Type(), variant, app_locale_);
596         if (AutofillField::FillFormField(*autofill_field,
597                                          value,
598                                          profile_language_code,
599                                          app_locale_,
600                                          &(*iter))) {
601           // Mark the cached field as autofilled, so that we can detect when a
602           // user edits an autofilled field (for metrics).
603           autofill_field->is_autofilled = true;
604
605           // Mark the field as autofilled when a non-empty value is assigned to
606           // it. This allows the renderer to distinguish autofilled fields from
607           // fields with non-empty values, such as select-one fields.
608           iter->is_autofilled = true;
609
610           if (!is_credit_card && !value.empty())
611             client_->DidFillOrPreviewField(value, profile_full_name);
612         }
613         break;
614       }
615     }
616
617     driver_->SendFormDataToRenderer(query_id, action, result);
618     return;
619   }
620
621   // Cache the field type for the field from which the user initiated autofill.
622   FieldTypeGroup initiating_group_type = autofill_field->Type().group();
623   DCHECK_EQ(form_structure->field_count(), form.fields.size());
624   for (size_t i = 0; i < form_structure->field_count(); ++i) {
625     if (form_structure->field(i)->section() != autofill_field->section())
626       continue;
627
628     DCHECK_EQ(*form_structure->field(i), result.fields[i]);
629
630     const AutofillField* cached_field = form_structure->field(i);
631     FieldTypeGroup field_group_type = cached_field->Type().group();
632     if (field_group_type != NO_GROUP) {
633       // If the field being filled is either
634       //   (a) the field that the user initiated the fill from, or
635       //   (b) part of the same logical unit, e.g. name or phone number,
636       // then take the multi-profile "variant" into account.
637       // Otherwise fill with the default (zeroth) variant.
638       size_t use_variant = 0;
639       if (result.fields[i] == field ||
640           field_group_type == initiating_group_type) {
641         use_variant = variant;
642       }
643       base::string16 value = data_model->GetInfoForVariant(
644           cached_field->Type(), use_variant, app_locale_);
645
646       // Must match ForEachMatchingFormField() in form_autofill_util.cc.
647       // Only notify autofilling of empty fields and the field that initiated
648       // the filling (note that "select-one" controls may not be empty but will
649       // still be autofilled).
650       bool should_notify =
651           !is_credit_card &&
652           !value.empty() &&
653           (result.fields[i] == field ||
654            result.fields[i].form_control_type == "select-one" ||
655            result.fields[i].value.empty());
656       if (AutofillField::FillFormField(*cached_field,
657                                        value,
658                                        profile_language_code,
659                                        app_locale_,
660                                        &result.fields[i])) {
661         // Mark the cached field as autofilled, so that we can detect when a
662         // user edits an autofilled field (for metrics).
663         form_structure->field(i)->is_autofilled = true;
664
665         // Mark the field as autofilled when a non-empty value is assigned to
666         // it. This allows the renderer to distinguish autofilled fields from
667         // fields with non-empty values, such as select-one fields.
668         result.fields[i].is_autofilled = true;
669
670         if (should_notify)
671           client_->DidFillOrPreviewField(value, profile_full_name);
672       }
673     }
674   }
675
676   autofilled_form_signatures_.push_front(form_structure->FormSignature());
677   // Only remember the last few forms that we've seen, both to avoid false
678   // positives and to avoid wasting memory.
679   if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember)
680     autofilled_form_signatures_.pop_back();
681
682   driver_->SendFormDataToRenderer(query_id, action, result);
683 }
684
685 void AutofillManager::OnDidPreviewAutofillFormData() {
686   if (test_delegate_)
687     test_delegate_->DidPreviewFormData();
688 }
689
690 void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) {
691   if (test_delegate_)
692     test_delegate_->DidFillFormData();
693
694   metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL);
695   if (!user_did_autofill_) {
696     user_did_autofill_ = true;
697     metric_logger_->LogUserHappinessMetric(
698         AutofillMetrics::USER_DID_AUTOFILL_ONCE);
699   }
700
701   UpdateInitialInteractionTimestamp(timestamp);
702 }
703
704 void AutofillManager::DidShowSuggestions(bool is_new_popup) {
705   if (test_delegate_)
706     test_delegate_->DidShowSuggestions();
707
708   if (is_new_popup) {
709     metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN);
710
711     if (!did_show_suggestions_) {
712       did_show_suggestions_ = true;
713       metric_logger_->LogUserHappinessMetric(
714           AutofillMetrics::SUGGESTIONS_SHOWN_ONCE);
715     }
716   }
717 }
718
719 void AutofillManager::OnHidePopup() {
720   if (!IsAutofillEnabled())
721     return;
722
723   autocomplete_history_manager_->CancelPendingQuery();
724   client_->HideAutofillPopup();
725 }
726
727 void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) {
728   const AutofillDataModel* data_model = NULL;
729   size_t variant = 0;
730   bool unused_is_credit_card = false;
731   if (!GetProfileOrCreditCard(
732           unique_id, &data_model, &variant, &unused_is_credit_card)) {
733     NOTREACHED();
734     return;
735   }
736
737   // TODO(csharp): If we are dealing with a variant only the variant should
738   // be deleted, instead of doing nothing.
739   // http://crbug.com/124211
740   if (variant != 0)
741     return;
742
743   personal_data_->RemoveByGUID(data_model->guid());
744 }
745
746 void AutofillManager::RemoveAutocompleteEntry(const base::string16& name,
747                                               const base::string16& value) {
748   autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value);
749 }
750
751 const std::vector<FormStructure*>& AutofillManager::GetFormStructures() {
752   return form_structures_.get();
753 }
754
755 void AutofillManager::SetTestDelegate(AutofillManagerTestDelegate* delegate) {
756   test_delegate_ = delegate;
757 }
758
759 void AutofillManager::OnSetDataList(const std::vector<base::string16>& values,
760                                     const std::vector<base::string16>& labels) {
761   if (!IsValidString16Vector(values) ||
762       !IsValidString16Vector(labels) ||
763       values.size() != labels.size())
764     return;
765
766   external_delegate_->SetCurrentDataListValues(values, labels);
767 }
768
769 void AutofillManager::OnLoadedServerPredictions(
770     const std::string& response_xml) {
771   // Parse and store the server predictions.
772   FormStructure::ParseQueryResponse(response_xml,
773                                     form_structures_.get(),
774                                     *metric_logger_);
775
776   // Forward form structures to the password generation manager to detect
777   // account creation forms.
778   client_->DetectAccountCreationForms(form_structures_.get());
779
780   // If the corresponding flag is set, annotate forms with the predicted types.
781   driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
782 }
783
784 void AutofillManager::OnDidEndTextFieldEditing() {
785   external_delegate_->DidEndTextFieldEditing();
786 }
787
788 bool AutofillManager::IsAutofillEnabled() const {
789   return client_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
790 }
791
792 void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
793   scoped_ptr<CreditCard> imported_credit_card;
794   if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
795     return;
796
797   // If credit card information was submitted, we need to confirm whether to
798   // save it.
799   if (imported_credit_card) {
800     client_->ConfirmSaveCreditCard(
801         *metric_logger_,
802         base::Bind(
803             base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard),
804             base::Unretained(personal_data_),
805             *imported_credit_card));
806   }
807 }
808
809 // Note that |submitted_form| is passed as a pointer rather than as a reference
810 // so that we can get memory management right across threads.  Note also that we
811 // explicitly pass in all the time stamps of interest, as the cached ones might
812 // get reset before this method executes.
813 void AutofillManager::UploadFormDataAsyncCallback(
814     const FormStructure* submitted_form,
815     const TimeTicks& load_time,
816     const TimeTicks& interaction_time,
817     const TimeTicks& submission_time) {
818   submitted_form->LogQualityMetrics(*metric_logger_,
819                                     load_time,
820                                     interaction_time,
821                                     submission_time);
822
823   if (submitted_form->ShouldBeCrowdsourced())
824     UploadFormData(*submitted_form);
825 }
826
827 void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
828   if (!download_manager_)
829     return;
830
831   // Check if the form is among the forms that were recently auto-filled.
832   bool was_autofilled = false;
833   std::string form_signature = submitted_form.FormSignature();
834   for (std::list<std::string>::const_iterator it =
835            autofilled_form_signatures_.begin();
836        it != autofilled_form_signatures_.end() && !was_autofilled;
837        ++it) {
838     if (*it == form_signature)
839       was_autofilled = true;
840   }
841
842   ServerFieldTypeSet non_empty_types;
843   personal_data_->GetNonEmptyTypes(&non_empty_types);
844
845   download_manager_->StartUploadRequest(submitted_form, was_autofilled,
846                                         non_empty_types);
847 }
848
849 bool AutofillManager::UploadPasswordForm(
850     const FormData& form,
851     const ServerFieldType& password_type) {
852   FormStructure form_structure(form);
853
854   if (!ShouldUploadForm(form_structure))
855     return false;
856
857   if (!form_structure.ShouldBeCrowdsourced())
858     return false;
859
860   // Find the first password field to label. We don't try to label anything
861   // else.
862   bool found_password_field = false;
863   for (size_t i = 0; i < form_structure.field_count(); ++i) {
864     AutofillField* field = form_structure.field(i);
865
866     ServerFieldTypeSet types;
867     if (!found_password_field && field->form_control_type == "password") {
868       types.insert(password_type);
869       found_password_field = true;
870     } else {
871       types.insert(UNKNOWN_TYPE);
872     }
873     field->set_possible_types(types);
874   }
875   DCHECK(found_password_field);
876
877   // Only one field type should be present.
878   ServerFieldTypeSet available_field_types;
879   available_field_types.insert(password_type);
880
881   // Force uploading as these events are relatively rare and we want to make
882   // sure to receive them. It also makes testing easier if these requests
883   // always pass.
884   form_structure.set_upload_required(UPLOAD_REQUIRED);
885
886   if (!download_manager_)
887     return false;
888
889   return download_manager_->StartUploadRequest(form_structure,
890                                                false /* was_autofilled */,
891                                                available_field_types);
892 }
893
894 void AutofillManager::Reset() {
895   form_structures_.clear();
896   has_logged_autofill_enabled_ = false;
897   has_logged_address_suggestions_count_ = false;
898   did_show_suggestions_ = false;
899   user_did_type_ = false;
900   user_did_autofill_ = false;
901   user_did_edit_autofilled_field_ = false;
902   forms_loaded_timestamps_.clear();
903   initial_interaction_timestamp_ = TimeTicks();
904   external_delegate_->Reset();
905 }
906
907 AutofillManager::AutofillManager(AutofillDriver* driver,
908                                  AutofillClient* client,
909                                  PersonalDataManager* personal_data)
910     : driver_(driver),
911       client_(client),
912       app_locale_("en-US"),
913       personal_data_(personal_data),
914       autocomplete_history_manager_(
915           new AutocompleteHistoryManager(driver, client)),
916       metric_logger_(new AutofillMetrics),
917       has_logged_autofill_enabled_(false),
918       has_logged_address_suggestions_count_(false),
919       did_show_suggestions_(false),
920       user_did_type_(false),
921       user_did_autofill_(false),
922       user_did_edit_autofilled_field_(false),
923       external_delegate_(NULL),
924       test_delegate_(NULL),
925       weak_ptr_factory_(this) {
926   DCHECK(driver_);
927   DCHECK(client_);
928 }
929
930 void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
931   metric_logger_.reset(metric_logger);
932 }
933
934 bool AutofillManager::RefreshDataModels() const {
935   if (!IsAutofillEnabled())
936     return false;
937
938   // No autofill data to return if the profiles are empty.
939   if (personal_data_->GetProfiles().empty() &&
940       personal_data_->GetCreditCards().empty()) {
941     return false;
942   }
943
944   return true;
945 }
946
947 bool AutofillManager::GetProfileOrCreditCard(
948     int unique_id,
949     const AutofillDataModel** data_model,
950     size_t* variant,
951     bool* is_credit_card) const {
952   // Unpack the |unique_id| into component parts.
953   GUIDPair credit_card_guid;
954   GUIDPair profile_guid;
955   UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
956   DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
957          !base::IsValidGUID(profile_guid.first));
958   *is_credit_card = false;
959
960   // Find the profile that matches the |profile_guid|, if one is specified.
961   // Otherwise find the credit card that matches the |credit_card_guid|,
962   // if specified.
963   if (base::IsValidGUID(profile_guid.first)) {
964     *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
965     *variant = profile_guid.second;
966   } else if (base::IsValidGUID(credit_card_guid.first)) {
967     *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
968     *variant = credit_card_guid.second;
969     *is_credit_card = true;
970   }
971
972   return !!*data_model;
973 }
974
975 bool AutofillManager::FindCachedForm(const FormData& form,
976                                      FormStructure** form_structure) const {
977   // Find the FormStructure that corresponds to |form|.
978   // Scan backward through the cached |form_structures_|, as updated versions of
979   // forms are added to the back of the list, whereas original versions of these
980   // forms might appear toward the beginning of the list.  The communication
981   // protocol with the crowdsourcing server does not permit us to discard the
982   // original versions of the forms.
983   *form_structure = NULL;
984   for (std::vector<FormStructure*>::const_reverse_iterator iter =
985            form_structures_.rbegin();
986        iter != form_structures_.rend(); ++iter) {
987     if (**iter == form) {
988       *form_structure = *iter;
989
990       // The same form might be cached with multiple field counts: in some
991       // cases, non-autofillable fields are filtered out, whereas in other cases
992       // they are not.  To avoid thrashing the cache, keep scanning until we
993       // find a cached version with the same number of fields, if there is one.
994       if ((*iter)->field_count() == form.fields.size())
995         break;
996     }
997   }
998
999   if (!(*form_structure))
1000     return false;
1001
1002   return true;
1003 }
1004
1005 bool AutofillManager::GetCachedFormAndField(const FormData& form,
1006                                             const FormFieldData& field,
1007                                             FormStructure** form_structure,
1008                                             AutofillField** autofill_field) {
1009   // Find the FormStructure that corresponds to |form|.
1010   // If we do not have this form in our cache but it is parseable, we'll add it
1011   // in the call to |UpdateCachedForm()|.
1012   if (!FindCachedForm(form, form_structure) &&
1013       !FormStructure(form).ShouldBeParsed()) {
1014     return false;
1015   }
1016
1017   // Update the cached form to reflect any dynamic changes to the form data, if
1018   // necessary.
1019   if (!UpdateCachedForm(form, *form_structure, form_structure))
1020     return false;
1021
1022   // No data to return if there are no auto-fillable fields.
1023   if (!(*form_structure)->autofill_count())
1024     return false;
1025
1026   // Find the AutofillField that corresponds to |field|.
1027   *autofill_field = NULL;
1028   for (std::vector<AutofillField*>::const_iterator iter =
1029            (*form_structure)->begin();
1030        iter != (*form_structure)->end(); ++iter) {
1031     if ((**iter) == field) {
1032       *autofill_field = *iter;
1033       break;
1034     }
1035   }
1036
1037   // Even though we always update the cache, the field might not exist if the
1038   // website disables autocomplete while the user is interacting with the form.
1039   // See http://crbug.com/160476
1040   return *autofill_field != NULL;
1041 }
1042
1043 bool AutofillManager::UpdateCachedForm(const FormData& live_form,
1044                                        const FormStructure* cached_form,
1045                                        FormStructure** updated_form) {
1046   bool needs_update =
1047       (!cached_form ||
1048        live_form.fields.size() != cached_form->field_count());
1049   for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
1050     needs_update = *cached_form->field(i) != live_form.fields[i];
1051   }
1052
1053   if (!needs_update)
1054     return true;
1055
1056   if (form_structures_.size() >= kMaxFormCacheSize)
1057     return false;
1058
1059   // Add the new or updated form to our cache.
1060   form_structures_.push_back(new FormStructure(live_form));
1061   *updated_form = *form_structures_.rbegin();
1062   (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
1063
1064   // If we have cached data, propagate it to the updated form.
1065   if (cached_form) {
1066     std::map<base::string16, const AutofillField*> cached_fields;
1067     for (size_t i = 0; i < cached_form->field_count(); ++i) {
1068       const AutofillField* field = cached_form->field(i);
1069       cached_fields[field->unique_name()] = field;
1070     }
1071
1072     for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
1073       AutofillField* field = (*updated_form)->field(i);
1074       std::map<base::string16, const AutofillField*>::iterator cached_field =
1075           cached_fields.find(field->unique_name());
1076       if (cached_field != cached_fields.end()) {
1077         field->set_server_type(cached_field->second->server_type());
1078         field->is_autofilled = cached_field->second->is_autofilled;
1079       }
1080     }
1081
1082     // Note: We _must not_ remove the original version of the cached form from
1083     // the list of |form_structures_|.  Otherwise, we break parsing of the
1084     // crowdsourcing server's response to our query.
1085   }
1086
1087   // Annotate the updated form with its predicted types.
1088   std::vector<FormStructure*> forms(1, *updated_form);
1089   driver_->SendAutofillTypePredictionsToRenderer(forms);
1090
1091   return true;
1092 }
1093
1094 void AutofillManager::GetProfileSuggestions(
1095     const FormStructure& form,
1096     const FormFieldData& field,
1097     const AutofillField& autofill_field,
1098     std::vector<base::string16>* values,
1099     std::vector<base::string16>* labels,
1100     std::vector<base::string16>* icons,
1101     std::vector<int>* unique_ids) const {
1102   std::vector<ServerFieldType> field_types(form.field_count());
1103   for (size_t i = 0; i < form.field_count(); ++i) {
1104     field_types.push_back(form.field(i)->Type().GetStorableType());
1105   }
1106   std::vector<GUIDPair> guid_pairs;
1107
1108   personal_data_->GetProfileSuggestions(
1109       autofill_field.Type(), field.value, field.is_autofilled, field_types,
1110       base::Callback<bool(const AutofillProfile&)>(),
1111       values, labels, icons, &guid_pairs);
1112
1113   // Adjust phone number to display in prefix/suffix case.
1114   if (autofill_field.Type().GetStorableType() == PHONE_HOME_NUMBER) {
1115     for (size_t i = 0; i < values->size(); ++i) {
1116       (*values)[i] = AutofillField::GetPhoneNumberValue(
1117           autofill_field, (*values)[i], field);
1118     }
1119   }
1120
1121   for (size_t i = 0; i < guid_pairs.size(); ++i) {
1122     unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1123                                     guid_pairs[i]));
1124   }
1125 }
1126
1127 void AutofillManager::GetCreditCardSuggestions(
1128     const FormFieldData& field,
1129     const AutofillType& type,
1130     std::vector<base::string16>* values,
1131     std::vector<base::string16>* labels,
1132     std::vector<base::string16>* icons,
1133     std::vector<int>* unique_ids) const {
1134   std::vector<GUIDPair> guid_pairs;
1135   personal_data_->GetCreditCardSuggestions(
1136       type, field.value, values, labels, icons, &guid_pairs);
1137
1138   for (size_t i = 0; i < guid_pairs.size(); ++i) {
1139     unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1140   }
1141 }
1142
1143 void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1144   std::vector<FormStructure*> non_queryable_forms;
1145   for (std::vector<FormData>::const_iterator iter = forms.begin();
1146        iter != forms.end(); ++iter) {
1147     scoped_ptr<FormStructure> form_structure(new FormStructure(*iter));
1148     if (!form_structure->ShouldBeParsed())
1149       continue;
1150
1151     form_structure->DetermineHeuristicTypes(*metric_logger_);
1152
1153     // Set aside forms with method GET or author-specified types, so that they
1154     // are not included in the query to the server.
1155     if (form_structure->ShouldBeCrowdsourced())
1156       form_structures_.push_back(form_structure.release());
1157     else
1158       non_queryable_forms.push_back(form_structure.release());
1159   }
1160
1161   if (!form_structures_.empty() && download_manager_) {
1162     // Query the server if we have at least one of the forms were parsed.
1163     download_manager_->StartQueryRequest(form_structures_.get(),
1164                                         *metric_logger_);
1165   }
1166
1167   for (std::vector<FormStructure*>::const_iterator iter =
1168            non_queryable_forms.begin();
1169        iter != non_queryable_forms.end(); ++iter) {
1170     form_structures_.push_back(*iter);
1171   }
1172
1173   if (!form_structures_.empty())
1174     metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1175
1176   // For the |non_queryable_forms|, we have all the field type info we're ever
1177   // going to get about them.  For the other forms, we'll wait until we get a
1178   // response from the server.
1179   driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1180 }
1181
1182 int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1183   if (!base::IsValidGUID(guid.first))
1184     return 0;
1185
1186   std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1187   if (iter == guid_id_map_.end()) {
1188     int id = guid_id_map_.size() + 1;
1189     guid_id_map_[guid] = id;
1190     id_guid_map_[id] = guid;
1191     return id;
1192   } else {
1193     return iter->second;
1194   }
1195 }
1196
1197 const GUIDPair AutofillManager::IDToGUID(int id) const {
1198   if (id == 0)
1199     return GUIDPair(std::string(), 0);
1200
1201   std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1202   if (iter == id_guid_map_.end()) {
1203     NOTREACHED();
1204     return GUIDPair(std::string(), 0);
1205   }
1206
1207   return iter->second;
1208 }
1209
1210 // When sending IDs (across processes) to the renderer we pack credit card and
1211 // profile IDs into a single integer.  Credit card IDs are sent in the high
1212 // word and profile IDs are sent in the low word.
1213 int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1214                                const GUIDPair& profile_guid) const {
1215   int cc_id = GUIDToID(cc_guid);
1216   int profile_id = GUIDToID(profile_guid);
1217
1218   DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1219   DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1220
1221   return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1222 }
1223
1224 // When receiving IDs (across processes) from the renderer we unpack credit card
1225 // and profile IDs from a single integer.  Credit card IDs are stored in the
1226 // high word and profile IDs are stored in the low word.
1227 void AutofillManager::UnpackGUIDs(int id,
1228                                   GUIDPair* cc_guid,
1229                                   GUIDPair* profile_guid) const {
1230   int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1231       std::numeric_limits<unsigned short>::max();
1232   int profile_id = id & std::numeric_limits<unsigned short>::max();
1233
1234   *cc_guid = IDToGUID(cc_id);
1235   *profile_guid = IDToGUID(profile_id);
1236 }
1237
1238 void AutofillManager::UpdateInitialInteractionTimestamp(
1239     const TimeTicks& interaction_timestamp) {
1240   if (initial_interaction_timestamp_.is_null() ||
1241       interaction_timestamp < initial_interaction_timestamp_) {
1242     initial_interaction_timestamp_ = interaction_timestamp;
1243   }
1244 }
1245
1246 bool AutofillManager::ShouldUploadForm(const FormStructure& form) {
1247   if (!IsAutofillEnabled())
1248     return false;
1249
1250   if (driver_->IsOffTheRecord())
1251     return false;
1252
1253   // Disregard forms that we wouldn't ever autofill in the first place.
1254   if (!form.ShouldBeParsed())
1255     return false;
1256
1257   return true;
1258 }
1259
1260 }  // namespace autofill