OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "sync/internal_api/sync_manager.h" | 5 #include "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" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/file_util.h" |
14 #include "base/json/json_writer.h" | 15 #include "base/json/json_writer.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
18 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
21 #include "sync/engine/net/server_connection_manager.h" | 22 #include "sync/engine/net/server_connection_manager.h" |
22 #include "sync/engine/nigori_util.h" | 23 #include "sync/engine/nigori_util.h" |
23 #include "sync/engine/polling_constants.h" | 24 #include "sync/engine/polling_constants.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 CHECK(!initialized_); | 189 CHECK(!initialized_); |
189 } | 190 } |
190 | 191 |
191 bool Init(const FilePath& database_location, | 192 bool Init(const FilePath& database_location, |
192 const WeakHandle<JsEventHandler>& event_handler, | 193 const WeakHandle<JsEventHandler>& event_handler, |
193 const std::string& sync_server_and_path, | 194 const std::string& sync_server_and_path, |
194 int port, | 195 int port, |
195 bool use_ssl, | 196 bool use_ssl, |
196 const scoped_refptr<base::TaskRunner>& blocking_task_runner, | 197 const scoped_refptr<base::TaskRunner>& blocking_task_runner, |
197 HttpPostProviderFactory* post_factory, | 198 HttpPostProviderFactory* post_factory, |
198 const browser_sync::ModelSafeRoutingInfo& model_safe_routing_info, | |
199 const std::vector<browser_sync::ModelSafeWorker*>& workers, | 199 const std::vector<browser_sync::ModelSafeWorker*>& workers, |
200 browser_sync::ExtensionsActivityMonitor* | 200 browser_sync::ExtensionsActivityMonitor* |
201 extensions_activity_monitor, | 201 extensions_activity_monitor, |
202 ChangeDelegate* change_delegate, | 202 ChangeDelegate* change_delegate, |
203 const std::string& user_agent, | 203 const std::string& user_agent, |
204 const SyncCredentials& credentials, | 204 const SyncCredentials& credentials, |
205 sync_notifier::SyncNotifier* sync_notifier, | 205 sync_notifier::SyncNotifier* sync_notifier, |
206 const std::string& restored_key_for_bootstrapping, | 206 const std::string& restored_key_for_bootstrapping, |
207 TestingMode testing_mode, | 207 TestingMode testing_mode, |
208 Encryptor* encryptor, | 208 Encryptor* encryptor, |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
732 } | 732 } |
733 | 733 |
734 bool SyncManager::Init( | 734 bool SyncManager::Init( |
735 const FilePath& database_location, | 735 const FilePath& database_location, |
736 const WeakHandle<JsEventHandler>& event_handler, | 736 const WeakHandle<JsEventHandler>& event_handler, |
737 const std::string& sync_server_and_path, | 737 const std::string& sync_server_and_path, |
738 int sync_server_port, | 738 int sync_server_port, |
739 bool use_ssl, | 739 bool use_ssl, |
740 const scoped_refptr<base::TaskRunner>& blocking_task_runner, | 740 const scoped_refptr<base::TaskRunner>& blocking_task_runner, |
741 HttpPostProviderFactory* post_factory, | 741 HttpPostProviderFactory* post_factory, |
742 const browser_sync::ModelSafeRoutingInfo& model_safe_routing_info, | |
743 const std::vector<browser_sync::ModelSafeWorker*>& workers, | 742 const std::vector<browser_sync::ModelSafeWorker*>& workers, |
744 browser_sync::ExtensionsActivityMonitor* extensions_activity_monitor, | 743 browser_sync::ExtensionsActivityMonitor* extensions_activity_monitor, |
745 ChangeDelegate* change_delegate, | 744 ChangeDelegate* change_delegate, |
746 const std::string& user_agent, | 745 const std::string& user_agent, |
747 const SyncCredentials& credentials, | 746 const SyncCredentials& credentials, |
748 sync_notifier::SyncNotifier* sync_notifier, | 747 sync_notifier::SyncNotifier* sync_notifier, |
749 const std::string& restored_key_for_bootstrapping, | 748 const std::string& restored_key_for_bootstrapping, |
750 TestingMode testing_mode, | 749 TestingMode testing_mode, |
751 Encryptor* encryptor, | 750 Encryptor* encryptor, |
752 UnrecoverableErrorHandler* unrecoverable_error_handler, | 751 UnrecoverableErrorHandler* unrecoverable_error_handler, |
753 ReportUnrecoverableErrorFunction report_unrecoverable_error_function) { | 752 ReportUnrecoverableErrorFunction report_unrecoverable_error_function) { |
754 DCHECK(thread_checker_.CalledOnValidThread()); | 753 DCHECK(thread_checker_.CalledOnValidThread()); |
755 DCHECK(post_factory); | 754 DCHECK(post_factory); |
756 DVLOG(1) << "SyncManager starting Init..."; | 755 DVLOG(1) << "SyncManager starting Init..."; |
757 std::string server_string(sync_server_and_path); | 756 std::string server_string(sync_server_and_path); |
758 return data_->Init(database_location, | 757 return data_->Init(database_location, |
759 event_handler, | 758 event_handler, |
760 server_string, | 759 server_string, |
761 sync_server_port, | 760 sync_server_port, |
762 use_ssl, | 761 use_ssl, |
763 blocking_task_runner, | 762 blocking_task_runner, |
764 post_factory, | 763 post_factory, |
765 model_safe_routing_info, | |
766 workers, | 764 workers, |
767 extensions_activity_monitor, | 765 extensions_activity_monitor, |
768 change_delegate, | 766 change_delegate, |
769 user_agent, | 767 user_agent, |
770 credentials, | 768 credentials, |
771 sync_notifier, | 769 sync_notifier, |
772 restored_key_for_bootstrapping, | 770 restored_key_for_bootstrapping, |
773 testing_mode, | 771 testing_mode, |
774 encryptor, | 772 encryptor, |
775 unrecoverable_error_handler, | 773 unrecoverable_error_handler, |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
884 } | 882 } |
885 | 883 |
886 bool SyncManager::SyncInternal::Init( | 884 bool SyncManager::SyncInternal::Init( |
887 const FilePath& database_location, | 885 const FilePath& database_location, |
888 const WeakHandle<JsEventHandler>& event_handler, | 886 const WeakHandle<JsEventHandler>& event_handler, |
889 const std::string& sync_server_and_path, | 887 const std::string& sync_server_and_path, |
890 int port, | 888 int port, |
891 bool use_ssl, | 889 bool use_ssl, |
892 const scoped_refptr<base::TaskRunner>& blocking_task_runner, | 890 const scoped_refptr<base::TaskRunner>& blocking_task_runner, |
893 HttpPostProviderFactory* post_factory, | 891 HttpPostProviderFactory* post_factory, |
894 const browser_sync::ModelSafeRoutingInfo& model_safe_routing_info, | |
895 const std::vector<browser_sync::ModelSafeWorker*>& workers, | 892 const std::vector<browser_sync::ModelSafeWorker*>& workers, |
896 browser_sync::ExtensionsActivityMonitor* extensions_activity_monitor, | 893 browser_sync::ExtensionsActivityMonitor* extensions_activity_monitor, |
897 ChangeDelegate* change_delegate, | 894 ChangeDelegate* change_delegate, |
898 const std::string& user_agent, | 895 const std::string& user_agent, |
899 const SyncCredentials& credentials, | 896 const SyncCredentials& credentials, |
900 sync_notifier::SyncNotifier* sync_notifier, | 897 sync_notifier::SyncNotifier* sync_notifier, |
901 const std::string& restored_key_for_bootstrapping, | 898 const std::string& restored_key_for_bootstrapping, |
902 TestingMode testing_mode, | 899 TestingMode testing_mode, |
903 Encryptor* encryptor, | 900 Encryptor* encryptor, |
904 UnrecoverableErrorHandler* unrecoverable_error_handler, | 901 UnrecoverableErrorHandler* unrecoverable_error_handler, |
(...skipping 16 matching lines...) Expand all Loading... |
921 AddObserver(&js_sync_manager_observer_); | 918 AddObserver(&js_sync_manager_observer_); |
922 SetJsEventHandler(event_handler); | 919 SetJsEventHandler(event_handler); |
923 | 920 |
924 AddObserver(&debug_info_event_listener_); | 921 AddObserver(&debug_info_event_listener_); |
925 | 922 |
926 database_path_ = database_location.Append( | 923 database_path_ = database_location.Append( |
927 syncable::Directory::kSyncDatabaseFilename); | 924 syncable::Directory::kSyncDatabaseFilename); |
928 encryptor_ = encryptor; | 925 encryptor_ = encryptor; |
929 unrecoverable_error_handler_ = unrecoverable_error_handler; | 926 unrecoverable_error_handler_ = unrecoverable_error_handler; |
930 report_unrecoverable_error_function_ = report_unrecoverable_error_function; | 927 report_unrecoverable_error_function_ = report_unrecoverable_error_function; |
931 share_.directory.reset( | |
932 new syncable::Directory(encryptor_, | |
933 unrecoverable_error_handler_, | |
934 report_unrecoverable_error_function_)); | |
935 | 928 |
936 connection_manager_.reset(new SyncAPIServerConnectionManager( | 929 connection_manager_.reset(new SyncAPIServerConnectionManager( |
937 sync_server_and_path, port, use_ssl, user_agent, post_factory)); | 930 sync_server_and_path, port, use_ssl, user_agent, post_factory)); |
938 | 931 |
939 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 932 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
940 observing_ip_address_changes_ = true; | 933 observing_ip_address_changes_ = true; |
941 | 934 |
942 connection_manager()->AddListener(this); | 935 connection_manager()->AddListener(this); |
943 | 936 |
| 937 bool signed_in = SignIn(credentials); |
| 938 |
944 // Test mode does not use a syncer context or syncer thread. | 939 // Test mode does not use a syncer context or syncer thread. |
945 if (testing_mode_ == NON_TEST) { | 940 if (testing_mode_ == NON_TEST) { |
946 // Build a SyncSessionContext and store the worker in it. | 941 // Build a SyncSessionContext and store the worker in it. |
947 DVLOG(1) << "Sync is bringing up SyncSessionContext."; | 942 DVLOG(1) << "Sync is bringing up SyncSessionContext."; |
948 std::vector<SyncEngineEventListener*> listeners; | 943 std::vector<SyncEngineEventListener*> listeners; |
949 listeners.push_back(&allstatus_); | 944 listeners.push_back(&allstatus_); |
950 listeners.push_back(this); | 945 listeners.push_back(this); |
951 session_context_.reset(new SyncSessionContext( | 946 session_context_.reset(new SyncSessionContext( |
952 connection_manager_.get(), | 947 connection_manager_.get(), |
953 directory(), | 948 directory(), |
954 model_safe_routing_info, | |
955 workers, | 949 workers, |
956 extensions_activity_monitor, | 950 extensions_activity_monitor, |
957 listeners, | 951 listeners, |
958 &debug_info_event_listener_, | 952 &debug_info_event_listener_, |
959 &traffic_recorder_)); | 953 &traffic_recorder_)); |
960 session_context()->set_account_name(credentials.email); | 954 session_context()->set_account_name(credentials.email); |
961 scheduler_.reset(new SyncScheduler(name_, session_context(), new Syncer())); | 955 scheduler_.reset(new SyncScheduler(name_, session_context(), new Syncer())); |
962 } | 956 } |
963 | 957 |
964 bool signed_in = SignIn(credentials); | |
965 | |
966 if (signed_in) { | 958 if (signed_in) { |
967 if (scheduler()) { | 959 if (scheduler()) { |
968 scheduler()->Start( | 960 scheduler()->Start( |
969 browser_sync::SyncScheduler::CONFIGURATION_MODE, base::Closure()); | 961 browser_sync::SyncScheduler::CONFIGURATION_MODE, base::Closure()); |
970 } | 962 } |
971 | 963 |
972 initialized_ = true; | 964 initialized_ = true; |
973 | 965 |
974 // Cryptographer should only be accessed while holding a | 966 // Cryptographer should only be accessed while holding a |
975 // transaction. Grabbing the user share for the transaction | 967 // transaction. Grabbing the user share for the transaction |
976 // checks the initialization state, so this must come after | 968 // checks the initialization state, so this must come after |
977 // |initialized_| is set to true. | 969 // |initialized_| is set to true. |
978 ReadTransaction trans(FROM_HERE, GetUserShare()); | 970 ReadTransaction trans(FROM_HERE, GetUserShare()); |
979 trans.GetCryptographer()->Bootstrap(restored_key_for_bootstrapping); | 971 trans.GetCryptographer()->Bootstrap(restored_key_for_bootstrapping); |
980 trans.GetCryptographer()->AddObserver(this); | 972 trans.GetCryptographer()->AddObserver(this); |
981 } | 973 } |
982 | 974 |
983 // Notify that initialization is complete. Note: This should be the last to | 975 // Notify that initialization is complete. Note: This should be the last to |
984 // execute if |signed_in| is false. Reason being in that case we would | 976 // execute if |signed_in| is false. Reason being in that case we would |
985 // post a task to shutdown sync. But if this function posts any other tasks | 977 // post a task to shutdown sync. But if this function posts any other tasks |
986 // on the UI thread and if shutdown wins then that tasks would execute on | 978 // on the UI thread and if shutdown wins then that tasks would execute on |
987 // a freed pointer. This is because UI thread is not shut down. | 979 // a freed pointer. This is because UI thread is not shut down. |
988 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 980 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
989 OnInitializationComplete( | 981 OnInitializationComplete( |
990 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()), | 982 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()), |
991 signed_in)); | 983 signed_in, |
| 984 signed_in ? InitialSyncEndedTypes() : |
| 985 syncable::ModelTypeSet())); |
992 | 986 |
993 if (!signed_in && testing_mode_ == NON_TEST) | 987 if (!signed_in && testing_mode_ == NON_TEST) |
994 return false; | 988 return false; |
995 | 989 |
996 sync_notifier_->AddObserver(this); | 990 sync_notifier_->AddObserver(this); |
997 | 991 |
998 return signed_in; | 992 return signed_in; |
999 } | 993 } |
1000 | 994 |
1001 void SyncManager::SyncInternal::UpdateCryptographerAndNigori( | 995 void SyncManager::SyncInternal::UpdateCryptographerAndNigori( |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1141 const browser_sync::ModelSafeRoutingInfo& routing_info) { | 1135 const browser_sync::ModelSafeRoutingInfo& routing_info) { |
1142 // Start the sync scheduler. | 1136 // Start the sync scheduler. |
1143 if (scheduler()) { // NULL during certain unittests. | 1137 if (scheduler()) { // NULL during certain unittests. |
1144 session_context()->set_routing_info(routing_info); | 1138 session_context()->set_routing_info(routing_info); |
1145 scheduler()->Start(SyncScheduler::NORMAL_MODE, base::Closure()); | 1139 scheduler()->Start(SyncScheduler::NORMAL_MODE, base::Closure()); |
1146 } | 1140 } |
1147 } | 1141 } |
1148 | 1142 |
1149 bool SyncManager::SyncInternal::OpenDirectory() { | 1143 bool SyncManager::SyncInternal::OpenDirectory() { |
1150 DCHECK(!initialized_) << "Should only happen once"; | 1144 DCHECK(!initialized_) << "Should only happen once"; |
| 1145 share_.directory.reset( |
| 1146 new syncable::Directory(encryptor_, |
| 1147 unrecoverable_error_handler_, |
| 1148 report_unrecoverable_error_function_)); |
1151 | 1149 |
1152 // Set before Open(). | 1150 // Set before Open(). |
1153 change_observer_ = | 1151 change_observer_ = |
1154 browser_sync::MakeWeakHandle(js_mutation_event_observer_.AsWeakPtr()); | 1152 browser_sync::MakeWeakHandle(js_mutation_event_observer_.AsWeakPtr()); |
1155 WeakHandle<syncable::TransactionObserver> transaction_observer( | 1153 WeakHandle<syncable::TransactionObserver> transaction_observer( |
1156 browser_sync::MakeWeakHandle(js_mutation_event_observer_.AsWeakPtr())); | 1154 browser_sync::MakeWeakHandle(js_mutation_event_observer_.AsWeakPtr())); |
1157 | 1155 |
1158 syncable::DirOpenResult open_result = syncable::NOT_INITIALIZED; | 1156 syncable::DirOpenResult open_result = syncable::NOT_INITIALIZED; |
1159 if (testing_mode_ == TEST_IN_MEMORY) { | 1157 if (testing_mode_ == TEST_IN_MEMORY) { |
1160 open_result = directory()->OpenInMemoryForTest( | 1158 open_result = directory()->OpenInMemoryForTest( |
1161 username_for_share(), this, transaction_observer); | 1159 username_for_share(), this, transaction_observer); |
1162 } else { | 1160 } else { |
1163 open_result = directory()->Open( | 1161 open_result = directory()->Open( |
1164 database_path_, username_for_share(), this, transaction_observer); | 1162 database_path_, username_for_share(), this, transaction_observer); |
| 1163 // If at first we don't succeed, delete the DB and try again. |
| 1164 if (open_result != syncable::OPENED) { |
| 1165 file_util::Delete(database_path_, true); |
| 1166 open_result = directory()->Open( |
| 1167 database_path_, username_for_share(), this, transaction_observer); |
| 1168 } |
1165 } | 1169 } |
1166 if (open_result != syncable::OPENED) { | 1170 if (open_result != syncable::OPENED) { |
1167 LOG(ERROR) << "Could not open share for:" << username_for_share(); | 1171 LOG(ERROR) << "Could not open share for:" << username_for_share(); |
1168 return false; | 1172 return false; |
1169 } | 1173 } |
1170 | 1174 |
1171 connection_manager()->set_client_id(directory()->cache_guid()); | 1175 connection_manager()->set_client_id(directory()->cache_guid()); |
1172 return true; | 1176 return true; |
1173 } | 1177 } |
1174 | 1178 |
(...skipping 1309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2484 bool InitialSyncEndedForTypes(syncable::ModelTypeSet types, | 2488 bool InitialSyncEndedForTypes(syncable::ModelTypeSet types, |
2485 sync_api::UserShare* share) { | 2489 sync_api::UserShare* share) { |
2486 for (syncable::ModelTypeSet::Iterator i = types.First(); | 2490 for (syncable::ModelTypeSet::Iterator i = types.First(); |
2487 i.Good(); i.Inc()) { | 2491 i.Good(); i.Inc()) { |
2488 if (!share->directory->initial_sync_ended_for_type(i.Get())) | 2492 if (!share->directory->initial_sync_ended_for_type(i.Get())) |
2489 return false; | 2493 return false; |
2490 } | 2494 } |
2491 return true; | 2495 return true; |
2492 } | 2496 } |
2493 | 2497 |
2494 syncable::ModelTypeSet GetTypesWithEmptyProgressMarkerToken( | |
2495 syncable::ModelTypeSet types, | |
2496 sync_api::UserShare* share) { | |
2497 syncable::ModelTypeSet result; | |
2498 for (syncable::ModelTypeSet::Iterator i = types.First(); | |
2499 i.Good(); i.Inc()) { | |
2500 sync_pb::DataTypeProgressMarker marker; | |
2501 share->directory->GetDownloadProgress(i.Get(), &marker); | |
2502 | |
2503 if (marker.token().empty()) | |
2504 result.Put(i.Get()); | |
2505 | |
2506 } | |
2507 return result; | |
2508 } | |
2509 | |
2510 } // namespace sync_api | 2498 } // namespace sync_api |
OLD | NEW |