| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/browser/sync/glue/shared_change_processor.h" | |
| 6 | |
| 7 #include "chrome/browser/sync/profile_sync_factory.h" | |
| 8 #include "chrome/browser/sync/profile_sync_service.h" | |
| 9 #include "chrome/browser/sync/api/sync_change.h" | |
| 10 #include "chrome/browser/sync/glue/generic_change_processor.h" | |
| 11 #include "content/browser/browser_thread.h" | |
| 12 | |
| 13 using base::AutoLock; | |
| 14 | |
| 15 namespace browser_sync { | |
| 16 | |
| 17 SharedChangeProcessor::SharedChangeProcessor() | |
| 18 : disconnected_(false) { | |
| 19 // We're always created on the UI thread. | |
| 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 21 DetachFromThread(); | |
| 22 } | |
| 23 | |
| 24 SharedChangeProcessor::~SharedChangeProcessor() { | |
| 25 // We can either be deleted when the DTC is destroyed (on UI thread), | |
| 26 // or when the SyncableService stop's syncing (datatype thread). | |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI) || | |
| 28 CalledOnValidThread()); | |
| 29 DetachFromThread(); | |
| 30 } | |
| 31 | |
| 32 bool SharedChangeProcessor::Connect( | |
| 33 ProfileSyncFactory* sync_factory, | |
| 34 ProfileSyncService* sync_service, | |
| 35 UnrecoverableErrorHandler* error_handler, | |
| 36 const base::WeakPtr<SyncableService>& local_service) { | |
| 37 DCHECK(CalledOnValidThread()); | |
| 38 AutoLock lock(monitor_lock_); | |
| 39 if (disconnected_) | |
| 40 return false; | |
| 41 if (!local_service) { | |
| 42 NOTREACHED() << "SyncableService destroyed before DTC was stopped."; | |
| 43 disconnected_ = true; | |
| 44 return false; | |
| 45 } | |
| 46 generic_change_processor_.reset( | |
| 47 sync_factory->CreateGenericChangeProcessor(sync_service, | |
| 48 error_handler, | |
| 49 local_service)); | |
| 50 return true; | |
| 51 } | |
| 52 | |
| 53 bool SharedChangeProcessor::Disconnect() { | |
| 54 // May be called from any thread. | |
| 55 VLOG(1) << "Disconnecting change processor."; | |
| 56 AutoLock lock(monitor_lock_); | |
| 57 bool was_connected = !disconnected_; | |
| 58 disconnected_ = true; | |
| 59 return was_connected; | |
| 60 } | |
| 61 | |
| 62 SyncError SharedChangeProcessor::GetSyncDataForType( | |
| 63 syncable::ModelType type, | |
| 64 SyncDataList* current_sync_data) { | |
| 65 DCHECK(CalledOnValidThread()); | |
| 66 AutoLock lock(monitor_lock_); | |
| 67 if (disconnected_) { | |
| 68 SyncError error(FROM_HERE, "Change processor disconnected.", type); | |
| 69 return error; | |
| 70 } | |
| 71 return generic_change_processor_->GetSyncDataForType(type, current_sync_data); | |
| 72 } | |
| 73 | |
| 74 SyncError SharedChangeProcessor::ProcessSyncChanges( | |
| 75 const tracked_objects::Location& from_here, | |
| 76 const SyncChangeList& list_of_changes) { | |
| 77 DCHECK(CalledOnValidThread()); | |
| 78 AutoLock lock(monitor_lock_); | |
| 79 if (disconnected_) { | |
| 80 // The DTC that disconnects us must ensure it posts a StopSyncing task. | |
| 81 // If we reach this, it means it just hasn't executed yet. | |
| 82 syncable::ModelType type; | |
| 83 if (list_of_changes.size() > 0) { | |
| 84 type = list_of_changes[0].sync_data().GetDataType(); | |
| 85 } | |
| 86 SyncError error(FROM_HERE, "Change processor disconnected.", type); | |
| 87 return error; | |
| 88 } | |
| 89 return generic_change_processor_->ProcessSyncChanges( | |
| 90 from_here, list_of_changes); | |
| 91 } | |
| 92 | |
| 93 bool SharedChangeProcessor::SyncModelHasUserCreatedNodes( | |
| 94 syncable::ModelType type, | |
| 95 bool* has_nodes) { | |
| 96 DCHECK(CalledOnValidThread()); | |
| 97 AutoLock lock(monitor_lock_); | |
| 98 if (disconnected_) { | |
| 99 LOG(ERROR) << "Change processor disconnected."; | |
| 100 return false; | |
| 101 } | |
| 102 return generic_change_processor_->SyncModelHasUserCreatedNodes( | |
| 103 type, has_nodes); | |
| 104 } | |
| 105 | |
| 106 bool SharedChangeProcessor::CryptoReadyIfNecessary(syncable::ModelType type) { | |
| 107 DCHECK(CalledOnValidThread()); | |
| 108 AutoLock lock(monitor_lock_); | |
| 109 if (disconnected_) { | |
| 110 LOG(ERROR) << "Change processor disconnected."; | |
| 111 return true; // Otherwise we get into infinite spin waiting. | |
| 112 } | |
| 113 return generic_change_processor_->CryptoReadyIfNecessary(type); | |
| 114 } | |
| 115 | |
| 116 void SharedChangeProcessor::ActivateDataType( | |
| 117 ProfileSyncService* sync_service, | |
| 118 syncable::ModelType model_type, | |
| 119 browser_sync::ModelSafeGroup model_safe_group) { | |
| 120 AutoLock lock(monitor_lock_); | |
| 121 if (disconnected_) { | |
| 122 LOG(ERROR) << "Change processor disconnected."; | |
| 123 return; | |
| 124 } | |
| 125 sync_service->ActivateDataType( | |
| 126 model_type, model_safe_group, generic_change_processor_.get()); | |
| 127 } | |
| 128 | |
| 129 } // namespace browser_sync | |
| OLD | NEW |