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 void SyncRollbackManagerBase::Init(
54 const base::FilePath
& database_location
,
55 const WeakHandle
<JsEventHandler
>& event_handler
,
56 const std::string
& sync_server_and_path
,
59 scoped_ptr
<HttpPostProviderFactory
> post_factory
,
60 const std::vector
<scoped_refptr
<ModelSafeWorker
> >& workers
,
61 ExtensionsActivity
* extensions_activity
,
62 SyncManager::ChangeDelegate
* change_delegate
,
63 const SyncCredentials
& credentials
,
64 const std::string
& invalidator_client_id
,
65 const std::string
& restored_key_for_bootstrapping
,
66 const std::string
& restored_keystore_key_for_bootstrapping
,
67 InternalComponentsFactory
* internal_components_factory
,
69 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
70 ReportUnrecoverableErrorFunction
71 report_unrecoverable_error_function
,
72 CancelationSignal
* cancelation_signal
) {
73 unrecoverable_error_handler_
= unrecoverable_error_handler
.Pass();
74 report_unrecoverable_error_function_
= report_unrecoverable_error_function
;
76 if (!InitBackupDB(database_location
, internal_components_factory
)) {
77 NotifyInitializationFailure();
82 NotifyInitializationSuccess();
85 ModelTypeSet
SyncRollbackManagerBase::InitialSyncEndedTypes() {
86 return share_
.directory
->InitialSyncEndedTypes();
89 ModelTypeSet
SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken(
91 ModelTypeSet inited_types
= share_
.directory
->InitialSyncEndedTypes();
92 types
.RemoveAll(inited_types
);
96 bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() {
101 void SyncRollbackManagerBase::UpdateCredentials(
102 const SyncCredentials
& credentials
) {
105 void SyncRollbackManagerBase::StartSyncingNormally(
106 const ModelSafeRoutingInfo
& routing_info
){
109 void SyncRollbackManagerBase::ConfigureSyncer(
110 ConfigureReason reason
,
111 ModelTypeSet to_download
,
112 ModelTypeSet to_purge
,
113 ModelTypeSet to_journal
,
114 ModelTypeSet to_unapply
,
115 const ModelSafeRoutingInfo
& new_routing_info
,
116 const base::Closure
& ready_task
,
117 const base::Closure
& retry_task
) {
118 for (ModelTypeSet::Iterator type
= to_download
.First();
119 type
.Good(); type
.Inc()) {
120 if (InitTypeRootNode(type
.Get())) {
121 if (type
.Get() == BOOKMARKS
) {
122 InitBookmarkFolder(kBookmarkBarTag
);
123 InitBookmarkFolder(kOtherBookmarksTag
);
131 void SyncRollbackManagerBase::SetInvalidatorEnabled(bool invalidator_enabled
) {
134 void SyncRollbackManagerBase::OnIncomingInvalidation(
135 syncer::ModelType type
,
136 scoped_ptr
<InvalidationInterface
> invalidation
) {
140 void SyncRollbackManagerBase::AddObserver(SyncManager::Observer
* observer
) {
141 observers_
.AddObserver(observer
);
144 void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer
* observer
) {
145 observers_
.RemoveObserver(observer
);
148 SyncStatus
SyncRollbackManagerBase::GetDetailedStatus() const {
152 void SyncRollbackManagerBase::SaveChanges() {
155 void SyncRollbackManagerBase::ShutdownOnSyncThread() {
158 share_
.directory
->Close();
159 share_
.directory
.reset();
160 initialized_
= false;
164 UserShare
* SyncRollbackManagerBase::GetUserShare() {
168 const std::string
SyncRollbackManagerBase::cache_guid() {
169 return share_
.directory
->cache_guid();
172 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments
* experiments
) {
176 bool SyncRollbackManagerBase::HasUnsyncedItems() {
177 ReadTransaction
trans(FROM_HERE
, &share_
);
178 syncable::Directory::Metahandles unsynced
;
179 share_
.directory
->GetUnsyncedMetaHandles(trans
.GetWrappedTrans(), &unsynced
);
180 return !unsynced
.empty();
183 SyncEncryptionHandler
* SyncRollbackManagerBase::GetEncryptionHandler() {
184 return dummy_handler_
.get();
187 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types
) {
191 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent(
192 ModelTypeSet models_with_changes
) {
195 ModelTypeSet
SyncRollbackManagerBase::HandleTransactionEndingChangeEvent(
196 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
197 syncable::BaseTransaction
* trans
) {
198 return ModelTypeSet();
201 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi(
202 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
203 syncable::BaseTransaction
* trans
,
204 std::vector
<int64
>* entries_changed
) {
207 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer(
208 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
209 syncable::BaseTransaction
* trans
,
210 std::vector
<int64
>* entries_changed
) {
213 void SyncRollbackManagerBase::OnTransactionWrite(
214 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
215 ModelTypeSet models_with_changes
) {
218 void SyncRollbackManagerBase::NotifyInitializationSuccess() {
220 SyncManager::Observer
, observers_
,
221 OnInitializationComplete(
222 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
223 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
224 true, InitialSyncEndedTypes()));
227 void SyncRollbackManagerBase::NotifyInitializationFailure() {
229 SyncManager::Observer
, observers_
,
230 OnInitializationComplete(
231 MakeWeakHandle(base::WeakPtr
<JsBackend
>()),
232 MakeWeakHandle(base::WeakPtr
<DataTypeDebugInfoListener
>()),
233 false, ModelTypeSet()));
236 syncer::SyncContextProxy
* SyncRollbackManagerBase::GetSyncContextProxy() {
240 ScopedVector
<syncer::ProtocolEvent
>
241 SyncRollbackManagerBase::GetBufferedProtocolEvents() {
242 return ScopedVector
<syncer::ProtocolEvent
>().Pass();
245 scoped_ptr
<base::ListValue
> SyncRollbackManagerBase::GetAllNodesForType(
246 syncer::ModelType type
) {
247 ReadTransaction
trans(FROM_HERE
, GetUserShare());
248 scoped_ptr
<base::ListValue
> nodes(
249 trans
.GetDirectory()->GetNodeDetailsForType(trans
.GetWrappedTrans(),
254 bool SyncRollbackManagerBase::InitBackupDB(
255 const base::FilePath
& sync_folder
,
256 InternalComponentsFactory
* internal_components_factory
) {
257 base::FilePath backup_db_path
= sync_folder
.Append(
258 syncable::Directory::kSyncDatabaseFilename
);
259 scoped_ptr
<syncable::DirectoryBackingStore
> backing_store
=
260 internal_components_factory
->BuildDirectoryBackingStore(
261 "backup", backup_db_path
).Pass();
263 DCHECK(backing_store
.get());
264 share_
.directory
.reset(
265 new syncable::Directory(
266 backing_store
.release(),
267 unrecoverable_error_handler_
.get(),
268 report_unrecoverable_error_function_
,
271 return syncable::OPENED
==
272 share_
.directory
->Open(
274 MakeWeakHandle(weak_ptr_factory_
.GetWeakPtr()));
277 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type
) {
278 WriteTransaction
trans(FROM_HERE
, &share_
);
279 ReadNode
root(&trans
);
280 if (BaseNode::INIT_OK
== root
.InitTypeRoot(type
))
283 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
284 syncable::CREATE_NEW_UPDATE_ITEM
,
285 syncable::Id::CreateFromServerId(
286 ModelTypeToString(type
)));
290 entry
.PutParentId(syncable::Id());
291 entry
.PutBaseVersion(1);
292 entry
.PutUniqueServerTag(ModelTypeToRootTag(type
));
293 entry
.PutNonUniqueName(ModelTypeToString(type
));
294 entry
.PutIsDel(false);
295 entry
.PutIsDir(true);
297 sync_pb::EntitySpecifics specifics
;
298 AddDefaultFieldValue(type
, &specifics
);
299 entry
.PutSpecifics(specifics
);
304 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string
& folder
) {
305 WriteTransaction
trans(FROM_HERE
, &share_
);
306 syncable::Entry
bookmark_root(trans
.GetWrappedTrans(),
307 syncable::GET_TYPE_ROOT
,
309 if (!bookmark_root
.good())
312 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
313 syncable::CREATE_NEW_UPDATE_ITEM
,
314 syncable::Id::CreateFromServerId(folder
));
318 entry
.PutParentId(bookmark_root
.GetId());
319 entry
.PutBaseVersion(1);
320 entry
.PutUniqueServerTag(folder
);
321 entry
.PutNonUniqueName(folder
);
322 entry
.PutIsDel(false);
323 entry
.PutIsDir(true);
325 sync_pb::EntitySpecifics specifics
;
326 AddDefaultFieldValue(BOOKMARKS
, &specifics
);
327 entry
.PutSpecifics(specifics
);
330 ObserverList
<SyncManager::Observer
>* SyncRollbackManagerBase::GetObservers() {
334 void SyncRollbackManagerBase::RegisterDirectoryTypeDebugInfoObserver(
335 syncer::TypeDebugInfoObserver
* observer
) {}
337 void SyncRollbackManagerBase::UnregisterDirectoryTypeDebugInfoObserver(
338 syncer::TypeDebugInfoObserver
* observer
) {}
340 bool SyncRollbackManagerBase::HasDirectoryTypeDebugInfoObserver(
341 syncer::TypeDebugInfoObserver
* observer
) { return false; }
343 void SyncRollbackManagerBase::RequestEmitDebugInfo() {}
345 } // namespace syncer