| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 | 38 |
| 39 void DeviceOAuth2TokenService::OnValidationCompleted( | 39 void DeviceOAuth2TokenService::OnValidationCompleted( |
| 40 GoogleServiceAuthError::State error) { | 40 GoogleServiceAuthError::State error) { |
| 41 if (error == GoogleServiceAuthError::NONE) | 41 if (error == GoogleServiceAuthError::NONE) |
| 42 FlushPendingRequests(true, GoogleServiceAuthError::NONE); | 42 FlushPendingRequests(true, GoogleServiceAuthError::NONE); |
| 43 else | 43 else |
| 44 FlushPendingRequests(false, error); | 44 FlushPendingRequests(false, error); |
| 45 } | 45 } |
| 46 | 46 |
| 47 DeviceOAuth2TokenService::DeviceOAuth2TokenService( | 47 DeviceOAuth2TokenService::DeviceOAuth2TokenService( |
| 48 DeviceOAuth2TokenServiceDelegate* delegate) | 48 std::unique_ptr<DeviceOAuth2TokenServiceDelegate> delegate) |
| 49 : OAuth2TokenService(delegate), | 49 : OAuth2TokenService(std::move(delegate)) { |
| 50 delegate_(static_cast<DeviceOAuth2TokenServiceDelegate*>(delegate)) { | 50 GetDeviceDelegate()->SetValidationStatusDelegate(this); |
| 51 delegate_->SetValidationStatusDelegate(this); | |
| 52 } | 51 } |
| 53 | 52 |
| 54 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { | 53 DeviceOAuth2TokenService::~DeviceOAuth2TokenService() { |
| 55 delegate_->SetValidationStatusDelegate(nullptr); | 54 GetDeviceDelegate()->SetValidationStatusDelegate(nullptr); |
| 56 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); | 55 FlushPendingRequests(false, GoogleServiceAuthError::REQUEST_CANCELED); |
| 57 } | 56 } |
| 58 | 57 |
| 59 // static | 58 // static |
| 60 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { | 59 void DeviceOAuth2TokenService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 61 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, | 60 registry->RegisterStringPref(prefs::kDeviceRobotAnyApiRefreshToken, |
| 62 std::string()); | 61 std::string()); |
| 63 } | 62 } |
| 64 | 63 |
| 65 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( | 64 void DeviceOAuth2TokenService::SetAndSaveRefreshToken( |
| 66 const std::string& refresh_token, | 65 const std::string& refresh_token, |
| 67 const StatusCallback& result_callback) { | 66 const StatusCallback& result_callback) { |
| 68 delegate_->SetAndSaveRefreshToken(refresh_token, result_callback); | 67 GetDeviceDelegate()->SetAndSaveRefreshToken(refresh_token, result_callback); |
| 69 } | 68 } |
| 70 | 69 |
| 71 std::string DeviceOAuth2TokenService::GetRobotAccountId() const { | 70 std::string DeviceOAuth2TokenService::GetRobotAccountId() const { |
| 72 return delegate_->GetRobotAccountId(); | 71 return GetDeviceDelegate()->GetRobotAccountId(); |
| 73 } | 72 } |
| 74 | 73 |
| 75 void DeviceOAuth2TokenService::FetchOAuth2Token( | 74 void DeviceOAuth2TokenService::FetchOAuth2Token( |
| 76 RequestImpl* request, | 75 RequestImpl* request, |
| 77 const std::string& account_id, | 76 const std::string& account_id, |
| 78 net::URLRequestContextGetter* getter, | 77 net::URLRequestContextGetter* getter, |
| 79 const std::string& client_id, | 78 const std::string& client_id, |
| 80 const std::string& client_secret, | 79 const std::string& client_secret, |
| 81 const ScopeSet& scopes) { | 80 const ScopeSet& scopes) { |
| 82 switch (delegate_->state_) { | 81 switch (GetDeviceDelegate()->state_) { |
| 83 case DeviceOAuth2TokenServiceDelegate::STATE_VALIDATION_PENDING: | 82 case DeviceOAuth2TokenServiceDelegate::STATE_VALIDATION_PENDING: |
| 84 // If this is the first request for a token, start validation. | 83 // If this is the first request for a token, start validation. |
| 85 delegate_->StartValidation(); | 84 GetDeviceDelegate()->StartValidation(); |
| 86 // fall through. | 85 // fall through. |
| 87 case DeviceOAuth2TokenServiceDelegate::STATE_LOADING: | 86 case DeviceOAuth2TokenServiceDelegate::STATE_LOADING: |
| 88 case DeviceOAuth2TokenServiceDelegate::STATE_VALIDATION_STARTED: | 87 case DeviceOAuth2TokenServiceDelegate::STATE_VALIDATION_STARTED: |
| 89 // Add a pending request that will be satisfied once validation completes. | 88 // Add a pending request that will be satisfied once validation completes. |
| 90 pending_requests_.push_back(new PendingRequest( | 89 pending_requests_.push_back(new PendingRequest( |
| 91 request->AsWeakPtr(), client_id, client_secret, scopes)); | 90 request->AsWeakPtr(), client_id, client_secret, scopes)); |
| 92 delegate_->RequestValidation(); | 91 GetDeviceDelegate()->RequestValidation(); |
| 93 return; | 92 return; |
| 94 case DeviceOAuth2TokenServiceDelegate::STATE_NO_TOKEN: | 93 case DeviceOAuth2TokenServiceDelegate::STATE_NO_TOKEN: |
| 95 FailRequest(request, GoogleServiceAuthError::USER_NOT_SIGNED_UP); | 94 FailRequest(request, GoogleServiceAuthError::USER_NOT_SIGNED_UP); |
| 96 return; | 95 return; |
| 97 case DeviceOAuth2TokenServiceDelegate::STATE_TOKEN_INVALID: | 96 case DeviceOAuth2TokenServiceDelegate::STATE_TOKEN_INVALID: |
| 98 FailRequest(request, GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 97 FailRequest(request, GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
| 99 return; | 98 return; |
| 100 case DeviceOAuth2TokenServiceDelegate::STATE_TOKEN_VALID: | 99 case DeviceOAuth2TokenServiceDelegate::STATE_TOKEN_VALID: |
| 101 // Pass through to OAuth2TokenService to satisfy the request. | 100 // Pass through to OAuth2TokenService to satisfy the request. |
| 102 OAuth2TokenService::FetchOAuth2Token( | 101 OAuth2TokenService::FetchOAuth2Token( |
| 103 request, account_id, getter, client_id, client_secret, scopes); | 102 request, account_id, getter, client_id, client_secret, scopes); |
| 104 return; | 103 return; |
| 105 } | 104 } |
| 106 | 105 |
| 107 NOTREACHED() << "Unexpected state " << delegate_->state_; | 106 NOTREACHED() << "Unexpected state " << GetDeviceDelegate()->state_; |
| 108 } | 107 } |
| 109 | 108 |
| 110 void DeviceOAuth2TokenService::FlushPendingRequests( | 109 void DeviceOAuth2TokenService::FlushPendingRequests( |
| 111 bool token_is_valid, | 110 bool token_is_valid, |
| 112 GoogleServiceAuthError::State error) { | 111 GoogleServiceAuthError::State error) { |
| 113 std::vector<PendingRequest*> requests; | 112 std::vector<PendingRequest*> requests; |
| 114 requests.swap(pending_requests_); | 113 requests.swap(pending_requests_); |
| 115 for (std::vector<PendingRequest*>::iterator request(requests.begin()); | 114 for (std::vector<PendingRequest*>::iterator request(requests.begin()); |
| 116 request != requests.end(); | 115 request != requests.end(); |
| 117 ++request) { | 116 ++request) { |
| 118 std::unique_ptr<PendingRequest> scoped_request(*request); | 117 std::unique_ptr<PendingRequest> scoped_request(*request); |
| 119 if (!scoped_request->request) | 118 if (!scoped_request->request) |
| 120 continue; | 119 continue; |
| 121 | 120 |
| 122 if (token_is_valid) { | 121 if (token_is_valid) { |
| 123 OAuth2TokenService::FetchOAuth2Token( | 122 OAuth2TokenService::FetchOAuth2Token( |
| 124 scoped_request->request.get(), | 123 scoped_request->request.get(), |
| 125 scoped_request->request->GetAccountId(), | 124 scoped_request->request->GetAccountId(), |
| 126 delegate_->GetRequestContext(), scoped_request->client_id, | 125 GetDeviceDelegate()->GetRequestContext(), scoped_request->client_id, |
| 127 scoped_request->client_secret, scoped_request->scopes); | 126 scoped_request->client_secret, scoped_request->scopes); |
| 128 } else { | 127 } else { |
| 129 FailRequest(scoped_request->request.get(), error); | 128 FailRequest(scoped_request->request.get(), error); |
| 130 } | 129 } |
| 131 } | 130 } |
| 132 } | 131 } |
| 133 | 132 |
| 134 void DeviceOAuth2TokenService::FailRequest( | 133 void DeviceOAuth2TokenService::FailRequest( |
| 135 RequestImpl* request, | 134 RequestImpl* request, |
| 136 GoogleServiceAuthError::State error) { | 135 GoogleServiceAuthError::State error) { |
| 137 GoogleServiceAuthError auth_error(error); | 136 GoogleServiceAuthError auth_error(error); |
| 138 base::ThreadTaskRunnerHandle::Get()->PostTask( | 137 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 139 FROM_HERE, base::Bind(&RequestImpl::InformConsumer, request->AsWeakPtr(), | 138 FROM_HERE, base::Bind(&RequestImpl::InformConsumer, request->AsWeakPtr(), |
| 140 auth_error, std::string(), base::Time())); | 139 auth_error, std::string(), base::Time())); |
| 141 } | 140 } |
| 142 | 141 |
| 142 DeviceOAuth2TokenServiceDelegate* |
| 143 DeviceOAuth2TokenService::GetDeviceDelegate() { |
| 144 return static_cast<DeviceOAuth2TokenServiceDelegate*>(GetDelegate()); |
| 145 } |
| 146 |
| 147 const DeviceOAuth2TokenServiceDelegate* |
| 148 DeviceOAuth2TokenService::GetDeviceDelegate() const { |
| 149 return static_cast<const DeviceOAuth2TokenServiceDelegate*>(GetDelegate()); |
| 150 } |
| 151 |
| 143 } // namespace chromeos | 152 } // namespace chromeos |
| OLD | NEW |