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/write_transaction.h"
8 #include "sync/syncable/directory.h"
9 #include "sync/syncable/mutable_entry.h"
13 SyncBackupManager::SyncBackupManager()
14 : in_normalization_(false) {
17 SyncBackupManager::~SyncBackupManager() {
20 void SyncBackupManager::Init(
21 const base::FilePath
& database_location
,
22 const WeakHandle
<JsEventHandler
>& event_handler
,
23 const std::string
& sync_server_and_path
,
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
,
36 scoped_ptr
<UnrecoverableErrorHandler
> unrecoverable_error_handler
,
37 ReportUnrecoverableErrorFunction
38 report_unrecoverable_error_function
,
39 CancelationSignal
* cancelation_signal
) {
40 SyncRollbackManagerBase::Init(database_location
, event_handler
,
41 sync_server_and_path
, sync_server_port
,
42 use_ssl
, post_factory
.Pass(),
43 workers
, extensions_activity
, change_delegate
,
44 credentials
, invalidator_client_id
,
45 restored_key_for_bootstrapping
,
46 restored_keystore_key_for_bootstrapping
,
47 internal_components_factory
, encryptor
,
48 unrecoverable_error_handler
.Pass(),
49 report_unrecoverable_error_function
,
52 GetUserShare()->directory
->CollectMetaHandleCounts(
53 &status_
.num_entries_by_type
,
54 &status_
.num_to_delete_entries_by_type
);
57 void SyncBackupManager::SaveChanges() {
59 GetUserShare()->directory
->SaveChanges();
62 SyncStatus
SyncBackupManager::GetDetailedStatus() const {
66 ModelTypeSet
SyncBackupManager::HandleTransactionEndingChangeEvent(
67 const syncable::ImmutableWriteTransactionInfo
& write_transaction_info
,
68 syncable::BaseTransaction
* trans
) {
70 if (in_normalization_
) {
71 // Skip if in our own WriteTransaction from NormalizeEntries().
72 in_normalization_
= false;
76 for (syncable::EntryKernelMutationMap::const_iterator it
=
77 write_transaction_info
.Get().mutations
.Get().begin();
78 it
!= write_transaction_info
.Get().mutations
.Get().end(); ++it
) {
80 if (unsynced_
.find(id
) == unsynced_
.end()) {
83 const syncable::EntryKernel
& e
= it
->second
.mutated
;
84 ModelType type
= e
.GetModelType();
86 if (!e
.ref(syncable::ID
).ServerKnows())
87 status_
.num_entries_by_type
[type
]++;
88 if (e
.ref(syncable::IS_DEL
))
89 status_
.num_to_delete_entries_by_type
[type
]++;
95 void SyncBackupManager::NormalizeEntries() {
96 WriteTransaction
trans(FROM_HERE
, GetUserShare());
97 in_normalization_
= true;
98 for (std::set
<int64
>::const_iterator it
= unsynced_
.begin();
99 it
!= unsynced_
.end(); ++it
) {
100 syncable::MutableEntry
entry(trans
.GetWrappedWriteTrans(),
101 syncable::GET_BY_HANDLE
, *it
);
104 if (!entry
.GetId().ServerKnows())
105 entry
.PutId(syncable::Id::CreateFromServerId(entry
.GetId().value()));
106 entry
.PutBaseVersion(1);
107 entry
.PutIsUnsynced(false);
112 } // namespace syncer