| 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 profile_. |
| 79 // A weak pointer to the profile associated with this service, if any. |
| 80 Profile* profile_; |
| 81 |
| 74 DISALLOW_COPY_AND_ASSIGN(AuthOperation); | 82 DISALLOW_COPY_AND_ASSIGN(AuthOperation); |
| 75 }; | 83 }; |
| 76 | 84 |
| 77 AuthOperation::AuthOperation( | 85 AuthOperation::AuthOperation( |
| 78 OperationRegistry* registry, | 86 OperationRegistry* registry, |
| 79 net::URLRequestContextGetter* url_request_context_getter, | 87 net::URLRequestContextGetter* url_request_context_getter, |
| 80 const AuthStatusCallback& callback, | 88 const AuthStatusCallback& callback, |
| 81 const std::vector<std::string>& scopes, | 89 const std::vector<std::string>& scopes, |
| 82 const std::string& refresh_token) | 90 const std::string& refresh_token, |
| 91 Profile* profile) |
| 83 : OperationRegistry::Operation(registry), | 92 : OperationRegistry::Operation(registry), |
| 84 url_request_context_getter_(url_request_context_getter), | 93 url_request_context_getter_(url_request_context_getter), |
| 85 refresh_token_(refresh_token), | 94 refresh_token_(refresh_token), |
| 86 callback_(callback), | 95 callback_(callback), |
| 87 scopes_(scopes) { | 96 scopes_(scopes), |
| 97 profile_(profile) { |
| 88 DCHECK(!callback_.is_null()); | 98 DCHECK(!callback_.is_null()); |
| 89 } | 99 } |
| 90 | 100 |
| 91 AuthOperation::~AuthOperation() {} | 101 AuthOperation::~AuthOperation() {} |
| 92 | 102 |
| 93 void AuthOperation::Start() { | 103 void AuthOperation::Start() { |
| 94 DCHECK(!refresh_token_.empty()); | 104 DCHECK(!refresh_token_.empty()); |
| 95 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( | 105 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( |
| 96 this, url_request_context_getter_)); | 106 this, url_request_context_getter_)); |
| 97 NotifyStart(); | 107 NotifyStart(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 110 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token | 120 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token |
| 111 // used to start fetching user data. | 121 // used to start fetching user data. |
| 112 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, | 122 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, |
| 113 const base::Time& expiration_time) { | 123 const base::Time& expiration_time) { |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 115 | 125 |
| 116 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 126 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 117 kSuccessRatioHistogramSuccess, | 127 kSuccessRatioHistogramSuccess, |
| 118 kSuccessRatioHistogramMaxValue); | 128 kSuccessRatioHistogramMaxValue); |
| 119 | 129 |
| 130 // TODO(vishwath): This can be removed once this class is migrated to use |
| 131 // OAuth2TokenService. |
| 132 AboutSigninInternalsFactory::GetForProfile(profile_)-> |
| 133 NotifyTokenReceivedSuccess(signin_internals_util::kAuthOperationToken, |
| 134 access_token, |
| 135 true); |
| 136 |
| 120 callback_.Run(HTTP_SUCCESS, access_token); | 137 callback_.Run(HTTP_SUCCESS, access_token); |
| 121 NotifyFinish(OPERATION_COMPLETED); | 138 NotifyFinish(OPERATION_COMPLETED); |
| 122 } | 139 } |
| 123 | 140 |
| 124 // Callback for OAuth2AccessTokenFetcher on failure. | 141 // Callback for OAuth2AccessTokenFetcher on failure. |
| 125 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { | 142 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 | 144 |
| 128 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " | 145 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " |
| 129 << error.ToString(); | 146 << error.ToString(); |
| 130 | 147 |
| 148 // TODO(vishwath): This needs to be converted to use OAuth2TokenService. |
| 149 AboutSigninInternalsFactory::GetForProfile(profile_)-> |
| 150 NotifyTokenReceivedFailure(signin_internals_util::kAuthOperationToken, |
| 151 error.ToString()); |
| 152 |
| 131 // There are many ways to fail, but if the failure is due to connection, | 153 // 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 | 154 // it's likely that the device is off-line. We treat the error differently |
| 133 // so that the file manager works while off-line. | 155 // so that the file manager works while off-line. |
| 134 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { | 156 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { |
| 135 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 157 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 136 kSuccessRatioHistogramNoConnection, | 158 kSuccessRatioHistogramNoConnection, |
| 137 kSuccessRatioHistogramMaxValue); | 159 kSuccessRatioHistogramMaxValue); |
| 138 callback_.Run(GDATA_NO_CONNECTION, std::string()); | 160 callback_.Run(GDATA_NO_CONNECTION, std::string()); |
| 139 } else { | 161 } else { |
| 140 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 162 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 relay_proxy->PostTask(FROM_HERE, | 209 relay_proxy->PostTask(FROM_HERE, |
| 188 base::Bind(callback, HTTP_SUCCESS, access_token_)); | 210 base::Bind(callback, HTTP_SUCCESS, access_token_)); |
| 189 } else if (HasRefreshToken()) { | 211 } else if (HasRefreshToken()) { |
| 190 // We have refresh token, let's get an access token. | 212 // We have refresh token, let's get an access token. |
| 191 (new AuthOperation(registry, | 213 (new AuthOperation(registry, |
| 192 url_request_context_getter_, | 214 url_request_context_getter_, |
| 193 base::Bind(&AuthService::OnAuthCompleted, | 215 base::Bind(&AuthService::OnAuthCompleted, |
| 194 weak_ptr_factory_.GetWeakPtr(), | 216 weak_ptr_factory_.GetWeakPtr(), |
| 195 callback), | 217 callback), |
| 196 scopes_, | 218 scopes_, |
| 197 refresh_token_))->Start(); | 219 refresh_token_, |
| 220 profile_))->Start(); |
| 198 } else { | 221 } else { |
| 199 relay_proxy->PostTask(FROM_HERE, | 222 relay_proxy->PostTask(FROM_HERE, |
| 200 base::Bind(callback, GDATA_NOT_READY, std::string())); | 223 base::Bind(callback, GDATA_NOT_READY, std::string())); |
| 201 } | 224 } |
| 202 } | 225 } |
| 203 | 226 |
| 204 bool AuthService::HasAccessToken() const { | 227 bool AuthService::HasAccessToken() const { |
| 205 return !access_token_.empty(); | 228 return !access_token_.empty(); |
| 206 } | 229 } |
| 207 | 230 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 #endif // OS_CHROMEOS | 294 #endif // OS_CHROMEOS |
| 272 | 295 |
| 273 // Authentication cannot be done with the incognito mode profile. | 296 // Authentication cannot be done with the incognito mode profile. |
| 274 if (profile->IsOffTheRecord()) | 297 if (profile->IsOffTheRecord()) |
| 275 return false; | 298 return false; |
| 276 | 299 |
| 277 return true; | 300 return true; |
| 278 } | 301 } |
| 279 | 302 |
| 280 } // namespace google_apis | 303 } // namespace google_apis |
| OLD | NEW |