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/content/renderer/password_form_conversion_utils.h" | 9 #include "components/autofill/content/renderer/password_form_conversion_utils.h" |
9 #include "components/autofill/core/common/autofill_messages.h" | 10 #include "components/autofill/core/common/autofill_messages.h" |
| 11 #include "components/autofill/core/common/form_data.h" |
10 #include "components/autofill/core/common/password_generation_util.h" | 12 #include "components/autofill/core/common/password_generation_util.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" |
19 #include "third_party/WebKit/public/web/WebInputElement.h" | 21 #include "third_party/WebKit/public/web/WebInputElement.h" |
(...skipping 37 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(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. |
| 109 // |
| 110 // TODO(zysxqn): Add stat when local heuristic fires but we don't show the |
| 111 // password generation icon. |
86 if (!frame->parent()) { | 112 if (!frame->parent()) { |
87 not_blacklisted_password_form_origins_.clear(); | 113 not_blacklisted_password_form_origins_.clear(); |
88 // Initialize to an empty and invalid GURL. | 114 account_creation_forms_.clear(); |
89 account_creation_form_origin_ = GURL(); | 115 possible_account_creation_form_.reset(new content::PasswordForm()); |
90 passwords_.clear(); | 116 passwords_.clear(); |
91 } | 117 } |
92 } | 118 } |
93 | 119 |
94 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) { | 120 void PasswordGenerationManager::DidFinishLoad(WebKit::WebFrame* frame) { |
95 // We don't want to generate passwords if the browser won't store or sync | 121 // We don't want to generate passwords if the browser won't store or sync |
96 // them. | 122 // them. |
97 if (!enabled_) | 123 if (!enabled_) |
98 return; | 124 return; |
99 | 125 |
(...skipping 20 matching lines...) Expand all Loading... |
120 GURL realm(password_form->signon_realm); | 146 GURL realm(password_form->signon_realm); |
121 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm())) | 147 if (realm == GURL(GaiaUrls::GetInstance()->gaia_login_form_realm())) |
122 continue; | 148 continue; |
123 | 149 |
124 std::vector<WebKit::WebInputElement> passwords; | 150 std::vector<WebKit::WebInputElement> passwords; |
125 if (GetAccountCreationPasswordFields(forms[i], &passwords)) { | 151 if (GetAccountCreationPasswordFields(forms[i], &passwords)) { |
126 DVLOG(2) << "Account creation form detected"; | 152 DVLOG(2) << "Account creation form detected"; |
127 password_generation::LogPasswordGenerationEvent( | 153 password_generation::LogPasswordGenerationEvent( |
128 password_generation::SIGN_UP_DETECTED); | 154 password_generation::SIGN_UP_DETECTED); |
129 passwords_ = passwords; | 155 passwords_ = passwords; |
130 account_creation_form_origin_ = password_form->origin; | 156 possible_account_creation_form_.swap(password_form); |
131 MaybeShowIcon(); | 157 MaybeShowIcon(); |
132 // We assume that there is only one account creation field per URL. | 158 // We assume that there is only one account creation field per URL. |
133 return; | 159 return; |
134 } | 160 } |
135 } | 161 } |
136 password_generation::LogPasswordGenerationEvent( | 162 password_generation::LogPasswordGenerationEvent( |
137 password_generation::NO_SIGN_UP_DETECTED); | 163 password_generation::NO_SIGN_UP_DETECTED); |
138 } | 164 } |
139 | 165 |
140 bool PasswordGenerationManager::ShouldAnalyzeDocument( | 166 bool PasswordGenerationManager::ShouldAnalyzeDocument( |
(...skipping 28 matching lines...) Expand all Loading... |
169 | 195 |
170 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { | 196 bool PasswordGenerationManager::OnMessageReceived(const IPC::Message& message) { |
171 bool handled = true; | 197 bool handled = true; |
172 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) | 198 IPC_BEGIN_MESSAGE_MAP(PasswordGenerationManager, message) |
173 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, | 199 IPC_MESSAGE_HANDLER(AutofillMsg_FormNotBlacklisted, |
174 OnFormNotBlacklisted) | 200 OnFormNotBlacklisted) |
175 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, | 201 IPC_MESSAGE_HANDLER(AutofillMsg_GeneratedPasswordAccepted, |
176 OnPasswordAccepted) | 202 OnPasswordAccepted) |
177 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, | 203 IPC_MESSAGE_HANDLER(AutofillMsg_PasswordGenerationEnabled, |
178 OnPasswordGenerationEnabled) | 204 OnPasswordGenerationEnabled) |
| 205 IPC_MESSAGE_HANDLER(AutofillMsg_AccountCreationFormsDetected, |
| 206 OnAccountCreationFormsDetected) |
179 IPC_MESSAGE_UNHANDLED(handled = false) | 207 IPC_MESSAGE_UNHANDLED(handled = false) |
180 IPC_END_MESSAGE_MAP() | 208 IPC_END_MESSAGE_MAP() |
181 return handled; | 209 return handled; |
182 } | 210 } |
183 | 211 |
184 void PasswordGenerationManager::OnFormNotBlacklisted( | 212 void PasswordGenerationManager::OnFormNotBlacklisted( |
185 const content::PasswordForm& form) { | 213 const content::PasswordForm& form) { |
186 not_blacklisted_password_form_origins_.push_back(form.origin); | 214 not_blacklisted_password_form_origins_.push_back(form.origin); |
187 MaybeShowIcon(); | 215 MaybeShowIcon(); |
188 } | 216 } |
189 | 217 |
190 void PasswordGenerationManager::OnPasswordAccepted( | 218 void PasswordGenerationManager::OnPasswordAccepted( |
191 const base::string16& password) { | 219 const base::string16& password) { |
192 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); | 220 for (std::vector<WebKit::WebInputElement>::iterator it = passwords_.begin(); |
193 it != passwords_.end(); ++it) { | 221 it != passwords_.end(); ++it) { |
194 it->setValue(password); | 222 it->setValue(password); |
195 it->setAutofilled(true); | 223 it->setAutofilled(true); |
196 // Advance focus to the next input field. We assume password fields in | 224 // Advance focus to the next input field. We assume password fields in |
197 // an account creation form are always adjacent. | 225 // an account creation form are always adjacent. |
198 render_view_->GetWebView()->advanceFocus(false); | 226 render_view_->GetWebView()->advanceFocus(false); |
199 } | 227 } |
200 } | 228 } |
201 | 229 |
202 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { | 230 void PasswordGenerationManager::OnPasswordGenerationEnabled(bool enabled) { |
203 enabled_ = enabled; | 231 enabled_ = enabled; |
204 } | 232 } |
205 | 233 |
| 234 void PasswordGenerationManager::OnAccountCreationFormsDetected( |
| 235 const std::vector<autofill::FormData>& forms) { |
| 236 account_creation_forms_.insert( |
| 237 account_creation_forms_.end(), forms.begin(), forms.end()); |
| 238 MaybeShowIcon(); |
| 239 } |
| 240 |
206 void PasswordGenerationManager::MaybeShowIcon() { | 241 void PasswordGenerationManager::MaybeShowIcon() { |
207 // We should show the password generation icon only when we have detected | 242 // 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 | 243 // account creation form, we have confirmed from browser that this form |
209 // is not blacklisted by the users. | 244 // is not blacklisted by the users, and the Autofill server has marked one |
210 if (!account_creation_form_origin_.is_valid() || | 245 // of its field as ACCOUNT_CREATION_PASSWORD. |
| 246 if (!possible_account_creation_form_.get() || |
211 passwords_.empty() || | 247 passwords_.empty() || |
212 not_blacklisted_password_form_origins_.empty()) { | 248 not_blacklisted_password_form_origins_.empty() || |
| 249 account_creation_forms_.empty()) { |
213 return; | 250 return; |
214 } | 251 } |
215 | 252 |
216 for (std::vector<GURL>::iterator it = | 253 if (!ContainsURL(not_blacklisted_password_form_origins_, |
217 not_blacklisted_password_form_origins_.begin(); | 254 possible_account_creation_form_->origin)) { |
218 it != not_blacklisted_password_form_origins_.end(); ++it) { | 255 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 } | 256 } |
| 257 |
| 258 if (!ContainsForm(account_creation_forms_, |
| 259 *possible_account_creation_form_)) { |
| 260 return; |
| 261 } |
| 262 |
| 263 passwords_[0].passwordGeneratorButtonElement().setAttribute("style", |
| 264 "display:block"); |
| 265 password_generation::LogPasswordGenerationEvent( |
| 266 password_generation::ICON_SHOWN); |
227 } | 267 } |
228 | 268 |
229 } // namespace autofill | 269 } // namespace autofill |
OLD | NEW |