| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/command_line.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 #include "chrome/browser/sync/engine/net/url_translator.h" | 15 #include "chrome/browser/sync/engine/net/url_translator.h" |
| 15 #include "chrome/browser/sync/engine/syncapi.h" | 16 #include "chrome/browser/sync/engine/syncapi.h" |
| 16 #include "chrome/browser/sync/engine/syncer.h" | 17 #include "chrome/browser/sync/engine/syncer.h" |
| 17 #include "chrome/browser/sync/engine/syncproto.h" | 18 #include "chrome/browser/sync/engine/syncproto.h" |
| 18 #include "chrome/browser/sync/protocol/sync.pb.h" | 19 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 19 #include "chrome/browser/sync/syncable/directory_manager.h" | 20 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 21 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/common/net/http_return.h" | 22 #include "chrome/common/net/http_return.h" |
| 21 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 22 | 24 |
| 23 namespace browser_sync { | 25 namespace browser_sync { |
| 24 | 26 |
| 25 using std::ostream; | 27 using std::ostream; |
| 26 using std::string; | 28 using std::string; |
| 27 using std::vector; | 29 using std::vector; |
| 28 | 30 |
| 29 static const char kSyncServerSyncPath[] = "/command/"; | 31 static const char kSyncServerSyncPath[] = "/command/"; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 bool use_ssl, | 139 bool use_ssl, |
| 138 const string& user_agent) | 140 const string& user_agent) |
| 139 : sync_server_(server), | 141 : sync_server_(server), |
| 140 sync_server_port_(port), | 142 sync_server_port_(port), |
| 141 user_agent_(user_agent), | 143 user_agent_(user_agent), |
| 142 use_ssl_(use_ssl), | 144 use_ssl_(use_ssl), |
| 143 proto_sync_path_(kSyncServerSyncPath), | 145 proto_sync_path_(kSyncServerSyncPath), |
| 144 get_time_path_(kSyncServerGetTimePath), | 146 get_time_path_(kSyncServerGetTimePath), |
| 145 error_count_(0), | 147 error_count_(0), |
| 146 channel_(new Channel(shutdown_event)), | 148 channel_(new Channel(shutdown_event)), |
| 149 listeners_(new ObserverListThreadSafe<ServerConnectionEventListener>()), |
| 147 server_status_(HttpResponse::NONE), | 150 server_status_(HttpResponse::NONE), |
| 148 server_reachable_(false), | 151 server_reachable_(false), |
| 149 reset_count_(0), | 152 reset_count_(0), |
| 150 terminate_all_io_(false) { | 153 terminate_all_io_(false) { |
| 151 } | 154 } |
| 152 | 155 |
| 153 ServerConnectionManager::~ServerConnectionManager() { | 156 ServerConnectionManager::~ServerConnectionManager() { |
| 154 delete channel_; | 157 delete channel_; |
| 155 } | 158 } |
| 156 | 159 |
| 157 void ServerConnectionManager::NotifyStatusChanged() { | 160 void ServerConnectionManager::NotifyStatusChanged() { |
| 158 ServerConnectionEvent event = { ServerConnectionEvent::STATUS_CHANGED, | 161 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 159 server_status_, | 162 switches::kNewSyncerThread)) { |
| 160 server_reachable_ }; | 163 listeners_->Notify(&ServerConnectionEventListener::OnServerConnectionEvent, |
| 161 channel_->NotifyListeners(event); | 164 ServerConnectionEvent2(server_status_, server_reachable_)); |
| 165 } else { |
| 166 ServerConnectionEvent event = { ServerConnectionEvent::STATUS_CHANGED, |
| 167 server_status_, |
| 168 server_reachable_ }; |
| 169 channel_->NotifyListeners(event); |
| 170 } |
| 162 } | 171 } |
| 163 | 172 |
| 164 bool ServerConnectionManager::PostBufferWithCachedAuth( | 173 bool ServerConnectionManager::PostBufferWithCachedAuth( |
| 165 const PostBufferParams* params, ScopedServerStatusWatcher* watcher) { | 174 const PostBufferParams* params, ScopedServerStatusWatcher* watcher) { |
| 166 string path = | 175 string path = |
| 167 MakeSyncServerPath(proto_sync_path(), MakeSyncQueryString(client_id_)); | 176 MakeSyncServerPath(proto_sync_path(), MakeSyncQueryString(client_id_)); |
| 168 return PostBufferToPath(params, path, auth_token(), watcher); | 177 return PostBufferToPath(params, path, auth_token(), watcher); |
| 169 } | 178 } |
| 170 | 179 |
| 171 bool ServerConnectionManager::PostBufferToPath(const PostBufferParams* params, | 180 bool ServerConnectionManager::PostBufferToPath(const PostBufferParams* params, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 // For unit tests. | 331 // For unit tests. |
| 323 if (server_url.empty()) | 332 if (server_url.empty()) |
| 324 return std::string(); | 333 return std::string(); |
| 325 // We just want the hostname, so we don't need to switch on use_ssl. | 334 // We just want the hostname, so we don't need to switch on use_ssl. |
| 326 server_url = "http://" + server_url; | 335 server_url = "http://" + server_url; |
| 327 GURL gurl(server_url); | 336 GURL gurl(server_url); |
| 328 DCHECK(gurl.is_valid()) << gurl; | 337 DCHECK(gurl.is_valid()) << gurl; |
| 329 return gurl.host(); | 338 return gurl.host(); |
| 330 } | 339 } |
| 331 | 340 |
| 341 void ServerConnectionManager::AddListener( |
| 342 ServerConnectionEventListener* listener) { |
| 343 listeners_->AddObserver(listener); |
| 344 } |
| 345 |
| 346 void ServerConnectionManager::RemoveListener( |
| 347 ServerConnectionEventListener* listener) { |
| 348 listeners_->RemoveObserver(listener); |
| 349 } |
| 350 |
| 332 ServerConnectionManager::Post* ServerConnectionManager::MakePost() { | 351 ServerConnectionManager::Post* ServerConnectionManager::MakePost() { |
| 333 return NULL; // For testing. | 352 return NULL; // For testing. |
| 334 } | 353 } |
| 335 | 354 |
| 336 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, | 355 bool FillMessageWithShareDetails(sync_pb::ClientToServerMessage* csm, |
| 337 syncable::DirectoryManager* manager, | 356 syncable::DirectoryManager* manager, |
| 338 const std::string& share) { | 357 const std::string& share) { |
| 339 syncable::ScopedDirLookup dir(manager, share); | 358 syncable::ScopedDirLookup dir(manager, share); |
| 340 if (!dir.good()) { | 359 if (!dir.good()) { |
| 341 VLOG(1) << "Dir lookup failed"; | 360 VLOG(1) << "Dir lookup failed"; |
| 342 return false; | 361 return false; |
| 343 } | 362 } |
| 344 string birthday = dir->store_birthday(); | 363 string birthday = dir->store_birthday(); |
| 345 if (!birthday.empty()) | 364 if (!birthday.empty()) |
| 346 csm->set_store_birthday(birthday); | 365 csm->set_store_birthday(birthday); |
| 347 csm->set_share(share); | 366 csm->set_share(share); |
| 348 return true; | 367 return true; |
| 349 } | 368 } |
| 350 | 369 |
| 351 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { | 370 std::ostream& operator << (std::ostream& s, const struct HttpResponse& hr) { |
| 352 s << " Response Code (bogus on error): " << hr.response_code; | 371 s << " Response Code (bogus on error): " << hr.response_code; |
| 353 s << " Content-Length (bogus on error): " << hr.content_length; | 372 s << " Content-Length (bogus on error): " << hr.content_length; |
| 354 s << " Server Status: " << hr.server_status; | 373 s << " Server Status: " << hr.server_status; |
| 355 return s; | 374 return s; |
| 356 } | 375 } |
| 357 | 376 |
| 358 } // namespace browser_sync | 377 } // namespace browser_sync |
| OLD | NEW |