| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 #include "sync/engine/net/server_connection_manager.h" | 5 #include "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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/metrics/histogram.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/http/http_status_code.h" | 18 #include "net/http/http_status_code.h" |
| 18 #include "sync/engine/net/url_translator.h" | 19 #include "sync/engine/net/url_translator.h" |
| 19 #include "sync/engine/syncer.h" | 20 #include "sync/engine/syncer.h" |
| 20 #include "sync/protocol/sync.pb.h" | 21 #include "sync/protocol/sync.pb.h" |
| 21 #include "sync/syncable/directory.h" | 22 #include "sync/syncable/directory.h" |
| 22 | 23 |
| 23 namespace syncer { | 24 namespace syncer { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 205 base::AutoLock lock(terminate_connection_lock_); | 206 base::AutoLock lock(terminate_connection_lock_); |
| 206 // |active_connection_| can be NULL already if it was aborted. Also, | 207 // |active_connection_| can be NULL already if it was aborted. Also, |
| 207 // it can legitimately be a different Connection object if a new Connection | 208 // it can legitimately be a different Connection object if a new Connection |
| 208 // was created after a previous one was Aborted and destroyed. | 209 // was created after a previous one was Aborted and destroyed. |
| 209 if (active_connection_ != connection) | 210 if (active_connection_ != connection) |
| 210 return; | 211 return; |
| 211 | 212 |
| 212 active_connection_ = NULL; | 213 active_connection_ = NULL; |
| 213 } | 214 } |
| 214 | 215 |
| 216 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token, |
| 217 const base::Time& auth_token_time) { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 if (previously_invalidated_token != auth_token) { |
| 220 auth_token_.assign(auth_token); |
| 221 auth_token_time_ = auth_token_time; |
| 222 previously_invalidated_token = std::string(); |
| 223 return true; |
| 224 } |
| 225 return false; |
| 226 } |
| 227 |
| 215 void ServerConnectionManager::OnInvalidationCredentialsRejected() { | 228 void ServerConnectionManager::OnInvalidationCredentialsRejected() { |
| 229 if (!auth_token_time_.is_null()) { |
| 230 base::TimeDelta age = base::Time::Now() - auth_token_time_; |
| 231 if (age < base::TimeDelta::FromHours(1)) { |
| 232 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.AuthInvalidationRejectedTokenAgeShort", |
| 233 age, |
| 234 base::TimeDelta::FromSeconds(1), |
| 235 base::TimeDelta::FromHours(1), |
| 236 50); |
| 237 } |
| 238 UMA_HISTOGRAM_COUNTS("Sync.AuthInvalidationRejectedTokenAgeLong", |
| 239 age.InDays()); |
| 240 } |
| 216 InvalidateAndClearAuthToken(); | 241 InvalidateAndClearAuthToken(); |
| 217 SetServerStatus(HttpResponse::SYNC_AUTH_ERROR); | 242 SetServerStatus(HttpResponse::SYNC_AUTH_ERROR); |
| 218 } | 243 } |
| 219 | 244 |
| 220 void ServerConnectionManager::InvalidateAndClearAuthToken() { | 245 void ServerConnectionManager::InvalidateAndClearAuthToken() { |
| 221 DCHECK(thread_checker_.CalledOnValidThread()); | 246 DCHECK(thread_checker_.CalledOnValidThread()); |
| 222 // Copy over the token to previous invalid token. | 247 // Copy over the token to previous invalid token. |
| 223 if (!auth_token_.empty()) { | 248 if (!auth_token_.empty()) { |
| 224 previously_invalidated_token.assign(auth_token_); | 249 previously_invalidated_token.assign(auth_token_); |
| 225 auth_token_ = std::string(); | 250 auth_token_ = std::string(); |
| 251 auth_token_time_ = base::Time(); |
| 226 } | 252 } |
| 227 } | 253 } |
| 228 | 254 |
| 229 void ServerConnectionManager::SetServerStatus( | 255 void ServerConnectionManager::SetServerStatus( |
| 230 HttpResponse::ServerConnectionCode server_status) { | 256 HttpResponse::ServerConnectionCode server_status) { |
| 231 if (server_status_ == server_status) | 257 if (server_status_ == server_status) |
| 232 return; | 258 return; |
| 233 server_status_ = server_status; | 259 server_status_ = server_status; |
| 234 NotifyStatusChanged(); | 260 NotifyStatusChanged(); |
| 235 } | 261 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 266 if (!post.get()) { | 292 if (!post.get()) { |
| 267 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; | 293 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; |
| 268 return false; | 294 return false; |
| 269 } | 295 } |
| 270 | 296 |
| 271 // Note that |post| may be aborted by now, which will just cause Init to fail | 297 // Note that |post| may be aborted by now, which will just cause Init to fail |
| 272 // with CONNECTION_UNAVAILABLE. | 298 // with CONNECTION_UNAVAILABLE. |
| 273 bool ok = post.get()->Init( | 299 bool ok = post.get()->Init( |
| 274 path.c_str(), auth_token, params->buffer_in, ¶ms->response); | 300 path.c_str(), auth_token, params->buffer_in, ¶ms->response); |
| 275 | 301 |
| 276 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) | 302 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) { |
| 303 if (!auth_token_time_.is_null()) { |
| 304 base::TimeDelta age = base::Time::Now() - auth_token_time_; |
| 305 if (age < base::TimeDelta::FromHours(1)) { |
| 306 UMA_HISTOGRAM_CUSTOM_TIMES("Sync.AuthServerRejectedTokenAgeShort", |
| 307 age, |
| 308 base::TimeDelta::FromSeconds(1), |
| 309 base::TimeDelta::FromHours(1), |
| 310 50); |
| 311 } |
| 312 UMA_HISTOGRAM_COUNTS("Sync.AuthServerRejectedTokenAgeLong", |
| 313 age.InDays()); |
| 314 } |
| 277 InvalidateAndClearAuthToken(); | 315 InvalidateAndClearAuthToken(); |
| 316 } |
| 278 | 317 |
| 279 if (!ok || net::HTTP_OK != params->response.response_code) | 318 if (!ok || net::HTTP_OK != params->response.response_code) |
| 280 return false; | 319 return false; |
| 281 | 320 |
| 282 if (post.get()->ReadBufferResponse( | 321 if (post.get()->ReadBufferResponse( |
| 283 ¶ms->buffer_out, ¶ms->response, true)) { | 322 ¶ms->buffer_out, ¶ms->response, true)) { |
| 284 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; | 323 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; |
| 285 return true; | 324 return true; |
| 286 } | 325 } |
| 287 return false; | 326 return false; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 | 383 |
| 345 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 384 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
| 346 s << " Response Code (bogus on error): " << hr.response_code; | 385 s << " Response Code (bogus on error): " << hr.response_code; |
| 347 s << " Content-Length (bogus on error): " << hr.content_length; | 386 s << " Content-Length (bogus on error): " << hr.content_length; |
| 348 s << " Server Status: " | 387 s << " Server Status: " |
| 349 << HttpResponse::GetServerConnectionCodeString(hr.server_status); | 388 << HttpResponse::GetServerConnectionCodeString(hr.server_status); |
| 350 return s; | 389 return s; |
| 351 } | 390 } |
| 352 | 391 |
| 353 } // namespace syncer | 392 } // namespace syncer |
| OLD | NEW |