| 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 "app/keyboard_codes.h" | 7 #include "app/keyboard_codes.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_ptr.h" | 9 #include "base/scoped_ptr.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 if (exact_match) | 162 if (exact_match) |
| 163 return username1 == username2; | 163 return username1 == username2; |
| 164 return StartsWith(username1, username2, true); | 164 return StartsWith(username1, username2, true); |
| 165 } | 165 } |
| 166 | 166 |
| 167 } // namespace | 167 } // namespace |
| 168 | 168 |
| 169 //////////////////////////////////////////////////////////////////////////////// | 169 //////////////////////////////////////////////////////////////////////////////// |
| 170 // PasswordAutocompleteManager, public: | 170 // PasswordAutocompleteManager, public: |
| 171 | 171 |
| 172 PasswordAutocompleteManager::PasswordAutocompleteManager( | 172 PasswordAutocompleteManager::PasswordAutocompleteManager() |
| 173 RenderView* render_view) | 173 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 174 : render_view_(render_view), | |
| 175 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 176 } | 174 } |
| 177 | 175 |
| 178 PasswordAutocompleteManager::~PasswordAutocompleteManager() { | 176 PasswordAutocompleteManager::~PasswordAutocompleteManager() { |
| 179 } | 177 } |
| 180 | 178 |
| 181 void PasswordAutocompleteManager::ReceivedPasswordFormFillData( | |
| 182 WebKit::WebView* view, | |
| 183 const webkit_glue::PasswordFormFillData& form_data) { | |
| 184 FormElementsList forms; | |
| 185 // We own the FormElements* in forms. | |
| 186 FindFormElements(view, form_data.basic_data, &forms); | |
| 187 FormElementsList::iterator iter; | |
| 188 for (iter = forms.begin(); iter != forms.end(); ++iter) { | |
| 189 scoped_ptr<FormElements> form_elements(*iter); | |
| 190 | |
| 191 // If wait_for_username is true, we don't want to initially fill the form | |
| 192 // until the user types in a valid username. | |
| 193 if (!form_data.wait_for_username) | |
| 194 FillForm(form_elements.get(), form_data.basic_data); | |
| 195 | |
| 196 // Attach autocomplete listener to enable selecting alternate logins. | |
| 197 // First, get pointers to username element. | |
| 198 WebKit::WebInputElement username_element = | |
| 199 form_elements->input_elements[form_data.basic_data.fields[0].name()]; | |
| 200 | |
| 201 // Get pointer to password element. (We currently only support single | |
| 202 // password forms). | |
| 203 WebKit::WebInputElement password_element = | |
| 204 form_elements->input_elements[form_data.basic_data.fields[1].name()]; | |
| 205 | |
| 206 DCHECK(login_to_password_info_.find(username_element) == | |
| 207 login_to_password_info_.end()); | |
| 208 PasswordInfo password_info; | |
| 209 password_info.fill_data = form_data; | |
| 210 password_info.password_field = password_element; | |
| 211 login_to_password_info_[username_element] = password_info; | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 void PasswordAutocompleteManager::FrameClosing(const WebKit::WebFrame* frame) { | 179 void PasswordAutocompleteManager::FrameClosing(const WebKit::WebFrame* frame) { |
| 216 for (LoginToPasswordInfoMap::iterator iter = login_to_password_info_.begin(); | 180 for (LoginToPasswordInfoMap::iterator iter = login_to_password_info_.begin(); |
| 217 iter != login_to_password_info_.end();) { | 181 iter != login_to_password_info_.end();) { |
| 218 if (iter->first.document().frame() == frame) | 182 if (iter->first.document().frame() == frame) |
| 219 login_to_password_info_.erase(iter++); | 183 login_to_password_info_.erase(iter++); |
| 220 else | 184 else |
| 221 ++iter; | 185 ++iter; |
| 222 } | 186 } |
| 223 } | 187 } |
| 224 | 188 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 scoped_ptr<webkit_glue::PasswordForm> password_form( | 330 scoped_ptr<webkit_glue::PasswordForm> password_form( |
| 367 webkit_glue::PasswordFormDomManager::CreatePasswordForm(form)); | 331 webkit_glue::PasswordFormDomManager::CreatePasswordForm(form)); |
| 368 if (password_form.get()) | 332 if (password_form.get()) |
| 369 password_forms.push_back(*password_form); | 333 password_forms.push_back(*password_form); |
| 370 } | 334 } |
| 371 | 335 |
| 372 if (password_forms.empty()) | 336 if (password_forms.empty()) |
| 373 return; | 337 return; |
| 374 | 338 |
| 375 if (only_visible) { | 339 if (only_visible) { |
| 376 render_view_->Send( | 340 Send(new ViewHostMsg_PasswordFormsVisible(routing_id(), password_forms)); |
| 377 new ViewHostMsg_PasswordFormsVisible(GetRoutingID(), password_forms)); | |
| 378 } else { | 341 } else { |
| 379 render_view_->Send( | 342 Send(new ViewHostMsg_PasswordFormsFound(routing_id(), password_forms)); |
| 380 new ViewHostMsg_PasswordFormsFound(GetRoutingID(), password_forms)); | |
| 381 } | 343 } |
| 382 } | 344 } |
| 383 | 345 |
| 346 bool PasswordAutocompleteManager::OnMessageReceived( |
| 347 const IPC::Message& message) { |
| 348 bool handled = true; |
| 349 IPC_BEGIN_MESSAGE_MAP(PasswordAutocompleteManager, message) |
| 350 IPC_MESSAGE_HANDLER(ViewMsg_FillPasswordForm, OnFillPasswordForm) |
| 351 IPC_MESSAGE_UNHANDLED(handled = false) |
| 352 IPC_END_MESSAGE_MAP() |
| 353 return handled; |
| 354 } |
| 355 |
| 356 void PasswordAutocompleteManager::DidFinishDocumentLoad( |
| 357 WebKit::WebFrame* frame) { |
| 358 SendPasswordForms(frame, false); |
| 359 } |
| 360 |
| 361 void PasswordAutocompleteManager::DidFinishLoad(WebKit::WebFrame* frame) { |
| 362 SendPasswordForms(frame, true); |
| 363 } |
| 364 |
| 384 //////////////////////////////////////////////////////////////////////////////// | 365 //////////////////////////////////////////////////////////////////////////////// |
| 385 // PageClickListener implementation: | 366 // PageClickListener implementation: |
| 386 | 367 |
| 387 bool PasswordAutocompleteManager::InputElementClicked( | 368 bool PasswordAutocompleteManager::InputElementClicked( |
| 388 const WebKit::WebInputElement& element, | 369 const WebKit::WebInputElement& element, |
| 389 bool was_focused, | 370 bool was_focused, |
| 390 bool is_focused) { | 371 bool is_focused) { |
| 391 // TODO(jcivelli): http://crbug.com/51644 Implement behavior. | 372 // TODO(jcivelli): http://crbug.com/51644 Implement behavior. |
| 392 return false; | 373 return false; |
| 393 } | 374 } |
| 394 | 375 |
| 376 void PasswordAutocompleteManager::OnFillPasswordForm( |
| 377 const webkit_glue::PasswordFormFillData& form_data) { |
| 378 FormElementsList forms; |
| 379 // We own the FormElements* in forms. |
| 380 FindFormElements(render_view()->webview(), form_data.basic_data, &forms); |
| 381 FormElementsList::iterator iter; |
| 382 for (iter = forms.begin(); iter != forms.end(); ++iter) { |
| 383 scoped_ptr<FormElements> form_elements(*iter); |
| 384 |
| 385 // If wait_for_username is true, we don't want to initially fill the form |
| 386 // until the user types in a valid username. |
| 387 if (!form_data.wait_for_username) |
| 388 FillForm(form_elements.get(), form_data.basic_data); |
| 389 |
| 390 // Attach autocomplete listener to enable selecting alternate logins. |
| 391 // First, get pointers to username element. |
| 392 WebKit::WebInputElement username_element = |
| 393 form_elements->input_elements[form_data.basic_data.fields[0].name()]; |
| 394 |
| 395 // Get pointer to password element. (We currently only support single |
| 396 // password forms). |
| 397 WebKit::WebInputElement password_element = |
| 398 form_elements->input_elements[form_data.basic_data.fields[1].name()]; |
| 399 |
| 400 DCHECK(login_to_password_info_.find(username_element) == |
| 401 login_to_password_info_.end()); |
| 402 PasswordInfo password_info; |
| 403 password_info.fill_data = form_data; |
| 404 password_info.password_field = password_element; |
| 405 login_to_password_info_[username_element] = password_info; |
| 406 } |
| 407 } |
| 408 |
| 395 //////////////////////////////////////////////////////////////////////////////// | 409 //////////////////////////////////////////////////////////////////////////////// |
| 396 // PasswordAutocompleteManager, private: | 410 // PasswordAutocompleteManager, private: |
| 397 | 411 |
| 398 void PasswordAutocompleteManager::GetSuggestions( | 412 void PasswordAutocompleteManager::GetSuggestions( |
| 399 const webkit_glue::PasswordFormFillData& fill_data, | 413 const webkit_glue::PasswordFormFillData& fill_data, |
| 400 const string16& input, | 414 const string16& input, |
| 401 std::vector<string16>* suggestions) { | 415 std::vector<string16>* suggestions) { |
| 402 if (StartsWith(fill_data.basic_data.fields[0].value(), input, false)) | 416 if (StartsWith(fill_data.basic_data.fields[0].value(), input, false)) |
| 403 suggestions->push_back(fill_data.basic_data.fields[0].value()); | 417 suggestions->push_back(fill_data.basic_data.fields[0].value()); |
| 404 | 418 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 // Input matches the username, fill in required values. | 480 // Input matches the username, fill in required values. |
| 467 username_element->setValue(username); | 481 username_element->setValue(username); |
| 468 username_element->setSelectionRange(current_username.length(), | 482 username_element->setSelectionRange(current_username.length(), |
| 469 username.length()); | 483 username.length()); |
| 470 SetElementAutofilled(username_element, true); | 484 SetElementAutofilled(username_element, true); |
| 471 if (IsElementEditable(*password_element)) | 485 if (IsElementEditable(*password_element)) |
| 472 password_element->setValue(password); | 486 password_element->setValue(password); |
| 473 SetElementAutofilled(password_element, true); | 487 SetElementAutofilled(password_element, true); |
| 474 return true; | 488 return true; |
| 475 } | 489 } |
| 476 | |
| 477 int PasswordAutocompleteManager::GetRoutingID() const { | |
| 478 return render_view_->routing_id(); | |
| 479 } | |
| OLD | NEW |