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

Side by Side Diff: chrome/renderer/autofill/password_generation_manager.cc

Issue 10540069: Refactor detection of account creation forms and fix two minor issues. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 8 years, 6 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/autofill/password_generation_manager.h" 5 #include "chrome/renderer/autofill/password_generation_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/common/autofill_messages.h" 8 #include "chrome/common/autofill_messages.h"
9 #include "content/public/renderer/render_view.h" 9 #include "content/public/renderer/render_view.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h " 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h "
17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h"
19 #include "ui/gfx/rect.h" 19 #include "ui/gfx/rect.h"
20 20
21 namespace autofill { 21 namespace autofill {
22 22
23 namespace {
24
25 // Returns true if we think that this form is for account creation. |passwords|
26 // is filled with the password field(s) in the form.
27 bool GetAccountCreationPasswordFields(
28 const WebKit::WebFormElement& form,
29 std::vector<WebKit::WebInputElement>* passwords) {
30 // Ignore forms with autocomplete turned off for now. We may remove this in
31 // the future, as we only want to avoid creating passwords if the signin
32 // form has autocomplete turned off.
33 if (form.isNull() || !form.autoComplete())
34 return false;
35
36 // If we can't get a valid PasswordForm, we skip this form because the
37 // the password won't get saved even if we generate it.
38 webkit::forms::PasswordForm* password_form(
39 webkit::forms::PasswordFormDomManager::CreatePasswordForm(form));
40 if (!password_form) {
41 DVLOG(2) << "Invalid action on form";
42 return false;
43 }
44
45 // Grab all of the passwords for the form.
46 WebKit::WebVector<WebKit::WebFormControlElement> control_elements;
47 form.getFormControlElements(control_elements);
48
49 for (size_t i = 0; i < control_elements.size(); i++) {
50 WebKit::WebInputElement* input_element =
51 toWebInputElement(&control_elements[i]);
52 // Only pay attention to visible password fields.
53 if (input_element &&
54 input_element->isPasswordField() &&
55 input_element->hasNonEmptyBoundingBox()) {
56 passwords->push_back(*input_element);
57 }
58 }
59
60 // For now, just assume that if there are two password fields in the
61 // form that this is meant for account creation.
62 // TODO(gcasto): Determine better heauristics for this.
63 if (passwords->size() == 2)
64 return true;
65
66 return false;
67 }
68
69 } // namespace
70
23 PasswordGenerationManager::PasswordGenerationManager( 71 PasswordGenerationManager::PasswordGenerationManager(
24 content::RenderView* render_view) 72 content::RenderView* render_view)
25 : content::RenderViewObserver(render_view), 73 : content::RenderViewObserver(render_view),
26 enabled_(false) { 74 enabled_(false) {
27 render_view->GetWebView()->addTextFieldDecoratorClient(this); 75 render_view->GetWebView()->addTextFieldDecoratorClient(this);
28 } 76 }
29 PasswordGenerationManager::~PasswordGenerationManager() {} 77 PasswordGenerationManager::~PasswordGenerationManager() {}
30 78
31 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { 79 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) {
80 // Clear previous state.
81 passwords_.clear();
82
32 // We don't want to generate passwords if the browser won't store or sync 83 // We don't want to generate passwords if the browser won't store or sync
33 // them. 84 // them.
34 if (!enabled_) 85 if (!enabled_)
35 return; 86 return;
36 87
37 if (!ShouldAnalyzeDocument(frame->document())) 88 if (!ShouldAnalyzeDocument(frame->document()))
38 return; 89 return;
39 90
40 WebKit::WebVector<WebKit::WebFormElement> forms; 91 WebKit::WebVector<WebKit::WebFormElement> forms;
41 frame->document().forms(forms); 92 frame->document().forms(forms);
42 for (size_t i = 0; i < forms.size(); ++i) { 93 for (size_t i = 0; i < forms.size(); ++i) {
43 const WebKit::WebFormElement& web_form = forms[i];
44 if (web_form.isNull() || !web_form.autoComplete())
45 continue;
46
47 // Grab all of the passwords for each form.
48 WebKit::WebVector<WebKit::WebFormControlElement> control_elements;
49 web_form.getFormControlElements(control_elements);
50
51 std::vector<WebKit::WebInputElement> passwords; 94 std::vector<WebKit::WebInputElement> passwords;
52 for (size_t i = 0; i < control_elements.size(); i++) { 95 if (GetAccountCreationPasswordFields(forms[i], &passwords)) {
53 WebKit::WebInputElement* input_element = 96 DVLOG(2) << "Account creation form detected";
54 toWebInputElement(&control_elements[i]);
55 if (input_element && input_element->isPasswordField())
56 passwords.push_back(*input_element);
57 }
58
59 // For now, just assume that if there are two password fields in the
60 // form that this is meant for account creation. Also, we assume that there
61 // is only one account creation field per URL.
62 // TODO(gcasto): Determine better heauristics for this.
63 if (passwords.size() == 2) {
64 passwords_ = passwords; 97 passwords_ = passwords;
65 // Make the decoration visible for this element. 98 // Make the decoration visible for this element.
66 passwords[0].decorationElementFor(this).setAttribute("style", 99 passwords[0].decorationElementFor(this).setAttribute("style",
67 "display:block"); 100 "display:block");
101 // We assume that there is only one account creation field per URL.
68 return; 102 return;
69 } 103 }
70 } 104 }
71 } 105 }
72 106
73 bool PasswordGenerationManager::ShouldAnalyzeDocument( 107 bool PasswordGenerationManager::ShouldAnalyzeDocument(
74 const WebKit::WebDocument& document) const { 108 const WebKit::WebDocument& document) const {
75 // Make sure that this security origin is allowed to use password manager. 109 // Make sure that this security origin is allowed to use password manager.
76 // Generating a password that can't be saved is a bad idea. 110 // Generating a password that can't be saved is a bad idea.
77 WebKit::WebSecurityOrigin origin = document.securityOrigin(); 111 WebKit::WebSecurityOrigin origin = document.securityOrigin();
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 it->setValue(password); 174 it->setValue(password);
141 it->setAutofilled(true); 175 it->setAutofilled(true);
142 } 176 }
143 } 177 }
144 178
145 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { 179 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) {
146 enabled_ = enabled; 180 enabled_ = enabled;
147 } 181 }
148 182
149 } // namespace autofill 183 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698