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"
14 SyncBackupManager::SyncBackupManager()
15 : in_normalization_(false) {
18 SyncBackupManager::~SyncBackupManager() {
21 void SyncBackupManager::Init(
22 const base::FilePath& database_location,
23 const WeakHandle<JsEventHandler>& event_handler,
24 const std::string& sync_server_and_path,
27 scoped_ptr<HttpPostProviderFactory> post_factory,
28 const std::vector<scoped_refptr<ModelSafeWorker> >& workers,
29 ExtensionsActivity* extensions_activity,
30 SyncManager::ChangeDelegate* change_delegate,
31 const SyncCredentials& credentials,
32 const std::string& invalidator_client_id,
33 const std::string& restored_key_for_bootstrapping,
34 const std::string& restored_keystore_key_for_bootstrapping,
35 InternalComponentsFactory* internal_components_factory,
37 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
38 ReportUnrecoverableErrorFunction
39 report_unrecoverable_error_function,
40 CancelationSignal* cancelation_signal) {
41 if (SyncRollbackManagerBase::InitInternal(
43 internal_components_factory,
44 unrecoverable_error_handler.Pass(),
45 report_unrecoverable_error_function)) {
46 GetUserShare()->directory->CollectMetaHandleCounts(
47 &status_.num_entries_by_type, &status_.num_to_delete_entries_by_type);
49 HideSyncPreference(PRIORITY_PREFERENCES);
50 HideSyncPreference(PREFERENCES);
54 void SyncBackupManager::SaveChanges() {
57 GetUserShare()->directory->SaveChanges();
61 SyncStatus SyncBackupManager::GetDetailedStatus() const {
65 ModelTypeSet SyncBackupManager::HandleTransactionEndingChangeEvent(
66 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
67 syncable::BaseTransaction* trans) {
69 if (in_normalization_) {
70 // Skip if in our own WriteTransaction from NormalizeEntries().
71 in_normalization_ = false;
75 for (syncable::EntryKernelMutationMap::const_iterator it =
76 write_transaction_info.Get().mutations.Get().begin();
77 it != write_transaction_info.Get().mutations.Get().end(); ++it) {
79 if (unsynced_.find(id) == unsynced_.end()) {
82 const syncable::EntryKernel& e = it->second.mutated;
83 ModelType type = e.GetModelType();
85 if (!e.ref(syncable::ID).ServerKnows())
86 status_.num_entries_by_type[type]++;
87 if (e.ref(syncable::IS_DEL))
88 status_.num_to_delete_entries_by_type[type]++;
94 void SyncBackupManager::NormalizeEntries() {
95 WriteTransaction trans(FROM_HERE, GetUserShare());
96 in_normalization_ = true;
97 for (std::set<int64>::const_iterator it = unsynced_.begin();
98 it != unsynced_.end(); ++it) {
99 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
100 syncable::GET_BY_HANDLE, *it);
103 if (!entry.GetId().ServerKnows())
104 entry.PutId(syncable::Id::CreateFromServerId(entry.GetId().value()));
105 if (!entry.GetParentId().ServerKnows()) {
106 entry.PutParentIdPropertyOnly(syncable::Id::CreateFromServerId(
107 entry.GetParentId().value()));
109 entry.PutBaseVersion(1);
110 entry.PutIsUnsynced(false);
115 void SyncBackupManager::HideSyncPreference(ModelType type) {
116 WriteTransaction trans(FROM_HERE, GetUserShare());
117 ReadNode pref_root(&trans);
118 if (BaseNode::INIT_OK != pref_root.InitTypeRoot(type))
121 std::vector<int64> pref_ids;
122 pref_root.GetChildIds(&pref_ids);
123 for (uint32 i = 0; i < pref_ids.size(); ++i) {
124 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
125 syncable::GET_BY_HANDLE, pref_ids[i]);
127 // HACKY: Set IS_DEL to true to remove entry from parent-children
128 // index so that it's not returned when syncable service asks
129 // for sync data. Syncable service then creates entry for local
130 // model. Then the existing entry is undeleted and set to local value
131 // because it has the same unique client tag.
132 entry.PutIsDel(true);
133 entry.PutIsUnsynced(false);
135 // Don't persist on disk so that if backup is aborted before receiving
136 // local preference values, values in sync DB are saved.
137 GetUserShare()->directory->UnmarkDirtyEntry(
138 trans.GetWrappedWriteTrans(), &entry);
143 void SyncBackupManager::RegisterDirectoryTypeDebugInfoObserver(
144 syncer::TypeDebugInfoObserver* observer) {}
146 void SyncBackupManager::UnregisterDirectoryTypeDebugInfoObserver(
147 syncer::TypeDebugInfoObserver* observer) {}
149 bool SyncBackupManager::HasDirectoryTypeDebugInfoObserver(
150 syncer::TypeDebugInfoObserver* observer) { return false; }
152 void SyncBackupManager::RequestEmitDebugInfo() {}
154 } // namespace syncer