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