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

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

Issue 23432002: Generate passwords only for forms that autofill server marks as account creation forms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address llya's nits and fix a bug in form structure unit test. Created 7 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_manager.h" 5 #include "components/autofill/content/renderer/password_generation_manager.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
8 #include "components/autofill/core/common/autofill_messages.h" 9 #include "components/autofill/core/common/autofill_messages.h"
10 #include "components/autofill/core/common/form_data.h"
9 #include "components/autofill/core/common/password_generation_util.h" 11 #include "components/autofill/core/common/password_generation_util.h"
10 #include "content/public/renderer/password_form_conversion_utils.h" 12 #include "content/public/renderer/password_form_conversion_utils.h"
11 #include "content/public/renderer/render_view.h" 13 #include "content/public/renderer/render_view.h"
12 #include "google_apis/gaia/gaia_urls.h" 14 #include "google_apis/gaia/gaia_urls.h"
13 #include "third_party/WebKit/public/platform/WebCString.h" 15 #include "third_party/WebKit/public/platform/WebCString.h"
14 #include "third_party/WebKit/public/platform/WebRect.h" 16 #include "third_party/WebKit/public/platform/WebRect.h"
15 #include "third_party/WebKit/public/platform/WebVector.h" 17 #include "third_party/WebKit/public/platform/WebVector.h"
16 #include "third_party/WebKit/public/web/WebDocument.h" 18 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebFormElement.h" 19 #include "third_party/WebKit/public/web/WebFormElement.h"
18 #include "third_party/WebKit/public/web/WebFrame.h" 20 #include "third_party/WebKit/public/web/WebFrame.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
57 // to fill those. 59 // to fill those.
58 if (passwords->size() > 2) 60 if (passwords->size() > 2)
59 passwords->resize(2); 61 passwords->resize(2);
60 62
61 return true; 63 return true;
62 } 64 }
63 65
64 return false; 66 return false;
65 } 67 }
66 68
69 bool ContainsURL(const std::vector<GURL>& urls, const GURL& url) {
70 return std::find(urls.begin(), urls.end(), url) != urls.end();
71 }
72
73 // Returns true if the |form1| is essentially equal to |form2|.
74 bool FormEquals(const autofill::FormData& form1,
75 const content::PasswordForm& form2) {
76 // TODO(zysxqn): use more signals than just origin to compare.
77 return form1.origin == form2.origin;
78 }
79
80 bool ContainsForm(const std::vector<autofill::FormData>& forms,
81 const content::PasswordForm& form) {
82 for (std::vector<autofill::FormData>::const_iterator it =
83 forms.begin(); it != forms.end(); ++it) {
84 if (FormEquals(*it, form))
85 return true;
86 }
87 return false;
88 }
89
67 } // namespace 90 } // namespace
68 91
69 PasswordGenerationManager::PasswordGenerationManager( 92 PasswordGenerationManager::PasswordGenerationManager(
70 content::RenderView* render_view) 93 content::RenderView* render_view)
71 : content::RenderViewObserver(render_view), 94 : content::RenderViewObserver(render_view),
72 render_view_(render_view), 95 render_view_(render_view),
73 enabled_(false) { 96 enabled_(false) {
74 render_view_->GetWebView()->setPasswordGeneratorClient(this); 97 render_view_->GetWebView()->setPasswordGeneratorClient(this);
75 } 98 }
76 PasswordGenerationManager::~PasswordGenerationManager() {} 99 PasswordGenerationManager::~PasswordGenerationManager() {}
77 100
78 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { 101 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) {
79 // In every navigation, the IPC message sent by the password autofill manager 102 // In every navigation, the IPC message sent by the password autofill manager
80 // to query whether the current form is blacklisted or not happens when the 103 // to query whether the current form is blacklisted or not happens when the
81 // document load finishes, so we need to clear previous states here before we 104 // document load finishes, so we need to clear previous states here before we
82 // hear back from the browser. We only clear this state on main frame load 105 // hear back from the browser. We only clear this state on main frame load
83 // as we don't want subframe loads to clear state that we have recieved from 106 // as we don't want subframe loads to clear state that we have recieved from
84 // the main frame. Note that we assume there is only one account creation 107 // the main frame. Note that we assume there is only one account creation
85 // form, but there could be multiple password forms in each frame. 108 // form, but there could be multiple password forms in each frame.
86 if (!frame->parent()) { 109 if (!frame->parent()) {
Garrett Casto 2013/09/04 22:03:30 One thing that would be really nice would be some
zysxqn 2013/09/04 22:17:50 Done.
87 not_blacklisted_password_form_origins_.clear(); 110 not_blacklisted_password_form_origins_.clear();
88 // Initialize to an empty and invalid GURL. 111 account_creation_forms_.clear();
89 account_creation_form_origin_ = GURL(); 112 possible_account_creation_form_.reset(new content::PasswordForm());
90 passwords_.clear(); 113 passwords_.clear();
91 } 114 }
92 } 115 }
93 116
94 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) { 117 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) {
95 // We don't want to generate passwords if the browser won't store or sync 118 // We don't want to generate passwords if the browser won't store or sync
96 // them. 119 // them.
97 if (!enabled_) 120 if (!enabled_)
98 return; 121 return;
99 122
(...skipping 20 matching lines...) Expand all
120 GURL realm(password_form->signon_realm); 143 GURL realm(password_form->signon_realm);
121 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm())) 144 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm()))
122 continue; 145 continue;
123 146
124 std::vector<WebKit::WebInputElement> passwords; 147 std::vector<WebKit::WebInputElement> passwords;
125 if (GetAccountCreationPasswordFields(forms[i], &passwords)) { 148 if (GetAccountCreationPasswordFields(forms[i], &passwords)) {
126 DVLOG(2) << "Account creation form detected"; 149 DVLOG(2) << "Account creation form detected";
127 password_generation::LogPasswordGenerationEvent( 150 password_generation::LogPasswordGenerationEvent(
128 password_generation::SIGN_UP_DETECTED); 151 password_generation::SIGN_UP_DETECTED);
129 passwords_ = passwords; 152 passwords_ = passwords;
130 account_creation_form_origin_ = password_form->origin; 153 possible_account_creation_form_.swap(password_form);
131 MaybeShowIcon(); 154 MaybeShowIcon();
132 // We assume that there is only one account creation field per URL. 155 // We assume that there is only one account creation field per URL.
133 return; 156 return;
134 } 157 }
135 } 158 }
136 password_generation::LogPasswordGenerationEvent( 159 password_generation::LogPasswordGenerationEvent(
137 password_generation::NO_SIGN_UP_DETECTED); 160 password_generation::NO_SIGN_UP_DETECTED);
138 } 161 }
139 162
140 bool PasswordGenerationManager::ShouldAnalyzeDocument( 163 bool PasswordGenerationManager::ShouldAnalyzeDocument(
(...skipping 28 matching lines...) Expand all
169 192
170 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { 193 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) {
171 bool handled = true; 194 bool handled = true;
172 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) 195 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message)
173 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, 196 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted,
174 OnFormNotBlacklisted) 197 OnFormNotBlacklisted)
175 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, 198 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted,
176 OnPasswordAccepted) 199 OnPasswordAccepted)
177 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, 200 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled,
178 OnPasswordGenerationEnabled) 201 OnPasswordGenerationEnabled)
202 IPC_MESSAGE_HANDLER(AutofillMsg_AccountCreationFormsDetected,
203 OnAccountCreationFormsDetected)
179 IPC_MESSAGE_UNHANDLED(handled = false) 204 IPC_MESSAGE_UNHANDLED(handled = false)
180 IPC_END_MESSAGE_MAP() 205 IPC_END_MESSAGE_MAP()
181 return handled; 206 return handled;
182 } 207 }
183 208
184 void PasswordGenerationManager::OnFormNotBlacklisted( 209 void PasswordGenerationManager::OnFormNotBlacklisted(
185 const content::PasswordForm& form) { 210 const content::PasswordForm& form) {
186 not_blacklisted_password_form_origins_.push_back(form.origin); 211 not_blacklisted_password_form_origins_.push_back(form.origin);
187 MaybeShowIcon(); 212 MaybeShowIcon();
188 } 213 }
189 214
190 void PasswordGenerationManager::OnPasswordAccepted( 215 void PasswordGenerationManager::OnPasswordAccepted(
191 const base::string16& password) { 216 const base::string16& password) {
192 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); 217 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin();
193 it != passwords_.end(); ++it) { 218 it != passwords_.end(); ++it) {
194 it->setValue(password); 219 it->setValue(password);
195 it->setAutofilled(true); 220 it->setAutofilled(true);
196 // Advance focus to the next input field. We assume password fields in 221 // Advance focus to the next input field. We assume password fields in
197 // an account creation form are always adjacent. 222 // an account creation form are always adjacent.
198 render_view_->GetWebView()->advanceFocus(false); 223 render_view_->GetWebView()->advanceFocus(false);
199 } 224 }
200 } 225 }
201 226
202 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { 227 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) {
203 enabled_ = enabled; 228 enabled_ = enabled;
204 } 229 }
205 230
231 void PasswordGenerationManager::OnAccountCreationFormsDetected(
232 const std::vector<autofill::FormData>& forms) {
233 account_creation_forms_.insert(
234 account_creation_forms_.end(), forms.begin(), forms.end());
235 MaybeShowIcon();
236 }
237
206 void PasswordGenerationManager::MaybeShowIcon() { 238 void PasswordGenerationManager::MaybeShowIcon() {
207 // We should show the password generation icon only when we have detected 239 // We should show the password generation icon only when we have detected
208 // account creation form and we have confirmed from browser that this form 240 // account creation form, we have confirmed from browser that this form
209 // is not blacklisted by the users. 241 // is not blacklisted by the users, and the Autofill server has marked one
210 if (!account_creation_form_origin_.is_valid() || 242 // of its field as ACCOUNT_CREATION_PASSWORD.
243 if (!possible_account_creation_form_.get() ||
211 passwords_.empty() || 244 passwords_.empty() ||
212 not_blacklisted_password_form_origins_.empty()) { 245 not_blacklisted_password_form_origins_.empty() ||
246 account_creation_forms_.empty()) {
213 return; 247 return;
214 } 248 }
215 249
216 for (std::vector<GURL>::iterator it = 250 if (!ContainsURL(not_blacklisted_password_form_origins_,
217 not_blacklisted_password_form_origins_.begin(); 251 possible_account_creation_form_->origin)) {
218 it != not_blacklisted_password_form_origins_.end(); ++it) { 252 return;
219 if (*it == account_creation_form_origin_) {
220 passwords_[0].passwordGeneratorButtonElement().setAttribute("style",
221 "display:block");
222 password_generation::LogPasswordGenerationEvent(
223 password_generation::ICON_SHOWN);
224 return;
225 }
226 } 253 }
254
255 if (!ContainsForm(account_creation_forms_,
256 *possible_account_creation_form_)) {
257 return;
258 }
259
260 passwords_[0].passwordGeneratorButtonElement().setAttribute("style",
261 "display:block");
262 password_generation::LogPasswordGenerationEvent(
263 password_generation::ICON_SHOWN);
227 } 264 }
228 265
229 } // namespace autofill 266 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698