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