| 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/chromeos/gdata/operations_base.h" | 5 #include "chrome/browser/chromeos/gdata/operations_base.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 << data; | 76 << data; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 } // namespace | 80 } // namespace |
| 81 | 81 |
| 82 namespace gdata { | 82 namespace gdata { |
| 83 | 83 |
| 84 //================================ AuthOperation =============================== | 84 //================================ AuthOperation =============================== |
| 85 | 85 |
| 86 AuthOperation::AuthOperation(GDataOperationRegistry* registry, | 86 AuthOperation::AuthOperation(OperationRegistry* registry, |
| 87 const AuthStatusCallback& callback, | 87 const AuthStatusCallback& callback, |
| 88 const std::string& refresh_token) | 88 const std::string& refresh_token) |
| 89 : GDataOperationRegistry::Operation(registry), | 89 : OperationRegistry::Operation(registry), |
| 90 refresh_token_(refresh_token), callback_(callback) { | 90 refresh_token_(refresh_token), callback_(callback) { |
| 91 } | 91 } |
| 92 | 92 |
| 93 AuthOperation::~AuthOperation() {} | 93 AuthOperation::~AuthOperation() {} |
| 94 | 94 |
| 95 void AuthOperation::Start() { | 95 void AuthOperation::Start() { |
| 96 DCHECK(!refresh_token_.empty()); | 96 DCHECK(!refresh_token_.empty()); |
| 97 std::vector<std::string> scopes; | 97 std::vector<std::string> scopes; |
| 98 if (gdata::util::IsDriveV2ApiEnabled()) { | 98 if (gdata::util::IsDriveV2ApiEnabled()) { |
| 99 scopes.push_back(kDriveScope); | 99 scopes.push_back(kDriveScope); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 126 // used to start fetching user data. | 126 // used to start fetching user data. |
| 127 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, | 127 void AuthOperation::OnGetTokenSuccess(const std::string& access_token, |
| 128 const base::Time& expiration_time) { | 128 const base::Time& expiration_time) { |
| 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 130 | 130 |
| 131 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 131 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 132 kSuccessRatioHistogramSuccess, | 132 kSuccessRatioHistogramSuccess, |
| 133 kSuccessRatioHistogramMaxValue); | 133 kSuccessRatioHistogramMaxValue); |
| 134 | 134 |
| 135 callback_.Run(HTTP_SUCCESS, access_token); | 135 callback_.Run(HTTP_SUCCESS, access_token); |
| 136 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 136 NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
| 137 } | 137 } |
| 138 | 138 |
| 139 // Callback for OAuth2AccessTokenFetcher on failure. | 139 // Callback for OAuth2AccessTokenFetcher on failure. |
| 140 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { | 140 void AuthOperation::OnGetTokenFailure(const GoogleServiceAuthError& error) { |
| 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 142 | 142 |
| 143 LOG(WARNING) << "AuthOperation: token request using refresh token failed" | 143 LOG(WARNING) << "AuthOperation: token request using refresh token failed" |
| 144 << error.ToString(); | 144 << error.ToString(); |
| 145 | 145 |
| 146 // There are many ways to fail, but if the failure is due to connection, | 146 // There are many ways to fail, but if the failure is due to connection, |
| 147 // it's likely that the device is off-line. We treat the error differently | 147 // it's likely that the device is off-line. We treat the error differently |
| 148 // so that the file manager works while off-line. | 148 // so that the file manager works while off-line. |
| 149 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { | 149 if (error.state() == GoogleServiceAuthError::CONNECTION_FAILED) { |
| 150 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 150 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 151 kSuccessRatioHistogramNoConnection, | 151 kSuccessRatioHistogramNoConnection, |
| 152 kSuccessRatioHistogramMaxValue); | 152 kSuccessRatioHistogramMaxValue); |
| 153 callback_.Run(GDATA_NO_CONNECTION, std::string()); | 153 callback_.Run(GDATA_NO_CONNECTION, std::string()); |
| 154 } else { | 154 } else { |
| 155 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", | 155 UMA_HISTOGRAM_ENUMERATION("GData.AuthSuccess", |
| 156 kSuccessRatioHistogramFailure, | 156 kSuccessRatioHistogramFailure, |
| 157 kSuccessRatioHistogramMaxValue); | 157 kSuccessRatioHistogramMaxValue); |
| 158 callback_.Run(HTTP_UNAUTHORIZED, std::string()); | 158 callback_.Run(HTTP_UNAUTHORIZED, std::string()); |
| 159 } | 159 } |
| 160 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED); | 160 NotifyFinish(OperationRegistry::OPERATION_FAILED); |
| 161 } | 161 } |
| 162 | 162 |
| 163 //============================ UrlFetchOperationBase =========================== | 163 //============================ UrlFetchOperationBase =========================== |
| 164 | 164 |
| 165 UrlFetchOperationBase::UrlFetchOperationBase(GDataOperationRegistry* registry) | 165 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry) |
| 166 : GDataOperationRegistry::Operation(registry), | 166 : OperationRegistry::Operation(registry), |
| 167 re_authenticate_count_(0), | 167 re_authenticate_count_(0), |
| 168 save_temp_file_(false), | 168 save_temp_file_(false), |
| 169 started_(false) { | 169 started_(false) { |
| 170 } | 170 } |
| 171 | 171 |
| 172 UrlFetchOperationBase::UrlFetchOperationBase( | 172 UrlFetchOperationBase::UrlFetchOperationBase( |
| 173 GDataOperationRegistry* registry, | 173 OperationRegistry* registry, |
| 174 GDataOperationRegistry::OperationType type, | 174 OperationRegistry::OperationType type, |
| 175 const FilePath& path) | 175 const FilePath& path) |
| 176 : GDataOperationRegistry::Operation(registry, type, path), | 176 : OperationRegistry::Operation(registry, type, path), |
| 177 re_authenticate_count_(0), | 177 re_authenticate_count_(0), |
| 178 save_temp_file_(false) { | 178 save_temp_file_(false) { |
| 179 } | 179 } |
| 180 | 180 |
| 181 UrlFetchOperationBase::~UrlFetchOperationBase() {} | 181 UrlFetchOperationBase::~UrlFetchOperationBase() {} |
| 182 | 182 |
| 183 void UrlFetchOperationBase::Start(const std::string& auth_token) { | 183 void UrlFetchOperationBase::Start(const std::string& auth_token) { |
| 184 DCHECK(!auth_token.empty()); | 184 DCHECK(!auth_token.empty()); |
| 185 | 185 |
| 186 GURL url = GetURL(); | 186 GURL url = GetURL(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // likely that the failure is due to loss of connection. | 262 // likely that the failure is due to loss of connection. |
| 263 code = GDATA_NO_CONNECTION; | 263 code = GDATA_NO_CONNECTION; |
| 264 } | 264 } |
| 265 return code; | 265 return code; |
| 266 } | 266 } |
| 267 | 267 |
| 268 void UrlFetchOperationBase::OnProcessURLFetchResultsComplete(bool result) { | 268 void UrlFetchOperationBase::OnProcessURLFetchResultsComplete(bool result) { |
| 269 if (result) | 269 if (result) |
| 270 NotifySuccessToOperationRegistry(); | 270 NotifySuccessToOperationRegistry(); |
| 271 else | 271 else |
| 272 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED); | 272 NotifyFinish(OperationRegistry::OPERATION_FAILED); |
| 273 } | 273 } |
| 274 | 274 |
| 275 void UrlFetchOperationBase::OnURLFetchComplete(const URLFetcher* source) { | 275 void UrlFetchOperationBase::OnURLFetchComplete(const URLFetcher* source) { |
| 276 GDataErrorCode code = GetErrorCode(source); | 276 GDataErrorCode code = GetErrorCode(source); |
| 277 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); | 277 DVLOG(1) << "Response headers:\n" << GetResponseHeadersAsString(source); |
| 278 | 278 |
| 279 if (code == HTTP_UNAUTHORIZED) { | 279 if (code == HTTP_UNAUTHORIZED) { |
| 280 if (!re_authenticate_callback_.is_null() && | 280 if (!re_authenticate_callback_.is_null() && |
| 281 ++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) { | 281 ++re_authenticate_count_ <= kMaxReAuthenticateAttemptsPerOperation) { |
| 282 re_authenticate_callback_.Run(this); | 282 re_authenticate_callback_.Run(this); |
| 283 return; | 283 return; |
| 284 } | 284 } |
| 285 | 285 |
| 286 OnAuthFailed(code); | 286 OnAuthFailed(code); |
| 287 return; | 287 return; |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Overridden by each specialization | 290 // Overridden by each specialization |
| 291 ProcessURLFetchResults(source); | 291 ProcessURLFetchResults(source); |
| 292 } | 292 } |
| 293 | 293 |
| 294 void UrlFetchOperationBase::NotifySuccessToOperationRegistry() { | 294 void UrlFetchOperationBase::NotifySuccessToOperationRegistry() { |
| 295 NotifyFinish(GDataOperationRegistry::OPERATION_COMPLETED); | 295 NotifyFinish(OperationRegistry::OPERATION_COMPLETED); |
| 296 } | 296 } |
| 297 | 297 |
| 298 void UrlFetchOperationBase::NotifyStartToOperationRegistry() { | 298 void UrlFetchOperationBase::NotifyStartToOperationRegistry() { |
| 299 NotifyStart(); | 299 NotifyStart(); |
| 300 } | 300 } |
| 301 | 301 |
| 302 void UrlFetchOperationBase::OnAuthFailed(GDataErrorCode code) { | 302 void UrlFetchOperationBase::OnAuthFailed(GDataErrorCode code) { |
| 303 RunCallbackOnPrematureFailure(code); | 303 RunCallbackOnPrematureFailure(code); |
| 304 | 304 |
| 305 // Notify authentication failed. | 305 // Notify authentication failed. |
| 306 NotifyAuthFailed(); | 306 NotifyAuthFailed(); |
| 307 | 307 |
| 308 // Check if this failed before we even started fetching. If so, register | 308 // Check if this failed before we even started fetching. If so, register |
| 309 // for start so we can properly unregister with finish. | 309 // for start so we can properly unregister with finish. |
| 310 if (!started_) | 310 if (!started_) |
| 311 NotifyStart(); | 311 NotifyStart(); |
| 312 | 312 |
| 313 // Note: NotifyFinish() must be invoked at the end, after all other callbacks | 313 // Note: NotifyFinish() must be invoked at the end, after all other callbacks |
| 314 // and notifications. Once NotifyFinish() is called, the current instance of | 314 // and notifications. Once NotifyFinish() is called, the current instance of |
| 315 // gdata operation will be deleted from the GDataOperationRegistry and become | 315 // gdata operation will be deleted from the OperationRegistry and become |
| 316 // invalid. | 316 // invalid. |
| 317 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED); | 317 NotifyFinish(OperationRegistry::OPERATION_FAILED); |
| 318 } | 318 } |
| 319 | 319 |
| 320 std::string UrlFetchOperationBase::GetResponseHeadersAsString( | 320 std::string UrlFetchOperationBase::GetResponseHeadersAsString( |
| 321 const URLFetcher* url_fetcher) { | 321 const URLFetcher* url_fetcher) { |
| 322 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores | 322 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores |
| 323 // all headers in their raw format, i.e each header is null-terminated. | 323 // all headers in their raw format, i.e each header is null-terminated. |
| 324 // So logging raw_headers() only shows the first header, which is probably | 324 // So logging raw_headers() only shows the first header, which is probably |
| 325 // the status line. GetNormalizedHeaders, on the other hand, will show all | 325 // the status line. GetNormalizedHeaders, on the other hand, will show all |
| 326 // the headers, one per line, which is probably what we want. | 326 // the headers, one per line, which is probably what we want. |
| 327 std::string headers; | 327 std::string headers; |
| 328 // Check that response code indicates response headers are valid (i.e. not | 328 // Check that response code indicates response headers are valid (i.e. not |
| 329 // malformed) before we retrieve the headers. | 329 // malformed) before we retrieve the headers. |
| 330 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { | 330 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { |
| 331 headers.assign("Response headers are malformed!!"); | 331 headers.assign("Response headers are malformed!!"); |
| 332 } else { | 332 } else { |
| 333 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); | 333 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); |
| 334 } | 334 } |
| 335 return headers; | 335 return headers; |
| 336 } | 336 } |
| 337 | 337 |
| 338 //============================ EntryActionOperation ============================ | 338 //============================ EntryActionOperation ============================ |
| 339 | 339 |
| 340 EntryActionOperation::EntryActionOperation(GDataOperationRegistry* registry, | 340 EntryActionOperation::EntryActionOperation(OperationRegistry* registry, |
| 341 const EntryActionCallback& callback, | 341 const EntryActionCallback& callback, |
| 342 const GURL& document_url) | 342 const GURL& document_url) |
| 343 : UrlFetchOperationBase(registry), | 343 : UrlFetchOperationBase(registry), |
| 344 callback_(callback), | 344 callback_(callback), |
| 345 document_url_(document_url) { | 345 document_url_(document_url) { |
| 346 } | 346 } |
| 347 | 347 |
| 348 EntryActionOperation::~EntryActionOperation() {} | 348 EntryActionOperation::~EntryActionOperation() {} |
| 349 | 349 |
| 350 void EntryActionOperation::ProcessURLFetchResults(const URLFetcher* source) { | 350 void EntryActionOperation::ProcessURLFetchResults(const URLFetcher* source) { |
| 351 if (!callback_.is_null()) { | 351 if (!callback_.is_null()) { |
| 352 GDataErrorCode code = GetErrorCode(source); | 352 GDataErrorCode code = GetErrorCode(source); |
| 353 callback_.Run(code, document_url_); | 353 callback_.Run(code, document_url_); |
| 354 } | 354 } |
| 355 const bool success = true; | 355 const bool success = true; |
| 356 OnProcessURLFetchResultsComplete(success); | 356 OnProcessURLFetchResultsComplete(success); |
| 357 } | 357 } |
| 358 | 358 |
| 359 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { | 359 void EntryActionOperation::RunCallbackOnPrematureFailure(GDataErrorCode code) { |
| 360 if (!callback_.is_null()) | 360 if (!callback_.is_null()) |
| 361 callback_.Run(code, document_url_); | 361 callback_.Run(code, document_url_); |
| 362 } | 362 } |
| 363 | 363 |
| 364 //============================== GetDataOperation ============================== | 364 //============================== GetDataOperation ============================== |
| 365 | 365 |
| 366 GetDataOperation::GetDataOperation(GDataOperationRegistry* registry, | 366 GetDataOperation::GetDataOperation(OperationRegistry* registry, |
| 367 const GetDataCallback& callback) | 367 const GetDataCallback& callback) |
| 368 : UrlFetchOperationBase(registry), | 368 : UrlFetchOperationBase(registry), |
| 369 callback_(callback), | 369 callback_(callback), |
| 370 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 370 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 371 } | 371 } |
| 372 | 372 |
| 373 GetDataOperation::~GetDataOperation() {} | 373 GetDataOperation::~GetDataOperation() {} |
| 374 | 374 |
| 375 void GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { | 375 void GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { |
| 376 std::string data; | 376 std::string data; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 } | 441 } |
| 442 | 442 |
| 443 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | 443 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, |
| 444 scoped_ptr<base::Value> value) { | 444 scoped_ptr<base::Value> value) { |
| 445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 446 if (!callback_.is_null()) | 446 if (!callback_.is_null()) |
| 447 callback_.Run(fetch_error_code, value.Pass()); | 447 callback_.Run(fetch_error_code, value.Pass()); |
| 448 } | 448 } |
| 449 | 449 |
| 450 } // namespace gdata | 450 } // namespace gdata |
| OLD | NEW |