OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/chrome/browser/passwords/credential_manager.h" | 5 #import "ios/chrome/browser/passwords/credential_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
10 #import "base/ios/weak_nsobject.h" | 10 #import "base/ios/weak_nsobject.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 | 110 |
111 void CredentialManager::CredentialsRequested( | 111 void CredentialManager::CredentialsRequested( |
112 int request_id, | 112 int request_id, |
113 const GURL& source_url, | 113 const GURL& source_url, |
114 bool zero_click_only, | 114 bool zero_click_only, |
115 const std::vector<std::string>& federations, | 115 const std::vector<std::string>& federations, |
116 bool is_user_initiated) { | 116 bool is_user_initiated) { |
117 // Invoked when the page invokes navigator.credentials.request(), this | 117 // Invoked when the page invokes navigator.credentials.request(), this |
118 // function will attempt to retrieve a Credential from the PasswordStore that | 118 // function will attempt to retrieve a Credential from the PasswordStore that |
119 // meets the specified parameters and, if successful, send it back to the page | 119 // meets the specified parameters and, if successful, send it back to the page |
120 // via SendCredential. | 120 // via SendCredentialByID. |
121 DCHECK_GE(request_id, 0); | 121 DCHECK_GE(request_id, 0); |
122 password_manager::PasswordStore* store = GetPasswordStore(); | 122 password_manager::PasswordStore* store = GetPasswordStore(); |
123 | 123 |
124 // If there's an outstanding request, or the PasswordStore isn't loaded yet, | 124 // If there's an outstanding request, or the PasswordStore isn't loaded yet, |
125 // the request should fail outright and the JS Promise should be rejected | 125 // the request should fail outright and the JS Promise should be rejected |
126 // with an appropriate error. | 126 // with an appropriate error. |
127 if (pending_request_ || !store) { | 127 if (pending_request_ || !store) { |
128 base::MessageLoop::current()->PostTask( | 128 base::MessageLoop::current()->PostTask( |
129 FROM_HERE, | 129 FROM_HERE, |
130 base::Bind(&CredentialManager::RejectPromise, | 130 base::Bind(&CredentialManager::RejectPromise, |
131 weak_factory_.GetWeakPtr(), request_id, | 131 weak_factory_.GetWeakPtr(), request_id, |
132 pending_request_ ? ERROR_TYPE_PENDING_REQUEST | 132 pending_request_ ? ERROR_TYPE_PENDING_REQUEST |
133 : ERROR_TYPE_PASSWORD_STORE_UNAVAILABLE)); | 133 : ERROR_TYPE_PASSWORD_STORE_UNAVAILABLE)); |
134 return; | 134 return; |
135 } | 135 } |
136 | 136 |
137 // If the page requested a zero-click credential -- one that can be returned | 137 // If the page requested a zero-click credential -- one that can be returned |
138 // without first asking the user -- and if zero-click isn't currently | 138 // without first asking the user -- and if zero-click isn't currently |
139 // available, send back an empty credential. | 139 // available, send back an empty credential. |
140 if (zero_click_only && !IsZeroClickAllowed()) { | 140 if (zero_click_only && !IsZeroClickAllowed()) { |
141 base::MessageLoop::current()->PostTask( | 141 base::MessageLoop::current()->PostTask( |
142 FROM_HERE, base::Bind(&CredentialManager::SendCredential, | 142 FROM_HERE, base::Bind(&CredentialManager::SendCredentialByID, |
143 weak_factory_.GetWeakPtr(), request_id, | 143 weak_factory_.GetWeakPtr(), request_id, |
144 password_manager::CredentialInfo())); | 144 password_manager::CredentialInfo())); |
145 return; | 145 return; |
146 } | 146 } |
147 | 147 |
148 // If the page origin is untrusted, the request should be rejected. | 148 // If the page origin is untrusted, the request should be rejected. |
149 GURL page_url; | 149 GURL page_url; |
150 if (!GetUrlWithAbsoluteTrust(&page_url)) { | 150 if (!GetUrlWithAbsoluteTrust(&page_url)) { |
151 RejectPromise(request_id, ERROR_TYPE_SECURITY_ERROR_UNTRUSTED_ORIGIN); | 151 RejectPromise(request_id, ERROR_TYPE_SECURITY_ERROR_UNTRUSTED_ORIGIN); |
152 return; | 152 return; |
153 } | 153 } |
154 | 154 |
155 // Bundle up the arguments and forward them to the PasswordStore, which will | 155 // Bundle up the arguments and forward them to the PasswordStore, which will |
156 // asynchronously return the resulting Credential by invoking | 156 // asynchronously return the resulting Credential by invoking |
157 // |SendCredential|. | 157 // |SendCredential|. |
158 std::vector<GURL> federation_urls; | 158 std::vector<GURL> federation_urls; |
159 for (const auto& federation : federations) | 159 for (const auto& federation : federations) |
160 federation_urls.push_back(GURL(federation)); | 160 federation_urls.push_back(GURL(federation)); |
161 std::vector<std::string> realms; | 161 std::vector<std::string> realms; |
162 pending_request_.reset( | 162 pending_request_.reset( |
163 new password_manager::CredentialManagerPendingRequestTask( | 163 new password_manager::CredentialManagerPendingRequestTask( |
164 this, request_id, zero_click_only, page_url, true, federation_urls, | 164 this, base::Bind(&CredentialManager::SendCredentialByID, |
165 realms)); | 165 base::Unretained(this), request_id), |
| 166 zero_click_only, page_url, true, federation_urls, realms)); |
166 store->GetAutofillableLogins(pending_request_.get()); | 167 store->GetAutofillableLogins(pending_request_.get()); |
167 } | 168 } |
168 | 169 |
169 void CredentialManager::SignedIn(int request_id, | 170 void CredentialManager::SignedIn(int request_id, |
170 const GURL& source_url, | 171 const GURL& source_url, |
171 const web::Credential& credential) { | 172 const web::Credential& credential) { |
172 // Invoked when the page invokes navigator.credentials.notifySignedIn(), this | 173 // Invoked when the page invokes navigator.credentials.notifySignedIn(), this |
173 // function stores the signed-in |credential| and sends a message back to the | 174 // function stores the signed-in |credential| and sends a message back to the |
174 // page to resolve the Promise associated with |request_id|. | 175 // page to resolve the Promise associated with |request_id|. |
175 DCHECK(credential.type != web::CredentialType::CREDENTIAL_TYPE_EMPTY); | 176 DCHECK(credential.type != web::CredentialType::CREDENTIAL_TYPE_EMPTY); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 | 263 |
263 GURL CredentialManager::GetOrigin() const { | 264 GURL CredentialManager::GetOrigin() const { |
264 web::URLVerificationTrustLevel trust_level = | 265 web::URLVerificationTrustLevel trust_level = |
265 web::URLVerificationTrustLevel::kNone; | 266 web::URLVerificationTrustLevel::kNone; |
266 const GURL page_url(web_state()->GetCurrentURL(&trust_level)); | 267 const GURL page_url(web_state()->GetCurrentURL(&trust_level)); |
267 DCHECK_EQ(trust_level, web::URLVerificationTrustLevel::kAbsolute); | 268 DCHECK_EQ(trust_level, web::URLVerificationTrustLevel::kAbsolute); |
268 return page_url; | 269 return page_url; |
269 } | 270 } |
270 | 271 |
271 void CredentialManager::SendCredential( | 272 void CredentialManager::SendCredential( |
| 273 const password_manager::SendCredentialCallback& send_callback, |
| 274 const password_manager::CredentialInfo& credential) { |
| 275 send_callback.Run(credential); |
| 276 } |
| 277 |
| 278 void CredentialManager::SendCredentialByID( |
272 int request_id, | 279 int request_id, |
273 const password_manager::CredentialInfo& credential) { | 280 const password_manager::CredentialInfo& credential) { |
274 // Invoked when the asynchronous interaction with the PasswordStore completes, | 281 // Invoked when the asynchronous interaction with the PasswordStore completes, |
275 // this function forwards a |credential| back to the page via |js_manager_| by | 282 // this function forwards a |credential| back to the page via |js_manager_| by |
276 // resolving the JavaScript Promise associated with |request_id|. | 283 // resolving the JavaScript Promise associated with |request_id|. |
277 base::WeakPtr<CredentialManager> weak_this = weak_factory_.GetWeakPtr(); | 284 base::WeakPtr<CredentialManager> weak_this = weak_factory_.GetWeakPtr(); |
278 [js_manager_ | 285 [js_manager_ |
279 resolvePromiseWithRequestID:request_id | 286 resolvePromiseWithRequestID:request_id |
280 credential:WebCredentialFromCredentialInfo(credential) | 287 credential:WebCredentialFromCredentialInfo(credential) |
281 completionHandler:^(BOOL) { | 288 completionHandler:^(BOOL) { |
282 if (weak_this) | 289 if (weak_this) |
283 weak_this->pending_request_.reset(); | 290 weak_this->pending_request_.reset(); |
284 }]; | 291 }]; |
285 } | 292 } |
286 | 293 |
287 void CredentialManager::SendPasswordForm(int request_id, | 294 void CredentialManager::SendPasswordForm( |
288 const autofill::PasswordForm* form) { | 295 const password_manager::SendCredentialCallback& send_callback, |
| 296 const autofill::PasswordForm* form) { |
289 password_manager::CredentialInfo info; | 297 password_manager::CredentialInfo info; |
290 if (form) { | 298 if (form) { |
291 password_manager::CredentialType type_to_return = | 299 password_manager::CredentialType type_to_return = |
292 form->federation_origin.unique() | 300 form->federation_origin.unique() |
293 ? password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD | 301 ? password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD |
294 : password_manager::CredentialType::CREDENTIAL_TYPE_FEDERATED; | 302 : password_manager::CredentialType::CREDENTIAL_TYPE_FEDERATED; |
295 info = password_manager::CredentialInfo(*form, type_to_return); | 303 info = password_manager::CredentialInfo(*form, type_to_return); |
296 // TODO(vasilii): update |skip_zero_click| in the store (crbug.com/594110). | 304 // TODO(vasilii): update |skip_zero_click| in the store (crbug.com/594110). |
297 } | 305 } |
298 SendCredential(request_id, info); | 306 SendCredential(send_callback, info); |
299 } | 307 } |
300 | 308 |
301 password_manager::PasswordManagerClient* CredentialManager::client() const { | 309 password_manager::PasswordManagerClient* CredentialManager::client() const { |
302 return client_; | 310 return client_; |
303 } | 311 } |
304 | 312 |
305 autofill::PasswordForm CredentialManager::GetSynthesizedFormForOrigin() const { | 313 autofill::PasswordForm CredentialManager::GetSynthesizedFormForOrigin() const { |
306 autofill::PasswordForm synthetic_form; | 314 autofill::PasswordForm synthetic_form; |
307 synthetic_form.origin = web_state()->GetLastCommittedURL().GetOrigin(); | 315 synthetic_form.origin = web_state()->GetLastCommittedURL().GetOrigin(); |
308 synthetic_form.signon_realm = synthetic_form.origin.spec(); | 316 synthetic_form.signon_realm = synthetic_form.origin.spec(); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { | 377 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { |
370 web::URLVerificationTrustLevel trust_level = | 378 web::URLVerificationTrustLevel trust_level = |
371 web::URLVerificationTrustLevel::kNone; | 379 web::URLVerificationTrustLevel::kNone; |
372 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); | 380 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); |
373 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { | 381 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { |
374 *page_url = possibly_untrusted_url; | 382 *page_url = possibly_untrusted_url; |
375 return true; | 383 return true; |
376 } | 384 } |
377 return false; | 385 return false; |
378 } | 386 } |
OLD | NEW |