Refactor AO2TS to make it easier to componentize
[chromium-blink-merge.git] / sync / internal_api / sync_rollback_manager_base.cc
blob3e748fc667b99ba281a01139d5ff9e077b7344a1
1 // Copyright 2014 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.
5 #include "sync/internal_api/sync_rollback_manager_base.h"
7 #include "sync/internal_api/public/base/model_type.h"
8 #include "sync/internal_api/public/read_node.h"
9 #include "sync/internal_api/public/read_transaction.h"
10 #include "sync/internal_api/public/write_transaction.h"
11 #include "sync/syncable/directory_backing_store.h"
12 #include "sync/syncable/mutable_entry.h"
14 namespace {
16 // Permanent bookmark folders as defined in bookmark_model_associator.cc.
17 // No mobile bookmarks because they only exists with sync enabled.
18 const char kBookmarkBarTag[] = "bookmark_bar";
19 const char kOtherBookmarksTag[] = "other_bookmarks";
21 class DummyEntryptionHandler : public syncer::SyncEncryptionHandler {
22 void AddObserver(Observer* observer) override {}
23 void RemoveObserver(Observer* observer) override {}
24 void Init() override {}
25 void SetEncryptionPassphrase(const std::string& passphrase,
26 bool is_explicit) override {}
27 void SetDecryptionPassphrase(const std::string& passphrase) override {}
28 void EnableEncryptEverything() override {}
29 bool EncryptEverythingEnabled() const override { return false; }
30 syncer::PassphraseType GetPassphraseType() const override {
31 return syncer::KEYSTORE_PASSPHRASE;
35 } // anonymous namespace
37 namespace syncer {
39 SyncRollbackManagerBase::SyncRollbackManagerBase()
40 : dummy_handler_(new DummyEntryptionHandler),
41 initialized_(false),
42 weak_ptr_factory_(this) {
45 SyncRollbackManagerBase::~SyncRollbackManagerBase() {
48 bool SyncRollbackManagerBase::InitInternal(
49 const base::FilePath& database_location,
50 InternalComponentsFactory* internal_components_factory,
51 InternalComponentsFactory::StorageOption storage,
52 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
53 const base::Closure& report_unrecoverable_error_function) {
54 unrecoverable_error_handler_ = unrecoverable_error_handler.Pass();
55 report_unrecoverable_error_function_ = report_unrecoverable_error_function;
57 if (!InitBackupDB(database_location, internal_components_factory, storage)) {
58 NotifyInitializationFailure();
59 return false;
62 initialized_ = true;
63 NotifyInitializationSuccess();
64 return true;
67 ModelTypeSet SyncRollbackManagerBase::InitialSyncEndedTypes() {
68 return share_.directory->InitialSyncEndedTypes();
71 ModelTypeSet SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken(
72 ModelTypeSet types) {
73 ModelTypeSet inited_types = share_.directory->InitialSyncEndedTypes();
74 types.RemoveAll(inited_types);
75 return types;
78 bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() {
79 NOTREACHED();
80 return true;
83 void SyncRollbackManagerBase::UpdateCredentials(
84 const SyncCredentials& credentials) {
87 void SyncRollbackManagerBase::StartSyncingNormally(
88 const ModelSafeRoutingInfo& routing_info, base::Time last_poll_time){
91 void SyncRollbackManagerBase::ConfigureSyncer(
92 ConfigureReason reason,
93 ModelTypeSet to_download,
94 ModelTypeSet to_purge,
95 ModelTypeSet to_journal,
96 ModelTypeSet to_unapply,
97 const ModelSafeRoutingInfo& new_routing_info,
98 const base::Closure& ready_task,
99 const base::Closure& retry_task) {
100 for (ModelTypeSet::Iterator type = to_download.First();
101 type.Good(); type.Inc()) {
102 if (InitTypeRootNode(type.Get())) {
103 if (type.Get() == BOOKMARKS) {
104 InitBookmarkFolder(kBookmarkBarTag);
105 InitBookmarkFolder(kOtherBookmarksTag);
110 ready_task.Run();
113 void SyncRollbackManagerBase::SetInvalidatorEnabled(bool invalidator_enabled) {
116 void SyncRollbackManagerBase::OnIncomingInvalidation(
117 syncer::ModelType type,
118 scoped_ptr<InvalidationInterface> invalidation) {
119 NOTREACHED();
122 void SyncRollbackManagerBase::AddObserver(SyncManager::Observer* observer) {
123 observers_.AddObserver(observer);
126 void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer* observer) {
127 observers_.RemoveObserver(observer);
130 SyncStatus SyncRollbackManagerBase::GetDetailedStatus() const {
131 return SyncStatus();
134 void SyncRollbackManagerBase::SaveChanges() {
137 void SyncRollbackManagerBase::ShutdownOnSyncThread(ShutdownReason reason) {
138 if (initialized_) {
139 share_.directory.reset();
140 initialized_ = false;
144 UserShare* SyncRollbackManagerBase::GetUserShare() {
145 return &share_;
148 const std::string SyncRollbackManagerBase::cache_guid() {
149 return share_.directory->cache_guid();
152 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments* experiments) {
153 return false;
156 bool SyncRollbackManagerBase::HasUnsyncedItems() {
157 ReadTransaction trans(FROM_HERE, &share_);
158 syncable::Directory::Metahandles unsynced;
159 share_.directory->GetUnsyncedMetaHandles(trans.GetWrappedTrans(), &unsynced);
160 return !unsynced.empty();
163 SyncEncryptionHandler* SyncRollbackManagerBase::GetEncryptionHandler() {
164 return dummy_handler_.get();
167 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types) {
171 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent(
172 ModelTypeSet models_with_changes) {
175 ModelTypeSet SyncRollbackManagerBase::HandleTransactionEndingChangeEvent(
176 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
177 syncable::BaseTransaction* trans) {
178 return ModelTypeSet();
181 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi(
182 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
183 syncable::BaseTransaction* trans,
184 std::vector<int64>* entries_changed) {
187 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer(
188 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
189 syncable::BaseTransaction* trans,
190 std::vector<int64>* entries_changed) {
193 void SyncRollbackManagerBase::OnTransactionWrite(
194 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
195 ModelTypeSet models_with_changes) {
198 void SyncRollbackManagerBase::NotifyInitializationSuccess() {
199 FOR_EACH_OBSERVER(
200 SyncManager::Observer, observers_,
201 OnInitializationComplete(
202 MakeWeakHandle(base::WeakPtr<JsBackend>()),
203 MakeWeakHandle(base::WeakPtr<DataTypeDebugInfoListener>()),
204 true, InitialSyncEndedTypes()));
207 void SyncRollbackManagerBase::NotifyInitializationFailure() {
208 FOR_EACH_OBSERVER(
209 SyncManager::Observer, observers_,
210 OnInitializationComplete(
211 MakeWeakHandle(base::WeakPtr<JsBackend>()),
212 MakeWeakHandle(base::WeakPtr<DataTypeDebugInfoListener>()),
213 false, ModelTypeSet()));
216 syncer::SyncContextProxy* SyncRollbackManagerBase::GetSyncContextProxy() {
217 return NULL;
220 ScopedVector<syncer::ProtocolEvent>
221 SyncRollbackManagerBase::GetBufferedProtocolEvents() {
222 return ScopedVector<syncer::ProtocolEvent>().Pass();
225 scoped_ptr<base::ListValue> SyncRollbackManagerBase::GetAllNodesForType(
226 syncer::ModelType type) {
227 ReadTransaction trans(FROM_HERE, GetUserShare());
228 scoped_ptr<base::ListValue> nodes(
229 trans.GetDirectory()->GetNodeDetailsForType(trans.GetWrappedTrans(),
230 type));
231 return nodes.Pass();
234 bool SyncRollbackManagerBase::InitBackupDB(
235 const base::FilePath& sync_folder,
236 InternalComponentsFactory* internal_components_factory,
237 InternalComponentsFactory::StorageOption storage) {
238 base::FilePath backup_db_path = sync_folder.Append(
239 syncable::Directory::kSyncDatabaseFilename);
240 scoped_ptr<syncable::DirectoryBackingStore> backing_store =
241 internal_components_factory->BuildDirectoryBackingStore(
242 storage, "backup", backup_db_path).Pass();
244 DCHECK(backing_store.get());
245 share_.directory.reset(
246 new syncable::Directory(
247 backing_store.release(),
248 unrecoverable_error_handler_.get(),
249 report_unrecoverable_error_function_,
250 NULL,
251 NULL));
252 return syncable::OPENED ==
253 share_.directory->Open(
254 "backup", this,
255 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr()));
258 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type) {
259 WriteTransaction trans(FROM_HERE, &share_);
260 ReadNode root(&trans);
261 if (BaseNode::INIT_OK == root.InitTypeRoot(type))
262 return true;
264 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
265 syncable::CREATE_NEW_UPDATE_ITEM,
266 syncable::Id::CreateFromServerId(
267 ModelTypeToString(type)));
268 if (!entry.good())
269 return false;
271 entry.PutParentId(syncable::Id::GetRoot());
272 entry.PutBaseVersion(1);
273 entry.PutUniqueServerTag(ModelTypeToRootTag(type));
274 entry.PutNonUniqueName(ModelTypeToString(type));
275 entry.PutIsDel(false);
276 entry.PutIsDir(true);
278 sync_pb::EntitySpecifics specifics;
279 AddDefaultFieldValue(type, &specifics);
280 entry.PutSpecifics(specifics);
282 return true;
285 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string& folder) {
286 WriteTransaction trans(FROM_HERE, &share_);
287 syncable::Entry bookmark_root(trans.GetWrappedTrans(),
288 syncable::GET_TYPE_ROOT,
289 BOOKMARKS);
290 if (!bookmark_root.good())
291 return;
293 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
294 syncable::CREATE_NEW_UPDATE_ITEM,
295 syncable::Id::CreateFromServerId(folder));
296 if (!entry.good())
297 return;
299 entry.PutParentId(bookmark_root.GetId());
300 entry.PutBaseVersion(1);
301 entry.PutUniqueServerTag(folder);
302 entry.PutNonUniqueName(folder);
303 entry.PutIsDel(false);
304 entry.PutIsDir(true);
306 sync_pb::EntitySpecifics specifics;
307 AddDefaultFieldValue(BOOKMARKS, &specifics);
308 entry.PutSpecifics(specifics);
311 base::ObserverList<SyncManager::Observer>*
312 SyncRollbackManagerBase::GetObservers() {
313 return &observers_;
316 void SyncRollbackManagerBase::RegisterDirectoryTypeDebugInfoObserver(
317 syncer::TypeDebugInfoObserver* observer) {}
319 void SyncRollbackManagerBase::UnregisterDirectoryTypeDebugInfoObserver(
320 syncer::TypeDebugInfoObserver* observer) {}
322 bool SyncRollbackManagerBase::HasDirectoryTypeDebugInfoObserver(
323 syncer::TypeDebugInfoObserver* observer) { return false; }
325 void SyncRollbackManagerBase::RequestEmitDebugInfo() {}
327 } // namespace syncer