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
|
// Copyright (c) 2006-2008 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 "chrome/browser/autocomplete_history_manager.h"
#include <vector>
#include "base/string_util.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/pref_names.h"
#include "webkit/glue/form_field_values.h"
// Limit on the number of suggestions to appear in the pop-up menu under an
// text input element in a form.
static const int kMaxAutofillMenuItems = 6;
AutocompleteHistoryManager::AutocompleteHistoryManager(
TabContents* tab_contents) : tab_contents_(tab_contents),
pending_query_handle_(0),
query_id_(0) {
form_autofill_enabled_.Init(prefs::kAutoFillEnabled,
profile()->GetPrefs(), NULL);
}
AutocompleteHistoryManager::~AutocompleteHistoryManager() {
CancelPendingQuery();
}
void AutocompleteHistoryManager::CancelPendingQuery() {
if (pending_query_handle_) {
SendSuggestions(NULL);
WebDataService* web_data_service =
profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
if (!web_data_service) {
NOTREACHED();
return;
}
web_data_service->CancelRequest(pending_query_handle_);
}
pending_query_handle_ = 0;
}
Profile* AutocompleteHistoryManager::profile() {
return tab_contents_->profile();
}
void AutocompleteHistoryManager::FormFieldValuesSubmitted(
const webkit_glue::FormFieldValues& form) {
StoreFormEntriesInWebDatabase(form);
}
bool AutocompleteHistoryManager::GetAutocompleteSuggestions(
int query_id, const string16& name, const string16& prefix) {
if (!*form_autofill_enabled_)
return false;
WebDataService* web_data_service =
profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
if (!web_data_service) {
NOTREACHED();
return false;
}
CancelPendingQuery();
query_id_ = query_id;
pending_query_handle_ = web_data_service->GetFormValuesForElementName(
name, prefix, kMaxAutofillMenuItems, this);
return true;
}
void AutocompleteHistoryManager::RemoveAutocompleteEntry(
const string16& name, const string16& value) {
WebDataService* web_data_service =
profile()->GetWebDataService(Profile::EXPLICIT_ACCESS);
if (!web_data_service) {
NOTREACHED();
return;
}
web_data_service->RemoveFormValueForElementName(name, value);
}
void AutocompleteHistoryManager::OnWebDataServiceRequestDone(
WebDataService::Handle h,
const WDTypedResult* result) {
DCHECK(pending_query_handle_);
pending_query_handle_ = 0;
if (*form_autofill_enabled_) {
DCHECK(result);
SendSuggestions(result);
} else {
SendSuggestions(NULL);
}
}
void AutocompleteHistoryManager::StoreFormEntriesInWebDatabase(
const webkit_glue::FormFieldValues& form) {
if (!*form_autofill_enabled_)
return;
if (profile()->IsOffTheRecord())
return;
// We put the following restriction on stored FormFields:
// - non-empty name
// - non-empty value
// - text field
std::vector<webkit_glue::FormField> values;
for (std::vector<webkit_glue::FormField>::const_iterator iter =
form.elements.begin();
iter != form.elements.end(); ++iter) {
if (!iter->value().empty() &&
!iter->name().empty() &&
iter->form_control_type() == ASCIIToUTF16("text"))
values.push_back(*iter);
}
profile()->GetWebDataService(Profile::EXPLICIT_ACCESS)->
AddFormFieldValues(values);
}
void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) {
RenderViewHost* host = tab_contents_->render_view_host();
if (!host)
return;
if (result) {
DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT);
const WDResult<std::vector<string16> >* autofill_result =
static_cast<const WDResult<std::vector<string16> >*>(result);
host->AutocompleteSuggestionsReturned(
query_id_, autofill_result->GetValue(), -1);
} else {
host->AutocompleteSuggestionsReturned(
query_id_, std::vector<string16>(), -1);
}
}
|