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" |
11 #include "components/autofill/content/common/autofill_messages.h" | 11 #include "components/autofill/content/common/autofill_messages.h" |
12 #include "components/autofill/content/renderer/form_autofill_util.h" | 12 #include "components/autofill/content/renderer/form_autofill_util.h" |
13 #include "components/autofill/content/renderer/form_classifier.h" | 13 #include "components/autofill/content/renderer/form_classifier.h" |
14 #include "components/autofill/content/renderer/password_autofill_agent.h" | 14 #include "components/autofill/content/renderer/password_autofill_agent.h" |
15 #include "components/autofill/content/renderer/password_form_conversion_utils.h" | 15 #include "components/autofill/content/renderer/password_form_conversion_utils.h" |
16 #include "components/autofill/core/common/autofill_switches.h" | 16 #include "components/autofill/core/common/autofill_switches.h" |
17 #include "components/autofill/core/common/form_data.h" | 17 #include "components/autofill/core/common/form_data.h" |
18 #include "components/autofill/core/common/password_form.h" | 18 #include "components/autofill/core/common/password_form.h" |
19 #include "components/autofill/core/common/password_form_generation_data.h" | 19 #include "components/autofill/core/common/password_form_generation_data.h" |
20 #include "components/autofill/core/common/password_generation_util.h" | 20 #include "components/autofill/core/common/password_generation_util.h" |
21 #include "content/public/renderer/render_frame.h" | 21 #include "content/public/renderer/render_frame.h" |
22 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
23 #include "google_apis/gaia/gaia_urls.h" | 23 #include "google_apis/gaia/gaia_urls.h" |
24 #include "services/shell/public/cpp/interface_registry.h" | |
25 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | 24 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
26 #include "third_party/WebKit/public/platform/WebVector.h" | 25 #include "third_party/WebKit/public/platform/WebVector.h" |
27 #include "third_party/WebKit/public/web/WebDocument.h" | 26 #include "third_party/WebKit/public/web/WebDocument.h" |
28 #include "third_party/WebKit/public/web/WebFormElement.h" | 27 #include "third_party/WebKit/public/web/WebFormElement.h" |
29 #include "third_party/WebKit/public/web/WebInputElement.h" | 28 #include "third_party/WebKit/public/web/WebInputElement.h" |
30 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
31 #include "third_party/WebKit/public/web/WebView.h" | 30 #include "third_party/WebKit/public/web/WebView.h" |
32 #include "ui/gfx/geometry/rect.h" | 31 #include "ui/gfx/geometry/rect.h" |
33 | 32 |
34 namespace autofill { | 33 namespace autofill { |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 content::RenderFrame* render_frame, | 120 content::RenderFrame* render_frame, |
122 PasswordAutofillAgent* password_agent) | 121 PasswordAutofillAgent* password_agent) |
123 : content::RenderFrameObserver(render_frame), | 122 : content::RenderFrameObserver(render_frame), |
124 password_is_generated_(false), | 123 password_is_generated_(false), |
125 is_manually_triggered_(false), | 124 is_manually_triggered_(false), |
126 password_edited_(false), | 125 password_edited_(false), |
127 generation_popup_shown_(false), | 126 generation_popup_shown_(false), |
128 editing_popup_shown_(false), | 127 editing_popup_shown_(false), |
129 enabled_(password_generation::IsPasswordGenerationEnabled()), | 128 enabled_(password_generation::IsPasswordGenerationEnabled()), |
130 form_classifier_enabled_(false), | 129 form_classifier_enabled_(false), |
131 password_agent_(password_agent), | 130 password_agent_(password_agent) { |
132 binding_(this) { | |
133 VLOG(2) << "Password Generation is " << (enabled_ ? "Enabled" : "Disabled"); | 131 VLOG(2) << "Password Generation is " << (enabled_ ? "Enabled" : "Disabled"); |
134 // PasswordGenerationAgent is guaranteed to outlive |render_frame|. | |
135 render_frame->GetInterfaceRegistry()->AddInterface(base::Bind( | |
136 &PasswordGenerationAgent::BindRequest, base::Unretained(this))); | |
137 } | 132 } |
138 PasswordGenerationAgent::~PasswordGenerationAgent() {} | 133 PasswordGenerationAgent::~PasswordGenerationAgent() {} |
139 | 134 |
140 void PasswordGenerationAgent::BindRequest( | |
141 mojom::PasswordGenerationAgentRequest request) { | |
142 binding_.Bind(std::move(request)); | |
143 } | |
144 | |
145 void PasswordGenerationAgent::DidFinishDocumentLoad() { | 135 void PasswordGenerationAgent::DidFinishDocumentLoad() { |
146 // Update stats for main frame navigation. | 136 // Update stats for main frame navigation. |
147 if (!render_frame()->GetWebFrame()->parent()) { | 137 if (!render_frame()->GetWebFrame()->parent()) { |
148 // In every navigation, the IPC message sent by the password autofill | 138 // In every navigation, the IPC message sent by the password autofill |
149 // manager to query whether the current form is blacklisted or not happens | 139 // manager to query whether the current form is blacklisted or not happens |
150 // when the document load finishes, so we need to clear previous states | 140 // when the document load finishes, so we need to clear previous states |
151 // here before we hear back from the browser. We only clear this state on | 141 // here before we hear back from the browser. We only clear this state on |
152 // main frame load as we don't want subframe loads to clear state that we | 142 // main frame load as we don't want subframe loads to clear state that we |
153 // have received from the main frame. Note that we assume there is only one | 143 // have received from the main frame. Note that we assume there is only one |
154 // account creation form, but there could be multiple password forms in | 144 // account creation form, but there could be multiple password forms in |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 } | 189 } |
200 | 190 |
201 void PasswordGenerationAgent::OnDestruct() { | 191 void PasswordGenerationAgent::OnDestruct() { |
202 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | 192 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); |
203 } | 193 } |
204 | 194 |
205 void PasswordGenerationAgent::OnDynamicFormsSeen() { | 195 void PasswordGenerationAgent::OnDynamicFormsSeen() { |
206 FindPossibleGenerationForm(); | 196 FindPossibleGenerationForm(); |
207 } | 197 } |
208 | 198 |
209 void PasswordGenerationAgent::AllowToRunFormClassifier() { | 199 void PasswordGenerationAgent::OnAllowToRunFormClassifier() { |
210 form_classifier_enabled_ = true; | 200 form_classifier_enabled_ = true; |
211 } | 201 } |
212 | 202 |
213 void PasswordGenerationAgent::RunFormClassifierAndSaveVote( | 203 void PasswordGenerationAgent::RunFormClassifierAndSaveVote( |
214 const blink::WebFormElement& web_form, | 204 const blink::WebFormElement& web_form, |
215 const PasswordForm& form) { | 205 const PasswordForm& form) { |
216 DCHECK(form_classifier_enabled_); | 206 DCHECK(form_classifier_enabled_); |
217 | 207 |
218 base::string16 generation_field; | 208 base::string16 generation_field; |
219 ClassifyFormAndFindGenerationField(web_form, &generation_field); | 209 ClassifyFormAndFindGenerationField(web_form, &generation_field); |
220 GetPasswordManagerDriver()->SaveGenerationFieldDetectedByClassifier( | 210 Send(new AutofillHostMsg_SaveGenerationFieldDetectedByClassifier( |
221 form, generation_field); | 211 routing_id(), form, generation_field)); |
222 } | 212 } |
223 | 213 |
224 void PasswordGenerationAgent::FindPossibleGenerationForm() { | 214 void PasswordGenerationAgent::FindPossibleGenerationForm() { |
225 if (!enabled_ || !render_frame()) | 215 if (!enabled_ || !render_frame()) |
226 return; | 216 return; |
227 | 217 |
228 // We don't want to generate passwords if the browser won't store or sync | 218 // We don't want to generate passwords if the browser won't store or sync |
229 // them. | 219 // them. |
230 if (!ShouldAnalyzeDocument()) | 220 if (!ShouldAnalyzeDocument()) |
231 return; | 221 return; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ->document() | 273 ->document() |
284 .getSecurityOrigin() | 274 .getSecurityOrigin() |
285 .canAccessPasswordManager()) { | 275 .canAccessPasswordManager()) { |
286 VLOG(1) << "No PasswordManager access"; | 276 VLOG(1) << "No PasswordManager access"; |
287 return false; | 277 return false; |
288 } | 278 } |
289 | 279 |
290 return true; | 280 return true; |
291 } | 281 } |
292 | 282 |
293 void PasswordGenerationAgent::FormNotBlacklisted(const PasswordForm& form) { | 283 bool PasswordGenerationAgent::OnMessageReceived(const IPC::Message& message) { |
| 284 bool handled = true; |
| 285 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationAgent, message) |
| 286 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, |
| 287 OnFormNotBlacklisted) |
| 288 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, |
| 289 OnPasswordAccepted) |
| 290 IPC_MESSAGE_HANDLER(AutofillMsg_FoundFormsEligibleForGeneration, |
| 291 OnFormsEligibleForGenerationFound); |
| 292 IPC_MESSAGE_HANDLER(AutofillMsg_UserTriggeredGeneratePassword, |
| 293 OnUserTriggeredGeneratePassword); |
| 294 IPC_MESSAGE_HANDLER(AutofillMsg_AllowToRunFormClassifier, |
| 295 OnAllowToRunFormClassifier); |
| 296 IPC_MESSAGE_UNHANDLED(handled = false) |
| 297 IPC_END_MESSAGE_MAP() |
| 298 return handled; |
| 299 } |
| 300 |
| 301 void PasswordGenerationAgent::OnFormNotBlacklisted(const PasswordForm& form) { |
294 not_blacklisted_password_form_origins_.push_back(form.origin); | 302 not_blacklisted_password_form_origins_.push_back(form.origin); |
295 DetermineGenerationElement(); | 303 DetermineGenerationElement(); |
296 } | 304 } |
297 | 305 |
298 void PasswordGenerationAgent::GeneratedPasswordAccepted( | 306 void PasswordGenerationAgent::OnPasswordAccepted( |
299 const base::string16& password) { | 307 const base::string16& password) { |
300 password_is_generated_ = true; | 308 password_is_generated_ = true; |
301 password_generation::LogPasswordGenerationEvent( | 309 password_generation::LogPasswordGenerationEvent( |
302 password_generation::PASSWORD_ACCEPTED); | 310 password_generation::PASSWORD_ACCEPTED); |
303 for (auto& password_element : generation_form_data_->password_elements) { | 311 for (auto& password_element : generation_form_data_->password_elements) { |
304 password_element.setValue(password, true /* sendEvents */); | 312 password_element.setValue(password, true /* sendEvents */); |
305 // setValue() above may have resulted in JavaScript closing the frame. | 313 // setValue() above may have resulted in JavaScript closing the frame. |
306 if (!render_frame()) | 314 if (!render_frame()) |
307 return; | 315 return; |
308 password_element.setAutofilled(true); | 316 password_element.setAutofilled(true); |
309 // Needed to notify password_autofill_agent that the content of the field | 317 // Needed to notify password_autofill_agent that the content of the field |
310 // has changed. Without this we will overwrite the generated | 318 // has changed. Without this we will overwrite the generated |
311 // password with an Autofilled password when saving. | 319 // password with an Autofilled password when saving. |
312 // https://crbug.com/493455 | 320 // https://crbug.com/493455 |
313 password_agent_->UpdateStateForTextChange(password_element); | 321 password_agent_->UpdateStateForTextChange(password_element); |
314 // Advance focus to the next input field. We assume password fields in | 322 // Advance focus to the next input field. We assume password fields in |
315 // an account creation form are always adjacent. | 323 // an account creation form are always adjacent. |
316 render_frame()->GetRenderView()->GetWebView()->advanceFocus(false); | 324 render_frame()->GetRenderView()->GetWebView()->advanceFocus(false); |
317 } | 325 } |
318 std::unique_ptr<PasswordForm> presaved_form(CreatePasswordFormToPresave()); | 326 std::unique_ptr<PasswordForm> presaved_form(CreatePasswordFormToPresave()); |
319 if (presaved_form) { | 327 if (presaved_form) { |
320 GetPasswordManagerDriver()->PresaveGeneratedPassword(*presaved_form); | 328 Send(new AutofillHostMsg_PresaveGeneratedPassword(routing_id(), |
| 329 *presaved_form)); |
321 } | 330 } |
322 } | 331 } |
323 | 332 |
324 std::unique_ptr<PasswordForm> | 333 std::unique_ptr<PasswordForm> |
325 PasswordGenerationAgent::CreatePasswordFormToPresave() { | 334 PasswordGenerationAgent::CreatePasswordFormToPresave() { |
326 DCHECK(!generation_element_.isNull()); | 335 DCHECK(!generation_element_.isNull()); |
327 // Since the form for presaving should match a form in the browser, create it | 336 // Since the form for presaving should match a form in the browser, create it |
328 // with the same algorithm (to match html attributes, action, etc.), but | 337 // with the same algorithm (to match html attributes, action, etc.), but |
329 // change username and password values. | 338 // change username and password values. |
330 std::unique_ptr<PasswordForm> password_form; | 339 std::unique_ptr<PasswordForm> password_form; |
331 if (!generation_element_.form().isNull()) { | 340 if (!generation_element_.form().isNull()) { |
332 password_form = CreatePasswordFormFromWebForm(generation_element_.form(), | 341 password_form = CreatePasswordFormFromWebForm(generation_element_.form(), |
333 nullptr, nullptr); | 342 nullptr, nullptr); |
334 } else { | 343 } else { |
335 password_form = CreatePasswordFormFromUnownedInputElements( | 344 password_form = CreatePasswordFormFromUnownedInputElements( |
336 *render_frame()->GetWebFrame(), nullptr, nullptr); | 345 *render_frame()->GetWebFrame(), nullptr, nullptr); |
337 } | 346 } |
338 if (password_form) { | 347 if (password_form) { |
339 // TODO(kolos): when we are good in username detection, save username | 348 // TODO(kolos): when we are good in username detection, save username |
340 // as well. | 349 // as well. |
341 password_form->username_value = base::string16(); | 350 password_form->username_value = base::string16(); |
342 password_form->password_value = generation_element_.value(); | 351 password_form->password_value = generation_element_.value(); |
343 } | 352 } |
344 | 353 |
345 return password_form; | 354 return password_form; |
346 } | 355 } |
347 | 356 |
348 void PasswordGenerationAgent::FoundFormsEligibleForGeneration( | 357 void PasswordGenerationAgent::OnFormsEligibleForGenerationFound( |
349 const std::vector<PasswordFormGenerationData>& forms) { | 358 const std::vector<autofill::PasswordFormGenerationData>& forms) { |
350 generation_enabled_forms_.insert(generation_enabled_forms_.end(), | 359 generation_enabled_forms_.insert(generation_enabled_forms_.end(), |
351 forms.begin(), forms.end()); | 360 forms.begin(), forms.end()); |
352 DetermineGenerationElement(); | 361 DetermineGenerationElement(); |
353 } | 362 } |
354 | 363 |
355 void PasswordGenerationAgent::DetermineGenerationElement() { | 364 void PasswordGenerationAgent::DetermineGenerationElement() { |
356 if (generation_form_data_) { | 365 if (generation_form_data_) { |
357 VLOG(2) << "Account creation form already found"; | 366 VLOG(2) << "Account creation form already found"; |
358 return; | 367 return; |
359 } | 368 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 if (element.value().isEmpty()) { | 476 if (element.value().isEmpty()) { |
468 if (password_is_generated_) { | 477 if (password_is_generated_) { |
469 // User generated a password and then deleted it. | 478 // User generated a password and then deleted it. |
470 password_generation::LogPasswordGenerationEvent( | 479 password_generation::LogPasswordGenerationEvent( |
471 password_generation::PASSWORD_DELETED); | 480 password_generation::PASSWORD_DELETED); |
472 CopyElementValueToOtherInputElements(&element, | 481 CopyElementValueToOtherInputElements(&element, |
473 &generation_form_data_->password_elements); | 482 &generation_form_data_->password_elements); |
474 std::unique_ptr<PasswordForm> presaved_form( | 483 std::unique_ptr<PasswordForm> presaved_form( |
475 CreatePasswordFormToPresave()); | 484 CreatePasswordFormToPresave()); |
476 if (presaved_form) { | 485 if (presaved_form) { |
477 GetPasswordManagerDriver()->PasswordNoLongerGenerated(*presaved_form); | 486 Send(new AutofillHostMsg_PasswordNoLongerGenerated(routing_id(), |
| 487 *presaved_form)); |
478 } | 488 } |
479 } | 489 } |
480 | 490 |
481 // Do not treat the password as generated, either here or in the browser. | 491 // Do not treat the password as generated, either here or in the browser. |
482 password_is_generated_ = false; | 492 password_is_generated_ = false; |
483 generation_element_.setShouldRevealPassword(false); | 493 generation_element_.setShouldRevealPassword(false); |
484 | 494 |
485 // Offer generation again. | 495 // Offer generation again. |
486 ShowGenerationPopup(); | 496 ShowGenerationPopup(); |
487 } else if (password_is_generated_) { | 497 } else if (password_is_generated_) { |
488 password_edited_ = true; | 498 password_edited_ = true; |
489 // Mirror edits to any confirmation password fields. | 499 // Mirror edits to any confirmation password fields. |
490 CopyElementValueToOtherInputElements(&element, | 500 CopyElementValueToOtherInputElements(&element, |
491 &generation_form_data_->password_elements); | 501 &generation_form_data_->password_elements); |
492 std::unique_ptr<PasswordForm> presaved_form(CreatePasswordFormToPresave()); | 502 std::unique_ptr<PasswordForm> presaved_form(CreatePasswordFormToPresave()); |
493 if (presaved_form) { | 503 if (presaved_form) { |
494 GetPasswordManagerDriver()->PresaveGeneratedPassword(*presaved_form); | 504 Send(new AutofillHostMsg_PresaveGeneratedPassword(routing_id(), |
| 505 *presaved_form)); |
495 } | 506 } |
496 } else if (element.value().length() > kMaximumOfferSize) { | 507 } else if (element.value().length() > kMaximumOfferSize) { |
497 // User has rejected the feature and has started typing a password. | 508 // User has rejected the feature and has started typing a password. |
498 HidePopup(); | 509 HidePopup(); |
499 } else { | 510 } else { |
500 // Password isn't generated and there are fewer than kMaximumOfferSize | 511 // Password isn't generated and there are fewer than kMaximumOfferSize |
501 // characters typed, so keep offering the password. Note this function | 512 // characters typed, so keep offering the password. Note this function |
502 // will just keep the previous popup if one is already showing. | 513 // will just keep the previous popup if one is already showing. |
503 ShowGenerationPopup(); | 514 ShowGenerationPopup(); |
504 } | 515 } |
(...skipping 23 matching lines...) Expand all Loading... |
528 render_frame()->GetRenderView()->ElementBoundsInWindow( | 539 render_frame()->GetRenderView()->ElementBoundsInWindow( |
529 generation_element_), | 540 generation_element_), |
530 *generation_form_data_->form)); | 541 *generation_form_data_->form)); |
531 editing_popup_shown_ = true; | 542 editing_popup_shown_ = true; |
532 } | 543 } |
533 | 544 |
534 void PasswordGenerationAgent::HidePopup() { | 545 void PasswordGenerationAgent::HidePopup() { |
535 Send(new AutofillHostMsg_HidePasswordGenerationPopup(routing_id())); | 546 Send(new AutofillHostMsg_HidePasswordGenerationPopup(routing_id())); |
536 } | 547 } |
537 | 548 |
538 void PasswordGenerationAgent::UserTriggeredGeneratePassword() { | 549 void PasswordGenerationAgent::OnUserTriggeredGeneratePassword() { |
539 if (last_focused_password_element_.isNull() || !render_frame()) | 550 if (last_focused_password_element_.isNull() || !render_frame()) |
540 return; | 551 return; |
541 | 552 |
542 blink::WebFormElement form = last_focused_password_element_.form(); | 553 blink::WebFormElement form = last_focused_password_element_.form(); |
543 std::unique_ptr<PasswordForm> password_form; | 554 std::unique_ptr<PasswordForm> password_form; |
544 std::vector<blink::WebFormControlElement> control_elements; | 555 std::vector<blink::WebFormControlElement> control_elements; |
545 if (!form.isNull()) { | 556 if (!form.isNull()) { |
546 password_form = CreatePasswordFormFromWebForm(form, nullptr, nullptr); | 557 password_form = CreatePasswordFormFromWebForm(form, nullptr, nullptr); |
547 control_elements = form_util::ExtractAutofillableElementsInForm(form); | 558 control_elements = form_util::ExtractAutofillableElementsInForm(form); |
548 } else { | 559 } else { |
(...skipping 14 matching lines...) Expand all Loading... |
563 std::vector<blink::WebInputElement> password_elements; | 574 std::vector<blink::WebInputElement> password_elements; |
564 GetAccountCreationPasswordFields(control_elements, &password_elements); | 575 GetAccountCreationPasswordFields(control_elements, &password_elements); |
565 password_elements = FindPasswordElementsForGeneration( | 576 password_elements = FindPasswordElementsForGeneration( |
566 password_elements, last_focused_password_element_.nameForAutofill()); | 577 password_elements, last_focused_password_element_.nameForAutofill()); |
567 generation_form_data_.reset(new AccountCreationFormData( | 578 generation_form_data_.reset(new AccountCreationFormData( |
568 make_linked_ptr(password_form.release()), password_elements)); | 579 make_linked_ptr(password_form.release()), password_elements)); |
569 is_manually_triggered_ = true; | 580 is_manually_triggered_ = true; |
570 ShowGenerationPopup(); | 581 ShowGenerationPopup(); |
571 } | 582 } |
572 | 583 |
573 const mojom::PasswordManagerDriverPtr& | |
574 PasswordGenerationAgent::GetPasswordManagerDriver() { | |
575 DCHECK(password_agent_); | |
576 return password_agent_->GetPasswordManagerDriver(); | |
577 } | |
578 | |
579 } // namespace autofill | 584 } // namespace autofill |
OLD | NEW |