autofill_manager.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_data_model.h"
25#include "components/autofill/core/browser/autofill_external_delegate.h"
26#include "components/autofill/core/browser/autofill_field.h"
27#include "components/autofill/core/browser/autofill_manager_delegate.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/user_prefs/pref_registry_syncable.h"
46#include "grit/component_strings.h"
47#include "ui/base/l10n/l10n_util.h"
48#include "ui/gfx/rect.h"
49#include "url/gurl.h"
50
51namespace autofill {
52
53typedef PersonalDataManager::GUIDPair GUIDPair;
54
55using base::TimeTicks;
56
57namespace {
58
59// We only send a fraction of the forms to upload server.
60// The rate for positive/negative matches potentially could be different.
61const double kAutofillPositiveUploadRateDefaultValue = 0.20;
62const double kAutofillNegativeUploadRateDefaultValue = 0.20;
63
64const 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.
68const size_t kMaxFormCacheSize = 100;
69
70// Removes duplicate suggestions whilst preserving their original order.
71void 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.
105bool 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
119bool 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.
130void 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    // If it's a password field, set the type directly.
142    if (field->form_control_type == "password") {
143      matching_types.insert(autofill::PASSWORD);
144    } else {
145      base::string16 value;
146      base::TrimWhitespace(field->value, base::TRIM_ALL, &value);
147      for (std::vector<AutofillProfile>::const_iterator it = profiles.begin();
148           it != profiles.end(); ++it) {
149        it->GetMatchingTypes(value, app_locale, &matching_types);
150      }
151      for (std::vector<CreditCard>::const_iterator it = credit_cards.begin();
152            it != credit_cards.end(); ++it) {
153        it->GetMatchingTypes(value, app_locale, &matching_types);
154      }
155    }
156
157    if (matching_types.empty())
158      matching_types.insert(UNKNOWN_TYPE);
159
160    field->set_possible_types(matching_types);
161  }
162}
163
164}  // namespace
165
166AutofillManager::AutofillManager(
167    AutofillDriver* driver,
168    autofill::AutofillManagerDelegate* delegate,
169    const std::string& app_locale,
170    AutofillDownloadManagerState enable_download_manager)
171    : driver_(driver),
172      manager_delegate_(delegate),
173      app_locale_(app_locale),
174      personal_data_(delegate->GetPersonalDataManager()),
175      autocomplete_history_manager_(
176          new AutocompleteHistoryManager(driver, delegate)),
177      metric_logger_(new AutofillMetrics),
178      has_logged_autofill_enabled_(false),
179      has_logged_address_suggestions_count_(false),
180      did_show_suggestions_(false),
181      user_did_type_(false),
182      user_did_autofill_(false),
183      user_did_edit_autofilled_field_(false),
184      external_delegate_(NULL),
185      test_delegate_(NULL),
186      weak_ptr_factory_(this) {
187  if (enable_download_manager == ENABLE_AUTOFILL_DOWNLOAD_MANAGER) {
188    download_manager_.reset(
189        new AutofillDownloadManager(driver,
190                                    manager_delegate_->GetPrefs(),
191                                    this));
192  }
193}
194
195AutofillManager::~AutofillManager() {}
196
197// static
198void AutofillManager::RegisterProfilePrefs(
199    user_prefs::PrefRegistrySyncable* registry) {
200  registry->RegisterBooleanPref(
201      prefs::kAutofillEnabled,
202      true,
203      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
204#if defined(OS_MACOSX) || defined(OS_ANDROID)
205  registry->RegisterBooleanPref(
206      prefs::kAutofillAuxiliaryProfilesEnabled,
207      true,
208      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
209#else
210  registry->RegisterBooleanPref(
211      prefs::kAutofillAuxiliaryProfilesEnabled,
212      false,
213      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
214#endif
215  registry->RegisterDoublePref(
216      prefs::kAutofillPositiveUploadRate,
217      kAutofillPositiveUploadRateDefaultValue,
218      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
219  registry->RegisterDoublePref(
220      prefs::kAutofillNegativeUploadRate,
221      kAutofillNegativeUploadRateDefaultValue,
222      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
223}
224
225void AutofillManager::SetExternalDelegate(AutofillExternalDelegate* delegate) {
226  // TODO(jrg): consider passing delegate into the ctor.  That won't
227  // work if the delegate has a pointer to the AutofillManager, but
228  // future directions may not need such a pointer.
229  external_delegate_ = delegate;
230  autocomplete_history_manager_->SetExternalDelegate(delegate);
231}
232
233void AutofillManager::ShowAutofillSettings() {
234  manager_delegate_->ShowAutofillSettings();
235}
236
237bool AutofillManager::OnFormSubmitted(const FormData& form,
238                                      const TimeTicks& timestamp) {
239  if (!IsValidFormData(form))
240    return false;
241
242  // Let Autocomplete know as well.
243  autocomplete_history_manager_->OnFormSubmitted(form);
244
245  // Grab a copy of the form data.
246  scoped_ptr<FormStructure> submitted_form(new FormStructure(form));
247
248  if (!ShouldUploadForm(*submitted_form))
249    return false;
250
251  // Don't save data that was submitted through JavaScript.
252  if (!form.user_submitted)
253    return false;
254
255  // Ignore forms not present in our cache.  These are typically forms with
256  // wonky JavaScript that also makes them not auto-fillable.
257  FormStructure* cached_submitted_form;
258  if (!FindCachedForm(form, &cached_submitted_form))
259    return false;
260
261  submitted_form->UpdateFromCache(*cached_submitted_form);
262  if (submitted_form->IsAutofillable(true))
263    ImportFormData(*submitted_form);
264
265  // Only upload server statistics and UMA metrics if at least some local data
266  // is available to use as a baseline.
267  const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
268  const std::vector<CreditCard*>& credit_cards =
269      personal_data_->GetCreditCards();
270  if (!profiles.empty() || !credit_cards.empty()) {
271    // Copy the profile and credit card data, so that it can be accessed on a
272    // separate thread.
273    std::vector<AutofillProfile> copied_profiles;
274    copied_profiles.reserve(profiles.size());
275    for (std::vector<AutofillProfile*>::const_iterator it = profiles.begin();
276         it != profiles.end(); ++it) {
277      copied_profiles.push_back(**it);
278    }
279
280    std::vector<CreditCard> copied_credit_cards;
281    copied_credit_cards.reserve(credit_cards.size());
282    for (std::vector<CreditCard*>::const_iterator it = credit_cards.begin();
283         it != credit_cards.end(); ++it) {
284      copied_credit_cards.push_back(**it);
285    }
286
287    // Note that ownership of |submitted_form| is passed to the second task,
288    // using |base::Owned|.
289    FormStructure* raw_submitted_form = submitted_form.get();
290    driver_->GetBlockingPool()->PostTaskAndReply(
291        FROM_HERE,
292        base::Bind(&DeterminePossibleFieldTypesForUpload,
293                   copied_profiles,
294                   copied_credit_cards,
295                   app_locale_,
296                   raw_submitted_form),
297        base::Bind(&AutofillManager::UploadFormDataAsyncCallback,
298                   weak_ptr_factory_.GetWeakPtr(),
299                   base::Owned(submitted_form.release()),
300                   forms_loaded_timestamp_,
301                   initial_interaction_timestamp_,
302                   timestamp));
303  }
304
305  return true;
306}
307
308void AutofillManager::OnFormsSeen(const std::vector<FormData>& forms,
309                                  const TimeTicks& timestamp,
310                                  autofill::FormsSeenState state) {
311  if (!IsValidFormDataVector(forms))
312    return;
313
314  bool is_post_document_load = state == autofill::DYNAMIC_FORMS_SEEN;
315  // If new forms were added dynamically, treat as a new page.
316  if (is_post_document_load)
317    Reset();
318
319  if (!driver_->RendererIsAvailable())
320    return;
321
322  bool enabled = IsAutofillEnabled();
323  if (!has_logged_autofill_enabled_) {
324    metric_logger_->LogIsAutofillEnabledAtPageLoad(enabled);
325    has_logged_autofill_enabled_ = true;
326  }
327
328  if (!enabled)
329    return;
330
331  forms_loaded_timestamp_ = timestamp;
332  ParseForms(forms);
333}
334
335void AutofillManager::OnTextFieldDidChange(const FormData& form,
336                                           const FormFieldData& field,
337                                           const TimeTicks& timestamp) {
338  if (!IsValidFormData(form) || !IsValidFormFieldData(field))
339    return;
340
341  FormStructure* form_structure = NULL;
342  AutofillField* autofill_field = NULL;
343  if (!GetCachedFormAndField(form, field, &form_structure, &autofill_field))
344    return;
345
346  if (!user_did_type_) {
347    user_did_type_ = true;
348    metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_TYPE);
349  }
350
351  if (autofill_field->is_autofilled) {
352    autofill_field->is_autofilled = false;
353    metric_logger_->LogUserHappinessMetric(
354        AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD);
355
356    if (!user_did_edit_autofilled_field_) {
357      user_did_edit_autofilled_field_ = true;
358      metric_logger_->LogUserHappinessMetric(
359          AutofillMetrics::USER_DID_EDIT_AUTOFILLED_FIELD_ONCE);
360    }
361  }
362
363  UpdateInitialInteractionTimestamp(timestamp);
364}
365
366void AutofillManager::OnQueryFormFieldAutofill(int query_id,
367                                               const FormData& form,
368                                               const FormFieldData& field,
369                                               const gfx::RectF& bounding_box,
370                                               bool display_warning) {
371  if (!IsValidFormData(form) || !IsValidFormFieldData(field))
372    return;
373
374  std::vector<base::string16> values;
375  std::vector<base::string16> labels;
376  std::vector<base::string16> icons;
377  std::vector<int> unique_ids;
378
379  external_delegate_->OnQuery(query_id,
380                              form,
381                              field,
382                              bounding_box,
383                              display_warning);
384  FormStructure* form_structure = NULL;
385  AutofillField* autofill_field = NULL;
386  if (RefreshDataModels() &&
387      driver_->RendererIsAvailable() &&
388      GetCachedFormAndField(form, field, &form_structure, &autofill_field) &&
389      // Don't send suggestions for forms that aren't auto-fillable.
390      form_structure->IsAutofillable(false)) {
391    AutofillType type = autofill_field->Type();
392    bool is_filling_credit_card = (type.group() == CREDIT_CARD);
393    if (is_filling_credit_card) {
394      GetCreditCardSuggestions(
395          field, type, &values, &labels, &icons, &unique_ids);
396    } else {
397      GetProfileSuggestions(
398          form_structure, field, type, &values, &labels, &icons, &unique_ids);
399    }
400
401    DCHECK_EQ(values.size(), labels.size());
402    DCHECK_EQ(values.size(), icons.size());
403    DCHECK_EQ(values.size(), unique_ids.size());
404
405    if (!values.empty()) {
406      // Don't provide Autofill suggestions when Autofill is disabled, and don't
407      // provide credit card suggestions for non-HTTPS pages. However, provide a
408      // warning to the user in these cases.
409      int warning = 0;
410      if (!form_structure->IsAutofillable(true))
411        warning = IDS_AUTOFILL_WARNING_FORM_DISABLED;
412      else if (is_filling_credit_card && !FormIsHTTPS(*form_structure))
413        warning = IDS_AUTOFILL_WARNING_INSECURE_CONNECTION;
414      if (warning) {
415        values.assign(1, l10n_util::GetStringUTF16(warning));
416        labels.assign(1, base::string16());
417        icons.assign(1, base::string16());
418        unique_ids.assign(1, POPUP_ITEM_ID_WARNING_MESSAGE);
419      } else {
420        bool section_is_autofilled =
421            SectionIsAutofilled(*form_structure, form,
422                                autofill_field->section());
423        if (section_is_autofilled) {
424          // If the relevant section is auto-filled and the renderer is querying
425          // for suggestions, then the user is editing the value of a field.
426          // In this case, mimic autocomplete: don't display labels or icons,
427          // as that information is redundant.
428          labels.assign(labels.size(), base::string16());
429          icons.assign(icons.size(), base::string16());
430        }
431
432        // When filling credit card suggestions, the values and labels are
433        // typically obfuscated, which makes detecting duplicates hard.  Since
434        // duplicates only tend to be a problem when filling address forms
435        // anyway, only don't de-dup credit card suggestions.
436        if (!is_filling_credit_card)
437          RemoveDuplicateSuggestions(&values, &labels, &icons, &unique_ids);
438
439        // The first time we show suggestions on this page, log the number of
440        // suggestions shown.
441        if (!has_logged_address_suggestions_count_ && !section_is_autofilled) {
442          metric_logger_->LogAddressSuggestionsCount(values.size());
443          has_logged_address_suggestions_count_ = true;
444        }
445      }
446    }
447  }
448
449  // Add the results from AutoComplete.  They come back asynchronously, so we
450  // hand off what we generated and they will send the results back to the
451  // renderer.
452  autocomplete_history_manager_->OnGetAutocompleteSuggestions(
453      query_id, field.name, field.value, values, labels, icons, unique_ids);
454}
455
456void AutofillManager::FillOrPreviewForm(
457    AutofillDriver::RendererFormDataAction action,
458    int query_id,
459    const FormData& form,
460    const FormFieldData& field,
461    int unique_id) {
462  if (!IsValidFormData(form) || !IsValidFormFieldData(field))
463    return;
464
465  const AutofillDataModel* data_model = NULL;
466  size_t variant = 0;
467  FormStructure* form_structure = NULL;
468  AutofillField* autofill_field = NULL;
469  bool is_credit_card = false;
470  // NOTE: RefreshDataModels may invalidate |data_model| because it causes the
471  // PersonalDataManager to reload Mac address book entries. Thus it must come
472  // before GetProfileOrCreditCard.
473  if (!RefreshDataModels() ||
474      !driver_->RendererIsAvailable() ||
475      !GetProfileOrCreditCard(
476          unique_id, &data_model, &variant, &is_credit_card) ||
477      !GetCachedFormAndField(form, field, &form_structure, &autofill_field))
478    return;
479
480  DCHECK(form_structure);
481  DCHECK(autofill_field);
482
483  FormData result = form;
484
485  base::string16 profile_full_name;
486  if (!is_credit_card) {
487    profile_full_name = data_model->GetInfo(
488        AutofillType(NAME_FULL), app_locale_);
489  }
490
491  // If the relevant section is auto-filled, we should fill |field| but not the
492  // rest of the form.
493  if (SectionIsAutofilled(*form_structure, form, autofill_field->section())) {
494    for (std::vector<FormFieldData>::iterator iter = result.fields.begin();
495         iter != result.fields.end(); ++iter) {
496      if ((*iter) == field) {
497        base::string16 value = data_model->GetInfoForVariant(
498            autofill_field->Type(), variant, app_locale_);
499        AutofillField::FillFormField(*autofill_field, value, app_locale_,
500                                     &(*iter));
501        // Mark the cached field as autofilled, so that we can detect when a
502        // user edits an autofilled field (for metrics).
503        autofill_field->is_autofilled = true;
504
505        if (!is_credit_card && !value.empty())
506          manager_delegate_->DidFillOrPreviewField(value, profile_full_name);
507        break;
508      }
509    }
510
511    driver_->SendFormDataToRenderer(query_id, action, result);
512    return;
513  }
514
515  // Cache the field type for the field from which the user initiated autofill.
516  FieldTypeGroup initiating_group_type = autofill_field->Type().group();
517  DCHECK_EQ(form_structure->field_count(), form.fields.size());
518  for (size_t i = 0; i < form_structure->field_count(); ++i) {
519    if (form_structure->field(i)->section() != autofill_field->section())
520      continue;
521
522    DCHECK_EQ(*form_structure->field(i), result.fields[i]);
523
524    const AutofillField* cached_field = form_structure->field(i);
525    FieldTypeGroup field_group_type = cached_field->Type().group();
526    if (field_group_type != NO_GROUP) {
527      // If the field being filled is either
528      //   (a) the field that the user initiated the fill from, or
529      //   (b) part of the same logical unit, e.g. name or phone number,
530      // then take the multi-profile "variant" into account.
531      // Otherwise fill with the default (zeroth) variant.
532      size_t use_variant = 0;
533      if (result.fields[i] == field ||
534          field_group_type == initiating_group_type) {
535        use_variant = variant;
536      }
537      base::string16 value = data_model->GetInfoForVariant(
538          cached_field->Type(), use_variant, app_locale_);
539
540      // Must match ForEachMatchingFormField() in form_autofill_util.cc.
541      // Only notify autofilling of empty fields and the field that initiated
542      // the filling (note that "select-one" controls may not be empty but will
543      // still be autofilled).
544      bool should_notify =
545          !is_credit_card &&
546          !value.empty() &&
547          (result.fields[i] == field ||
548           result.fields[i].form_control_type == "select-one" ||
549           result.fields[i].value.empty());
550      AutofillField::FillFormField(*cached_field, value, app_locale_,
551                                   &result.fields[i]);
552      // Mark the cached field as autofilled, so that we can detect when a user
553      // edits an autofilled field (for metrics).
554      form_structure->field(i)->is_autofilled = true;
555
556      if (should_notify)
557        manager_delegate_->DidFillOrPreviewField(value, profile_full_name);
558    }
559  }
560
561  autofilled_form_signatures_.push_front(form_structure->FormSignature());
562  // Only remember the last few forms that we've seen, both to avoid false
563  // positives and to avoid wasting memory.
564  if (autofilled_form_signatures_.size() > kMaxRecentFormSignaturesToRemember)
565    autofilled_form_signatures_.pop_back();
566
567  driver_->SendFormDataToRenderer(query_id, action, result);
568}
569
570void AutofillManager::OnDidPreviewAutofillFormData() {
571  if (test_delegate_)
572    test_delegate_->DidPreviewFormData();
573}
574
575void AutofillManager::OnDidFillAutofillFormData(const TimeTicks& timestamp) {
576  if (test_delegate_)
577    test_delegate_->DidFillFormData();
578
579  metric_logger_->LogUserHappinessMetric(AutofillMetrics::USER_DID_AUTOFILL);
580  if (!user_did_autofill_) {
581    user_did_autofill_ = true;
582    metric_logger_->LogUserHappinessMetric(
583        AutofillMetrics::USER_DID_AUTOFILL_ONCE);
584  }
585
586  UpdateInitialInteractionTimestamp(timestamp);
587}
588
589void AutofillManager::DidShowSuggestions(bool is_new_popup) {
590  if (test_delegate_)
591    test_delegate_->DidShowSuggestions();
592
593  if (is_new_popup) {
594    metric_logger_->LogUserHappinessMetric(AutofillMetrics::SUGGESTIONS_SHOWN);
595
596    if (!did_show_suggestions_) {
597      did_show_suggestions_ = true;
598      metric_logger_->LogUserHappinessMetric(
599          AutofillMetrics::SUGGESTIONS_SHOWN_ONCE);
600    }
601  }
602}
603
604void AutofillManager::OnHidePopup() {
605  if (!IsAutofillEnabled())
606    return;
607
608  manager_delegate_->HideAutofillPopup();
609}
610
611void AutofillManager::RemoveAutofillProfileOrCreditCard(int unique_id) {
612  const AutofillDataModel* data_model = NULL;
613  size_t variant = 0;
614  bool unused_is_credit_card = false;
615  if (!GetProfileOrCreditCard(
616          unique_id, &data_model, &variant, &unused_is_credit_card)) {
617    NOTREACHED();
618    return;
619  }
620
621  // TODO(csharp): If we are dealing with a variant only the variant should
622  // be deleted, instead of doing nothing.
623  // http://crbug.com/124211
624  if (variant != 0)
625    return;
626
627  personal_data_->RemoveByGUID(data_model->guid());
628}
629
630void AutofillManager::RemoveAutocompleteEntry(const base::string16& name,
631                                              const base::string16& value) {
632  autocomplete_history_manager_->OnRemoveAutocompleteEntry(name, value);
633}
634
635const std::vector<FormStructure*>& AutofillManager::GetFormStructures() {
636  return form_structures_.get();
637}
638
639void AutofillManager::SetTestDelegate(
640    autofill::AutofillManagerTestDelegate* delegate) {
641  test_delegate_ = delegate;
642}
643
644void AutofillManager::OnAddPasswordFormMapping(
645      const FormFieldData& username_field,
646      const PasswordFormFillData& fill_data) {
647  if (!IsValidFormFieldData(username_field) ||
648      !IsValidPasswordFormFillData(fill_data))
649    return;
650
651  external_delegate_->AddPasswordFormMapping(username_field, fill_data);
652}
653
654void AutofillManager::OnShowPasswordSuggestions(
655    const FormFieldData& field,
656    const gfx::RectF& bounds,
657    const std::vector<base::string16>& suggestions,
658    const std::vector<base::string16>& realms) {
659  if (!IsValidString16Vector(suggestions) ||
660      !IsValidString16Vector(realms) ||
661      suggestions.size() != realms.size())
662    return;
663
664  external_delegate_->OnShowPasswordSuggestions(suggestions,
665                                                realms,
666                                                field,
667                                                bounds);
668}
669
670void AutofillManager::OnSetDataList(const std::vector<base::string16>& values,
671                                    const std::vector<base::string16>& labels) {
672  if (!IsValidString16Vector(values) ||
673      !IsValidString16Vector(labels) ||
674      values.size() != labels.size())
675    return;
676
677  external_delegate_->SetCurrentDataListValues(values, labels);
678}
679
680void AutofillManager::OnLoadedServerPredictions(
681    const std::string& response_xml) {
682  // Parse and store the server predictions.
683  FormStructure::ParseQueryResponse(response_xml,
684                                    form_structures_.get(),
685                                    *metric_logger_);
686
687  // Forward form structures to the password generation manager to detect
688  // account creation forms.
689  manager_delegate_->DetectAccountCreationForms(form_structures_.get());
690
691  // If the corresponding flag is set, annotate forms with the predicted types.
692  driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
693}
694
695void AutofillManager::OnDidEndTextFieldEditing() {
696  external_delegate_->DidEndTextFieldEditing();
697}
698
699bool AutofillManager::IsAutofillEnabled() const {
700  return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
701}
702
703void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
704  scoped_ptr<CreditCard> imported_credit_card;
705  if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
706    return;
707
708  // If credit card information was submitted, we need to confirm whether to
709  // save it.
710  if (imported_credit_card) {
711    manager_delegate_->ConfirmSaveCreditCard(
712        *metric_logger_,
713        base::Bind(
714            base::IgnoreResult(&PersonalDataManager::SaveImportedCreditCard),
715            base::Unretained(personal_data_), *imported_credit_card));
716  }
717}
718
719// Note that |submitted_form| is passed as a pointer rather than as a reference
720// so that we can get memory management right across threads.  Note also that we
721// explicitly pass in all the time stamps of interest, as the cached ones might
722// get reset before this method executes.
723void AutofillManager::UploadFormDataAsyncCallback(
724    const FormStructure* submitted_form,
725    const TimeTicks& load_time,
726    const TimeTicks& interaction_time,
727    const TimeTicks& submission_time) {
728  submitted_form->LogQualityMetrics(*metric_logger_,
729                                    load_time,
730                                    interaction_time,
731                                    submission_time);
732
733  if (submitted_form->ShouldBeCrowdsourced())
734    UploadFormData(*submitted_form);
735}
736
737void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
738  if (!download_manager_)
739    return;
740
741  // Check if the form is among the forms that were recently auto-filled.
742  bool was_autofilled = false;
743  std::string form_signature = submitted_form.FormSignature();
744  for (std::list<std::string>::const_iterator it =
745           autofilled_form_signatures_.begin();
746       it != autofilled_form_signatures_.end() && !was_autofilled;
747       ++it) {
748    if (*it == form_signature)
749      was_autofilled = true;
750  }
751
752  ServerFieldTypeSet non_empty_types;
753  personal_data_->GetNonEmptyTypes(&non_empty_types);
754  // Always add PASSWORD to |non_empty_types| so that if |submitted_form|
755  // contains a password field it will be uploaded to the server. If
756  // |submitted_form| doesn't contain a password field, there is no side
757  // effect from adding PASSWORD to |non_empty_types|.
758  non_empty_types.insert(autofill::PASSWORD);
759
760  download_manager_->StartUploadRequest(submitted_form, was_autofilled,
761                                        non_empty_types);
762}
763
764bool AutofillManager::UploadPasswordGenerationForm(const FormData& form) {
765  FormStructure form_structure(form);
766
767  if (!ShouldUploadForm(form_structure))
768    return false;
769
770  if (!form_structure.ShouldBeCrowdsourced())
771    return false;
772
773  // TODO(gcasto): Check that PasswordGeneration is enabled?
774
775  // Find the first password field to label. We don't try to label anything
776  // else.
777  bool found_password_field = false;
778  for (size_t i = 0; i < form_structure.field_count(); ++i) {
779    AutofillField* field = form_structure.field(i);
780
781    ServerFieldTypeSet types;
782    if (!found_password_field && field->form_control_type == "password") {
783      types.insert(ACCOUNT_CREATION_PASSWORD);
784      found_password_field = true;
785    } else {
786      types.insert(UNKNOWN_TYPE);
787    }
788    field->set_possible_types(types);
789  }
790  DCHECK(found_password_field);
791
792  // Only one field type should be present.
793  ServerFieldTypeSet available_field_types;
794  available_field_types.insert(ACCOUNT_CREATION_PASSWORD);
795
796  // Force uploading as these events are relatively rare and we want to make
797  // sure to receive them. It also makes testing easier if these requests
798  // always pass.
799  form_structure.set_upload_required(UPLOAD_REQUIRED);
800
801  if (!download_manager_)
802    return false;
803
804  return download_manager_->StartUploadRequest(form_structure,
805                                               false /* was_autofilled */,
806                                               available_field_types);
807}
808
809void AutofillManager::Reset() {
810  form_structures_.clear();
811  has_logged_autofill_enabled_ = false;
812  has_logged_address_suggestions_count_ = false;
813  did_show_suggestions_ = false;
814  user_did_type_ = false;
815  user_did_autofill_ = false;
816  user_did_edit_autofilled_field_ = false;
817  forms_loaded_timestamp_ = TimeTicks();
818  initial_interaction_timestamp_ = TimeTicks();
819  external_delegate_->Reset();
820}
821
822AutofillManager::AutofillManager(AutofillDriver* driver,
823                                 autofill::AutofillManagerDelegate* delegate,
824                                 PersonalDataManager* personal_data)
825    : driver_(driver),
826      manager_delegate_(delegate),
827      app_locale_("en-US"),
828      personal_data_(personal_data),
829      autocomplete_history_manager_(
830          new AutocompleteHistoryManager(driver, delegate)),
831      metric_logger_(new AutofillMetrics),
832      has_logged_autofill_enabled_(false),
833      has_logged_address_suggestions_count_(false),
834      did_show_suggestions_(false),
835      user_did_type_(false),
836      user_did_autofill_(false),
837      user_did_edit_autofilled_field_(false),
838      external_delegate_(NULL),
839      test_delegate_(NULL),
840      weak_ptr_factory_(this) {
841  DCHECK(driver_);
842  DCHECK(manager_delegate_);
843}
844
845void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
846  metric_logger_.reset(metric_logger);
847}
848
849bool AutofillManager::RefreshDataModels() const {
850  if (!IsAutofillEnabled())
851    return false;
852
853  // No autofill data to return if the profiles are empty.
854  if (personal_data_->GetProfiles().empty() &&
855      personal_data_->GetCreditCards().empty()) {
856    return false;
857  }
858
859  return true;
860}
861
862bool AutofillManager::GetProfileOrCreditCard(
863    int unique_id,
864    const AutofillDataModel** data_model,
865    size_t* variant,
866    bool* is_credit_card) const {
867  // Unpack the |unique_id| into component parts.
868  GUIDPair credit_card_guid;
869  GUIDPair profile_guid;
870  UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
871  DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
872         !base::IsValidGUID(profile_guid.first));
873  *is_credit_card = false;
874
875  // Find the profile that matches the |profile_guid|, if one is specified.
876  // Otherwise find the credit card that matches the |credit_card_guid|,
877  // if specified.
878  if (base::IsValidGUID(profile_guid.first)) {
879    *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
880    *variant = profile_guid.second;
881  } else if (base::IsValidGUID(credit_card_guid.first)) {
882    *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
883    *variant = credit_card_guid.second;
884    *is_credit_card = true;
885  }
886
887  return !!*data_model;
888}
889
890bool AutofillManager::FindCachedForm(const FormData& form,
891                                     FormStructure** form_structure) const {
892  // Find the FormStructure that corresponds to |form|.
893  // Scan backward through the cached |form_structures_|, as updated versions of
894  // forms are added to the back of the list, whereas original versions of these
895  // forms might appear toward the beginning of the list.  The communication
896  // protocol with the crowdsourcing server does not permit us to discard the
897  // original versions of the forms.
898  *form_structure = NULL;
899  for (std::vector<FormStructure*>::const_reverse_iterator iter =
900           form_structures_.rbegin();
901       iter != form_structures_.rend(); ++iter) {
902    if (**iter == form) {
903      *form_structure = *iter;
904
905      // The same form might be cached with multiple field counts: in some
906      // cases, non-autofillable fields are filtered out, whereas in other cases
907      // they are not.  To avoid thrashing the cache, keep scanning until we
908      // find a cached version with the same number of fields, if there is one.
909      if ((*iter)->field_count() == form.fields.size())
910        break;
911    }
912  }
913
914  if (!(*form_structure))
915    return false;
916
917  return true;
918}
919
920bool AutofillManager::GetCachedFormAndField(const FormData& form,
921                                            const FormFieldData& field,
922                                            FormStructure** form_structure,
923                                            AutofillField** autofill_field) {
924  // Find the FormStructure that corresponds to |form|.
925  // If we do not have this form in our cache but it is parseable, we'll add it
926  // in the call to |UpdateCachedForm()|.
927  if (!FindCachedForm(form, form_structure) &&
928      !FormStructure(form).ShouldBeParsed(false)) {
929    return false;
930  }
931
932  // Update the cached form to reflect any dynamic changes to the form data, if
933  // necessary.
934  if (!UpdateCachedForm(form, *form_structure, form_structure))
935    return false;
936
937  // No data to return if there are no auto-fillable fields.
938  if (!(*form_structure)->autofill_count())
939    return false;
940
941  // Find the AutofillField that corresponds to |field|.
942  *autofill_field = NULL;
943  for (std::vector<AutofillField*>::const_iterator iter =
944           (*form_structure)->begin();
945       iter != (*form_structure)->end(); ++iter) {
946    if ((**iter) == field) {
947      *autofill_field = *iter;
948      break;
949    }
950  }
951
952  // Even though we always update the cache, the field might not exist if the
953  // website disables autocomplete while the user is interacting with the form.
954  // See http://crbug.com/160476
955  return *autofill_field != NULL;
956}
957
958bool AutofillManager::UpdateCachedForm(const FormData& live_form,
959                                       const FormStructure* cached_form,
960                                       FormStructure** updated_form) {
961  bool needs_update =
962      (!cached_form ||
963       live_form.fields.size() != cached_form->field_count());
964  for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
965    needs_update = *cached_form->field(i) != live_form.fields[i];
966  }
967
968  if (!needs_update)
969    return true;
970
971  if (form_structures_.size() >= kMaxFormCacheSize)
972    return false;
973
974  // Add the new or updated form to our cache.
975  form_structures_.push_back(new FormStructure(live_form));
976  *updated_form = *form_structures_.rbegin();
977  (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
978
979  // If we have cached data, propagate it to the updated form.
980  if (cached_form) {
981    std::map<base::string16, const AutofillField*> cached_fields;
982    for (size_t i = 0; i < cached_form->field_count(); ++i) {
983      const AutofillField* field = cached_form->field(i);
984      cached_fields[field->unique_name()] = field;
985    }
986
987    for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
988      AutofillField* field = (*updated_form)->field(i);
989      std::map<base::string16, const AutofillField*>::iterator cached_field =
990          cached_fields.find(field->unique_name());
991      if (cached_field != cached_fields.end()) {
992        field->set_server_type(cached_field->second->server_type());
993        field->is_autofilled = cached_field->second->is_autofilled;
994      }
995    }
996
997    // Note: We _must not_ remove the original version of the cached form from
998    // the list of |form_structures_|.  Otherwise, we break parsing of the
999    // crowdsourcing server's response to our query.
1000  }
1001
1002  // Annotate the updated form with its predicted types.
1003  std::vector<FormStructure*> forms(1, *updated_form);
1004  driver_->SendAutofillTypePredictionsToRenderer(forms);
1005
1006  return true;
1007}
1008
1009void AutofillManager::GetProfileSuggestions(
1010    FormStructure* form,
1011    const FormFieldData& field,
1012    const AutofillType& type,
1013    std::vector<base::string16>* values,
1014    std::vector<base::string16>* labels,
1015    std::vector<base::string16>* icons,
1016    std::vector<int>* unique_ids) const {
1017  std::vector<ServerFieldType> field_types(form->field_count());
1018  for (size_t i = 0; i < form->field_count(); ++i) {
1019    field_types.push_back(form->field(i)->Type().GetStorableType());
1020  }
1021  std::vector<GUIDPair> guid_pairs;
1022
1023  personal_data_->GetProfileSuggestions(
1024      type, field.value, field.is_autofilled, field_types,
1025      base::Callback<bool(const AutofillProfile&)>(),
1026      values, labels, icons, &guid_pairs);
1027
1028  for (size_t i = 0; i < guid_pairs.size(); ++i) {
1029    unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1030                                    guid_pairs[i]));
1031  }
1032}
1033
1034void AutofillManager::GetCreditCardSuggestions(
1035    const FormFieldData& field,
1036    const AutofillType& type,
1037    std::vector<base::string16>* values,
1038    std::vector<base::string16>* labels,
1039    std::vector<base::string16>* icons,
1040    std::vector<int>* unique_ids) const {
1041  std::vector<GUIDPair> guid_pairs;
1042  personal_data_->GetCreditCardSuggestions(
1043      type, field.value, values, labels, icons, &guid_pairs);
1044
1045  for (size_t i = 0; i < guid_pairs.size(); ++i) {
1046    unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1047  }
1048}
1049
1050void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1051  std::vector<FormStructure*> non_queryable_forms;
1052  for (std::vector<FormData>::const_iterator iter = forms.begin();
1053       iter != forms.end(); ++iter) {
1054    scoped_ptr<FormStructure> form_structure(new FormStructure(*iter));
1055    if (!form_structure->ShouldBeParsed(false))
1056      continue;
1057
1058    form_structure->DetermineHeuristicTypes(*metric_logger_);
1059
1060    // Set aside forms with method GET or author-specified types, so that they
1061    // are not included in the query to the server.
1062    if (form_structure->ShouldBeCrowdsourced())
1063      form_structures_.push_back(form_structure.release());
1064    else
1065      non_queryable_forms.push_back(form_structure.release());
1066  }
1067
1068  if (!form_structures_.empty() && download_manager_) {
1069    // Query the server if we have at least one of the forms were parsed.
1070    download_manager_->StartQueryRequest(form_structures_.get(),
1071                                        *metric_logger_);
1072  }
1073
1074  for (std::vector<FormStructure*>::const_iterator iter =
1075           non_queryable_forms.begin();
1076       iter != non_queryable_forms.end(); ++iter) {
1077    form_structures_.push_back(*iter);
1078  }
1079
1080  if (!form_structures_.empty())
1081    metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1082
1083  // For the |non_queryable_forms|, we have all the field type info we're ever
1084  // going to get about them.  For the other forms, we'll wait until we get a
1085  // response from the server.
1086  driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1087}
1088
1089int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1090  if (!base::IsValidGUID(guid.first))
1091    return 0;
1092
1093  std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1094  if (iter == guid_id_map_.end()) {
1095    int id = guid_id_map_.size() + 1;
1096    guid_id_map_[guid] = id;
1097    id_guid_map_[id] = guid;
1098    return id;
1099  } else {
1100    return iter->second;
1101  }
1102}
1103
1104const GUIDPair AutofillManager::IDToGUID(int id) const {
1105  if (id == 0)
1106    return GUIDPair(std::string(), 0);
1107
1108  std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1109  if (iter == id_guid_map_.end()) {
1110    NOTREACHED();
1111    return GUIDPair(std::string(), 0);
1112  }
1113
1114  return iter->second;
1115}
1116
1117// When sending IDs (across processes) to the renderer we pack credit card and
1118// profile IDs into a single integer.  Credit card IDs are sent in the high
1119// word and profile IDs are sent in the low word.
1120int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1121                               const GUIDPair& profile_guid) const {
1122  int cc_id = GUIDToID(cc_guid);
1123  int profile_id = GUIDToID(profile_guid);
1124
1125  DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1126  DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1127
1128  return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1129}
1130
1131// When receiving IDs (across processes) from the renderer we unpack credit card
1132// and profile IDs from a single integer.  Credit card IDs are stored in the
1133// high word and profile IDs are stored in the low word.
1134void AutofillManager::UnpackGUIDs(int id,
1135                                  GUIDPair* cc_guid,
1136                                  GUIDPair* profile_guid) const {
1137  int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1138      std::numeric_limits<unsigned short>::max();
1139  int profile_id = id & std::numeric_limits<unsigned short>::max();
1140
1141  *cc_guid = IDToGUID(cc_id);
1142  *profile_guid = IDToGUID(profile_id);
1143}
1144
1145void AutofillManager::UpdateInitialInteractionTimestamp(
1146    const TimeTicks& interaction_timestamp) {
1147  if (initial_interaction_timestamp_.is_null() ||
1148      interaction_timestamp < initial_interaction_timestamp_) {
1149    initial_interaction_timestamp_ = interaction_timestamp;
1150  }
1151}
1152
1153bool AutofillManager::ShouldUploadForm(const FormStructure& form) {
1154  if (!IsAutofillEnabled())
1155    return false;
1156
1157  if (driver_->IsOffTheRecord())
1158    return false;
1159
1160  // Disregard forms that we wouldn't ever autofill in the first place.
1161  if (!form.ShouldBeParsed(true))
1162    return false;
1163
1164  return true;
1165}
1166
1167}  // namespace autofill
1168