Chromium Code Reviews| 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/syncapi.h" | 5 #include "chrome/browser/sync/engine/syncapi.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <bitset> | 8 #include <bitset> |
| 9 #include <iomanip> | 9 #include <iomanip> |
| 10 #include <list> | 10 #include <list> |
| 11 #include <queue> | 11 #include <queue> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/base64.h" | 15 #include "base/base64.h" |
| 16 #include "base/command_line.h" | |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 18 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 19 #include "base/scoped_ptr.h" | 20 #include "base/scoped_ptr.h" |
| 20 #include "base/sha1.h" | 21 #include "base/sha1.h" |
| 21 #include "base/string_number_conversions.h" | 22 #include "base/string_number_conversions.h" |
| 22 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 23 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
| 24 #include "base/task.h" | 25 #include "base/task.h" |
| 25 #include "base/utf_string_conversions.h" | 26 #include "base/utf_string_conversions.h" |
| 26 #include "base/values.h" | 27 #include "base/values.h" |
| 27 #include "chrome/browser/browser_thread.h" | 28 #include "chrome/browser/browser_thread.h" |
| 28 #include "chrome/browser/sync/sync_constants.h" | 29 #include "chrome/browser/sync/sync_constants.h" |
| 29 #include "chrome/browser/sync/engine/all_status.h" | 30 #include "chrome/browser/sync/engine/all_status.h" |
| 30 #include "chrome/browser/sync/engine/change_reorder_buffer.h" | 31 #include "chrome/browser/sync/engine/change_reorder_buffer.h" |
| 31 #include "chrome/browser/sync/engine/model_safe_worker.h" | 32 #include "chrome/browser/sync/engine/model_safe_worker.h" |
| 32 #include "chrome/browser/sync/engine/net/server_connection_manager.h" | 33 #include "chrome/browser/sync/engine/net/server_connection_manager.h" |
| 33 #include "chrome/browser/sync/engine/net/syncapi_server_connection_manager.h" | 34 #include "chrome/browser/sync/engine/net/syncapi_server_connection_manager.h" |
| 34 #include "chrome/browser/sync/engine/syncer.h" | 35 #include "chrome/browser/sync/engine/syncer.h" |
| 35 #include "chrome/browser/sync/engine/syncer_thread.h" | 36 #include "chrome/browser/sync/engine/syncer_thread.h" |
| 36 #include "chrome/browser/sync/js_arg_list.h" | 37 #include "chrome/browser/sync/js_arg_list.h" |
| 37 #include "chrome/browser/sync/js_backend.h" | 38 #include "chrome/browser/sync/js_backend.h" |
| 38 #include "chrome/browser/sync/js_event_router.h" | 39 #include "chrome/browser/sync/js_event_router.h" |
| 39 #include "chrome/browser/sync/notifier/server_notifier_thread.h" | 40 #include "chrome/browser/sync/notifier/sync_notifier_callback.h" |
| 40 #include "chrome/browser/sync/notifier/state_writer.h" | 41 #include "chrome/browser/sync/notifier/sync_notifier.h" |
| 42 #include "chrome/browser/sync/notifier/sync_notifier_factory.h" | |
| 41 #include "chrome/browser/sync/protocol/app_specifics.pb.h" | 43 #include "chrome/browser/sync/protocol/app_specifics.pb.h" |
| 42 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" | 44 #include "chrome/browser/sync/protocol/autofill_specifics.pb.h" |
| 43 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h" | 45 #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h" |
| 44 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" | 46 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" |
| 45 #include "chrome/browser/sync/protocol/nigori_specifics.pb.h" | 47 #include "chrome/browser/sync/protocol/nigori_specifics.pb.h" |
| 46 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" | 48 #include "chrome/browser/sync/protocol/preference_specifics.pb.h" |
| 47 #include "chrome/browser/sync/protocol/proto_value_conversions.h" | 49 #include "chrome/browser/sync/protocol/proto_value_conversions.h" |
| 48 #include "chrome/browser/sync/protocol/service_constants.h" | 50 #include "chrome/browser/sync/protocol/service_constants.h" |
| 49 #include "chrome/browser/sync/protocol/session_specifics.pb.h" | 51 #include "chrome/browser/sync/protocol/session_specifics.pb.h" |
| 50 #include "chrome/browser/sync/protocol/sync.pb.h" | 52 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 51 #include "chrome/browser/sync/protocol/theme_specifics.pb.h" | 53 #include "chrome/browser/sync/protocol/theme_specifics.pb.h" |
| 52 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" | 54 #include "chrome/browser/sync/protocol/typed_url_specifics.pb.h" |
| 53 #include "chrome/browser/sync/sessions/sync_session.h" | 55 #include "chrome/browser/sync/sessions/sync_session.h" |
| 54 #include "chrome/browser/sync/sessions/sync_session_context.h" | 56 #include "chrome/browser/sync/sessions/sync_session_context.h" |
| 55 #include "chrome/browser/sync/syncable/autofill_migration.h" | 57 #include "chrome/browser/sync/syncable/autofill_migration.h" |
| 56 #include "chrome/browser/sync/syncable/directory_manager.h" | 58 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 57 #include "chrome/browser/sync/syncable/nigori_util.h" | 59 #include "chrome/browser/sync/syncable/nigori_util.h" |
| 58 #include "chrome/browser/sync/syncable/syncable.h" | 60 #include "chrome/browser/sync/syncable/syncable.h" |
| 59 #include "chrome/browser/sync/util/crypto_helpers.h" | 61 #include "chrome/browser/sync/util/crypto_helpers.h" |
| 62 #include "chrome/common/chrome_switches.h" | |
| 60 #include "chrome/common/deprecated/event_sys.h" | 63 #include "chrome/common/deprecated/event_sys.h" |
| 61 #include "chrome/common/net/gaia/gaia_authenticator.h" | 64 #include "chrome/common/net/gaia/gaia_authenticator.h" |
| 62 #include "jingle/notifier/listener/mediator_thread_impl.h" | |
| 63 #include "jingle/notifier/listener/notification_constants.h" | |
| 64 #include "jingle/notifier/listener/talk_mediator.h" | |
| 65 #include "jingle/notifier/listener/talk_mediator_impl.h" | |
| 66 #include "net/base/network_change_notifier.h" | 65 #include "net/base/network_change_notifier.h" |
| 67 | 66 |
| 68 using browser_sync::AllStatus; | 67 using browser_sync::AllStatus; |
| 69 using browser_sync::Cryptographer; | 68 using browser_sync::Cryptographer; |
| 70 using browser_sync::KeyParams; | 69 using browser_sync::KeyParams; |
| 71 using browser_sync::ModelSafeRoutingInfo; | 70 using browser_sync::ModelSafeRoutingInfo; |
| 72 using browser_sync::ModelSafeWorker; | 71 using browser_sync::ModelSafeWorker; |
| 73 using browser_sync::ModelSafeWorkerRegistrar; | 72 using browser_sync::ModelSafeWorkerRegistrar; |
| 74 using browser_sync::ServerConnectionEvent; | 73 using browser_sync::ServerConnectionEvent; |
| 75 using browser_sync::SyncEngineEvent; | 74 using browser_sync::SyncEngineEvent; |
| 76 using browser_sync::SyncEngineEventListener; | 75 using browser_sync::SyncEngineEventListener; |
| 77 using browser_sync::Syncer; | 76 using browser_sync::Syncer; |
| 78 using browser_sync::SyncerThread; | 77 using browser_sync::SyncerThread; |
| 79 using browser_sync::kNigoriTag; | 78 using browser_sync::kNigoriTag; |
| 80 using browser_sync::sessions::SyncSessionContext; | 79 using browser_sync::sessions::SyncSessionContext; |
| 81 using notifier::TalkMediator; | |
| 82 using notifier::TalkMediatorImpl; | |
| 83 using std::list; | 80 using std::list; |
| 84 using std::hex; | 81 using std::hex; |
| 85 using std::string; | 82 using std::string; |
| 86 using std::vector; | 83 using std::vector; |
| 87 using syncable::Directory; | 84 using syncable::Directory; |
| 88 using syncable::DirectoryManager; | 85 using syncable::DirectoryManager; |
| 89 using syncable::Entry; | 86 using syncable::Entry; |
| 90 using syncable::SPECIFICS; | 87 using syncable::SPECIFICS; |
| 88 using sync_notifier::SyncNotifierFactory; | |
| 91 using sync_pb::AutofillProfileSpecifics; | 89 using sync_pb::AutofillProfileSpecifics; |
| 92 | 90 |
| 93 typedef GoogleServiceAuthError AuthError; | 91 typedef GoogleServiceAuthError AuthError; |
| 94 | 92 |
| 95 static const int kThreadExitTimeoutMsec = 60000; | 93 static const int kThreadExitTimeoutMsec = 60000; |
| 96 static const int kSSLPort = 443; | 94 static const int kSSLPort = 443; |
| 97 static const int kSyncerThreadDelayMsec = 250; | 95 static const int kSyncerThreadDelayMsec = 250; |
| 98 | 96 |
| 99 #if defined(OS_CHROMEOS) | 97 #if defined(OS_CHROMEOS) |
| 100 static const int kChromeOSNetworkChangeReactionDelayHackMsec = 5000; | 98 static const int kChromeOSNetworkChangeReactionDelayHackMsec = 5000; |
| (...skipping 1000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1101 | 1099 |
| 1102 const sync_pb::PasswordSpecificsData& | 1100 const sync_pb::PasswordSpecificsData& |
| 1103 SyncManager::ExtraPasswordChangeRecordData::unencrypted() const { | 1101 SyncManager::ExtraPasswordChangeRecordData::unencrypted() const { |
| 1104 return unencrypted_; | 1102 return unencrypted_; |
| 1105 } | 1103 } |
| 1106 | 1104 |
| 1107 ////////////////////////////////////////////////////////////////////////// | 1105 ////////////////////////////////////////////////////////////////////////// |
| 1108 // SyncManager's implementation: SyncManager::SyncInternal | 1106 // SyncManager's implementation: SyncManager::SyncInternal |
| 1109 class SyncManager::SyncInternal | 1107 class SyncManager::SyncInternal |
| 1110 : public net::NetworkChangeNotifier::Observer, | 1108 : public net::NetworkChangeNotifier::Observer, |
| 1111 public TalkMediator::Delegate, | 1109 public sync_notifier::SyncNotifierCallback, |
| 1112 public sync_notifier::StateWriter, | |
| 1113 public browser_sync::ChannelEventHandler<syncable::DirectoryChangeEvent>, | 1110 public browser_sync::ChannelEventHandler<syncable::DirectoryChangeEvent>, |
| 1114 public browser_sync::JsBackend, | 1111 public browser_sync::JsBackend, |
| 1115 public SyncEngineEventListener { | 1112 public SyncEngineEventListener { |
| 1116 static const int kDefaultNudgeDelayMilliseconds; | 1113 static const int kDefaultNudgeDelayMilliseconds; |
| 1117 static const int kPreferencesNudgeDelayMilliseconds; | 1114 static const int kPreferencesNudgeDelayMilliseconds; |
| 1118 public: | 1115 public: |
| 1119 explicit SyncInternal(SyncManager* sync_manager) | 1116 explicit SyncInternal(SyncManager* sync_manager) |
| 1120 : core_message_loop_(NULL), | 1117 : core_message_loop_(NULL), |
| 1121 parent_router_(NULL), | 1118 parent_router_(NULL), |
| 1122 sync_manager_(sync_manager), | 1119 sync_manager_(sync_manager), |
| 1123 registrar_(NULL), | 1120 registrar_(NULL), |
| 1124 notification_pending_(false), | 1121 notification_pending_(false), |
| 1125 initialized_(false), | 1122 initialized_(false), |
| 1126 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 1123 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 1127 server_notifier_thread_(NULL) { | |
| 1128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1129 } | 1125 } |
| 1130 | 1126 |
| 1131 virtual ~SyncInternal() { | 1127 virtual ~SyncInternal() { |
| 1132 DCHECK(!core_message_loop_); | 1128 DCHECK(!core_message_loop_); |
| 1133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1134 } | 1130 } |
| 1135 | 1131 |
| 1136 bool Init(const FilePath& database_location, | 1132 bool Init(const FilePath& database_location, |
| 1137 const std::string& sync_server_and_path, | 1133 const std::string& sync_server_and_path, |
| 1138 int port, | 1134 int port, |
| 1139 bool use_ssl, | 1135 bool use_ssl, |
| 1140 HttpPostProviderFactory* post_factory, | 1136 HttpPostProviderFactory* post_factory, |
| 1141 ModelSafeWorkerRegistrar* model_safe_worker_registrar, | 1137 ModelSafeWorkerRegistrar* model_safe_worker_registrar, |
| 1142 const char* user_agent, | 1138 const char* user_agent, |
| 1143 const SyncCredentials& credentials, | 1139 const SyncCredentials& credentials, |
| 1144 const notifier::NotifierOptions& notifier_options, | |
| 1145 const std::string& restored_key_for_bootstrapping, | 1140 const std::string& restored_key_for_bootstrapping, |
| 1146 bool setup_for_test_mode); | 1141 bool setup_for_test_mode); |
| 1147 | 1142 |
| 1148 // Sign into sync with given credentials. | 1143 // Sign into sync with given credentials. |
| 1149 // We do not verify the tokens given. After this call, the tokens are set | 1144 // We do not verify the tokens given. After this call, the tokens are set |
| 1150 // and the sync DB is open. True if successful, false if something | 1145 // and the sync DB is open. True if successful, false if something |
| 1151 // went wrong. | 1146 // went wrong. |
| 1152 bool SignIn(const SyncCredentials& credentials); | 1147 bool SignIn(const SyncCredentials& credentials); |
| 1153 | 1148 |
| 1154 // Update tokens that we're using in Sync. Email must stay the same. | 1149 // Update tokens that we're using in Sync. Email must stay the same. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1188 const syncable::DirectoryChangeEvent& event); | 1183 const syncable::DirectoryChangeEvent& event); |
| 1189 void HandleCalculateChangesChangeEventFromSyncer( | 1184 void HandleCalculateChangesChangeEventFromSyncer( |
| 1190 const syncable::DirectoryChangeEvent& event); | 1185 const syncable::DirectoryChangeEvent& event); |
| 1191 | 1186 |
| 1192 // Listens for notifications from the ServerConnectionManager | 1187 // Listens for notifications from the ServerConnectionManager |
| 1193 void HandleServerConnectionEvent(const ServerConnectionEvent& event); | 1188 void HandleServerConnectionEvent(const ServerConnectionEvent& event); |
| 1194 | 1189 |
| 1195 // Open the directory named with username_for_share | 1190 // Open the directory named with username_for_share |
| 1196 bool OpenDirectory(); | 1191 bool OpenDirectory(); |
| 1197 | 1192 |
| 1198 // Login to the talk mediator with the given credentials. | 1193 // Login to the sync notifier with the given credentials. |
| 1199 void TalkMediatorLogin( | 1194 void SyncNotifierLogin( |
| 1200 const std::string& email, const std::string& token); | 1195 const std::string& email, const std::string& token); |
| 1201 | 1196 |
| 1202 // TalkMediator::Delegate implementation. | 1197 // SyncNotifierCallback implementation. |
| 1203 virtual void OnNotificationStateChange( | 1198 virtual void OnNotificationStateChange( |
| 1204 bool notifications_enabled); | 1199 bool notifications_enabled); |
| 1205 | 1200 |
| 1206 virtual void OnIncomingNotification( | 1201 virtual void OnIncomingNotification( |
| 1207 const IncomingNotificationData& notification_data); | 1202 const IncomingNotificationData& notification_data); |
| 1208 | 1203 |
| 1209 virtual void OnOutgoingNotification(); | 1204 virtual void OnOutgoingNotification(); |
| 1210 | 1205 virtual void StoreCookie(const std::string& cookie); |
| 1211 // sync_notifier::StateWriter implementation. | |
| 1212 virtual void WriteState(const std::string& state); | |
| 1213 | 1206 |
| 1214 void AddObserver(SyncManager::Observer* observer); | 1207 void AddObserver(SyncManager::Observer* observer); |
| 1215 | 1208 |
| 1216 void RemoveObserver(SyncManager::Observer* observer); | 1209 void RemoveObserver(SyncManager::Observer* observer); |
| 1217 | 1210 |
| 1218 // Accessors for the private members. | 1211 // Accessors for the private members. |
| 1219 DirectoryManager* dir_manager() { return share_.dir_manager.get(); } | 1212 DirectoryManager* dir_manager() { return share_.dir_manager.get(); } |
| 1220 SyncAPIServerConnectionManager* connection_manager() { | 1213 SyncAPIServerConnectionManager* connection_manager() { |
| 1221 return connection_manager_.get(); | 1214 return connection_manager_.get(); |
| 1222 } | 1215 } |
| 1223 SyncerThread* syncer_thread() { return syncer_thread_.get(); } | 1216 SyncerThread* syncer_thread() { return syncer_thread_.get(); } |
| 1224 TalkMediator* talk_mediator() { return talk_mediator_.get(); } | |
| 1225 UserShare* GetUserShare() { return &share_; } | 1217 UserShare* GetUserShare() { return &share_; } |
| 1226 | 1218 |
| 1227 // Return the currently active (validated) username for use with syncable | 1219 // Return the currently active (validated) username for use with syncable |
| 1228 // types. | 1220 // types. |
| 1229 const std::string& username_for_share() const { | 1221 const std::string& username_for_share() const { |
| 1230 return share_.name; | 1222 return share_.name; |
| 1231 } | 1223 } |
| 1232 | 1224 |
| 1233 Status GetStatus(); | 1225 Status GetStatus(); |
| 1234 | 1226 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1322 | 1314 |
| 1323 // browser_sync::JsBackend implementation. | 1315 // browser_sync::JsBackend implementation. |
| 1324 virtual void SetParentJsEventRouter(browser_sync::JsEventRouter* router); | 1316 virtual void SetParentJsEventRouter(browser_sync::JsEventRouter* router); |
| 1325 virtual void RemoveParentJsEventRouter(); | 1317 virtual void RemoveParentJsEventRouter(); |
| 1326 virtual const browser_sync::JsEventRouter* GetParentJsEventRouter() const; | 1318 virtual const browser_sync::JsEventRouter* GetParentJsEventRouter() const; |
| 1327 virtual void ProcessMessage(const std::string& name, | 1319 virtual void ProcessMessage(const std::string& name, |
| 1328 const browser_sync::JsArgList& args, | 1320 const browser_sync::JsArgList& args, |
| 1329 const browser_sync::JsEventHandler* sender); | 1321 const browser_sync::JsEventHandler* sender); |
| 1330 | 1322 |
| 1331 private: | 1323 private: |
| 1332 // Helper to handle the details of initializing the TalkMediator. | |
| 1333 // Must be called only after OpenDirectory() is called. | |
| 1334 void InitializeTalkMediator(); | |
| 1335 | |
| 1336 // Helper to call OnAuthError when no authentication credentials are | 1324 // Helper to call OnAuthError when no authentication credentials are |
| 1337 // available. | 1325 // available. |
| 1338 void RaiseAuthNeededEvent(); | 1326 void RaiseAuthNeededEvent(); |
| 1339 | 1327 |
| 1340 // Helper to set initialized_ to true and raise an event to clients to notify | 1328 // Helper to set initialized_ to true and raise an event to clients to notify |
| 1341 // that initialization is complete and it is safe to send us changes. If | 1329 // that initialization is complete and it is safe to send us changes. If |
| 1342 // already initialized, this is a no-op. | 1330 // already initialized, this is a no-op. |
| 1343 void MarkAndNotifyInitializationComplete(); | 1331 void MarkAndNotifyInitializationComplete(); |
| 1344 | 1332 |
| 1345 // If there's a pending notification to be sent, either from the | 1333 // If there's a pending notification to be sent, either from the |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1455 | 1443 |
| 1456 browser_sync::JsEventRouter* parent_router_; | 1444 browser_sync::JsEventRouter* parent_router_; |
| 1457 | 1445 |
| 1458 // The ServerConnectionManager used to abstract communication between the | 1446 // The ServerConnectionManager used to abstract communication between the |
| 1459 // client (the Syncer) and the sync server. | 1447 // client (the Syncer) and the sync server. |
| 1460 scoped_ptr<SyncAPIServerConnectionManager> connection_manager_; | 1448 scoped_ptr<SyncAPIServerConnectionManager> connection_manager_; |
| 1461 | 1449 |
| 1462 // The thread that runs the Syncer. Needs to be explicitly Start()ed. | 1450 // The thread that runs the Syncer. Needs to be explicitly Start()ed. |
| 1463 scoped_refptr<SyncerThread> syncer_thread_; | 1451 scoped_refptr<SyncerThread> syncer_thread_; |
| 1464 | 1452 |
| 1465 // Notification (xmpp) handler. | 1453 // SyncNotifier to setup notifications. |
| 1466 scoped_ptr<TalkMediator> talk_mediator_; | 1454 scoped_ptr<sync_notifier::SyncNotifier> sync_notifier_; |
| 1467 | 1455 |
| 1468 // A multi-purpose status watch object that aggregates stats from various | 1456 // A multi-purpose status watch object that aggregates stats from various |
| 1469 // sync components. | 1457 // sync components. |
| 1470 AllStatus allstatus_; | 1458 AllStatus allstatus_; |
| 1471 | 1459 |
| 1472 // Each element of this array is a store of change records produced by | 1460 // Each element of this array is a store of change records produced by |
| 1473 // HandleChangeEvent during the CALCULATE_CHANGES step. The changes are | 1461 // HandleChangeEvent during the CALCULATE_CHANGES step. The changes are |
| 1474 // segregated by model type, and are stored here to be processed and | 1462 // segregated by model type, and are stored here to be processed and |
| 1475 // forwarded to the observer slightly later, at the TRANSACTION_ENDING | 1463 // forwarded to the observer slightly later, at the TRANSACTION_ENDING |
| 1476 // step by HandleTransactionEndingChangeEvent. The list is cleared in the | 1464 // step by HandleTransactionEndingChangeEvent. The list is cleared in the |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1503 | 1491 |
| 1504 // Set to true once Init has been called, and we know of an authenticated | 1492 // Set to true once Init has been called, and we know of an authenticated |
| 1505 // valid) username either from a fresh authentication attempt (as in | 1493 // valid) username either from a fresh authentication attempt (as in |
| 1506 // first-use case) or from a previous attempt stored in our UserSettings | 1494 // first-use case) or from a previous attempt stored in our UserSettings |
| 1507 // (as in the steady-state), and the syncable::Directory has been opened, | 1495 // (as in the steady-state), and the syncable::Directory has been opened, |
| 1508 // meaning we are ready to accept changes. Protected by initialized_mutex_ | 1496 // meaning we are ready to accept changes. Protected by initialized_mutex_ |
| 1509 // as it can get read/set by both the SyncerThread and the AuthWatcherThread. | 1497 // as it can get read/set by both the SyncerThread and the AuthWatcherThread. |
| 1510 bool initialized_; | 1498 bool initialized_; |
| 1511 mutable base::Lock initialized_mutex_; | 1499 mutable base::Lock initialized_mutex_; |
| 1512 | 1500 |
| 1513 notifier::NotifierOptions notifier_options_; | |
| 1514 | |
| 1515 // True if the SyncManager should be running in test mode (no syncer thread | 1501 // True if the SyncManager should be running in test mode (no syncer thread |
| 1516 // actually communicating with the server). | 1502 // actually communicating with the server). |
| 1517 bool setup_for_test_mode_; | 1503 bool setup_for_test_mode_; |
| 1518 | 1504 |
| 1519 syncable::ModelTypeSet enabled_types_; | 1505 // We still use p2p notifications for tests and it set from the command line. |
| 1506 bool using_p2p_notifications_; | |
| 1520 | 1507 |
| 1521 ScopedRunnableMethodFactory<SyncManager::SyncInternal> method_factory_; | 1508 ScopedRunnableMethodFactory<SyncManager::SyncInternal> method_factory_; |
| 1522 | |
| 1523 sync_notifier::ServerNotifierThread* server_notifier_thread_; | |
| 1524 }; | 1509 }; |
| 1525 const int SyncManager::SyncInternal::kDefaultNudgeDelayMilliseconds = 200; | 1510 const int SyncManager::SyncInternal::kDefaultNudgeDelayMilliseconds = 200; |
| 1526 const int SyncManager::SyncInternal::kPreferencesNudgeDelayMilliseconds = 2000; | 1511 const int SyncManager::SyncInternal::kPreferencesNudgeDelayMilliseconds = 2000; |
| 1527 | 1512 |
| 1528 SyncManager::Observer::~Observer() {} | 1513 SyncManager::Observer::~Observer() {} |
| 1529 | 1514 |
| 1530 SyncManager::SyncManager() { | 1515 SyncManager::SyncManager() { |
| 1531 data_ = new SyncInternal(this); | 1516 data_ = new SyncInternal(this); |
| 1532 } | 1517 } |
| 1533 | 1518 |
| 1534 bool SyncManager::Init(const FilePath& database_location, | 1519 bool SyncManager::Init(const FilePath& database_location, |
| 1535 const char* sync_server_and_path, | 1520 const char* sync_server_and_path, |
| 1536 int sync_server_port, | 1521 int sync_server_port, |
| 1537 bool use_ssl, | 1522 bool use_ssl, |
| 1538 HttpPostProviderFactory* post_factory, | 1523 HttpPostProviderFactory* post_factory, |
| 1539 ModelSafeWorkerRegistrar* registrar, | 1524 ModelSafeWorkerRegistrar* registrar, |
| 1540 const char* user_agent, | 1525 const char* user_agent, |
| 1541 const SyncCredentials& credentials, | 1526 const SyncCredentials& credentials, |
| 1542 const notifier::NotifierOptions& notifier_options, | |
| 1543 const std::string& restored_key_for_bootstrapping, | 1527 const std::string& restored_key_for_bootstrapping, |
| 1544 bool setup_for_test_mode) { | 1528 bool setup_for_test_mode) { |
| 1545 DCHECK(post_factory); | 1529 DCHECK(post_factory); |
| 1546 VLOG(1) << "SyncManager starting Init..."; | 1530 VLOG(1) << "SyncManager starting Init..."; |
| 1547 string server_string(sync_server_and_path); | 1531 string server_string(sync_server_and_path); |
| 1548 return data_->Init(database_location, | 1532 return data_->Init(database_location, |
| 1549 server_string, | 1533 server_string, |
| 1550 sync_server_port, | 1534 sync_server_port, |
| 1551 use_ssl, | 1535 use_ssl, |
| 1552 post_factory, | 1536 post_factory, |
| 1553 registrar, | 1537 registrar, |
| 1554 user_agent, | 1538 user_agent, |
| 1555 credentials, | 1539 credentials, |
| 1556 notifier_options, | |
| 1557 restored_key_for_bootstrapping, | 1540 restored_key_for_bootstrapping, |
| 1558 setup_for_test_mode); | 1541 setup_for_test_mode); |
| 1559 } | 1542 } |
| 1560 | 1543 |
| 1561 void SyncManager::UpdateCredentials(const SyncCredentials& credentials) { | 1544 void SyncManager::UpdateCredentials(const SyncCredentials& credentials) { |
| 1562 data_->UpdateCredentials(credentials); | 1545 data_->UpdateCredentials(credentials); |
| 1563 } | 1546 } |
| 1564 | 1547 |
| 1565 void SyncManager::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { | 1548 void SyncManager::UpdateEnabledTypes(const syncable::ModelTypeSet& types) { |
| 1566 data_->UpdateEnabledTypes(types); | 1549 data_->UpdateEnabledTypes(types); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1639 | 1622 |
| 1640 bool SyncManager::SyncInternal::Init( | 1623 bool SyncManager::SyncInternal::Init( |
| 1641 const FilePath& database_location, | 1624 const FilePath& database_location, |
| 1642 const std::string& sync_server_and_path, | 1625 const std::string& sync_server_and_path, |
| 1643 int port, | 1626 int port, |
| 1644 bool use_ssl, | 1627 bool use_ssl, |
| 1645 HttpPostProviderFactory* post_factory, | 1628 HttpPostProviderFactory* post_factory, |
| 1646 ModelSafeWorkerRegistrar* model_safe_worker_registrar, | 1629 ModelSafeWorkerRegistrar* model_safe_worker_registrar, |
| 1647 const char* user_agent, | 1630 const char* user_agent, |
| 1648 const SyncCredentials& credentials, | 1631 const SyncCredentials& credentials, |
| 1649 const notifier::NotifierOptions& notifier_options, | |
| 1650 const std::string& restored_key_for_bootstrapping, | 1632 const std::string& restored_key_for_bootstrapping, |
| 1651 bool setup_for_test_mode) { | 1633 bool setup_for_test_mode) { |
| 1652 | 1634 |
| 1653 VLOG(1) << "Starting SyncInternal initialization."; | 1635 VLOG(1) << "Starting SyncInternal initialization."; |
| 1654 | 1636 |
| 1655 core_message_loop_ = MessageLoop::current(); | 1637 core_message_loop_ = MessageLoop::current(); |
| 1656 DCHECK(core_message_loop_); | 1638 DCHECK(core_message_loop_); |
| 1657 notifier_options_ = notifier_options; | |
| 1658 registrar_ = model_safe_worker_registrar; | 1639 registrar_ = model_safe_worker_registrar; |
| 1659 setup_for_test_mode_ = setup_for_test_mode; | 1640 setup_for_test_mode_ = setup_for_test_mode; |
| 1660 | 1641 |
| 1642 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 1643 if (command_line.HasSwitch(switches::kSyncNotificationMethod)) { | |
| 1644 const std::string notification_method_str( | |
| 1645 command_line.GetSwitchValueASCII(switches::kSyncNotificationMethod)); | |
| 1646 // Directly using string "p2p" as we do not want to depend on jingle. | |
| 1647 if (notification_method_str == "p2p") { | |
| 1648 using_p2p_notifications_ = true; | |
| 1649 } | |
| 1650 } | |
| 1651 | |
| 1661 share_.dir_manager.reset(new DirectoryManager(database_location)); | 1652 share_.dir_manager.reset(new DirectoryManager(database_location)); |
| 1662 | 1653 |
| 1663 connection_manager_.reset(new SyncAPIServerConnectionManager( | 1654 connection_manager_.reset(new SyncAPIServerConnectionManager( |
| 1664 sync_server_and_path, port, use_ssl, user_agent, post_factory)); | 1655 sync_server_and_path, port, use_ssl, user_agent, post_factory)); |
| 1665 | 1656 |
| 1666 connection_manager_hookup_.reset( | 1657 connection_manager_hookup_.reset( |
| 1667 NewEventListenerHookup(connection_manager()->channel(), this, | 1658 NewEventListenerHookup(connection_manager()->channel(), this, |
| 1668 &SyncManager::SyncInternal::HandleServerConnectionEvent)); | 1659 &SyncManager::SyncInternal::HandleServerConnectionEvent)); |
| 1669 | 1660 |
| 1670 net::NetworkChangeNotifier::AddObserver(this); | 1661 net::NetworkChangeNotifier::AddObserver(this); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1762 } | 1753 } |
| 1763 | 1754 |
| 1764 // Notify that initialization is complete. | 1755 // Notify that initialization is complete. |
| 1765 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 1756 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1766 OnInitializationComplete()); | 1757 OnInitializationComplete()); |
| 1767 } | 1758 } |
| 1768 | 1759 |
| 1769 void SyncManager::SyncInternal::SendPendingXMPPNotification( | 1760 void SyncManager::SyncInternal::SendPendingXMPPNotification( |
| 1770 bool new_pending_notification) { | 1761 bool new_pending_notification) { |
| 1771 DCHECK_EQ(MessageLoop::current(), core_message_loop_); | 1762 DCHECK_EQ(MessageLoop::current(), core_message_loop_); |
| 1772 DCHECK_NE(notifier_options_.notification_method, | 1763 DCHECK(using_p2p_notifications_); |
| 1773 notifier::NOTIFICATION_SERVER); | |
| 1774 notification_pending_ = notification_pending_ || new_pending_notification; | 1764 notification_pending_ = notification_pending_ || new_pending_notification; |
| 1775 if (!notification_pending_) { | 1765 if (!notification_pending_) { |
| 1776 VLOG(1) << "Not sending notification: no pending notification"; | 1766 VLOG(1) << "Not sending notification: no pending notification"; |
| 1777 return; | 1767 return; |
| 1778 } | 1768 } |
| 1779 if (!talk_mediator()) { | 1769 #if defined(UNIT_TEST) |
| 1780 VLOG(1) << "Not sending notification: shutting down (talk_mediator_ is " | 1770 ((SyncNotifierImpl*) sync_notifier_)->SendNotification(); |
| 1781 "NULL)"; | 1771 #endif |
| 1782 return; | |
| 1783 } | |
| 1784 VLOG(1) << "Sending XMPP notification..."; | |
| 1785 OutgoingNotificationData notification_data; | |
| 1786 notification_data.service_id = browser_sync::kSyncServiceId; | |
| 1787 notification_data.service_url = browser_sync::kSyncServiceUrl; | |
| 1788 notification_data.send_content = true; | |
| 1789 notification_data.priority = browser_sync::kSyncPriority; | |
| 1790 notification_data.write_to_cache_only = true; | |
| 1791 notification_data.service_specific_data = | |
| 1792 browser_sync::kSyncServiceSpecificData; | |
| 1793 notification_data.require_subscription = true; | |
| 1794 bool success = talk_mediator()->SendNotification(notification_data); | |
| 1795 if (success) { | |
| 1796 notification_pending_ = false; | |
| 1797 VLOG(1) << "Sent XMPP notification"; | |
| 1798 } else { | |
| 1799 VLOG(1) << "Could not send XMPP notification"; | |
| 1800 } | |
| 1801 } | 1772 } |
| 1802 | 1773 |
| 1803 bool SyncManager::SyncInternal::OpenDirectory() { | 1774 bool SyncManager::SyncInternal::OpenDirectory() { |
| 1804 DCHECK(!initialized()) << "Should only happen once"; | 1775 DCHECK(!initialized()) << "Should only happen once"; |
| 1805 | 1776 |
| 1806 bool share_opened = dir_manager()->Open(username_for_share()); | 1777 bool share_opened = dir_manager()->Open(username_for_share()); |
| 1807 DCHECK(share_opened); | 1778 DCHECK(share_opened); |
| 1808 if (!share_opened) { | 1779 if (!share_opened) { |
| 1809 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 1780 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1810 OnStopSyncingPermanently()); | 1781 OnStopSyncingPermanently()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1843 UpdateCredentials(credentials); | 1814 UpdateCredentials(credentials); |
| 1844 } | 1815 } |
| 1845 return true; | 1816 return true; |
| 1846 } | 1817 } |
| 1847 | 1818 |
| 1848 void SyncManager::SyncInternal::UpdateCredentials( | 1819 void SyncManager::SyncInternal::UpdateCredentials( |
| 1849 const SyncCredentials& credentials) { | 1820 const SyncCredentials& credentials) { |
| 1850 DCHECK_EQ(MessageLoop::current(), core_message_loop_); | 1821 DCHECK_EQ(MessageLoop::current(), core_message_loop_); |
| 1851 DCHECK_EQ(credentials.email, share_.name); | 1822 DCHECK_EQ(credentials.email, share_.name); |
| 1852 connection_manager()->set_auth_token(credentials.sync_token); | 1823 connection_manager()->set_auth_token(credentials.sync_token); |
| 1853 TalkMediatorLogin(credentials.email, credentials.sync_token); | 1824 SyncNotifierLogin(credentials.email, credentials.sync_token); |
| 1854 CheckServerReachable(); | 1825 CheckServerReachable(); |
| 1855 sync_manager_->RequestNudge(); | 1826 sync_manager_->RequestNudge(); |
| 1856 } | 1827 } |
| 1857 | 1828 |
| 1858 void SyncManager::SyncInternal::UpdateEnabledTypes( | 1829 void SyncManager::SyncInternal::UpdateEnabledTypes( |
| 1859 const syncable::ModelTypeSet& types) { | 1830 const syncable::ModelTypeSet& types) { |
| 1860 DCHECK_EQ(MessageLoop::current(), core_message_loop_); | 1831 DCHECK_EQ(MessageLoop::current(), core_message_loop_); |
| 1861 | 1832 |
| 1862 enabled_types_ = types; | 1833 sync_notifier_->UpdateEnabledTypes(types); |
| 1863 if (server_notifier_thread_ != NULL) { | |
| 1864 server_notifier_thread_->UpdateEnabledTypes(types); | |
| 1865 } | |
| 1866 } | |
| 1867 | |
| 1868 void SyncManager::SyncInternal::InitializeTalkMediator() { | |
| 1869 if (notifier_options_.notification_method == | |
| 1870 notifier::NOTIFICATION_SERVER) { | |
| 1871 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); | |
| 1872 std::string state; | |
| 1873 if (lookup.good()) | |
| 1874 state = lookup->GetAndClearNotificationState(); | |
| 1875 else | |
| 1876 LOG(ERROR) << "Could not read notification state"; | |
| 1877 if (VLOG_IS_ON(1)) { | |
| 1878 std::string encoded_state; | |
| 1879 base::Base64Encode(state, &encoded_state); | |
| 1880 VLOG(1) << "Read notification state: " << encoded_state; | |
| 1881 } | |
| 1882 | |
| 1883 // |talk_mediator_| takes ownership of |sync_notifier_thread_| | |
| 1884 // but it is. guaranteed that |sync_notifier_thread_| is destroyed only | |
| 1885 // when |talk_mediator_| is (see the comments in talk_mediator.h). | |
| 1886 server_notifier_thread_ = new sync_notifier::ServerNotifierThread( | |
| 1887 notifier_options_, state, this); | |
| 1888 talk_mediator_.reset( | |
| 1889 new TalkMediatorImpl(server_notifier_thread_, | |
| 1890 notifier_options_.invalidate_xmpp_login, | |
| 1891 notifier_options_.allow_insecure_connection)); | |
| 1892 | |
| 1893 // Since we may be initialized more than once, make sure that any | |
| 1894 // newly created server notifier thread has the latest enabled types. | |
| 1895 server_notifier_thread_->UpdateEnabledTypes(enabled_types_); | |
| 1896 } else { | |
| 1897 notifier::MediatorThread* mediator_thread = | |
| 1898 new notifier::MediatorThreadImpl(notifier_options_); | |
| 1899 talk_mediator_.reset( | |
| 1900 new TalkMediatorImpl(mediator_thread, | |
| 1901 notifier_options_.invalidate_xmpp_login, | |
| 1902 notifier_options_.allow_insecure_connection)); | |
| 1903 talk_mediator_->AddSubscribedServiceUrl(browser_sync::kSyncServiceUrl); | |
| 1904 server_notifier_thread_ = NULL; | |
| 1905 } | |
| 1906 talk_mediator()->SetDelegate(this); | |
| 1907 } | 1834 } |
| 1908 | 1835 |
| 1909 void SyncManager::SyncInternal::RaiseAuthNeededEvent() { | 1836 void SyncManager::SyncInternal::RaiseAuthNeededEvent() { |
| 1910 FOR_EACH_OBSERVER( | 1837 FOR_EACH_OBSERVER( |
| 1911 SyncManager::Observer, observers_, | 1838 SyncManager::Observer, observers_, |
| 1912 OnAuthError(AuthError(AuthError::INVALID_GAIA_CREDENTIALS))); | 1839 OnAuthError(AuthError(AuthError::INVALID_GAIA_CREDENTIALS))); |
| 1913 } | 1840 } |
| 1914 | 1841 |
| 1915 void SyncManager::SyncInternal::SetUsingExplicitPassphrasePrefForMigration() { | 1842 void SyncManager::SyncInternal::SetUsingExplicitPassphrasePrefForMigration() { |
| 1916 WriteTransaction trans(&share_); | 1843 WriteTransaction trans(&share_); |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2114 return data_; | 2041 return data_; |
| 2115 } | 2042 } |
| 2116 | 2043 |
| 2117 void SyncManager::Shutdown() { | 2044 void SyncManager::Shutdown() { |
| 2118 data_->Shutdown(); | 2045 data_->Shutdown(); |
| 2119 } | 2046 } |
| 2120 | 2047 |
| 2121 void SyncManager::SyncInternal::Shutdown() { | 2048 void SyncManager::SyncInternal::Shutdown() { |
| 2122 method_factory_.RevokeAll(); | 2049 method_factory_.RevokeAll(); |
| 2123 | 2050 |
| 2124 // We NULL out talk_mediator_ so that any tasks pumped below do not | |
| 2125 // trigger further XMPP actions. | |
| 2126 // | |
| 2127 // TODO(akalin): NULL the other member variables defensively, too. | |
| 2128 scoped_ptr<TalkMediator> talk_mediator(talk_mediator_.release()); | |
| 2129 | |
| 2130 if (syncer_thread()) { | 2051 if (syncer_thread()) { |
| 2131 if (!syncer_thread()->Stop(kThreadExitTimeoutMsec)) { | 2052 if (!syncer_thread()->Stop(kThreadExitTimeoutMsec)) { |
| 2132 LOG(FATAL) << "Unable to stop the syncer, it won't be happy..."; | 2053 LOG(FATAL) << "Unable to stop the syncer, it won't be happy..."; |
| 2133 } | 2054 } |
| 2134 syncer_thread_ = NULL; | 2055 syncer_thread_ = NULL; |
| 2135 } | 2056 } |
| 2136 | 2057 |
| 2137 // Shutdown the xmpp buzz connection. | 2058 // Notify the notifier of shutdown. |
| 2138 if (talk_mediator.get()) { | 2059 if (sync_notifier_.get()) { |
| 2139 VLOG(1) << "P2P: Mediator logout started."; | 2060 sync_notifier_->Logout(); |
| 2140 talk_mediator->Logout(); | 2061 sync_notifier_.reset(); |
| 2141 VLOG(1) << "P2P: Mediator logout completed."; | |
| 2142 talk_mediator.reset(); | |
| 2143 | |
| 2144 // |server_notifier_thread_| is owned by |talk_mediator|. We NULL | |
| 2145 // it out here so as to not have a dangling pointer. | |
| 2146 server_notifier_thread_= NULL; | |
| 2147 VLOG(1) << "P2P: Mediator destroyed."; | |
| 2148 } | 2062 } |
| 2149 | 2063 |
| 2150 // Pump any messages the auth watcher, syncer thread, or talk | 2064 // Pump any messages the auth watcher, syncer thread, or talk |
| 2151 // mediator posted before they shut down. (See OnSyncEngineEvent(), | 2065 // mediator posted before they shut down. (See OnSyncEngineEvent(), |
| 2152 // and HandleTalkMediatorEvent() for the | 2066 // and HandleTalkMediatorEvent() for the |
| 2153 // events that may be posted.) | 2067 // events that may be posted.) |
| 2154 { | 2068 { |
| 2155 CHECK(core_message_loop_); | 2069 CHECK(core_message_loop_); |
| 2156 bool old_state = core_message_loop_->NestableTasksAllowed(); | 2070 bool old_state = core_message_loop_->NestableTasksAllowed(); |
| 2157 core_message_loop_->SetNestableTasksAllowed(true); | 2071 core_message_loop_->SetNestableTasksAllowed(true); |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2465 } | 2379 } |
| 2466 | 2380 |
| 2467 if (!initialized()) | 2381 if (!initialized()) |
| 2468 return; | 2382 return; |
| 2469 | 2383 |
| 2470 if (!event.snapshot->has_more_to_sync) { | 2384 if (!event.snapshot->has_more_to_sync) { |
| 2471 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 2385 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 2472 OnSyncCycleCompleted(event.snapshot)); | 2386 OnSyncCycleCompleted(event.snapshot)); |
| 2473 } | 2387 } |
| 2474 | 2388 |
| 2475 if (notifier_options_.notification_method != | 2389 #if defined(UNIT_TEST) |
| 2476 notifier::NOTIFICATION_SERVER) { | 2390 if (using_p2p_notifications_) { |
| 2477 // TODO(chron): Consider changing this back to track has_more_to_sync | 2391 // TODO(chron): Consider changing this back to track has_more_to_sync |
| 2478 // only notify peers if a successful commit has occurred. | 2392 // only notify peers if a successful commit has occurred. |
| 2479 bool new_pending_notification = | 2393 bool new_pending_notification = |
| 2480 (event.snapshot->syncer_status.num_successful_commits > 0); | 2394 (event.snapshot->syncer_status.num_successful_commits > 0); |
| 2481 core_message_loop_->PostTask( | 2395 core_message_loop_->PostTask( |
| 2482 FROM_HERE, | 2396 FROM_HERE, |
| 2483 NewRunnableMethod( | 2397 NewRunnableMethod( |
| 2484 this, | 2398 this, |
| 2485 &SyncManager::SyncInternal::SendPendingXMPPNotification, | 2399 &SyncManager::SyncInternal::SendPendingXMPPNotification, |
| 2486 new_pending_notification)); | 2400 new_pending_notification)); |
| 2487 } | 2401 } |
| 2402 #endif | |
| 2488 } | 2403 } |
| 2489 | 2404 |
| 2490 if (event.what_happened == SyncEngineEvent::SYNCER_THREAD_PAUSED) { | 2405 if (event.what_happened == SyncEngineEvent::SYNCER_THREAD_PAUSED) { |
| 2491 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 2406 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 2492 OnPaused()); | 2407 OnPaused()); |
| 2493 return; | 2408 return; |
| 2494 } | 2409 } |
| 2495 | 2410 |
| 2496 if (event.what_happened == SyncEngineEvent::SYNCER_THREAD_RESUMED) { | 2411 if (event.what_happened == SyncEngineEvent::SYNCER_THREAD_RESUMED) { |
| 2497 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 2412 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2624 if (syncer_thread()) { | 2539 if (syncer_thread()) { |
| 2625 syncer_thread()->SetNotificationsEnabled(notifications_enabled); | 2540 syncer_thread()->SetNotificationsEnabled(notifications_enabled); |
| 2626 } | 2541 } |
| 2627 if (parent_router_) { | 2542 if (parent_router_) { |
| 2628 ListValue args; | 2543 ListValue args; |
| 2629 args.Append(Value::CreateBooleanValue(notifications_enabled)); | 2544 args.Append(Value::CreateBooleanValue(notifications_enabled)); |
| 2630 // TODO(akalin): Tidy up grammar in event names. | 2545 // TODO(akalin): Tidy up grammar in event names. |
| 2631 parent_router_->RouteJsEvent("onSyncNotificationStateChange", | 2546 parent_router_->RouteJsEvent("onSyncNotificationStateChange", |
| 2632 browser_sync::JsArgList(args), NULL); | 2547 browser_sync::JsArgList(args), NULL); |
| 2633 } | 2548 } |
| 2634 if ((notifier_options_.notification_method != | 2549 |
| 2635 notifier::NOTIFICATION_SERVER) && notifications_enabled) { | 2550 #if defined(UNIT_TEST) |
| 2551 // P2P notifications used for integration tests. | |
| 2552 if (using_p2p_notifications_ && notifications_enabled) { | |
| 2636 // Nudge the syncer thread when notifications are enabled, in case there is | 2553 // Nudge the syncer thread when notifications are enabled, in case there is |
| 2637 // any data that has not yet been synced. If we are listening to | 2554 // any data that has not yet been synced. If we are listening to |
| 2638 // server-issued notifications, we are already guaranteed to receive a | 2555 // server-issued notifications, we are already guaranteed to receive a |
| 2639 // notification on a successful connection. | 2556 // notification on a successful connection. |
| 2640 if (syncer_thread()) { | 2557 if (syncer_thread()) { |
| 2641 syncer_thread()->NudgeSyncer(0, SyncerThread::kLocal); | 2558 syncer_thread()->NudgeSyncer(0, SyncerThread::kLocal); |
| 2642 } | 2559 } |
| 2643 | 2560 |
| 2644 // Send a notification as soon as subscriptions are on | 2561 // Send a notification as soon as subscriptions are on |
| 2645 // (see http://code.google.com/p/chromium/issues/detail?id=38563 ). | 2562 // (see http://code.google.com/p/chromium/issues/detail?id=38563 ). |
| 2646 core_message_loop_->PostTask( | 2563 core_message_loop_->PostTask( |
| 2647 FROM_HERE, | 2564 FROM_HERE, |
| 2648 NewRunnableMethod( | 2565 NewRunnableMethod( |
| 2649 this, | 2566 this, |
| 2650 &SyncManager::SyncInternal::SendPendingXMPPNotification, | 2567 &SyncManager::SyncInternal::SendPendingXMPPNotification, |
| 2651 true)); | 2568 true)); |
| 2652 } | 2569 } |
| 2570 #endif | |
| 2653 } | 2571 } |
| 2654 | 2572 |
| 2655 void SyncManager::SyncInternal::TalkMediatorLogin( | 2573 void SyncManager::SyncInternal::SyncNotifierLogin( |
| 2656 const std::string& email, const std::string& token) { | 2574 const std::string& email, const std::string& token) { |
| 2657 DCHECK_EQ(MessageLoop::current(), core_message_loop_); | 2575 DCHECK_EQ(MessageLoop::current(), core_message_loop_); |
| 2658 DCHECK(!email.empty()); | 2576 DCHECK(!email.empty()); |
| 2659 DCHECK(!token.empty()); | 2577 DCHECK(!token.empty()); |
| 2660 InitializeTalkMediator(); | 2578 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); |
| 2661 talk_mediator()->SetAuthToken(email, token, SYNC_SERVICE_NAME); | 2579 std::string state; |
| 2662 talk_mediator()->Login(); | 2580 if (lookup.good()) |
| 2581 state = lookup->GetAndClearNotificationState(); | |
| 2582 else | |
| 2583 LOG(ERROR) << "Could not read notification state"; | |
| 2584 if (VLOG_IS_ON(1)) { | |
| 2585 std::string encoded_state; | |
| 2586 base::Base64Encode(state, &encoded_state); | |
| 2587 VLOG(1) << "Read notification state: " << encoded_state; | |
| 2588 } | |
| 2589 | |
| 2590 scoped_ptr<SyncNotifierFactory> sync_notifier_factory( | |
|
akalin
2011/03/08 02:48:30
no need for this to be a scoped_ptr, just create i
Agrawal
2011/03/08 23:07:28
Done.
| |
| 2591 new SyncNotifierFactory()); | |
| 2592 sync_notifier_.reset(sync_notifier_factory->CreateSyncNotifier()); | |
| 2593 sync_notifier_->Login(email, token, state, this); | |
| 2663 } | 2594 } |
| 2664 | 2595 |
| 2665 void SyncManager::SyncInternal::OnIncomingNotification( | 2596 void SyncManager::SyncInternal::OnIncomingNotification( |
| 2666 const IncomingNotificationData& notification_data) { | 2597 const IncomingNotificationData& notification_data) { |
| 2667 browser_sync::sessions::TypePayloadMap model_types_with_payloads; | 2598 browser_sync::sessions::TypePayloadMap model_types_with_payloads; |
| 2668 | 2599 |
| 2669 // Check if the service url is a sync URL. An empty service URL is | 2600 // Check if the service url is a sync URL. An empty service URL is |
| 2670 // treated as a legacy sync notification. If we're listening to | 2601 // treated as a legacy sync notification. If we're listening to |
| 2671 // server-issued notifications, no need to check the service_url. | 2602 // server-issued notifications, no need to check the service_url. |
| 2672 if (notifier_options_.notification_method == | 2603 if (!using_p2p_notifications_) { |
| 2673 notifier::NOTIFICATION_SERVER) { | |
| 2674 VLOG(1) << "Sync received server notification from " << | 2604 VLOG(1) << "Sync received server notification from " << |
| 2675 notification_data.service_url << ": " << | 2605 notification_data.service_url << ": " << |
| 2676 notification_data.service_specific_data; | 2606 notification_data.service_specific_data; |
| 2677 syncable::ModelTypeBitSet model_types; | 2607 syncable::ModelTypeBitSet model_types; |
| 2678 const std::string& model_type_list = notification_data.service_url; | 2608 const std::string& model_type_list = notification_data.service_url; |
| 2679 const std::string& notification_payload = | 2609 const std::string& notification_payload = |
| 2680 notification_data.service_specific_data; | 2610 notification_data.service_specific_data; |
| 2681 | 2611 |
| 2682 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) { | 2612 if (!syncable::ModelTypeBitSetFromString(model_type_list, &model_types)) { |
| 2683 LOG(DFATAL) << "Could not extract model types from server data."; | 2613 LOG(DFATAL) << "Could not extract model types from server data."; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2728 const std::string& model_type_str = | 2658 const std::string& model_type_str = |
| 2729 syncable::ModelTypeToString(it->first); | 2659 syncable::ModelTypeToString(it->first); |
| 2730 changed_types->Append(Value::CreateStringValue(model_type_str)); | 2660 changed_types->Append(Value::CreateStringValue(model_type_str)); |
| 2731 } | 2661 } |
| 2732 parent_router_->RouteJsEvent("onSyncIncomingNotification", | 2662 parent_router_->RouteJsEvent("onSyncIncomingNotification", |
| 2733 browser_sync::JsArgList(args), NULL); | 2663 browser_sync::JsArgList(args), NULL); |
| 2734 } | 2664 } |
| 2735 } | 2665 } |
| 2736 | 2666 |
| 2737 void SyncManager::SyncInternal::OnOutgoingNotification() { | 2667 void SyncManager::SyncInternal::OnOutgoingNotification() { |
| 2738 DCHECK_NE(notifier_options_.notification_method, | 2668 DCHECK(using_p2p_notifications_); |
| 2739 notifier::NOTIFICATION_SERVER); | |
| 2740 allstatus_.IncrementNotificationsSent(); | 2669 allstatus_.IncrementNotificationsSent(); |
| 2741 } | 2670 } |
| 2742 | 2671 |
| 2743 void SyncManager::SyncInternal::WriteState(const std::string& state) { | 2672 void SyncManager::SyncInternal::StoreCookie( |
| 2673 const std::string& state) { | |
| 2744 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); | 2674 syncable::ScopedDirLookup lookup(dir_manager(), username_for_share()); |
| 2745 if (!lookup.good()) { | 2675 if (!lookup.good()) { |
| 2746 LOG(ERROR) << "Could not write notification state"; | 2676 LOG(ERROR) << "Could not write notification state"; |
| 2747 // TODO(akalin): Propagate result callback all the way to this | 2677 // TODO(akalin): Propagate result callback all the way to this |
| 2748 // function and call it with "false" to signal failure. | 2678 // function and call it with "false" to signal failure. |
| 2749 return; | 2679 return; |
| 2750 } | 2680 } |
| 2751 if (VLOG_IS_ON(1)) { | 2681 if (VLOG_IS_ON(1)) { |
| 2752 std::string encoded_state; | 2682 std::string encoded_state; |
| 2753 base::Base64Encode(state, &encoded_state); | 2683 base::Base64Encode(state, &encoded_state); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2822 | 2752 |
| 2823 void SyncManager::TriggerOnIncomingNotificationForTest( | 2753 void SyncManager::TriggerOnIncomingNotificationForTest( |
| 2824 const syncable::ModelTypeBitSet& model_types) { | 2754 const syncable::ModelTypeBitSet& model_types) { |
| 2825 IncomingNotificationData notification_data; | 2755 IncomingNotificationData notification_data; |
| 2826 notification_data.service_url = model_types.to_string(); | 2756 notification_data.service_url = model_types.to_string(); |
| 2827 // Here we rely on the default notification method being SERVER. | 2757 // Here we rely on the default notification method being SERVER. |
| 2828 data_->OnIncomingNotification(notification_data); | 2758 data_->OnIncomingNotification(notification_data); |
| 2829 } | 2759 } |
| 2830 | 2760 |
| 2831 } // namespace sync_api | 2761 } // namespace sync_api |
| OLD | NEW |