autofill_manager.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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() == 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 != 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  if (values.size() != labels.size())
690    return;
691
692  external_delegate_->SetCurrentDataListValues(values, labels);
693}
694
695void AutofillManager::OnRequestAutocomplete(
696    const FormData& form,
697    const GURL& frame_url) {
698  if (!IsAutofillEnabled()) {
699    ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorDisabled,
700                             FormData());
701    return;
702  }
703
704  base::Callback<void(const FormStructure*, const std::string&)> callback =
705      base::Bind(&AutofillManager::ReturnAutocompleteData,
706                 weak_ptr_factory_.GetWeakPtr());
707  ShowRequestAutocompleteDialog(
708      form, frame_url, autofill::DIALOG_TYPE_REQUEST_AUTOCOMPLETE, callback);
709}
710
711void AutofillManager::ReturnAutocompleteResult(
712    WebFormElement::AutocompleteResult result, const FormData& form_data) {
713  // driver_->GetWebContents() will be NULL when the interactive autocomplete
714  // is closed due to a tab or browser window closing.
715  if (!driver_->GetWebContents())
716    return;
717
718  RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost();
719  if (!host)
720    return;
721
722  host->Send(new AutofillMsg_RequestAutocompleteResult(host->GetRoutingID(),
723                                                       result,
724                                                       form_data));
725}
726
727void AutofillManager::ReturnAutocompleteData(
728    const FormStructure* result,
729    const std::string& unused_transaction_id) {
730  if (!result) {
731    ReturnAutocompleteResult(WebFormElement::AutocompleteResultErrorCancel,
732                             FormData());
733  } else {
734    ReturnAutocompleteResult(WebFormElement::AutocompleteResultSuccess,
735                             result->ToFormData());
736  }
737}
738
739void AutofillManager::OnLoadedServerPredictions(
740    const std::string& response_xml) {
741  scoped_ptr<autofill::AutocheckoutPageMetaData> page_meta_data(
742      new autofill::AutocheckoutPageMetaData());
743
744  // Parse and store the server predictions.
745  FormStructure::ParseQueryResponse(response_xml,
746                                    form_structures_.get(),
747                                    page_meta_data.get(),
748                                    *metric_logger_);
749
750  if (page_meta_data->IsInAutofillableFlow()) {
751    RenderViewHost* host = driver_->GetWebContents()->GetRenderViewHost();
752    if (host)
753      host->Send(new AutofillMsg_AutocheckoutSupported(host->GetRoutingID()));
754  }
755
756  // TODO(ahutter): Remove this once Autocheckout is implemented on other
757  // platforms. See http://crbug.com/173416.
758#if defined(TOOLKIT_VIEWS)
759  if (!GetAutocheckoutURLPrefix().empty())
760    autocheckout_manager_.OnLoadedPageMetaData(page_meta_data.Pass());
761#endif  // #if defined(TOOLKIT_VIEWS)
762
763  // If the corresponding flag is set, annotate forms with the predicted types.
764  driver_->SendAutofillTypePredictionsToRenderer(form_structures_.get());
765}
766
767void AutofillManager::OnDidEndTextFieldEditing() {
768  external_delegate_->DidEndTextFieldEditing();
769}
770
771void AutofillManager::OnAutocheckoutPageCompleted(
772    autofill::AutocheckoutStatus status) {
773  autocheckout_manager_.OnAutocheckoutPageCompleted(status);
774}
775
776std::string AutofillManager::GetAutocheckoutURLPrefix() const {
777  if (!driver_->GetWebContents())
778    return std::string();
779
780  autofill::autocheckout::WhitelistManager* whitelist_manager =
781      manager_delegate_->GetAutocheckoutWhitelistManager();
782
783  return whitelist_manager ? whitelist_manager->GetMatchedURLPrefix(
784      driver_->GetWebContents()->GetURL()) : std::string();
785}
786
787bool AutofillManager::IsAutofillEnabled() const {
788  return manager_delegate_->GetPrefs()->GetBoolean(prefs::kAutofillEnabled);
789}
790
791void AutofillManager::ImportFormData(const FormStructure& submitted_form) {
792  const CreditCard* imported_credit_card;
793  if (!personal_data_->ImportFormData(submitted_form, &imported_credit_card))
794    return;
795
796  // If credit card information was submitted, we need to confirm whether to
797  // save it.
798  if (imported_credit_card) {
799    manager_delegate_->ConfirmSaveCreditCard(
800        *metric_logger_,
801        *imported_credit_card,
802        base::Bind(&PersonalDataManager::SaveImportedCreditCard,
803                   base::Unretained(personal_data_), *imported_credit_card));
804  }
805}
806
807// Note that |submitted_form| is passed as a pointer rather than as a reference
808// so that we can get memory management right across threads.  Note also that we
809// explicitly pass in all the time stamps of interest, as the cached ones might
810// get reset before this method executes.
811void AutofillManager::UploadFormDataAsyncCallback(
812    const FormStructure* submitted_form,
813    const TimeTicks& load_time,
814    const TimeTicks& interaction_time,
815    const TimeTicks& submission_time) {
816  submitted_form->LogQualityMetrics(*metric_logger_,
817                                    load_time,
818                                    interaction_time,
819                                    submission_time);
820
821  if (submitted_form->ShouldBeCrowdsourced())
822    UploadFormData(*submitted_form);
823}
824
825void AutofillManager::OnMaybeShowAutocheckoutBubble(
826    const FormData& form,
827    const gfx::RectF& bounding_box) {
828  if (!IsAutofillEnabled())
829    return;
830
831  // Don't show bubble if corresponding FormStructure doesn't have anything to
832  // autofill.
833  FormStructure* cached_form;
834  if (!FindCachedForm(form, &cached_form))
835    return;
836
837  // Don't offer Autocheckout bubble if Autofill server is not aware of this
838  // form in the context of Autocheckout experiment.
839  if (!HasServerSpecifiedFieldTypes(*cached_form))
840    return;
841
842  autocheckout_manager_.MaybeShowAutocheckoutBubble(form.origin, bounding_box);
843}
844
845void AutofillManager::UploadFormData(const FormStructure& submitted_form) {
846  if (!download_manager_)
847    return;
848
849  // Check if the form is among the forms that were recently auto-filled.
850  bool was_autofilled = false;
851  std::string form_signature = submitted_form.FormSignature();
852  for (std::list<std::string>::const_iterator it =
853           autofilled_form_signatures_.begin();
854       it != autofilled_form_signatures_.end() && !was_autofilled;
855       ++it) {
856    if (*it == form_signature)
857      was_autofilled = true;
858  }
859
860  FieldTypeSet non_empty_types;
861  personal_data_->GetNonEmptyTypes(&non_empty_types);
862
863  download_manager_->StartUploadRequest(submitted_form, was_autofilled,
864                                       non_empty_types);
865}
866
867void AutofillManager::Reset() {
868  form_structures_.clear();
869  has_logged_autofill_enabled_ = false;
870  has_logged_address_suggestions_count_ = false;
871  did_show_suggestions_ = false;
872  user_did_type_ = false;
873  user_did_autofill_ = false;
874  user_did_edit_autofilled_field_ = false;
875  forms_loaded_timestamp_ = TimeTicks();
876  initial_interaction_timestamp_ = TimeTicks();
877  external_delegate_->Reset();
878}
879
880AutofillManager::AutofillManager(AutofillDriver* driver,
881                                 autofill::AutofillManagerDelegate* delegate,
882                                 PersonalDataManager* personal_data)
883    : driver_(driver),
884      manager_delegate_(delegate),
885      app_locale_("en-US"),
886      personal_data_(personal_data),
887      autocomplete_history_manager_(
888          new AutocompleteHistoryManager(driver, delegate)),
889      autocheckout_manager_(this),
890      metric_logger_(new AutofillMetrics),
891      has_logged_autofill_enabled_(false),
892      has_logged_address_suggestions_count_(false),
893      did_show_suggestions_(false),
894      user_did_type_(false),
895      user_did_autofill_(false),
896      user_did_edit_autofilled_field_(false),
897      external_delegate_(NULL),
898      test_delegate_(NULL),
899      weak_ptr_factory_(this) {
900  DCHECK(driver_);
901  DCHECK(driver_->GetWebContents());
902  DCHECK(manager_delegate_);
903}
904
905void AutofillManager::set_metric_logger(const AutofillMetrics* metric_logger) {
906  metric_logger_.reset(metric_logger);
907}
908
909bool AutofillManager::GetHost(RenderViewHost** host) const {
910  if (!IsAutofillEnabled())
911    return false;
912
913  // No autofill data to return if the profiles are empty.
914  if (personal_data_->GetProfiles().empty() &&
915      personal_data_->GetCreditCards().empty()) {
916    return false;
917  }
918
919  if (!driver_->RendererIsAvailable())
920    return false;
921
922  *host = driver_->GetWebContents()->GetRenderViewHost();
923  return true;
924}
925
926bool AutofillManager::GetProfileOrCreditCard(
927    int unique_id,
928    const AutofillDataModel** data_model,
929    size_t* variant) const {
930  // Unpack the |unique_id| into component parts.
931  GUIDPair credit_card_guid;
932  GUIDPair profile_guid;
933  UnpackGUIDs(unique_id, &credit_card_guid, &profile_guid);
934  DCHECK(!base::IsValidGUID(credit_card_guid.first) ||
935         !base::IsValidGUID(profile_guid.first));
936
937  // Find the profile that matches the |profile_guid|, if one is specified.
938  // Otherwise find the credit card that matches the |credit_card_guid|,
939  // if specified.
940  if (base::IsValidGUID(profile_guid.first)) {
941    *data_model = personal_data_->GetProfileByGUID(profile_guid.first);
942    *variant = profile_guid.second;
943  } else if (base::IsValidGUID(credit_card_guid.first)) {
944    *data_model = personal_data_->GetCreditCardByGUID(credit_card_guid.first);
945    *variant = credit_card_guid.second;
946  }
947
948  return !!*data_model;
949}
950
951bool AutofillManager::FindCachedForm(const FormData& form,
952                                     FormStructure** form_structure) const {
953  // Find the FormStructure that corresponds to |form|.
954  // Scan backward through the cached |form_structures_|, as updated versions of
955  // forms are added to the back of the list, whereas original versions of these
956  // forms might appear toward the beginning of the list.  The communication
957  // protocol with the crowdsourcing server does not permit us to discard the
958  // original versions of the forms.
959  *form_structure = NULL;
960  for (std::vector<FormStructure*>::const_reverse_iterator iter =
961           form_structures_.rbegin();
962       iter != form_structures_.rend(); ++iter) {
963    if (**iter == form) {
964      *form_structure = *iter;
965
966      // The same form might be cached with multiple field counts: in some
967      // cases, non-autofillable fields are filtered out, whereas in other cases
968      // they are not.  To avoid thrashing the cache, keep scanning until we
969      // find a cached version with the same number of fields, if there is one.
970      if ((*iter)->field_count() == form.fields.size())
971        break;
972    }
973  }
974
975  if (!(*form_structure))
976    return false;
977
978  return true;
979}
980
981bool AutofillManager::GetCachedFormAndField(const FormData& form,
982                                            const FormFieldData& field,
983                                            FormStructure** form_structure,
984                                            AutofillField** autofill_field) {
985  // Find the FormStructure that corresponds to |form|.
986  // If we do not have this form in our cache but it is parseable, we'll add it
987  // in the call to |UpdateCachedForm()|.
988  if (!FindCachedForm(form, form_structure) &&
989      !FormStructure(form, GetAutocheckoutURLPrefix()).ShouldBeParsed(false)) {
990    return false;
991  }
992
993  // Update the cached form to reflect any dynamic changes to the form data, if
994  // necessary.
995  if (!UpdateCachedForm(form, *form_structure, form_structure))
996    return false;
997
998  // No data to return if there are no auto-fillable fields.
999  if (!(*form_structure)->autofill_count())
1000    return false;
1001
1002  // Find the AutofillField that corresponds to |field|.
1003  *autofill_field = NULL;
1004  for (std::vector<AutofillField*>::const_iterator iter =
1005           (*form_structure)->begin();
1006       iter != (*form_structure)->end(); ++iter) {
1007    if ((**iter) == field) {
1008      *autofill_field = *iter;
1009      break;
1010    }
1011  }
1012
1013  // Even though we always update the cache, the field might not exist if the
1014  // website disables autocomplete while the user is interacting with the form.
1015  // See http://crbug.com/160476
1016  return *autofill_field != NULL;
1017}
1018
1019bool AutofillManager::UpdateCachedForm(const FormData& live_form,
1020                                       const FormStructure* cached_form,
1021                                       FormStructure** updated_form) {
1022  bool needs_update =
1023      (!cached_form ||
1024       live_form.fields.size() != cached_form->field_count());
1025  for (size_t i = 0; !needs_update && i < cached_form->field_count(); ++i) {
1026    needs_update = *cached_form->field(i) != live_form.fields[i];
1027  }
1028
1029  if (!needs_update)
1030    return true;
1031
1032  if (form_structures_.size() >= kMaxFormCacheSize)
1033    return false;
1034
1035  // Add the new or updated form to our cache.
1036  form_structures_.push_back(
1037      new FormStructure(live_form, GetAutocheckoutURLPrefix()));
1038  *updated_form = *form_structures_.rbegin();
1039  (*updated_form)->DetermineHeuristicTypes(*metric_logger_);
1040
1041  // If we have cached data, propagate it to the updated form.
1042  if (cached_form) {
1043    std::map<base::string16, const AutofillField*> cached_fields;
1044    for (size_t i = 0; i < cached_form->field_count(); ++i) {
1045      const AutofillField* field = cached_form->field(i);
1046      cached_fields[field->unique_name()] = field;
1047    }
1048
1049    for (size_t i = 0; i < (*updated_form)->field_count(); ++i) {
1050      AutofillField* field = (*updated_form)->field(i);
1051      std::map<base::string16, const AutofillField*>::iterator cached_field =
1052          cached_fields.find(field->unique_name());
1053      if (cached_field != cached_fields.end()) {
1054        field->set_server_type(cached_field->second->server_type());
1055        field->is_autofilled = cached_field->second->is_autofilled;
1056      }
1057    }
1058
1059    // Note: We _must not_ remove the original version of the cached form from
1060    // the list of |form_structures_|.  Otherwise, we break parsing of the
1061    // crowdsourcing server's response to our query.
1062  }
1063
1064  // Annotate the updated form with its predicted types.
1065  std::vector<FormStructure*> forms(1, *updated_form);
1066  driver_->SendAutofillTypePredictionsToRenderer(forms);
1067
1068  return true;
1069}
1070
1071void AutofillManager::GetProfileSuggestions(
1072    FormStructure* form,
1073    const FormFieldData& field,
1074    AutofillFieldType type,
1075    std::vector<base::string16>* values,
1076    std::vector<base::string16>* labels,
1077    std::vector<base::string16>* icons,
1078    std::vector<int>* unique_ids) const {
1079  std::vector<AutofillFieldType> field_types(form->field_count());
1080  for (size_t i = 0; i < form->field_count(); ++i) {
1081    field_types[i] = form->field(i)->type();
1082  }
1083  std::vector<GUIDPair> guid_pairs;
1084
1085  personal_data_->GetProfileSuggestions(
1086      type, field.value, field.is_autofilled, field_types,
1087      values, labels, icons, &guid_pairs);
1088
1089  for (size_t i = 0; i < guid_pairs.size(); ++i) {
1090    unique_ids->push_back(PackGUIDs(GUIDPair(std::string(), 0),
1091                                    guid_pairs[i]));
1092  }
1093}
1094
1095void AutofillManager::GetCreditCardSuggestions(
1096    const FormFieldData& field,
1097    AutofillFieldType type,
1098    std::vector<base::string16>* values,
1099    std::vector<base::string16>* labels,
1100    std::vector<base::string16>* icons,
1101    std::vector<int>* unique_ids) const {
1102  std::vector<GUIDPair> guid_pairs;
1103  personal_data_->GetCreditCardSuggestions(
1104      type, field.value, values, labels, icons, &guid_pairs);
1105
1106  for (size_t i = 0; i < guid_pairs.size(); ++i) {
1107    unique_ids->push_back(PackGUIDs(guid_pairs[i], GUIDPair(std::string(), 0)));
1108  }
1109}
1110
1111void AutofillManager::ParseForms(const std::vector<FormData>& forms) {
1112  std::vector<FormStructure*> non_queryable_forms;
1113  std::string autocheckout_url_prefix = GetAutocheckoutURLPrefix();
1114  for (std::vector<FormData>::const_iterator iter = forms.begin();
1115       iter != forms.end(); ++iter) {
1116    scoped_ptr<FormStructure> form_structure(
1117        new FormStructure(*iter, autocheckout_url_prefix));
1118    if (!form_structure->ShouldBeParsed(false))
1119      continue;
1120
1121    form_structure->DetermineHeuristicTypes(*metric_logger_);
1122
1123    // Set aside forms with method GET or author-specified types, so that they
1124    // are not included in the query to the server.
1125    if (form_structure->ShouldBeCrowdsourced())
1126      form_structures_.push_back(form_structure.release());
1127    else
1128      non_queryable_forms.push_back(form_structure.release());
1129  }
1130
1131  if (form_structures_.empty()) {
1132    // Call OnLoadedPageMetaData with no page metadata immediately if there is
1133    // no form in the page. This give |autocheckout_manager| a chance to
1134    // terminate Autocheckout and send Autocheckout status.
1135    autocheckout_manager_.OnLoadedPageMetaData(
1136        scoped_ptr<autofill::AutocheckoutPageMetaData>());
1137  } else if (download_manager_) {
1138    // Query the server if we have at least one of the forms were parsed.
1139    download_manager_->StartQueryRequest(form_structures_.get(),
1140                                        *metric_logger_);
1141  }
1142
1143  for (std::vector<FormStructure*>::const_iterator iter =
1144           non_queryable_forms.begin();
1145       iter != non_queryable_forms.end(); ++iter) {
1146    form_structures_.push_back(*iter);
1147  }
1148
1149  if (!form_structures_.empty())
1150    metric_logger_->LogUserHappinessMetric(AutofillMetrics::FORMS_LOADED);
1151
1152  // For the |non_queryable_forms|, we have all the field type info we're ever
1153  // going to get about them.  For the other forms, we'll wait until we get a
1154  // response from the server.
1155  driver_->SendAutofillTypePredictionsToRenderer(non_queryable_forms);
1156}
1157
1158int AutofillManager::GUIDToID(const GUIDPair& guid) const {
1159  if (!base::IsValidGUID(guid.first))
1160    return 0;
1161
1162  std::map<GUIDPair, int>::const_iterator iter = guid_id_map_.find(guid);
1163  if (iter == guid_id_map_.end()) {
1164    int id = guid_id_map_.size() + 1;
1165    guid_id_map_[guid] = id;
1166    id_guid_map_[id] = guid;
1167    return id;
1168  } else {
1169    return iter->second;
1170  }
1171}
1172
1173const GUIDPair AutofillManager::IDToGUID(int id) const {
1174  if (id == 0)
1175    return GUIDPair(std::string(), 0);
1176
1177  std::map<int, GUIDPair>::const_iterator iter = id_guid_map_.find(id);
1178  if (iter == id_guid_map_.end()) {
1179    NOTREACHED();
1180    return GUIDPair(std::string(), 0);
1181  }
1182
1183  return iter->second;
1184}
1185
1186// When sending IDs (across processes) to the renderer we pack credit card and
1187// profile IDs into a single integer.  Credit card IDs are sent in the high
1188// word and profile IDs are sent in the low word.
1189int AutofillManager::PackGUIDs(const GUIDPair& cc_guid,
1190                               const GUIDPair& profile_guid) const {
1191  int cc_id = GUIDToID(cc_guid);
1192  int profile_id = GUIDToID(profile_guid);
1193
1194  DCHECK(cc_id <= std::numeric_limits<unsigned short>::max());
1195  DCHECK(profile_id <= std::numeric_limits<unsigned short>::max());
1196
1197  return cc_id << std::numeric_limits<unsigned short>::digits | profile_id;
1198}
1199
1200// When receiving IDs (across processes) from the renderer we unpack credit card
1201// and profile IDs from a single integer.  Credit card IDs are stored in the
1202// high word and profile IDs are stored in the low word.
1203void AutofillManager::UnpackGUIDs(int id,
1204                                  GUIDPair* cc_guid,
1205                                  GUIDPair* profile_guid) const {
1206  int cc_id = id >> std::numeric_limits<unsigned short>::digits &
1207      std::numeric_limits<unsigned short>::max();
1208  int profile_id = id & std::numeric_limits<unsigned short>::max();
1209
1210  *cc_guid = IDToGUID(cc_id);
1211  *profile_guid = IDToGUID(profile_id);
1212}
1213
1214void AutofillManager::UpdateInitialInteractionTimestamp(
1215    const TimeTicks& interaction_timestamp) {
1216  if (initial_interaction_timestamp_.is_null() ||
1217      interaction_timestamp < initial_interaction_timestamp_) {
1218    initial_interaction_timestamp_ = interaction_timestamp;
1219  }
1220}
1221
1222}  // namespace autofill
1223