| 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/operation_runner.h" | 5 #include "chrome/browser/google_apis/request_sender.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/google_apis/auth_service.h" | 8 #include "chrome/browser/google_apis/auth_service.h" |
| 9 #include "chrome/browser/google_apis/base_requests.h" | 9 #include "chrome/browser/google_apis/base_requests.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 | 11 |
| 12 using content::BrowserThread; | 12 using content::BrowserThread; |
| 13 | 13 |
| 14 namespace google_apis { | 14 namespace google_apis { |
| 15 | 15 |
| 16 OperationRunner::OperationRunner( | 16 RequestSender::RequestSender( |
| 17 Profile* profile, | 17 Profile* profile, |
| 18 net::URLRequestContextGetter* url_request_context_getter, | 18 net::URLRequestContextGetter* url_request_context_getter, |
| 19 const std::vector<std::string>& scopes, | 19 const std::vector<std::string>& scopes, |
| 20 const std::string& custom_user_agent) | 20 const std::string& custom_user_agent) |
| 21 : profile_(profile), | 21 : profile_(profile), |
| 22 auth_service_(new AuthService(url_request_context_getter, scopes)), | 22 auth_service_(new AuthService(url_request_context_getter, scopes)), |
| 23 operation_registry_(new OperationRegistry()), | 23 operation_registry_(new OperationRegistry()), |
| 24 custom_user_agent_(custom_user_agent), | 24 custom_user_agent_(custom_user_agent), |
| 25 weak_ptr_factory_(this) { | 25 weak_ptr_factory_(this) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 OperationRunner::~OperationRunner() { | 29 RequestSender::~RequestSender() { |
| 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void OperationRunner::Initialize() { | 33 void RequestSender::Initialize() { |
| 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 35 auth_service_->Initialize(profile_); | 35 auth_service_->Initialize(profile_); |
| 36 } | 36 } |
| 37 | 37 |
| 38 void OperationRunner::CancelAll() { | 38 void RequestSender::CancelAll() { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 operation_registry_->CancelAll(); | 40 operation_registry_->CancelAll(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void OperationRunner::StartOperationWithRetry( | 43 void RequestSender::StartRequestWithRetry( |
| 44 AuthenticatedRequestInterface* operation) { | 44 AuthenticatedRequestInterface* request) { |
| 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 46 | 46 |
| 47 if (!auth_service_->HasAccessToken()) { | 47 if (!auth_service_->HasAccessToken()) { |
| 48 // Fetch OAuth2 access token from the refresh token first. | 48 // Fetch OAuth2 access token from the refresh token first. |
| 49 auth_service_->StartAuthentication( | 49 auth_service_->StartAuthentication( |
| 50 base::Bind(&OperationRunner::OnAccessTokenFetched, | 50 base::Bind(&RequestSender::OnAccessTokenFetched, |
| 51 weak_ptr_factory_.GetWeakPtr(), | 51 weak_ptr_factory_.GetWeakPtr(), |
| 52 operation->GetWeakPtr())); | 52 request->GetWeakPtr())); |
| 53 return; | 53 return; |
| 54 } | 54 } |
| 55 | 55 |
| 56 operation->Start(auth_service_->access_token(), | 56 request->Start(auth_service_->access_token(), |
| 57 custom_user_agent_, | 57 custom_user_agent_, |
| 58 base::Bind(&OperationRunner::RetryOperation, | 58 base::Bind(&RequestSender::RetryRequest, |
| 59 weak_ptr_factory_.GetWeakPtr())); | 59 weak_ptr_factory_.GetWeakPtr())); |
| 60 } | 60 } |
| 61 | 61 |
| 62 void OperationRunner::OnAccessTokenFetched( | 62 void RequestSender::OnAccessTokenFetched( |
| 63 const base::WeakPtr<AuthenticatedRequestInterface>& operation, | 63 const base::WeakPtr<AuthenticatedRequestInterface>& request, |
| 64 GDataErrorCode code, | 64 GDataErrorCode code, |
| 65 const std::string& /* access_token */) { | 65 const std::string& /* access_token */) { |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 67 | 67 |
| 68 // Do nothing if the operation is canceled during authentication. | 68 // Do nothing if the request is canceled during authentication. |
| 69 if (!operation.get()) | 69 if (!request.get()) |
| 70 return; | 70 return; |
| 71 | 71 |
| 72 if (code == HTTP_SUCCESS) { | 72 if (code == HTTP_SUCCESS) { |
| 73 DCHECK(auth_service_->HasAccessToken()); | 73 DCHECK(auth_service_->HasAccessToken()); |
| 74 StartOperationWithRetry(operation.get()); | 74 StartRequestWithRetry(request.get()); |
| 75 } else { | 75 } else { |
| 76 operation->OnAuthFailed(code); | 76 request->OnAuthFailed(code); |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 void OperationRunner::RetryOperation( | 80 void RequestSender::RetryRequest( |
| 81 AuthenticatedRequestInterface* operation) { | 81 AuthenticatedRequestInterface* request) { |
| 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 | 83 |
| 84 auth_service_->ClearAccessToken(); | 84 auth_service_->ClearAccessToken(); |
| 85 // User authentication might have expired - rerun the request to force | 85 // User authentication might have expired - rerun the request to force |
| 86 // auth token refresh. | 86 // auth token refresh. |
| 87 StartOperationWithRetry(operation); | 87 StartRequestWithRetry(request); |
| 88 } | 88 } |
| 89 | 89 |
| 90 } // namespace google_apis | 90 } // namespace google_apis |
| OLD | NEW |