| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/google_apis/auth_service.h" | 5 #include "chrome/browser/google_apis/auth_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 12 matching lines...) Expand all Loading... |
| 23 const int kSuccessRatioHistogramFailure = 0; | 23 const int kSuccessRatioHistogramFailure = 0; |
| 24 const int kSuccessRatioHistogramSuccess = 1; | 24 const int kSuccessRatioHistogramSuccess = 1; |
| 25 const int kSuccessRatioHistogramNoConnection = 2; | 25 const int kSuccessRatioHistogramNoConnection = 2; |
| 26 const int kSuccessRatioHistogramTemporaryFailure = 3; | 26 const int kSuccessRatioHistogramTemporaryFailure = 3; |
| 27 const int kSuccessRatioHistogramMaxValue = 4; // The max value is exclusive. | 27 const int kSuccessRatioHistogramMaxValue = 4; // The max value is exclusive. |
| 28 | 28 |
| 29 // OAuth2 authorization token retrieval request. | 29 // OAuth2 authorization token retrieval request. |
| 30 class AuthRequest : public OAuth2TokenService::Consumer { | 30 class AuthRequest : public OAuth2TokenService::Consumer { |
| 31 public: | 31 public: |
| 32 AuthRequest(OAuth2TokenService* oauth2_token_service, | 32 AuthRequest(OAuth2TokenService* oauth2_token_service, |
| 33 const std::string& account_id, |
| 33 net::URLRequestContextGetter* url_request_context_getter, | 34 net::URLRequestContextGetter* url_request_context_getter, |
| 34 const AuthStatusCallback& callback, | 35 const AuthStatusCallback& callback, |
| 35 const std::vector<std::string>& scopes); | 36 const std::vector<std::string>& scopes); |
| 36 virtual ~AuthRequest(); | 37 virtual ~AuthRequest(); |
| 37 | 38 |
| 38 private: | 39 private: |
| 39 // Overridden from OAuth2TokenService::Consumer: | 40 // Overridden from OAuth2TokenService::Consumer: |
| 40 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 41 virtual void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 41 const std::string& access_token, | 42 const std::string& access_token, |
| 42 const base::Time& expiration_time) OVERRIDE; | 43 const base::Time& expiration_time) OVERRIDE; |
| 43 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 44 virtual void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 44 const GoogleServiceAuthError& error) OVERRIDE; | 45 const GoogleServiceAuthError& error) OVERRIDE; |
| 45 | 46 |
| 46 AuthStatusCallback callback_; | 47 AuthStatusCallback callback_; |
| 47 scoped_ptr<OAuth2TokenService::Request> request_; | 48 scoped_ptr<OAuth2TokenService::Request> request_; |
| 48 base::ThreadChecker thread_checker_; | 49 base::ThreadChecker thread_checker_; |
| 49 | 50 |
| 50 DISALLOW_COPY_AND_ASSIGN(AuthRequest); | 51 DISALLOW_COPY_AND_ASSIGN(AuthRequest); |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 AuthRequest::AuthRequest( | 54 AuthRequest::AuthRequest( |
| 54 OAuth2TokenService* oauth2_token_service, | 55 OAuth2TokenService* oauth2_token_service, |
| 56 const std::string& account_id, |
| 55 net::URLRequestContextGetter* url_request_context_getter, | 57 net::URLRequestContextGetter* url_request_context_getter, |
| 56 const AuthStatusCallback& callback, | 58 const AuthStatusCallback& callback, |
| 57 const std::vector<std::string>& scopes) | 59 const std::vector<std::string>& scopes) |
| 58 : callback_(callback) { | 60 : callback_(callback) { |
| 59 DCHECK(!callback_.is_null()); | 61 DCHECK(!callback_.is_null()); |
| 60 request_ = oauth2_token_service-> | 62 request_ = oauth2_token_service-> |
| 61 StartRequestWithContext( | 63 StartRequestWithContext( |
| 64 account_id, |
| 62 url_request_context_getter, | 65 url_request_context_getter, |
| 63 OAuth2TokenService::ScopeSet(scopes.begin(), scopes.end()), | 66 OAuth2TokenService::ScopeSet(scopes.begin(), scopes.end()), |
| 64 this); | 67 this); |
| 65 } | 68 } |
| 66 | 69 |
| 67 AuthRequest::~AuthRequest() {} | 70 AuthRequest::~AuthRequest() {} |
| 68 | 71 |
| 69 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token | 72 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token |
| 70 // used to start fetching user data. | 73 // used to start fetching user data. |
| 71 void AuthRequest::OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 74 void AuthRequest::OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 kSuccessRatioHistogramMaxValue); | 113 kSuccessRatioHistogramMaxValue); |
| 111 callback_.Run(HTTP_UNAUTHORIZED, std::string()); | 114 callback_.Run(HTTP_UNAUTHORIZED, std::string()); |
| 112 } | 115 } |
| 113 delete this; | 116 delete this; |
| 114 } | 117 } |
| 115 | 118 |
| 116 } // namespace | 119 } // namespace |
| 117 | 120 |
| 118 AuthService::AuthService( | 121 AuthService::AuthService( |
| 119 OAuth2TokenService* oauth2_token_service, | 122 OAuth2TokenService* oauth2_token_service, |
| 123 const std::string& account_id, |
| 120 net::URLRequestContextGetter* url_request_context_getter, | 124 net::URLRequestContextGetter* url_request_context_getter, |
| 121 const std::vector<std::string>& scopes) | 125 const std::vector<std::string>& scopes) |
| 122 : oauth2_token_service_(oauth2_token_service), | 126 : oauth2_token_service_(oauth2_token_service), |
| 127 account_id_(account_id), |
| 123 url_request_context_getter_(url_request_context_getter), | 128 url_request_context_getter_(url_request_context_getter), |
| 124 scopes_(scopes), | 129 scopes_(scopes), |
| 125 weak_ptr_factory_(this) { | 130 weak_ptr_factory_(this) { |
| 126 DCHECK(oauth2_token_service); | 131 DCHECK(oauth2_token_service); |
| 127 | 132 |
| 128 // Get OAuth2 refresh token (if we have any) and register for its updates. | 133 // Get OAuth2 refresh token (if we have any) and register for its updates. |
| 129 oauth2_token_service_->AddObserver(this); | 134 oauth2_token_service_->AddObserver(this); |
| 130 has_refresh_token_ = oauth2_token_service_->RefreshTokenIsAvailable(); | 135 has_refresh_token_ = oauth2_token_service_->RefreshTokenIsAvailable( |
| 136 account_id_); |
| 131 } | 137 } |
| 132 | 138 |
| 133 AuthService::~AuthService() { | 139 AuthService::~AuthService() { |
| 134 oauth2_token_service_->RemoveObserver(this); | 140 oauth2_token_service_->RemoveObserver(this); |
| 135 } | 141 } |
| 136 | 142 |
| 137 void AuthService::StartAuthentication(const AuthStatusCallback& callback) { | 143 void AuthService::StartAuthentication(const AuthStatusCallback& callback) { |
| 138 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 139 scoped_refptr<base::MessageLoopProxy> relay_proxy( | 145 scoped_refptr<base::MessageLoopProxy> relay_proxy( |
| 140 base::MessageLoopProxy::current()); | 146 base::MessageLoopProxy::current()); |
| 141 | 147 |
| 142 if (HasAccessToken()) { | 148 if (HasAccessToken()) { |
| 143 // We already have access token. Give it back to the caller asynchronously. | 149 // We already have access token. Give it back to the caller asynchronously. |
| 144 relay_proxy->PostTask(FROM_HERE, | 150 relay_proxy->PostTask(FROM_HERE, |
| 145 base::Bind(callback, HTTP_SUCCESS, access_token_)); | 151 base::Bind(callback, HTTP_SUCCESS, access_token_)); |
| 146 } else if (HasRefreshToken()) { | 152 } else if (HasRefreshToken()) { |
| 147 // We have refresh token, let's get an access token. | 153 // We have refresh token, let's get an access token. |
| 148 new AuthRequest(oauth2_token_service_, | 154 new AuthRequest(oauth2_token_service_, |
| 155 account_id_, |
| 149 url_request_context_getter_, | 156 url_request_context_getter_, |
| 150 base::Bind(&AuthService::OnAuthCompleted, | 157 base::Bind(&AuthService::OnAuthCompleted, |
| 151 weak_ptr_factory_.GetWeakPtr(), | 158 weak_ptr_factory_.GetWeakPtr(), |
| 152 callback), | 159 callback), |
| 153 scopes_); | 160 scopes_); |
| 154 } else { | 161 } else { |
| 155 relay_proxy->PostTask(FROM_HERE, | 162 relay_proxy->PostTask(FROM_HERE, |
| 156 base::Bind(callback, GDATA_NOT_READY, std::string())); | 163 base::Bind(callback, GDATA_NOT_READY, std::string())); |
| 157 } | 164 } |
| 158 } | 165 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 void AuthService::OnHandleRefreshToken(bool has_refresh_token) { | 230 void AuthService::OnHandleRefreshToken(bool has_refresh_token) { |
| 224 access_token_.clear(); | 231 access_token_.clear(); |
| 225 has_refresh_token_ = has_refresh_token; | 232 has_refresh_token_ = has_refresh_token; |
| 226 | 233 |
| 227 FOR_EACH_OBSERVER(AuthServiceObserver, | 234 FOR_EACH_OBSERVER(AuthServiceObserver, |
| 228 observers_, | 235 observers_, |
| 229 OnOAuth2RefreshTokenChanged()); | 236 OnOAuth2RefreshTokenChanged()); |
| 230 } | 237 } |
| 231 | 238 |
| 232 } // namespace google_apis | 239 } // namespace google_apis |
| OLD | NEW |