Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / sync_backup_manager.cc
index 295f79a..3295568 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "sync/internal_api/sync_backup_manager.h"
 
+#include "sync/internal_api/public/read_node.h"
 #include "sync/internal_api/public/write_transaction.h"
 #include "sync/syncable/directory.h"
 #include "sync/syncable/mutable_entry.h"
@@ -37,26 +38,24 @@ void SyncBackupManager::Init(
       ReportUnrecoverableErrorFunction
           report_unrecoverable_error_function,
       CancelationSignal* cancelation_signal) {
-  SyncRollbackManagerBase::Init(database_location, event_handler,
-                                sync_server_and_path, sync_server_port,
-                                use_ssl, post_factory.Pass(),
-                                workers, extensions_activity, change_delegate,
-                                credentials, invalidator_client_id,
-                                restored_key_for_bootstrapping,
-                                restored_keystore_key_for_bootstrapping,
-                                internal_components_factory, encryptor,
-                                unrecoverable_error_handler.Pass(),
-                                report_unrecoverable_error_function,
-                                cancelation_signal);
-
-  GetUserShare()->directory->CollectMetaHandleCounts(
-      &status_.num_entries_by_type,
-      &status_.num_to_delete_entries_by_type);
+  if (SyncRollbackManagerBase::InitInternal(
+          database_location,
+          internal_components_factory,
+          unrecoverable_error_handler.Pass(),
+          report_unrecoverable_error_function)) {
+    GetUserShare()->directory->CollectMetaHandleCounts(
+        &status_.num_entries_by_type, &status_.num_to_delete_entries_by_type);
+
+    HideSyncPreference(PRIORITY_PREFERENCES);
+    HideSyncPreference(PREFERENCES);
+  }
 }
 
 void SyncBackupManager::SaveChanges() {
-  NormalizeEntries();
-  GetUserShare()->directory->SaveChanges();
+  if (initialized()) {
+    NormalizeEntries();
+    GetUserShare()->directory->SaveChanges();
+  }
 }
 
 SyncStatus SyncBackupManager::GetDetailedStatus() const {
@@ -104,7 +103,7 @@ void SyncBackupManager::NormalizeEntries() {
     if (!entry.GetId().ServerKnows())
       entry.PutId(syncable::Id::CreateFromServerId(entry.GetId().value()));
     if (!entry.GetParentId().ServerKnows()) {
-      entry.PutParentId(syncable::Id::CreateFromServerId(
+      entry.PutParentIdPropertyOnly(syncable::Id::CreateFromServerId(
           entry.GetParentId().value()));
     }
     entry.PutBaseVersion(1);
@@ -113,6 +112,34 @@ void SyncBackupManager::NormalizeEntries() {
   unsynced_.clear();
 }
 
+void SyncBackupManager::HideSyncPreference(ModelType type) {
+  WriteTransaction trans(FROM_HERE, GetUserShare());
+  ReadNode pref_root(&trans);
+  if (BaseNode::INIT_OK != pref_root.InitTypeRoot(type))
+    return;
+
+  std::vector<int64> pref_ids;
+  pref_root.GetChildIds(&pref_ids);
+  for (uint32 i = 0; i < pref_ids.size(); ++i) {
+    syncable::MutableEntry entry(trans.GetWrappedWriteTrans(),
+                                 syncable::GET_BY_HANDLE, pref_ids[i]);
+    if (entry.good()) {
+      // HACKY: Set IS_DEL to true to remove entry from parent-children
+      // index so that it's not returned when syncable service asks
+      // for sync data. Syncable service then creates entry for local
+      // model. Then the existing entry is undeleted and set to local value
+      // because it has the same unique client tag.
+      entry.PutIsDel(true);
+      entry.PutIsUnsynced(false);
+
+      // Don't persist on disk so that if backup is aborted before receiving
+      // local preference values, values in sync DB are saved.
+      GetUserShare()->directory->UnmarkDirtyEntry(
+          trans.GetWrappedWriteTrans(), &entry);
+    }
+  }
+}
+
 void SyncBackupManager::RegisterDirectoryTypeDebugInfoObserver(
     syncer::TypeDebugInfoObserver* observer) {}