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_backup_manager.h"
7 #include "sync/internal_api/public/read_node.h"
8 #include "sync/internal_api/public/write_transaction.h"
9 #include "sync/syncable/directory.h"
10 #include "sync/syncable/mutable_entry.h"
15 SyncBackupManager::SyncBackupManager()
16 : in_normalization_(false) {
19 SyncBackupManager::~SyncBackupManager() {
22 void SyncBackupManager::Init(
23 const base::FilePath
& database_location
,
24 const WeakHandle
<JsEventHandler
>& event_handler
,
25 const GURL
& service_url
,
26 scoped_ptr
<HttpPostProviderFactory
> post_factory
,
27 const std::vector
<scoped_refptr
<ModelSafeWorker
> >& workers
,
28 ExtensionsActivity
* extensions_activity
,
29 SyncManager::ChangeDelegate
* change_delegate
,
30 const SyncCredentials
& credentials
,
31 const std::string
& invalidator_client_id
,
32 const std::string
& restored_key_for_bootstrapping
,
33 const std::string
& restored_keystore_key_for_bootstrapping
,
34 InternalComponentsFactory
* internal_components_factory
,
36 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
37 ReportUnrecoverableErrorFunction report_unrecoverable_error_function
,
38 CancelationSignal
* cancelation_signal
) {
39 if (SyncRollbackManagerBase::InitInternal(
41 internal_components_factory
,
42 unrecoverable_error_handler
.Pass(),
43 report_unrecoverable_error_function
)) {
44 GetUserShare()->directory
->CollectMetaHandleCounts(
45 &status_
.num_entries_by_type
, &status_
.num_to_delete_entries_by_type
);
47 HideSyncPreference(PRIORITY_PREFERENCES
);
48 HideSyncPreference(PREFERENCES
);
52 void SyncBackupManager::SaveChanges() {
55 GetUserShare()->directory
->SaveChanges();
59 SyncStatus
SyncBackupManager::GetDetailedStatus() const {
63 ModelTypeSet
SyncBackupManager::HandleTransactionEndingChangeEvent(
64 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
65 syncable::BaseTransaction
* trans
) {
67 if (in_normalization_
) {
68 // Skip if in our own WriteTransaction from NormalizeEntries().
69 in_normalization_
= false;
73 for (syncable::EntryKernelMutationMap::const_iterator it
=
74 write_transaction_info
.Get().mutations
.Get().begin();
75 it
!= write_transaction_info
.Get().mutations
.Get().end(); ++it
) {
77 if (unsynced_
.find(id
) == unsynced_
.end()) {
80 const syncable::EntryKernel
& e
= it
->second
.mutated
;
81 ModelType type
= e
.GetModelType();
83 if (!e
.ref(syncable::ID
).ServerKnows())
84 status_
.num_entries_by_type
[type
]++;
85 if (e
.ref(syncable::IS_DEL
))
86 status_
.num_to_delete_entries_by_type
[type
]++;
92 void SyncBackupManager::NormalizeEntries() {
93 WriteTransaction
trans(FROM_HERE
, GetUserShare());
94 in_normalization_
= true;
95 for (std::set
<int64
>::const_iterator it
= unsynced_
.begin();
96 it
!= unsynced_
.end(); ++it
) {
97 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
98 syncable::GET_BY_HANDLE
, *it
);
101 if (!entry
.GetId().ServerKnows())
102 entry
.PutId(syncable::Id::CreateFromServerId(entry
.GetId().value()));
103 if (!entry
.GetParentId().ServerKnows()) {
104 entry
.PutParentIdPropertyOnly(syncable::Id::CreateFromServerId(
105 entry
.GetParentId().value()));
107 entry
.PutBaseVersion(1);
108 entry
.PutIsUnsynced(false);
113 void SyncBackupManager::HideSyncPreference(ModelType type
) {
114 WriteTransaction
trans(FROM_HERE
, GetUserShare());
115 ReadNode
pref_root(&trans
);
116 if (BaseNode::INIT_OK
!= pref_root
.InitTypeRoot(type
))
119 std::vector
<int64
> pref_ids
;
120 pref_root
.GetChildIds(&pref_ids
);
121 for (uint32 i
= 0; i
< pref_ids
.size(); ++i
) {
122 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
123 syncable::GET_BY_HANDLE
, pref_ids
[i
]);
125 // HACKY: Set IS_DEL to true to remove entry from parent-children
126 // index so that it's not returned when syncable service asks
127 // for sync data. Syncable service then creates entry for local
128 // model. Then the existing entry is undeleted and set to local value
129 // because it has the same unique client tag.
130 entry
.PutIsDel(true);
131 entry
.PutIsUnsynced(false);
133 // Don't persist on disk so that if backup is aborted before receiving
134 // local preference values, values in sync DB are saved.
135 GetUserShare()->directory
->UnmarkDirtyEntry(
136 trans
.GetWrappedWriteTrans(), &entry
);
141 void SyncBackupManager::RegisterDirectoryTypeDebugInfoObserver(
142 syncer::TypeDebugInfoObserver
* observer
) {}
144 void SyncBackupManager::UnregisterDirectoryTypeDebugInfoObserver(
145 syncer::TypeDebugInfoObserver
* observer
) {}
147 bool SyncBackupManager::HasDirectoryTypeDebugInfoObserver(
148 syncer::TypeDebugInfoObserver
* observer
) { return false; }
150 void SyncBackupManager::RequestEmitDebugInfo() {}
152 } // namespace syncer