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