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