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* 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 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 federations_(request_federations) {} | |
34 | 38 |
35 int id; | 39 int id() const { return id_; } |
36 bool zero_click_only; | 40 |
37 GURL origin; | 41 // PasswordStoreConsumer implementation. |
38 std::vector<GURL> federations; | 42 void OnGetPasswordStoreResults( |
43 const std::vector<autofill::PasswordForm*>& results) override { | |
44 std::set<std::string> federations; | |
45 for (const GURL& origin : federations_) | |
46 federations.insert(origin.spec()); | |
47 | |
48 // We own the PasswordForm instances, so we're responsible for cleaning | |
49 // up the instances we don't add to |local_results| or |federated_results|. | |
50 std::vector<autofill::PasswordForm*> local_results; | |
51 std::vector<autofill::PasswordForm*> federated_results; | |
52 for (autofill::PasswordForm* form : results) { | |
53 if (form->origin == origin_) | |
54 local_results.push_back(form); | |
55 else if (federations.count(form->origin.spec()) != 0) | |
56 federated_results.push_back(form); | |
57 else | |
58 delete form; | |
59 } | |
60 | |
61 if ((local_results.empty() && federated_results.empty()) || | |
62 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != | |
63 origin_) { | |
64 dispatcher_->SendCredential(id_, CredentialInfo()); | |
65 return; | |
66 } | |
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 dispatcher_->SendCredential(id_, CredentialInfo()); | |
87 } | |
88 } | |
89 | |
90 private: | |
91 CredentialManagerDispatcher* dispatcher_; | |
vabr (Chromium)
2015/01/29 14:04:34
nit: Maybe comment that this is a weak (back-)link
vabr (Chromium)
2015/01/29 14:04:34
All data members here should be const, they are no
| |
92 int id_; | |
93 bool zero_click_only_; | |
94 GURL origin_; | |
95 std::vector<GURL> federations_; | |
vabr (Chromium)
2015/01/29 14:04:34
Seems like a lot of stuff to copy. Is there any ch
| |
39 }; | 96 }; |
vabr (Chromium)
2015/01/29 14:04:34
Does this class need to be copied or assigned? If
| |
40 | 97 |
41 CredentialManagerDispatcher::CredentialManagerDispatcher( | 98 CredentialManagerDispatcher::CredentialManagerDispatcher( |
42 content::WebContents* web_contents, | 99 content::WebContents* web_contents, |
43 PasswordManagerClient* client) | 100 PasswordManagerClient* client) |
44 : WebContentsObserver(web_contents), client_(client) { | 101 : WebContentsObserver(web_contents), client_(client) { |
45 DCHECK(web_contents); | 102 DCHECK(web_contents); |
46 } | 103 } |
47 | 104 |
48 CredentialManagerDispatcher::~CredentialManagerDispatcher() { | 105 CredentialManagerDispatcher::~CredentialManagerDispatcher() { |
49 } | 106 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 } | 183 } |
127 | 184 |
128 if (zero_click_only && !IsZeroClickAllowed()) { | 185 if (zero_click_only && !IsZeroClickAllowed()) { |
129 web_contents()->GetRenderViewHost()->Send( | 186 web_contents()->GetRenderViewHost()->Send( |
130 new CredentialManagerMsg_SendCredential( | 187 new CredentialManagerMsg_SendCredential( |
131 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, | 188 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, |
132 CredentialInfo())); | 189 CredentialInfo())); |
133 return; | 190 return; |
134 } | 191 } |
135 | 192 |
136 pending_request_.reset(new PendingRequestParameters( | 193 pending_request_.reset(new PendingRequestTask( |
137 request_id, zero_click_only, | 194 this, request_id, zero_click_only, |
138 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); | 195 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); |
139 | 196 |
140 // This will result in a callback to ::OnGetPasswordStoreResults(). | 197 // This will result in a callback to |
141 store->GetAutofillableLogins(this); | 198 // PendingRequestTask::OnGetPasswordStoreResults(). |
142 } | 199 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 SendCredential(pending_request_->id, CredentialInfo()); | |
191 } | |
192 } | 200 } |
193 | 201 |
194 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { | 202 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { |
195 return client_ ? client_->GetPasswordStore() : nullptr; | 203 return client_ ? client_->GetPasswordStore() : nullptr; |
196 } | 204 } |
197 | 205 |
198 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { | 206 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { |
199 // TODO(vasilii): add more, see http://crbug.com/450583. | 207 // TODO(vasilii): add more, see http://crbug.com/450583. |
200 return !client_->IsOffTheRecord(); | 208 return !client_->IsOffTheRecord(); |
201 } | 209 } |
202 | 210 |
203 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { | 211 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { |
204 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); | 212 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); |
205 } | 213 } |
206 | 214 |
207 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { | 215 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { |
208 ContentPasswordManagerDriverFactory* driver_factory = | 216 ContentPasswordManagerDriverFactory* driver_factory = |
209 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); | 217 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); |
210 DCHECK(driver_factory); | 218 DCHECK(driver_factory); |
211 PasswordManagerDriver* driver = | 219 PasswordManagerDriver* driver = |
212 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); | 220 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); |
213 return driver->AsWeakPtr(); | 221 return driver->AsWeakPtr(); |
214 } | 222 } |
215 | 223 |
216 void CredentialManagerDispatcher::SendCredential(int request_id, | 224 void CredentialManagerDispatcher::SendCredential(int request_id, |
217 const CredentialInfo& info) { | 225 const CredentialInfo& info) { |
218 DCHECK(pending_request_); | 226 DCHECK(pending_request_); |
219 DCHECK_EQ(pending_request_->id, request_id); | 227 DCHECK_EQ(pending_request_->id(), request_id); |
220 web_contents()->GetRenderViewHost()->Send( | 228 web_contents()->GetRenderViewHost()->Send( |
221 new CredentialManagerMsg_SendCredential( | 229 new CredentialManagerMsg_SendCredential( |
222 web_contents()->GetRenderViewHost()->GetRoutingID(), | 230 web_contents()->GetRenderViewHost()->GetRoutingID(), |
223 pending_request_->id, info)); | 231 pending_request_->id(), info)); |
224 pending_request_.reset(); | 232 pending_request_.reset(); |
225 } | 233 } |
226 | 234 |
227 } // namespace password_manager | 235 } // namespace password_manager |
OLD | NEW |