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
);
59 // HttpPostProvider implementation.
60 void SetExtraRequestHeaders(const char* headers
) override
;
61 void SetURL(const char* url
, int port
) override
;
62 void SetPostPayload(const char* content_type
,
64 const char* content
) override
;
65 bool MakeSynchronousPost(int* error_code
, int* response_code
) override
;
66 void Abort() override
;
68 // WARNING: these response content methods are used to extract plain old data
69 // and not null terminated strings, so you should make sure you have read
70 // GetResponseContentLength() characters when using GetResponseContent. e.g
71 // string r(b->GetResponseContent(), b->GetResponseContentLength()).
72 int GetResponseContentLength() const override
;
73 const char* GetResponseContent() const override
;
74 const std::string
GetResponseHeaderValue(
75 const std::string
& name
) const override
;
77 // net::URLFetcherDelegate implementation.
78 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
79 void OnURLFetchDownloadProgress(const net::URLFetcher
* source
,
80 int64 current
, int64 total
) override
;
81 void OnURLFetchUploadProgress(const net::URLFetcher
* source
,
82 int64 current
, int64 total
) override
;
84 net::URLRequestContextGetter
* GetRequestContextGetterForTest() const;
87 friend class base::RefCountedThreadSafe
<HttpBridge
>;
89 ~HttpBridge() override
;
91 // Protected virtual so the unit test can override to shunt network requests.
92 virtual void MakeAsynchronousPost();
95 friend class SyncHttpBridgeTest
;
96 friend class ::HttpBridgeTest
;
98 // Called on the IO loop to issue the network request. The extra level
99 // of indirection is so that the unit test can override this behavior but we
100 // still have a function to statically pass to PostTask.
101 void CallMakeAsynchronousPost() { MakeAsynchronousPost(); }
103 // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that
104 // a reference to |this| is held while flushing any pending fetch completion
105 // callbacks coming from the IO thread en route to finally destroying the
107 void DestroyURLFetcherOnIOThread(net::URLFetcher
* fetcher
,
108 base::Timer
* fetch_timer
);
110 void UpdateNetworkTime();
112 // Helper method to abort the request if we timed out.
113 void OnURLFetchTimedOut();
115 // The message loop of the thread we were created on. This is the thread that
116 // will block on MakeSynchronousPost while the IO thread fetches data from
118 // This should be the main syncer thread (SyncerThread) which is what blocks
119 // on network IO through curl_easy_perform.
120 base::MessageLoop
* const created_on_loop_
;
122 // The user agent for all requests.
123 const std::string user_agent_
;
125 // The URL to POST to.
126 GURL url_for_request_
;
128 // POST payload information.
129 std::string content_type_
;
130 std::string request_content_
;
131 std::string extra_headers_
;
133 // A waitable event we use to provide blocking semantics to
134 // MakeSynchronousPost. We block created_on_loop_ while the IO loop fetches
136 base::WaitableEvent http_post_completed_
;
138 struct URLFetchState
{
141 // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO
142 // LOOP, so we can block created_on_loop_ while the fetch is in progress.
143 // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the
144 // same thread that created it, which isn't the same thread |this| gets
145 // deleted on. We must manually delete url_poster_ on the IO loop.
146 net::URLFetcher
* url_poster
;
148 // Start and finish time of request. Set immediately before sending
149 // request and after receiving response.
150 base::Time start_time
;
153 // Used to support 'Abort' functionality.
156 // Cached response data.
157 bool request_completed
;
158 bool request_succeeded
;
159 int http_response_code
;
161 std::string response_content
;
162 scoped_refptr
<net::HttpResponseHeaders
> response_headers
;
164 // Timer to ensure http requests aren't stalled. Reset every time upload or
165 // download progress is made.
166 scoped_ptr
<base::Timer
> http_request_timeout_timer
;
169 // This lock synchronizes use of state involved in the flow to fetch a URL
170 // using URLFetcher, including |fetch_state_| and |request_context_getter_| on
171 // any thread, for example, this flow needs to be synchronized to gracefully
172 // clean up URLFetcher and return appropriate values in |error_code|.
173 mutable base::Lock fetch_state_lock_
;
174 URLFetchState fetch_state_
;
176 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
178 const scoped_refptr
<base::SingleThreadTaskRunner
> network_task_runner_
;
180 // Callback for updating network time.
181 NetworkTimeUpdateCallback network_time_update_callback_
;
183 DISALLOW_COPY_AND_ASSIGN(HttpBridge
);
186 class SYNC_EXPORT HttpBridgeFactory
: public HttpPostProviderFactory
,
187 public CancelationObserver
{
190 const scoped_refptr
<net::URLRequestContextGetter
>&
191 baseline_context_getter
,
192 const NetworkTimeUpdateCallback
& network_time_update_callback
,
193 CancelationSignal
* cancelation_signal
);
194 ~HttpBridgeFactory() override
;
196 // HttpPostProviderFactory:
197 void Init(const std::string
& user_agent
) override
;
198 HttpPostProviderInterface
* Create() override
;
199 void Destroy(HttpPostProviderInterface
* http
) override
;
201 // CancelationObserver implementation:
202 void OnSignalReceived() override
;
205 // The user agent to use in all requests.
206 std::string user_agent_
;
208 // Protects |request_context_getter_| to allow releasing it's reference from
209 // the sync thread, even when it's in use on the IO thread.
210 base::Lock request_context_getter_lock_
;
212 // The request context getter used for making all requests.
213 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
215 NetworkTimeUpdateCallback network_time_update_callback_
;
217 CancelationSignal
* const cancelation_signal_
;
219 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory
);
222 } // namespace syncer
224 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_