1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/autocomplete_history_manager.h"
#include <vector>
#include "base/profiler/scoped_tracker.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_client.h"
#include "components/autofill/core/browser/autofill_driver.h"
#include "components/autofill/core/browser/autofill_experiments.h"
#include "components/autofill/core/browser/autofill_external_delegate.h"
#include "components/autofill/core/browser/suggestion.h"
#include "components/autofill/core/browser/validation.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/autofill/core/common/form_data.h"
#include "components/prefs/pref_service.h"
namespace autofill {
namespace {
// Limit on the number of suggestions to appear in the pop-up menu under an
// text input element in a form.
const int kMaxAutocompleteMenuItems = 6;
bool IsTextField(const FormFieldData& field) {
return
field.form_control_type == "text" ||
field.form_control_type == "search" ||
field.form_control_type == "tel" ||
field.form_control_type == "url" ||
field.form_control_type == "email";
}
} // namespace
AutocompleteHistoryManager::AutocompleteHistoryManager(
AutofillDriver* driver,
AutofillClient* autofill_client)
: driver_(driver),
database_(autofill_client->GetDatabase()),
pending_query_handle_(0),
query_id_(0),
external_delegate_(NULL),
autofill_client_(autofill_client) {
DCHECK(autofill_client_);
}
AutocompleteHistoryManager::~AutocompleteHistoryManager() {
CancelPendingQuery();
}
void AutocompleteHistoryManager::OnGetAutocompleteSuggestions(
int query_id,
const base::string16& name,
const base::string16& prefix,
const std::string& form_control_type) {
CancelPendingQuery();
query_id_ = query_id;
if (!autofill_client_->IsAutocompleteEnabled() ||
form_control_type == "textarea" ||
IsInAutofillSuggestionsDisabledExperiment()) {
SendSuggestions(NULL);
return;
}
if (database_.get()) {
pending_query_handle_ = database_->GetFormValuesForElementName(
name, prefix, kMaxAutocompleteMenuItems, this);
}
}
void AutocompleteHistoryManager::OnWillSubmitForm(const FormData& form) {
if (!autofill_client_->IsAutocompleteEnabled())
return;
if (driver_->IsOffTheRecord())
return;
// We put the following restriction on stored FormFields:
// - non-empty name
// - non-empty value
// - text field
// - autocomplete is not disabled
// - value is not a credit card number
// - value is not a SSN
// - field was not identified as a CVC field (this is handled in
// AutofillManager)
std::vector<FormFieldData> values;
for (const FormFieldData& field : form.fields) {
if (!field.value.empty() &&
!field.name.empty() &&
IsTextField(field) &&
field.should_autocomplete &&
!IsValidCreditCardNumber(field.value) &&
!IsSSN(field.value)) {
values.push_back(field);
}
}
if (!values.empty() && database_.get())
database_->AddFormFields(values);
}
void AutocompleteHistoryManager::OnRemoveAutocompleteEntry(
const base::string16& name, const base::string16& value) {
if (database_.get())
database_->RemoveFormValueForElementName(name, value);
}
void AutocompleteHistoryManager::SetExternalDelegate(
AutofillExternalDelegate* delegate) {
external_delegate_ = delegate;
}
void AutocompleteHistoryManager::CancelPendingQuery() {
if (pending_query_handle_) {
if (database_.get())
database_->CancelRequest(pending_query_handle_);
pending_query_handle_ = 0;
}
}
void AutocompleteHistoryManager::SendSuggestions(
const std::vector<base::string16>* autocomplete_results) {
std::vector<Suggestion> suggestions;
if (autocomplete_results) {
for (const auto& result : *autocomplete_results) {
suggestions.push_back(Suggestion(result));
}
}
external_delegate_->OnSuggestionsReturned(query_id_, suggestions);
query_id_ = 0;
}
void AutocompleteHistoryManager::OnWebDataServiceRequestDone(
WebDataServiceBase::Handle h,
std::unique_ptr<WDTypedResult> result) {
// TODO(robliao): Remove ScopedTracker below once https://crbug.com/422460 is
// fixed.
tracked_objects::ScopedTracker tracking_profile(
FROM_HERE_WITH_EXPLICIT_FUNCTION(
"422460 AutocompleteHistoryManager::OnWebDataServiceRequestDone"));
DCHECK(pending_query_handle_);
pending_query_handle_ = 0;
DCHECK(result);
// Returning early here if |result| is NULL. We've seen this happen on
// Linux due to NFS dismounting and causing sql failures.
// See http://crbug.com/68783.
if (!result) {
SendSuggestions(NULL);
return;
}
DCHECK_EQ(AUTOFILL_VALUE_RESULT, result->GetType());
const WDResult<std::vector<base::string16>>* autofill_result =
static_cast<const WDResult<std::vector<base::string16>>*>(result.get());
std::vector<base::string16> suggestions = autofill_result->GetValue();
SendSuggestions(&suggestions);
}
} // namespace autofill
|