| 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 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.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 "chrome/browser/google_apis/gdata_errorcode.h" | 17 #include "chrome/browser/google_apis/gdata_errorcode.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class TaskRunner; | 20 class SequencedTaskRunner; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace google_apis { | 27 namespace google_apis { |
| 28 | 28 |
| 29 class AuthenticatedRequestInterface; | 29 class AuthenticatedRequestInterface; |
| 30 class AuthServiceInterface; | 30 class AuthServiceInterface; |
| 31 | 31 |
| 32 // Helper class that sends requests implementing | 32 // Helper class that sends requests implementing |
| 33 // AuthenticatedRequestInterface and handles retries and authentication. | 33 // AuthenticatedRequestInterface and handles retries and authentication. |
| 34 class RequestSender { | 34 class RequestSender { |
| 35 public: | 35 public: |
| 36 // |auth_service| is used for fetching OAuth tokens. It'll be owned by | 36 // |auth_service| is used for fetching OAuth tokens. It'll be owned by |
| 37 // this RequestSender. | 37 // this RequestSender. |
| 38 // | 38 // |
| 39 // |url_request_context_getter| is the context used to perform network | 39 // |url_request_context_getter| is the context used to perform network |
| 40 // requests from this RequestSender. | 40 // requests from this RequestSender. |
| 41 // | 41 // |
| 42 // |blocking_task_runner| is used for running blocking operation, e.g., | 42 // |blocking_task_runner| is used for running blocking operation, e.g., |
| 43 // parsing JSON response from the server. | 43 // parsing JSON response from the server. |
| 44 // | 44 // |
| 45 // |custom_user_agent| will be used for the User-Agent header in HTTP | 45 // |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. | 46 // requests issued through the request sender if the value is not empty. |
| 47 RequestSender(AuthServiceInterface* auth_service, | 47 RequestSender(AuthServiceInterface* auth_service, |
| 48 net::URLRequestContextGetter* url_request_context_getter, | 48 net::URLRequestContextGetter* url_request_context_getter, |
| 49 base::TaskRunner* blocking_task_runner, | 49 base::SequencedTaskRunner* blocking_task_runner, |
| 50 const std::string& custom_user_agent); | 50 const std::string& custom_user_agent); |
| 51 ~RequestSender(); | 51 ~RequestSender(); |
| 52 | 52 |
| 53 AuthServiceInterface* auth_service() { return auth_service_.get(); } | 53 AuthServiceInterface* auth_service() { return auth_service_.get(); } |
| 54 | 54 |
| 55 net::URLRequestContextGetter* url_request_context_getter() const { | 55 net::URLRequestContextGetter* url_request_context_getter() const { |
| 56 return url_request_context_getter_; | 56 return url_request_context_getter_; |
| 57 } | 57 } |
| 58 | 58 |
| 59 base::TaskRunner* blocking_task_runner() const { | 59 base::SequencedTaskRunner* blocking_task_runner() const { |
| 60 return blocking_task_runner_.get(); | 60 return blocking_task_runner_.get(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Starts a request implementing the AuthenticatedRequestInterface | 63 // Starts a request implementing the AuthenticatedRequestInterface |
| 64 // interface, and makes the request retry upon authentication failures by | 64 // interface, and makes the request retry upon authentication failures by |
| 65 // calling back to RetryRequest. The |request| object is owned by this | 65 // calling back to RetryRequest. The |request| object is owned by this |
| 66 // RequestSender. It will be deleted in RequestSender's destructor or | 66 // RequestSender. It will be deleted in RequestSender's destructor or |
| 67 // in RequestFinished(). | 67 // in RequestFinished(). |
| 68 // | 68 // |
| 69 // Returns a closure to cancel the request. The closure cancels the request | 69 // Returns a closure to cancel the request. The closure cancels the request |
| (...skipping 15 matching lines...) Expand all Loading... |
| 85 // an authentication token refresh. | 85 // an authentication token refresh. |
| 86 void RetryRequest(AuthenticatedRequestInterface* request); | 86 void RetryRequest(AuthenticatedRequestInterface* request); |
| 87 | 87 |
| 88 // Cancels the request. Used for implementing the returned closure of | 88 // Cancels the request. Used for implementing the returned closure of |
| 89 // StartRequestWithRetry. | 89 // StartRequestWithRetry. |
| 90 void CancelRequest( | 90 void CancelRequest( |
| 91 const base::WeakPtr<AuthenticatedRequestInterface>& request); | 91 const base::WeakPtr<AuthenticatedRequestInterface>& request); |
| 92 | 92 |
| 93 scoped_ptr<AuthServiceInterface> auth_service_; | 93 scoped_ptr<AuthServiceInterface> auth_service_; |
| 94 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 94 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 95 scoped_refptr<base::TaskRunner> blocking_task_runner_; | 95 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 96 | 96 |
| 97 std::set<AuthenticatedRequestInterface*> in_flight_requests_; | 97 std::set<AuthenticatedRequestInterface*> in_flight_requests_; |
| 98 const std::string custom_user_agent_; | 98 const std::string custom_user_agent_; |
| 99 | 99 |
| 100 base::ThreadChecker thread_checker_; | 100 base::ThreadChecker thread_checker_; |
| 101 | 101 |
| 102 // Note: This should remain the last member so it'll be destroyed and | 102 // Note: This should remain the last member so it'll be destroyed and |
| 103 // invalidate its weak pointers before any other members are destroyed. | 103 // invalidate its weak pointers before any other members are destroyed. |
| 104 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; | 104 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; |
| 105 | 105 |
| 106 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 106 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 } // namespace google_apis | 109 } // namespace google_apis |
| 110 | 110 |
| 111 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ | 111 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ |
| OLD | NEW |