OLD | NEW |
---|---|
(Empty) | |
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_rollback_manager.h" | |
6 | |
7 #include "sync/internal_api/public/base/model_type.h" | |
8 #include "sync/internal_api/public/read_node.h" | |
9 #include "sync/internal_api/public/read_transaction.h" | |
10 #include "sync/internal_api/public/util/syncer_error.h" | |
11 #include "sync/internal_api/public/write_transaction.h" | |
12 #include "sync/syncable/directory.h" | |
13 #include "sync/syncable/mutable_entry.h" | |
14 | |
15 namespace syncer { | |
16 | |
17 SyncRollbackManager::SyncRollbackManager() | |
18 : change_delegate_(NULL) { | |
19 } | |
20 | |
21 SyncRollbackManager::~SyncRollbackManager() { | |
22 } | |
23 | |
24 void SyncRollbackManager::Init( | |
25 const base::FilePath& database_location, | |
26 const WeakHandle<JsEventHandler>& event_handler, | |
27 const std::string& sync_server_and_path, | |
28 int sync_server_port, | |
29 bool use_ssl, | |
30 scoped_ptr<HttpPostProviderFactory> post_factory, | |
31 const std::vector<scoped_refptr<ModelSafeWorker> >& workers, | |
32 ExtensionsActivity* extensions_activity, | |
33 SyncManager::ChangeDelegate* change_delegate, | |
34 const SyncCredentials& credentials, | |
35 const std::string& invalidator_client_id, | |
36 const std::string& restored_key_for_bootstrapping, | |
37 const std::string& restored_keystore_key_for_bootstrapping, | |
38 InternalComponentsFactory* internal_components_factory, | |
39 Encryptor* encryptor, | |
40 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler, | |
41 ReportUnrecoverableErrorFunction | |
42 report_unrecoverable_error_function, | |
43 CancelationSignal* cancelation_signal) { | |
44 SyncRollbackManagerBase::Init(database_location, event_handler, | |
45 sync_server_and_path, sync_server_port, | |
46 use_ssl, post_factory.Pass(), | |
47 workers, extensions_activity, change_delegate, | |
48 credentials, invalidator_client_id, | |
49 restored_key_for_bootstrapping, | |
50 restored_keystore_key_for_bootstrapping, | |
51 internal_components_factory, encryptor, | |
52 unrecoverable_error_handler.Pass(), | |
53 report_unrecoverable_error_function, | |
54 cancelation_signal); | |
55 | |
56 change_delegate_ = change_delegate; | |
Nicolas Zea
2014/04/17 18:22:56
Where do you clear the old backup directory? Or th
haitaol1
2014/04/17 19:30:38
SyncBackendHostCore::DoShutdown() deletes the dire
| |
57 | |
58 for (size_t i = 0; i < workers.size(); ++i) { | |
59 ModelSafeGroup group = workers[i]->GetModelSafeGroup(); | |
60 CHECK(workers_.find(group) == workers_.end()); | |
61 workers_[group] = workers[i]; | |
62 } | |
63 | |
64 rollback_ready_types_ = GetUserShare()->directory->InitialSyncEndedTypes(); | |
65 rollback_ready_types_.RetainAll(BackupTypes()); | |
66 } | |
67 | |
68 void SyncRollbackManager::StartSyncingNormally( | |
69 const ModelSafeRoutingInfo& routing_info){ | |
70 std::map<ModelType, syncable::Directory::Metahandles> to_delete; | |
71 | |
72 { | |
73 WriteTransaction trans(FROM_HERE, GetUserShare()); | |
74 syncable::Directory::Metahandles unsynced; | |
75 GetUserShare()->directory->GetUnsyncedMetaHandles(trans.GetWrappedTrans(), | |
76 &unsynced); | |
Nicolas Zea
2014/04/17 18:22:56
fix indent (FYI, you can use git cl format to fix
haitaol1
2014/04/17 19:30:38
Done.
| |
77 for (size_t i = 0; i < unsynced.size(); ++i) { | |
78 syncable::MutableEntry e(trans.GetWrappedWriteTrans(), | |
79 syncable::GET_BY_HANDLE, unsynced[i]); | |
80 if (!e.good() || e.GetIsDel() || e.GetId().ServerKnows()) | |
81 continue; | |
82 | |
83 ModelType type = GetModelTypeFromSpecifics(e.GetSpecifics()); | |
84 if (!rollback_ready_types_.Has(type)) | |
85 continue; | |
86 | |
87 to_delete[type].push_back(unsynced[i]); | |
88 } | |
89 } | |
90 | |
91 for (std::map<ModelType, syncable::Directory::Metahandles>::iterator it = | |
92 to_delete.begin(); it != to_delete.end(); ++it) { | |
93 ModelSafeGroup group = routing_info.find(it->first)->second; | |
94 CHECK(workers_.find(group) != workers_.end()); | |
95 workers_[group]->DoWorkAndWaitUntilDone( | |
96 base::Bind(&SyncRollbackManager::DeleteOnWorkerThread, | |
97 base::Unretained(this), | |
98 it->first, it->second)); | |
99 } | |
Nicolas Zea
2014/04/17 18:22:56
add a TODO to handle any cases where data was merg
haitaol1
2014/04/17 19:30:38
Added TODO above.
On 2014/04/17 18:22:56, Nicolas
| |
100 } | |
101 | |
102 SyncerError SyncRollbackManager::DeleteOnWorkerThread( | |
103 ModelType type, std::vector<int64> handles) { | |
104 CHECK(change_delegate_); | |
105 | |
106 { | |
107 ChangeRecordList deletes; | |
108 WriteTransaction trans(FROM_HERE, GetUserShare()); | |
109 for (size_t i = 0; i < handles.size(); ++i) { | |
110 syncable::MutableEntry e(trans.GetWrappedWriteTrans(), | |
111 syncable::GET_BY_HANDLE, handles[i]); | |
112 if (!e.good() || e.GetIsDel()) | |
113 continue; | |
114 | |
115 ChangeRecord del; | |
116 del.action = ChangeRecord::ACTION_DELETE; | |
117 del.id = handles[i]; | |
118 del.specifics = e.GetSpecifics(); | |
119 deletes.push_back(del); | |
120 } | |
121 | |
122 change_delegate_->OnChangesApplied(type, 1, &trans, | |
123 MakeImmutable(&deletes)); | |
124 } | |
125 | |
126 change_delegate_->OnChangesComplete(type); | |
127 return SYNCER_OK; | |
128 } | |
129 | |
130 } // namespace syncer | |
OLD | NEW |