OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 5 #ifndef SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 6 #define SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "base/threading/non_thread_safe.h" | 16 #include "base/threading/non_thread_safe.h" |
17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
18 #include "sync/base/sync_export.h" | 18 #include "sync/base/sync_export.h" |
| 19 #include "sync/internal_api/public/base/cancelation_observer.h" |
19 #include "sync/syncable/syncable_id.h" | 20 #include "sync/syncable/syncable_id.h" |
20 | 21 |
21 namespace sync_pb { | 22 namespace sync_pb { |
22 class ClientToServerMessage; | 23 class ClientToServerMessage; |
23 } | 24 } |
24 | 25 |
25 namespace syncer { | 26 namespace syncer { |
26 | 27 |
| 28 class CancelationSignal; |
| 29 |
27 namespace syncable { | 30 namespace syncable { |
28 class Directory; | 31 class Directory; |
29 } | 32 } |
30 | 33 |
31 static const int32 kUnsetResponseCode = -1; | 34 static const int32 kUnsetResponseCode = -1; |
32 static const int32 kUnsetContentLength = -1; | 35 static const int32 kUnsetContentLength = -1; |
33 static const int32 kUnsetPayloadLength = -1; | 36 static const int32 kUnsetPayloadLength = -1; |
34 | 37 |
35 // HttpResponse gathers the relevant output properties of an HTTP request. | 38 // HttpResponse gathers the relevant output properties of an HTTP request. |
36 // Depending on the value of the server_status code, response_code, and | 39 // Depending on the value of the server_status code, response_code, and |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 virtual ~ScopedServerStatusWatcher(); | 121 virtual ~ScopedServerStatusWatcher(); |
119 private: | 122 private: |
120 ServerConnectionManager* const conn_mgr_; | 123 ServerConnectionManager* const conn_mgr_; |
121 HttpResponse* const response_; | 124 HttpResponse* const response_; |
122 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); | 125 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); |
123 }; | 126 }; |
124 | 127 |
125 // Use this class to interact with the sync server. | 128 // Use this class to interact with the sync server. |
126 // The ServerConnectionManager currently supports POSTing protocol buffers. | 129 // The ServerConnectionManager currently supports POSTing protocol buffers. |
127 // | 130 // |
128 class SYNC_EXPORT_PRIVATE ServerConnectionManager { | 131 class SYNC_EXPORT_PRIVATE ServerConnectionManager : public CancelationObserver { |
129 public: | 132 public: |
130 // buffer_in - will be POSTed | 133 // buffer_in - will be POSTed |
131 // buffer_out - string will be overwritten with response | 134 // buffer_out - string will be overwritten with response |
132 struct PostBufferParams { | 135 struct PostBufferParams { |
133 std::string buffer_in; | 136 std::string buffer_in; |
134 std::string buffer_out; | 137 std::string buffer_out; |
135 HttpResponse response; | 138 HttpResponse response; |
136 }; | 139 }; |
137 | 140 |
138 // Abstract class providing network-layer functionality to the | 141 // Abstract class providing network-layer functionality to the |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 ServerConnectionManager* scm_; | 179 ServerConnectionManager* scm_; |
177 | 180 |
178 private: | 181 private: |
179 int ReadResponse(void* buffer, int length); | 182 int ReadResponse(void* buffer, int length); |
180 int ReadResponse(std::string* buffer, int length); | 183 int ReadResponse(std::string* buffer, int length); |
181 }; | 184 }; |
182 | 185 |
183 ServerConnectionManager(const std::string& server, | 186 ServerConnectionManager(const std::string& server, |
184 int port, | 187 int port, |
185 bool use_ssl, | 188 bool use_ssl, |
186 bool use_oauth2_token); | 189 bool use_oauth2_token, |
| 190 CancelationSignal* cancelation_signal); |
187 | 191 |
188 virtual ~ServerConnectionManager(); | 192 virtual ~ServerConnectionManager(); |
189 | 193 |
190 // POSTS buffer_in and reads a response into buffer_out. Uses our currently | 194 // POSTS buffer_in and reads a response into buffer_out. Uses our currently |
191 // set auth token in our headers. | 195 // set auth token in our headers. |
192 // | 196 // |
193 // Returns true if executed successfully. | 197 // Returns true if executed successfully. |
194 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, | 198 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, |
195 ScopedServerStatusWatcher* watcher); | 199 ScopedServerStatusWatcher* watcher); |
196 | 200 |
(...skipping 15 matching lines...) Expand all Loading... |
212 std::string GetServerHost() const; | 216 std::string GetServerHost() const; |
213 | 217 |
214 // Factory method to create an Connection object we can use for | 218 // Factory method to create an Connection object we can use for |
215 // communication with the server. | 219 // communication with the server. |
216 virtual Connection* MakeConnection(); | 220 virtual Connection* MakeConnection(); |
217 | 221 |
218 // Closes any active network connections to the sync server. | 222 // Closes any active network connections to the sync server. |
219 // We expect this to get called on a different thread than the valid | 223 // We expect this to get called on a different thread than the valid |
220 // ThreadChecker thread, as we want to kill any pending http traffic without | 224 // ThreadChecker thread, as we want to kill any pending http traffic without |
221 // having to wait for the request to complete. | 225 // having to wait for the request to complete. |
222 virtual void TerminateAllIO(); | 226 virtual void OnSignalReceived() OVERRIDE FINAL; |
223 | 227 |
224 void set_client_id(const std::string& client_id) { | 228 void set_client_id(const std::string& client_id) { |
225 DCHECK(thread_checker_.CalledOnValidThread()); | 229 DCHECK(thread_checker_.CalledOnValidThread()); |
226 DCHECK(client_id_.empty()); | 230 DCHECK(client_id_.empty()); |
227 client_id_.assign(client_id); | 231 client_id_.assign(client_id); |
228 } | 232 } |
229 | 233 |
230 // Sets a new auth token and time. | 234 // Sets a new auth token and time. |
231 bool SetAuthToken(const std::string& auth_token); | 235 bool SetAuthToken(const std::string& auth_token); |
232 | 236 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 ~ScopedConnectionHelper(); | 339 ~ScopedConnectionHelper(); |
336 Connection* get(); | 340 Connection* get(); |
337 private: | 341 private: |
338 ServerConnectionManager* manager_; | 342 ServerConnectionManager* manager_; |
339 scoped_ptr<Connection> connection_; | 343 scoped_ptr<Connection> connection_; |
340 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); | 344 DISALLOW_COPY_AND_ASSIGN(ScopedConnectionHelper); |
341 }; | 345 }; |
342 | 346 |
343 void NotifyStatusChanged(); | 347 void NotifyStatusChanged(); |
344 | 348 |
| 349 CancelationSignal* const cancelation_signal_; |
| 350 bool signal_handler_registered_; |
| 351 |
345 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 352 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
346 }; | 353 }; |
347 | 354 |
348 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 355 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
349 | 356 |
350 } // namespace syncer | 357 } // namespace syncer |
351 | 358 |
352 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 359 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
OLD | NEW |