[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / sync / internal_api / sync_backup_manager.cc
bloba8a377e21770ca2684d6b7d99881070db1592ea2
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"
12 namespace syncer {
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,
25 int sync_server_port,
26 bool use_ssl,
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,
36 Encryptor* encryptor,
37 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
38 ReportUnrecoverableErrorFunction
39 report_unrecoverable_error_function,
40 CancelationSignal* cancelation_signal) {
41 SyncRollbackManagerBase::Init(database_location, event_handler,
42 sync_server_and_path, sync_server_port,
43 use_ssl, post_factory.Pass(),
44 workers, extensions_activity, change_delegate,
45 credentials, invalidator_client_id,
46 restored_key_for_bootstrapping,
47 restored_keystore_key_for_bootstrapping,
48 internal_components_factory, encryptor,
49 unrecoverable_error_handler.Pass(),
50 report_unrecoverable_error_function,
51 cancelation_signal);
53 if (!initialized())
54 return;
56 GetUserShare()->directory->CollectMetaHandleCounts(
57 &status_.num_entries_by_type,
58 &status_.num_to_delete_entries_by_type);
60 HideSyncPreference(PRIORITY_PREFERENCES);
61 HideSyncPreference(PREFERENCES);
64 void SyncBackupManager::SaveChanges() {
65 if (initialized()) {
66 NormalizeEntries();
67 GetUserShare()->directory->SaveChanges();
71 SyncStatus SyncBackupManager::GetDetailedStatus() const {
72 return status_;
75 ModelTypeSet SyncBackupManager::HandleTransactionEndingChangeEvent(
76 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
77 syncable::BaseTransaction* trans) {
78 ModelTypeSet types;
79 if (in_normalization_) {
80 // Skip if in our own WriteTransaction from NormalizeEntries().
81 in_normalization_ = false;
82 return types;
85 for (syncable::EntryKernelMutationMap::const_iterator it =
86 write_transaction_info.Get().mutations.Get().begin();
87 it != write_transaction_info.Get().mutations.Get().end(); ++it) {
88 int64 id = it->first;
89 if (unsynced_.find(id) == unsynced_.end()) {
90 unsynced_.insert(id);
92 const syncable::EntryKernel& e = it->second.mutated;
93 ModelType type = e.GetModelType();
94 types.Put(type);
95 if (!e.ref(syncable::ID).ServerKnows())
96 status_.num_entries_by_type[type]++;
97 if (e.ref(syncable::IS_DEL))
98 status_.num_to_delete_entries_by_type[type]++;
101 return types;
104 void SyncBackupManager::NormalizeEntries() {
105 WriteTransaction trans(FROM_HERE, GetUserShare());
106 in_normalization_ = true;
107 for (std::set<int64>::const_iterator it = unsynced_.begin();
108 it != unsynced_.end(); ++it) {
109 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
110 syncable::GET_BY_HANDLE, *it);
111 CHECK(entry.good());
113 if (!entry.GetId().ServerKnows())
114 entry.PutId(syncable::Id::CreateFromServerId(entry.GetId().value()));
115 if (!entry.GetParentId().ServerKnows()) {
116 entry.PutParentIdPropertyOnly(syncable::Id::CreateFromServerId(
117 entry.GetParentId().value()));
119 entry.PutBaseVersion(1);
120 entry.PutIsUnsynced(false);
122 unsynced_.clear();
125 void SyncBackupManager::HideSyncPreference(ModelType type) {
126 WriteTransaction trans(FROM_HERE, GetUserShare());
127 ReadNode pref_root(&trans);
128 if (BaseNode::INIT_OK != pref_root.InitTypeRoot(type))
129 return;
131 std::vector<int64> pref_ids;
132 pref_root.GetChildIds(&pref_ids);
133 for (uint32 i = 0; i < pref_ids.size(); ++i) {
134 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
135 syncable::GET_BY_HANDLE, pref_ids[i]);
136 if (entry.good()) {
137 // HACKY: Set IS_DEL to true to remove entry from parent-children
138 // index so that it's not returned when syncable service asks
139 // for sync data. Syncable service then creates entry for local
140 // model. Then the existing entry is undeleted and set to local value
141 // because it has the same unique client tag.
142 entry.PutIsDel(true);
143 entry.PutIsUnsynced(false);
145 // Don't persist on disk so that if backup is aborted before receiving
146 // local preference values, values in sync DB are saved.
147 GetUserShare()->directory->UnmarkDirtyEntry(
148 trans.GetWrappedWriteTrans(), &entry);
153 void SyncBackupManager::RegisterDirectoryTypeDebugInfoObserver(
154 syncer::TypeDebugInfoObserver* observer) {}
156 void SyncBackupManager::UnregisterDirectoryTypeDebugInfoObserver(
157 syncer::TypeDebugInfoObserver* observer) {}
159 bool SyncBackupManager::HasDirectoryTypeDebugInfoObserver(
160 syncer::TypeDebugInfoObserver* observer) { return false; }
162 void SyncBackupManager::RequestEmitDebugInfo() {}
164 } // namespace syncer