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" |
| 13 #include "chrome/browser/browser_process.h" |
12 #include "chrome/browser/google_apis/auth_service_observer.h" | 14 #include "chrome/browser/google_apis/auth_service_observer.h" |
13 #include "chrome/browser/google_apis/base_operations.h" | 15 #include "chrome/browser/google_apis/base_operations.h" |
14 #include "chrome/browser/google_apis/task_util.h" | 16 #include "chrome/browser/google_apis/task_util.h" |
15 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.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" |
| 26 #include "google_apis/gaia/gaia_urls.h" |
| 27 #include "google_apis/gaia/google_service_auth_error.h" |
| 28 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
24 | 29 |
25 #if defined(OS_CHROMEOS) | 30 #if defined(OS_CHROMEOS) |
26 #include "chrome/browser/chromeos/login/user_manager.h" | 31 #include "chrome/browser/chromeos/login/user_manager.h" |
27 #endif // OS_CHROMEOS | 32 #endif // OS_CHROMEOS |
28 | 33 |
29 using content::BrowserThread; | 34 using content::BrowserThread; |
30 | 35 |
31 namespace google_apis { | 36 namespace google_apis { |
32 | 37 |
| 38 namespace { |
| 39 |
| 40 // Used for success ratio histograms. 0 for failure, 1 for success, |
| 41 // 2 for no connection (likely offline). |
| 42 const int kSuccessRatioHistogramFailure = 0; |
| 43 const int kSuccessRatioHistogramSuccess = 1; |
| 44 const int kSuccessRatioHistogramNoConnection = 2; |
| 45 const int kSuccessRatioHistogramMaxValue = 3; // The max value is exclusive. |
| 46 |
| 47 } // namespace |
| 48 |
| 49 // OAuth2 authorization token retrieval operation. |
| 50 class AuthOperation : public OperationRegistry::Operation, |
| 51 public OAuth2AccessTokenConsumer { |
| 52 public: |
| 53 AuthOperation(OperationRegistry* registry, |
| 54 const AuthStatusCallback& callback, |
| 55 const std::vector<std::string>& scopes, |
| 56 const std::string& refresh_token); |
| 57 virtual ~AuthOperation(); |
| 58 void Start(); |
| 59 |
| 60 // Overridden from OAuth2AccessTokenConsumer: |
| 61 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 62 const base::Time& expiration_time) OVERRIDE; |
| 63 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 64 |
| 65 // Overridden from OperationRegistry::Operation |
| 66 virtual void DoCancel() OVERRIDE; |
| 67 |
| 68 private: |
| 69 std::string refresh_token_; |
| 70 AuthStatusCallback callback_; |
| 71 std::vector<std::string> scopes_; |
| 72 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; |
| 73 |
| 74 DISALLOW_COPY_AND_ASSIGN(AuthOperation); |
| 75 }; |
| 76 |
| 77 AuthOperation::AuthOperation(OperationRegistry* registry, |
| 78 const AuthStatusCallback& callback, |
| 79 const std::vector<std::string>& scopes, |
| 80 const std::string& refresh_token) |
| 81 : OperationRegistry::Operation(registry), |
| 82 refresh_token_(refresh_token), |
| 83 callback_(callback), |
| 84 scopes_(scopes) { |
| 85 } |
| 86 |
| 87 AuthOperation::~AuthOperation() {} |
| 88 |
| 89 void AuthOperation::Start() { |
| 90 DCHECK(!refresh_token_.empty()); |
| 91 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( |
| 92 this, g_browser_process->system_request_context())); |
| 93 NotifyStart(); |
| 94 oauth2_access_token_fetcher_->Start( |
| 95 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), |
| 96 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), |
| 97 refresh_token_, |
| 98 scopes_); |
| 99 } |
| 100 |
| 101 void AuthOperation::DoCancel() { |
| 102 oauth2_access_token_fetcher_->CancelRequest(); |
| 103 if (!callback_.is_null()) |
| 104 callback_.Run(GDATA_CANCELLED, std::string()); |
| 105 } |
| 106 |
| 107 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token |
| 108 // used to start fetching user data. |
| 109 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, |
| 110 const base::Time& expiration_time) { |
| 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 112 |
| 113 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 114 kSuccessRatioHistogramSuccess, |
| 115 kSuccessRatioHistogramMaxValue); |
| 116 |
| 117 callback_.Run(HTTP_SUCCESS, access_token); |
| 118 NotifyFinish(OPERATION_COMPLETED); |
| 119 } |
| 120 |
| 121 // Callback for OAuth2AccessTokenFetcher on failure. |
| 122 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 124 |
| 125 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " |
| 126 << error.ToString(); |
| 127 |
| 128 // There are many ways to fail, but if the failure is due to connection, |
| 129 // it's likely that the device is off-line. We treat the error differently |
| 130 // so that the file manager works while off-line. |
| 131 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { |
| 132 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 133 kSuccessRatioHistogramNoConnection, |
| 134 kSuccessRatioHistogramMaxValue); |
| 135 callback_.Run(GDATA_NO_CONNECTION, std::string()); |
| 136 } else { |
| 137 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 138 kSuccessRatioHistogramFailure, |
| 139 kSuccessRatioHistogramMaxValue); |
| 140 callback_.Run(HTTP_UNAUTHORIZED, std::string()); |
| 141 } |
| 142 NotifyFinish(OPERATION_FAILED); |
| 143 } |
| 144 |
33 void AuthService::Initialize(Profile* profile) { | 145 void AuthService::Initialize(Profile* profile) { |
34 profile_ = profile; | 146 profile_ = profile; |
35 // Get OAuth2 refresh token (if we have any) and register for its updates. | 147 // Get OAuth2 refresh token (if we have any) and register for its updates. |
36 TokenService* service = TokenServiceFactory::GetForProfile(profile_); | 148 TokenService* service = TokenServiceFactory::GetForProfile(profile_); |
37 refresh_token_ = service->GetOAuth2LoginRefreshToken(); | 149 refresh_token_ = service->GetOAuth2LoginRefreshToken(); |
38 registrar_.Add(this, | 150 registrar_.Add(this, |
39 chrome::NOTIFICATION_TOKEN_AVAILABLE, | 151 chrome::NOTIFICATION_TOKEN_AVAILABLE, |
40 content::Source<TokenService>(service)); | 152 content::Source<TokenService>(service)); |
41 registrar_.Add(this, | 153 registrar_.Add(this, |
42 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, | 154 chrome::NOTIFICATION_TOKEN_REQUEST_FAILED, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 #endif // OS_CHROMEOS | 257 #endif // OS_CHROMEOS |
146 | 258 |
147 // Authentication cannot be done with the incognito mode profile. | 259 // Authentication cannot be done with the incognito mode profile. |
148 if (profile->IsOffTheRecord()) | 260 if (profile->IsOffTheRecord()) |
149 return false; | 261 return false; |
150 | 262 |
151 return true; | 263 return true; |
152 } | 264 } |
153 | 265 |
154 } // namespace google_apis | 266 } // namespace google_apis |
OLD | NEW |