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 <string> | 9 #include <string> |
9 #include <vector> | 10 #include <vector> |
10 | 11 |
11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
12 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
15 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
16 #include "chrome/browser/google_apis/gdata_errorcode.h" | 17 #include "chrome/browser/google_apis/gdata_errorcode.h" |
17 | 18 |
18 class Profile; | 19 class Profile; |
19 | 20 |
20 namespace net { | 21 namespace net { |
21 class URLRequestContextGetter; | 22 class URLRequestContextGetter; |
22 } | 23 } |
23 | 24 |
24 namespace google_apis { | 25 namespace google_apis { |
25 | 26 |
26 class AuthenticatedRequestInterface; | 27 class AuthenticatedRequestInterface; |
27 class AuthService; | 28 class AuthService; |
28 class RequestRegistry; | |
29 | 29 |
30 // Helper class that sends requests implementing | 30 // Helper class that sends requests implementing |
31 // AuthenticatedRequestInterface and handles retries and authentication. | 31 // AuthenticatedRequestInterface and handles retries and authentication. |
32 class RequestSender { | 32 class RequestSender { |
33 public: | 33 public: |
34 // |url_request_context_getter| is used to perform authentication with | 34 // |url_request_context_getter| is used to perform authentication with |
35 // AuthService. | 35 // AuthService. |
36 // | 36 // |
37 // |scopes| specifies OAuth2 scopes. | 37 // |scopes| specifies OAuth2 scopes. |
38 // | 38 // |
39 // |custom_user_agent| will be used for the User-Agent header in HTTP | 39 // |custom_user_agent| will be used for the User-Agent header in HTTP |
40 // requests issued through the request sender if the value is not empty. | 40 // requests issued through the request sender if the value is not empty. |
41 RequestSender(Profile* profile, | 41 RequestSender(Profile* profile, |
42 net::URLRequestContextGetter* url_request_context_getter, | 42 net::URLRequestContextGetter* url_request_context_getter, |
43 const std::vector<std::string>& scopes, | 43 const std::vector<std::string>& scopes, |
44 const std::string& custom_user_agent); | 44 const std::string& custom_user_agent); |
45 virtual ~RequestSender(); | 45 virtual ~RequestSender(); |
46 | 46 |
47 AuthService* auth_service() { return auth_service_.get(); } | 47 AuthService* auth_service() { return auth_service_.get(); } |
48 RequestRegistry* request_registry() { | |
49 return request_registry_.get(); | |
50 } | |
51 | 48 |
52 // Prepares the object for use. | 49 // Prepares the object for use. |
53 virtual void Initialize(); | 50 virtual void Initialize(); |
54 | 51 |
55 // Starts a request implementing the AuthenticatedRequestInterface | 52 // Starts a request implementing the AuthenticatedRequestInterface |
56 // interface, and makes the request retry upon authentication failures by | 53 // interface, and makes the request retry upon authentication failures by |
57 // calling back to RetryRequest. | 54 // calling back to RetryRequest. The |request| object is owned by this |
| 55 // RequestSender. It will be deleted in RequestSender's destructor or |
| 56 // in RequestFinished(). |
58 // | 57 // |
59 // Returns a closure to cancel the request. The closure cancels the request | 58 // Returns a closure to cancel the request. The closure cancels the request |
60 // if it is in-flight, and does nothing if it is already terminated. | 59 // if it is in-flight, and does nothing if it is already terminated. |
61 base::Closure StartRequestWithRetry(AuthenticatedRequestInterface* request); | 60 base::Closure StartRequestWithRetry(AuthenticatedRequestInterface* request); |
62 | 61 |
| 62 // Notifies to this RequestSender that |request| has finished. |
| 63 // TODO(kinaba): refactor the life time management and make this at private. |
| 64 void RequestFinished(AuthenticatedRequestInterface* request); |
| 65 |
63 private: | 66 private: |
64 // Called when the access token is fetched. | 67 // Called when the access token is fetched. |
65 void OnAccessTokenFetched( | 68 void OnAccessTokenFetched( |
66 const base::WeakPtr<AuthenticatedRequestInterface>& request, | 69 const base::WeakPtr<AuthenticatedRequestInterface>& request, |
67 GDataErrorCode error, | 70 GDataErrorCode error, |
68 const std::string& access_token); | 71 const std::string& access_token); |
69 | 72 |
70 // Clears any authentication token and retries the request, which forces | 73 // Clears any authentication token and retries the request, which forces |
71 // an authentication token refresh. | 74 // an authentication token refresh. |
72 void RetryRequest(AuthenticatedRequestInterface* request); | 75 void RetryRequest(AuthenticatedRequestInterface* request); |
73 | 76 |
74 // Cancels the request. Used for implementing the returned closure of | 77 // Cancels the request. Used for implementing the returned closure of |
75 // StartRequestWithRetry. | 78 // StartRequestWithRetry. |
76 void CancelRequest( | 79 void CancelRequest( |
77 const base::WeakPtr<AuthenticatedRequestInterface>& request); | 80 const base::WeakPtr<AuthenticatedRequestInterface>& request); |
78 | 81 |
79 Profile* profile_; // Not owned. | 82 Profile* profile_; // Not owned. |
80 | 83 |
81 scoped_ptr<AuthService> auth_service_; | 84 scoped_ptr<AuthService> auth_service_; |
82 scoped_ptr<RequestRegistry> request_registry_; | 85 std::set<AuthenticatedRequestInterface*> in_flight_requests_; |
83 const std::string custom_user_agent_; | 86 const std::string custom_user_agent_; |
84 | 87 |
85 base::ThreadChecker thread_checker_; | 88 base::ThreadChecker thread_checker_; |
86 | 89 |
87 // Note: This should remain the last member so it'll be destroyed and | 90 // Note: This should remain the last member so it'll be destroyed and |
88 // invalidate its weak pointers before any other members are destroyed. | 91 // invalidate its weak pointers before any other members are destroyed. |
89 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; | 92 base::WeakPtrFactory<RequestSender> weak_ptr_factory_; |
90 | 93 |
91 DISALLOW_COPY_AND_ASSIGN(RequestSender); | 94 DISALLOW_COPY_AND_ASSIGN(RequestSender); |
92 }; | 95 }; |
93 | 96 |
94 } // namespace google_apis | 97 } // namespace google_apis |
95 | 98 |
96 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ | 99 #endif // CHROME_BROWSER_GOOGLE_APIS_REQUEST_SENDER_H_ |
OLD | NEW |