Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / public / http_bridge.h
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.
4
5 #ifndef SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_
6 #define SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_
7
8 #include <string>
9
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 "net/base/network_time_notifier.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"
25 #include "url/gurl.h"
26
27 class HttpBridgeTest;
28
29 namespace base {
30 class MessageLoop;
31 }
32
33 namespace net {
34 class HttpResponseHeaders;
35 class HttpUserAgentSettings;
36 class URLFetcher;
37 }
38
39 namespace syncer {
40
41 class CancelationSignal;
42
43 // A bridge between the syncer and Chromium HTTP layers.
44 // Provides a way for the sync backend to use Chromium directly for HTTP
45 // requests rather than depending on a third party provider (e.g libcurl).
46 // This is a one-time use bridge. Create one for each request you want to make.
47 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus
48 // needs to stick around across context switches, etc.
49 class SYNC_EXPORT_PRIVATE HttpBridge
50     : public base::RefCountedThreadSafe<HttpBridge>,
51       public HttpPostProviderInterface,
52       public net::URLFetcherDelegate {
53  public:
54   // A request context used for HTTP requests bridged from the sync backend.
55   // A bridged RequestContext has a dedicated in-memory cookie store and does
56   // not use a cache. Thus the same type can be used for incognito mode.
57   class RequestContext : public net::URLRequestContext {
58    public:
59     // |baseline_context| is used to obtain the accept-language
60     // and proxy service information for bridged requests.
61     // Typically |baseline_context| should be the net::URLRequestContext of the
62     // currently active profile.
63     RequestContext(
64         net::URLRequestContext* baseline_context,
65         const scoped_refptr<base::SingleThreadTaskRunner>&
66             network_task_runner,
67         const std::string& user_agent);
68
69     // The destructor MUST be called on the IO thread.
70     virtual ~RequestContext();
71
72    private:
73     net::URLRequestContext* const baseline_context_;
74     const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
75     scoped_ptr<net::HttpUserAgentSettings> http_user_agent_settings_;
76
77     DISALLOW_COPY_AND_ASSIGN(RequestContext);
78   };
79
80   // Lazy-getter for RequestContext objects.
81   class SYNC_EXPORT_PRIVATE RequestContextGetter
82       : public net::URLRequestContextGetter {
83    public:
84     RequestContextGetter(
85         net::URLRequestContextGetter* baseline_context_getter,
86         const std::string& user_agent);
87
88     // net::URLRequestContextGetter implementation.
89     virtual net::URLRequestContext* GetURLRequestContext() OVERRIDE;
90     virtual scoped_refptr<base::SingleThreadTaskRunner>
91         GetNetworkTaskRunner() const OVERRIDE;
92
93    protected:
94     virtual ~RequestContextGetter();
95
96    private:
97     scoped_refptr<net::URLRequestContextGetter> baseline_context_getter_;
98     const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
99     // User agent to apply to the net::URLRequestContext.
100     const std::string user_agent_;
101
102     // Lazily initialized by GetURLRequestContext().
103     scoped_ptr<RequestContext> context_;
104
105     DISALLOW_COPY_AND_ASSIGN(RequestContextGetter);
106   };
107
108   HttpBridge(RequestContextGetter* context,
109              const NetworkTimeUpdateCallback& network_time_update_callback);
110
111   // HttpPostProvider implementation.
112   virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE;
113   virtual void SetURL(const char* url, int port) OVERRIDE;
114   virtual void SetPostPayload(const char* content_type, int content_length,
115                               const char* content) OVERRIDE;
116   virtual bool MakeSynchronousPost(int* error_code,
117                                    int* response_code) OVERRIDE;
118   virtual void Abort() OVERRIDE;
119
120   // WARNING: these response content methods are used to extract plain old data
121   // and not null terminated strings, so you should make sure you have read
122   // GetResponseContentLength() characters when using GetResponseContent. e.g
123   // string r(b->GetResponseContent(), b->GetResponseContentLength()).
124   virtual int GetResponseContentLength() const OVERRIDE;
125   virtual const char* GetResponseContent() const OVERRIDE;
126   virtual const std::string GetResponseHeaderValue(
127       const std::string& name) const OVERRIDE;
128
129   // net::URLFetcherDelegate implementation.
130   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
131
132   net::URLRequestContextGetter* GetRequestContextGetterForTest() const;
133
134  protected:
135   friend class base::RefCountedThreadSafe<HttpBridge>;
136
137   virtual ~HttpBridge();
138
139   // Protected virtual so the unit test can override to shunt network requests.
140   virtual void MakeAsynchronousPost();
141
142  private:
143   friend class SyncHttpBridgeTest;
144   friend class ::HttpBridgeTest;
145
146   // Called on the IO loop to issue the network request. The extra level
147   // of indirection is so that the unit test can override this behavior but we
148   // still have a function to statically pass to PostTask.
149   void CallMakeAsynchronousPost() { MakeAsynchronousPost(); }
150
151   // Used to destroy a fetcher when the bridge is Abort()ed, to ensure that
152   // a reference to |this| is held while flushing any pending fetch completion
153   // callbacks coming from the IO thread en route to finally destroying the
154   // fetcher.
155   void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher);
156
157   void UpdateNetworkTime();
158
159   // The message loop of the thread we were created on. This is the thread that
160   // will block on MakeSynchronousPost while the IO thread fetches data from
161   // the network.
162   // This should be the main syncer thread (SyncerThread) which is what blocks
163   // on network IO through curl_easy_perform.
164   base::MessageLoop* const created_on_loop_;
165
166   // The URL to POST to.
167   GURL url_for_request_;
168
169   // POST payload information.
170   std::string content_type_;
171   std::string request_content_;
172   std::string extra_headers_;
173
174   // A waitable event we use to provide blocking semantics to
175   // MakeSynchronousPost. We block created_on_loop_ while the IO loop fetches
176   // network request.
177   base::WaitableEvent http_post_completed_;
178
179   struct URLFetchState {
180     URLFetchState();
181     ~URLFetchState();
182     // Our hook into the network layer is a URLFetcher. USED ONLY ON THE IO
183     // LOOP, so we can block created_on_loop_ while the fetch is in progress.
184     // NOTE: This is not a scoped_ptr for a reason. It must be deleted on the
185     // same thread that created it, which isn't the same thread |this| gets
186     // deleted on. We must manually delete url_poster_ on the IO loop.
187     net::URLFetcher* url_poster;
188
189     // Start and finish time of request. Set immediately before sending
190     // request and after receiving response.
191     base::Time start_time;
192     base::Time end_time;
193
194     // Used to support 'Abort' functionality.
195     bool aborted;
196
197     // Cached response data.
198     bool request_completed;
199     bool request_succeeded;
200     int http_response_code;
201     int error_code;
202     std::string response_content;
203     scoped_refptr<net::HttpResponseHeaders> response_headers;
204   };
205
206   // This lock synchronizes use of state involved in the flow to fetch a URL
207   // using URLFetcher, including |fetch_state_| and
208   // |context_getter_for_request_| on any thread, for example, this flow needs
209   // to be synchronized to gracefully clean up URLFetcher and return
210   // appropriate values in |error_code|.
211   mutable base::Lock fetch_state_lock_;
212   URLFetchState fetch_state_;
213
214   // Gets a customized net::URLRequestContext for bridged requests. See
215   // RequestContext definition for details.
216   scoped_refptr<RequestContextGetter> context_getter_for_request_;
217
218   const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
219
220   // Callback for updating network time.
221   NetworkTimeUpdateCallback network_time_update_callback_;
222
223   DISALLOW_COPY_AND_ASSIGN(HttpBridge);
224 };
225
226 class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory,
227                                       public CancelationObserver {
228  public:
229   HttpBridgeFactory(
230       net::URLRequestContextGetter* baseline_context_getter,
231       const NetworkTimeUpdateCallback& network_time_update_callback,
232       CancelationSignal* cancelation_signal);
233   virtual ~HttpBridgeFactory();
234
235   // HttpPostProviderFactory:
236   virtual void Init(const std::string& user_agent) OVERRIDE;
237   virtual HttpPostProviderInterface* Create() OVERRIDE;
238   virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE;
239
240   // CancelationObserver implementation:
241   virtual void OnSignalReceived() OVERRIDE;
242
243  private:
244   // Protects |request_context_getter_| and |baseline_request_context_getter_|.
245   base::Lock context_getter_lock_;
246
247   // This request context is the starting point for the request_context_getter_
248   // that we eventually use to make requests.  During shutdown we must drop all
249   // references to it before the ProfileSyncService's Shutdown() call is
250   // complete.
251   scoped_refptr<net::URLRequestContextGetter> baseline_request_context_getter_;
252
253   // This request context is built on top of the baseline context and shares
254   // common components. Takes a reference to the
255   // baseline_request_context_getter_.  It's mostly used on sync thread when
256   // creating connection but is released as soon as possible during shutdown.
257   // Protected by |context_getter_lock_|.
258   scoped_refptr<HttpBridge::RequestContextGetter> request_context_getter_;
259
260   NetworkTimeUpdateCallback network_time_update_callback_;
261
262   CancelationSignal* const cancelation_signal_;
263
264   DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory);
265 };
266
267 }  //  namespace syncer
268
269 #endif  // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_