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 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 connection_code(code) {} | 99 connection_code(code) {} |
100 }; | 100 }; |
101 | 101 |
102 class SYNC_EXPORT_PRIVATE ServerConnectionEventListener { | 102 class SYNC_EXPORT_PRIVATE ServerConnectionEventListener { |
103 public: | 103 public: |
104 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; | 104 virtual void OnServerConnectionEvent(const ServerConnectionEvent& event) = 0; |
105 protected: | 105 protected: |
106 virtual ~ServerConnectionEventListener() {} | 106 virtual ~ServerConnectionEventListener() {} |
107 }; | 107 }; |
108 | 108 |
109 class ServerConnectionManager; | |
110 // A helper class that automatically notifies when the status changes. | |
111 // TODO(tim): This class shouldn't be exposed outside of the implementation, | |
112 // bug 35060. | |
113 class SYNC_EXPORT_PRIVATE ScopedServerStatusWatcher | |
114 : public base::NonThreadSafe { | |
115 public: | |
116 ScopedServerStatusWatcher(ServerConnectionManager* conn_mgr, | |
117 HttpResponse* response); | |
118 virtual ~ScopedServerStatusWatcher(); | |
119 private: | |
120 ServerConnectionManager* const conn_mgr_; | |
121 HttpResponse* const response_; | |
122 DISALLOW_COPY_AND_ASSIGN(ScopedServerStatusWatcher); | |
123 }; | |
124 | |
125 // Use this class to interact with the sync server. | 109 // Use this class to interact with the sync server. |
126 // The ServerConnectionManager currently supports POSTing protocol buffers. | 110 // The ServerConnectionManager currently supports POSTing protocol buffers. |
127 // | 111 // |
128 class SYNC_EXPORT_PRIVATE ServerConnectionManager : public CancelationObserver { | 112 class SYNC_EXPORT_PRIVATE ServerConnectionManager : public CancelationObserver { |
129 public: | 113 public: |
130 // buffer_in - will be POSTed | 114 // buffer_in - will be POSTed |
131 // buffer_out - string will be overwritten with response | 115 // buffer_out - string will be overwritten with response |
132 struct PostBufferParams { | 116 struct PostBufferParams { |
133 std::string buffer_in; | 117 std::string buffer_in; |
134 std::string buffer_out; | 118 std::string buffer_out; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 int port, | 166 int port, |
183 bool use_ssl, | 167 bool use_ssl, |
184 CancelationSignal* cancelation_signal); | 168 CancelationSignal* cancelation_signal); |
185 | 169 |
186 ~ServerConnectionManager() override; | 170 ~ServerConnectionManager() override; |
187 | 171 |
188 // POSTS buffer_in and reads a response into buffer_out. Uses our currently | 172 // POSTS buffer_in and reads a response into buffer_out. Uses our currently |
189 // set auth token in our headers. | 173 // set auth token in our headers. |
190 // | 174 // |
191 // Returns true if executed successfully. | 175 // Returns true if executed successfully. |
192 virtual bool PostBufferWithCachedAuth(PostBufferParams* params, | 176 virtual bool PostBufferWithCachedAuth(PostBufferParams* params); |
193 ScopedServerStatusWatcher* watcher); | |
194 | 177 |
195 void AddListener(ServerConnectionEventListener* listener); | 178 void AddListener(ServerConnectionEventListener* listener); |
196 void RemoveListener(ServerConnectionEventListener* listener); | 179 void RemoveListener(ServerConnectionEventListener* listener); |
197 | 180 |
198 inline HttpResponse::ServerConnectionCode server_status() const { | 181 inline HttpResponse::ServerConnectionCode server_status() const { |
199 DCHECK(thread_checker_.CalledOnValidThread()); | 182 DCHECK(thread_checker_.CalledOnValidThread()); |
200 return server_status_; | 183 return server_status_; |
201 } | 184 } |
202 | 185 |
203 const std::string client_id() const { return client_id_; } | 186 const std::string client_id() const { return client_id_; } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 226 } |
244 | 227 |
245 // Updates server_status_ and notifies listeners if server_status_ changed | 228 // Updates server_status_ and notifies listeners if server_status_ changed |
246 void SetServerStatus(HttpResponse::ServerConnectionCode server_status); | 229 void SetServerStatus(HttpResponse::ServerConnectionCode server_status); |
247 | 230 |
248 // NOTE: Tests rely on this protected function being virtual. | 231 // NOTE: Tests rely on this protected function being virtual. |
249 // | 232 // |
250 // Internal PostBuffer base function. | 233 // Internal PostBuffer base function. |
251 virtual bool PostBufferToPath(PostBufferParams*, | 234 virtual bool PostBufferToPath(PostBufferParams*, |
252 const std::string& path, | 235 const std::string& path, |
253 const std::string& auth_token, | 236 const std::string& auth_token); |
254 ScopedServerStatusWatcher* watcher); | |
255 | 237 |
256 // An internal helper to clear our auth_token_ and cache the old version | 238 // An internal helper to clear our auth_token_ and cache the old version |
257 // in |previously_invalidated_token_| to shelter us from retrying with a | 239 // in |previously_invalidated_token_| to shelter us from retrying with a |
258 // known bad token. | 240 // known bad token. |
259 void InvalidateAndClearAuthToken(); | 241 void InvalidateAndClearAuthToken(); |
260 | 242 |
261 // Helper to check terminated flags and build a Connection object, installing | 243 // Helper to check terminated flags and build a Connection object, installing |
262 // it as the |active_connection_|. If this ServerConnectionManager has been | 244 // it as the |active_connection_|. If this ServerConnectionManager has been |
263 // terminated, this will return NULL. | 245 // terminated, this will return NULL. |
264 Connection* MakeActiveConnection(); | 246 Connection* MakeActiveConnection(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 // If true, we've been told to terminate IO and expect to be destroyed | 282 // If true, we've been told to terminate IO and expect to be destroyed |
301 // shortly. No future network requests will be made. | 283 // shortly. No future network requests will be made. |
302 bool terminated_; | 284 bool terminated_; |
303 | 285 |
304 // A non-owning pointer to any active http connection, so that we can abort | 286 // A non-owning pointer to any active http connection, so that we can abort |
305 // it if necessary. | 287 // it if necessary. |
306 Connection* active_connection_; | 288 Connection* active_connection_; |
307 | 289 |
308 private: | 290 private: |
309 friend class Connection; | 291 friend class Connection; |
310 friend class ScopedServerStatusWatcher; | |
311 | 292 |
312 // A class to help deal with cleaning up active Connection objects when (for | 293 // A class to help deal with cleaning up active Connection objects when (for |
313 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper | 294 // ex) multiple early-exits are present in some scope. ScopedConnectionHelper |
314 // informs the ServerConnectionManager before the Connection object it takes | 295 // informs the ServerConnectionManager before the Connection object it takes |
315 // ownership of is destroyed. | 296 // ownership of is destroyed. |
316 class ScopedConnectionHelper { | 297 class ScopedConnectionHelper { |
317 public: | 298 public: |
318 // |manager| must outlive this. Takes ownership of |connection|. | 299 // |manager| must outlive this. Takes ownership of |connection|. |
319 ScopedConnectionHelper(ServerConnectionManager* manager, | 300 ScopedConnectionHelper(ServerConnectionManager* manager, |
320 Connection* connection); | 301 Connection* connection); |
(...skipping 11 matching lines...) Expand all Loading... |
332 bool signal_handler_registered_; | 313 bool signal_handler_registered_; |
333 | 314 |
334 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); | 315 DISALLOW_COPY_AND_ASSIGN(ServerConnectionManager); |
335 }; | 316 }; |
336 | 317 |
337 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); | 318 std::ostream& operator<<(std::ostream& s, const struct HttpResponse& hr); |
338 | 319 |
339 } // namespace syncer | 320 } // namespace syncer |
340 | 321 |
341 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ | 322 #endif // SYNC_ENGINE_NET_SERVER_CONNECTION_MANAGER_H_ |
OLD | NEW |