| 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 "chrome/browser/sync/internal_api/sync_manager.h" | 5 #include "chrome/browser/sync/internal_api/sync_manager.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 HttpPostProviderFactory* post_factory, | 190 HttpPostProviderFactory* post_factory, |
| 191 ModelSafeWorkerRegistrar* model_safe_worker_registrar, | 191 ModelSafeWorkerRegistrar* model_safe_worker_registrar, |
| 192 ChangeDelegate* change_delegate, | 192 ChangeDelegate* change_delegate, |
| 193 const std::string& user_agent, | 193 const std::string& user_agent, |
| 194 const SyncCredentials& credentials, | 194 const SyncCredentials& credentials, |
| 195 sync_notifier::SyncNotifier* sync_notifier, | 195 sync_notifier::SyncNotifier* sync_notifier, |
| 196 const std::string& restored_key_for_bootstrapping, | 196 const std::string& restored_key_for_bootstrapping, |
| 197 bool setup_for_test_mode, | 197 bool setup_for_test_mode, |
| 198 UnrecoverableErrorHandler* unrecoverable_error_handler); | 198 UnrecoverableErrorHandler* unrecoverable_error_handler); |
| 199 | 199 |
| 200 void CheckServerReachable() { | |
| 201 if (connection_manager()) { | |
| 202 connection_manager()->CheckServerReachable(); | |
| 203 } else { | |
| 204 NOTREACHED() << "Should be valid connection manager!"; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 // Sign into sync with given credentials. | 200 // Sign into sync with given credentials. |
| 209 // We do not verify the tokens given. After this call, the tokens are set | 201 // We do not verify the tokens given. After this call, the tokens are set |
| 210 // and the sync DB is open. True if successful, false if something | 202 // and the sync DB is open. True if successful, false if something |
| 211 // went wrong. | 203 // went wrong. |
| 212 bool SignIn(const SyncCredentials& credentials); | 204 bool SignIn(const SyncCredentials& credentials); |
| 213 | 205 |
| 214 // Update tokens that we're using in Sync. Email must stay the same. | 206 // Update tokens that we're using in Sync. Email must stay the same. |
| 215 void UpdateCredentials(const SyncCredentials& credentials); | 207 void UpdateCredentials(const SyncCredentials& credentials); |
| 216 | 208 |
| 209 void KickScheduler(); |
| 210 |
| 217 // Called when the user disables or enables a sync type. | 211 // Called when the user disables or enables a sync type. |
| 218 void UpdateEnabledTypes(); | 212 void UpdateEnabledTypes(); |
| 219 | 213 |
| 220 // Conditionally sets the flag in the Nigori node which instructs other | 214 // Conditionally sets the flag in the Nigori node which instructs other |
| 221 // clients to start syncing tabs. | 215 // clients to start syncing tabs. |
| 222 void MaybeSetSyncTabsInNigoriNode(ModelTypeSet enabled_types); | 216 void MaybeSetSyncTabsInNigoriNode(ModelTypeSet enabled_types); |
| 223 | 217 |
| 224 // Tell the sync engine to start the syncing process. | 218 // Tell the sync engine to start the syncing process. |
| 225 void StartSyncingNormally(); | 219 void StartSyncingNormally(); |
| 226 | 220 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 return value; | 374 return value; |
| 381 } | 375 } |
| 382 }; | 376 }; |
| 383 | 377 |
| 384 typedef std::map<syncable::ModelType, NotificationInfo> NotificationInfoMap; | 378 typedef std::map<syncable::ModelType, NotificationInfo> NotificationInfoMap; |
| 385 typedef JsArgList | 379 typedef JsArgList |
| 386 (SyncManager::SyncInternal::*UnboundJsMessageHandler)(const JsArgList&); | 380 (SyncManager::SyncInternal::*UnboundJsMessageHandler)(const JsArgList&); |
| 387 typedef base::Callback<JsArgList(const JsArgList&)> JsMessageHandler; | 381 typedef base::Callback<JsArgList(const JsArgList&)> JsMessageHandler; |
| 388 typedef std::map<std::string, JsMessageHandler> JsMessageHandlerMap; | 382 typedef std::map<std::string, JsMessageHandler> JsMessageHandlerMap; |
| 389 | 383 |
| 390 // Helper to call OnAuthError when no authentication credentials are | |
| 391 // available. | |
| 392 void RaiseAuthNeededEvent(); | |
| 393 | |
| 394 // Internal callback of UpdateCryptographerAndNigoriCallback. | 384 // Internal callback of UpdateCryptographerAndNigoriCallback. |
| 395 void UpdateCryptographerAndNigoriCallback( | 385 void UpdateCryptographerAndNigoriCallback( |
| 396 const base::Callback<void(bool)>& done_callback, | 386 const base::Callback<void(bool)>& done_callback, |
| 397 const std::string& session_name); | 387 const std::string& session_name); |
| 398 | 388 |
| 399 // Determine if the parents or predecessors differ between the old and new | 389 // Determine if the parents or predecessors differ between the old and new |
| 400 // versions of an entry stored in |a| and |b|. Note that a node's index may | 390 // versions of an entry stored in |a| and |b|. Note that a node's index may |
| 401 // change without its NEXT_ID changing if the node at NEXT_ID also moved (but | 391 // change without its NEXT_ID changing if the node at NEXT_ID also moved (but |
| 402 // the relative order is unchanged). To handle such cases, we rely on the | 392 // the relative order is unchanged). To handle such cases, we rely on the |
| 403 // caller to treat a position update on any sibling as updating the positions | 393 // caller to treat a position update on any sibling as updating the positions |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 | 642 |
| 653 SyncManager::ChangeObserver::~ChangeObserver() {} | 643 SyncManager::ChangeObserver::~ChangeObserver() {} |
| 654 | 644 |
| 655 SyncManager::Observer::~Observer() {} | 645 SyncManager::Observer::~Observer() {} |
| 656 | 646 |
| 657 SyncManager::SyncManager(const std::string& name) | 647 SyncManager::SyncManager(const std::string& name) |
| 658 : data_(new SyncInternal(name)) {} | 648 : data_(new SyncInternal(name)) {} |
| 659 | 649 |
| 660 SyncManager::Status::Status() | 650 SyncManager::Status::Status() |
| 661 : summary(INVALID), | 651 : summary(INVALID), |
| 662 authenticated(false), | |
| 663 server_up(false), | |
| 664 server_reachable(false), | |
| 665 server_broken(false), | 652 server_broken(false), |
| 666 notifications_enabled(false), | 653 notifications_enabled(false), |
| 667 notifications_received(0), | 654 notifications_received(0), |
| 668 notifiable_commits(0), | 655 notifiable_commits(0), |
| 669 max_consecutive_errors(0), | 656 max_consecutive_errors(0), |
| 670 unsynced_count(0), | 657 unsynced_count(0), |
| 671 conflicting_count(0), | 658 conflicting_count(0), |
| 672 syncing(false), | 659 syncing(false), |
| 673 initial_sync_ended(false), | 660 initial_sync_ended(false), |
| 674 syncer_stuck(false), | 661 syncer_stuck(false), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 registrar, | 704 registrar, |
| 718 change_delegate, | 705 change_delegate, |
| 719 user_agent, | 706 user_agent, |
| 720 credentials, | 707 credentials, |
| 721 sync_notifier, | 708 sync_notifier, |
| 722 restored_key_for_bootstrapping, | 709 restored_key_for_bootstrapping, |
| 723 setup_for_test_mode, | 710 setup_for_test_mode, |
| 724 unrecoverable_error_handler); | 711 unrecoverable_error_handler); |
| 725 } | 712 } |
| 726 | 713 |
| 727 void SyncManager::CheckServerReachable() { | |
| 728 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 729 data_->CheckServerReachable(); | |
| 730 } | |
| 731 | |
| 732 void SyncManager::UpdateCredentials(const SyncCredentials& credentials) { | 714 void SyncManager::UpdateCredentials(const SyncCredentials& credentials) { |
| 733 DCHECK(thread_checker_.CalledOnValidThread()); | 715 DCHECK(thread_checker_.CalledOnValidThread()); |
| 734 data_->UpdateCredentials(credentials); | 716 data_->UpdateCredentials(credentials); |
| 735 } | 717 } |
| 736 | 718 |
| 737 void SyncManager::UpdateEnabledTypes() { | 719 void SyncManager::UpdateEnabledTypes() { |
| 738 DCHECK(thread_checker_.CalledOnValidThread()); | 720 DCHECK(thread_checker_.CalledOnValidThread()); |
| 739 data_->UpdateEnabledTypes(); | 721 data_->UpdateEnabledTypes(); |
| 740 } | 722 } |
| 741 | 723 |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 DCHECK(thread_checker_.CalledOnValidThread()); | 1112 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1131 DCHECK_EQ(credentials.email, share_.name); | 1113 DCHECK_EQ(credentials.email, share_.name); |
| 1132 DCHECK(!credentials.email.empty()); | 1114 DCHECK(!credentials.email.empty()); |
| 1133 DCHECK(!credentials.sync_token.empty()); | 1115 DCHECK(!credentials.sync_token.empty()); |
| 1134 | 1116 |
| 1135 observing_ip_address_changes_ = true; | 1117 observing_ip_address_changes_ = true; |
| 1136 if (connection_manager()->set_auth_token(credentials.sync_token)) { | 1118 if (connection_manager()->set_auth_token(credentials.sync_token)) { |
| 1137 sync_notifier_->UpdateCredentials( | 1119 sync_notifier_->UpdateCredentials( |
| 1138 credentials.email, credentials.sync_token); | 1120 credentials.email, credentials.sync_token); |
| 1139 if (!setup_for_test_mode_ && initialized_) { | 1121 if (!setup_for_test_mode_ && initialized_) { |
| 1140 // Post a task so we don't block UpdateCredentials. | 1122 // Kick the sync scheduler. It may have been waiting for that auth token. |
| 1141 MessageLoop::current()->PostTask( | 1123 MessageLoop::current()->PostTask( |
| 1142 FROM_HERE, base::Bind(&SyncInternal::CheckServerReachable, | 1124 FROM_HERE, base::Bind(&SyncInternal::KickScheduler, |
| 1143 weak_ptr_factory_.GetWeakPtr())); | 1125 weak_ptr_factory_.GetWeakPtr())); |
| 1144 } | 1126 } |
| 1145 } | 1127 } |
| 1146 } | 1128 } |
| 1147 | 1129 |
| 1130 void SyncManager::SyncInternal::KickScheduler() { |
| 1131 scheduler()->TryToConnect(); |
| 1132 } |
| 1133 |
| 1148 void SyncManager::SyncInternal::UpdateEnabledTypes() { | 1134 void SyncManager::SyncInternal::UpdateEnabledTypes() { |
| 1149 DCHECK(thread_checker_.CalledOnValidThread()); | 1135 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1150 ModelSafeRoutingInfo routes; | 1136 ModelSafeRoutingInfo routes; |
| 1151 registrar_->GetModelSafeRoutingInfo(&routes); | 1137 registrar_->GetModelSafeRoutingInfo(&routes); |
| 1152 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); | 1138 const ModelTypeSet enabled_types = GetRoutingInfoTypes(routes); |
| 1153 sync_notifier_->UpdateEnabledTypes(enabled_types); | 1139 sync_notifier_->UpdateEnabledTypes(enabled_types); |
| 1154 if (CommandLine::ForCurrentProcess()->HasSwitch( | 1140 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 1155 switches::kEnableSyncTabsForOtherClients)) { | 1141 switches::kEnableSyncTabsForOtherClients)) { |
| 1156 MaybeSetSyncTabsInNigoriNode(enabled_types); | 1142 MaybeSetSyncTabsInNigoriNode(enabled_types); |
| 1157 } | 1143 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1170 << "found."; | 1156 << "found."; |
| 1171 return; | 1157 return; |
| 1172 } | 1158 } |
| 1173 | 1159 |
| 1174 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics()); | 1160 sync_pb::NigoriSpecifics specifics(node.GetNigoriSpecifics()); |
| 1175 specifics.set_sync_tabs(true); | 1161 specifics.set_sync_tabs(true); |
| 1176 node.SetNigoriSpecifics(specifics); | 1162 node.SetNigoriSpecifics(specifics); |
| 1177 } | 1163 } |
| 1178 } | 1164 } |
| 1179 | 1165 |
| 1180 void SyncManager::SyncInternal::RaiseAuthNeededEvent() { | |
| 1181 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | |
| 1182 OnAuthError(AuthError(AuthError::INVALID_GAIA_CREDENTIALS))); | |
| 1183 } | |
| 1184 | |
| 1185 void SyncManager::SyncInternal::SetPassphrase( | 1166 void SyncManager::SyncInternal::SetPassphrase( |
| 1186 const std::string& passphrase, bool is_explicit) { | 1167 const std::string& passphrase, bool is_explicit) { |
| 1187 // We do not accept empty passphrases. | 1168 // We do not accept empty passphrases. |
| 1188 if (passphrase.empty()) { | 1169 if (passphrase.empty()) { |
| 1189 DVLOG(1) << "Rejecting empty passphrase."; | 1170 DVLOG(1) << "Rejecting empty passphrase."; |
| 1190 WriteTransaction trans(FROM_HERE, GetUserShare()); | 1171 WriteTransaction trans(FROM_HERE, GetUserShare()); |
| 1191 Cryptographer* cryptographer = trans.GetCryptographer(); | 1172 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 1192 sync_pb::EncryptedData pending_keys; | 1173 sync_pb::EncryptedData pending_keys; |
| 1193 if (cryptographer->has_pending_keys()) | 1174 if (cryptographer->has_pending_keys()) |
| 1194 pending_keys = cryptographer->GetPendingKeys(); | 1175 pending_keys = cryptographer->GetPendingKeys(); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1515 base::Bind(&SyncInternal::OnIPAddressChangedImpl, | 1496 base::Bind(&SyncInternal::OnIPAddressChangedImpl, |
| 1516 weak_ptr_factory_.GetWeakPtr()), | 1497 weak_ptr_factory_.GetWeakPtr()), |
| 1517 kChromeOSNetworkChangeReactionDelayHackMsec); | 1498 kChromeOSNetworkChangeReactionDelayHackMsec); |
| 1518 #else | 1499 #else |
| 1519 OnIPAddressChangedImpl(); | 1500 OnIPAddressChangedImpl(); |
| 1520 #endif // defined(OS_CHROMEOS) | 1501 #endif // defined(OS_CHROMEOS) |
| 1521 } | 1502 } |
| 1522 | 1503 |
| 1523 void SyncManager::SyncInternal::OnIPAddressChangedImpl() { | 1504 void SyncManager::SyncInternal::OnIPAddressChangedImpl() { |
| 1524 DCHECK(thread_checker_.CalledOnValidThread()); | 1505 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1525 CheckServerReachable(); | 1506 scheduler()->TryToConnect(); |
| 1526 } | 1507 } |
| 1527 | 1508 |
| 1528 void SyncManager::SyncInternal::OnServerConnectionEvent( | 1509 void SyncManager::SyncInternal::OnServerConnectionEvent( |
| 1529 const ServerConnectionEvent& event) { | 1510 const ServerConnectionEvent& event) { |
| 1530 DCHECK(thread_checker_.CalledOnValidThread()); | 1511 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1531 allstatus_.HandleServerConnectionEvent(event); | |
| 1532 if (event.connection_code == | 1512 if (event.connection_code == |
| 1533 browser_sync::HttpResponse::SERVER_CONNECTION_OK) { | 1513 browser_sync::HttpResponse::SERVER_CONNECTION_OK) { |
| 1534 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 1514 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1535 OnAuthError(AuthError::None())); | 1515 OnAuthError(AuthError::None())); |
| 1536 } | 1516 } |
| 1537 | 1517 |
| 1538 if (event.connection_code == browser_sync::HttpResponse::SYNC_AUTH_ERROR) { | 1518 if (event.connection_code == browser_sync::HttpResponse::SYNC_AUTH_ERROR) { |
| 1539 observing_ip_address_changes_ = false; | 1519 observing_ip_address_changes_ = false; |
| 1540 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 1520 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
| 1541 OnAuthError(AuthError(AuthError::INVALID_GAIA_CREDENTIALS))); | 1521 OnAuthError(AuthError(AuthError::INVALID_GAIA_CREDENTIALS))); |
| (...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2288 lookup->GetDownloadProgress(i.Get(), &marker); | 2268 lookup->GetDownloadProgress(i.Get(), &marker); |
| 2289 | 2269 |
| 2290 if (marker.token().empty()) | 2270 if (marker.token().empty()) |
| 2291 result.Put(i.Get()); | 2271 result.Put(i.Get()); |
| 2292 | 2272 |
| 2293 } | 2273 } |
| 2294 return result; | 2274 return result; |
| 2295 } | 2275 } |
| 2296 | 2276 |
| 2297 } // namespace sync_api | 2277 } // namespace sync_api |
| OLD | NEW |