Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: chrome/renderer/autofill/autofill_agent.cc

Issue 10024059: DataList UI (Chromium part) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added type ahead for datalist Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/autofill/autofill_agent.h" 5 #include "chrome/renderer/autofill/autofill_agent.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/time.h" 9 #include "base/time.h"
10 #include "base/utf_string_conversions.h" 10 #include "base/utf_string_conversions.h"
11 #include "chrome/common/autofill_messages.h" 11 #include "chrome/common/autofill_messages.h"
12 #include "chrome/common/chrome_constants.h" 12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/renderer/autofill/form_autofill_util.h" 13 #include "chrome/renderer/autofill/form_autofill_util.h"
14 #include "chrome/renderer/autofill/password_autofill_manager.h" 14 #include "chrome/renderer/autofill/password_autofill_manager.h"
15 #include "content/public/renderer/render_view.h" 15 #include "content/public/renderer/render_view.h"
16 #include "grit/chromium_strings.h" 16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h" 17 #include "grit/generated_resources.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h"
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h" 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputEvent.h"
22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNodeCollection.h"
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebOptionElement.h"
23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
24 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
25 #include "ui/base/keycodes/keyboard_codes.h" 27 #include "ui/base/keycodes/keyboard_codes.h"
26 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
27 #include "webkit/forms/form_data.h" 29 #include "webkit/forms/form_data.h"
28 #include "webkit/forms/form_data_predictions.h" 30 #include "webkit/forms/form_data_predictions.h"
29 #include "webkit/forms/form_field.h" 31 #include "webkit/forms/form_field.h"
30 #include "webkit/forms/password_form.h" 32 #include "webkit/forms/password_form.h"
31 33
34 using WebKit::WebAutofillClient;
32 using WebKit::WebFormControlElement; 35 using WebKit::WebFormControlElement;
33 using WebKit::WebFormElement; 36 using WebKit::WebFormElement;
34 using WebKit::WebFrame; 37 using WebKit::WebFrame;
35 using WebKit::WebInputElement; 38 using WebKit::WebInputElement;
36 using WebKit::WebKeyboardEvent; 39 using WebKit::WebKeyboardEvent;
37 using WebKit::WebNode; 40 using WebKit::WebNode;
41 using WebKit::WebNodeCollection;
42 using WebKit::WebOptionElement;
38 using WebKit::WebString; 43 using WebKit::WebString;
39 using webkit::forms::FormData; 44 using webkit::forms::FormData;
40 using webkit::forms::FormDataPredictions; 45 using webkit::forms::FormDataPredictions;
41 46
42 namespace { 47 namespace {
43 48
44 // The size above which we stop triggering autofill for an input text field 49 // The size above which we stop triggering autofill for an input text field
45 // (so to avoid sending long strings through IPC). 50 // (so to avoid sending long strings through IPC).
46 const size_t kMaximumTextSizeForAutofill = 1000; 51 const size_t kMaximumTextSizeForAutofill = 1000;
47 52
53 void AppendDataListSuggestions(const WebKit::WebInputElement& element,
54 std::vector<string16>* values,
55 std::vector<string16>* labels,
56 std::vector<string16>* icons,
57 std::vector<int>* item_ids) {
58 WebNodeCollection options = element.dataListOptions();
59 if (options.isNull())
60 return;
61
62 for (WebOptionElement option = options.firstItem().to<WebOptionElement>();
63 !option.isNull(); option = options.nextItem().to<WebOptionElement>()) {
64 if (!StartsWith(option.value(), element.value(), false))
65 continue;
Ilya Sherman 2012/04/16 21:45:49 nit: Please add a blank line after this line, for
keishi 2012/04/17 09:15:10 Done.
66 values->push_back(option.value());
67 if (option.value() != option.label())
68 labels->push_back(option.label());
69 else
70 labels->push_back(string16());
71 icons->push_back(string16());
72 item_ids->push_back(WebAutofillClient::MenuItemIDDataListEntry);
73 }
74 }
75
48 } // namespace 76 } // namespace
49 77
50 namespace autofill { 78 namespace autofill {
51 79
52 AutofillAgent::AutofillAgent( 80 AutofillAgent::AutofillAgent(
53 content::RenderView* render_view, 81 content::RenderView* render_view,
54 PasswordAutofillManager* password_autofill_manager) 82 PasswordAutofillManager* password_autofill_manager)
55 : content::RenderViewObserver(render_view), 83 : content::RenderViewObserver(render_view),
56 password_autofill_manager_(password_autofill_manager), 84 password_autofill_manager_(password_autofill_manager),
57 autofill_query_id_(0), 85 autofill_query_id_(0),
58 autofill_action_(AUTOFILL_NONE), 86 autofill_action_(AUTOFILL_NONE),
59 display_warning_if_disabled_(false), 87 display_warning_if_disabled_(false),
60 was_query_node_autofilled_(false), 88 was_query_node_autofilled_(false),
61 suggestions_clear_index_(-1),
62 suggestions_options_index_(-1),
63 has_shown_autofill_popup_for_current_edit_(false), 89 has_shown_autofill_popup_for_current_edit_(false),
64 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { 90 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
65 render_view->GetWebView()->setAutofillClient(this); 91 render_view->GetWebView()->setAutofillClient(this);
66 } 92 }
67 93
68 AutofillAgent::~AutofillAgent() {} 94 AutofillAgent::~AutofillAgent() {}
69 95
70 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) { 96 bool AutofillAgent::OnMessageReceived(const IPC::Message& message) {
71 bool handled = true; 97 bool handled = true;
72 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message) 98 IPC_BEGIN_MESSAGE_MAP(AutofillAgent, message)
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 165
140 bool AutofillAgent::InputElementLostFocus() { 166 bool AutofillAgent::InputElementLostFocus() {
141 Send(new AutofillHostMsg_HideAutofillPopup(routing_id())); 167 Send(new AutofillHostMsg_HideAutofillPopup(routing_id()));
142 168
143 return false; 169 return false;
144 } 170 }
145 171
146 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node, 172 void AutofillAgent::didAcceptAutofillSuggestion(const WebNode& node,
147 const WebString& value, 173 const WebString& value,
148 const WebString& label, 174 const WebString& label,
149 int unique_id, 175 int item_id,
150 unsigned index) { 176 unsigned index) {
151 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) 177 if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value))
152 return; 178 return;
153 179
154 DCHECK(node == autofill_query_element_); 180 DCHECK(node == element_);
155 181
156 if (suggestions_options_index_ != -1 && 182 switch (item_id) {
157 index == static_cast<unsigned>(suggestions_options_index_)) { 183 case WebAutofillClient::MenuItemIDWarningMessage:
158 // User selected 'Autofill Options'. 184 case WebAutofillClient::MenuItemIDSeparator:
159 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id())); 185 NOTREACHED();
160 } else if (suggestions_clear_index_ != -1 && 186 break;
161 index == static_cast<unsigned>(suggestions_clear_index_)) { 187 case WebAutofillClient::MenuItemIDAutofillOptions:
162 // User selected 'Clear form'. 188 // User selected 'Autofill Options'.
163 form_cache_.ClearFormWithElement(autofill_query_element_); 189 Send(new AutofillHostMsg_ShowAutofillDialog(routing_id()));
164 } else if (!unique_id) { 190 break;
165 // User selected an Autocomplete entry, so we fill directly. 191 case WebAutofillClient::MenuItemIDClearForm:
166 WebInputElement element = node.toConst<WebInputElement>(); 192 // User selected 'Clear form'.
167 SetNodeText(value, &element); 193 form_cache_.ClearFormWithElement(element_);
168 } else { 194 break;
169 // Fill the values for the whole form. 195 case WebAutofillClient::MenuItemIDAutocompleteEntry:
170 FillAutofillFormData(node, unique_id, AUTOFILL_FILL); 196 case WebAutofillClient::MenuItemIDPasswordEntry:
197 case WebAutofillClient::MenuItemIDDataListEntry:
198 // User selected an Autocomplete or password or datalist entry, so we
199 // fill directly.
200 SetNodeText(value, &element_);
201 break;
202 default:
203 // A positive item_id is a unique id for an autofill (vs. autocomplete)
204 // suggestion.
205 DCHECK_GT(item_id, 0);
206 // Fill the values for the whole form.
207 FillAutofillFormData(node, item_id, AUTOFILL_FILL);
171 } 208 }
172
173 suggestions_clear_index_ = -1;
174 suggestions_options_index_ = -1;
175 } 209 }
176 210
177 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node, 211 void AutofillAgent::didSelectAutofillSuggestion(const WebNode& node,
178 const WebString& value, 212 const WebString& value,
179 const WebString& label, 213 const WebString& label,
180 int unique_id) { 214 int item_id) {
181 DCHECK_GE(unique_id, 0);
182 if (password_autofill_manager_->DidSelectAutofillSuggestion(node)) 215 if (password_autofill_manager_->DidSelectAutofillSuggestion(node))
183 return; 216 return;
184 217
185 didClearAutofillSelection(node); 218 didClearAutofillSelection(node);
186 FillAutofillFormData(node, unique_id, AUTOFILL_PREVIEW); 219
220 if (item_id > 0)
221 FillAutofillFormData(node, item_id, AUTOFILL_PREVIEW);
187 } 222 }
188 223
189 void AutofillAgent::didClearAutofillSelection(const WebNode& node) { 224 void AutofillAgent::didClearAutofillSelection(const WebNode& node) {
190 if (password_autofill_manager_->DidClearAutofillSelection(node)) 225 if (password_autofill_manager_->DidClearAutofillSelection(node))
191 return; 226 return;
192 227
193 if (!autofill_query_element_.isNull() && node == autofill_query_element_) { 228 if (!element_.isNull() && node == element_) {
194 ClearPreviewedFormWithElement(autofill_query_element_, 229 ClearPreviewedFormWithElement(element_, was_query_node_autofilled_);
195 was_query_node_autofilled_);
196 } else { 230 } else {
197 // TODO(isherman): There seem to be rare cases where this code *is* 231 // TODO(isherman): There seem to be rare cases where this code *is*
198 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would 232 // reachable: see [ http://crbug.com/96321#c6 ]. Ideally we would
199 // understand those cases and fix the code to avoid them. However, so far I 233 // understand those cases and fix the code to avoid them. However, so far I
200 // have been unable to reproduce such a case locally. If you hit this 234 // have been unable to reproduce such a case locally. If you hit this
201 // NOTREACHED(), please file a bug against me. 235 // NOTREACHED(), please file a bug against me.
202 NOTREACHED(); 236 NOTREACHED();
203 } 237 }
204 } 238 }
205 239
206 void AutofillAgent::removeAutocompleteSuggestion(const WebString& name, 240 void AutofillAgent::removeAutocompleteSuggestion(const WebString& name,
207 const WebString& value) { 241 const WebString& value) {
208 // The index of clear & options will have shifted down.
209 if (suggestions_clear_index_ != -1)
210 suggestions_clear_index_--;
211 if (suggestions_options_index_ != -1)
212 suggestions_options_index_--;
213
214 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value)); 242 Send(new AutofillHostMsg_RemoveAutocompleteEntry(routing_id(), name, value));
215 } 243 }
216 244
217 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) { 245 void AutofillAgent::textFieldDidEndEditing(const WebInputElement& element) {
218 password_autofill_manager_->TextFieldDidEndEditing(element); 246 password_autofill_manager_->TextFieldDidEndEditing(element);
219 has_shown_autofill_popup_for_current_edit_ = false; 247 has_shown_autofill_popup_for_current_edit_ = false;
220 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id())); 248 Send(new AutofillHostMsg_DidEndTextFieldEditing(routing_id()));
221 } 249 }
222 250
223 void AutofillAgent::textFieldDidChange(const WebInputElement& element) { 251 void AutofillAgent::textFieldDidChange(const WebInputElement& element) {
224 // We post a task for doing the Autofill as the caret position is not set 252 // We post a task for doing the Autofill as the caret position is not set
225 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and 253 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) and
226 // it is needed to trigger autofill. 254 // it is needed to trigger autofill.
227 weak_ptr_factory_.InvalidateWeakPtrs(); 255 weak_ptr_factory_.InvalidateWeakPtrs();
228 MessageLoop::current()->PostTask( 256 MessageLoop::current()->PostTask(
229 FROM_HERE, 257 FROM_HERE,
230 base::Bind(&AutofillAgent::TextFieldDidChangeImpl, 258 base::Bind(&AutofillAgent::TextFieldDidChangeImpl,
231 weak_ptr_factory_.GetWeakPtr(), element)); 259 weak_ptr_factory_.GetWeakPtr(), element));
232 } 260 }
233 261
234 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) { 262 void AutofillAgent::TextFieldDidChangeImpl(const WebInputElement& element) {
235 if (password_autofill_manager_->TextDidChangeInTextField(element)) { 263 if (password_autofill_manager_->TextDidChangeInTextField(element)) {
236 autofill_query_element_ = element; 264 element_ = element;
237 return; 265 return;
238 } 266 }
239 267
240 ShowSuggestions(element, false, true, false); 268 ShowSuggestions(element, false, true, false);
241 269
242 webkit::forms::FormData form; 270 webkit::forms::FormData form;
243 webkit::forms::FormField field; 271 webkit::forms::FormField field;
244 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) { 272 if (FindFormAndFieldForInputElement(element, &form, &field, REQUIRE_NONE)) {
245 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field, 273 Send(new AutofillHostMsg_TextFieldDidChange(routing_id(), form, field,
246 base::TimeTicks::Now())); 274 base::TimeTicks::Now()));
247 } 275 }
248 } 276 }
249 277
250 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element, 278 void AutofillAgent::textFieldDidReceiveKeyDown(const WebInputElement& element,
251 const WebKeyboardEvent& event) { 279 const WebKeyboardEvent& event) {
252 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) { 280 if (password_autofill_manager_->TextFieldHandlingKeyDown(element, event)) {
253 autofill_query_element_ = element; 281 element_ = element;
254 return; 282 return;
255 } 283 }
256 284
257 if (event.windowsKeyCode == ui::VKEY_DOWN || 285 if (event.windowsKeyCode == ui::VKEY_DOWN ||
258 event.windowsKeyCode == ui::VKEY_UP) 286 event.windowsKeyCode == ui::VKEY_UP)
259 ShowSuggestions(element, true, true, true); 287 ShowSuggestions(element, true, true, true);
260 } 288 }
261 289
262 void AutofillAgent::OnSuggestionsReturned(int query_id, 290 void AutofillAgent::OnSuggestionsReturned(int query_id,
263 const std::vector<string16>& values, 291 const std::vector<string16>& values,
264 const std::vector<string16>& labels, 292 const std::vector<string16>& labels,
265 const std::vector<string16>& icons, 293 const std::vector<string16>& icons,
266 const std::vector<int>& unique_ids) { 294 const std::vector<int>& unique_ids) {
267 WebKit::WebView* web_view = render_view()->GetWebView(); 295 if (query_id != autofill_query_id_)
268 if (!web_view || query_id != autofill_query_id_)
269 return; 296 return;
270 297
271 if (values.empty()) {
272 // No suggestions, any popup currently showing is obsolete.
273 web_view->hidePopups();
274 return;
275 }
276
277 std::vector<string16> v(values); 298 std::vector<string16> v(values);
278 std::vector<string16> l(labels); 299 std::vector<string16> l(labels);
279 std::vector<string16> i(icons); 300 std::vector<string16> i(icons);
280 std::vector<int> ids(unique_ids); 301 std::vector<int> ids(unique_ids);
281 int separator_index = -1;
282 302
283 DCHECK_GT(ids.size(), 0U); 303 // Only include "Autofill Options" special menu item if we have Autofill
284 if (!autofill_query_element_.isNull() && 304 // items, identified by |unique_ids| having at least one valid value.
285 !autofill_query_element_.autoComplete()) { 305 bool has_autofill_item = false;
306 for (size_t j = 0; j < ids.size(); ++j) {
307 if (ids[j] > 0) {
308 has_autofill_item = true;
309 break;
310 }
311 }
Ilya Sherman 2012/04/16 21:45:49 nit: It looks like you don't use |has_autofill_ite
keishi 2012/04/17 09:15:10 Done.
312
313 if (!element_.isNull() && !element_.autoComplete() && !v.empty()) {
286 // If autofill is disabled and we had suggestions, show a warning instead. 314 // If autofill is disabled and we had suggestions, show a warning instead.
287 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); 315 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED));
288 l.assign(1, string16()); 316 l.assign(1, string16());
289 i.assign(1, string16()); 317 i.assign(1, string16());
290 ids.assign(1, -1); 318 ids.assign(1, WebAutofillClient::MenuItemIDWarningMessage);
291 } else if (ids[0] < 0 && ids.size() > 1) { 319 } else if (ids.size() > 1 &&
292 // If we received a warning instead of suggestions from autofill but regular 320 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
293 // suggestions from autocomplete, don't show the autofill warning. 321 // If we received an autofill warning plus some autocomplete suggestions,
322 // remove the autofill warning.
294 v.erase(v.begin()); 323 v.erase(v.begin());
295 l.erase(l.begin()); 324 l.erase(l.begin());
296 i.erase(i.begin()); 325 i.erase(i.begin());
297 ids.erase(ids.begin()); 326 ids.erase(ids.begin());
298 } 327 }
299 328
300 // If we were about to show a warning and we shouldn't, don't. 329 // If we were about to show a warning and we shouldn't, don't.
301 if (ids[0] < 0 && !display_warning_if_disabled_) 330 if (!display_warning_if_disabled_ && !v.empty() &&
302 return; 331 ids[0] == WebAutofillClient::MenuItemIDWarningMessage) {
303 332 v.clear();
304 // Only include "Autofill Options" special menu item if we have Autofill 333 l.clear();
305 // items, identified by |unique_ids| having at least one valid value. 334 i.clear();
306 bool has_autofill_item = false; 335 ids.clear();
307 for (size_t i = 0; i < ids.size(); ++i) {
308 if (ids[i] > 0) {
309 has_autofill_item = true;
310 break;
311 }
312 }
313
314 // The form has been auto-filled, so give the user the chance to clear the
315 // form. Append the 'Clear form' menu item.
316 if (has_autofill_item &&
317 FormWithElementIsAutofilled(autofill_query_element_)) {
318 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
319 l.push_back(string16());
320 i.push_back(string16());
321 ids.push_back(0);
322 suggestions_clear_index_ = v.size() - 1;
323 separator_index = v.size() - 1;
324 } 336 }
325 337
326 if (has_autofill_item) { 338 if (has_autofill_item) {
339 v.push_back(string16());
340 l.push_back(string16());
341 i.push_back(string16());
342 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
343
344 if (FormWithElementIsAutofilled(element_)) {
345 // The form has been auto-filled, so give the user the chance to clear the
346 // form. Append the 'Clear form' menu item.
347 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM));
348 l.push_back(string16());
349 i.push_back(string16());
350 ids.push_back(WebAutofillClient::MenuItemIDClearForm);
351 }
352
327 // Append the 'Chrome Autofill options' menu item; 353 // Append the 'Chrome Autofill options' menu item;
328 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); 354 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP));
329 l.push_back(string16()); 355 l.push_back(string16());
330 i.push_back(string16()); 356 i.push_back(string16());
331 ids.push_back(0); 357 ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
332 suggestions_options_index_ = v.size() - 1; 358 }
333 separator_index = values.size(); 359
360 if (!element_.isNull() && element_.isFocusable())
Ilya Sherman 2012/04/16 21:45:49 nit: Can these checks be moved to the very top of
keishi 2012/04/17 09:15:10 Done.
361 CombineDataListEntriesAndShow(element_, v, l, i, ids, has_autofill_item);
362 }
363
364 void AutofillAgent::CombineDataListEntriesAndShow(
365 const WebKit::WebInputElement& element,
366 const std::vector<string16>& values,
367 const std::vector<string16>& labels,
368 const std::vector<string16>& icons,
369 const std::vector<int>& item_ids,
370 bool has_autofill_item) {
371 std::vector<string16> v;
372 std::vector<string16> l;
373 std::vector<string16> i;
374 std::vector<int> ids;
375
376 AppendDataListSuggestions(element, &v, &l, &i, &ids);
377
378 // If there are both <datalist> items and Autofill suggestions, add a
379 // separator between them.
380 if (!v.empty() && !values.empty()) {
381 v.push_back(string16());
382 l.push_back(string16());
383 i.push_back(string16());
384 ids.push_back(WebAutofillClient::MenuItemIDSeparator);
385 }
386
387 v.insert(v.end(), values.begin(), values.end());
388 l.insert(l.end(), labels.begin(), labels.end());
389 i.insert(i.end(), icons.begin(), icons.end());
390 ids.insert(ids.end(), item_ids.begin(), item_ids.end());
Ilya Sherman 2012/04/16 21:45:49 nit: Please add a brief comment for this block, so
keishi 2012/04/17 09:15:10 Done.
391
392 WebKit::WebView* web_view = render_view()->GetWebView();
393 if (!web_view)
394 return;
395
396 if (v.empty()) {
397 // No suggestions, any popup currently showing is obsolete.
398 web_view->hidePopups();
399 return;
334 } 400 }
335 401
336 // Send to WebKit for display. 402 // Send to WebKit for display.
337 if (!v.empty() && !autofill_query_element_.isNull() && 403 web_view->applyAutofillSuggestions(element, v, l, i, ids);
338 autofill_query_element_.isFocusable()) {
339 web_view->applyAutofillSuggestions(
340 autofill_query_element_, v, l, i, ids, separator_index);
341 }
342 404
343 Send(new AutofillHostMsg_DidShowAutofillSuggestions( 405 Send(new AutofillHostMsg_DidShowAutofillSuggestions(
344 routing_id(), 406 routing_id(),
345 has_autofill_item && !has_shown_autofill_popup_for_current_edit_)); 407 has_autofill_item && !has_shown_autofill_popup_for_current_edit_));
346 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; 408 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item;
347 } 409 }
348 410
349 void AutofillAgent::OnFormDataFilled(int query_id, 411 void AutofillAgent::OnFormDataFilled(int query_id,
350 const webkit::forms::FormData& form) { 412 const webkit::forms::FormData& form) {
351 if (!render_view()->GetWebView() || query_id != autofill_query_id_) 413 if (!render_view()->GetWebView() || query_id != autofill_query_id_)
352 return; 414 return;
353 415
354 was_query_node_autofilled_ = autofill_query_element_.isAutofilled(); 416 was_query_node_autofilled_ = element_.isAutofilled();
355 417
356 switch (autofill_action_) { 418 switch (autofill_action_) {
357 case AUTOFILL_FILL: 419 case AUTOFILL_FILL:
358 FillForm(form, autofill_query_element_); 420 FillForm(form, element_);
359 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(), 421 Send(new AutofillHostMsg_DidFillAutofillFormData(routing_id(),
360 base::TimeTicks::Now())); 422 base::TimeTicks::Now()));
361 break; 423 break;
362 case AUTOFILL_PREVIEW: 424 case AUTOFILL_PREVIEW:
363 didClearAutofillSelection(autofill_query_element_); 425 didClearAutofillSelection(element_);
364 426
365 PreviewForm(form, autofill_query_element_); 427 PreviewForm(form, element_);
366 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id())); 428 Send(new AutofillHostMsg_DidPreviewAutofillFormData(routing_id()));
367 break; 429 break;
368 default: 430 default:
369 NOTREACHED(); 431 NOTREACHED();
370 } 432 }
371 autofill_action_ = AUTOFILL_NONE; 433 autofill_action_ = AUTOFILL_NONE;
372 } 434 }
373 435
374 void AutofillAgent::OnFieldTypePredictionsAvailable( 436 void AutofillAgent::OnFieldTypePredictionsAvailable(
375 const std::vector<FormDataPredictions>& forms) { 437 const std::vector<FormDataPredictions>& forms) {
376 for (size_t i = 0; i < forms.size(); ++i) { 438 for (size_t i = 0; i < forms.size(); ++i) {
377 form_cache_.ShowPredictions(forms[i]); 439 form_cache_.ShowPredictions(forms[i]);
378 } 440 }
379 } 441 }
380 442
381 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) { 443 void AutofillAgent::OnSelectAutofillSuggestionAtIndex(int listIndex) {
382 NOTIMPLEMENTED(); 444 NOTIMPLEMENTED();
383 // TODO(jrg): enable once changes land in WebKit 445 // TODO(jrg): enable once changes land in WebKit
384 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex); 446 // render_view()->webview()->selectAutofillSuggestionAtIndex(listIndex);
385 } 447 }
386 448
387 void AutofillAgent::OnSetAutofillActionFill() { 449 void AutofillAgent::OnSetAutofillActionFill() {
388 autofill_action_ = AUTOFILL_FILL; 450 autofill_action_ = AUTOFILL_FILL;
389 } 451 }
390 452
391 void AutofillAgent::OnClearForm() { 453 void AutofillAgent::OnClearForm() {
392 form_cache_.ClearFormWithElement(autofill_query_element_); 454 form_cache_.ClearFormWithElement(element_);
393 } 455 }
394 456
395 void AutofillAgent::OnSetAutofillActionPreview() { 457 void AutofillAgent::OnSetAutofillActionPreview() {
396 autofill_action_ = AUTOFILL_PREVIEW; 458 autofill_action_ = AUTOFILL_PREVIEW;
397 } 459 }
398 460
399 void AutofillAgent::OnClearPreviewedForm() { 461 void AutofillAgent::OnClearPreviewedForm() {
400 didClearAutofillSelection(autofill_query_element_); 462 didClearAutofillSelection(element_);
401 } 463 }
402 464
403 void AutofillAgent::OnSetNodeText(const string16& value) { 465 void AutofillAgent::OnSetNodeText(const string16& value) {
404 SetNodeText(value, &autofill_query_element_); 466 SetNodeText(value, &element_);
405 } 467 }
406 468
407 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) { 469 void AutofillAgent::OnAcceptPasswordAutofillSuggestion(const string16& value) {
408 // We need to make sure this is handled here because the browser process 470 // We need to make sure this is handled here because the browser process
409 // skipped it handling because it believed it would be handled here. If it 471 // skipped it handling because it believed it would be handled here. If it
410 // isn't handled here then the browser logic needs to be updated. 472 // isn't handled here then the browser logic needs to be updated.
411 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion( 473 bool handled = password_autofill_manager_->DidAcceptAutofillSuggestion(
412 autofill_query_element_, 474 element_,
413 value); 475 value);
414 DCHECK(handled); 476 DCHECK(handled);
415 } 477 }
416 478
417 void AutofillAgent::ShowSuggestions(const WebInputElement& element, 479 void AutofillAgent::ShowSuggestions(const WebInputElement& element,
418 bool autofill_on_empty_values, 480 bool autofill_on_empty_values,
419 bool requires_caret_at_end, 481 bool requires_caret_at_end,
420 bool display_warning_if_disabled) { 482 bool display_warning_if_disabled) {
421 // If autocomplete is disabled at the form level, then we might want to show 483 if (!element.isEnabled() || element.isReadOnly() || !element.isTextField() ||
422 // a warning in place of suggestions. However, if autocomplete is disabled 484 element.isPasswordField() || !element.suggestedValue().isEmpty())
423 // specifically for this field, we never want to show a warning. Otherwise,
424 // we might interfere with custom popups (e.g. search suggestions) used by
425 // the website.
426 const WebFormElement form = element.form();
427 if (!element.isEnabled() || element.isReadOnly() ||
428 (!element.autoComplete() && (form.isNull() || form.autoComplete())) ||
429 !element.isTextField() || element.isPasswordField() ||
430 !element.suggestedValue().isEmpty())
431 return;
432
433 // If the field has no name, then we won't have values.
434 if (element.nameForAutofill().isEmpty())
435 return; 485 return;
436 486
437 // Don't attempt to autofill with values that are too large or if filling 487 // Don't attempt to autofill with values that are too large or if filling
438 // criteria are not met. 488 // criteria are not met.
439 WebString value = element.value(); 489 WebString value = element.value();
440 if (value.length() > kMaximumTextSizeForAutofill || 490 if (value.length() > kMaximumTextSizeForAutofill ||
441 (!autofill_on_empty_values && value.isEmpty()) || 491 (!autofill_on_empty_values && value.isEmpty()) ||
442 (requires_caret_at_end && 492 (requires_caret_at_end &&
443 (element.selectionStart() != element.selectionEnd() || 493 (element.selectionStart() != element.selectionEnd() ||
444 element.selectionEnd() != static_cast<int>(value.length())))) { 494 element.selectionEnd() != static_cast<int>(value.length())))) {
445 // Any popup currently showing is obsolete. 495 // Any popup currently showing is obsolete.
446 WebKit::WebView* web_view = render_view()->GetWebView(); 496 WebKit::WebView* web_view = render_view()->GetWebView();
447 if (web_view) 497 if (web_view)
448 web_view->hidePopups(); 498 web_view->hidePopups();
449 499
450 return; 500 return;
451 } 501 }
452 502
503 element_ = element;
504
505 // If autocomplete is disabled at the form level, then we might want to show
506 // a warning in place of suggestions. However, if autocomplete is disabled
507 // specifically for this field, we never want to show a warning. Otherwise,
508 // we might interfere with custom popups (e.g. search suggestions) used by
509 // the website. Also, if the field has no name, then we won't have values.
510 const WebFormElement form = element.form();
511 if ((!element.autoComplete() && (form.isNull() || form.autoComplete())) ||
512 element.nameForAutofill().isEmpty()) {
513 CombineDataListEntriesAndShow(element, std::vector<string16>(),
514 std::vector<string16>(),
515 std::vector<string16>(),
516 std::vector<int>(), false);
517 return;
518 }
519
453 QueryAutofillSuggestions(element, display_warning_if_disabled); 520 QueryAutofillSuggestions(element, display_warning_if_disabled);
454 } 521 }
455 522
456 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element, 523 void AutofillAgent::QueryAutofillSuggestions(const WebInputElement& element,
457 bool display_warning_if_disabled) { 524 bool display_warning_if_disabled) {
458 static int query_counter = 0; 525 static int query_counter = 0;
459 autofill_query_id_ = query_counter++; 526 autofill_query_id_ = query_counter++;
460 autofill_query_element_ = element;
461 display_warning_if_disabled_ = display_warning_if_disabled; 527 display_warning_if_disabled_ = display_warning_if_disabled;
462 528
463 webkit::forms::FormData form; 529 webkit::forms::FormData form;
464 webkit::forms::FormField field; 530 webkit::forms::FormField field;
465 if (!FindFormAndFieldForInputElement(element, &form, &field, 531 if (!FindFormAndFieldForInputElement(element, &form, &field,
466 REQUIRE_AUTOCOMPLETE)) { 532 REQUIRE_AUTOCOMPLETE)) {
467 // If we didn't find the cached form, at least let autocomplete have a shot 533 // If we didn't find the cached form, at least let autocomplete have a shot
468 // at providing suggestions. 534 // at providing suggestions.
469 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field); 535 WebFormControlElementToFormField(element, EXTRACT_VALUE, &field);
470 } 536 }
471 537
472 gfx::Rect bounding_box(autofill_query_element_.boundsInViewportSpace()); 538 gfx::Rect bounding_box(element_.boundsInViewportSpace());
473 539
474 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(), 540 Send(new AutofillHostMsg_QueryFormFieldAutofill(routing_id(),
475 autofill_query_id_, 541 autofill_query_id_,
476 form, 542 form,
477 field, 543 field,
478 bounding_box, 544 bounding_box,
479 display_warning_if_disabled)); 545 display_warning_if_disabled));
480 } 546 }
481 547
482 void AutofillAgent::FillAutofillFormData(const WebNode& node, 548 void AutofillAgent::FillAutofillFormData(const WebNode& node,
483 int unique_id, 549 int unique_id,
484 AutofillAction action) { 550 AutofillAction action) {
551 DCHECK_GT(unique_id, 0);
552
485 static int query_counter = 0; 553 static int query_counter = 0;
486 autofill_query_id_ = query_counter++; 554 autofill_query_id_ = query_counter++;
487 555
488 webkit::forms::FormData form; 556 webkit::forms::FormData form;
489 webkit::forms::FormField field; 557 webkit::forms::FormField field;
490 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form, 558 if (!FindFormAndFieldForInputElement(node.toConst<WebInputElement>(), &form,
491 &field, REQUIRE_AUTOCOMPLETE)) { 559 &field, REQUIRE_AUTOCOMPLETE)) {
492 return; 560 return;
493 } 561 }
494 562
495 autofill_action_ = action; 563 autofill_action_ = action;
496 Send(new AutofillHostMsg_FillAutofillFormData( 564 Send(new AutofillHostMsg_FillAutofillFormData(
497 routing_id(), autofill_query_id_, form, field, unique_id)); 565 routing_id(), autofill_query_id_, form, field, unique_id));
498 } 566 }
499 567
500 void AutofillAgent::SetNodeText(const string16& value, 568 void AutofillAgent::SetNodeText(const string16& value,
501 WebKit::WebInputElement* node) { 569 WebKit::WebInputElement* node) {
502 string16 substring = value; 570 string16 substring = value;
503 substring = substring.substr(0, node->maxLength()); 571 substring = substring.substr(0, node->maxLength());
504 572
505 node->setValue(substring, true); 573 node->setValue(substring, true);
506 } 574 }
507 575
508 } // namespace autofill 576 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698