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 CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ | 5 #ifndef CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ |
6 #define CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ | 6 #define CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 12 matching lines...) Expand all Loading... |
23 class TaskRunner; | 23 class TaskRunner; |
24 } | 24 } |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 class URLRequestContextGetter; | 27 class URLRequestContextGetter; |
28 } | 28 } |
29 | 29 |
30 namespace google_apis { | 30 namespace google_apis { |
31 | 31 |
32 class AuthenticatedRequestInterface; | 32 class AuthenticatedRequestInterface; |
33 class AuthService; | 33 class AuthServiceInterface; |
34 | 34 |
35 // Helper class that sends requests implementing | 35 // Helper class that sends requests implementing |
36 // AuthenticatedRequestInterface and handles retries and authentication. | 36 // AuthenticatedRequestInterface and handles retries and authentication. |
37 class RequestSender { | 37 class RequestSender { |
38 public: | 38 public: |
| 39 // |auth_service| is used for fetching OAuth tokens. It'll be owned by |
| 40 // this RequestSender. |
| 41 // |
39 // |url_request_context_getter| is the context used to perform network | 42 // |url_request_context_getter| is the context used to perform network |
40 // requests from this RequestSender. | 43 // requests from this RequestSender. |
41 // | 44 // |
42 // |blocking_task_runner| is used for running blocking operation, e.g., | 45 // |blocking_task_runner| is used for running blocking operation, e.g., |
43 // parsing JSON response from the server. | 46 // parsing JSON response from the server. |
44 // | 47 // |
45 // |scopes| specifies OAuth2 scopes. | 48 // |scopes| specifies OAuth2 scopes. |
46 // | 49 // |
47 // |custom_user_agent| will be used for the User-Agent header in HTTP | 50 // |custom_user_agent| will be used for the User-Agent header in HTTP |
48 // requests issued through the request sender if the value is not empty. | 51 // requests issued through the request sender if the value is not empty. |
49 RequestSender(Profile* profile, | 52 RequestSender(AuthServiceInterface* auth_service, |
50 net::URLRequestContextGetter* url_request_context_getter, | 53 net::URLRequestContextGetter* url_request_context_getter, |
51 base::TaskRunner* blocking_task_runner, | 54 base::TaskRunner* blocking_task_runner, |
52 const std::vector<std::string>& scopes, | |
53 const std::string& custom_user_agent); | 55 const std::string& custom_user_agent); |
54 virtual ~RequestSender(); | 56 ~RequestSender(); |
55 | 57 |
56 AuthService* auth_service() { return auth_service_.get(); } | 58 AuthServiceInterface* auth_service() { return auth_service_.get(); } |
57 | 59 |
58 net::URLRequestContextGetter* url_request_context_getter() const { | 60 net::URLRequestContextGetter* url_request_context_getter() const { |
59 return url_request_context_getter_; | 61 return url_request_context_getter_; |
60 } | 62 } |
61 | 63 |
62 base::TaskRunner* blocking_task_runner() const { | 64 base::TaskRunner* blocking_task_runner() const { |
63 return blocking_task_runner_.get(); | 65 return blocking_task_runner_.get(); |
64 } | 66 } |
65 | 67 |
66 // Prepares the object for use. | |
67 virtual void Initialize(); | |
68 | |
69 // Starts a request implementing the AuthenticatedRequestInterface | 68 // Starts a request implementing the AuthenticatedRequestInterface |
70 // interface, and makes the request retry upon authentication failures by | 69 // interface, and makes the request retry upon authentication failures by |
71 // calling back to RetryRequest. The |request| object is owned by this | 70 // calling back to RetryRequest. The |request| object is owned by this |
72 // RequestSender. It will be deleted in RequestSender's destructor or | 71 // RequestSender. It will be deleted in RequestSender's destructor or |
73 // in RequestFinished(). | 72 // in RequestFinished(). |
74 // | 73 // |
75 // Returns a closure to cancel the request. The closure cancels the request | 74 // Returns a closure to cancel the request. The closure cancels the request |
76 // if it is in-flight, and does nothing if it is already terminated. | 75 // if it is in-flight, and does nothing if it is already terminated. |
77 base::Closure StartRequestWithRetry(AuthenticatedRequestInterface* request); | 76 base::Closure StartRequestWithRetry(AuthenticatedRequestInterface* request); |
78 | 77 |
(...skipping 10 matching lines...) Expand all Loading... |
89 | 88 |
90 // Clears any authentication token and retries the request, which forces | 89 // Clears any authentication token and retries the request, which forces |
91 // an authentication token refresh. | 90 // an authentication token refresh. |
92 void RetryRequest(AuthenticatedRequestInterface* request); | 91 void RetryRequest(AuthenticatedRequestInterface* request); |
93 | 92 |
94 // Cancels the request. Used for implementing the returned closure of | 93 // Cancels the request. Used for implementing the returned closure of |
95 // StartRequestWithRetry. | 94 // StartRequestWithRetry. |
96 void CancelRequest( | 95 void CancelRequest( |
97 const base::WeakPtr<AuthenticatedRequestInterface>& request); | 96 const base::WeakPtr<AuthenticatedRequestInterface>& request); |
98 | 97 |
99 Profile* profile_; // Not owned. | 98 scoped_ptr<AuthServiceInterface> auth_service_; |
100 net::URLRequestContextGetter* url_request_context_getter_; // Not owned. | 99 net::URLRequestContextGetter* url_request_context_getter_; // Not owned. |
101 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 100 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
102 | 101 |
103 scoped_ptr<AuthService> auth_service_; | |
104 std::set<AuthenticatedRequestInterface*> in_flight_requests_; | 102 std::set<AuthenticatedRequestInterface*> in_flight_requests_; |
105 const std::string custom_user_agent_; | 103 const std::string custom_user_agent_; |
106 | 104 |
107 base::ThreadChecker thread_checker_; | 105 base::ThreadChecker thread_checker_; |
108 | 106 |
109 // Note: This should remain the last member so it'll be destroyed and | 107 // Note: This should remain the last member so it'll be destroyed and |
110 // invalidate its weak pointers before any other members are destroyed. | 108 // invalidate its weak pointers before any other members are destroyed. |
111 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; | 109 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; |
112 | 110 |
113 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 111 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
114 }; | 112 }; |
115 | 113 |
116 } // namespace google_apis | 114 } // namespace google_apis |
117 | 115 |
118 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ | 116 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ |
OLD | NEW |