| 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/base_operations.h" | 5 #include "chrome/browser/google_apis/base_operations.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 11 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| 12 #include "base/threading/sequenced_worker_pool.h" | 11 #include "base/threading/sequenced_worker_pool.h" |
| 13 #include "base/values.h" | 12 #include "base/values.h" |
| 14 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 15 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 16 #include "google_apis/gaia/gaia_urls.h" | |
| 17 #include "google_apis/gaia/google_service_auth_error.h" | |
| 18 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | |
| 19 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
| 20 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
| 21 #include "net/url_request/url_fetcher.h" | 17 #include "net/url_request/url_fetcher.h" |
| 22 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 23 | 19 |
| 24 using content::BrowserThread; | 20 using content::BrowserThread; |
| 25 using net::URLFetcher; | 21 using net::URLFetcher; |
| 26 | 22 |
| 27 namespace { | 23 namespace { |
| 28 | 24 |
| 29 // Used for success ratio histograms. 0 for failure, 1 for success, | |
| 30 // 2 for no connection (likely offline). | |
| 31 const int kSuccessRatioHistogramFailure = 0; | |
| 32 const int kSuccessRatioHistogramSuccess = 1; | |
| 33 const int kSuccessRatioHistogramNoConnection = 2; | |
| 34 const int kSuccessRatioHistogramMaxValue = 3; // The max value is exclusive. | |
| 35 | |
| 36 // Template for optional OAuth2 authorization HTTP header. | 25 // Template for optional OAuth2 authorization HTTP header. |
| 37 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 26 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
| 38 // Template for GData API version HTTP header. | 27 // Template for GData API version HTTP header. |
| 39 const char kGDataVersionHeader[] = "GData-Version: 3.0"; | 28 const char kGDataVersionHeader[] = "GData-Version: 3.0"; |
| 40 | 29 |
| 41 // Maximum number of attempts for re-authentication per operation. | 30 // Maximum number of attempts for re-authentication per operation. |
| 42 const int kMaxReAuthenticateAttemptsPerOperation = 1; | 31 const int kMaxReAuthenticateAttemptsPerOperation = 1; |
| 43 | 32 |
| 44 // Parse JSON string to base::Value object. | 33 // Parse JSON string to base::Value object. |
| 45 scoped_ptr<base::Value> ParseJsonOnBlockingPool(const std::string& data) { | 34 scoped_ptr<base::Value> ParseJsonOnBlockingPool(const std::string& data) { |
| 46 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 35 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 | 36 |
| 48 int error_code = -1; | 37 int error_code = -1; |
| 49 std::string error_message; | 38 std::string error_message; |
| 50 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 39 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
| 51 data, base::JSON_PARSE_RFC, &error_code, &error_message)); | 40 data, base::JSON_PARSE_RFC, &error_code, &error_message)); |
| 52 | 41 |
| 53 if (!value.get()) { | 42 if (!value.get()) { |
| 54 LOG(ERROR) << "Error while parsing entry response: " << error_message | 43 LOG(ERROR) << "Error while parsing entry response: " << error_message |
| 55 << ", code: " << error_code << ", data:\n" << data; | 44 << ", code: " << error_code << ", data:\n" << data; |
| 56 } | 45 } |
| 57 return value.Pass(); | 46 return value.Pass(); |
| 58 } | 47 } |
| 59 | 48 |
| 60 } // namespace | 49 } // namespace |
| 61 | 50 |
| 62 namespace google_apis { | 51 namespace google_apis { |
| 63 | 52 |
| 64 //================================ AuthOperation =============================== | |
| 65 | |
| 66 AuthOperation::AuthOperation(OperationRegistry* registry, | |
| 67 const AuthStatusCallback& callback, | |
| 68 const std::vector<std::string>& scopes, | |
| 69 const std::string& refresh_token) | |
| 70 : OperationRegistry::Operation(registry), | |
| 71 refresh_token_(refresh_token), | |
| 72 callback_(callback), | |
| 73 scopes_(scopes) { | |
| 74 } | |
| 75 | |
| 76 AuthOperation::~AuthOperation() {} | |
| 77 | |
| 78 void AuthOperation::Start() { | |
| 79 DCHECK(!refresh_token_.empty()); | |
| 80 oauth2_access_token_fetcher_.reset(new OAuth2AccessTokenFetcher( | |
| 81 this, g_browser_process->system_request_context())); | |
| 82 NotifyStart(); | |
| 83 oauth2_access_token_fetcher_->Start( | |
| 84 GaiaUrls::GetInstance()->oauth2_chrome_client_id(), | |
| 85 GaiaUrls::GetInstance()->oauth2_chrome_client_secret(), | |
| 86 refresh_token_, | |
| 87 scopes_); | |
| 88 } | |
| 89 | |
| 90 void AuthOperation::DoCancel() { | |
| 91 oauth2_access_token_fetcher_->CancelRequest(); | |
| 92 if (!callback_.is_null()) | |
| 93 callback_.Run(GDATA_CANCELLED, std::string()); | |
| 94 } | |
| 95 | |
| 96 // Callback for OAuth2AccessTokenFetcher on success. |access_token| is the token | |
| 97 // used to start fetching user data. | |
| 98 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, | |
| 99 const base::Time& expiration_time) { | |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 101 | |
| 102 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | |
| 103 kSuccessRatioHistogramSuccess, | |
| 104 kSuccessRatioHistogramMaxValue); | |
| 105 | |
| 106 callback_.Run(HTTP_SUCCESS, access_token); | |
| 107 NotifyFinish(OPERATION_COMPLETED); | |
| 108 } | |
| 109 | |
| 110 // Callback for OAuth2AccessTokenFetcher on failure. | |
| 111 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { | |
| 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 113 | |
| 114 LOG(WARNING) << "AuthOperation: token request using refresh token failed: " | |
| 115 << error.ToString(); | |
| 116 | |
| 117 // There are many ways to fail, but if the failure is due to connection, | |
| 118 // it's likely that the device is off-line. We treat the error differently | |
| 119 // so that the file manager works while off-line. | |
| 120 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { | |
| 121 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | |
| 122 kSuccessRatioHistogramNoConnection, | |
| 123 kSuccessRatioHistogramMaxValue); | |
| 124 callback_.Run(GDATA_NO_CONNECTION, std::string()); | |
| 125 } else { | |
| 126 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | |
| 127 kSuccessRatioHistogramFailure, | |
| 128 kSuccessRatioHistogramMaxValue); | |
| 129 callback_.Run(HTTP_UNAUTHORIZED, std::string()); | |
| 130 } | |
| 131 NotifyFinish(OPERATION_FAILED); | |
| 132 } | |
| 133 | |
| 134 //============================ UrlFetchOperationBase =========================== | 53 //============================ UrlFetchOperationBase =========================== |
| 135 | 54 |
| 136 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry) | 55 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry) |
| 137 : OperationRegistry::Operation(registry), | 56 : OperationRegistry::Operation(registry), |
| 138 re_authenticate_count_(0), | 57 re_authenticate_count_(0), |
| 139 save_temp_file_(false), | 58 save_temp_file_(false), |
| 140 started_(false), | 59 started_(false), |
| 141 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 60 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 143 } | 62 } |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 } | 332 } |
| 414 | 333 |
| 415 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | 334 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, |
| 416 scoped_ptr<base::Value> value) { | 335 scoped_ptr<base::Value> value) { |
| 417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 418 if (!callback_.is_null()) | 337 if (!callback_.is_null()) |
| 419 callback_.Run(fetch_error_code, value.Pass()); | 338 callback_.Run(fetch_error_code, value.Pass()); |
| 420 } | 339 } |
| 421 | 340 |
| 422 } // namespace google_apis | 341 } // namespace google_apis |
| OLD | NEW |