| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/password_autocomplete_manager.h" | 5 #include "chrome/renderer/password_autocomplete_manager.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/renderer/render_view.h" | 10 #include "chrome/renderer/render_view.h" |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 234 |
| 235 // If wait_for_username is false, we should have filled when the text changed. | 235 // If wait_for_username is false, we should have filled when the text changed. |
| 236 if (!fill_data.wait_for_username) | 236 if (!fill_data.wait_for_username) |
| 237 return false; | 237 return false; |
| 238 | 238 |
| 239 WebKit::WebInputElement password = iter->second.password_field; | 239 WebKit::WebInputElement password = iter->second.password_field; |
| 240 if (!IsElementEditable(password)) | 240 if (!IsElementEditable(password)) |
| 241 return false; | 241 return false; |
| 242 | 242 |
| 243 WebKit::WebInputElement username = element; // We need a non-const. | 243 WebKit::WebInputElement username = element; // We need a non-const. |
| 244 FillUserNameAndPassword(&username, &password, fill_data, true); | 244 |
| 245 // Do not set selection when ending an editing session, otherwise it can |
| 246 // mess with focus. |
| 247 FillUserNameAndPassword(&username, &password, fill_data, true, false); |
| 245 return true; | 248 return true; |
| 246 } | 249 } |
| 247 | 250 |
| 248 bool PasswordAutocompleteManager::TextDidChangeInTextField( | 251 bool PasswordAutocompleteManager::TextDidChangeInTextField( |
| 249 const WebKit::WebInputElement& element) { | 252 const WebKit::WebInputElement& element) { |
| 250 LoginToPasswordInfoMap::const_iterator iter = | 253 LoginToPasswordInfoMap::const_iterator iter = |
| 251 login_to_password_info_.find(element); | 254 login_to_password_info_.find(element); |
| 252 if (iter == login_to_password_info_.end()) | 255 if (iter == login_to_password_info_.end()) |
| 253 return false; | 256 return false; |
| 254 | 257 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 const WebKit::WebInputElement& user_input) { | 313 const WebKit::WebInputElement& user_input) { |
| 311 LoginToPasswordInfoMap::iterator iter = | 314 LoginToPasswordInfoMap::iterator iter = |
| 312 login_to_password_info_.find(user_input); | 315 login_to_password_info_.find(user_input); |
| 313 if (iter == login_to_password_info_.end()) | 316 if (iter == login_to_password_info_.end()) |
| 314 return false; | 317 return false; |
| 315 const webkit_glue::PasswordFormFillData& fill_data = | 318 const webkit_glue::PasswordFormFillData& fill_data = |
| 316 iter->second.fill_data; | 319 iter->second.fill_data; |
| 317 WebKit::WebInputElement password = iter->second.password_field; | 320 WebKit::WebInputElement password = iter->second.password_field; |
| 318 WebKit::WebInputElement non_const_user_input(user_input); | 321 WebKit::WebInputElement non_const_user_input(user_input); |
| 319 return FillUserNameAndPassword(&non_const_user_input, &password, | 322 return FillUserNameAndPassword(&non_const_user_input, &password, |
| 320 fill_data, true); | 323 fill_data, true, true); |
| 321 } | 324 } |
| 322 | 325 |
| 323 void PasswordAutocompleteManager::PerformInlineAutocomplete( | 326 void PasswordAutocompleteManager::PerformInlineAutocomplete( |
| 324 const WebKit::WebInputElement& username_input, | 327 const WebKit::WebInputElement& username_input, |
| 325 const WebKit::WebInputElement& password_input, | 328 const WebKit::WebInputElement& password_input, |
| 326 const webkit_glue::PasswordFormFillData& fill_data) { | 329 const webkit_glue::PasswordFormFillData& fill_data) { |
| 327 DCHECK(!fill_data.wait_for_username); | 330 DCHECK(!fill_data.wait_for_username); |
| 328 | 331 |
| 329 // We need non-const versions of the username and password inputs. | 332 // We need non-const versions of the username and password inputs. |
| 330 WebKit::WebInputElement username = username_input; | 333 WebKit::WebInputElement username = username_input; |
| 331 WebKit::WebInputElement password = password_input; | 334 WebKit::WebInputElement password = password_input; |
| 332 | 335 |
| 333 // Don't inline autocomplete if the caret is not at the end. | 336 // Don't inline autocomplete if the caret is not at the end. |
| 334 // TODO(jcivelli): is there a better way to test the caret location? | 337 // TODO(jcivelli): is there a better way to test the caret location? |
| 335 if (username.selectionStart() != username.selectionEnd() || | 338 if (username.selectionStart() != username.selectionEnd() || |
| 336 username.selectionEnd() != static_cast<int>(username.value().length())) { | 339 username.selectionEnd() != static_cast<int>(username.value().length())) { |
| 337 return; | 340 return; |
| 338 } | 341 } |
| 339 | 342 |
| 340 // Show the popup with the list of available usernames. | 343 // Show the popup with the list of available usernames. |
| 341 ShowSuggestionPopup(fill_data, username); | 344 ShowSuggestionPopup(fill_data, username); |
| 342 | 345 |
| 343 // Fill the user and password field with the most relevant match. | 346 // Fill the user and password field with the most relevant match. |
| 344 FillUserNameAndPassword(&username, &password, fill_data, false); | 347 FillUserNameAndPassword(&username, &password, fill_data, false, true); |
| 345 } | 348 } |
| 346 | 349 |
| 347 void PasswordAutocompleteManager::SendPasswordForms(WebKit::WebFrame* frame, | 350 void PasswordAutocompleteManager::SendPasswordForms(WebKit::WebFrame* frame, |
| 348 bool only_visible) { | 351 bool only_visible) { |
| 349 // Make sure that this security origin is allowed to use password manager. | 352 // Make sure that this security origin is allowed to use password manager. |
| 350 WebKit::WebSecurityOrigin security_origin = frame->securityOrigin(); | 353 WebKit::WebSecurityOrigin security_origin = frame->securityOrigin(); |
| 351 if (!security_origin.canAccessPasswordManager()) | 354 if (!security_origin.canAccessPasswordManager()) |
| 352 return; | 355 return; |
| 353 | 356 |
| 354 WebKit::WebVector<WebKit::WebFormElement> forms; | 357 WebKit::WebVector<WebKit::WebFormElement> forms; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 std::vector<int> ids(suggestions.size(), 0); | 432 std::vector<int> ids(suggestions.size(), 0); |
| 430 webview->applyAutoFillSuggestions(user_input, suggestions, labels, icons, ids, | 433 webview->applyAutoFillSuggestions(user_input, suggestions, labels, icons, ids, |
| 431 -1); | 434 -1); |
| 432 return true; | 435 return true; |
| 433 } | 436 } |
| 434 | 437 |
| 435 bool PasswordAutocompleteManager::FillUserNameAndPassword( | 438 bool PasswordAutocompleteManager::FillUserNameAndPassword( |
| 436 WebKit::WebInputElement* username_element, | 439 WebKit::WebInputElement* username_element, |
| 437 WebKit::WebInputElement* password_element, | 440 WebKit::WebInputElement* password_element, |
| 438 const webkit_glue::PasswordFormFillData& fill_data, | 441 const webkit_glue::PasswordFormFillData& fill_data, |
| 439 bool exact_username_match) { | 442 bool exact_username_match, |
| 443 bool set_selection) { |
| 440 string16 current_username = username_element->value(); | 444 string16 current_username = username_element->value(); |
| 441 // username and password will contain the match found if any. | 445 // username and password will contain the match found if any. |
| 442 string16 username; | 446 string16 username; |
| 443 string16 password; | 447 string16 password; |
| 444 | 448 |
| 445 // Look for any suitable matches to current field text. | 449 // Look for any suitable matches to current field text. |
| 446 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value(), current_username, | 450 if (DoUsernamesMatch(fill_data.basic_data.fields[0].value(), current_username, |
| 447 exact_username_match)) { | 451 exact_username_match)) { |
| 448 username = fill_data.basic_data.fields[0].value(); | 452 username = fill_data.basic_data.fields[0].value(); |
| 449 password = fill_data.basic_data.fields[1].value(); | 453 password = fill_data.basic_data.fields[1].value(); |
| 450 } else { | 454 } else { |
| 451 // Scan additional logins for a match. | 455 // Scan additional logins for a match. |
| 452 webkit_glue::PasswordFormFillData::LoginCollection::const_iterator iter; | 456 webkit_glue::PasswordFormFillData::LoginCollection::const_iterator iter; |
| 453 for (iter = fill_data.additional_logins.begin(); | 457 for (iter = fill_data.additional_logins.begin(); |
| 454 iter != fill_data.additional_logins.end(); ++iter) { | 458 iter != fill_data.additional_logins.end(); ++iter) { |
| 455 if (DoUsernamesMatch(iter->first, current_username, | 459 if (DoUsernamesMatch(iter->first, current_username, |
| 456 exact_username_match)) { | 460 exact_username_match)) { |
| 457 username = iter->first; | 461 username = iter->first; |
| 458 password = iter->second; | 462 password = iter->second; |
| 459 break; | 463 break; |
| 460 } | 464 } |
| 461 } | 465 } |
| 462 } | 466 } |
| 463 if (password.empty()) | 467 if (password.empty()) |
| 464 return false; // No match was found. | 468 return false; // No match was found. |
| 465 | 469 |
| 466 // Input matches the username, fill in required values. | 470 // Input matches the username, fill in required values. |
| 467 username_element->setValue(username); | 471 username_element->setValue(username); |
| 468 username_element->setSelectionRange(current_username.length(), | 472 |
| 469 username.length()); | 473 if (set_selection) { |
| 474 username_element->setSelectionRange(current_username.length(), |
| 475 username.length()); |
| 476 } |
| 477 |
| 470 SetElementAutofilled(username_element, true); | 478 SetElementAutofilled(username_element, true); |
| 471 if (IsElementEditable(*password_element)) | 479 if (IsElementEditable(*password_element)) |
| 472 password_element->setValue(password); | 480 password_element->setValue(password); |
| 473 SetElementAutofilled(password_element, true); | 481 SetElementAutofilled(password_element, true); |
| 474 return true; | 482 return true; |
| 475 } | 483 } |
| 476 | 484 |
| 477 int PasswordAutocompleteManager::GetRoutingID() const { | 485 int PasswordAutocompleteManager::GetRoutingID() const { |
| 478 return render_view_->routing_id(); | 486 return render_view_->routing_id(); |
| 479 } | 487 } |
| OLD | NEW |