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"
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
39 SyncRollbackManagerBase::SyncRollbackManagerBase()
40 : report_unrecoverable_error_function_(NULL
),
41 dummy_handler_(new DummyEntryptionHandler
),
43 weak_ptr_factory_(this) {
46 SyncRollbackManagerBase::~SyncRollbackManagerBase() {
49 bool SyncRollbackManagerBase::InitInternal(
50 const base::FilePath
& database_location
,
51 InternalComponentsFactory
* internal_components_factory
,
52 InternalComponentsFactory::StorageOption storage
,
53 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
54 ReportUnrecoverableErrorFunction report_unrecoverable_error_function
) {
55 unrecoverable_error_handler_
= unrecoverable_error_handler
.Pass();
56 report_unrecoverable_error_function_
= report_unrecoverable_error_function
;
58 if (!InitBackupDB(database_location
, internal_components_factory
, storage
)) {
59 NotifyInitializationFailure();
64 NotifyInitializationSuccess();
68 ModelTypeSet
SyncRollbackManagerBase::InitialSyncEndedTypes() {
69 return share_
.directory
->InitialSyncEndedTypes();
72 ModelTypeSet
SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken(
74 ModelTypeSet inited_types
= share_
.directory
->InitialSyncEndedTypes();
75 types
.RemoveAll(inited_types
);
79 bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() {
84 void SyncRollbackManagerBase::UpdateCredentials(
85 const SyncCredentials
& credentials
) {
88 void SyncRollbackManagerBase::StartSyncingNormally(
89 const ModelSafeRoutingInfo
& routing_info
){
92 void SyncRollbackManagerBase::ConfigureSyncer(
93 ConfigureReason reason
,
94 ModelTypeSet to_download
,
95 ModelTypeSet to_purge
,
96 ModelTypeSet to_journal
,
97 ModelTypeSet to_unapply
,
98 const ModelSafeRoutingInfo
& new_routing_info
,
99 const base::Closure
& ready_task
,
100 const base::Closure
& retry_task
) {
101 for (ModelTypeSet::Iterator type
= to_download
.First();
102 type
.Good(); type
.Inc()) {
103 if (InitTypeRootNode(type
.Get())) {
104 if (type
.Get() == BOOKMARKS
) {
105 InitBookmarkFolder(kBookmarkBarTag
);
106 InitBookmarkFolder(kOtherBookmarksTag
);
114 void SyncRollbackManagerBase::SetInvalidatorEnabled(bool invalidator_enabled
) {
117 void SyncRollbackManagerBase::OnIncomingInvalidation(
118 syncer::ModelType type
,
119 scoped_ptr
<InvalidationInterface
> invalidation
) {
123 void SyncRollbackManagerBase::AddObserver(SyncManager::Observer
* observer
) {
124 observers_
.AddObserver(observer
);
127 void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer
* observer
) {
128 observers_
.RemoveObserver(observer
);
131 SyncStatus
SyncRollbackManagerBase::GetDetailedStatus() const {
135 void SyncRollbackManagerBase::SaveChanges() {
138 void SyncRollbackManagerBase::ShutdownOnSyncThread(ShutdownReason reason
) {
140 share_
.directory
->Close();
141 share_
.directory
.reset();
142 initialized_
= false;
146 UserShare
* SyncRollbackManagerBase::GetUserShare() {
150 const std::string
SyncRollbackManagerBase::cache_guid() {
151 return share_
.directory
->cache_guid();
154 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments
* experiments
) {
158 bool SyncRollbackManagerBase::HasUnsyncedItems() {
159 ReadTransaction
trans(FROM_HERE
, &share_
);
160 syncable::Directory::Metahandles unsynced
;
161 share_
.directory
->GetUnsyncedMetaHandles(trans
.GetWrappedTrans(), &unsynced
);
162 return !unsynced
.empty();
165 SyncEncryptionHandler
* SyncRollbackManagerBase::GetEncryptionHandler() {
166 return dummy_handler_
.get();
169 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types
) {
173 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent(
174 ModelTypeSet models_with_changes
) {
177 ModelTypeSet
SyncRollbackManagerBase::HandleTransactionEndingChangeEvent(
178 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
179 syncable::BaseTransaction
* trans
) {
180 return ModelTypeSet();
183 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi(
184 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
185 syncable::BaseTransaction
* trans
,
186 std::vector
<int64
>* entries_changed
) {
189 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer(
190 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
191 syncable::BaseTransaction
* trans
,
192 std::vector
<int64
>* entries_changed
) {
195 void SyncRollbackManagerBase::OnTransactionWrite(
196 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
197 ModelTypeSet models_with_changes
) {
200 void SyncRollbackManagerBase::NotifyInitializationSuccess() {
202 SyncManager::Observer
, observers_
,
203 OnInitializationComplete(
204 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
205 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
206 true, InitialSyncEndedTypes()));
209 void SyncRollbackManagerBase::NotifyInitializationFailure() {
211 SyncManager::Observer
, observers_
,
212 OnInitializationComplete(
213 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
214 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
215 false, ModelTypeSet()));
218 syncer::SyncContextProxy
* SyncRollbackManagerBase::GetSyncContextProxy() {
222 ScopedVector
<syncer::ProtocolEvent
>
223 SyncRollbackManagerBase::GetBufferedProtocolEvents() {
224 return ScopedVector
<syncer::ProtocolEvent
>().Pass();
227 scoped_ptr
<base::ListValue
> SyncRollbackManagerBase::GetAllNodesForType(
228 syncer::ModelType type
) {
229 ReadTransaction
trans(FROM_HERE
, GetUserShare());
230 scoped_ptr
<base::ListValue
> nodes(
231 trans
.GetDirectory()->GetNodeDetailsForType(trans
.GetWrappedTrans(),
236 bool SyncRollbackManagerBase::InitBackupDB(
237 const base::FilePath
& sync_folder
,
238 InternalComponentsFactory
* internal_components_factory
,
239 InternalComponentsFactory::StorageOption storage
) {
240 base::FilePath backup_db_path
= sync_folder
.Append(
241 syncable::Directory::kSyncDatabaseFilename
);
242 scoped_ptr
<syncable::DirectoryBackingStore
> backing_store
=
243 internal_components_factory
->BuildDirectoryBackingStore(
244 storage
, "backup", backup_db_path
).Pass();
246 DCHECK(backing_store
.get());
247 share_
.directory
.reset(
248 new syncable::Directory(
249 backing_store
.release(),
250 unrecoverable_error_handler_
.get(),
251 report_unrecoverable_error_function_
,
254 return syncable::OPENED
==
255 share_
.directory
->Open(
257 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
260 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type
) {
261 WriteTransaction
trans(FROM_HERE
, &share_
);
262 ReadNode
root(&trans
);
263 if (BaseNode::INIT_OK
== root
.InitTypeRoot(type
))
266 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
267 syncable::CREATE_NEW_UPDATE_ITEM
,
268 syncable::Id::CreateFromServerId(
269 ModelTypeToString(type
)));
273 entry
.PutParentId(syncable::Id::GetRoot());
274 entry
.PutBaseVersion(1);
275 entry
.PutUniqueServerTag(ModelTypeToRootTag(type
));
276 entry
.PutNonUniqueName(ModelTypeToString(type
));
277 entry
.PutIsDel(false);
278 entry
.PutIsDir(true);
280 sync_pb::EntitySpecifics specifics
;
281 AddDefaultFieldValue(type
, &specifics
);
282 entry
.PutSpecifics(specifics
);
287 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string
& folder
) {
288 WriteTransaction
trans(FROM_HERE
, &share_
);
289 syncable::Entry
bookmark_root(trans
.GetWrappedTrans(),
290 syncable::GET_TYPE_ROOT
,
292 if (!bookmark_root
.good())
295 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
296 syncable::CREATE_NEW_UPDATE_ITEM
,
297 syncable::Id::CreateFromServerId(folder
));
301 entry
.PutParentId(bookmark_root
.GetId());
302 entry
.PutBaseVersion(1);
303 entry
.PutUniqueServerTag(folder
);
304 entry
.PutNonUniqueName(folder
);
305 entry
.PutIsDel(false);
306 entry
.PutIsDir(true);
308 sync_pb::EntitySpecifics specifics
;
309 AddDefaultFieldValue(BOOKMARKS
, &specifics
);
310 entry
.PutSpecifics(specifics
);
313 ObserverList
<SyncManager::Observer
>* SyncRollbackManagerBase::GetObservers() {
317 void SyncRollbackManagerBase::RegisterDirectoryTypeDebugInfoObserver(
318 syncer::TypeDebugInfoObserver
* observer
) {}
320 void SyncRollbackManagerBase::UnregisterDirectoryTypeDebugInfoObserver(
321 syncer::TypeDebugInfoObserver
* observer
) {}
323 bool SyncRollbackManagerBase::HasDirectoryTypeDebugInfoObserver(
324 syncer::TypeDebugInfoObserver
* observer
) { return false; }
326 void SyncRollbackManagerBase::RequestEmitDebugInfo() {}
328 } // namespace syncer