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) | |
vabr (Chromium)
2015/01/30 12:54:19
If this is possible in the IPC framework we have,
| |
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 std::vector<autofill::PasswordForm*> local_results; | |
vabr (Chromium)
2015/01/30 12:54:19
Consider using ScopedVector for both these vectors
| |
49 std::vector<autofill::PasswordForm*> federated_results; | |
50 for (autofill::PasswordForm* form : results) { | |
51 if (form->origin == origin_) | |
52 local_results.push_back(form); | |
53 else if (federations_.count(form->origin.spec())) | |
54 federated_results.push_back(form); | |
55 else | |
56 delete form; | |
57 } | |
58 | |
59 if ((local_results.empty() && federated_results.empty()) || | |
60 dispatcher_->web_contents()->GetLastCommittedURL().GetOrigin() != | |
61 origin_) { | |
62 dispatcher_->SendCredential(id_, CredentialInfo()); | |
63 return; | |
64 } | |
65 if (local_results.size() == 1 && dispatcher_->IsZeroClickAllowed()) { | |
66 // TODO(mkwst): Use the `one_time_disable_zero_click` flag on the result | |
67 // to prevent auto-sign-in, once that flag is implemented. | |
68 CredentialInfo info(*local_results[0], | |
69 local_results[0]->federation_url.is_empty() | |
70 ? CredentialType::CREDENTIAL_TYPE_LOCAL | |
71 : CredentialType::CREDENTIAL_TYPE_FEDERATED); | |
72 STLDeleteElements(&local_results); | |
73 STLDeleteElements(&federated_results); | |
74 dispatcher_->SendCredential(id_, info); | |
75 return; | |
76 } | |
77 | |
78 if (zero_click_only_ || | |
79 !dispatcher_->client()->PromptUserToChooseCredentials( | |
80 local_results, federated_results, | |
81 base::Bind(&CredentialManagerDispatcher::SendCredential, | |
82 base::Unretained(dispatcher_), id_))) { | |
83 STLDeleteElements(&local_results); | |
84 STLDeleteElements(&federated_results); | |
85 dispatcher_->SendCredential(id_, CredentialInfo()); | |
86 } | |
87 } | |
88 | |
89 private: | |
90 // Backlink to the CredentialManagerDispatcher that owns this object. | |
91 CredentialManagerDispatcher* const dispatcher_; | |
92 | |
93 const int id_; | |
94 const bool zero_click_only_; | |
95 const GURL origin_; | |
96 std::set<std::string> federations_; | |
97 | |
98 DISALLOW_COPY_AND_ASSIGN(PendingRequestTask); | |
39 }; | 99 }; |
40 | 100 |
41 CredentialManagerDispatcher::CredentialManagerDispatcher( | 101 CredentialManagerDispatcher::CredentialManagerDispatcher( |
42 content::WebContents* web_contents, | 102 content::WebContents* web_contents, |
43 PasswordManagerClient* client) | 103 PasswordManagerClient* client) |
44 : WebContentsObserver(web_contents), client_(client) { | 104 : WebContentsObserver(web_contents), client_(client) { |
45 DCHECK(web_contents); | 105 DCHECK(web_contents); |
46 } | 106 } |
47 | 107 |
48 CredentialManagerDispatcher::~CredentialManagerDispatcher() { | 108 CredentialManagerDispatcher::~CredentialManagerDispatcher() { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
126 } | 186 } |
127 | 187 |
128 if (zero_click_only && !IsZeroClickAllowed()) { | 188 if (zero_click_only && !IsZeroClickAllowed()) { |
129 web_contents()->GetRenderViewHost()->Send( | 189 web_contents()->GetRenderViewHost()->Send( |
130 new CredentialManagerMsg_SendCredential( | 190 new CredentialManagerMsg_SendCredential( |
131 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, | 191 web_contents()->GetRenderViewHost()->GetRoutingID(), request_id, |
132 CredentialInfo())); | 192 CredentialInfo())); |
133 return; | 193 return; |
134 } | 194 } |
135 | 195 |
136 pending_request_.reset(new PendingRequestParameters( | 196 pending_request_.reset(new PendingRequestTask( |
137 request_id, zero_click_only, | 197 this, request_id, zero_click_only, |
138 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); | 198 web_contents()->GetLastCommittedURL().GetOrigin(), federations)); |
139 | 199 |
140 // This will result in a callback to ::OnGetPasswordStoreResults(). | 200 // This will result in a callback to |
141 store->GetAutofillableLogins(this); | 201 // PendingRequestTask::OnGetPasswordStoreResults(). |
142 } | 202 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 } | 203 } |
195 | 204 |
196 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { | 205 PasswordStore* CredentialManagerDispatcher::GetPasswordStore() { |
197 return client_ ? client_->GetPasswordStore() : nullptr; | 206 return client_ ? client_->GetPasswordStore() : nullptr; |
198 } | 207 } |
199 | 208 |
200 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { | 209 bool CredentialManagerDispatcher::IsSavingEnabledForCurrentPage() const { |
201 // TODO(vasilii): add more, see http://crbug.com/450583. | 210 // TODO(vasilii): add more, see http://crbug.com/450583. |
202 return !client_->IsOffTheRecord(); | 211 return !client_->IsOffTheRecord(); |
203 } | 212 } |
204 | 213 |
205 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { | 214 bool CredentialManagerDispatcher::IsZeroClickAllowed() const { |
206 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); | 215 return !client_->IsOffTheRecord() && client_->IsZeroClickEnabled(); |
207 } | 216 } |
208 | 217 |
209 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { | 218 base::WeakPtr<PasswordManagerDriver> CredentialManagerDispatcher::GetDriver() { |
210 ContentPasswordManagerDriverFactory* driver_factory = | 219 ContentPasswordManagerDriverFactory* driver_factory = |
211 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); | 220 ContentPasswordManagerDriverFactory::FromWebContents(web_contents()); |
212 DCHECK(driver_factory); | 221 DCHECK(driver_factory); |
213 PasswordManagerDriver* driver = | 222 PasswordManagerDriver* driver = |
214 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); | 223 driver_factory->GetDriverForFrame(web_contents()->GetMainFrame()); |
215 return driver->AsWeakPtr(); | 224 return driver->AsWeakPtr(); |
216 } | 225 } |
217 | 226 |
218 void CredentialManagerDispatcher::SendCredential(int request_id, | 227 void CredentialManagerDispatcher::SendCredential(int request_id, |
219 const CredentialInfo& info) { | 228 const CredentialInfo& info) { |
220 DCHECK(pending_request_); | 229 DCHECK(pending_request_); |
221 DCHECK_EQ(pending_request_->id, request_id); | 230 DCHECK_EQ(pending_request_->id(), request_id); |
222 web_contents()->GetRenderViewHost()->Send( | 231 web_contents()->GetRenderViewHost()->Send( |
223 new CredentialManagerMsg_SendCredential( | 232 new CredentialManagerMsg_SendCredential( |
224 web_contents()->GetRenderViewHost()->GetRoutingID(), | 233 web_contents()->GetRenderViewHost()->GetRoutingID(), |
225 pending_request_->id, info)); | 234 pending_request_->id(), info)); |
226 pending_request_.reset(); | 235 pending_request_.reset(); |
227 } | 236 } |
228 | 237 |
229 } // namespace password_manager | 238 } // namespace password_manager |
OLD | NEW |