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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
// Copyright (c) 2012 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 "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_external_delegate.h"
#include "chrome/browser/autofill/autofill_manager.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/autofill_messages.h"
#include "chrome/common/chrome_constants.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
AutofillExternalDelegate::~AutofillExternalDelegate() {
}
AutofillExternalDelegate::AutofillExternalDelegate(
TabContentsWrapper* tab_contents_wrapper,
AutofillManager* autofill_manager)
: tab_contents_wrapper_(tab_contents_wrapper),
autofill_manager_(autofill_manager),
autofill_query_id_(0),
display_warning_if_disabled_(false),
has_shown_autofill_popup_for_current_edit_(false),
suggestions_clear_index_(-1),
suggestions_options_index_(-1) {
}
void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id,
int list_index) {
if (list_index == suggestions_options_index_ ||
list_index == suggestions_clear_index_ ||
unique_id == -1)
return;
FillAutofillFormData(unique_id, true);
}
void AutofillExternalDelegate::OnQuery(int query_id,
const webkit::forms::FormData& form,
const webkit::forms::FormField& field,
const gfx::Rect& bounds,
bool display_warning_if_disabled) {
autofill_query_form_ = form;
autofill_query_field_ = field;
display_warning_if_disabled_ = display_warning_if_disabled;
autofill_query_id_ = query_id;
OnQueryPlatformSpecific(query_id, form, field, bounds);
}
void AutofillExternalDelegate::OnSuggestionsReturned(
int query_id,
const std::vector<string16>& values,
const std::vector<string16>& labels,
const std::vector<string16>& icons,
const std::vector<int>& unique_ids) {
if (query_id != autofill_query_id_)
return;
if (values.empty()) {
// No suggestions, any popup currently showing is obsolete.
HideAutofillPopup();
return;
}
std::vector<string16> v(values);
std::vector<string16> l(labels);
std::vector<string16> i(icons);
std::vector<int> ids(unique_ids);
int separator_index = -1;
DCHECK_GT(ids.size(), 0U);
if (!autofill_query_field_.should_autocomplete) {
// If autofill is disabled and we had suggestions, show a warning instead.
v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED));
l.assign(1, string16());
i.assign(1, string16());
ids.assign(1, -1);
} else if (ids[0] < 0 && ids.size() > 1) {
// If we received a warning instead of suggestions from autofill but regular
// suggestions from autocomplete, don't show the autofill warning.
v.erase(v.begin());
l.erase(l.begin());
i.erase(i.begin());
ids.erase(ids.begin());
}
// If we were about to show a warning and we shouldn't, don't.
if (ids[0] < 0 && !display_warning_if_disabled_)
return;
// Only include "Autofill Options" special menu item if we have Autofill
// items, identified by |unique_ids| having at least one valid value.
bool has_autofill_item = false;
for (size_t i = 0; i < ids.size(); ++i) {
if (ids[i] > 0) {
has_autofill_item = true;
break;
}
}
// The form has been auto-filled, so give the user the chance to clear the
// form. Append the 'Clear form' menu item.
if (has_autofill_item &&
autofill_query_field_.is_autofilled) {
v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
l.push_back(string16());
i.push_back(string16());
ids.push_back(0);
suggestions_clear_index_ = v.size() - 1;
separator_index = v.size() - 1;
}
if (has_autofill_item) {
// Append the 'Chrome Autofill options' menu item;
v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP));
l.push_back(string16());
i.push_back(string16());
ids.push_back(0);
suggestions_options_index_ = v.size() - 1;
separator_index = values.size();
}
// Send to display.
if (!v.empty() && autofill_query_field_.is_focusable)
ApplyAutofillSuggestions(v, l, i, ids, separator_index);
tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions(
has_autofill_item && !has_shown_autofill_popup_for_current_edit_);
has_shown_autofill_popup_for_current_edit_ |= has_autofill_item;
}
void AutofillExternalDelegate::DidEndTextFieldEditing() {
has_shown_autofill_popup_for_current_edit_ = false;
}
void AutofillExternalDelegate::DidAcceptAutofillSuggestions(
const string16& value,
int unique_id,
unsigned index) {
// If the selected element is a warning we don't want to do anything.
if (unique_id < 0)
return;
// TODO(csharp): Add the password autofill manager.
// if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value))
// return;
if (suggestions_options_index_ != -1 &&
index == static_cast<unsigned>(suggestions_options_index_)) {
// User selected 'Autofill Options'.
autofill_manager_->OnShowAutofillDialog();
} else if (suggestions_clear_index_ != -1 &&
index == static_cast<unsigned>(suggestions_clear_index_)) {
// User selected 'Clear form'.
RenderViewHost* host =
tab_contents_wrapper_->web_contents()->GetRenderViewHost();
host->Send(new AutofillMsg_ClearForm(host->routing_id()));
} else if (!unique_id) {
// User selected an Autocomplete entry, so we fill directly.
RenderViewHost* host =
tab_contents_wrapper_->web_contents()->GetRenderViewHost();
host->Send(new AutofillMsg_SetNodeText(
host->routing_id(),
value));
} else {
FillAutofillFormData(unique_id, false);
}
HideAutofillPopup();
}
void AutofillExternalDelegate::ClearPreviewedForm() {
RenderViewHost* host =
tab_contents_wrapper_->web_contents()->GetRenderViewHost();
host->Send(new AutofillMsg_ClearPreviewedForm(host->routing_id()));
}
void AutofillExternalDelegate::HideAutofillPopup() {
suggestions_clear_index_ = -1;
suggestions_options_index_ = -1;
HideAutofillPopupInternal();
}
void AutofillExternalDelegate::FillAutofillFormData(int unique_id,
bool is_preview) {
RenderViewHost* host =
tab_contents_wrapper_->web_contents()->GetRenderViewHost();
if (is_preview) {
host->Send(new AutofillMsg_SetAutofillActionPreview(
host->routing_id()));
} else {
host->Send(new AutofillMsg_SetAutofillActionFill(
host->routing_id()));
}
// Fill the values for the whole form.
autofill_manager_->OnFillAutofillFormData(autofill_query_id_,
autofill_query_form_,
autofill_query_field_,
unique_id);
}
// Add a "!defined(OS_YOUROS) for each platform that implements this
// in an autofill_external_delegate_YOUROS.cc. Currently there are
// none, so all platforms use the default.
#if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK)
AutofillExternalDelegate* AutofillExternalDelegate::Create(
TabContentsWrapper*, AutofillManager*) {
return NULL;
}
#endif
|