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_base.h" 6 7 #include "sync/internal_api/public/base/model_type.h" 8 #include "sync/internal_api/public/internal_components_factory.h" 9 #include "sync/internal_api/public/read_node.h" 10 #include "sync/internal_api/public/read_transaction.h" 11 #include "sync/internal_api/public/write_transaction.h" 12 #include "sync/syncable/directory_backing_store.h" 13 #include "sync/syncable/mutable_entry.h" 14 15 namespace { 16 17 // Permanent bookmark folders as defined in bookmark_model_associator.cc. 18 // No mobile bookmarks because they only exists with sync enabled. 19 const char kBookmarkBarTag[] = "bookmark_bar"; 20 const char kOtherBookmarksTag[] = "other_bookmarks"; 21 22 class DummyEntryptionHandler : public syncer::SyncEncryptionHandler { 23 virtual void AddObserver(Observer* observer) OVERRIDE {} 24 virtual void RemoveObserver(Observer* observer) OVERRIDE {} 25 virtual void Init() OVERRIDE {} 26 virtual void SetEncryptionPassphrase(const std::string& passphrase, 27 bool is_explicit) OVERRIDE {} 28 virtual void SetDecryptionPassphrase(const std::string& passphrase) 29 OVERRIDE {} 30 virtual void EnableEncryptEverything() OVERRIDE {} 31 virtual bool EncryptEverythingEnabled() const OVERRIDE { 32 return false; 33 } 34 virtual syncer::PassphraseType GetPassphraseType() const OVERRIDE { 35 return syncer::KEYSTORE_PASSPHRASE; 36 } 37 }; 38 39 } // anonymous namespace 40 41 namespace syncer { 42 43 SyncRollbackManagerBase::SyncRollbackManagerBase() 44 : report_unrecoverable_error_function_(NULL), 45 weak_ptr_factory_(this), 46 dummy_handler_(new DummyEntryptionHandler), 47 initialized_(false) { 48 } 49 50 SyncRollbackManagerBase::~SyncRollbackManagerBase() { 51 } 52 53 bool SyncRollbackManagerBase::InitInternal( 54 const base::FilePath& database_location, 55 InternalComponentsFactory* internal_components_factory, 56 scoped_ptr<UnrecoverableErrorHandler> unrecoverable_error_handler, 57 ReportUnrecoverableErrorFunction report_unrecoverable_error_function) { 58 unrecoverable_error_handler_ = unrecoverable_error_handler.Pass(); 59 report_unrecoverable_error_function_ = report_unrecoverable_error_function; 60 61 if (!InitBackupDB(database_location, internal_components_factory)) { 62 NotifyInitializationFailure(); 63 return false; 64 } 65 66 initialized_ = true; 67 NotifyInitializationSuccess(); 68 return true; 69 } 70 71 ModelTypeSet SyncRollbackManagerBase::InitialSyncEndedTypes() { 72 return share_.directory->InitialSyncEndedTypes(); 73 } 74 75 ModelTypeSet SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken( 76 ModelTypeSet types) { 77 ModelTypeSet inited_types = share_.directory->InitialSyncEndedTypes(); 78 types.RemoveAll(inited_types); 79 return types; 80 } 81 82 bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() { 83 NOTREACHED(); 84 return true; 85 } 86 87 void SyncRollbackManagerBase::UpdateCredentials( 88 const SyncCredentials& credentials) { 89 } 90 91 void SyncRollbackManagerBase::StartSyncingNormally( 92 const ModelSafeRoutingInfo& routing_info){ 93 } 94 95 void SyncRollbackManagerBase::ConfigureSyncer( 96 ConfigureReason reason, 97 ModelTypeSet to_download, 98 ModelTypeSet to_purge, 99 ModelTypeSet to_journal, 100 ModelTypeSet to_unapply, 101 const ModelSafeRoutingInfo& new_routing_info, 102 const base::Closure& ready_task, 103 const base::Closure& retry_task) { 104 for (ModelTypeSet::Iterator type = to_download.First(); 105 type.Good(); type.Inc()) { 106 if (InitTypeRootNode(type.Get())) { 107 if (type.Get() == BOOKMARKS) { 108 InitBookmarkFolder(kBookmarkBarTag); 109 InitBookmarkFolder(kOtherBookmarksTag); 110 } 111 } 112 } 113 114 ready_task.Run(); 115 } 116 117 void SyncRollbackManagerBase::OnInvalidatorStateChange(InvalidatorState state) { 118 } 119 120 void SyncRollbackManagerBase::OnIncomingInvalidation( 121 const ObjectIdInvalidationMap& invalidation_map) { 122 NOTREACHED(); 123 } 124 125 void SyncRollbackManagerBase::AddObserver(SyncManager::Observer* observer) { 126 observers_.AddObserver(observer); 127 } 128 129 void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer* observer) { 130 observers_.RemoveObserver(observer); 131 } 132 133 SyncStatus SyncRollbackManagerBase::GetDetailedStatus() const { 134 return SyncStatus(); 135 } 136 137 void SyncRollbackManagerBase::SaveChanges() { 138 } 139 140 void SyncRollbackManagerBase::ShutdownOnSyncThread() { 141 if (initialized_) { 142 SaveChanges(); 143 share_.directory->Close(); 144 share_.directory.reset(); 145 initialized_ = false; 146 } 147 } 148 149 UserShare* SyncRollbackManagerBase::GetUserShare() { 150 return &share_; 151 } 152 153 const std::string SyncRollbackManagerBase::cache_guid() { 154 return share_.directory->cache_guid(); 155 } 156 157 bool SyncRollbackManagerBase::ReceivedExperiment(Experiments* experiments) { 158 return false; 159 } 160 161 bool SyncRollbackManagerBase::HasUnsyncedItems() { 162 ReadTransaction trans(FROM_HERE, &share_); 163 syncable::Directory::Metahandles unsynced; 164 share_.directory->GetUnsyncedMetaHandles(trans.GetWrappedTrans(), &unsynced); 165 return !unsynced.empty(); 166 } 167 168 SyncEncryptionHandler* SyncRollbackManagerBase::GetEncryptionHandler() { 169 return dummy_handler_.get(); 170 } 171 172 void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types) { 173 174 } 175 176 void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent( 177 ModelTypeSet models_with_changes) { 178 } 179 180 ModelTypeSet SyncRollbackManagerBase::HandleTransactionEndingChangeEvent( 181 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, 182 syncable::BaseTransaction* trans) { 183 return ModelTypeSet(); 184 } 185 186 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi( 187 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, 188 syncable::BaseTransaction* trans, 189 std::vector<int64>* entries_changed) { 190 } 191 192 void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer( 193 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, 194 syncable::BaseTransaction* trans, 195 std::vector<int64>* entries_changed) { 196 } 197 198 void SyncRollbackManagerBase::OnTransactionWrite( 199 const syncable::ImmutableWriteTransactionInfo& write_transaction_info, 200 ModelTypeSet models_with_changes) { 201 } 202 203 void SyncRollbackManagerBase::NotifyInitializationSuccess() { 204 FOR_EACH_OBSERVER( 205 SyncManager::Observer, observers_, 206 OnInitializationComplete( 207 MakeWeakHandle(base::WeakPtr<JsBackend>()), 208 MakeWeakHandle(base::WeakPtr<DataTypeDebugInfoListener>()), 209 true, InitialSyncEndedTypes())); 210 } 211 212 void SyncRollbackManagerBase::NotifyInitializationFailure() { 213 FOR_EACH_OBSERVER( 214 SyncManager::Observer, observers_, 215 OnInitializationComplete( 216 MakeWeakHandle(base::WeakPtr<JsBackend>()), 217 MakeWeakHandle(base::WeakPtr<DataTypeDebugInfoListener>()), 218 false, ModelTypeSet())); 219 } 220 221 std::string SyncRollbackManagerBase::GetOwnerName() const { 222 return ""; 223 } 224 225 syncer::SyncCoreProxy* SyncRollbackManagerBase::GetSyncCoreProxy() { 226 return NULL; 227 } 228 229 ScopedVector<syncer::ProtocolEvent> 230 SyncRollbackManagerBase::GetBufferedProtocolEvents() { 231 return ScopedVector<syncer::ProtocolEvent>().Pass(); 232 } 233 234 scoped_ptr<base::ListValue> SyncRollbackManagerBase::GetAllNodesForType( 235 syncer::ModelType type) { 236 ReadTransaction trans(FROM_HERE, GetUserShare()); 237 scoped_ptr<base::ListValue> nodes( 238 trans.GetDirectory()->GetNodeDetailsForType(trans.GetWrappedTrans(), 239 type)); 240 return nodes.Pass(); 241 } 242 243 bool SyncRollbackManagerBase::InitBackupDB( 244 const base::FilePath& sync_folder, 245 InternalComponentsFactory* internal_components_factory) { 246 base::FilePath backup_db_path = sync_folder.Append( 247 syncable::Directory::kSyncDatabaseFilename); 248 scoped_ptr<syncable::DirectoryBackingStore> backing_store = 249 internal_components_factory->BuildDirectoryBackingStore( 250 "backup", backup_db_path).Pass(); 251 252 DCHECK(backing_store.get()); 253 share_.directory.reset( 254 new syncable::Directory( 255 backing_store.release(), 256 unrecoverable_error_handler_.get(), 257 report_unrecoverable_error_function_, 258 NULL, 259 NULL)); 260 return syncable::OPENED == 261 share_.directory->Open( 262 "backup", this, 263 MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); 264 } 265 266 bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type) { 267 WriteTransaction trans(FROM_HERE, &share_); 268 ReadNode root(&trans); 269 if (BaseNode::INIT_OK == root.InitTypeRoot(type)) 270 return true; 271 272 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(), 273 syncable::CREATE_NEW_UPDATE_ITEM, 274 syncable::Id::CreateFromServerId( 275 ModelTypeToString(type))); 276 if (!entry.good()) 277 return false; 278 279 entry.PutParentId(syncable::Id()); 280 entry.PutBaseVersion(1); 281 entry.PutUniqueServerTag(ModelTypeToRootTag(type)); 282 entry.PutNonUniqueName(ModelTypeToString(type)); 283 entry.PutIsDel(false); 284 entry.PutIsDir(true); 285 286 sync_pb::EntitySpecifics specifics; 287 AddDefaultFieldValue(type, &specifics); 288 entry.PutSpecifics(specifics); 289 290 return true; 291 } 292 293 void SyncRollbackManagerBase::InitBookmarkFolder(const std::string& folder) { 294 WriteTransaction trans(FROM_HERE, &share_); 295 syncable::Entry bookmark_root(trans.GetWrappedTrans(), 296 syncable::GET_TYPE_ROOT, 297 BOOKMARKS); 298 if (!bookmark_root.good()) 299 return; 300 301 syncable::MutableEntry entry(trans.GetWrappedWriteTrans(), 302 syncable::CREATE_NEW_UPDATE_ITEM, 303 syncable::Id::CreateFromServerId(folder)); 304 if (!entry.good()) 305 return; 306 307 entry.PutParentId(bookmark_root.GetId()); 308 entry.PutBaseVersion(1); 309 entry.PutUniqueServerTag(folder); 310 entry.PutNonUniqueName(folder); 311 entry.PutIsDel(false); 312 entry.PutIsDir(true); 313 314 sync_pb::EntitySpecifics specifics; 315 AddDefaultFieldValue(BOOKMARKS, &specifics); 316 entry.PutSpecifics(specifics); 317 } 318 319 ObserverList<SyncManager::Observer>* SyncRollbackManagerBase::GetObservers() { 320 return &observers_; 321 } 322 323 void SyncRollbackManagerBase::RegisterDirectoryTypeDebugInfoObserver( 324 syncer::TypeDebugInfoObserver* observer) {} 325 326 void SyncRollbackManagerBase::UnregisterDirectoryTypeDebugInfoObserver( 327 syncer::TypeDebugInfoObserver* observer) {} 328 329 bool SyncRollbackManagerBase::HasDirectoryTypeDebugInfoObserver( 330 syncer::TypeDebugInfoObserver* observer) { return false; } 331 332 void SyncRollbackManagerBase::RequestEmitDebugInfo() {} 333 334 } // namespace syncer 335