Eager Gesture Recognition on Aura
[chromium-blink-merge.git] / sync / internal_api / sync_backup_manager.cc
blob0e71f3b7de51b940581c102092fe70335001bcc2
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"
11 #include "url/gurl.h"
13 namespace syncer {
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,
35 Encryptor* encryptor,
36 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler,
37 ReportUnrecoverableErrorFunction report_unrecoverable_error_function,
38 CancelationSignal* cancelation_signal) {
39 if (SyncRollbackManagerBase::InitInternal(
40 database_location,
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() {
53 if (initialized()) {
54 NormalizeEntries();
55 GetUserShare()->directory->SaveChanges();
59 SyncStatus SyncBackupManager::GetDetailedStatus() const {
60 return status_;
63 ModelTypeSet SyncBackupManager::HandleTransactionEndingChangeEvent(
64 const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
65 syncable::BaseTransaction* trans) {
66 ModelTypeSet types;
67 if (in_normalization_) {
68 // Skip if in our own WriteTransaction from NormalizeEntries().
69 in_normalization_ = false;
70 return types;
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) {
76 int64 id = it->first;
77 if (unsynced_.find(id) == unsynced_.end()) {
78 unsynced_.insert(id);
80 const syncable::EntryKernel& e = it->second.mutated;
81 ModelType type = e.GetModelType();
82 types.Put(type);
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]++;
89 return types;
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);
99 CHECK(entry.good());
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);
110 unsynced_.clear();
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))
117 return;
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]);
124 if (entry.good()) {
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