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_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 Loading... | |
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: use more signals than just origin to compare. | |
Ilya Sherman
2013/09/03 23:58:01
nit: TODO(zysxqn):
zysxqn
2013/09/04 17:26:20
Done.
| |
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 } | |
Ilya Sherman
2013/09/03 23:58:01
nit: No need for curly braces.
zysxqn
2013/09/04 17:26:20
Done.
| |
87 } | |
88 return false; | |
89 } | |
90 | |
67 } // namespace | 91 } // namespace |
68 | 92 |
69 PasswordGenerationManager::PasswordGenerationManager( | 93 PasswordGenerationManager::PasswordGenerationManager( |
70 content::RenderView* render_view) | 94 content::RenderView* render_view) |
71 : content::RenderViewObserver(render_view), | 95 : content::RenderViewObserver(render_view), |
72 render_view_(render_view), | 96 render_view_(render_view), |
73 enabled_(false) { | 97 enabled_(false) { |
74 render_view_->GetWebView()->setPasswordGeneratorClient(this); | 98 render_view_->GetWebView()->setPasswordGeneratorClient(this); |
75 } | 99 } |
76 PasswordGenerationManager::~PasswordGenerationManager() {} | 100 PasswordGenerationManager::~PasswordGenerationManager() {} |
77 | 101 |
78 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { | 102 void PasswordGenerationManager::DidFinishDocumentLoad(WebKit::WebFrame* frame) { |
79 // In every navigation, the IPC message sent by the password autofill manager | 103 // 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 | 104 // 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 | 105 // 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 | 106 // 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 | 107 // 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 | 108 // 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. | 109 // form, but there could be multiple password forms in each frame. |
86 if (!frame->parent()) { | 110 if (!frame->parent()) { |
87 not_blacklisted_password_form_origins_.clear(); | 111 not_blacklisted_password_form_origins_.clear(); |
88 // Initialize to an empty and invalid GURL. | 112 account_creation_forms_.clear(); |
89 account_creation_form_origin_ = GURL(); | 113 possible_account_creation_form_.reset(new content::PasswordForm()); |
90 passwords_.clear(); | 114 passwords_.clear(); |
91 } | 115 } |
92 } | 116 } |
93 | 117 |
94 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) { | 118 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) { |
95 // We don't want to generate passwords if the browser won't store or sync | 119 // We don't want to generate passwords if the browser won't store or sync |
96 // them. | 120 // them. |
97 if (!enabled_) | 121 if (!enabled_) |
98 return; | 122 return; |
99 | 123 |
(...skipping 20 matching lines...) Expand all Loading... | |
120 GURL realm(password_form->signon_realm); | 144 GURL realm(password_form->signon_realm); |
121 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm())) | 145 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm())) |
122 continue; | 146 continue; |
123 | 147 |
124 std::vector<WebKit::WebInputElement> passwords; | 148 std::vector<WebKit::WebInputElement> passwords; |
125 if (GetAccountCreationPasswordFields(forms[i], &passwords)) { | 149 if (GetAccountCreationPasswordFields(forms[i], &passwords)) { |
126 DVLOG(2) << "Account creation form detected"; | 150 DVLOG(2) << "Account creation form detected"; |
127 password_generation::LogPasswordGenerationEvent( | 151 password_generation::LogPasswordGenerationEvent( |
128 password_generation::SIGN_UP_DETECTED); | 152 password_generation::SIGN_UP_DETECTED); |
129 passwords_ = passwords; | 153 passwords_ = passwords; |
130 account_creation_form_origin_ = password_form->origin; | 154 possible_account_creation_form_.swap(password_form); |
131 MaybeShowIcon(); | 155 MaybeShowIcon(); |
132 // We assume that there is only one account creation field per URL. | 156 // We assume that there is only one account creation field per URL. |
133 return; | 157 return; |
134 } | 158 } |
135 } | 159 } |
136 password_generation::LogPasswordGenerationEvent( | 160 password_generation::LogPasswordGenerationEvent( |
137 password_generation::NO_SIGN_UP_DETECTED); | 161 password_generation::NO_SIGN_UP_DETECTED); |
138 } | 162 } |
139 | 163 |
140 bool PasswordGenerationManager::ShouldAnalyzeDocument( | 164 bool PasswordGenerationManager::ShouldAnalyzeDocument( |
(...skipping 28 matching lines...) Expand all Loading... | |
169 | 193 |
170 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { | 194 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { |
171 bool handled = true; | 195 bool handled = true; |
172 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) | 196 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) |
173 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, | 197 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, |
174 OnFormNotBlacklisted) | 198 OnFormNotBlacklisted) |
175 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, | 199 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, |
176 OnPasswordAccepted) | 200 OnPasswordAccepted) |
177 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, | 201 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, |
178 OnPasswordGenerationEnabled) | 202 OnPasswordGenerationEnabled) |
203 IPC_MESSAGE_HANDLER(AutofillMsg_AccountCreationFormsDetected, | |
204 OnAccountCreationFormsDetected) | |
179 IPC_MESSAGE_UNHANDLED(handled = false) | 205 IPC_MESSAGE_UNHANDLED(handled = false) |
180 IPC_END_MESSAGE_MAP() | 206 IPC_END_MESSAGE_MAP() |
181 return handled; | 207 return handled; |
182 } | 208 } |
183 | 209 |
184 void PasswordGenerationManager::OnFormNotBlacklisted( | 210 void PasswordGenerationManager::OnFormNotBlacklisted( |
185 const content::PasswordForm& form) { | 211 const content::PasswordForm& form) { |
186 not_blacklisted_password_form_origins_.push_back(form.origin); | 212 not_blacklisted_password_form_origins_.push_back(form.origin); |
187 MaybeShowIcon(); | 213 MaybeShowIcon(); |
188 } | 214 } |
189 | 215 |
190 void PasswordGenerationManager::OnPasswordAccepted( | 216 void PasswordGenerationManager::OnPasswordAccepted( |
191 const base::string16& password) { | 217 const base::string16& password) { |
192 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); | 218 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); |
193 it != passwords_.end(); ++it) { | 219 it != passwords_.end(); ++it) { |
194 it->setValue(password); | 220 it->setValue(password); |
195 it->setAutofilled(true); | 221 it->setAutofilled(true); |
196 // Advance focus to the next input field. We assume password fields in | 222 // Advance focus to the next input field. We assume password fields in |
197 // an account creation form are always adjacent. | 223 // an account creation form are always adjacent. |
198 render_view_->GetWebView()->advanceFocus(false); | 224 render_view_->GetWebView()->advanceFocus(false); |
199 } | 225 } |
200 } | 226 } |
201 | 227 |
202 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { | 228 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { |
203 enabled_ = enabled; | 229 enabled_ = enabled; |
204 } | 230 } |
205 | 231 |
232 void PasswordGenerationManager::OnAccountCreationFormsDetected( | |
233 const std::vector<autofill::FormData>& forms) { | |
234 account_creation_forms_.insert( | |
235 account_creation_forms_.begin(), forms.begin(), forms.end()); | |
Ilya Sherman
2013/09/03 23:58:01
nit: Shouldn't be a big deal in practice, but it's
zysxqn
2013/09/04 17:26:20
Done.
| |
236 MaybeShowIcon(); | |
237 } | |
238 | |
206 void PasswordGenerationManager::MaybeShowIcon() { | 239 void PasswordGenerationManager::MaybeShowIcon() { |
207 // We should show the password generation icon only when we have detected | 240 // 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 | 241 // account creation form, we have confirmed from browser that this form |
209 // is not blacklisted by the users. | 242 // is not blacklisted by the users, and the Autofill server has marked one |
210 if (!account_creation_form_origin_.is_valid() || | 243 // of its field as ACCOUNT_CREATION_PASSWORD. |
244 if (!possible_account_creation_form_.get() || | |
211 passwords_.empty() || | 245 passwords_.empty() || |
212 not_blacklisted_password_form_origins_.empty()) { | 246 not_blacklisted_password_form_origins_.empty() || |
247 account_creation_forms_.empty()) { | |
213 return; | 248 return; |
214 } | 249 } |
215 | 250 |
216 for (std::vector<GURL>::iterator it = | 251 if (!ContainsURL(not_blacklisted_password_form_origins_, |
217 not_blacklisted_password_form_origins_.begin(); | 252 possible_account_creation_form_->origin)) { |
218 it != not_blacklisted_password_form_origins_.end(); ++it) { | 253 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 } | 254 } |
255 | |
256 if (!ContainsForm(account_creation_forms_, | |
257 *possible_account_creation_form_)) { | |
258 return; | |
259 } | |
260 | |
261 passwords_[0].passwordGeneratorButtonElement().setAttribute("style", | |
262 "display:block"); | |
263 password_generation::LogPasswordGenerationEvent( | |
264 password_generation::ICON_SHOWN); | |
227 } | 265 } |
228 | 266 |
229 } // namespace autofill | 267 } // namespace autofill |
OLD | NEW |