| 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) { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { | 362 bool CredentialManager::GetUrlWithAbsoluteTrust(GURL* page_url) { |
| 356 web::URLVerificationTrustLevel trust_level = | 363 web::URLVerificationTrustLevel trust_level = |
| 357 web::URLVerificationTrustLevel::kNone; | 364 web::URLVerificationTrustLevel::kNone; |
| 358 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); | 365 const GURL possibly_untrusted_url(web_state()->GetCurrentURL(&trust_level)); |
| 359 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { | 366 if (trust_level == web::URLVerificationTrustLevel::kAbsolute) { |
| 360 *page_url = possibly_untrusted_url; | 367 *page_url = possibly_untrusted_url; |
| 361 return true; | 368 return true; |
| 362 } | 369 } |
| 363 return false; | 370 return false; |
| 364 } | 371 } |
| OLD | NEW |