Chromium Code Reviews| 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" |
| 11 #include "base/message_loop_proxy.h" | 11 #include "base/message_loop_proxy.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "chrome/browser/google_apis/auth_service_observer.h" | 13 #include "chrome/browser/google_apis/auth_service_observer.h" |
| 14 #include "chrome/browser/google_apis/base_operations.h" | 14 #include "chrome/browser/google_apis/base_operations.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/about_signin_internals.h" | |
| 17 #include "chrome/browser/signin/about_signin_internals_factory.h" | |
| 16 #include "chrome/browser/signin/token_service.h" | 18 #include "chrome/browser/signin/token_service.h" |
| 17 #include "chrome/browser/signin/token_service_factory.h" | 19 #include "chrome/browser/signin/token_service_factory.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/notification_types.h" | 24 #include "content/public/browser/notification_types.h" |
| 23 #include "google_apis/gaia/gaia_constants.h" | 25 #include "google_apis/gaia/gaia_constants.h" |
| 24 #include "google_apis/gaia/gaia_urls.h" | 26 #include "google_apis/gaia/gaia_urls.h" |
| 25 #include "google_apis/gaia/google_service_auth_error.h" | 27 #include "google_apis/gaia/google_service_auth_error.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 45 } // namespace | 47 } // namespace |
| 46 | 48 |
| 47 // OAuth2 authorization token retrieval operation. | 49 // OAuth2 authorization token retrieval operation. |
| 48 class AuthOperation : public OperationRegistry::Operation, | 50 class AuthOperation : public OperationRegistry::Operation, |
| 49 public OAuth2AccessTokenConsumer { | 51 public OAuth2AccessTokenConsumer { |
| 50 public: | 52 public: |
| 51 AuthOperation(OperationRegistry* registry, | 53 AuthOperation(OperationRegistry* registry, |
| 52 net::URLRequestContextGetter* url_request_context_getter, | 54 net::URLRequestContextGetter* url_request_context_getter, |
| 53 const AuthStatusCallback& callback, | 55 const AuthStatusCallback& callback, |
| 54 const std::vector<std::string>& scopes, | 56 const std::vector<std::string>& scopes, |
| 55 const std::string& refresh_token); | 57 const std::string& refresh_token, |
| 58 Profile* profile); | |
| 56 virtual ~AuthOperation(); | 59 virtual ~AuthOperation(); |
| 57 void Start(); | 60 void Start(); |
| 58 | 61 |
| 59 // Overridden from OAuth2AccessTokenConsumer: | 62 // Overridden from OAuth2AccessTokenConsumer: |
| 60 virtual void OnGetTokenSuccess(const std::string& access_token, | 63 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 61 const base::Time& expiration_time) OVERRIDE; | 64 const base::Time& expiration_time) OVERRIDE; |
| 62 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 65 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 63 | 66 |
| 64 // Overridden from OperationRegistry::Operation | 67 // Overridden from OperationRegistry::Operation |
| 65 virtual void DoCancel() OVERRIDE; | 68 virtual void DoCancel() OVERRIDE; |
| 66 | 69 |
| 67 private: | 70 private: |
| 68 net::URLRequestContextGetter* url_request_context_getter_; | 71 net::URLRequestContextGetter* url_request_context_getter_; |
| 69 std::string refresh_token_; | 72 std::string refresh_token_; |
| 70 AuthStatusCallback callback_; | 73 AuthStatusCallback callback_; |
| 71 std::vector<std::string> scopes_; | 74 std::vector<std::string> scopes_; |
| 72 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | 75 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; |
| 73 | 76 |
| 77 // TODO(vishwath): Once we move to OAuth2TokenService, this class no longer | |
| 78 // needs a profile var. Remove and refactor ctors()/call-sites accordingly. | |
|
James Hawkins
2012/12/21 23:31:04
nit: Document the member variable (also, state 'we
| |
| 79 Profile* profile_; | |
| 80 | |
| 74 DISALLOW_COPY_AND_ASSIGN(AuthOperation); | 81 DISALLOW_COPY_AND_ASSIGN(AuthOperation); |
| 75 }; | 82 }; |
| 76 | 83 |
| 77 AuthOperation::AuthOperation( | 84 AuthOperation::AuthOperation( |
| 78 OperationRegistry* registry, | 85 OperationRegistry* registry, |
| 79 net::URLRequestContextGetter* url_request_context_getter, | 86 net::URLRequestContextGetter* url_request_context_getter, |
| 80 const AuthStatusCallback& callback, | 87 const AuthStatusCallback& callback, |
| 81 const std::vector<std::string>& scopes, | 88 const std::vector<std::string>& scopes, |
| 82 const std::string& refresh_token) | 89 const std::string& refresh_token, |
| 90 Profile* profile) | |
| 83 : OperationRegistry::Operation(registry), | 91 : OperationRegistry::Operation(registry), |
| 84 url_request_context_getter_(url_request_context_getter), | 92 url_request_context_getter_(url_request_context_getter), |
| 85 refresh_token_(refresh_token), | 93 refresh_token_(refresh_token), |
| 86 callback_(callback), | 94 callback_(callback), |
| 87 scopes_(scopes) { | 95 scopes_(scopes), |
| 96 profile_(profile) { | |
| 88 DCHECK(!callback_.is_null()); | 97 DCHECK(!callback_.is_null()); |
| 89 } | 98 } |
| 90 | 99 |
| 91 AuthOperation::~AuthOperation() {} | 100 AuthOperation::~AuthOperation() {} |
| 92 | 101 |
| 93 void AuthOperation::Start() { | 102 void AuthOperation::Start() { |
| 94 DCHECK(!refresh_token_.empty()); | 103 DCHECK(!refresh_token_.empty()); |
| 95 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( | 104 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( |
| 96 this, url_request_context_getter_)); | 105 this, url_request_context_getter_)); |
| 97 NotifyStart(); | 106 NotifyStart(); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 110 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token | 119 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token |
| 111 // used to start fetching user data. | 120 // used to start fetching user data. |
| 112 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, | 121 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, |
| 113 const base::Time& expiration_time) { | 122 const base::Time& expiration_time) { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 115 | 124 |
| 116 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 125 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 117 kSuccessRatioHistogramSuccess, | 126 kSuccessRatioHistogramSuccess, |
| 118 kSuccessRatioHistogramMaxValue); | 127 kSuccessRatioHistogramMaxValue); |
| 119 | 128 |
| 129 // TODO(vishwath): This can be removed once this class is migrated to use | |
| 130 // OAuth2TokenService. | |
| 131 AboutSigninInternalsFactory::GetForProfile(profile_)-> | |
| 132 NotifyTokenReceivedSuccess(signin_internals_util::kAuthOperationToken, | |
| 133 access_token, | |
| 134 true); | |
| 135 | |
| 120 callback_.Run(HTTP_SUCCESS, access_token); | 136 callback_.Run(HTTP_SUCCESS, access_token); |
| 121 NotifyFinish(OPERATION_COMPLETED); | 137 NotifyFinish(OPERATION_COMPLETED); |
| 122 } | 138 } |
| 123 | 139 |
| 124 // Callback for OAuth2AccessTokenFetcher on failure. | 140 // Callback for OAuth2AccessTokenFetcher on failure. |
| 125 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { | 141 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 | 143 |
| 128 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " | 144 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " |
| 129 << error.ToString(); | 145 << error.ToString(); |
| 130 | 146 |
| 147 // TODO(vishwath): This needs to be converted to use OAuth2TokenService. | |
| 148 AboutSigninInternalsFactory::GetForProfile(profile_)-> | |
| 149 NotifyTokenReceivedFailure(signin_internals_util::kAuthOperationToken, | |
| 150 error.ToString()); | |
| 151 | |
| 131 // There are many ways to fail, but if the failure is due to connection, | 152 // There are many ways to fail, but if the failure is due to connection, |
| 132 // it's likely that the device is off-line. We treat the error differently | 153 // it's likely that the device is off-line. We treat the error differently |
| 133 // so that the file manager works while off-line. | 154 // so that the file manager works while off-line. |
| 134 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { | 155 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { |
| 135 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 156 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 136 kSuccessRatioHistogramNoConnection, | 157 kSuccessRatioHistogramNoConnection, |
| 137 kSuccessRatioHistogramMaxValue); | 158 kSuccessRatioHistogramMaxValue); |
| 138 callback_.Run(GDATA_NO_CONNECTION, std::string()); | 159 callback_.Run(GDATA_NO_CONNECTION, std::string()); |
| 139 } else { | 160 } else { |
| 140 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 161 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 187 relay_proxy->PostTask(FROM_HERE, | 208 relay_proxy->PostTask(FROM_HERE, |
| 188 base::Bind(callback, HTTP_SUCCESS, access_token_)); | 209 base::Bind(callback, HTTP_SUCCESS, access_token_)); |
| 189 } else if (HasRefreshToken()) { | 210 } else if (HasRefreshToken()) { |
| 190 // We have refresh token, let's get an access token. | 211 // We have refresh token, let's get an access token. |
| 191 (new AuthOperation(registry, | 212 (new AuthOperation(registry, |
| 192 url_request_context_getter_, | 213 url_request_context_getter_, |
| 193 base::Bind(&AuthService::OnAuthCompleted, | 214 base::Bind(&AuthService::OnAuthCompleted, |
| 194 weak_ptr_factory_.GetWeakPtr(), | 215 weak_ptr_factory_.GetWeakPtr(), |
| 195 callback), | 216 callback), |
| 196 scopes_, | 217 scopes_, |
| 197 refresh_token_))->Start(); | 218 refresh_token_, |
| 219 profile_))->Start(); | |
| 198 } else { | 220 } else { |
| 199 relay_proxy->PostTask(FROM_HERE, | 221 relay_proxy->PostTask(FROM_HERE, |
| 200 base::Bind(callback, GDATA_NOT_READY, std::string())); | 222 base::Bind(callback, GDATA_NOT_READY, std::string())); |
| 201 } | 223 } |
| 202 } | 224 } |
| 203 | 225 |
| 204 bool AuthService::HasAccessToken() const { | 226 bool AuthService::HasAccessToken() const { |
| 205 return !access_token_.empty(); | 227 return !access_token_.empty(); |
| 206 } | 228 } |
| 207 | 229 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 #endif // OS_CHROMEOS | 293 #endif // OS_CHROMEOS |
| 272 | 294 |
| 273 // Authentication cannot be done with the incognito mode profile. | 295 // Authentication cannot be done with the incognito mode profile. |
| 274 if (profile->IsOffTheRecord()) | 296 if (profile->IsOffTheRecord()) |
| 275 return false; | 297 return false; |
| 276 | 298 |
| 277 return true; | 299 return true; |
| 278 } | 300 } |
| 279 | 301 |
| 280 } // namespace google_apis | 302 } // namespace google_apis |
| OLD | NEW |