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

Side by Side Diff: components/sync/core/http_bridge.h

Issue 2408463002: [Sync] Move network-related code from core/ to engine/net/. (Closed)
Patch Set: Use SyncServerConnectionManager instead of ServerConnectionManagerImpl. Created 4 years, 2 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
« no previous file with comments | « components/sync/BUILD.gn ('k') | components/sync/core/http_bridge.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 COMPONENTS_SYNC_CORE_HTTP_BRIDGE_H_
6 #define COMPONENTS_SYNC_CORE_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 "components/sync/base/cancelation_observer.h"
21 #include "components/sync/core/http_post_provider_factory.h"
22 #include "components/sync/core/http_post_provider_interface.h"
23 #include "components/sync/core/network_time_update_callback.h"
24 #include "net/url_request/url_fetcher_delegate.h"
25 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_context_getter.h"
27 #include "url/gurl.h"
28
29 class HttpBridgeTest;
30
31 namespace net {
32 class HttpResponseHeaders;
33 class HttpUserAgentSettings;
34 class URLFetcher;
35 class URLRequestJobFactory;
36 }
37
38 namespace syncer {
39
40 class CancelationSignal;
41
42 // A bridge between the syncer and Chromium HTTP layers.
43 // Provides a way for the sync backend to use Chromium directly for HTTP
44 // requests rather than depending on a third party provider (e.g libcurl).
45 // This is a one-time use bridge. Create one for each request you want to make.
46 // It is RefCountedThreadSafe because it can PostTask to the io loop, and thus
47 // needs to stick around across context switches, etc.
48 class HttpBridge : public base::RefCountedThreadSafe<HttpBridge>,
49 public HttpPostProviderInterface,
50 public net::URLFetcherDelegate {
51 public:
52 HttpBridge(const std::string& user_agent,
53 const scoped_refptr<net::URLRequestContextGetter>& context,
54 const NetworkTimeUpdateCallback& network_time_update_callback,
55 const BindToTrackerCallback& bind_to_tracker_callback);
56
57 // HttpPostProvider implementation.
58 void SetExtraRequestHeaders(const char* headers) override;
59 void SetURL(const char* url, int port) override;
60 void SetPostPayload(const char* content_type,
61 int content_length,
62 const char* content) override;
63 bool MakeSynchronousPost(int* error_code, int* response_code) override;
64 void Abort() override;
65
66 // WARNING: these response content methods are used to extract plain old data
67 // and not null terminated strings, so you should make sure you have read
68 // GetResponseContentLength() characters when using GetResponseContent. e.g
69 // string r(b->GetResponseContent(), b->GetResponseContentLength()).
70 int GetResponseContentLength() const override;
71 const char* GetResponseContent() const override;
72 const std::string GetResponseHeaderValue(
73 const std::string& name) const override;
74
75 // net::URLFetcherDelegate implementation.
76 void OnURLFetchComplete(const net::URLFetcher* source) override;
77 void OnURLFetchDownloadProgress(const net::URLFetcher* source,
78 int64_t current,
79 int64_t total,
80 int64_t current_network_bytes) 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 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 // COMPONENTS_SYNC_CORE_HTTP_BRIDGE_H_
OLDNEW
« no previous file with comments | « components/sync/BUILD.gn ('k') | components/sync/core/http_bridge.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698