1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_
6 #define SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/timer/timer.h"
17 #include "net/url_request/url_fetcher_delegate.h"
18 #include "net/url_request/url_request_context.h"
19 #include "net/url_request/url_request_context_getter.h"
20 #include "sync/base/sync_export.h"
21 #include "sync/internal_api/public/base/cancelation_observer.h"
22 #include "sync/internal_api/public/http_post_provider_factory.h"
23 #include "sync/internal_api/public/http_post_provider_interface.h"
24 #include "sync/internal_api/public/network_time_update_callback.h"
34 class HttpResponseHeaders
;
35 class HttpUserAgentSettings
;
37 class URLRequestJobFactory
;
42 class CancelationSignal
;
44 // A bridge between the syncer and Chromium HTTP layers.
45 // Provides a way for the sync backend to use Chromium directly for HTTP
46 // requests rather than depending on a third party provider (e.g libcurl).
47 // This is a one-time use bridge. Create one for each request you want to make.
48 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus
49 // needs to stick around across context switches, etc.
50 class SYNC_EXPORT_PRIVATE HttpBridge
51 : public base::RefCountedThreadSafe
<HttpBridge
>,
52 public HttpPostProviderInterface
,
53 public net::URLFetcherDelegate
{
55 HttpBridge(const std::string
& user_agent
,
56 const scoped_refptr
<net::URLRequestContextGetter
>& context
,
57 const NetworkTimeUpdateCallback
& network_time_update_callback
,
58 const BindToTrackerCallback
& bind_to_tracker_callback
);
60 // HttpPostProvider implementation.
61 void SetExtraRequestHeaders(const char* headers
) override
;
62 void SetURL(const char* url
, int port
) override
;
63 void SetPostPayload(const char* content_type
,
65 const char* content
) override
;
66 bool MakeSynchronousPost(int* error_code
, int* response_code
) override
;
67 void Abort() override
;
69 // WARNING: these response content methods are used to extract plain old data
70 // and not null terminated strings, so you should make sure you have read
71 // GetResponseContentLength() characters when using GetResponseContent. e.g
72 // string r(b->GetResponseContent(), b->GetResponseContentLength()).
73 int GetResponseContentLength() const override
;
74 const char* GetResponseContent() const override
;
75 const std::string
GetResponseHeaderValue(
76 const std::string
& name
) const override
;
78 // net::URLFetcherDelegate implementation.
79 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
80 void OnURLFetchDownloadProgress(const net::URLFetcher
* source
,
81 int64 current
, int64 total
) override
;
82 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
83 int64 current
, int64 total
) override
;
85 net::URLRequestContextGetter
* GetRequestContextGetterForTest() const;
88 friend class base::RefCountedThreadSafe
<HttpBridge
>;
90 ~HttpBridge() override
;
92 // Protected virtual so the unit test can override to shunt network requests.
93 virtual void MakeAsynchronousPost();
96 friend class SyncHttpBridgeTest
;
97 friend class ::HttpBridgeTest
;
99 // Called on the IO loop to issue the network request. The extra level
100 // of indirection is so that the unit test can override this behavior but we
101 // still have a function to statically pass to PostTask.
102 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); }
104 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that
105 // a reference to |this| is held while flushing any pending fetch completion
106 // callbacks coming from the IO thread en route to finally destroying the
108 void DestroyURLFetcherOnIOThread(net::URLFetcher
* fetcher
,
109 base::Timer
* fetch_timer
);
111 void UpdateNetworkTime();
113 // Helper method to abort the request if we timed out.
114 void OnURLFetchTimedOut();
116 // The message loop of the thread we were created on. This is the thread that
117 // will block on MakeSynchronousPost while the IO thread fetches data from
119 // This should be the main syncer thread (SyncerThread) which is what blocks
120 // on network IO through curl_easy_perform.
121 base::MessageLoop
* const created_on_loop_
;
123 // The user agent for all requests.
124 const std::string user_agent_
;
126 // The URL to POST to.
127 GURL url_for_request_
;
129 // POST payload information.
130 std::string content_type_
;
131 std::string request_content_
;
132 std::string extra_headers_
;
134 // A waitable event we use to provide blocking semantics to
135 // MakeSynchronousPost. We block created_on_loop_ while the IO loop fetches
137 base::WaitableEvent http_post_completed_
;
139 struct URLFetchState
{
142 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO
143 // LOOP, so we can block created_on_loop_ while the fetch is in progress.
144 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the
145 // same thread that created it, which isn't the same thread |this| gets
146 // deleted on. We must manually delete url_poster_ on the IO loop.
147 net::URLFetcher
* url_poster
;
149 // Start and finish time of request. Set immediately before sending
150 // request and after receiving response.
151 base::Time start_time
;
154 // Used to support 'Abort' functionality.
157 // Cached response data.
158 bool request_completed
;
159 bool request_succeeded
;
160 int http_response_code
;
162 std::string response_content
;
163 scoped_refptr
<net::HttpResponseHeaders
> response_headers
;
165 // Timer to ensure http requests aren't stalled. Reset every time upload or
166 // download progress is made.
167 scoped_ptr
<base::Timer
> http_request_timeout_timer
;
170 // This lock synchronizes use of state involved in the flow to fetch a URL
171 // using URLFetcher, including |fetch_state_| and |request_context_getter_| on
172 // any thread, for example, this flow needs to be synchronized to gracefully
173 // clean up URLFetcher and return appropriate values in |error_code|.
174 mutable base::Lock fetch_state_lock_
;
175 URLFetchState fetch_state_
;
177 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
179 const scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
181 // Callback for updating network time.
182 NetworkTimeUpdateCallback network_time_update_callback_
;
184 // A callback to tag Sync request to be able to record data use of this
185 // service by data_use_measurement component.
186 BindToTrackerCallback bind_to_tracker_callback_
;
188 DISALLOW_COPY_AND_ASSIGN(HttpBridge
);
191 class SYNC_EXPORT HttpBridgeFactory
: public HttpPostProviderFactory
,
192 public CancelationObserver
{
195 const scoped_refptr
<net::URLRequestContextGetter
>&
196 baseline_context_getter
,
197 const NetworkTimeUpdateCallback
& network_time_update_callback
,
198 CancelationSignal
* cancelation_signal
);
199 ~HttpBridgeFactory() override
;
201 // HttpPostProviderFactory:
202 void Init(const std::string
& user_agent
,
203 const BindToTrackerCallback
& bind_to_tracker_callback
) override
;
204 HttpPostProviderInterface
* Create() override
;
205 void Destroy(HttpPostProviderInterface
* http
) override
;
207 // CancelationObserver implementation:
208 void OnSignalReceived() override
;
211 // The user agent to use in all requests.
212 std::string user_agent_
;
214 // Protects |request_context_getter_| to allow releasing it's reference from
215 // the sync thread, even when it's in use on the IO thread.
216 base::Lock request_context_getter_lock_
;
218 // The request context getter used for making all requests.
219 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
221 NetworkTimeUpdateCallback network_time_update_callback_
;
223 CancelationSignal
* const cancelation_signal_
;
225 // A callback to tag Sync request to be able to record data use of this
226 // service by data_use_measurement component.
227 BindToTrackerCallback bind_to_tracker_callback_
;
229 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory
);
232 } // namespace syncer
234 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_