Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: sync/internal_api/public/http_bridge.h

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <stdint.h>
9
10 #include <memory>
11 #include <string>
12
13 #include "base/compiler_specific.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/synchronization/lock.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/threading/thread_checker.h"
19 #include "base/timer/timer.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "sync/base/sync_export.h"
24 #include "sync/internal_api/public/base/cancelation_observer.h"
25 #include "sync/internal_api/public/http_post_provider_factory.h"
26 #include "sync/internal_api/public/http_post_provider_interface.h"
27 #include "sync/internal_api/public/network_time_update_callback.h"
28 #include "url/gurl.h"
29
30 class HttpBridgeTest;
31
32 namespace net {
33 class HttpResponseHeaders;
34 class HttpUserAgentSettings;
35 class URLFetcher;
36 class URLRequestJobFactory;
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 HttpBridge : public base::RefCountedThreadSafe<HttpBridge>,
50 public HttpPostProviderInterface,
51 public net::URLFetcherDelegate {
52 public:
53 HttpBridge(const std::string& user_agent,
54 const scoped_refptr<net::URLRequestContextGetter>& context,
55 const NetworkTimeUpdateCallback& network_time_update_callback,
56 const BindToTrackerCallback& bind_to_tracker_callback);
57
58 // HttpPostProvider implementation.
59 void SetExtraRequestHeaders(const char* headers) override;
60 void SetURL(const char* url, int port) override;
61 void SetPostPayload(const char* content_type,
62 int content_length,
63 const char* content) override;
64 bool MakeSynchronousPost(int* error_code, int* response_code) override;
65 void Abort() override;
66
67 // WARNING: these response content methods are used to extract plain old data
68 // and not null terminated strings, so you should make sure you have read
69 // GetResponseContentLength() characters when using GetResponseContent. e.g
70 // string r(b->GetResponseContent(), b->GetResponseContentLength()).
71 int GetResponseContentLength() const override;
72 const char* GetResponseContent() const override;
73 const std::string GetResponseHeaderValue(
74 const std::string& name) const override;
75
76 // net::URLFetcherDelegate implementation.
77 void OnURLFetchComplete(const net::URLFetcher* source) override;
78 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
79 int64_t current,
80 int64_t total) override;
81 void OnURLFetchUploadProgress(const net::URLFetcher* source,
82 int64_t current,
83 int64_t total) override;
84
85 net::URLRequestContextGetter* GetRequestContextGetterForTest() const;
86
87 protected:
88 ~HttpBridge() override;
89
90 // Protected virtual so the unit test can override to shunt network requests.
91 virtual void MakeAsynchronousPost();
92
93 private:
94 friend class base::RefCountedThreadSafe<HttpBridge>;
95 friend class SyncHttpBridgeTest;
96 friend class ::HttpBridgeTest;
97
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(); }
102
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
106 // fetcher.
107 void DestroyURLFetcherOnIOThread(net::URLFetcher* fetcher,
108 base::Timer* fetch_timer);
109
110 void UpdateNetworkTime();
111
112 // Helper method to abort the request if we timed out.
113 void OnURLFetchTimedOut();
114
115 // Used to check whether a method runs on the thread that we were created on.
116 // This is the thread that will block on MakeSynchronousPost while the IO
117 // thread fetches data from the network.
118 // This should be the main syncer thread (SyncerThread) which is what blocks
119 // on network IO through curl_easy_perform.
120 base::ThreadChecker thread_checker_;
121
122 // The user agent for all requests.
123 const std::string user_agent_;
124
125 // The URL to POST to.
126 GURL url_for_request_;
127
128 // POST payload information.
129 std::string content_type_;
130 std::string request_content_;
131 std::string extra_headers_;
132
133 // A waitable event we use to provide blocking semantics to
134 // MakeSynchronousPost. We block created_on_loop_ while the IO loop fetches
135 // network request.
136 base::WaitableEvent http_post_completed_;
137
138 struct URLFetchState {
139 URLFetchState();
140 ~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 unique_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;
147
148 // Start and finish time of request. Set immediately before sending
149 // request and after receiving response.
150 base::Time start_time;
151 base::Time end_time;
152
153 // Used to support 'Abort' functionality.
154 bool aborted;
155
156 // Cached response data.
157 bool request_completed;
158 bool request_succeeded;
159 int http_response_code;
160 int error_code;
161 std::string response_content;
162 scoped_refptr<net::HttpResponseHeaders> response_headers;
163
164 // Timer to ensure http requests aren't stalled. Reset every time upload or
165 // download progress is made.
166 std::unique_ptr<base::Timer> http_request_timeout_timer;
167 };
168
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_;
175
176 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
177
178 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
179
180 // Callback for updating network time.
181 NetworkTimeUpdateCallback network_time_update_callback_;
182
183 // A callback to tag Sync request to be able to record data use of this
184 // service by data_use_measurement component.
185 BindToTrackerCallback bind_to_tracker_callback_;
186
187 DISALLOW_COPY_AND_ASSIGN(HttpBridge);
188 };
189
190 class SYNC_EXPORT HttpBridgeFactory : public HttpPostProviderFactory,
191 public CancelationObserver {
192 public:
193 HttpBridgeFactory(
194 const scoped_refptr<net::URLRequestContextGetter>&
195 baseline_context_getter,
196 const NetworkTimeUpdateCallback& network_time_update_callback,
197 CancelationSignal* cancelation_signal);
198 ~HttpBridgeFactory() override;
199
200 // HttpPostProviderFactory:
201 void Init(const std::string& user_agent,
202 const BindToTrackerCallback& bind_to_tracker_callback) override;
203 HttpPostProviderInterface* Create() override;
204 void Destroy(HttpPostProviderInterface* http) override;
205
206 // CancelationObserver implementation:
207 void OnSignalReceived() override;
208
209 private:
210 // The user agent to use in all requests.
211 std::string user_agent_;
212
213 // Protects |request_context_getter_| to allow releasing it's reference from
214 // the sync thread, even when it's in use on the IO thread.
215 base::Lock request_context_getter_lock_;
216
217 // The request context getter used for making all requests.
218 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
219
220 NetworkTimeUpdateCallback network_time_update_callback_;
221
222 CancelationSignal* const cancelation_signal_;
223
224 // A callback to tag Sync request to be able to record data use of this
225 // service by data_use_measurement component.
226 BindToTrackerCallback bind_to_tracker_callback_;
227
228 DISALLOW_COPY_AND_ASSIGN(HttpBridgeFactory);
229 };
230
231 } // namespace syncer
232
233 #endif // SYNC_INTERNAL_API_PUBLIC_HTTP_BRIDGE_H_
OLDNEW
« no previous file with comments | « sync/internal_api/public/events/protocol_event.h ('k') | sync/internal_api/public/http_bridge_network_resources.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698