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 #ifndef GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ | 5 #ifndef GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ |
6 #define GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ | 6 #define GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "google_apis/drive/drive_api_error_codes.h" | 17 #include "google_apis/drive/drive_api_error_codes.h" |
| 18 #include "net/traffic_annotation/network_traffic_annotation.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 class SequencedTaskRunner; | 21 class SequencedTaskRunner; |
21 } | 22 } |
22 | 23 |
23 namespace net { | 24 namespace net { |
24 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
25 } | 26 } |
26 | 27 |
27 namespace google_apis { | 28 namespace google_apis { |
(...skipping 13 matching lines...) Expand all Loading... |
41 // | 42 // |
42 // |blocking_task_runner| is used for running blocking operation, e.g., | 43 // |blocking_task_runner| is used for running blocking operation, e.g., |
43 // parsing JSON response from the server. | 44 // parsing JSON response from the server. |
44 // | 45 // |
45 // |custom_user_agent| will be used for the User-Agent header in HTTP | 46 // |custom_user_agent| will be used for the User-Agent header in HTTP |
46 // requests issued through the request sender if the value is not empty. | 47 // requests issued through the request sender if the value is not empty. |
47 RequestSender( | 48 RequestSender( |
48 AuthServiceInterface* auth_service, | 49 AuthServiceInterface* auth_service, |
49 net::URLRequestContextGetter* url_request_context_getter, | 50 net::URLRequestContextGetter* url_request_context_getter, |
50 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, | 51 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner, |
51 const std::string& custom_user_agent); | 52 const std::string& custom_user_agent, |
| 53 const net::NetworkTrafficAnnotationTag& traffic_annotation); |
52 ~RequestSender(); | 54 ~RequestSender(); |
53 | 55 |
54 AuthServiceInterface* auth_service() { return auth_service_.get(); } | 56 AuthServiceInterface* auth_service() { return auth_service_.get(); } |
55 | 57 |
56 net::URLRequestContextGetter* url_request_context_getter() const { | 58 net::URLRequestContextGetter* url_request_context_getter() const { |
57 return url_request_context_getter_.get(); | 59 return url_request_context_getter_.get(); |
58 } | 60 } |
59 | 61 |
60 base::SequencedTaskRunner* blocking_task_runner() const { | 62 base::SequencedTaskRunner* blocking_task_runner() const { |
61 return blocking_task_runner_.get(); | 63 return blocking_task_runner_.get(); |
62 } | 64 } |
63 | 65 |
64 // Starts a request implementing the AuthenticatedRequestInterface | 66 // Starts a request implementing the AuthenticatedRequestInterface |
65 // interface, and makes the request retry upon authentication failures by | 67 // interface, and makes the request retry upon authentication failures by |
66 // calling back to RetryRequest. | 68 // calling back to RetryRequest. |
67 // | 69 // |
68 // Returns a closure to cancel the request. The closure cancels the request | 70 // Returns a closure to cancel the request. The closure cancels the request |
69 // if it is in-flight, and does nothing if it is already terminated. | 71 // if it is in-flight, and does nothing if it is already terminated. |
70 base::Closure StartRequestWithAuthRetry( | 72 base::Closure StartRequestWithAuthRetry( |
71 std::unique_ptr<AuthenticatedRequestInterface> request); | 73 std::unique_ptr<AuthenticatedRequestInterface> request); |
72 | 74 |
73 // Notifies to this RequestSender that |request| has finished. | 75 // Notifies to this RequestSender that |request| has finished. |
74 // TODO(kinaba): refactor the life time management and make this at private. | 76 // TODO(kinaba): refactor the life time management and make this at private. |
75 void RequestFinished(AuthenticatedRequestInterface* request); | 77 void RequestFinished(AuthenticatedRequestInterface* request); |
76 | 78 |
| 79 // Returns traffic annotation tag asssigned to this object. |
| 80 const net::NetworkTrafficAnnotationTag& get_traffic_annotation_tag() const { |
| 81 return traffic_annotation_; |
| 82 } |
| 83 |
77 private: | 84 private: |
78 base::Closure StartRequestWithAuthRetryInternal( | 85 base::Closure StartRequestWithAuthRetryInternal( |
79 AuthenticatedRequestInterface* request); | 86 AuthenticatedRequestInterface* request); |
80 | 87 |
81 // Called when the access token is fetched. | 88 // Called when the access token is fetched. |
82 void OnAccessTokenFetched( | 89 void OnAccessTokenFetched( |
83 const base::WeakPtr<AuthenticatedRequestInterface>& request, | 90 const base::WeakPtr<AuthenticatedRequestInterface>& request, |
84 DriveApiErrorCode error, | 91 DriveApiErrorCode error, |
85 const std::string& access_token); | 92 const std::string& access_token); |
86 | 93 |
87 // Clears any authentication token and retries the request, which forces | 94 // Clears any authentication token and retries the request, which forces |
88 // an authentication token refresh. | 95 // an authentication token refresh. |
89 void RetryRequest(AuthenticatedRequestInterface* request); | 96 void RetryRequest(AuthenticatedRequestInterface* request); |
90 | 97 |
91 // Cancels the request. Used for implementing the returned closure of | 98 // Cancels the request. Used for implementing the returned closure of |
92 // StartRequestWithAuthRetry. | 99 // StartRequestWithAuthRetry. |
93 void CancelRequest( | 100 void CancelRequest( |
94 const base::WeakPtr<AuthenticatedRequestInterface>& request); | 101 const base::WeakPtr<AuthenticatedRequestInterface>& request); |
95 | 102 |
96 std::unique_ptr<AuthServiceInterface> auth_service_; | 103 std::unique_ptr<AuthServiceInterface> auth_service_; |
97 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 104 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
98 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 105 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
99 | 106 |
100 std::set<std::unique_ptr<AuthenticatedRequestInterface>> in_flight_requests_; | 107 std::set<std::unique_ptr<AuthenticatedRequestInterface>> in_flight_requests_; |
101 const std::string custom_user_agent_; | 108 const std::string custom_user_agent_; |
102 | 109 |
103 base::ThreadChecker thread_checker_; | 110 base::ThreadChecker thread_checker_; |
104 | 111 |
| 112 const net::NetworkTrafficAnnotationTag traffic_annotation_; |
| 113 |
105 // Note: This should remain the last member so it'll be destroyed and | 114 // Note: This should remain the last member so it'll be destroyed and |
106 // invalidate its weak pointers before any other members are destroyed. | 115 // invalidate its weak pointers before any other members are destroyed. |
107 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; | 116 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; |
108 | 117 |
109 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 118 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
110 }; | 119 }; |
111 | 120 |
112 } // namespace google_apis | 121 } // namespace google_apis |
113 | 122 |
114 #endif // GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ | 123 #endif // GOOGLE_APIS_DRIVE_REQUEST_SENDER_H_ |
OLD | NEW |