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