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/internal_components_factory.h"
9 #include "sync/internal_api/public/read_node.h"
10 #include "sync/internal_api/public/read_transaction.h"
11 #include "sync/internal_api/public/write_transaction.h"
12 #include "sync/syncable/directory_backing_store.h"
13 #include "sync/syncable/mutable_entry.h"
17 // Permanent bookmark folders as defined in bookmark_model_associator.cc.
18 // No mobile bookmarks because they only exists with sync enabled.
19 const char kBookmarkBarTag
[] = "bookmark_bar";
20 const char kOtherBookmarksTag
[] = "other_bookmarks";
22 class DummyEntryptionHandler
: public syncer::SyncEncryptionHandler
{
23 virtual void AddObserver(Observer
* observer
) OVERRIDE
{}
24 virtual void RemoveObserver(Observer
* observer
) OVERRIDE
{}
25 virtual void Init() OVERRIDE
{}
26 virtual void SetEncryptionPassphrase(const std::string
& passphrase
,
27 bool is_explicit
) OVERRIDE
{}
28 virtual void SetDecryptionPassphrase(const std::string
& passphrase
)
30 virtual void EnableEncryptEverything() OVERRIDE
{}
31 virtual bool EncryptEverythingEnabled() const OVERRIDE
{
34 virtual syncer::PassphraseType
GetPassphraseType() const OVERRIDE
{
35 return syncer::KEYSTORE_PASSPHRASE
;
39 } // anonymous namespace
43 SyncRollbackManagerBase::SyncRollbackManagerBase()
44 : report_unrecoverable_error_function_(NULL
),
45 weak_ptr_factory_(this),
46 dummy_handler_(new DummyEntryptionHandler
),
50 SyncRollbackManagerBase::~SyncRollbackManagerBase() {
53 bool SyncRollbackManagerBase::InitInternal(
54 const base::FilePath
& database_location
,
55 InternalComponentsFactory
* internal_components_factory
,
56 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
57 ReportUnrecoverableErrorFunction report_unrecoverable_error_function
) {
58 unrecoverable_error_handler_
= unrecoverable_error_handler
.Pass();
59 report_unrecoverable_error_function_
= report_unrecoverable_error_function
;
61 if (!InitBackupDB(database_location
, internal_components_factory
)) {
62 NotifyInitializationFailure();
67 NotifyInitializationSuccess();
71 ModelTypeSet
SyncRollbackManagerBase::InitialSyncEndedTypes() {
72 return share_
.directory
->InitialSyncEndedTypes();
75 ModelTypeSet
SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken(
77 ModelTypeSet inited_types
= share_
.directory
->InitialSyncEndedTypes();
78 types
.RemoveAll(inited_types
);
82 bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() {
87 void SyncRollbackManagerBase::UpdateCredentials(
88 const SyncCredentials
& credentials
) {
91 void SyncRollbackManagerBase::StartSyncingNormally(
92 const ModelSafeRoutingInfo
& routing_info
){
95 void SyncRollbackManagerBase::ConfigureSyncer(
96 ConfigureReason reason
,
97 ModelTypeSet to_download
,
98 ModelTypeSet to_purge
,
99 ModelTypeSet to_journal
,
100 ModelTypeSet to_unapply
,
101 const ModelSafeRoutingInfo
& new_routing_info
,
102 const base::Closure
& ready_task
,
103 const base::Closure
& retry_task
) {
104 for (ModelTypeSet::Iterator type
= to_download
.First();
105 type
.Good(); type
.Inc()) {
106 if (InitTypeRootNode(type
.Get())) {
107 if (type
.Get() == BOOKMARKS
) {
108 InitBookmarkFolder(kBookmarkBarTag
);
109 InitBookmarkFolder(kOtherBookmarksTag
);
117 void SyncRollbackManagerBase::SetInvalidatorEnabled(bool invalidator_enabled
) {
120 void SyncRollbackManagerBase::OnIncomingInvalidation(
121 syncer::ModelType type
,
122 scoped_ptr
<InvalidationInterface
> invalidation
) {
126 void SyncRollbackManagerBase::AddObserver(SyncManager::Observer
* observer
) {
127 observers_
.AddObserver(observer
);
130 void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer
* observer
) {
131 observers_
.RemoveObserver(observer
);
134 SyncStatus
SyncRollbackManagerBase::GetDetailedStatus() const {
138 void SyncRollbackManagerBase::SaveChanges() {
141 void SyncRollbackManagerBase::ShutdownOnSyncThread(ShutdownReason reason
) {
144 share_
.directory
->Close();
145 share_
.directory
.reset();
146 initialized_
= false;
150 UserShare
* SyncRollbackManagerBase::GetUserShare() {
154 const std::string
SyncRollbackManagerBase::cache_guid() {
155 return share_
.directory
->cache_guid();
158 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments
* experiments
) {
162 bool SyncRollbackManagerBase::HasUnsyncedItems() {
163 ReadTransaction
trans(FROM_HERE
, &share_
);
164 syncable::Directory::Metahandles unsynced
;
165 share_
.directory
->GetUnsyncedMetaHandles(trans
.GetWrappedTrans(), &unsynced
);
166 return !unsynced
.empty();
169 SyncEncryptionHandler
* SyncRollbackManagerBase::GetEncryptionHandler() {
170 return dummy_handler_
.get();
173 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types
) {
177 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent(
178 ModelTypeSet models_with_changes
) {
181 ModelTypeSet
SyncRollbackManagerBase::HandleTransactionEndingChangeEvent(
182 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
183 syncable::BaseTransaction
* trans
) {
184 return ModelTypeSet();
187 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi(
188 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
189 syncable::BaseTransaction
* trans
,
190 std::vector
<int64
>* entries_changed
) {
193 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer(
194 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
195 syncable::BaseTransaction
* trans
,
196 std::vector
<int64
>* entries_changed
) {
199 void SyncRollbackManagerBase::OnTransactionWrite(
200 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
201 ModelTypeSet models_with_changes
) {
204 void SyncRollbackManagerBase::NotifyInitializationSuccess() {
206 SyncManager::Observer
, observers_
,
207 OnInitializationComplete(
208 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
209 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
210 true, InitialSyncEndedTypes()));
213 void SyncRollbackManagerBase::NotifyInitializationFailure() {
215 SyncManager::Observer
, observers_
,
216 OnInitializationComplete(
217 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
218 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
219 false, ModelTypeSet()));
222 syncer::SyncContextProxy
* SyncRollbackManagerBase::GetSyncContextProxy() {
226 ScopedVector
<syncer::ProtocolEvent
>
227 SyncRollbackManagerBase::GetBufferedProtocolEvents() {
228 return ScopedVector
<syncer::ProtocolEvent
>().Pass();
231 scoped_ptr
<base::ListValue
> SyncRollbackManagerBase::GetAllNodesForType(
232 syncer::ModelType type
) {
233 ReadTransaction
trans(FROM_HERE
, GetUserShare());
234 scoped_ptr
<base::ListValue
> nodes(
235 trans
.GetDirectory()->GetNodeDetailsForType(trans
.GetWrappedTrans(),
240 bool SyncRollbackManagerBase::InitBackupDB(
241 const base::FilePath
& sync_folder
,
242 InternalComponentsFactory
* internal_components_factory
) {
243 base::FilePath backup_db_path
= sync_folder
.Append(
244 syncable::Directory::kSyncDatabaseFilename
);
245 scoped_ptr
<syncable::DirectoryBackingStore
> backing_store
=
246 internal_components_factory
->BuildDirectoryBackingStore(
247 "backup", backup_db_path
).Pass();
249 DCHECK(backing_store
.get());
250 share_
.directory
.reset(
251 new syncable::Directory(
252 backing_store
.release(),
253 unrecoverable_error_handler_
.get(),
254 report_unrecoverable_error_function_
,
257 return syncable::OPENED
==
258 share_
.directory
->Open(
260 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
263 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type
) {
264 WriteTransaction
trans(FROM_HERE
, &share_
);
265 ReadNode
root(&trans
);
266 if (BaseNode::INIT_OK
== root
.InitTypeRoot(type
))
269 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
270 syncable::CREATE_NEW_UPDATE_ITEM
,
271 syncable::Id::CreateFromServerId(
272 ModelTypeToString(type
)));
276 entry
.PutParentId(syncable::Id());
277 entry
.PutBaseVersion(1);
278 entry
.PutUniqueServerTag(ModelTypeToRootTag(type
));
279 entry
.PutNonUniqueName(ModelTypeToString(type
));
280 entry
.PutIsDel(false);
281 entry
.PutIsDir(true);
283 sync_pb::EntitySpecifics specifics
;
284 AddDefaultFieldValue(type
, &specifics
);
285 entry
.PutSpecifics(specifics
);
290 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string
& folder
) {
291 WriteTransaction
trans(FROM_HERE
, &share_
);
292 syncable::Entry
bookmark_root(trans
.GetWrappedTrans(),
293 syncable::GET_TYPE_ROOT
,
295 if (!bookmark_root
.good())
298 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
299 syncable::CREATE_NEW_UPDATE_ITEM
,
300 syncable::Id::CreateFromServerId(folder
));
304 entry
.PutParentId(bookmark_root
.GetId());
305 entry
.PutBaseVersion(1);
306 entry
.PutUniqueServerTag(folder
);
307 entry
.PutNonUniqueName(folder
);
308 entry
.PutIsDel(false);
309 entry
.PutIsDir(true);
311 sync_pb::EntitySpecifics specifics
;
312 AddDefaultFieldValue(BOOKMARKS
, &specifics
);
313 entry
.PutSpecifics(specifics
);
316 ObserverList
<SyncManager::Observer
>* SyncRollbackManagerBase::GetObservers() {
320 void SyncRollbackManagerBase::RegisterDirectoryTypeDebugInfoObserver(
321 syncer::TypeDebugInfoObserver
* observer
) {}
323 void SyncRollbackManagerBase::UnregisterDirectoryTypeDebugInfoObserver(
324 syncer::TypeDebugInfoObserver
* observer
) {}
326 bool SyncRollbackManagerBase::HasDirectoryTypeDebugInfoObserver(
327 syncer::TypeDebugInfoObserver
* observer
) { return false; }
329 void SyncRollbackManagerBase::RequestEmitDebugInfo() {}
331 } // namespace syncer