32955683dd9ee21f96d1c4bed8665519e09afbea
[platform/framework/web/crosswalk.git] / src / sync / internal_api / sync_backup_manager.cc
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.
4
5 #include "sync/internal_api/sync_backup_manager.h"
6
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
12 namespace syncer {
13
14 SyncBackupManager::SyncBackupManager()
15     : in_normalization_(false) {
16 }
17
18 SyncBackupManager::~SyncBackupManager() {
19 }
20
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   if (SyncRollbackManagerBase::InitInternal(
42           database_location,
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);
48
49     HideSyncPreference(PRIORITY_PREFERENCES);
50     HideSyncPreference(PREFERENCES);
51   }
52 }
53
54 void SyncBackupManager::SaveChanges() {
55   if (initialized()) {
56     NormalizeEntries();
57     GetUserShare()->directory->SaveChanges();
58   }
59 }
60
61 SyncStatus SyncBackupManager::GetDetailedStatus() const {
62   return status_;
63 }
64
65 ModelTypeSet SyncBackupManager::HandleTransactionEndingChangeEvent(
66     const syncable::ImmutableWriteTransactionInfo& write_transaction_info,
67     syncable::BaseTransaction* trans) {
68   ModelTypeSet types;
69   if (in_normalization_) {
70     // Skip if in our own WriteTransaction from NormalizeEntries().
71     in_normalization_ = false;
72     return types;
73   }
74
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) {
78     int64 id = it->first;
79     if (unsynced_.find(id) == unsynced_.end()) {
80       unsynced_.insert(id);
81
82       const syncable::EntryKernel& e = it->second.mutated;
83       ModelType type = e.GetModelType();
84       types.Put(type);
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]++;
89     }
90   }
91   return types;
92 }
93
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);
101     CHECK(entry.good());
102
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()));
108     }
109     entry.PutBaseVersion(1);
110     entry.PutIsUnsynced(false);
111   }
112   unsynced_.clear();
113 }
114
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))
119     return;
120
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]);
126     if (entry.good()) {
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);
134
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);
139     }
140   }
141 }
142
143 void SyncBackupManager::RegisterDirectoryTypeDebugInfoObserver(
144     syncer::TypeDebugInfoObserver* observer) {}
145
146 void SyncBackupManager::UnregisterDirectoryTypeDebugInfoObserver(
147     syncer::TypeDebugInfoObserver* observer) {}
148
149 bool SyncBackupManager::HasDirectoryTypeDebugInfoObserver(
150     syncer::TypeDebugInfoObserver* observer) { return false; }
151
152 void SyncBackupManager::RequestEmitDebugInfo() {}
153
154 }  // namespace syncer