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

Side by Side Diff: chrome/browser/sync/engine/net/server_connection_manager.cc

Issue 9348036: Trim code from sync's ServerConnectionManager (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Back off some changes Created 8 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "chrome/browser/sync/engine/net/server_connection_manager.h" 5 #include "chrome/browser/sync/engine/net/server_connection_manager.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 8
9 #include <ostream> 9 #include <ostream>
10 #include <string> 10 #include <string>
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 int length) { 146 int length) {
147 int bytes_read = buffer_.length(); 147 int bytes_read = buffer_.length();
148 CHECK(length <= bytes_read); 148 CHECK(length <= bytes_read);
149 out_buffer->assign(buffer_); 149 out_buffer->assign(buffer_);
150 return bytes_read; 150 return bytes_read;
151 } 151 }
152 152
153 ScopedServerStatusWatcher::ScopedServerStatusWatcher( 153 ScopedServerStatusWatcher::ScopedServerStatusWatcher(
154 ServerConnectionManager* conn_mgr, HttpResponse* response) 154 ServerConnectionManager* conn_mgr, HttpResponse* response)
155 : conn_mgr_(conn_mgr), 155 : conn_mgr_(conn_mgr),
156 response_(response), 156 response_(response) {
157 server_reachable_(conn_mgr->server_reachable_) {
158 response->server_status = conn_mgr->server_status_; 157 response->server_status = conn_mgr->server_status_;
159 } 158 }
160 159
161 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { 160 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() {
162 if (conn_mgr_->server_status_ != response_->server_status) { 161 if (conn_mgr_->server_status_ != response_->server_status) {
lipalani1 2012/02/11 00:12:10 Is this still needed? Is this the auth notificatio
rlarocque 2012/02/11 01:31:36 This gets passed back to the SyncManager::OnServer
163 conn_mgr_->server_status_ = response_->server_status; 162 conn_mgr_->server_status_ = response_->server_status;
164 conn_mgr_->NotifyStatusChanged(); 163 conn_mgr_->NotifyStatusChanged();
165 return; 164 return;
166 } 165 }
167 // Notify if we've gone on or offline.
168 if (server_reachable_ != conn_mgr_->server_reachable_)
169 conn_mgr_->NotifyStatusChanged();
170 } 166 }
171 167
172 ServerConnectionManager::ServerConnectionManager( 168 ServerConnectionManager::ServerConnectionManager(
173 const string& server, 169 const string& server,
174 int port, 170 int port,
175 bool use_ssl, 171 bool use_ssl,
176 const string& user_agent) 172 const string& user_agent)
177 : sync_server_(server), 173 : sync_server_(server),
178 sync_server_port_(port), 174 sync_server_port_(port),
179 user_agent_(user_agent), 175 user_agent_(user_agent),
180 use_ssl_(use_ssl), 176 use_ssl_(use_ssl),
181 proto_sync_path_(kSyncServerSyncPath), 177 proto_sync_path_(kSyncServerSyncPath),
182 get_time_path_(kSyncServerGetTimePath), 178 get_time_path_(kSyncServerGetTimePath),
183 server_status_(HttpResponse::NONE), 179 server_status_(HttpResponse::NONE),
184 server_reachable_(false),
185 terminated_(false), 180 terminated_(false),
186 active_connection_(NULL) { 181 active_connection_(NULL) {
187 } 182 }
188 183
189 ServerConnectionManager::~ServerConnectionManager() { 184 ServerConnectionManager::~ServerConnectionManager() {
190 } 185 }
191 186
192 ServerConnectionManager::Connection* 187 ServerConnectionManager::Connection*
193 ServerConnectionManager::MakeActiveConnection() { 188 ServerConnectionManager::MakeActiveConnection() {
194 base::AutoLock lock(terminate_connection_lock_); 189 base::AutoLock lock(terminate_connection_lock_);
(...skipping 14 matching lines...) Expand all
209 if (active_connection_ != connection) 204 if (active_connection_ != connection)
210 return; 205 return;
211 206
212 active_connection_ = NULL; 207 active_connection_ = NULL;
213 } 208 }
214 209
215 void ServerConnectionManager::NotifyStatusChanged() { 210 void ServerConnectionManager::NotifyStatusChanged() {
216 DCHECK(thread_checker_.CalledOnValidThread()); 211 DCHECK(thread_checker_.CalledOnValidThread());
217 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_, 212 FOR_EACH_OBSERVER(ServerConnectionEventListener, listeners_,
218 OnServerConnectionEvent( 213 OnServerConnectionEvent(
219 ServerConnectionEvent(server_status_, server_reachable_))); 214 ServerConnectionEvent(server_status_)));
220 } 215 }
221 216
222 bool ServerConnectionManager::PostBufferWithCachedAuth( 217 bool ServerConnectionManager::PostBufferWithCachedAuth(
223 PostBufferParams* params, ScopedServerStatusWatcher* watcher) { 218 PostBufferParams* params, ScopedServerStatusWatcher* watcher) {
224 DCHECK(thread_checker_.CalledOnValidThread()); 219 DCHECK(thread_checker_.CalledOnValidThread());
225 string path = 220 string path =
226 MakeSyncServerPath(proto_sync_path(), MakeSyncQueryString(client_id_)); 221 MakeSyncServerPath(proto_sync_path(), MakeSyncQueryString(client_id_));
227 return PostBufferToPath(params, path, auth_token(), watcher); 222 return PostBufferToPath(params, path, auth_token(), watcher);
228 } 223 }
229 224
(...skipping 23 matching lines...) Expand all
253 248
254 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) 249 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR)
255 InvalidateAndClearAuthToken(); 250 InvalidateAndClearAuthToken();
256 251
257 if (!ok || RC_REQUEST_OK != params->response.response_code) 252 if (!ok || RC_REQUEST_OK != params->response.response_code)
258 return false; 253 return false;
259 254
260 if (post.get()->ReadBufferResponse( 255 if (post.get()->ReadBufferResponse(
261 &params->buffer_out, &params->response, true)) { 256 &params->buffer_out, &params->response, true)) {
262 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; 257 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK;
263 server_reachable_ = true;
264 return true; 258 return true;
265 } 259 }
266 return false; 260 return false;
267 } 261 }
268 262
269 bool ServerConnectionManager::CheckTime(int32* out_time) {
270 DCHECK(thread_checker_.CalledOnValidThread());
271
272 // Verify that the server really is reachable by checking the time. We need
273 // to do this because of wifi interstitials that intercept messages from the
274 // client and return HTTP OK instead of a redirect.
275 HttpResponse response;
276 ScopedServerStatusWatcher watcher(this, &response);
277 string post_body = "command=get_time";
278
279 for (int i = 0 ; i < 3; i++) {
280 ScopedConnectionHelper post(this, MakeActiveConnection());
281 if (!post.get())
282 break;
283
284 // Note that the server's get_time path doesn't require authentication.
285 string get_time_path =
286 MakeSyncServerPath(kSyncServerGetTimePath, post_body);
287 DVLOG(1) << "Requesting get_time from:" << get_time_path;
288
289 string blank_post_body;
290 bool ok = post.get()->Init(get_time_path.c_str(), blank_post_body,
291 blank_post_body, &response);
292 if (!ok) {
293 DVLOG(1) << "Unable to check the time";
294 continue;
295 }
296 string time_response;
297 time_response.resize(
298 static_cast<string::size_type>(response.content_length));
299 ok = post.get()->ReadDownloadResponse(&response, &time_response);
300 if (!ok || string::npos !=
301 time_response.find_first_not_of("0123456789")) {
302 LOG(ERROR) << "unable to read a non-numeric response from get_time:"
303 << time_response;
304 continue;
305 }
306 *out_time = atoi(time_response.c_str());
307 DVLOG(1) << "Server was reachable.";
308 return true;
309 }
310 return false;
311 }
312
313 bool ServerConnectionManager::IsServerReachable() {
314 DCHECK(thread_checker_.CalledOnValidThread());
315 int32 time;
316 return CheckTime(&time);
317 }
318
319 bool ServerConnectionManager::IsUserAuthenticated() {
320 DCHECK(thread_checker_.CalledOnValidThread());
321 return IsGoodReplyFromServer(server_status_);
322 }
323
324 bool ServerConnectionManager::CheckServerReachable() {
325 DCHECK(thread_checker_.CalledOnValidThread());
326 const bool server_is_reachable = IsServerReachable();
327 if (server_reachable_ != server_is_reachable) {
328 server_reachable_ = server_is_reachable;
329 NotifyStatusChanged();
330 }
331 return server_is_reachable;
332 }
333
334 void ServerConnectionManager::SetServerParameters(const string& server_url,
335 int port,
336 bool use_ssl) {
337 DCHECK(thread_checker_.CalledOnValidThread());
338 sync_server_ = server_url;
339 sync_server_port_ = port;
340 use_ssl_ = use_ssl;
341 }
342
343 // Returns the current server parameters in server_url and port. 263 // Returns the current server parameters in server_url and port.
344 void ServerConnectionManager::GetServerParameters(string* server_url, 264 void ServerConnectionManager::GetServerParameters(string* server_url,
345 int* port, 265 int* port,
346 bool* use_ssl) const { 266 bool* use_ssl) const {
347 if (server_url != NULL) 267 if (server_url != NULL)
348 *server_url = sync_server_; 268 *server_url = sync_server_;
349 if (port != NULL) 269 if (port != NULL)
350 *port = sync_server_port_; 270 *port = sync_server_port_;
351 if (use_ssl != NULL) 271 if (use_ssl != NULL)
352 *use_ssl = use_ssl_; 272 *use_ssl = use_ssl_;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 } 331 }
412 332
413 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { 333 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) {
414 s << " Response Code (bogus on error): " << hr.response_code; 334 s << " Response Code (bogus on error): " << hr.response_code;
415 s << " Content-Length (bogus on error): " << hr.content_length; 335 s << " Content-Length (bogus on error): " << hr.content_length;
416 s << " Server Status: " << hr.server_status; 336 s << " Server Status: " << hr.server_status;
417 return s; 337 return s;
418 } 338 }
419 339
420 } // namespace browser_sync 340 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698