| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sync/driver/shared_change_processor.h" | 5 #include "components/sync/driver/shared_change_processor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "components/sync/api/data_type_error_handler.h" |
| 11 #include "components/sync/api/sync_change.h" | 12 #include "components/sync/api/sync_change.h" |
| 12 #include "components/sync/api/syncable_service.h" | 13 #include "components/sync/api/syncable_service.h" |
| 13 #include "components/sync/base/data_type_histogram.h" | 14 #include "components/sync/base/data_type_histogram.h" |
| 14 #include "components/sync/core/data_type_error_handler.h" | |
| 15 #include "components/sync/driver/generic_change_processor.h" | 15 #include "components/sync/driver/generic_change_processor.h" |
| 16 #include "components/sync/driver/generic_change_processor_factory.h" | 16 #include "components/sync/driver/generic_change_processor_factory.h" |
| 17 #include "components/sync/driver/shared_change_processor_ref.h" | 17 #include "components/sync/driver/shared_change_processor_ref.h" |
| 18 #include "components/sync/driver/sync_client.h" | 18 #include "components/sync/driver/sync_client.h" |
| 19 | 19 |
| 20 using base::AutoLock; | 20 using base::AutoLock; |
| 21 | 21 |
| 22 namespace syncer { | 22 namespace syncer { |
| 23 class AttachmentService; | 23 class AttachmentService; |
| 24 } | 24 } |
| 25 | 25 |
| 26 namespace sync_driver { | 26 namespace sync_driver { |
| 27 | 27 |
| 28 SharedChangeProcessor::SharedChangeProcessor(syncer::ModelType type) | 28 SharedChangeProcessor::SharedChangeProcessor(syncer::ModelType type) |
| 29 : disconnected_(false), | 29 : disconnected_(false), |
| 30 type_(type), | 30 type_(type), |
| 31 frontend_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 31 frontend_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 32 generic_change_processor_(NULL), | 32 generic_change_processor_(NULL) { |
| 33 error_handler_(NULL) { | |
| 34 DCHECK_NE(type_, syncer::UNSPECIFIED); | 33 DCHECK_NE(type_, syncer::UNSPECIFIED); |
| 35 } | 34 } |
| 36 | 35 |
| 37 SharedChangeProcessor::~SharedChangeProcessor() { | 36 SharedChangeProcessor::~SharedChangeProcessor() { |
| 38 // We can either be deleted when the DTC is destroyed (on UI | 37 // We can either be deleted when the DTC is destroyed (on UI |
| 39 // thread), or when the syncer::SyncableService stops syncing (datatype | 38 // thread), or when the syncer::SyncableService stops syncing (datatype |
| 40 // thread). |generic_change_processor_|, if non-NULL, must be | 39 // thread). |generic_change_processor_|, if non-NULL, must be |
| 41 // deleted on |backend_loop_|. | 40 // deleted on |backend_loop_|. |
| 42 if (backend_task_runner_.get()) { | 41 if (backend_task_runner_.get()) { |
| 43 if (backend_task_runner_->BelongsToCurrentThread()) { | 42 if (backend_task_runner_->BelongsToCurrentThread()) { |
| 44 delete generic_change_processor_; | 43 delete generic_change_processor_; |
| 45 } else { | 44 } else { |
| 46 DCHECK(frontend_task_runner_->BelongsToCurrentThread()); | 45 DCHECK(frontend_task_runner_->BelongsToCurrentThread()); |
| 47 if (!backend_task_runner_->DeleteSoon(FROM_HERE, | 46 if (!backend_task_runner_->DeleteSoon(FROM_HERE, |
| 48 generic_change_processor_)) { | 47 generic_change_processor_)) { |
| 49 NOTREACHED(); | 48 NOTREACHED(); |
| 50 } | 49 } |
| 51 } | 50 } |
| 52 } else { | 51 } else { |
| 53 DCHECK(!generic_change_processor_); | 52 DCHECK(!generic_change_processor_); |
| 54 } | 53 } |
| 55 } | 54 } |
| 56 | 55 |
| 57 void SharedChangeProcessor::StartAssociation( | 56 void SharedChangeProcessor::StartAssociation( |
| 58 StartDoneCallback start_done, | 57 StartDoneCallback start_done, |
| 59 SyncClient* const sync_client, | 58 SyncClient* const sync_client, |
| 60 syncer::UserShare* user_share, | 59 syncer::UserShare* user_share, |
| 61 syncer::DataTypeErrorHandler* error_handler) { | 60 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler) { |
| 62 DCHECK(user_share); | 61 DCHECK(user_share); |
| 63 syncer::SyncMergeResult local_merge_result(type_); | 62 syncer::SyncMergeResult local_merge_result(type_); |
| 64 syncer::SyncMergeResult syncer_merge_result(type_); | 63 syncer::SyncMergeResult syncer_merge_result(type_); |
| 65 base::WeakPtrFactory<syncer::SyncMergeResult> weak_ptr_factory( | 64 base::WeakPtrFactory<syncer::SyncMergeResult> weak_ptr_factory( |
| 66 &syncer_merge_result); | 65 &syncer_merge_result); |
| 67 | 66 |
| 68 // Connect |shared_change_processor| to the syncer and get the | 67 // Connect |shared_change_processor| to the syncer and get the |
| 69 // syncer::SyncableService associated with type_. | 68 // syncer::SyncableService associated with type_. |
| 70 // Note that it's possible the shared_change_processor has already been | 69 // Note that it's possible the shared_change_processor has already been |
| 71 // disconnected at this point, so all our accesses to the syncer from this | 70 // disconnected at this point, so all our accesses to the syncer from this |
| 72 // point on are through it. | 71 // point on are through it. |
| 73 GenericChangeProcessorFactory factory; | 72 GenericChangeProcessorFactory factory; |
| 74 local_service_ = Connect(sync_client, &factory, user_share, error_handler, | 73 local_service_ = |
| 75 weak_ptr_factory.GetWeakPtr()); | 74 Connect(sync_client, &factory, user_share, std::move(error_handler), |
| 75 weak_ptr_factory.GetWeakPtr()); |
| 76 if (!local_service_.get()) { | 76 if (!local_service_.get()) { |
| 77 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 77 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 78 "Failed to connect to syncer.", type_); | 78 "Failed to connect to syncer.", type_); |
| 79 local_merge_result.set_error(error); | 79 local_merge_result.set_error(error); |
| 80 start_done.Run(DataTypeController::ASSOCIATION_FAILED, local_merge_result, | 80 start_done.Run(DataTypeController::ASSOCIATION_FAILED, local_merge_result, |
| 81 syncer_merge_result); | 81 syncer_merge_result); |
| 82 return; | 82 return; |
| 83 } | 83 } |
| 84 | 84 |
| 85 if (!CryptoReadyIfNecessary()) { | 85 if (!CryptoReadyIfNecessary()) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 142 |
| 143 start_done.Run(!sync_has_nodes ? DataTypeController::OK_FIRST_RUN | 143 start_done.Run(!sync_has_nodes ? DataTypeController::OK_FIRST_RUN |
| 144 : DataTypeController::OK, | 144 : DataTypeController::OK, |
| 145 local_merge_result, syncer_merge_result); | 145 local_merge_result, syncer_merge_result); |
| 146 } | 146 } |
| 147 | 147 |
| 148 base::WeakPtr<syncer::SyncableService> SharedChangeProcessor::Connect( | 148 base::WeakPtr<syncer::SyncableService> SharedChangeProcessor::Connect( |
| 149 SyncClient* sync_client, | 149 SyncClient* sync_client, |
| 150 GenericChangeProcessorFactory* processor_factory, | 150 GenericChangeProcessorFactory* processor_factory, |
| 151 syncer::UserShare* user_share, | 151 syncer::UserShare* user_share, |
| 152 syncer::DataTypeErrorHandler* error_handler, | 152 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, |
| 153 const base::WeakPtr<syncer::SyncMergeResult>& merge_result) { | 153 const base::WeakPtr<syncer::SyncMergeResult>& merge_result) { |
| 154 DCHECK(sync_client); | 154 DCHECK(sync_client); |
| 155 DCHECK(error_handler); | 155 DCHECK(error_handler); |
| 156 backend_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 156 backend_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 157 AutoLock lock(monitor_lock_); | 157 AutoLock lock(monitor_lock_); |
| 158 if (disconnected_) | 158 if (disconnected_) |
| 159 return base::WeakPtr<syncer::SyncableService>(); | 159 return base::WeakPtr<syncer::SyncableService>(); |
| 160 error_handler_ = error_handler; | 160 error_handler_ = std::move(error_handler); |
| 161 base::WeakPtr<syncer::SyncableService> local_service = | 161 base::WeakPtr<syncer::SyncableService> local_service = |
| 162 sync_client->GetSyncableServiceForType(type_); | 162 sync_client->GetSyncableServiceForType(type_); |
| 163 if (!local_service.get()) { | 163 if (!local_service.get()) { |
| 164 LOG(WARNING) << "SyncableService destroyed before DTC was stopped."; | 164 LOG(WARNING) << "SyncableService destroyed before DTC was stopped."; |
| 165 disconnected_ = true; | 165 disconnected_ = true; |
| 166 return base::WeakPtr<syncer::SyncableService>(); | 166 return base::WeakPtr<syncer::SyncableService>(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 generic_change_processor_ = processor_factory | 169 generic_change_processor_ = processor_factory |
| 170 ->CreateGenericChangeProcessor( | 170 ->CreateGenericChangeProcessor( |
| 171 type_, user_share, error_handler, | 171 type_, user_share, error_handler_->Copy(), |
| 172 local_service, merge_result, sync_client) | 172 local_service, merge_result, sync_client) |
| 173 .release(); | 173 .release(); |
| 174 // If available, propagate attachment service to the syncable service. | 174 // If available, propagate attachment service to the syncable service. |
| 175 std::unique_ptr<syncer::AttachmentService> attachment_service = | 175 std::unique_ptr<syncer::AttachmentService> attachment_service = |
| 176 generic_change_processor_->GetAttachmentService(); | 176 generic_change_processor_->GetAttachmentService(); |
| 177 if (attachment_service) { | 177 if (attachment_service) { |
| 178 local_service->SetAttachmentService(std::move(attachment_service)); | 178 local_service->SetAttachmentService(std::move(attachment_service)); |
| 179 } | 179 } |
| 180 return local_service; | 180 return local_service; |
| 181 } | 181 } |
| 182 | 182 |
| 183 bool SharedChangeProcessor::Disconnect() { | 183 bool SharedChangeProcessor::Disconnect() { |
| 184 // May be called from any thread. | 184 // May be called from any thread. |
| 185 DVLOG(1) << "Disconnecting change processor."; | 185 DVLOG(1) << "Disconnecting change processor."; |
| 186 AutoLock lock(monitor_lock_); | 186 AutoLock lock(monitor_lock_); |
| 187 bool was_connected = !disconnected_; | 187 bool was_connected = !disconnected_; |
| 188 disconnected_ = true; | 188 disconnected_ = true; |
| 189 error_handler_ = NULL; | 189 error_handler_.reset(); |
| 190 return was_connected; | 190 return was_connected; |
| 191 } | 191 } |
| 192 | 192 |
| 193 ChangeProcessor* SharedChangeProcessor::generic_change_processor() { | 193 ChangeProcessor* SharedChangeProcessor::generic_change_processor() { |
| 194 return generic_change_processor_; | 194 return generic_change_processor_; |
| 195 } | 195 } |
| 196 | 196 |
| 197 int SharedChangeProcessor::GetSyncCount() { | 197 int SharedChangeProcessor::GetSyncCount() { |
| 198 DCHECK(backend_task_runner_.get()); | 198 DCHECK(backend_task_runner_.get()); |
| 199 DCHECK(backend_task_runner_->BelongsToCurrentThread()); | 199 DCHECK(backend_task_runner_->BelongsToCurrentThread()); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 311 #undef PER_DATA_TYPE_MACRO | 311 #undef PER_DATA_TYPE_MACRO |
| 312 } | 312 } |
| 313 | 313 |
| 314 void SharedChangeProcessor::StopLocalService() { | 314 void SharedChangeProcessor::StopLocalService() { |
| 315 if (local_service_.get()) | 315 if (local_service_.get()) |
| 316 local_service_->StopSyncing(type_); | 316 local_service_->StopSyncing(type_); |
| 317 local_service_.reset(); | 317 local_service_.reset(); |
| 318 } | 318 } |
| 319 | 319 |
| 320 } // namespace sync_driver | 320 } // namespace sync_driver |
| OLD | NEW |