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 3 // found in the LICENSE file. 4 5 #include "sync/internal_api/public/test/fake_sync_manager.h" 6 7 #include <cstddef> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/location.h" 12 #include "base/logging.h" 13 #include "base/run_loop.h" 14 #include "base/sequenced_task_runner.h" 15 #include "base/single_thread_task_runner.h" 16 #include "base/thread_task_runner_handle.h" 17 #include "sync/internal_api/public/http_post_provider_factory.h" 18 #include "sync/internal_api/public/internal_components_factory.h" 19 #include "sync/internal_api/public/util/weak_handle.h" 20 #include "sync/notifier/invalidator.h" 21 #include "sync/notifier/invalidator_state.h" 22 #include "sync/notifier/object_id_invalidation_map.h" 23 #include "sync/syncable/directory.h" 24 #include "sync/test/fake_sync_encryption_handler.h" 25 26 namespace syncer { 27 28 FakeSyncManager::FakeSyncManager(ModelTypeSet initial_sync_ended_types, 29 ModelTypeSet progress_marker_types, 30 ModelTypeSet configure_fail_types) : 31 initial_sync_ended_types_(initial_sync_ended_types), 32 progress_marker_types_(progress_marker_types), 33 configure_fail_types_(configure_fail_types), 34 last_configure_reason_(CONFIGURE_REASON_UNKNOWN) { 35 fake_encryption_handler_.reset(new FakeSyncEncryptionHandler()); 36 } 37 38 FakeSyncManager::~FakeSyncManager() {} 39 40 ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() { 41 ModelTypeSet cleaned_types = cleaned_types_; 42 cleaned_types_.Clear(); 43 return cleaned_types; 44 } 45 46 ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() { 47 ModelTypeSet downloaded_types = downloaded_types_; 48 downloaded_types_.Clear(); 49 return downloaded_types; 50 } 51 52 ModelTypeSet FakeSyncManager::GetAndResetEnabledTypes() { 53 ModelTypeSet enabled_types = enabled_types_; 54 enabled_types_.Clear(); 55 return enabled_types; 56 } 57 58 ConfigureReason FakeSyncManager::GetAndResetConfigureReason() { 59 ConfigureReason reason = last_configure_reason_; 60 last_configure_reason_ = CONFIGURE_REASON_UNKNOWN; 61 return reason; 62 } 63 64 void FakeSyncManager::WaitForSyncThread() { 65 // Post a task to |sync_task_runner_| and block until it runs. 66 base::RunLoop run_loop; 67 if (!sync_task_runner_->PostTaskAndReply( 68 FROM_HERE, 69 base::Bind(&base::DoNothing), 70 run_loop.QuitClosure())) { 71 NOTREACHED(); 72 } 73 run_loop.Run(); 74 } 75 76 void FakeSyncManager::Init( 77 const base::FilePath& database_location, 78 const WeakHandle<JsEventHandler>& event_handler, 79 const std::string& sync_server_and_path, 80 int sync_server_port, 81 bool use_ssl, 82 scoped_ptr<HttpPostProviderFactory> post_factory, 83 const std::vector<ModelSafeWorker*>& workers, 84 ExtensionsActivity* extensions_activity, 85 ChangeDelegate* change_delegate, 86 const SyncCredentials& credentials, 87 const std::string& invalidator_client_id, 88 const std::string& restored_key_for_bootstrapping, 89 const std::string& restored_keystore_key_for_bootstrapping, 90 InternalComponentsFactory* internal_components_factory, 91 Encryptor* encryptor, 92 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler, 93 ReportUnrecoverableErrorFunction report_unrecoverable_error_function, 94 CancelationSignal* cancelation_signal) { 95 sync_task_runner_ = base::ThreadTaskRunnerHandle::Get(); 96 PurgePartiallySyncedTypes(); 97 98 test_user_share_.SetUp(); 99 UserShare* share = test_user_share_.user_share(); 100 for (ModelTypeSet::Iterator it = initial_sync_ended_types_.First(); 101 it.Good(); it.Inc()) { 102 TestUserShare::CreateRoot(it.Get(), share); 103 } 104 105 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, 106 OnInitializationComplete( 107 WeakHandle<JsBackend>(), 108 WeakHandle<DataTypeDebugInfoListener>(), 109 true, initial_sync_ended_types_)); 110 } 111 112 void FakeSyncManager::ThrowUnrecoverableError() { 113 NOTIMPLEMENTED(); 114 } 115 116 ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() { 117 return initial_sync_ended_types_; 118 } 119 120 ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken( 121 ModelTypeSet types) { 122 ModelTypeSet empty_types = types; 123 empty_types.RemoveAll(progress_marker_types_); 124 return empty_types; 125 } 126 127 bool FakeSyncManager::PurgePartiallySyncedTypes() { 128 ModelTypeSet partial_types; 129 for (ModelTypeSet::Iterator i = progress_marker_types_.First(); 130 i.Good(); i.Inc()) { 131 if (!initial_sync_ended_types_.Has(i.Get())) 132 partial_types.Put(i.Get()); 133 } 134 progress_marker_types_.RemoveAll(partial_types); 135 cleaned_types_.PutAll(partial_types); 136 return true; 137 } 138 139 void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) { 140 NOTIMPLEMENTED(); 141 } 142 143 void FakeSyncManager::StartSyncingNormally( 144 const ModelSafeRoutingInfo& routing_info) { 145 // Do nothing. 146 } 147 148 void FakeSyncManager::ConfigureSyncer( 149 ConfigureReason reason, 150 ModelTypeSet to_download, 151 ModelTypeSet to_purge, 152 ModelTypeSet to_journal, 153 ModelTypeSet to_unapply, 154 const ModelSafeRoutingInfo& new_routing_info, 155 const base::Closure& ready_task, 156 const base::Closure& retry_task) { 157 last_configure_reason_ = reason; 158 enabled_types_ = GetRoutingInfoTypes(new_routing_info); 159 ModelTypeSet success_types = to_download; 160 success_types.RemoveAll(configure_fail_types_); 161 162 DVLOG(1) << "Faking configuration. Downloading: " 163 << ModelTypeSetToString(success_types) << ". Cleaning: " 164 << ModelTypeSetToString(to_purge); 165 166 // Update our fake directory by clearing and fake-downloading as necessary. 167 UserShare* share = GetUserShare(); 168 share->directory->PurgeEntriesWithTypeIn(to_purge, 169 to_journal, 170 to_unapply); 171 for (ModelTypeSet::Iterator it = success_types.First(); it.Good(); it.Inc()) { 172 // We must be careful to not create the same root node twice. 173 if (!initial_sync_ended_types_.Has(it.Get())) { 174 TestUserShare::CreateRoot(it.Get(), share); 175 } 176 } 177 178 // Simulate cleaning up disabled types. 179 // TODO(sync): consider only cleaning those types that were recently disabled, 180 // if this isn't the first cleanup, which more accurately reflects the 181 // behavior of the real cleanup logic. 182 initial_sync_ended_types_.RemoveAll(to_purge); 183 progress_marker_types_.RemoveAll(to_purge); 184 cleaned_types_.PutAll(to_purge); 185 186 // Now simulate the actual configuration for those types that successfully 187 // download + apply. 188 progress_marker_types_.PutAll(success_types); 189 initial_sync_ended_types_.PutAll(success_types); 190 downloaded_types_.PutAll(success_types); 191 192 ready_task.Run(); 193 } 194 195 void FakeSyncManager::AddObserver(Observer* observer) { 196 observers_.AddObserver(observer); 197 } 198 199 void FakeSyncManager::RemoveObserver(Observer* observer) { 200 observers_.RemoveObserver(observer); 201 } 202 203 SyncStatus FakeSyncManager::GetDetailedStatus() const { 204 NOTIMPLEMENTED(); 205 return SyncStatus(); 206 } 207 208 void FakeSyncManager::SaveChanges() { 209 // Do nothing. 210 } 211 212 void FakeSyncManager::ShutdownOnSyncThread() { 213 DCHECK(sync_task_runner_->RunsTasksOnCurrentThread()); 214 test_user_share_.TearDown(); 215 } 216 217 UserShare* FakeSyncManager::GetUserShare() { 218 return test_user_share_.user_share(); 219 } 220 221 const std::string FakeSyncManager::cache_guid() { 222 return test_user_share_.user_share()->directory->cache_guid(); 223 } 224 225 bool FakeSyncManager::ReceivedExperiment(Experiments* experiments) { 226 return false; 227 } 228 229 bool FakeSyncManager::HasUnsyncedItems() { 230 NOTIMPLEMENTED(); 231 return false; 232 } 233 234 SyncEncryptionHandler* FakeSyncManager::GetEncryptionHandler() { 235 return fake_encryption_handler_.get(); 236 } 237 238 void FakeSyncManager::RefreshTypes(ModelTypeSet types) { 239 last_refresh_request_types_ = types; 240 } 241 242 void FakeSyncManager::OnIncomingInvalidation( 243 const ObjectIdInvalidationMap& invalidation_map) { 244 // Do nothing. 245 } 246 247 ModelTypeSet FakeSyncManager::GetLastRefreshRequestTypes() { 248 return last_refresh_request_types_; 249 } 250 251 void FakeSyncManager::OnInvalidatorStateChange(InvalidatorState state) { 252 // Do nothing. 253 } 254 255 } // namespace syncer 256