| OLD | NEW |
| 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_generation_agent.h" | 5 #include "components/autofill/content/renderer/password_generation_agent.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 if (editing_popup_shown_) { | 172 if (editing_popup_shown_) { |
| 173 password_generation::LogPasswordGenerationEvent( | 173 password_generation::LogPasswordGenerationEvent( |
| 174 password_generation::EDITING_POPUP_SHOWN); | 174 password_generation::EDITING_POPUP_SHOWN); |
| 175 } | 175 } |
| 176 editing_popup_shown_ = false; | 176 editing_popup_shown_ = false; |
| 177 } | 177 } |
| 178 | 178 |
| 179 FindPossibleGenerationForm(); | 179 FindPossibleGenerationForm(); |
| 180 } | 180 } |
| 181 | 181 |
| 182 void PasswordGenerationAgent::OnDestruct() { |
| 183 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
| 184 } |
| 185 |
| 182 void PasswordGenerationAgent::OnDynamicFormsSeen() { | 186 void PasswordGenerationAgent::OnDynamicFormsSeen() { |
| 183 FindPossibleGenerationForm(); | 187 FindPossibleGenerationForm(); |
| 184 } | 188 } |
| 185 | 189 |
| 186 void PasswordGenerationAgent::FindPossibleGenerationForm() { | 190 void PasswordGenerationAgent::FindPossibleGenerationForm() { |
| 187 if (!enabled_) | 191 if (!enabled_ || !render_frame()) |
| 188 return; | 192 return; |
| 189 | 193 |
| 190 // We don't want to generate passwords if the browser won't store or sync | 194 // We don't want to generate passwords if the browser won't store or sync |
| 191 // them. | 195 // them. |
| 192 if (!ShouldAnalyzeDocument()) | 196 if (!ShouldAnalyzeDocument()) |
| 193 return; | 197 return; |
| 194 | 198 |
| 195 // If we have already found a signup form for this page, no need to continue. | 199 // If we have already found a signup form for this page, no need to continue. |
| 196 if (generation_form_data_) | 200 if (generation_form_data_) |
| 197 return; | 201 return; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 if (!possible_account_creation_forms_.empty()) { | 234 if (!possible_account_creation_forms_.empty()) { |
| 231 VLOG(2) << possible_account_creation_forms_.size() | 235 VLOG(2) << possible_account_creation_forms_.size() |
| 232 << " possible account creation forms deteceted"; | 236 << " possible account creation forms deteceted"; |
| 233 DetermineGenerationElement(); | 237 DetermineGenerationElement(); |
| 234 } | 238 } |
| 235 } | 239 } |
| 236 | 240 |
| 237 bool PasswordGenerationAgent::ShouldAnalyzeDocument() const { | 241 bool PasswordGenerationAgent::ShouldAnalyzeDocument() const { |
| 238 // Make sure that this security origin is allowed to use password manager. | 242 // Make sure that this security origin is allowed to use password manager. |
| 239 // Generating a password that can't be saved is a bad idea. | 243 // Generating a password that can't be saved is a bad idea. |
| 240 blink::WebSecurityOrigin origin = | 244 if (!render_frame() || |
| 241 render_frame()->GetWebFrame()->document().getSecurityOrigin(); | 245 !render_frame() |
| 242 if (!origin.canAccessPasswordManager()) { | 246 ->GetWebFrame() |
| 247 ->document() |
| 248 .getSecurityOrigin() |
| 249 .canAccessPasswordManager()) { |
| 243 VLOG(1) << "No PasswordManager access"; | 250 VLOG(1) << "No PasswordManager access"; |
| 244 return false; | 251 return false; |
| 245 } | 252 } |
| 246 | 253 |
| 247 return true; | 254 return true; |
| 248 } | 255 } |
| 249 | 256 |
| 250 bool PasswordGenerationAgent::OnMessageReceived(const IPC::Message& message) { | 257 bool PasswordGenerationAgent::OnMessageReceived(const IPC::Message& message) { |
| 251 bool handled = true; | 258 bool handled = true; |
| 252 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationAgent, message) | 259 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationAgent, message) |
| (...skipping 15 matching lines...) Expand all Loading... |
| 268 DetermineGenerationElement(); | 275 DetermineGenerationElement(); |
| 269 } | 276 } |
| 270 | 277 |
| 271 void PasswordGenerationAgent::OnPasswordAccepted( | 278 void PasswordGenerationAgent::OnPasswordAccepted( |
| 272 const base::string16& password) { | 279 const base::string16& password) { |
| 273 password_is_generated_ = true; | 280 password_is_generated_ = true; |
| 274 password_generation::LogPasswordGenerationEvent( | 281 password_generation::LogPasswordGenerationEvent( |
| 275 password_generation::PASSWORD_ACCEPTED); | 282 password_generation::PASSWORD_ACCEPTED); |
| 276 for (auto& password_element : generation_form_data_->password_elements) { | 283 for (auto& password_element : generation_form_data_->password_elements) { |
| 277 password_element.setValue(password, true /* sendEvents */); | 284 password_element.setValue(password, true /* sendEvents */); |
| 285 // setValue() above may have resulted in JavaScript closing the frame. |
| 286 if (!render_frame()) |
| 287 return; |
| 278 password_element.setAutofilled(true); | 288 password_element.setAutofilled(true); |
| 279 // Needed to notify password_autofill_agent that the content of the field | 289 // Needed to notify password_autofill_agent that the content of the field |
| 280 // has changed. Without this we will overwrite the generated | 290 // has changed. Without this we will overwrite the generated |
| 281 // password with an Autofilled password when saving. | 291 // password with an Autofilled password when saving. |
| 282 // https://crbug.com/493455 | 292 // https://crbug.com/493455 |
| 283 password_agent_->UpdateStateForTextChange(password_element); | 293 password_agent_->UpdateStateForTextChange(password_element); |
| 284 // Advance focus to the next input field. We assume password fields in | 294 // Advance focus to the next input field. We assume password fields in |
| 285 // an account creation form are always adjacent. | 295 // an account creation form are always adjacent. |
| 286 render_frame()->GetRenderView()->GetWebView()->advanceFocus(false); | 296 render_frame()->GetRenderView()->GetWebView()->advanceFocus(false); |
| 287 } | 297 } |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 // Password isn't generated and there are fewer than kMaximumOfferSize | 446 // Password isn't generated and there are fewer than kMaximumOfferSize |
| 437 // characters typed, so keep offering the password. Note this function | 447 // characters typed, so keep offering the password. Note this function |
| 438 // will just keep the previous popup if one is already showing. | 448 // will just keep the previous popup if one is already showing. |
| 439 ShowGenerationPopup(); | 449 ShowGenerationPopup(); |
| 440 } | 450 } |
| 441 | 451 |
| 442 return true; | 452 return true; |
| 443 } | 453 } |
| 444 | 454 |
| 445 void PasswordGenerationAgent::ShowGenerationPopup() { | 455 void PasswordGenerationAgent::ShowGenerationPopup() { |
| 456 if (!render_frame()) |
| 457 return; |
| 446 Send(new AutofillHostMsg_ShowPasswordGenerationPopup( | 458 Send(new AutofillHostMsg_ShowPasswordGenerationPopup( |
| 447 routing_id(), | 459 routing_id(), |
| 448 render_frame()->GetRenderView()->ElementBoundsInWindow( | 460 render_frame()->GetRenderView()->ElementBoundsInWindow( |
| 449 generation_element_), | 461 generation_element_), |
| 450 generation_element_.maxLength(), | 462 generation_element_.maxLength(), |
| 451 generation_element_.nameForAutofill(), | 463 generation_element_.nameForAutofill(), |
| 452 is_manually_triggered_, | 464 is_manually_triggered_, |
| 453 *generation_form_data_->form)); | 465 *generation_form_data_->form)); |
| 454 generation_popup_shown_ = true; | 466 generation_popup_shown_ = true; |
| 455 } | 467 } |
| 456 | 468 |
| 457 void PasswordGenerationAgent::ShowEditingPopup() { | 469 void PasswordGenerationAgent::ShowEditingPopup() { |
| 470 if (!render_frame()) |
| 471 return; |
| 458 Send(new AutofillHostMsg_ShowPasswordEditingPopup( | 472 Send(new AutofillHostMsg_ShowPasswordEditingPopup( |
| 459 routing_id(), | 473 routing_id(), |
| 460 render_frame()->GetRenderView()->ElementBoundsInWindow( | 474 render_frame()->GetRenderView()->ElementBoundsInWindow( |
| 461 generation_element_), | 475 generation_element_), |
| 462 *generation_form_data_->form)); | 476 *generation_form_data_->form)); |
| 463 editing_popup_shown_ = true; | 477 editing_popup_shown_ = true; |
| 464 } | 478 } |
| 465 | 479 |
| 466 void PasswordGenerationAgent::HidePopup() { | 480 void PasswordGenerationAgent::HidePopup() { |
| 467 Send(new AutofillHostMsg_HidePasswordGenerationPopup(routing_id())); | 481 Send(new AutofillHostMsg_HidePasswordGenerationPopup(routing_id())); |
| 468 } | 482 } |
| 469 | 483 |
| 470 void PasswordGenerationAgent::OnUserTriggeredGeneratePassword() { | 484 void PasswordGenerationAgent::OnUserTriggeredGeneratePassword() { |
| 471 if (last_focused_password_element_.isNull()) | 485 if (last_focused_password_element_.isNull() || !render_frame()) |
| 472 return; | 486 return; |
| 473 | 487 |
| 474 blink::WebFormElement form = last_focused_password_element_.form(); | 488 blink::WebFormElement form = last_focused_password_element_.form(); |
| 475 std::unique_ptr<PasswordForm> password_form; | 489 std::unique_ptr<PasswordForm> password_form; |
| 476 std::vector<blink::WebFormControlElement> control_elements; | 490 std::vector<blink::WebFormControlElement> control_elements; |
| 477 if (!form.isNull()) { | 491 if (!form.isNull()) { |
| 478 password_form = CreatePasswordFormFromWebForm(form, nullptr, nullptr); | 492 password_form = CreatePasswordFormFromWebForm(form, nullptr, nullptr); |
| 479 control_elements = form_util::ExtractAutofillableElementsInForm(form); | 493 control_elements = form_util::ExtractAutofillableElementsInForm(form); |
| 480 } else { | 494 } else { |
| 481 const blink::WebFrame& frame = *render_frame()->GetWebFrame(); | 495 const blink::WebFrame& frame = *render_frame()->GetWebFrame(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 496 GetAccountCreationPasswordFields(control_elements, &password_elements); | 510 GetAccountCreationPasswordFields(control_elements, &password_elements); |
| 497 password_elements = FindPasswordElementsForGeneration( | 511 password_elements = FindPasswordElementsForGeneration( |
| 498 password_elements, last_focused_password_element_.nameForAutofill()); | 512 password_elements, last_focused_password_element_.nameForAutofill()); |
| 499 generation_form_data_.reset(new AccountCreationFormData( | 513 generation_form_data_.reset(new AccountCreationFormData( |
| 500 make_linked_ptr(password_form.release()), password_elements)); | 514 make_linked_ptr(password_form.release()), password_elements)); |
| 501 is_manually_triggered_ = true; | 515 is_manually_triggered_ = true; |
| 502 ShowGenerationPopup(); | 516 ShowGenerationPopup(); |
| 503 } | 517 } |
| 504 | 518 |
| 505 } // namespace autofill | 519 } // namespace autofill |
| OLD | NEW |