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

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: 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"
(...skipping 10 matching lines...) Expand all
21 namespace autofill { 21 namespace autofill {
22 22
23 PasswordGenerationManager::PasswordGenerationManager( 23 PasswordGenerationManager::PasswordGenerationManager(
24 content::RenderView* render_view) 24 content::RenderView* render_view)
25 : content::RenderViewObserver(render_view), 25 : content::RenderViewObserver(render_view),
26 enabled_(false) { 26 enabled_(false) {
27 render_view->GetWebView()->addTextFieldDecoratorClient(this); 27 render_view->GetWebView()->addTextFieldDecoratorClient(this);
28 } 28 }
29 PasswordGenerationManager::~PasswordGenerationManager() {} 29 PasswordGenerationManager::~PasswordGenerationManager() {}
30 30
31 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { 31 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) {
32 // Clear previous state.
33 passwords_.clear();
34
32 // We don't want to generate passwords if the browser won't store or sync 35 // We don't want to generate passwords if the browser won't store or sync
33 // them. 36 // them.
34 if (!enabled_) 37 if (!enabled_)
35 return; 38 return;
36 39
37 if (!ShouldAnalyzeDocument(frame->document())) 40 if (!ShouldAnalyzeDocument(frame->document()))
38 return; 41 return;
39 42
40 WebKit::WebVector<WebKit::WebFormElement> forms; 43 WebKit::WebVector<WebKit::WebFormElement> forms;
41 frame->document().forms(forms); 44 frame->document().forms(forms);
42 for (size_t i = 0; i < forms.size(); ++i) { 45 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; 46 std::vector<WebKit::WebInputElement> passwords;
52 for (size_t i = 0; i < control_elements.size(); i++) { 47 if (IsAccountCreationForm(forms[i], &passwords)) {
53 WebKit::WebInputElement* input_element = 48 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; 49 passwords_ = passwords;
65 // Make the decoration visible for this element. 50 // Make the decoration visible for this element.
66 passwords[0].decorationElementFor(this).setAttribute("style", 51 passwords[0].decorationElementFor(this).setAttribute("style",
67 "display:block"); 52 "display:block");
53 // We assume that there is only one account creation field per URL.
68 return; 54 return;
69 } 55 }
70 } 56 }
71 } 57 }
72 58
73 bool PasswordGenerationManager::ShouldAnalyzeDocument( 59 bool PasswordGenerationManager::ShouldAnalyzeDocument(
74 const WebKit::WebDocument& document) const { 60 const WebKit::WebDocument& document) const {
75 // Make sure that this security origin is allowed to use password manager. 61 // 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. 62 // Generating a password that can't be saved is a bad idea.
77 WebKit::WebSecurityOrigin origin = document.securityOrigin(); 63 WebKit::WebSecurityOrigin origin = document.securityOrigin();
78 if (!origin.canAccessPasswordManager()) { 64 if (!origin.canAccessPasswordManager()) {
79 DVLOG(1) << "No PasswordManager access"; 65 DVLOG(1) << "No PasswordManager access";
80 return false; 66 return false;
81 } 67 }
82 68
83 return true; 69 return true;
84 } 70 }
85 71
72 bool PasswordGenerationManager::IsAccountCreationForm(
73 const WebKit::WebFormElement& form,
74 std::vector<WebKit::WebInputElement>* passwords) {
75 // Ignore forms with autocomplete turned off for now. We may remove this in
76 // the future, as we only want to avoid creating passwords if the signin
77 // form has autocomplete turned off.
78 if (form.isNull() || !form.autoComplete())
79 return false;
80
81 // If we can't get a valid PasswordForm, we skip this form because the
82 // the password won't get saved even if we generate it.
83 webkit::forms::PasswordForm* password_form(
84 webkit::forms::PasswordFormDomManager::CreatePasswordForm(form));
85 if (!password_form) {
86 DVLOG(2) << "Invalid action on form";
87 return false;
88 }
89
90 // Grab all of the passwords for the form.
91 WebKit::WebVector<WebKit::WebFormControlElement> control_elements;
92 form.getFormControlElements(control_elements);
93
94 for (size_t i = 0; i < control_elements.size(); i++) {
95 WebKit::WebInputElement* input_element =
96 toWebInputElement(&control_elements[i]);
97 // Only pay attention to visible password fields.
98 if (input_element &&
99 input_element->isPasswordField() &&
100 input_element->hasNonEmptyBoundingBox())
101 passwords->push_back(*input_element);
Ilya Sherman 2012/06/08 05:22:18 nit: We usually do include curly braces for if-stm
Garrett Casto 2012/06/08 20:15:09 Done.
102 }
103
104 // For now, just assume that if there are two password fields in the
105 // form that this is meant for account creation.
106 // TODO(gcasto): Determine better heauristics for this.
107 if (passwords->size() == 2)
108 return true;
109
110 return false;
111 }
112
86 bool PasswordGenerationManager::shouldAddDecorationTo( 113 bool PasswordGenerationManager::shouldAddDecorationTo(
87 const WebKit::WebInputElement& element) { 114 const WebKit::WebInputElement& element) {
88 return element.isPasswordField(); 115 return element.isPasswordField();
89 } 116 }
90 117
91 bool PasswordGenerationManager::visibleByDefault() { 118 bool PasswordGenerationManager::visibleByDefault() {
92 return false; 119 return false;
93 } 120 }
94 121
95 WebKit::WebCString PasswordGenerationManager::imageNameForNormalState() { 122 WebKit::WebCString PasswordGenerationManager::imageNameForNormalState() {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 it->setValue(password); 167 it->setValue(password);
141 it->setAutofilled(true); 168 it->setAutofilled(true);
142 } 169 }
143 } 170 }
144 171
145 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { 172 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) {
146 enabled_ = enabled; 173 enabled_ = enabled;
147 } 174 }
148 175
149 } // namespace autofill 176 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698