OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/password_manager/content/browser/credential_manager_dispatc
her.h" | 5 #include "components/password_manager/content/browser/credential_manager_dispatc
her.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "components/autofill/core/common/password_form.h" | 11 #include "components/autofill/core/common/password_form.h" |
12 #include "components/password_manager/content/browser/content_password_manager_d
river.h" | 12 #include "components/password_manager/content/browser/content_password_manager_d
river.h" |
13 #include "components/password_manager/content/browser/content_password_manager_d
river_factory.h" | 13 #include "components/password_manager/content/browser/content_password_manager_d
river_factory.h" |
14 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" | 14 #include "components/password_manager/content/browser/credential_manager_passwor
d_form_manager.h" |
15 #include "components/password_manager/content/common/credential_manager_messages
.h" | 15 #include "components/password_manager/content/common/credential_manager_messages
.h" |
16 #include "components/password_manager/content/common/credential_manager_types.h" | 16 #include "components/password_manager/content/common/credential_manager_types.h" |
17 #include "components/password_manager/core/browser/password_manager_client.h" | 17 #include "components/password_manager/core/browser/password_manager_client.h" |
18 #include "components/password_manager/core/browser/password_store.h" | 18 #include "components/password_manager/core/browser/password_store.h" |
19 #include "content/public/browser/render_view_host.h" | 19 #include "content/public/browser/render_view_host.h" |
20 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
21 #include "ipc/ipc_message_macros.h" | 21 #include "ipc/ipc_message_macros.h" |
22 | 22 |
23 namespace password_manager { | 23 namespace password_manager { |
24 | 24 |
25 struct CredentialManagerDispatcher::PendingRequestParameters { | 25 class CredentialManagerDispatcher::PendingRequestTask |
26 PendingRequestParameters(int request_id, | 26 : public PasswordStoreConsumer { |
27 bool request_zero_click_only, | 27 public: |
28 GURL request_origin, | 28 PendingRequestTask(CredentialManagerDispatcher* const dispatcher, |
29 const std::vector<GURL>& request_federations) | 29 int request_id, |
30 : id(request_id), | 30 bool request_zero_click_only, |
31 zero_click_only(request_zero_click_only), | 31 const GURL& request_origin, |
32 origin(request_origin), | 32 const std::vector<GURL>& request_federations) |
33 federations(request_federations) {} | 33 : dispatcher_(dispatcher), |
| 34 id_(request_id), |
| 35 zero_click_only_(request_zero_click_only), |
| 36 origin_(request_origin) { |
| 37 for (const GURL& origin : request_federations) |
| 38 federations_.insert(origin.spec()); |
| 39 } |
34 | 40 |
35 int id; | 41 int id() const { return id_; } |
36 bool zero_click_only; | 42 |
37 GURL origin; | 43 // PasswordStoreConsumer implementation. |
38 std::vector<GURL> federations; | 44 void OnGetPasswordStoreResults( |
| 45 const std::vector<autofill::PasswordForm*>& results) override { |
| 46 // We own the PasswordForm instances, so we're responsible for cleaning |
| 47 // up the instances we don't add to |local_results| or |federated_results|. |
| 48 // |
| 49 // TODO(mkwst): Switch this and PromptUserToChooseCredentials() to use |
| 50 // ScopedVector. |
| 51 std::vector<autofill::PasswordForm*> local_results; |
| 52 std::vector<autofill::PasswordForm*> federated_results; |
| 53 for (autofill::PasswordForm* form : results) { |
| 54 if (form->origin == origin_) |
| 55 local_results.push_back(form); |
| 56 else if (federations_.count(form->origin.spec())) |
| 57 federated_results.push_back(form); |
| 58 else |
| 59 delete form; |
| 60 } |
| 61 |
| 62 if ((local_results.empty() && federated_results.empty()) || |
| 63 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != |
| 64 origin_) { |
| 65 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 66 return; |
| 67 } |
| 68 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) { |
| 69 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result |
| 70 // to prevent auto-sign-in, once that flag is implemented. |
| 71 CredentialInfo info(*local_results[0], |
| 72 local_results[0]->federation_url.is_empty() |
| 73 ? CredentialType::CREDENTIAL_TYPE_LOCAL |
| 74 : CredentialType::CREDENTIAL_TYPE_FEDERATED); |
| 75 STLDeleteElements(&local_results); |
| 76 STLDeleteElements(&federated_results); |
| 77 dispatcher_->SendCredential(id_, info); |
| 78 return; |
| 79 } |
| 80 |
| 81 if (zero_click_only_ || |
| 82 !dispatcher_->client()->PromptUserToChooseCredentials( |
| 83 local_results, federated_results, |
| 84 base::Bind(&CredentialManagerDispatcher::SendCredential, |
| 85 base::Unretained(dispatcher_), id_))) { |
| 86 STLDeleteElements(&local_results); |
| 87 STLDeleteElements(&federated_results); |
| 88 dispatcher_->SendCredential(id_, CredentialInfo()); |
| 89 } |
| 90 } |
| 91 |
| 92 private: |
| 93 // Backlink to the CredentialManagerDispatcher that owns this object. |
| 94 CredentialManagerDispatcher* const dispatcher_; |
| 95 |
| 96 const int id_; |
| 97 const bool zero_click_only_; |
| 98 const GURL origin_; |
| 99 std::set<std::string> federations_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); |
39 }; | 102 }; |
40 | 103 |
41 CredentialManagerDispatcher::CredentialManagerDispatcher( | 104 CredentialManagerDispatcher::CredentialManagerDispatcher( |
42 content::WebContents* web_contents, | 105 content::WebContents* web_contents, |
43 PasswordManagerClient* client) | 106 PasswordManagerClient* client) |
44 : WebContentsObserver(web_contents), client_(client) { | 107 : WebContentsObserver(web_contents), client_(client) { |
45 DCHECK(web_contents); | 108 DCHECK(web_contents); |
46 } | 109 } |
47 | 110 |
48 CredentialManagerDispatcher::~CredentialManagerDispatcher() { | 111 CredentialManagerDispatcher::~CredentialManagerDispatcher() { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 189 } |
127 | 190 |
128 if (zero_click_only && !IsZeroClickAllowed()) { | 191 if (zero_click_only && !IsZeroClickAllowed()) { |
129 web_contents()->GetRenderViewHost()->Send( | 192 web_contents()->GetRenderViewHost()->Send( |
130 new CredentialManagerMsg_SendCredential( | 193 new CredentialManagerMsg_SendCredential( |
131 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, | 194 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, |
132 CredentialInfo())); | 195 CredentialInfo())); |
133 return; | 196 return; |
134 } | 197 } |
135 | 198 |
136 pending_request_.reset(new PendingRequestParameters( | 199 pending_request_.reset(new PendingRequestTask( |
137 request_id, zero_click_only, | 200 this, request_id, zero_click_only, |
138 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); | 201 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); |
139 | 202 |
140 // This will result in a callback to ::OnGetPasswordStoreResults(). | 203 // This will result in a callback to |
141 store->GetAutofillableLogins(this); | 204 // PendingRequestTask::OnGetPasswordStoreResults(). |
142 } | 205 store->GetAutofillableLogins(pending_request_.get()); |
143 | |
144 void CredentialManagerDispatcher::OnGetPasswordStoreResults( | |
145 const std::vector<autofill::PasswordForm*>& results) { | |
146 DCHECK(pending_request_); | |
147 | |
148 std::set<std::string> federations; | |
149 for (const GURL& origin : pending_request_->federations) | |
150 federations.insert(origin.spec()); | |
151 | |
152 // We own the PasswordForm instances, so we're responsible for cleaning | |
153 // up the instances we don't add to |local_results| or |federated_results|. | |
154 std::vector<autofill::PasswordForm*> local_results; | |
155 std::vector<autofill::PasswordForm*> federated_results; | |
156 for (autofill::PasswordForm* form : results) { | |
157 if (form->origin == pending_request_->origin) | |
158 local_results.push_back(form); | |
159 else if (federations.count(form->origin.spec()) != 0) | |
160 federated_results.push_back(form); | |
161 else | |
162 delete form; | |
163 } | |
164 | |
165 if ((local_results.empty() && federated_results.empty()) || | |
166 web_contents()->GetLastCommittedURL().GetOrigin() != | |
167 pending_request_->origin) { | |
168 SendCredential(pending_request_->id, CredentialInfo()); | |
169 return; | |
170 } | |
171 | |
172 if (local_results.size() == 1 && IsZeroClickAllowed()) { | |
173 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result | |
174 // to prevent auto-sign-in, once that flag is implemented. | |
175 CredentialInfo info(*local_results[0], | |
176 local_results[0]->federation_url.is_empty() | |
177 ? CredentialType::CREDENTIAL_TYPE_LOCAL | |
178 : CredentialType::CREDENTIAL_TYPE_FEDERATED); | |
179 STLDeleteElements(&local_results); | |
180 STLDeleteElements(&federated_results); | |
181 SendCredential(pending_request_->id, info); | |
182 return; | |
183 } | |
184 | |
185 if (pending_request_->zero_click_only || | |
186 !client_->PromptUserToChooseCredentials( | |
187 local_results, federated_results, | |
188 base::Bind(&CredentialManagerDispatcher::SendCredential, | |
189 base::Unretained(this), pending_request_->id))) { | |
190 STLDeleteElements(&local_results); | |
191 STLDeleteElements(&federated_results); | |
192 SendCredential(pending_request_->id, CredentialInfo()); | |
193 } | |
194 } | 206 } |
195 | 207 |
196 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { | 208 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { |
197 return client_ ? client_->GetPasswordStore() : nullptr; | 209 return client_ ? client_->GetPasswordStore() : nullptr; |
198 } | 210 } |
199 | 211 |
200 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { | 212 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { |
201 // TODO(vasilii): add more, see http://crbug.com/450583. | 213 // TODO(vasilii): add more, see http://crbug.com/450583. |
202 return !client_->IsOffTheRecord(); | 214 return !client_->IsOffTheRecord(); |
203 } | 215 } |
204 | 216 |
205 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { | 217 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { |
206 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); | 218 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); |
207 } | 219 } |
208 | 220 |
209 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { | 221 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { |
210 ContentPasswordManagerDriverFactory* driver_factory = | 222 ContentPasswordManagerDriverFactory* driver_factory = |
211 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); | 223 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); |
212 DCHECK(driver_factory); | 224 DCHECK(driver_factory); |
213 PasswordManagerDriver* driver = | 225 PasswordManagerDriver* driver = |
214 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); | 226 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); |
215 return driver->AsWeakPtr(); | 227 return driver->AsWeakPtr(); |
216 } | 228 } |
217 | 229 |
218 void CredentialManagerDispatcher::SendCredential(int request_id, | 230 void CredentialManagerDispatcher::SendCredential(int request_id, |
219 const CredentialInfo& info) { | 231 const CredentialInfo& info) { |
220 DCHECK(pending_request_); | 232 DCHECK(pending_request_); |
221 DCHECK_EQ(pending_request_->id, request_id); | 233 DCHECK_EQ(pending_request_->id(), request_id); |
222 web_contents()->GetRenderViewHost()->Send( | 234 web_contents()->GetRenderViewHost()->Send( |
223 new CredentialManagerMsg_SendCredential( | 235 new CredentialManagerMsg_SendCredential( |
224 web_contents()->GetRenderViewHost()->GetRoutingID(), | 236 web_contents()->GetRenderViewHost()->GetRoutingID(), |
225 pending_request_->id, info)); | 237 pending_request_->id(), info)); |
226 pending_request_.reset(); | 238 pending_request_.reset(); |
227 } | 239 } |
228 | 240 |
229 } // namespace password_manager | 241 } // namespace password_manager |
OLD | NEW |