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

Side by Side Diff: components/autofill/content/renderer/password_autofill_agent.cc

Issue 597983003: Refactor PasswordAutofillAgent: methods to functions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes as per review comments. Created 6 years, 2 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
« no previous file with comments | « components/autofill/content/renderer/password_autofill_agent.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 "components/autofill/content/renderer/password_autofill_agent.h" 5 #include "components/autofill/content/renderer/password_autofill_agent.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 const blink::WebFormElement& form) { 218 const blink::WebFormElement& form) {
219 logger->LogHTMLForm(message_id, 219 logger->LogHTMLForm(message_id,
220 form.name().utf8(), 220 form.name().utf8(),
221 GURL(form.action().utf8())); 221 GURL(form.action().utf8()));
222 } 222 }
223 223
224 bool FillDataContainsUsername(const PasswordFormFillData& fill_data) { 224 bool FillDataContainsUsername(const PasswordFormFillData& fill_data) {
225 return !fill_data.basic_data.fields[0].name.empty(); 225 return !fill_data.basic_data.fields[0].name.empty();
226 } 226 }
227 227
228 // This helper function will fill the |suggestions| and |realms| form the
Garrett Casto 2014/09/30 00:16:41 "fill the |suggestions| and |realms| form the fill
Deepak 2014/09/30 04:07:21 Done.
vabr (Chromium) 2014/09/30 08:19:41 The form->from typo seems to be still present. :)
229 // fill_data based on the |input| and return true when suggestion
vabr (Chromium) 2014/09/29 15:19:09 typo: suggestion -> suggestions
Garrett Casto 2014/09/30 00:16:40 Just "|input|" not "the |input|". "Input" is also
Deepak 2014/09/30 04:07:21 Done.
Deepak 2014/09/30 04:07:21 Done.
vabr (Chromium) 2014/09/30 08:19:41 I believe Garrett meant renaming the argument |inp
230 // get filled from usernamescollection else return false.
vabr (Chromium) 2014/09/29 15:19:09 nit: Not clear what is meant by the username colle
Garrett Casto 2014/09/30 00:16:40 From |fill_data| isn't specific, all suggestions a
Deepak 2014/09/30 04:07:21 I have made this like: return true when suggestion
Deepak 2014/09/30 04:07:21 Done.
vabr (Chromium) 2014/09/30 08:19:41 2 nits: (1) Replace other_possible_usernames with
231 bool GetSuggestions(const PasswordFormFillData& fill_data,
232 const base::string16& input,
233 std::vector<base::string16>* suggestions,
234 std::vector<base::string16>* realms,
235 bool show_all) {
236 bool usernames_shown = false;
237 if (show_all ||
238 StartsWith(fill_data.basic_data.fields[0].value, input, false)) {
239 suggestions->push_back(fill_data.basic_data.fields[0].value);
240 realms->push_back(base::UTF8ToUTF16(fill_data.preferred_realm));
241 }
242
243 for (PasswordFormFillData::LoginCollection::const_iterator iter =
244 fill_data.additional_logins.begin();
245 iter != fill_data.additional_logins.end();
246 ++iter) {
247 if (show_all || StartsWith(iter->first, input, false)) {
248 suggestions->push_back(iter->first);
249 realms->push_back(base::UTF8ToUTF16(iter->second.realm));
250 }
251 }
252
253 for (PasswordFormFillData::UsernamesCollection::const_iterator iter =
254 fill_data.other_possible_usernames.begin();
255 iter != fill_data.other_possible_usernames.end();
256 ++iter) {
257 for (size_t i = 0; i < iter->second.size(); ++i) {
258 if (show_all || StartsWith(iter->second[i], input, false)) {
259 usernames_shown = true;
260 suggestions->push_back(iter->second[i]);
261 realms->push_back(base::UTF8ToUTF16(iter->first.realm));
262 }
263 }
264 }
265 return usernames_shown;
266 }
267
268 // This function attempts to set the |username_element]
269 // and |password_element| values with the |fill_data|
Garrett Casto 2014/09/30 00:16:41 Line wrapping too early. You should wrap closer to
Deepak 2014/09/30 04:07:22 Acknowledged.
270 // returns true when username get selected from possible usernames
vabr (Chromium) 2014/09/29 15:19:10 typo: "username get" -> "a username gets"
Garrett Casto 2014/09/30 00:16:40 I think there is supposed to be a period and "ret
Deepak 2014/09/30 04:07:21 Done.
Deepak 2014/09/30 04:07:22 Done.
271 // else returns false.it will also register the password element when
vabr (Chromium) 2014/09/29 15:19:10 nit: Specify that it will register the password el
vabr (Chromium) 2014/09/29 15:19:10 nit: "false.it" -> "false. It"
Deepak 2014/09/30 04:07:21 Done.
272 // its value set.
273 bool FillUserNameAndPassword(
274 blink::WebInputElement* username_element,
275 blink::WebInputElement* password_element,
276 const PasswordFormFillData& fill_data,
277 bool exact_username_match,
278 bool set_selection,
279 base::Callback<void(blink::WebInputElement*)> gatekeeper_callback) {
280 bool username_selected = false;
Garrett Casto 2014/09/30 00:16:41 |username_selected| is too vague. It's not just if
Deepak 2014/09/30 04:07:21 I have made this "possible_username_selected" to m
vabr (Chromium) 2014/09/30 08:19:41 I would actually suggest |other_possible_username_
vabr (Chromium) 2014/09/30 13:54:53 You seem to have missed this comment as well.
281 // Don't fill username if password can't be set.
282 if (!IsElementAutocompletable(*password_element))
283 return false;
284
285 base::string16 current_username;
286 if (!username_element->isNull()) {
287 current_username = username_element->value();
288 }
289
290 // username and password will contain the match found if any.
291 base::string16 username;
292 base::string16 password;
293
294 // Look for any suitable matches to current field text.
295 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value,
296 current_username,
297 exact_username_match)) {
298 username = fill_data.basic_data.fields[0].value;
299 password = fill_data.basic_data.fields[1].value;
300 } else {
301 // Scan additional logins for a match.
302 PasswordFormFillData::LoginCollection::const_iterator iter;
303 for (iter = fill_data.additional_logins.begin();
304 iter != fill_data.additional_logins.end();
305 ++iter) {
306 if (DoUsernamesMatch(
307 iter->first, current_username, exact_username_match)) {
308 username = iter->first;
309 password = iter->second.password;
310 break;
311 }
312 }
313
314 // Check possible usernames.
315 if (username.empty() && password.empty()) {
316 for (PasswordFormFillData::UsernamesCollection::const_iterator iter =
317 fill_data.other_possible_usernames.begin();
318 iter != fill_data.other_possible_usernames.end();
319 ++iter) {
320 for (size_t i = 0; i < iter->second.size(); ++i) {
321 if (DoUsernamesMatch(
322 iter->second[i], current_username, exact_username_match)) {
323 username_selected = true;
324 username = iter->second[i];
325 password = iter->first.password;
326 break;
327 }
328 }
329 if (!username.empty() && !password.empty())
330 break;
331 }
332 }
333 }
334 if (password.empty())
335 return username_selected; // No match was found.
336
337 // TODO(tkent): Check maxlength and pattern for both username and password
338 // fields.
339
340 // Input matches the username, fill in required values.
341 if (!username_element->isNull() &&
342 IsElementAutocompletable(*username_element)) {
343 username_element->setValue(username, true);
344 username_element->setAutofilled(true);
345
346 if (set_selection) {
347 username_element->setSelectionRange(current_username.length(),
348 username.length());
349 }
350 } else if (current_username != username) {
351 // If the username can't be filled and it doesn't match a saved password
352 // as is, don't autofill a password.
353 return username_selected;
354 }
355
356 // Wait to fill in the password until a user gesture occurs. This is to make
357 // sure that we do not fill in the DOM with a password until we believe the
358 // user is intentionally interacting with the page.
359 password_element->setSuggestedValue(password);
360 gatekeeper_callback.Run(password_element);
361
362 password_element->setAutofilled(true);
363 return username_selected;
364 }
365
366 // Attempts to fill |username_element| and |password_element| with the
Garrett Casto 2014/09/30 00:16:40 As above, be consistent with pronouns. It's more c
367 // |fill_data|. Will use the data corresponding to the preferred username,
368 // unless the |username_element| already has a value set. In that case,
369 // attempts to fill the password matching the already filled username, if
370 // such a password exists.gatekeeper_ptr will register the password element.
vabr (Chromium) 2014/09/29 15:19:09 nit: gatekeeper_ptr sounds like a typo. Maybe say
vabr (Chromium) 2014/09/29 15:19:10 nit: space after a full-stop
Deepak 2014/09/30 04:07:21 Done.
Deepak 2014/09/30 04:07:21 Done.
371 // returns true when username is selected else return false.
Garrett Casto 2014/09/30 00:16:41 Username is selected is misleading, it's just othe
Deepak 2014/09/30 04:07:21 Done.
372 bool FillFormOnPasswordRecieved(
373 const PasswordFormFillData& fill_data,
374 blink::WebInputElement username_element,
375 blink::WebInputElement password_element,
376 base::Callback<void(blink::WebInputElement*)> gatekeeper_callback) {
377 // Do not fill if the password field is in an iframe.
378 DCHECK(password_element.document().frame());
379 if (password_element.document().frame()->parent())
380 return false;
381
382 bool form_contains_username_field = FillDataContainsUsername(fill_data);
383 if (!ShouldIgnoreAutocompleteOffForPasswordFields() &&
384 form_contains_username_field && !username_element.form().autoComplete())
385 return false;
386
387 // If we can't modify the password, don't try to set the username
388 if (!IsElementAutocompletable(password_element))
389 return false;
390
391 // Try to set the username to the preferred name, but only if the field
392 // can be set and isn't prefilled.
393 if (form_contains_username_field &&
394 IsElementAutocompletable(username_element) &&
395 username_element.value().isEmpty()) {
396 // TODO(tkent): Check maxlength and pattern.
397 username_element.setValue(fill_data.basic_data.fields[0].value, true);
398 }
399
400 // Fill if we have an exact match for the username. Note that this sets
401 // username to autofilled.
402 return FillUserNameAndPassword(&username_element,
403 &password_element,
404 fill_data,
405 true /* exact_username_match */,
406 false /* set_selection */,
407 gatekeeper_callback);
408 }
409
228 } // namespace 410 } // namespace
229 411
230 //////////////////////////////////////////////////////////////////////////////// 412 ////////////////////////////////////////////////////////////////////////////////
231 // PasswordAutofillAgent, public: 413 // PasswordAutofillAgent, public:
232 414
233 PasswordAutofillAgent::PasswordAutofillAgent(content::RenderView* render_view) 415 PasswordAutofillAgent::PasswordAutofillAgent(content::RenderView* render_view)
234 : content::RenderViewObserver(render_view), 416 : content::RenderViewObserver(render_view),
235 usernames_usage_(NOTHING_TO_AUTOFILL), 417 usernames_usage_(NOTHING_TO_AUTOFILL),
236 web_view_(render_view->GetWebView()), 418 web_view_(render_view->GetWebView()),
237 logging_state_active_(false), 419 logging_state_active_(false),
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 return false; 485 return false;
304 486
305 blink::WebInputElement password = password_info.password_field; 487 blink::WebInputElement password = password_info.password_field;
306 if (!IsElementEditable(password)) 488 if (!IsElementEditable(password))
307 return false; 489 return false;
308 490
309 blink::WebInputElement username = element; // We need a non-const. 491 blink::WebInputElement username = element; // We need a non-const.
310 492
311 // Do not set selection when ending an editing session, otherwise it can 493 // Do not set selection when ending an editing session, otherwise it can
312 // mess with focus. 494 // mess with focus.
313 FillUserNameAndPassword(&username, 495 if (FillUserNameAndPassword(
314 &password, 496 &username,
315 fill_data, 497 &password,
316 true /* exact_username_match */, 498 fill_data,
317 false /* set_selection */); 499 true /* exact_username_match */,
500 false /* set_selection */,
501 base::Bind(&PasswordValueGatekeeper::RegisterElement,
502 base::Unretained(&gatekeeper_))))
503 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
vabr (Chromium) 2014/09/29 15:19:10 nit: Please add curly braces around the statement,
Deepak 2014/09/30 04:07:21 Done.
318 return true; 504 return true;
319 } 505 }
320 506
321 bool PasswordAutofillAgent::TextDidChangeInTextField( 507 bool PasswordAutofillAgent::TextDidChangeInTextField(
322 const blink::WebInputElement& element) { 508 const blink::WebInputElement& element) {
323 // TODO(vabr): Get a mutable argument instead. http://crbug.com/397083 509 // TODO(vabr): Get a mutable argument instead. http://crbug.com/397083
324 blink::WebInputElement mutable_element = element; // We need a non-const. 510 blink::WebInputElement mutable_element = element; // We need a non-const.
325 511
326 if (element.isPasswordField()) { 512 if (element.isPasswordField()) {
327 // Some login forms have event handlers that put a hash of the password into 513 // Some login forms have event handlers that put a hash of the password into
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 if (form_data.basic_data.fields[1].name.empty()) 1008 if (form_data.basic_data.fields[1].name.empty())
823 break; 1009 break;
824 1010
825 // Get pointer to password element. (We currently only support single 1011 // Get pointer to password element. (We currently only support single
826 // password forms). 1012 // password forms).
827 password_element = 1013 password_element =
828 form_elements->input_elements[form_data.basic_data.fields[1].name]; 1014 form_elements->input_elements[form_data.basic_data.fields[1].name];
829 1015
830 // If wait_for_username is true, we don't want to initially fill the form 1016 // If wait_for_username is true, we don't want to initially fill the form
831 // until the user types in a valid username. 1017 // until the user types in a valid username.
832 if (!form_data.wait_for_username) 1018 if (!form_data.wait_for_username &&
833 FillFormOnPasswordRecieved(form_data, username_element, password_element); 1019 FillFormOnPasswordRecieved(
1020 form_data,
1021 username_element,
1022 password_element,
1023 base::Bind(&PasswordValueGatekeeper::RegisterElement,
1024 base::Unretained(&gatekeeper_))))
1025 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
834 1026
835 // We might have already filled this form if there are two <form> elements 1027 // We might have already filled this form if there are two <form> elements
836 // with identical markup. 1028 // with identical markup.
837 if (login_to_password_info_.find(username_element) != 1029 if (login_to_password_info_.find(username_element) !=
838 login_to_password_info_.end()) 1030 login_to_password_info_.end())
839 continue; 1031 continue;
840 1032
841 PasswordInfo password_info; 1033 PasswordInfo password_info;
842 password_info.fill_data = form_data; 1034 password_info.fill_data = form_data;
843 password_info.password_field = password_element; 1035 password_info.password_field = password_element;
(...skipping 16 matching lines...) Expand all
860 logging_state_active_ = active; 1052 logging_state_active_ = active;
861 } 1053 }
862 1054
863 //////////////////////////////////////////////////////////////////////////////// 1055 ////////////////////////////////////////////////////////////////////////////////
864 // PasswordAutofillAgent, private: 1056 // PasswordAutofillAgent, private:
865 1057
866 PasswordAutofillAgent::PasswordInfo::PasswordInfo() 1058 PasswordAutofillAgent::PasswordInfo::PasswordInfo()
867 : backspace_pressed_last(false), password_was_edited_last(false) { 1059 : backspace_pressed_last(false), password_was_edited_last(false) {
868 } 1060 }
869 1061
870 void PasswordAutofillAgent::GetSuggestions(
871 const PasswordFormFillData& fill_data,
872 const base::string16& input,
873 std::vector<base::string16>* suggestions,
874 std::vector<base::string16>* realms,
875 bool show_all) {
876 if (show_all ||
877 StartsWith(fill_data.basic_data.fields[0].value, input, false)) {
878 suggestions->push_back(fill_data.basic_data.fields[0].value);
879 realms->push_back(base::UTF8ToUTF16(fill_data.preferred_realm));
880 }
881
882 for (PasswordFormFillData::LoginCollection::const_iterator iter =
883 fill_data.additional_logins.begin();
884 iter != fill_data.additional_logins.end();
885 ++iter) {
886 if (show_all || StartsWith(iter->first, input, false)) {
887 suggestions->push_back(iter->first);
888 realms->push_back(base::UTF8ToUTF16(iter->second.realm));
889 }
890 }
891
892 for (PasswordFormFillData::UsernamesCollection::const_iterator iter =
893 fill_data.other_possible_usernames.begin();
894 iter != fill_data.other_possible_usernames.end();
895 ++iter) {
896 for (size_t i = 0; i < iter->second.size(); ++i) {
897 if (show_all || StartsWith(iter->second[i], input, false)) {
898 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SHOWN;
899 suggestions->push_back(iter->second[i]);
900 realms->push_back(base::UTF8ToUTF16(iter->first.realm));
901 }
902 }
903 }
904 }
905
906 bool PasswordAutofillAgent::ShowSuggestionPopup( 1062 bool PasswordAutofillAgent::ShowSuggestionPopup(
907 const PasswordFormFillData& fill_data, 1063 const PasswordFormFillData& fill_data,
908 const blink::WebInputElement& user_input, 1064 const blink::WebInputElement& user_input,
909 bool show_all) { 1065 bool show_all) {
910 blink::WebFrame* frame = user_input.document().frame(); 1066 blink::WebFrame* frame = user_input.document().frame();
911 if (!frame) 1067 if (!frame)
912 return false; 1068 return false;
913 1069
914 blink::WebView* webview = frame->view(); 1070 blink::WebView* webview = frame->view();
915 if (!webview) 1071 if (!webview)
916 return false; 1072 return false;
917 1073
918 std::vector<base::string16> suggestions; 1074 std::vector<base::string16> suggestions;
919 std::vector<base::string16> realms; 1075 std::vector<base::string16> realms;
920 GetSuggestions( 1076 if (GetSuggestions(
921 fill_data, user_input.value(), &suggestions, &realms, show_all); 1077 fill_data, user_input.value(), &suggestions, &realms, show_all))
1078 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SHOWN;
1079
922 DCHECK_EQ(suggestions.size(), realms.size()); 1080 DCHECK_EQ(suggestions.size(), realms.size());
923 1081
924 FormData form; 1082 FormData form;
925 FormFieldData field; 1083 FormFieldData field;
926 FindFormAndFieldForFormControlElement( 1084 FindFormAndFieldForFormControlElement(
927 user_input, &form, &field, REQUIRE_NONE); 1085 user_input, &form, &field, REQUIRE_NONE);
928 1086
929 blink::WebInputElement selected_element = user_input; 1087 blink::WebInputElement selected_element = user_input;
930 gfx::Rect bounding_box(selected_element.boundsInViewportSpace()); 1088 gfx::Rect bounding_box(selected_element.boundsInViewportSpace());
931 1089
932 float scale = web_view_->pageScaleFactor(); 1090 float scale = web_view_->pageScaleFactor();
933 gfx::RectF bounding_box_scaled(bounding_box.x() * scale, 1091 gfx::RectF bounding_box_scaled(bounding_box.x() * scale,
934 bounding_box.y() * scale, 1092 bounding_box.y() * scale,
935 bounding_box.width() * scale, 1093 bounding_box.width() * scale,
936 bounding_box.height() * scale); 1094 bounding_box.height() * scale);
937 Send(new AutofillHostMsg_ShowPasswordSuggestions( 1095 Send(new AutofillHostMsg_ShowPasswordSuggestions(
938 routing_id(), field, bounding_box_scaled, suggestions, realms)); 1096 routing_id(), field, bounding_box_scaled, suggestions, realms));
939 return !suggestions.empty(); 1097 return !suggestions.empty();
940 } 1098 }
941 1099
942 void PasswordAutofillAgent::FillFormOnPasswordRecieved(
943 const PasswordFormFillData& fill_data,
944 blink::WebInputElement username_element,
945 blink::WebInputElement password_element) {
946 // Do not fill if the password field is in an iframe.
947 DCHECK(password_element.document().frame());
948 if (password_element.document().frame()->parent())
949 return;
950
951 bool form_contains_username_field = FillDataContainsUsername(fill_data);
952 if (!ShouldIgnoreAutocompleteOffForPasswordFields() &&
953 form_contains_username_field && !username_element.form().autoComplete())
954 return;
955
956 // If we can't modify the password, don't try to set the username
957 if (!IsElementAutocompletable(password_element))
958 return;
959
960 // Try to set the username to the preferred name, but only if the field
961 // can be set and isn't prefilled.
962 if (form_contains_username_field &&
963 IsElementAutocompletable(username_element) &&
964 username_element.value().isEmpty()) {
965 // TODO(tkent): Check maxlength and pattern.
966 username_element.setValue(fill_data.basic_data.fields[0].value, true);
967 }
968
969 // Fill if we have an exact match for the username. Note that this sets
970 // username to autofilled.
971 FillUserNameAndPassword(&username_element,
972 &password_element,
973 fill_data,
974 true /* exact_username_match */,
975 false /* set_selection */);
976 }
977
978 bool PasswordAutofillAgent::FillUserNameAndPassword(
979 blink::WebInputElement* username_element,
980 blink::WebInputElement* password_element,
981 const PasswordFormFillData& fill_data,
982 bool exact_username_match,
983 bool set_selection) {
984 // Don't fill username if password can't be set.
985 if (!IsElementAutocompletable(*password_element))
986 return false;
987
988 base::string16 current_username;
989 if (!username_element->isNull()) {
990 current_username = username_element->value();
991 }
992
993 // username and password will contain the match found if any.
994 base::string16 username;
995 base::string16 password;
996
997 // Look for any suitable matches to current field text.
998 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value,
999 current_username,
1000 exact_username_match)) {
1001 username = fill_data.basic_data.fields[0].value;
1002 password = fill_data.basic_data.fields[1].value;
1003 } else {
1004 // Scan additional logins for a match.
1005 PasswordFormFillData::LoginCollection::const_iterator iter;
1006 for (iter = fill_data.additional_logins.begin();
1007 iter != fill_data.additional_logins.end();
1008 ++iter) {
1009 if (DoUsernamesMatch(
1010 iter->first, current_username, exact_username_match)) {
1011 username = iter->first;
1012 password = iter->second.password;
1013 break;
1014 }
1015 }
1016
1017 // Check possible usernames.
1018 if (username.empty() && password.empty()) {
1019 for (PasswordFormFillData::UsernamesCollection::const_iterator iter =
1020 fill_data.other_possible_usernames.begin();
1021 iter != fill_data.other_possible_usernames.end();
1022 ++iter) {
1023 for (size_t i = 0; i < iter->second.size(); ++i) {
1024 if (DoUsernamesMatch(
1025 iter->second[i], current_username, exact_username_match)) {
1026 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
1027 username = iter->second[i];
1028 password = iter->first.password;
1029 break;
1030 }
1031 }
1032 if (!username.empty() && !password.empty())
1033 break;
1034 }
1035 }
1036 }
1037 if (password.empty())
1038 return false; // No match was found.
1039
1040 // TODO(tkent): Check maxlength and pattern for both username and password
1041 // fields.
1042
1043 // Input matches the username, fill in required values.
1044 if (!username_element->isNull() &&
1045 IsElementAutocompletable(*username_element)) {
1046 username_element->setValue(username, true);
1047 username_element->setAutofilled(true);
1048
1049 if (set_selection) {
1050 username_element->setSelectionRange(current_username.length(),
1051 username.length());
1052 }
1053 } else if (current_username != username) {
1054 // If the username can't be filled and it doesn't match a saved password
1055 // as is, don't autofill a password.
1056 return false;
1057 }
1058
1059 // Wait to fill in the password until a user gesture occurs. This is to make
1060 // sure that we do not fill in the DOM with a password until we believe the
1061 // user is intentionally interacting with the page.
1062 password_element->setSuggestedValue(password);
1063 gatekeeper_.RegisterElement(password_element);
1064
1065 password_element->setAutofilled(true);
1066 return true;
1067 }
1068
1069 void PasswordAutofillAgent::PerformInlineAutocomplete( 1100 void PasswordAutofillAgent::PerformInlineAutocomplete(
1070 const blink::WebInputElement& username_input, 1101 const blink::WebInputElement& username_input,
1071 const blink::WebInputElement& password_input, 1102 const blink::WebInputElement& password_input,
1072 const PasswordFormFillData& fill_data) { 1103 const PasswordFormFillData& fill_data) {
1073 DCHECK(!fill_data.wait_for_username); 1104 DCHECK(!fill_data.wait_for_username);
1074 1105
1075 // We need non-const versions of the username and password inputs. 1106 // We need non-const versions of the username and password inputs.
1076 blink::WebInputElement username = username_input; 1107 blink::WebInputElement username = username_input;
1077 blink::WebInputElement password = password_input; 1108 blink::WebInputElement password = password_input;
1078 1109
1079 // Don't inline autocomplete if the caret is not at the end. 1110 // Don't inline autocomplete if the caret is not at the end.
1080 // TODO(jcivelli): is there a better way to test the caret location? 1111 // TODO(jcivelli): is there a better way to test the caret location?
1081 if (username.selectionStart() != username.selectionEnd() || 1112 if (username.selectionStart() != username.selectionEnd() ||
1082 username.selectionEnd() != static_cast<int>(username.value().length())) { 1113 username.selectionEnd() != static_cast<int>(username.value().length())) {
1083 return; 1114 return;
1084 } 1115 }
1085 1116
1086 // Show the popup with the list of available usernames. 1117 // Show the popup with the list of available usernames.
1087 ShowSuggestionPopup(fill_data, username, false); 1118 ShowSuggestionPopup(fill_data, username, false);
1088 1119
1089 #if !defined(OS_ANDROID) 1120 #if !defined(OS_ANDROID)
1090 // Fill the user and password field with the most relevant match. Android 1121 // Fill the user and password field with the most relevant match. Android
1091 // only fills in the fields after the user clicks on the suggestion popup. 1122 // only fills in the fields after the user clicks on the suggestion popup.
1092 FillUserNameAndPassword(&username, 1123 if (FillUserNameAndPassword(
1093 &password, 1124 &username,
1094 fill_data, 1125 &password,
1095 false /* exact_username_match */, 1126 fill_data,
1096 true /* set_selection */); 1127 false /* exact_username_match */,
1128 true /* set_selection */,
1129 base::Bind(&PasswordValueGatekeeper::RegisterElement,
1130 base::Unretained(&gatekeeper_))))
1131 usernames_usage_ = OTHER_POSSIBLE_USERNAME_SELECTED;
1132
1097 #endif 1133 #endif
1098 } 1134 }
1099 1135
1100 void PasswordAutofillAgent::FrameClosing(const blink::WebFrame* frame) { 1136 void PasswordAutofillAgent::FrameClosing(const blink::WebFrame* frame) {
1101 for (LoginToPasswordInfoMap::iterator iter = login_to_password_info_.begin(); 1137 for (LoginToPasswordInfoMap::iterator iter = login_to_password_info_.begin();
1102 iter != login_to_password_info_.end();) { 1138 iter != login_to_password_info_.end();) {
1103 // There may not be a username field, so get the frame from the password 1139 // There may not be a username field, so get the frame from the password
1104 // field. 1140 // field.
1105 if (iter->second.password_field.document().frame() == frame) { 1141 if (iter->second.password_field.document().frame() == frame) {
1106 password_to_username_.erase(iter->second.password_field); 1142 password_to_username_.erase(iter->second.password_field);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
1162 scoped_ptr<PasswordForm> password_form(CreatePasswordForm(form)); 1198 scoped_ptr<PasswordForm> password_form(CreatePasswordForm(form));
1163 if (!password_form || (restriction == RESTRICTION_NON_EMPTY_PASSWORD && 1199 if (!password_form || (restriction == RESTRICTION_NON_EMPTY_PASSWORD &&
1164 password_form->password_value.empty() && 1200 password_form->password_value.empty() &&
1165 password_form->new_password_value.empty())) { 1201 password_form->new_password_value.empty())) {
1166 return; 1202 return;
1167 } 1203 }
1168 provisionally_saved_forms_[frame].reset(password_form.release()); 1204 provisionally_saved_forms_[frame].reset(password_form.release());
1169 } 1205 }
1170 1206
1171 } // namespace autofill 1207 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/content/renderer/password_autofill_agent.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698