Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: components/autofill/content/renderer/password_generation_agent.cc

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

Powered by Google App Engine
This is Rietveld 408576698