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/request_sender.h" | 5 #include "chrome/browser/google_apis/request_sender.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" |
8 #include "chrome/browser/google_apis/auth_service.h" | 9 #include "chrome/browser/google_apis/auth_service.h" |
9 #include "chrome/browser/google_apis/base_requests.h" | 10 #include "chrome/browser/google_apis/base_requests.h" |
10 | 11 |
11 namespace google_apis { | 12 namespace google_apis { |
12 | 13 |
13 RequestSender::RequestSender( | 14 RequestSender::RequestSender( |
14 Profile* profile, | 15 Profile* profile, |
15 net::URLRequestContextGetter* url_request_context_getter, | 16 net::URLRequestContextGetter* url_request_context_getter, |
16 const std::vector<std::string>& scopes, | 17 const std::vector<std::string>& scopes, |
17 const std::string& custom_user_agent) | 18 const std::string& custom_user_agent) |
18 : profile_(profile), | 19 : profile_(profile), |
19 auth_service_(new AuthService(url_request_context_getter, scopes)), | 20 auth_service_(new AuthService(url_request_context_getter, scopes)), |
20 request_registry_(new RequestRegistry()), | |
21 custom_user_agent_(custom_user_agent), | 21 custom_user_agent_(custom_user_agent), |
22 weak_ptr_factory_(this) { | 22 weak_ptr_factory_(this) { |
23 DCHECK(thread_checker_.CalledOnValidThread()); | 23 DCHECK(thread_checker_.CalledOnValidThread()); |
24 } | 24 } |
25 | 25 |
26 RequestSender::~RequestSender() { | 26 RequestSender::~RequestSender() { |
27 DCHECK(thread_checker_.CalledOnValidThread()); | 27 DCHECK(thread_checker_.CalledOnValidThread()); |
| 28 STLDeleteContainerPointers(in_flight_requests_.begin(), |
| 29 in_flight_requests_.end()); |
28 } | 30 } |
29 | 31 |
30 void RequestSender::Initialize() { | 32 void RequestSender::Initialize() { |
31 DCHECK(thread_checker_.CalledOnValidThread()); | 33 DCHECK(thread_checker_.CalledOnValidThread()); |
32 auth_service_->Initialize(profile_); | 34 auth_service_->Initialize(profile_); |
33 } | 35 } |
34 | 36 |
35 base::Closure RequestSender::StartRequestWithRetry( | 37 base::Closure RequestSender::StartRequestWithRetry( |
36 AuthenticatedRequestInterface* request) { | 38 AuthenticatedRequestInterface* request) { |
37 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
38 | 40 |
| 41 in_flight_requests_.insert(request); |
| 42 |
39 // TODO(kinaba): Stop relying on weak pointers. Move lifetime management | 43 // TODO(kinaba): Stop relying on weak pointers. Move lifetime management |
40 // of the requests to request sender. | 44 // of the requests to request sender. |
41 base::Closure cancel_closure = | 45 base::Closure cancel_closure = |
42 base::Bind(&RequestSender::CancelRequest, | 46 base::Bind(&RequestSender::CancelRequest, |
43 weak_ptr_factory_.GetWeakPtr(), | 47 weak_ptr_factory_.GetWeakPtr(), |
44 request->GetWeakPtr()); | 48 request->GetWeakPtr()); |
45 | 49 |
46 if (!auth_service_->HasAccessToken()) { | 50 if (!auth_service_->HasAccessToken()) { |
47 // Fetch OAuth2 access token from the refresh token first. | 51 // Fetch OAuth2 access token from the refresh token first. |
48 auth_service_->StartAuthentication( | 52 auth_service_->StartAuthentication( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 void RequestSender::CancelRequest( | 93 void RequestSender::CancelRequest( |
90 const base::WeakPtr<AuthenticatedRequestInterface>& request) { | 94 const base::WeakPtr<AuthenticatedRequestInterface>& request) { |
91 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
92 | 96 |
93 // Do nothing if the request is already finished. | 97 // Do nothing if the request is already finished. |
94 if (!request.get()) | 98 if (!request.get()) |
95 return; | 99 return; |
96 request->Cancel(); | 100 request->Cancel(); |
97 } | 101 } |
98 | 102 |
| 103 void RequestSender::RequestFinished(AuthenticatedRequestInterface* request) { |
| 104 in_flight_requests_.erase(request); |
| 105 delete request; |
| 106 } |
| 107 |
99 } // namespace google_apis | 108 } // namespace google_apis |
OLD | NEW |