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

Side by Side Diff: sync/engine/net/server_connection_manager.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
« no previous file with comments | « sync/engine/net/DEPS ('k') | sync/engine/net/server_connection_manager.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 SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_
6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_
7
8 #include <stdint.h>
9
10 #include <iosfwd>
11 #include <memory>
12 #include <string>
13
14 #include "base/atomicops.h"
15 #include "base/macros.h"
16 #include "base/observer_list.h"
17 #include "base/strings/string_util.h"
18 #include "base/synchronization/lock.h"
19 #include "base/threading/non_thread_safe.h"
20 #include "base/threading/thread_checker.h"
21 #include "sync/base/sync_export.h"
22 #include "sync/internal_api/public/base/cancelation_observer.h"
23 #include "sync/syncable/syncable_id.h"
24
25 namespace sync_pb {
26 class ClientToServerMessage;
27 }
28
29 namespace syncer {
30
31 class CancelationSignal;
32
33 namespace syncable {
34 class Directory;
35 }
36
37 static const int32_t kUnsetResponseCode = -1;
38 static const int32_t kUnsetContentLength = -1;
39 static const int32_t kUnsetPayloadLength = -1;
40
41 // HttpResponse gathers the relevant output properties of an HTTP request.
42 // Depending on the value of the server_status code, response_code, and
43 // content_length may not be valid.
44 struct SYNC_EXPORT HttpResponse {
45 enum ServerConnectionCode {
46 // For uninitialized state.
47 NONE,
48
49 // CONNECTION_UNAVAILABLE is returned when InternetConnect() fails.
50 CONNECTION_UNAVAILABLE,
51
52 // IO_ERROR is returned when reading/writing to a buffer has failed.
53 IO_ERROR,
54
55 // SYNC_SERVER_ERROR is returned when the HTTP status code indicates that
56 // a non-auth error has occured.
57 SYNC_SERVER_ERROR,
58
59 // SYNC_AUTH_ERROR is returned when the HTTP status code indicates that an
60 // auth error has occured (i.e. a 401 or sync-specific AUTH_INVALID
61 // response)
62 // TODO(tim): Caring about AUTH_INVALID is a layering violation. But
63 // this app-specific logic is being added as a stable branch hotfix so
64 // minimal changes prevail for the moment. Fix this! Bug 35060.
65 SYNC_AUTH_ERROR,
66
67 // SERVER_CONNECTION_OK is returned when request was handled correctly.
68 SERVER_CONNECTION_OK,
69
70 // RETRY is returned when a Commit request fails with a RETRY response from
71 // the server.
72 //
73 // TODO(idana): the server no longer returns RETRY so we should remove this
74 // value.
75 RETRY,
76 };
77
78 // The HTTP Status code.
79 int64_t response_code;
80
81 // The value of the Content-length header.
82 int64_t content_length;
83
84 // The size of a download request's payload.
85 int64_t payload_length;
86
87 // Identifies the type of failure, if any.
88 ServerConnectionCode server_status;
89
90 HttpResponse();
91
92 static const char* GetServerConnectionCodeString(
93 ServerConnectionCode code);
94 };
95
96 struct ServerConnectionEvent {
97 HttpResponse::ServerConnectionCode connection_code;
98 explicit ServerConnectionEvent(HttpResponse::ServerConnectionCode code) :
99 connection_code(code) {}
100 };
101
102 class SYNC_EXPORT ServerConnectionEventListener {
103 public:
104 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0;
105 protected:
106 virtual ~ServerConnectionEventListener() {}
107 };
108
109 // Use this class to interact with the sync server.
110 // The ServerConnectionManager currently supports POSTing protocol buffers.
111 //
112 class SYNC_EXPORT ServerConnectionManager : public CancelationObserver {
113 public:
114 // buffer_in - will be POSTed
115 // buffer_out - string will be overwritten with response
116 struct PostBufferParams {
117 std::string buffer_in;
118 std::string buffer_out;
119 HttpResponse response;
120 };
121
122 // Abstract class providing network-layer functionality to the
123 // ServerConnectionManager. Subclasses implement this using an HTTP stack of
124 // their choice.
125 class Connection {
126 public:
127 explicit Connection(ServerConnectionManager* scm);
128 virtual ~Connection();
129
130 // Called to initialize and perform an HTTP POST.
131 virtual bool Init(const char* path,
132 const std::string& auth_token,
133 const std::string& payload,
134 HttpResponse* response) = 0;
135
136 // Immediately abandons a pending HTTP POST request and unblocks caller
137 // in Init.
138 virtual void Abort() = 0;
139
140 bool ReadBufferResponse(std::string* buffer_out, HttpResponse* response,
141 bool require_response);
142 bool ReadDownloadResponse(HttpResponse* response, std::string* buffer_out);
143
144 protected:
145 std::string MakeConnectionURL(const std::string& sync_server,
146 const std::string& path,
147 bool use_ssl) const;
148
149 void GetServerParams(std::string* server,
150 int* server_port,
151 bool* use_ssl) const {
152 server->assign(scm_->sync_server_);
153 *server_port = scm_->sync_server_port_;
154 *use_ssl = scm_->use_ssl_;
155 }
156
157 std::string buffer_;
158 ServerConnectionManager* scm_;
159
160 private:
161 int ReadResponse(std::string* buffer, int length);
162 };
163
164 ServerConnectionManager(const std::string& server,
165 int port,
166 bool use_ssl,
167 CancelationSignal* cancelation_signal);
168
169 ~ServerConnectionManager() override;
170
171 // POSTS buffer_in and reads a response into buffer_out. Uses our currently
172 // set auth token in our headers.
173 //
174 // Returns true if executed successfully.
175 virtual bool PostBufferWithCachedAuth(PostBufferParams* params);
176
177 void AddListener(ServerConnectionEventListener* listener);
178 void RemoveListener(ServerConnectionEventListener* listener);
179
180 inline HttpResponse::ServerConnectionCode server_status() const {
181 DCHECK(thread_checker_.CalledOnValidThread());
182 return server_status_;
183 }
184
185 const std::string client_id() const { return client_id_; }
186
187 // Factory method to create an Connection object we can use for
188 // communication with the server.
189 virtual Connection* MakeConnection();
190
191 // Closes any active network connections to the sync server.
192 // We expect this to get called on a different thread than the valid
193 // ThreadChecker thread, as we want to kill any pending http traffic without
194 // having to wait for the request to complete.
195 void OnSignalReceived() final;
196
197 void set_client_id(const std::string& client_id) {
198 DCHECK(thread_checker_.CalledOnValidThread());
199 DCHECK(client_id_.empty());
200 client_id_.assign(client_id);
201 }
202
203 // Sets a new auth token and time.
204 bool SetAuthToken(const std::string& auth_token);
205
206 bool HasInvalidAuthToken() {
207 return auth_token_.empty();
208 }
209
210 const std::string auth_token() const {
211 DCHECK(thread_checker_.CalledOnValidThread());
212 return auth_token_;
213 }
214
215 protected:
216 inline std::string proto_sync_path() const {
217 return proto_sync_path_;
218 }
219
220 // Updates server_status_ and notifies listeners if server_status_ changed
221 void SetServerStatus(HttpResponse::ServerConnectionCode server_status);
222
223 // NOTE: Tests rely on this protected function being virtual.
224 //
225 // Internal PostBuffer base function.
226 virtual bool PostBufferToPath(PostBufferParams*,
227 const std::string& path,
228 const std::string& auth_token);
229
230 // An internal helper to clear our auth_token_ and cache the old version
231 // in |previously_invalidated_token_| to shelter us from retrying with a
232 // known bad token.
233 void InvalidateAndClearAuthToken();
234
235 // Helper to check terminated flags and build a Connection object, installing
236 // it as the |active_connection_|. If this ServerConnectionManager has been
237 // terminated, this will return NULL.
238 Connection* MakeActiveConnection();
239
240 // Called by Connection objects as they are destroyed to allow the
241 // ServerConnectionManager to cleanup active connections.
242 void OnConnectionDestroyed(Connection* connection);
243
244 // The sync_server_ is the server that requests will be made to.
245 std::string sync_server_;
246
247 // The sync_server_port_ is the port that HTTP requests will be made on.
248 int sync_server_port_;
249
250 // The unique id of the user's client.
251 std::string client_id_;
252
253 // Indicates whether or not requests should be made using HTTPS.
254 bool use_ssl_;
255
256 // The paths we post to.
257 std::string proto_sync_path_;
258
259 // The auth token to use in authenticated requests.
260 std::string auth_token_;
261
262 // The previous auth token that is invalid now.
263 std::string previously_invalidated_token;
264
265 base::ObserverList<ServerConnectionEventListener> listeners_;
266
267 HttpResponse::ServerConnectionCode server_status_;
268
269 base::ThreadChecker thread_checker_;
270
271 // Protects all variables below to allow bailing out of active connections.
272 base::Lock terminate_connection_lock_;
273
274 // If true, we've been told to terminate IO and expect to be destroyed
275 // shortly. No future network requests will be made.
276 bool terminated_;
277
278 // A non-owning pointer to any active http connection, so that we can abort
279 // it if necessary.
280 Connection* active_connection_;
281
282 private:
283 // A class to help deal with cleaning up active Connection objects when (for
284 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper
285 // informs the ServerConnectionManager before the Connection object it takes
286 // ownership of is destroyed.
287 class ScopedConnectionHelper {
288 public:
289 // |manager| must outlive this. Takes ownership of |connection|.
290 ScopedConnectionHelper(ServerConnectionManager* manager,
291 Connection* connection);
292 ~ScopedConnectionHelper();
293 Connection* get();
294 private:
295 ServerConnectionManager* manager_;
296 std::unique_ptr<Connection> connection_;
297 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper);
298 };
299
300 void NotifyStatusChanged();
301
302 CancelationSignal* const cancelation_signal_;
303 bool signal_handler_registered_;
304
305 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager);
306 };
307
308 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr);
309
310 } // namespace syncer
311
312 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_
OLDNEW
« no previous file with comments | « sync/engine/net/DEPS ('k') | sync/engine/net/server_connection_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698