| 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" | |
| 14 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 15 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 18 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
| 19 #include "sync/engine/net/url_translator.h" | 18 #include "sync/engine/net/url_translator.h" |
| 20 #include "sync/engine/syncer.h" | 19 #include "sync/engine/syncer.h" |
| 21 #include "sync/protocol/sync.pb.h" | 20 #include "sync/protocol/sync.pb.h" |
| 22 #include "sync/syncable/directory.h" | 21 #include "sync/syncable/directory.h" |
| 23 | 22 |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 response->server_status = conn_mgr->server_status_; | 169 response->server_status = conn_mgr->server_status_; |
| 171 } | 170 } |
| 172 | 171 |
| 173 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { | 172 ScopedServerStatusWatcher::~ScopedServerStatusWatcher() { |
| 174 conn_mgr_->SetServerStatus(response_->server_status); | 173 conn_mgr_->SetServerStatus(response_->server_status); |
| 175 } | 174 } |
| 176 | 175 |
| 177 ServerConnectionManager::ServerConnectionManager( | 176 ServerConnectionManager::ServerConnectionManager( |
| 178 const string& server, | 177 const string& server, |
| 179 int port, | 178 int port, |
| 180 bool use_ssl) | 179 bool use_ssl, |
| 180 bool use_oauth2_token) |
| 181 : sync_server_(server), | 181 : sync_server_(server), |
| 182 sync_server_port_(port), | 182 sync_server_port_(port), |
| 183 use_ssl_(use_ssl), | 183 use_ssl_(use_ssl), |
| 184 use_oauth2_token_(use_oauth2_token), |
| 184 proto_sync_path_(kSyncServerSyncPath), | 185 proto_sync_path_(kSyncServerSyncPath), |
| 185 server_status_(HttpResponse::NONE), | 186 server_status_(HttpResponse::NONE), |
| 186 terminated_(false), | 187 terminated_(false), |
| 187 active_connection_(NULL) { | 188 active_connection_(NULL) { |
| 188 } | 189 } |
| 189 | 190 |
| 190 ServerConnectionManager::~ServerConnectionManager() { | 191 ServerConnectionManager::~ServerConnectionManager() { |
| 191 } | 192 } |
| 192 | 193 |
| 193 ServerConnectionManager::Connection* | 194 ServerConnectionManager::Connection* |
| (...skipping 12 matching lines...) Expand all Loading... |
| 206 base::AutoLock lock(terminate_connection_lock_); | 207 base::AutoLock lock(terminate_connection_lock_); |
| 207 // |active_connection_| can be NULL already if it was aborted. Also, | 208 // |active_connection_| can be NULL already if it was aborted. Also, |
| 208 // it can legitimately be a different Connection object if a new Connection | 209 // it can legitimately be a different Connection object if a new Connection |
| 209 // was created after a previous one was Aborted and destroyed. | 210 // was created after a previous one was Aborted and destroyed. |
| 210 if (active_connection_ != connection) | 211 if (active_connection_ != connection) |
| 211 return; | 212 return; |
| 212 | 213 |
| 213 active_connection_ = NULL; | 214 active_connection_ = NULL; |
| 214 } | 215 } |
| 215 | 216 |
| 216 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token, | 217 bool ServerConnectionManager::SetAuthToken(const std::string& auth_token) { |
| 217 const base::Time& auth_token_time) { | |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 if (previously_invalidated_token != auth_token) { | 219 if (previously_invalidated_token != auth_token) { |
| 220 auth_token_.assign(auth_token); | 220 auth_token_.assign(auth_token); |
| 221 auth_token_time_ = auth_token_time; | |
| 222 previously_invalidated_token = std::string(); | 221 previously_invalidated_token = std::string(); |
| 223 return true; | 222 return true; |
| 224 } | 223 } |
| 225 return false; | 224 return false; |
| 226 } | 225 } |
| 227 | 226 |
| 228 void ServerConnectionManager::OnInvalidationCredentialsRejected() { | 227 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 } | |
| 241 InvalidateAndClearAuthToken(); | 228 InvalidateAndClearAuthToken(); |
| 242 SetServerStatus(HttpResponse::SYNC_AUTH_ERROR); | 229 SetServerStatus(HttpResponse::SYNC_AUTH_ERROR); |
| 243 } | 230 } |
| 244 | 231 |
| 245 void ServerConnectionManager::InvalidateAndClearAuthToken() { | 232 void ServerConnectionManager::InvalidateAndClearAuthToken() { |
| 246 DCHECK(thread_checker_.CalledOnValidThread()); | 233 DCHECK(thread_checker_.CalledOnValidThread()); |
| 247 // Copy over the token to previous invalid token. | 234 // Copy over the token to previous invalid token. |
| 248 if (!auth_token_.empty()) { | 235 if (!auth_token_.empty()) { |
| 249 previously_invalidated_token.assign(auth_token_); | 236 previously_invalidated_token.assign(auth_token_); |
| 250 auth_token_ = std::string(); | 237 auth_token_ = std::string(); |
| 251 auth_token_time_ = base::Time(); | |
| 252 } | 238 } |
| 253 } | 239 } |
| 254 | 240 |
| 255 void ServerConnectionManager::SetServerStatus( | 241 void ServerConnectionManager::SetServerStatus( |
| 256 HttpResponse::ServerConnectionCode server_status) { | 242 HttpResponse::ServerConnectionCode server_status) { |
| 257 if (server_status_ == server_status) | 243 if (server_status_ == server_status) |
| 258 return; | 244 return; |
| 259 server_status_ = server_status; | 245 server_status_ = server_status; |
| 260 NotifyStatusChanged(); | 246 NotifyStatusChanged(); |
| 261 } | 247 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; | 279 params->response.server_status = HttpResponse::CONNECTION_UNAVAILABLE; |
| 294 return false; | 280 return false; |
| 295 } | 281 } |
| 296 | 282 |
| 297 // Note that |post| may be aborted by now, which will just cause Init to fail | 283 // Note that |post| may be aborted by now, which will just cause Init to fail |
| 298 // with CONNECTION_UNAVAILABLE. | 284 // with CONNECTION_UNAVAILABLE. |
| 299 bool ok = post.get()->Init( | 285 bool ok = post.get()->Init( |
| 300 path.c_str(), auth_token, params->buffer_in, ¶ms->response); | 286 path.c_str(), auth_token, params->buffer_in, ¶ms->response); |
| 301 | 287 |
| 302 if (params->response.server_status == HttpResponse::SYNC_AUTH_ERROR) { | 288 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 } | |
| 315 InvalidateAndClearAuthToken(); | 289 InvalidateAndClearAuthToken(); |
| 316 } | 290 } |
| 317 | 291 |
| 318 if (!ok || net::HTTP_OK != params->response.response_code) | 292 if (!ok || net::HTTP_OK != params->response.response_code) |
| 319 return false; | 293 return false; |
| 320 | 294 |
| 321 if (post.get()->ReadBufferResponse( | 295 if (post.get()->ReadBufferResponse( |
| 322 ¶ms->buffer_out, ¶ms->response, true)) { | 296 ¶ms->buffer_out, ¶ms->response, true)) { |
| 323 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; | 297 params->response.server_status = HttpResponse::SERVER_CONNECTION_OK; |
| 324 return true; | 298 return true; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 | 357 |
| 384 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 358 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
| 385 s << " Response Code (bogus on error): " << hr.response_code; | 359 s << " Response Code (bogus on error): " << hr.response_code; |
| 386 s << " Content-Length (bogus on error): " << hr.content_length; | 360 s << " Content-Length (bogus on error): " << hr.content_length; |
| 387 s << " Server Status: " | 361 s << " Server Status: " |
| 388 << HttpResponse::GetServerConnectionCodeString(hr.server_status); | 362 << HttpResponse::GetServerConnectionCodeString(hr.server_status); |
| 389 return s; | 363 return s; |
| 390 } | 364 } |
| 391 | 365 |
| 392 } // namespace syncer | 366 } // namespace syncer |
| OLD | NEW |