• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/invalidator_state.h"
18 #include "sync/internal_api/public/http_post_provider_factory.h"
19 #include "sync/internal_api/public/internal_components_factory.h"
20 #include "sync/internal_api/public/util/weak_handle.h"
21 #include "sync/notifier/invalidator.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 
FakeSyncManager(ModelTypeSet initial_sync_ended_types,ModelTypeSet progress_marker_types,ModelTypeSet configure_fail_types)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 
~FakeSyncManager()38 FakeSyncManager::~FakeSyncManager() {}
39 
GetAndResetCleanedTypes()40 ModelTypeSet FakeSyncManager::GetAndResetCleanedTypes() {
41   ModelTypeSet cleaned_types = cleaned_types_;
42   cleaned_types_.Clear();
43   return cleaned_types;
44 }
45 
GetAndResetDownloadedTypes()46 ModelTypeSet FakeSyncManager::GetAndResetDownloadedTypes() {
47   ModelTypeSet downloaded_types = downloaded_types_;
48   downloaded_types_.Clear();
49   return downloaded_types;
50 }
51 
GetAndResetEnabledTypes()52 ModelTypeSet FakeSyncManager::GetAndResetEnabledTypes() {
53   ModelTypeSet enabled_types = enabled_types_;
54   enabled_types_.Clear();
55   return enabled_types;
56 }
57 
GetAndResetConfigureReason()58 ConfigureReason FakeSyncManager::GetAndResetConfigureReason() {
59   ConfigureReason reason = last_configure_reason_;
60   last_configure_reason_ = CONFIGURE_REASON_UNKNOWN;
61   return reason;
62 }
63 
WaitForSyncThread()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 
Init(const base::FilePath & database_location,const WeakHandle<JsEventHandler> & event_handler,const std::string & sync_server_and_path,int sync_server_port,bool use_ssl,scoped_ptr<HttpPostProviderFactory> post_factory,const std::vector<scoped_refptr<ModelSafeWorker>> & workers,ExtensionsActivity * extensions_activity,ChangeDelegate * change_delegate,const SyncCredentials & credentials,const std::string & invalidator_client_id,const std::string & restored_key_for_bootstrapping,const std::string & restored_keystore_key_for_bootstrapping,InternalComponentsFactory * internal_components_factory,Encryptor * encryptor,scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,ReportUnrecoverableErrorFunction report_unrecoverable_error_function,CancelationSignal * cancelation_signal)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<scoped_refptr<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 
InitialSyncEndedTypes()112 ModelTypeSet FakeSyncManager::InitialSyncEndedTypes() {
113   return initial_sync_ended_types_;
114 }
115 
GetTypesWithEmptyProgressMarkerToken(ModelTypeSet types)116 ModelTypeSet FakeSyncManager::GetTypesWithEmptyProgressMarkerToken(
117     ModelTypeSet types) {
118   ModelTypeSet empty_types = types;
119   empty_types.RemoveAll(progress_marker_types_);
120   return empty_types;
121 }
122 
PurgePartiallySyncedTypes()123 bool FakeSyncManager::PurgePartiallySyncedTypes() {
124   ModelTypeSet partial_types;
125   for (ModelTypeSet::Iterator i = progress_marker_types_.First();
126        i.Good(); i.Inc()) {
127     if (!initial_sync_ended_types_.Has(i.Get()))
128       partial_types.Put(i.Get());
129   }
130   progress_marker_types_.RemoveAll(partial_types);
131   cleaned_types_.PutAll(partial_types);
132   return true;
133 }
134 
UpdateCredentials(const SyncCredentials & credentials)135 void FakeSyncManager::UpdateCredentials(const SyncCredentials& credentials) {
136   NOTIMPLEMENTED();
137 }
138 
StartSyncingNormally(const ModelSafeRoutingInfo & routing_info)139 void FakeSyncManager::StartSyncingNormally(
140       const ModelSafeRoutingInfo& routing_info) {
141   // Do nothing.
142 }
143 
ConfigureSyncer(ConfigureReason reason,ModelTypeSet to_download,ModelTypeSet to_purge,ModelTypeSet to_journal,ModelTypeSet to_unapply,const ModelSafeRoutingInfo & new_routing_info,const base::Closure & ready_task,const base::Closure & retry_task)144 void FakeSyncManager::ConfigureSyncer(
145     ConfigureReason reason,
146     ModelTypeSet to_download,
147     ModelTypeSet to_purge,
148     ModelTypeSet to_journal,
149     ModelTypeSet to_unapply,
150     const ModelSafeRoutingInfo& new_routing_info,
151     const base::Closure& ready_task,
152     const base::Closure& retry_task) {
153   last_configure_reason_ = reason;
154   enabled_types_ = GetRoutingInfoTypes(new_routing_info);
155   ModelTypeSet success_types = to_download;
156   success_types.RemoveAll(configure_fail_types_);
157 
158   DVLOG(1) << "Faking configuration. Downloading: "
159            << ModelTypeSetToString(success_types) << ". Cleaning: "
160            << ModelTypeSetToString(to_purge);
161 
162   // Update our fake directory by clearing and fake-downloading as necessary.
163   UserShare* share = GetUserShare();
164   share->directory->PurgeEntriesWithTypeIn(to_purge,
165                                            to_journal,
166                                            to_unapply);
167   for (ModelTypeSet::Iterator it = success_types.First(); it.Good(); it.Inc()) {
168     // We must be careful to not create the same root node twice.
169     if (!initial_sync_ended_types_.Has(it.Get())) {
170       TestUserShare::CreateRoot(it.Get(), share);
171     }
172   }
173 
174   // Simulate cleaning up disabled types.
175   // TODO(sync): consider only cleaning those types that were recently disabled,
176   // if this isn't the first cleanup, which more accurately reflects the
177   // behavior of the real cleanup logic.
178   initial_sync_ended_types_.RemoveAll(to_purge);
179   progress_marker_types_.RemoveAll(to_purge);
180   cleaned_types_.PutAll(to_purge);
181 
182   // Now simulate the actual configuration for those types that successfully
183   // download + apply.
184   progress_marker_types_.PutAll(success_types);
185   initial_sync_ended_types_.PutAll(success_types);
186   downloaded_types_.PutAll(success_types);
187 
188   ready_task.Run();
189 }
190 
AddObserver(Observer * observer)191 void FakeSyncManager::AddObserver(Observer* observer) {
192   observers_.AddObserver(observer);
193 }
194 
RemoveObserver(Observer * observer)195 void FakeSyncManager::RemoveObserver(Observer* observer) {
196   observers_.RemoveObserver(observer);
197 }
198 
GetDetailedStatus() const199 SyncStatus FakeSyncManager::GetDetailedStatus() const {
200   NOTIMPLEMENTED();
201   return SyncStatus();
202 }
203 
SaveChanges()204 void FakeSyncManager::SaveChanges() {
205   // Do nothing.
206 }
207 
ShutdownOnSyncThread()208 void FakeSyncManager::ShutdownOnSyncThread() {
209   DCHECK(sync_task_runner_->RunsTasksOnCurrentThread());
210   test_user_share_.TearDown();
211 }
212 
GetUserShare()213 UserShare* FakeSyncManager::GetUserShare() {
214   return test_user_share_.user_share();
215 }
216 
GetSyncCoreProxy()217 syncer::SyncCoreProxy* FakeSyncManager::GetSyncCoreProxy() {
218   return &null_sync_core_proxy_;
219 }
220 
cache_guid()221 const std::string FakeSyncManager::cache_guid() {
222   return test_user_share_.user_share()->directory->cache_guid();
223 }
224 
ReceivedExperiment(Experiments * experiments)225 bool FakeSyncManager::ReceivedExperiment(Experiments* experiments) {
226   return false;
227 }
228 
HasUnsyncedItems()229 bool FakeSyncManager::HasUnsyncedItems() {
230   NOTIMPLEMENTED();
231   return false;
232 }
233 
GetEncryptionHandler()234 SyncEncryptionHandler* FakeSyncManager::GetEncryptionHandler() {
235   return fake_encryption_handler_.get();
236 }
237 
238 ScopedVector<syncer::ProtocolEvent>
GetBufferedProtocolEvents()239 FakeSyncManager::GetBufferedProtocolEvents() {
240   return ScopedVector<syncer::ProtocolEvent>();
241 }
242 
GetAllNodesForType(syncer::ModelType type)243 scoped_ptr<base::ListValue> FakeSyncManager::GetAllNodesForType(
244     syncer::ModelType type) {
245   return scoped_ptr<base::ListValue>(new base::ListValue());
246 }
247 
RefreshTypes(ModelTypeSet types)248 void FakeSyncManager::RefreshTypes(ModelTypeSet types) {
249   last_refresh_request_types_ = types;
250 }
251 
RegisterDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)252 void FakeSyncManager::RegisterDirectoryTypeDebugInfoObserver(
253     syncer::TypeDebugInfoObserver* observer) {}
254 
UnregisterDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)255 void FakeSyncManager::UnregisterDirectoryTypeDebugInfoObserver(
256     syncer::TypeDebugInfoObserver* observer) {}
257 
HasDirectoryTypeDebugInfoObserver(syncer::TypeDebugInfoObserver * observer)258 bool FakeSyncManager::HasDirectoryTypeDebugInfoObserver(
259     syncer::TypeDebugInfoObserver* observer) {
260   return false;
261 }
262 
RequestEmitDebugInfo()263 void FakeSyncManager::RequestEmitDebugInfo() {}
264 
OnIncomingInvalidation(const ObjectIdInvalidationMap & invalidation_map)265 void FakeSyncManager::OnIncomingInvalidation(
266       const ObjectIdInvalidationMap& invalidation_map) {
267   // Do nothing.
268 }
269 
GetLastRefreshRequestTypes()270 ModelTypeSet FakeSyncManager::GetLastRefreshRequestTypes() {
271   return last_refresh_request_types_;
272 }
273 
OnInvalidatorStateChange(InvalidatorState state)274 void FakeSyncManager::OnInvalidatorStateChange(InvalidatorState state) {
275   // Do nothing.
276 }
277 
GetOwnerName() const278 std::string FakeSyncManager::GetOwnerName() const { return "FakeSyncManager"; }
279 
280 }  // namespace syncer
281