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

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

Issue 9625026: Save password without an associated username. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Refine the detail of the patch Created 8 years, 9 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/password_autofill_manager.h" 5 #include "chrome/renderer/autofill/password_autofill_manager.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.h" 9 #include "base/message_loop.h"
10 #include "chrome/common/autofill_messages.h" 10 #include "chrome/common/autofill_messages.h"
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 } 187 }
188 188
189 bool DoUsernamesMatch(const string16& username1, 189 bool DoUsernamesMatch(const string16& username1,
190 const string16& username2, 190 const string16& username2,
191 bool exact_match) { 191 bool exact_match) {
192 if (exact_match) 192 if (exact_match)
193 return username1 == username2; 193 return username1 == username2;
194 return StartsWith(username1, username2, true); 194 return StartsWith(username1, username2, true);
195 } 195 }
196 196
197 // Get whether fill_data has a username or not.
198 bool HasUsernameField(const webkit::forms::PasswordFormFillData& fill_data) {
199 return fill_data.basic_data.fields.size() == 2;
200 }
201
202 // Get the username field from fill_data.
203 // If it hasn't any username, calls NOTREACHED().
204 const webkit::forms::FormField& GetUsernameField(
205 const webkit::forms::PasswordFormFillData& fill_data) {
206 DCHECK(HasUsernameField(fill_data));
207 return fill_data.basic_data.fields[1];
208 }
209
210 // Get the password field from fill_data.
211 const webkit::forms::FormField& GetPasswordField(
212 const webkit::forms::PasswordFormFillData& fill_data) {
213 DCHECK(!fill_data.basic_data.fields.empty());
214 return fill_data.basic_data.fields[0];
215 }
197 } // namespace 216 } // namespace
198 217
199 namespace autofill { 218 namespace autofill {
200 219
201 //////////////////////////////////////////////////////////////////////////////// 220 ////////////////////////////////////////////////////////////////////////////////
202 // PasswordAutofillManager, public: 221 // PasswordAutofillManager, public:
203 222
204 PasswordAutofillManager::PasswordAutofillManager( 223 PasswordAutofillManager::PasswordAutofillManager(
205 content::RenderView* render_view) 224 content::RenderView* render_view)
206 : content::RenderViewObserver(render_view), 225 : content::RenderViewObserver(render_view),
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms); 435 FindFormElements(render_view()->GetWebView(), form_data.basic_data, &forms);
417 FormElementsList::iterator iter; 436 FormElementsList::iterator iter;
418 for (iter = forms.begin(); iter != forms.end(); ++iter) { 437 for (iter = forms.begin(); iter != forms.end(); ++iter) {
419 scoped_ptr<FormElements> form_elements(*iter); 438 scoped_ptr<FormElements> form_elements(*iter);
420 439
421 // If wait_for_username is true, we don't want to initially fill the form 440 // If wait_for_username is true, we don't want to initially fill the form
422 // until the user types in a valid username. 441 // until the user types in a valid username.
423 if (!form_data.wait_for_username) 442 if (!form_data.wait_for_username)
424 FillForm(form_elements.get(), form_data.basic_data); 443 FillForm(form_elements.get(), form_data.basic_data);
425 444
426 // Attach autocomplete listener to enable selecting alternate logins. 445 // If there isn't any username, go to next iteration.
427 // First, get pointers to username element. 446 if (!HasUsernameField(form_data))
428 WebKit::WebInputElement username_element = 447 continue;
429 form_elements->input_elements[form_data.basic_data.fields[0].name];
430 448
431 // Get pointer to password element. (We currently only support single 449 // Get pointer to password element. (We currently only support single
432 // password forms). 450 // password forms).
433 WebKit::WebInputElement password_element = 451 WebKit::WebInputElement password_element =
434 form_elements->input_elements[form_data.basic_data.fields[1].name]; 452 form_elements->input_elements[GetPasswordField(form_data).name];
435 453
454 // If there is a username, set the username and login_to_password_info_.
455 // Attach autocomplete listener to enable selecting alternate logins.
456 // First, get pointers to username element.
457 WebKit::WebInputElement username_element =
458 form_elements->input_elements[GetUsernameField(form_data).name];
Ilya Sherman 2012/03/28 00:21:54 nit: I would avoid moving this block of code relat
Yumikiyo Osanai 2012/03/29 00:14:33 Oh! I understand it. It means we'd like to set the
436 // We might have already filled this form if there are two <form> elements 459 // We might have already filled this form if there are two <form> elements
437 // with identical markup. 460 // with identical markup.
438 if (login_to_password_info_.find(username_element) != 461 if (login_to_password_info_.find(username_element) !=
439 login_to_password_info_.end()) 462 login_to_password_info_.end())
Ilya Sherman 2012/03/28 00:21:54 nit: This indentation was correct previously (thou
Yumikiyo Osanai 2012/03/29 00:14:33 Done.
440 continue; 463 continue;
441
Ilya Sherman 2012/03/28 00:21:54 nit: Please preserve this line break.
Yumikiyo Osanai 2012/03/29 00:14:33 Done.
442 PasswordInfo password_info; 464 PasswordInfo password_info;
443 password_info.fill_data = form_data; 465 password_info.fill_data = form_data;
444 password_info.password_field = password_element; 466 password_info.password_field = password_element;
445 login_to_password_info_[username_element] = password_info; 467 login_to_password_info_[username_element] = password_info;
446 } 468 }
447 } 469 }
448 470
449 //////////////////////////////////////////////////////////////////////////////// 471 ////////////////////////////////////////////////////////////////////////////////
450 // PasswordAutofillManager, private: 472 // PasswordAutofillManager, private:
451 473
452 void PasswordAutofillManager::GetSuggestions( 474 void PasswordAutofillManager::GetSuggestions(
453 const webkit::forms::PasswordFormFillData& fill_data, 475 const webkit::forms::PasswordFormFillData& fill_data,
454 const string16& input, 476 const string16& input,
455 std::vector<string16>* suggestions) { 477 std::vector<string16>* suggestions) {
456 if (StartsWith(fill_data.basic_data.fields[0].value, input, false)) 478 // There are both a password and a username.
457 suggestions->push_back(fill_data.basic_data.fields[0].value); 479 if (HasUsernameField(fill_data) &&
480 StartsWith(GetUsernameField(fill_data).value, input, false)) {
481 suggestions->push_back(GetUsernameField(fill_data).value);
482 }
458 483
459 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; 484 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
460 for (iter = fill_data.additional_logins.begin(); 485 for (iter = fill_data.additional_logins.begin();
461 iter != fill_data.additional_logins.end(); ++iter) { 486 iter != fill_data.additional_logins.end(); ++iter) {
462 if (StartsWith(iter->first, input, false)) 487 if (StartsWith(iter->first, input, false))
463 suggestions->push_back(iter->first); 488 suggestions->push_back(iter->first);
464 } 489 }
465 } 490 }
466 491
467 bool PasswordAutofillManager::ShowSuggestionPopup( 492 bool PasswordAutofillManager::ShowSuggestionPopup(
(...skipping 26 matching lines...) Expand all
494 WebKit::WebInputElement* username_element, 519 WebKit::WebInputElement* username_element,
495 WebKit::WebInputElement* password_element, 520 WebKit::WebInputElement* password_element,
496 const webkit::forms::PasswordFormFillData& fill_data, 521 const webkit::forms::PasswordFormFillData& fill_data,
497 bool exact_username_match, 522 bool exact_username_match,
498 bool set_selection) { 523 bool set_selection) {
499 string16 current_username = username_element->value(); 524 string16 current_username = username_element->value();
500 // username and password will contain the match found if any. 525 // username and password will contain the match found if any.
501 string16 username; 526 string16 username;
502 string16 password; 527 string16 password;
503 528
529 // If there isn't any username form, just exit.
530 // Because this function is used for the case
531 // that the username form and the possword form exist.
532 if (!HasUsernameField(fill_data))
533 return false;
534
504 // Look for any suitable matches to current field text. 535 // Look for any suitable matches to current field text.
505 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value, current_username, 536 if (DoUsernamesMatch(GetUsernameField(fill_data).value, current_username,
506 exact_username_match)) { 537 exact_username_match)) {
507 username = fill_data.basic_data.fields[0].value; 538 username = GetUsernameField(fill_data).value;
508 password = fill_data.basic_data.fields[1].value; 539 password = GetPasswordField(fill_data).value;
509 } else { 540 } else {
510 // Scan additional logins for a match. 541 // Scan additional logins for a match.
511 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter; 542 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
512 for (iter = fill_data.additional_logins.begin(); 543 for (iter = fill_data.additional_logins.begin();
513 iter != fill_data.additional_logins.end(); ++iter) { 544 iter != fill_data.additional_logins.end(); ++iter) {
514 if (DoUsernamesMatch(iter->first, current_username, 545 if (DoUsernamesMatch(iter->first, current_username,
515 exact_username_match)) { 546 exact_username_match)) {
516 username = iter->first; 547 username = iter->first;
517 password = iter->second; 548 password = iter->second;
518 break; 549 break;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input); 617 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(input);
587 if (iter == login_to_password_info_.end()) 618 if (iter == login_to_password_info_.end())
588 return false; 619 return false;
589 620
590 *found_input = input; 621 *found_input = input;
591 *found_password = iter->second; 622 *found_password = iter->second;
592 return true; 623 return true;
593 } 624 }
594 625
595 } // namespace autofill 626 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698