| 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "chrome/browser/sync/glue/sync_backend_host.h" | 7 #include "chrome/browser/sync/glue/sync_backend_host.h" |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const FilePath& sync_data_folder_path, | 79 const FilePath& sync_data_folder_path, |
| 80 const base::WeakPtr<SyncBackendHost>& backend); | 80 const base::WeakPtr<SyncBackendHost>& backend); |
| 81 | 81 |
| 82 // SyncManager::Observer implementation. The Core just acts like an air | 82 // SyncManager::Observer implementation. The Core just acts like an air |
| 83 // traffic controller here, forwarding incoming messages to appropriate | 83 // traffic controller here, forwarding incoming messages to appropriate |
| 84 // landing threads. | 84 // landing threads. |
| 85 virtual void OnSyncCycleCompleted( | 85 virtual void OnSyncCycleCompleted( |
| 86 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; | 86 const sessions::SyncSessionSnapshot& snapshot) OVERRIDE; |
| 87 virtual void OnInitializationComplete( | 87 virtual void OnInitializationComplete( |
| 88 const WeakHandle<JsBackend>& js_backend, | 88 const WeakHandle<JsBackend>& js_backend, |
| 89 bool success) OVERRIDE; | 89 bool success, |
| 90 syncable::ModelTypeSet restored_types) OVERRIDE; |
| 90 virtual void OnConnectionStatusChange( | 91 virtual void OnConnectionStatusChange( |
| 91 sync_api::ConnectionStatus status) OVERRIDE; | 92 sync_api::ConnectionStatus status) OVERRIDE; |
| 92 virtual void OnPassphraseRequired( | 93 virtual void OnPassphraseRequired( |
| 93 sync_api::PassphraseRequiredReason reason, | 94 sync_api::PassphraseRequiredReason reason, |
| 94 const sync_pb::EncryptedData& pending_keys) OVERRIDE; | 95 const sync_pb::EncryptedData& pending_keys) OVERRIDE; |
| 95 virtual void OnPassphraseAccepted() OVERRIDE; | 96 virtual void OnPassphraseAccepted() OVERRIDE; |
| 96 virtual void OnBootstrapTokenUpdated( | 97 virtual void OnBootstrapTokenUpdated( |
| 97 const std::string& bootstrap_token) OVERRIDE; | 98 const std::string& bootstrap_token) OVERRIDE; |
| 98 virtual void OnStopSyncingPermanently() OVERRIDE; | 99 virtual void OnStopSyncingPermanently() OVERRIDE; |
| 99 virtual void OnUpdatedToken(const std::string& token) OVERRIDE; | 100 virtual void OnUpdatedToken(const std::string& token) OVERRIDE; |
| (...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 const scoped_refptr<net::URLRequestContextGetter>& getter) { | 333 const scoped_refptr<net::URLRequestContextGetter>& getter) { |
| 333 return new HttpBridgeFactory(getter); | 334 return new HttpBridgeFactory(getter); |
| 334 } | 335 } |
| 335 | 336 |
| 336 } // namespace | 337 } // namespace |
| 337 | 338 |
| 338 void SyncBackendHost::Initialize( | 339 void SyncBackendHost::Initialize( |
| 339 SyncFrontend* frontend, | 340 SyncFrontend* frontend, |
| 340 const WeakHandle<JsEventHandler>& event_handler, | 341 const WeakHandle<JsEventHandler>& event_handler, |
| 341 const GURL& sync_service_url, | 342 const GURL& sync_service_url, |
| 342 syncable::ModelTypeSet initial_types, | |
| 343 const SyncCredentials& credentials, | 343 const SyncCredentials& credentials, |
| 344 bool delete_sync_data_folder, | 344 bool delete_sync_data_folder, |
| 345 UnrecoverableErrorHandler* unrecoverable_error_handler, | 345 UnrecoverableErrorHandler* unrecoverable_error_handler, |
| 346 ReportUnrecoverableErrorFunction report_unrecoverable_error_function) { | 346 ReportUnrecoverableErrorFunction report_unrecoverable_error_function) { |
| 347 if (!sync_thread_.Start()) | 347 if (!sync_thread_.Start()) |
| 348 return; | 348 return; |
| 349 | 349 |
| 350 frontend_ = frontend; | 350 frontend_ = frontend; |
| 351 DCHECK(frontend); | 351 DCHECK(frontend); |
| 352 | 352 |
| 353 syncable::ModelTypeSet initial_types_with_nigori(initial_types); | 353 registrar_.reset(new SyncBackendRegistrar(name_, |
| 354 CHECK(sync_prefs_.get()); | |
| 355 if (sync_prefs_->HasSyncSetupCompleted()) { | |
| 356 initial_types_with_nigori.Put(syncable::NIGORI); | |
| 357 } | |
| 358 | |
| 359 registrar_.reset(new SyncBackendRegistrar(initial_types_with_nigori, | |
| 360 name_, | |
| 361 profile_, | 354 profile_, |
| 362 sync_thread_.message_loop())); | 355 sync_thread_.message_loop())); |
| 363 ModelSafeRoutingInfo routing_info; | 356 ModelSafeRoutingInfo routing_info; |
| 364 std::vector<ModelSafeWorker*> workers; | 357 std::vector<ModelSafeWorker*> workers; |
| 365 registrar_->GetModelSafeRoutingInfo(&routing_info); | 358 registrar_->GetModelSafeRoutingInfo(&routing_info); |
| 366 registrar_->GetWorkers(&workers); | 359 registrar_->GetWorkers(&workers); |
| 367 | 360 |
| 368 initialization_state_ = CREATING_SYNC_MANAGER; | 361 initialization_state_ = CREATING_SYNC_MANAGER; |
| 369 InitCore(DoInitializeOptions( | 362 InitCore(DoInitializeOptions( |
| 370 sync_thread_.message_loop(), | 363 sync_thread_.message_loop(), |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 762 // | 755 // |
| 763 // In the case of additions, on the next sync cycle, the syncer should | 756 // In the case of additions, on the next sync cycle, the syncer should |
| 764 // notice that the routing info has changed and start the process of | 757 // notice that the routing info has changed and start the process of |
| 765 // downloading updates for newly added data types. Once this is | 758 // downloading updates for newly added data types. Once this is |
| 766 // complete, the configure_state_.ready_task_ is run via an | 759 // complete, the configure_state_.ready_task_ is run via an |
| 767 // OnInitializationComplete notification. | 760 // OnInitializationComplete notification. |
| 768 | 761 |
| 769 SDVLOG(1) << "Syncer in config mode. SBH executing " | 762 SDVLOG(1) << "Syncer in config mode. SBH executing " |
| 770 << "FinishConfigureDataTypesOnFrontendLoop"; | 763 << "FinishConfigureDataTypesOnFrontendLoop"; |
| 771 | 764 |
| 772 | |
| 773 ModelSafeRoutingInfo routing_info; | 765 ModelSafeRoutingInfo routing_info; |
| 774 registrar_->GetModelSafeRoutingInfo(&routing_info); | 766 registrar_->GetModelSafeRoutingInfo(&routing_info); |
| 775 const syncable::ModelTypeSet enabled_types = | 767 const syncable::ModelTypeSet enabled_types = |
| 776 GetRoutingInfoTypes(routing_info); | 768 GetRoutingInfoTypes(routing_info); |
| 777 | 769 |
| 778 // Update |chrome_sync_notification_bridge_|'s enabled types here as it has | 770 // Update |chrome_sync_notification_bridge_|'s enabled types here as it has |
| 779 // to happen on the UI thread. | 771 // to happen on the UI thread. |
| 780 chrome_sync_notification_bridge_.UpdateEnabledTypes(enabled_types); | 772 chrome_sync_notification_bridge_.UpdateEnabledTypes(enabled_types); |
| 781 | 773 |
| 782 if (pending_config_mode_state_->added_types.Empty() && | |
| 783 !core_->sync_manager()->InitialSyncEndedTypes().HasAll(enabled_types)) { | |
| 784 | |
| 785 // TODO(tim): Log / UMA / count this somehow? | |
| 786 // Add only the types with empty progress markers. Note: it is possible | |
| 787 // that some types have their initial_sync_ended be false but with non | |
| 788 // empty progress marker. Which is ok as the rest of the changes would | |
| 789 // be downloaded on a regular nudge and initial_sync_ended should be set | |
| 790 // to true. However this is a very corner case. So it is not explicitly | |
| 791 // handled. | |
| 792 pending_config_mode_state_->added_types = | |
| 793 sync_api::GetTypesWithEmptyProgressMarkerToken(enabled_types, | |
| 794 GetUserShare()); | |
| 795 } | |
| 796 | |
| 797 // If we've added types, we always want to request a nudge/config (even if | 774 // If we've added types, we always want to request a nudge/config (even if |
| 798 // the initial sync is ended), in case we could not decrypt the data. | 775 // the initial sync is ended), in case we could not decrypt the data. |
| 799 if (pending_config_mode_state_->added_types.Empty()) { | 776 if (pending_config_mode_state_->added_types.Empty()) { |
| 800 SDVLOG(1) << "No new types added; calling ready_task directly"; | 777 SDVLOG(1) << "No new types added; calling ready_task directly"; |
| 801 // No new types - just notify the caller that the types are available. | 778 // No new types - just notify the caller that the types are available. |
| 802 const syncable::ModelTypeSet failed_configuration_types; | 779 const syncable::ModelTypeSet failed_configuration_types; |
| 803 pending_config_mode_state_->ready_task.Run(failed_configuration_types); | 780 pending_config_mode_state_->ready_task.Run(failed_configuration_types); |
| 804 } else { | 781 } else { |
| 805 pending_download_state_.reset(pending_config_mode_state_.release()); | 782 pending_download_state_.reset(pending_config_mode_state_.release()); |
| 806 | 783 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 829 } | 806 } |
| 830 | 807 |
| 831 pending_config_mode_state_.reset(); | 808 pending_config_mode_state_.reset(); |
| 832 | 809 |
| 833 // Notify SyncManager (especially the notification listener) about new types. | 810 // Notify SyncManager (especially the notification listener) about new types. |
| 834 sync_thread_.message_loop()->PostTask(FROM_HERE, | 811 sync_thread_.message_loop()->PostTask(FROM_HERE, |
| 835 base::Bind(&SyncBackendHost::Core::DoUpdateEnabledTypes, core_.get(), | 812 base::Bind(&SyncBackendHost::Core::DoUpdateEnabledTypes, core_.get(), |
| 836 enabled_types)); | 813 enabled_types)); |
| 837 } | 814 } |
| 838 | 815 |
| 839 bool SyncBackendHost::IsDownloadingNigoriForTest() const { | |
| 840 return initialization_state_ == DOWNLOADING_NIGORI; | |
| 841 } | |
| 842 | |
| 843 SyncBackendHost::DoInitializeOptions::DoInitializeOptions( | 816 SyncBackendHost::DoInitializeOptions::DoInitializeOptions( |
| 844 MessageLoop* sync_loop, | 817 MessageLoop* sync_loop, |
| 845 SyncBackendRegistrar* registrar, | 818 SyncBackendRegistrar* registrar, |
| 846 const ModelSafeRoutingInfo& routing_info, | 819 const ModelSafeRoutingInfo& routing_info, |
| 847 const std::vector<ModelSafeWorker*>& workers, | 820 const std::vector<ModelSafeWorker*>& workers, |
| 848 ExtensionsActivityMonitor* extensions_activity_monitor, | 821 ExtensionsActivityMonitor* extensions_activity_monitor, |
| 849 const WeakHandle<JsEventHandler>& event_handler, | 822 const WeakHandle<JsEventHandler>& event_handler, |
| 850 const GURL& service_url, | 823 const GURL& service_url, |
| 851 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn, | 824 MakeHttpBridgeFactoryFn make_http_bridge_factory_fn, |
| 852 const sync_api::SyncCredentials& credentials, | 825 const sync_api::SyncCredentials& credentials, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 882 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
| 910 host_.Call( | 883 host_.Call( |
| 911 FROM_HERE, | 884 FROM_HERE, |
| 912 &SyncBackendHost::HandleSyncCycleCompletedOnFrontendLoop, | 885 &SyncBackendHost::HandleSyncCycleCompletedOnFrontendLoop, |
| 913 snapshot); | 886 snapshot); |
| 914 } | 887 } |
| 915 | 888 |
| 916 | 889 |
| 917 void SyncBackendHost::Core::OnInitializationComplete( | 890 void SyncBackendHost::Core::OnInitializationComplete( |
| 918 const WeakHandle<JsBackend>& js_backend, | 891 const WeakHandle<JsBackend>& js_backend, |
| 919 bool success) { | 892 bool success, |
| 893 syncable::ModelTypeSet restored_types) { |
| 920 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 894 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
| 921 host_.Call( | 895 host_.Call( |
| 922 FROM_HERE, | 896 FROM_HERE, |
| 923 &SyncBackendHost::HandleInitializationCompletedOnFrontendLoop, | 897 &SyncBackendHost::HandleSyncManagerInitializationOnFrontendLoop, |
| 924 js_backend, success); | 898 js_backend, success, restored_types); |
| 925 | 899 |
| 926 if (success) { | 900 if (success) { |
| 927 // Initialization is complete, so we can schedule recurring SaveChanges. | 901 // Initialization is complete, so we can schedule recurring SaveChanges. |
| 928 sync_loop_->PostTask(FROM_HERE, | 902 sync_loop_->PostTask(FROM_HERE, |
| 929 base::Bind(&Core::StartSavingChanges, this)); | 903 base::Bind(&Core::StartSavingChanges, this)); |
| 930 } | 904 } |
| 931 } | 905 } |
| 932 | 906 |
| 933 void SyncBackendHost::Core::OnConnectionStatusChange( | 907 void SyncBackendHost::Core::OnConnectionStatusChange( |
| 934 sync_api::ConnectionStatus status) { | 908 sync_api::ConnectionStatus status) { |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1095 sync_manager_.reset(new sync_api::SyncManager(name_)); | 1069 sync_manager_.reset(new sync_api::SyncManager(name_)); |
| 1096 sync_manager_->AddObserver(this); | 1070 sync_manager_->AddObserver(this); |
| 1097 success = sync_manager_->Init( | 1071 success = sync_manager_->Init( |
| 1098 sync_data_folder_path_, | 1072 sync_data_folder_path_, |
| 1099 options.event_handler, | 1073 options.event_handler, |
| 1100 options.service_url.host() + options.service_url.path(), | 1074 options.service_url.host() + options.service_url.path(), |
| 1101 options.service_url.EffectiveIntPort(), | 1075 options.service_url.EffectiveIntPort(), |
| 1102 options.service_url.SchemeIsSecure(), | 1076 options.service_url.SchemeIsSecure(), |
| 1103 BrowserThread::GetBlockingPool(), | 1077 BrowserThread::GetBlockingPool(), |
| 1104 options.make_http_bridge_factory_fn.Run(), | 1078 options.make_http_bridge_factory_fn.Run(), |
| 1105 options.routing_info, | |
| 1106 options.workers, | 1079 options.workers, |
| 1107 options.extensions_activity_monitor, | 1080 options.extensions_activity_monitor, |
| 1108 options.registrar /* as SyncManager::ChangeDelegate */, | 1081 options.registrar /* as SyncManager::ChangeDelegate */, |
| 1109 MakeUserAgentForSyncApi(), | 1082 MakeUserAgentForSyncApi(), |
| 1110 options.credentials, | 1083 options.credentials, |
| 1111 new BridgedSyncNotifier( | 1084 new BridgedSyncNotifier( |
| 1112 options.chrome_sync_notification_bridge, | 1085 options.chrome_sync_notification_bridge, |
| 1113 options.sync_notifier_factory->CreateSyncNotifier()), | 1086 options.sync_notifier_factory->CreateSyncNotifier()), |
| 1114 options.restored_key_for_bootstrapping, | 1087 options.restored_key_for_bootstrapping, |
| 1115 options.testing_mode, | 1088 options.testing_mode, |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1282 if (initialization_state_ == CREATING_SYNC_MANAGER) | 1255 if (initialization_state_ == CREATING_SYNC_MANAGER) |
| 1283 initialization_state_ = NOT_INITIALIZED; | 1256 initialization_state_ = NOT_INITIALIZED; |
| 1284 | 1257 |
| 1285 DCHECK_EQ(MessageLoop::current(), frontend_loop_); | 1258 DCHECK_EQ(MessageLoop::current(), frontend_loop_); |
| 1286 if (!success) { | 1259 if (!success) { |
| 1287 initialization_state_ = NOT_INITIALIZED; | 1260 initialization_state_ = NOT_INITIALIZED; |
| 1288 frontend_->OnBackendInitialized(WeakHandle<JsBackend>(), false); | 1261 frontend_->OnBackendInitialized(WeakHandle<JsBackend>(), false); |
| 1289 return; | 1262 return; |
| 1290 } | 1263 } |
| 1291 | 1264 |
| 1292 // If setup has completed, start off in DOWNLOADING_NIGORI so that | |
| 1293 // we start off by refreshing nigori. | |
| 1294 CHECK(sync_prefs_.get()); | |
| 1295 if (sync_prefs_->HasSyncSetupCompleted() && | |
| 1296 initialization_state_ < DOWNLOADING_NIGORI) { | |
| 1297 initialization_state_ = DOWNLOADING_NIGORI; | |
| 1298 } | |
| 1299 | |
| 1300 // Run initialization state machine. | 1265 // Run initialization state machine. |
| 1301 switch (initialization_state_) { | 1266 switch (initialization_state_) { |
| 1302 case NOT_INITIALIZED: | 1267 case NOT_INITIALIZED: |
| 1303 initialization_state_ = DOWNLOADING_NIGORI; | 1268 initialization_state_ = DOWNLOADING_NIGORI; |
| 1304 ConfigureDataTypes( | 1269 ConfigureDataTypes( |
| 1305 sync_api::CONFIGURE_REASON_NEW_CLIENT, | 1270 sync_api::CONFIGURE_REASON_NEW_CLIENT, |
| 1306 syncable::ModelTypeSet(), | 1271 syncable::ModelTypeSet(), |
| 1307 syncable::ModelTypeSet(), | 1272 syncable::ModelTypeSet(), |
| 1308 WITH_NIGORI, | 1273 WITH_NIGORI, |
| 1309 // Calls back into this function. | 1274 // Calls back into this function. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1443 frontend_->OnConnectionStatusChange(status); | 1408 frontend_->OnConnectionStatusChange(status); |
| 1444 } | 1409 } |
| 1445 | 1410 |
| 1446 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop( | 1411 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop( |
| 1447 const WeakHandle<JsBackend>& js_backend, | 1412 const WeakHandle<JsBackend>& js_backend, |
| 1448 const syncable::ModelTypeSet failed_configuration_types) { | 1413 const syncable::ModelTypeSet failed_configuration_types) { |
| 1449 HandleInitializationCompletedOnFrontendLoop( | 1414 HandleInitializationCompletedOnFrontendLoop( |
| 1450 js_backend, failed_configuration_types.Empty()); | 1415 js_backend, failed_configuration_types.Empty()); |
| 1451 } | 1416 } |
| 1452 | 1417 |
| 1418 void SyncBackendHost::HandleSyncManagerInitializationOnFrontendLoop( |
| 1419 const WeakHandle<JsBackend>& js_backend, bool success, |
| 1420 syncable::ModelTypeSet restored_types) { |
| 1421 registrar_->SetInitialTypes(restored_types); |
| 1422 HandleInitializationCompletedOnFrontendLoop(js_backend, success); |
| 1423 } |
| 1424 |
| 1453 namespace { | 1425 namespace { |
| 1454 | 1426 |
| 1455 // Needed because MessageLoop::PostTask is overloaded. | 1427 // Needed because MessageLoop::PostTask is overloaded. |
| 1456 void PostClosure(MessageLoop* message_loop, | 1428 void PostClosure(MessageLoop* message_loop, |
| 1457 const tracked_objects::Location& from_here, | 1429 const tracked_objects::Location& from_here, |
| 1458 const base::Closure& callback) { | 1430 const base::Closure& callback) { |
| 1459 message_loop->PostTask(from_here, callback); | 1431 message_loop->PostTask(from_here, callback); |
| 1460 } | 1432 } |
| 1461 | 1433 |
| 1462 } // namespace | 1434 } // namespace |
| 1463 | 1435 |
| 1464 void SyncBackendHost::RefreshNigori(const base::Closure& done_callback) { | 1436 void SyncBackendHost::RefreshNigori(const base::Closure& done_callback) { |
| 1465 DCHECK_EQ(MessageLoop::current(), frontend_loop_); | 1437 DCHECK_EQ(MessageLoop::current(), frontend_loop_); |
| 1466 base::Closure sync_thread_done_callback = | 1438 base::Closure sync_thread_done_callback = |
| 1467 base::Bind(&PostClosure, | 1439 base::Bind(&PostClosure, |
| 1468 MessageLoop::current(), FROM_HERE, done_callback); | 1440 MessageLoop::current(), FROM_HERE, done_callback); |
| 1469 sync_thread_.message_loop()->PostTask( | 1441 sync_thread_.message_loop()->PostTask( |
| 1470 FROM_HERE, | 1442 FROM_HERE, |
| 1471 base::Bind(&SyncBackendHost::Core::DoRefreshNigori, | 1443 base::Bind(&SyncBackendHost::Core::DoRefreshNigori, |
| 1472 core_.get(), sync_thread_done_callback)); | 1444 core_.get(), sync_thread_done_callback)); |
| 1473 } | 1445 } |
| 1474 | 1446 |
| 1475 #undef SDVLOG | 1447 #undef SDVLOG |
| 1476 | 1448 |
| 1477 #undef SLOG | 1449 #undef SLOG |
| 1478 | 1450 |
| 1479 } // namespace browser_sync | 1451 } // namespace browser_sync |
| OLD | NEW |