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
, base::Time last_poll_time
){
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
.reset();
141 initialized_
= false;
145 UserShare
* SyncRollbackManagerBase::GetUserShare() {
149 const std::string
SyncRollbackManagerBase::cache_guid() {
150 return share_
.directory
->cache_guid();
153 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments
* experiments
) {
157 bool SyncRollbackManagerBase::HasUnsyncedItems() {
158 ReadTransaction
trans(FROM_HERE
, &share_
);
159 syncable::Directory::Metahandles unsynced
;
160 share_
.directory
->GetUnsyncedMetaHandles(trans
.GetWrappedTrans(), &unsynced
);
161 return !unsynced
.empty();
164 SyncEncryptionHandler
* SyncRollbackManagerBase::GetEncryptionHandler() {
165 return dummy_handler_
.get();
168 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types
) {
172 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent(
173 ModelTypeSet models_with_changes
) {
176 ModelTypeSet
SyncRollbackManagerBase::HandleTransactionEndingChangeEvent(
177 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
178 syncable::BaseTransaction
* trans
) {
179 return ModelTypeSet();
182 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi(
183 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
184 syncable::BaseTransaction
* trans
,
185 std::vector
<int64
>* entries_changed
) {
188 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer(
189 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
190 syncable::BaseTransaction
* trans
,
191 std::vector
<int64
>* entries_changed
) {
194 void SyncRollbackManagerBase::OnTransactionWrite(
195 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
196 ModelTypeSet models_with_changes
) {
199 void SyncRollbackManagerBase::NotifyInitializationSuccess() {
201 SyncManager::Observer
, observers_
,
202 OnInitializationComplete(
203 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
204 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
205 true, InitialSyncEndedTypes()));
208 void SyncRollbackManagerBase::NotifyInitializationFailure() {
210 SyncManager::Observer
, observers_
,
211 OnInitializationComplete(
212 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
213 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
214 false, ModelTypeSet()));
217 syncer::SyncContextProxy
* SyncRollbackManagerBase::GetSyncContextProxy() {
221 ScopedVector
<syncer::ProtocolEvent
>
222 SyncRollbackManagerBase::GetBufferedProtocolEvents() {
223 return ScopedVector
<syncer::ProtocolEvent
>().Pass();
226 scoped_ptr
<base::ListValue
> SyncRollbackManagerBase::GetAllNodesForType(
227 syncer::ModelType type
) {
228 ReadTransaction
trans(FROM_HERE
, GetUserShare());
229 scoped_ptr
<base::ListValue
> nodes(
230 trans
.GetDirectory()->GetNodeDetailsForType(trans
.GetWrappedTrans(),
235 bool SyncRollbackManagerBase::InitBackupDB(
236 const base::FilePath
& sync_folder
,
237 InternalComponentsFactory
* internal_components_factory
,
238 InternalComponentsFactory::StorageOption storage
) {
239 base::FilePath backup_db_path
= sync_folder
.Append(
240 syncable::Directory::kSyncDatabaseFilename
);
241 scoped_ptr
<syncable::DirectoryBackingStore
> backing_store
=
242 internal_components_factory
->BuildDirectoryBackingStore(
243 storage
, "backup", backup_db_path
).Pass();
245 DCHECK(backing_store
.get());
246 share_
.directory
.reset(
247 new syncable::Directory(
248 backing_store
.release(),
249 unrecoverable_error_handler_
.get(),
250 report_unrecoverable_error_function_
,
253 return syncable::OPENED
==
254 share_
.directory
->Open(
256 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
259 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type
) {
260 WriteTransaction
trans(FROM_HERE
, &share_
);
261 ReadNode
root(&trans
);
262 if (BaseNode::INIT_OK
== root
.InitTypeRoot(type
))
265 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
266 syncable::CREATE_NEW_UPDATE_ITEM
,
267 syncable::Id::CreateFromServerId(
268 ModelTypeToString(type
)));
272 entry
.PutParentId(syncable::Id::GetRoot());
273 entry
.PutBaseVersion(1);
274 entry
.PutUniqueServerTag(ModelTypeToRootTag(type
));
275 entry
.PutNonUniqueName(ModelTypeToString(type
));
276 entry
.PutIsDel(false);
277 entry
.PutIsDir(true);
279 sync_pb::EntitySpecifics specifics
;
280 AddDefaultFieldValue(type
, &specifics
);
281 entry
.PutSpecifics(specifics
);
286 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string
& folder
) {
287 WriteTransaction
trans(FROM_HERE
, &share_
);
288 syncable::Entry
bookmark_root(trans
.GetWrappedTrans(),
289 syncable::GET_TYPE_ROOT
,
291 if (!bookmark_root
.good())
294 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
295 syncable::CREATE_NEW_UPDATE_ITEM
,
296 syncable::Id::CreateFromServerId(folder
));
300 entry
.PutParentId(bookmark_root
.GetId());
301 entry
.PutBaseVersion(1);
302 entry
.PutUniqueServerTag(folder
);
303 entry
.PutNonUniqueName(folder
);
304 entry
.PutIsDel(false);
305 entry
.PutIsDir(true);
307 sync_pb::EntitySpecifics specifics
;
308 AddDefaultFieldValue(BOOKMARKS
, &specifics
);
309 entry
.PutSpecifics(specifics
);
312 ObserverList
<SyncManager::Observer
>* SyncRollbackManagerBase::GetObservers() {
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