| 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/model_type_controller.h" | 5 #include "components/sync/driver/model_type_controller.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/location.h" | 10 #include "base/location.h" |
| 12 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "components/sync/base/bind_to_task_runner.h" | 13 #include "components/sync/base/bind_to_task_runner.h" |
| 15 #include "components/sync/base/data_type_histogram.h" | 14 #include "components/sync/base/data_type_histogram.h" |
| 16 #include "components/sync/driver/sync_client.h" | 15 #include "components/sync/driver/sync_client.h" |
| 17 #include "components/sync/engine/activation_context.h" | 16 #include "components/sync/engine/activation_context.h" |
| 18 #include "components/sync/engine/model_type_configurer.h" | 17 #include "components/sync/engine/model_type_configurer.h" |
| 19 #include "components/sync/model/data_type_error_handler_impl.h" | 18 #include "components/sync/model/data_type_error_handler_impl.h" |
| 20 #include "components/sync/model/model_type_change_processor.h" | 19 #include "components/sync/model/model_type_change_processor.h" |
| 21 #include "components/sync/model/model_type_debug_info.h" | 20 #include "components/sync/model/model_type_debug_info.h" |
| 22 #include "components/sync/model/model_type_sync_bridge.h" | 21 #include "components/sync/model/model_type_sync_bridge.h" |
| 23 #include "components/sync/model/sync_merge_result.h" | 22 #include "components/sync/model/sync_merge_result.h" |
| 24 | 23 |
| 25 namespace syncer { | 24 namespace syncer { |
| 26 | 25 |
| 26 using BridgeProvider = ModelTypeController::BridgeProvider; |
| 27 using BridgeTask = ModelTypeController::BridgeTask; |
| 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| 29 void CallOnSyncStartingHelper( | 31 void OnSyncStartingHelper( |
| 30 SyncClient* sync_client, | |
| 31 ModelType type, | |
| 32 const ModelErrorHandler& error_handler, | 32 const ModelErrorHandler& error_handler, |
| 33 ModelTypeChangeProcessor::StartCallback callback) { | 33 const ModelTypeChangeProcessor::StartCallback& callback, |
| 34 base::WeakPtr<ModelTypeSyncBridge> bridge = | 34 ModelTypeSyncBridge* bridge) { |
| 35 sync_client->GetSyncBridgeForModelType(type); | 35 bridge->OnSyncStarting(std::move(error_handler), std::move(callback)); |
| 36 if (bridge) { | |
| 37 bridge->OnSyncStarting(std::move(error_handler), callback); | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 void CallGetAllNodesHelper(SyncClient* sync_client, | |
| 42 ModelType type, | |
| 43 DataTypeController::AllNodesCallback callback) { | |
| 44 base::WeakPtr<ModelTypeSyncBridge> bridge = | |
| 45 sync_client->GetSyncBridgeForModelType(type); | |
| 46 if (bridge) { | |
| 47 ModelTypeDebugInfo::GetAllNodes(bridge, callback); | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 void CallGetStatusCountersHelper( | |
| 52 SyncClient* sync_client, | |
| 53 ModelType type, | |
| 54 const DataTypeController::StatusCountersCallback& callback) { | |
| 55 base::WeakPtr<ModelTypeSyncBridge> bridge = | |
| 56 sync_client->GetSyncBridgeForModelType(type); | |
| 57 if (bridge) { | |
| 58 ModelTypeDebugInfo::GetStatusCounters(bridge, callback); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void CallDisableSyncHelper(SyncClient* sync_client, ModelType type) { | |
| 63 base::WeakPtr<ModelTypeSyncBridge> bridge = | |
| 64 sync_client->GetSyncBridgeForModelType(type); | |
| 65 if (bridge) { | |
| 66 bridge->DisableSync(); | |
| 67 } | |
| 68 } | 36 } |
| 69 | 37 |
| 70 void ReportError(ModelType model_type, | 38 void ReportError(ModelType model_type, |
| 71 scoped_refptr<base::SingleThreadTaskRunner> ui_thread, | 39 scoped_refptr<base::SingleThreadTaskRunner> ui_thread, |
| 72 const ModelErrorHandler& error_handler, | 40 const ModelErrorHandler& error_handler, |
| 73 const ModelError& error) { | 41 const ModelError& error) { |
| 74 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeRunFailures", | 42 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeRunFailures", |
| 75 ModelTypeToHistogramInt(model_type), | 43 ModelTypeToHistogramInt(model_type), |
| 76 MODEL_TYPE_COUNT); | 44 MODEL_TYPE_COUNT); |
| 77 ui_thread->PostTask(error.location(), base::Bind(error_handler, error)); | 45 ui_thread->PostTask(error.location(), base::Bind(error_handler, error)); |
| 78 } | 46 } |
| 79 | 47 |
| 48 // This function allows us to return a Callback using Bind that returns the |
| 49 // given |arg|. This function itself does nothing. |
| 50 base::WeakPtr<ModelTypeSyncBridge> ReturnCapturedBridge( |
| 51 base::WeakPtr<ModelTypeSyncBridge> arg) { |
| 52 return arg; |
| 53 } |
| 54 |
| 55 void RunBridgeTask(const BridgeProvider& bridge_provider, |
| 56 const BridgeTask& task) { |
| 57 if (base::WeakPtr<ModelTypeSyncBridge> bridge = bridge_provider.Run()) { |
| 58 task.Run(bridge.get()); |
| 59 } |
| 60 } |
| 61 |
| 80 } // namespace | 62 } // namespace |
| 81 | 63 |
| 82 ModelTypeController::ModelTypeController( | 64 ModelTypeController::ModelTypeController( |
| 83 ModelType type, | 65 ModelType type, |
| 84 SyncClient* sync_client, | 66 SyncClient* sync_client, |
| 85 const scoped_refptr<base::SingleThreadTaskRunner>& model_thread) | 67 const scoped_refptr<base::SingleThreadTaskRunner>& model_thread) |
| 86 : DataTypeController(type), | 68 : DataTypeController(type), |
| 87 sync_client_(sync_client), | 69 sync_client_(sync_client), |
| 88 model_thread_(model_thread), | 70 model_thread_(model_thread), |
| 89 sync_prefs_(sync_client->GetPrefService()), | 71 sync_prefs_(sync_client->GetPrefService()), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 118 ModelTypeChangeProcessor::StartCallback callback = | 100 ModelTypeChangeProcessor::StartCallback callback = |
| 119 BindToCurrentThread(base::Bind(&ModelTypeController::OnProcessorStarted, | 101 BindToCurrentThread(base::Bind(&ModelTypeController::OnProcessorStarted, |
| 120 base::AsWeakPtr(this))); | 102 base::AsWeakPtr(this))); |
| 121 | 103 |
| 122 ModelErrorHandler error_handler = base::BindRepeating( | 104 ModelErrorHandler error_handler = base::BindRepeating( |
| 123 &ReportError, type(), base::ThreadTaskRunnerHandle::Get(), | 105 &ReportError, type(), base::ThreadTaskRunnerHandle::Get(), |
| 124 base::Bind(&ModelTypeController::ReportModelError, | 106 base::Bind(&ModelTypeController::ReportModelError, |
| 125 base::AsWeakPtr(this))); | 107 base::AsWeakPtr(this))); |
| 126 | 108 |
| 127 // Start the type processor on the model thread. | 109 // Start the type processor on the model thread. |
| 128 model_thread_->PostTask( | 110 PostBridgeTask(FROM_HERE, |
| 129 FROM_HERE, base::Bind(&CallOnSyncStartingHelper, sync_client_, type(), | 111 base::Bind(&OnSyncStartingHelper, std::move(error_handler), |
| 130 std::move(error_handler), std::move(callback))); | 112 std::move(callback))); |
| 131 } | 113 } |
| 132 | 114 |
| 133 void ModelTypeController::BeforeLoadModels(ModelTypeConfigurer* configurer) {} | 115 void ModelTypeController::BeforeLoadModels(ModelTypeConfigurer* configurer) {} |
| 134 | 116 |
| 135 void ModelTypeController::LoadModelsDone(ConfigureResult result, | 117 void ModelTypeController::LoadModelsDone(ConfigureResult result, |
| 136 const SyncError& error) { | 118 const SyncError& error) { |
| 137 DCHECK(CalledOnValidThread()); | 119 DCHECK(CalledOnValidThread()); |
| 138 | 120 |
| 139 if (state_ == NOT_RUNNING) { | 121 if (state_ == NOT_RUNNING) { |
| 140 // The callback arrived on the UI thread after the type has been already | 122 // The callback arrived on the UI thread after the type has been already |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 if (state() == NOT_RUNNING) | 203 if (state() == NOT_RUNNING) |
| 222 return; | 204 return; |
| 223 | 205 |
| 224 // Check preferences if datatype is not in preferred datatypes. Only call | 206 // Check preferences if datatype is not in preferred datatypes. Only call |
| 225 // DisableSync if the bridge is ready to handle it (controller is in loaded | 207 // DisableSync if the bridge is ready to handle it (controller is in loaded |
| 226 // state). | 208 // state). |
| 227 ModelTypeSet preferred_types = | 209 ModelTypeSet preferred_types = |
| 228 sync_prefs_.GetPreferredDataTypes(ModelTypeSet(type())); | 210 sync_prefs_.GetPreferredDataTypes(ModelTypeSet(type())); |
| 229 if ((state() == MODEL_LOADED || state() == RUNNING) && | 211 if ((state() == MODEL_LOADED || state() == RUNNING) && |
| 230 (!sync_prefs_.IsFirstSetupComplete() || !preferred_types.Has(type()))) { | 212 (!sync_prefs_.IsFirstSetupComplete() || !preferred_types.Has(type()))) { |
| 231 model_thread_->PostTask( | 213 PostBridgeTask(FROM_HERE, base::Bind(&ModelTypeSyncBridge::DisableSync)); |
| 232 FROM_HERE, base::Bind(&CallDisableSyncHelper, sync_client_, type())); | |
| 233 } | 214 } |
| 234 | 215 |
| 235 state_ = NOT_RUNNING; | 216 state_ = NOT_RUNNING; |
| 236 } | 217 } |
| 237 | 218 |
| 238 std::string ModelTypeController::name() const { | 219 std::string ModelTypeController::name() const { |
| 239 // For logging only. | 220 // For logging only. |
| 240 return ModelTypeToString(type()); | 221 return ModelTypeToString(type()); |
| 241 } | 222 } |
| 242 | 223 |
| 243 DataTypeController::State ModelTypeController::state() const { | 224 DataTypeController::State ModelTypeController::state() const { |
| 244 return state_; | 225 return state_; |
| 245 } | 226 } |
| 246 | 227 |
| 247 void ModelTypeController::GetAllNodes(const AllNodesCallback& callback) { | 228 void ModelTypeController::GetAllNodes(const AllNodesCallback& callback) { |
| 248 model_thread_->PostTask( | 229 PostBridgeTask(FROM_HERE, |
| 249 FROM_HERE, base::Bind(&CallGetAllNodesHelper, sync_client_, type(), | 230 base::Bind(&ModelTypeDebugInfo::GetAllNodes, callback)); |
| 250 BindToCurrentThread(callback))); | |
| 251 } | 231 } |
| 252 | 232 |
| 253 void ModelTypeController::GetStatusCounters( | 233 void ModelTypeController::GetStatusCounters( |
| 254 const StatusCountersCallback& callback) { | 234 const StatusCountersCallback& callback) { |
| 255 model_thread_->PostTask( | 235 PostBridgeTask(FROM_HERE, |
| 256 FROM_HERE, | 236 base::Bind(&ModelTypeDebugInfo::GetStatusCounters, callback)); |
| 257 base::Bind(&CallGetStatusCountersHelper, sync_client_, type(), callback)); | |
| 258 } | 237 } |
| 259 | 238 |
| 260 void ModelTypeController::ReportModelError(const ModelError& error) { | 239 void ModelTypeController::ReportModelError(const ModelError& error) { |
| 261 DCHECK(CalledOnValidThread()); | 240 DCHECK(CalledOnValidThread()); |
| 262 LoadModelsDone(UNRECOVERABLE_ERROR, | 241 LoadModelsDone(UNRECOVERABLE_ERROR, |
| 263 SyncError(error.location(), SyncError::DATATYPE_ERROR, | 242 SyncError(error.location(), SyncError::DATATYPE_ERROR, |
| 264 error.message(), type())); | 243 error.message(), type())); |
| 265 } | 244 } |
| 266 | 245 |
| 267 void ModelTypeController::RecordStartFailure(ConfigureResult result) const { | 246 void ModelTypeController::RecordStartFailure(ConfigureResult result) const { |
| 268 DCHECK(CalledOnValidThread()); | 247 DCHECK(CalledOnValidThread()); |
| 269 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures", | 248 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures", |
| 270 ModelTypeToHistogramInt(type()), MODEL_TYPE_COUNT); | 249 ModelTypeToHistogramInt(type()), MODEL_TYPE_COUNT); |
| 271 #define PER_DATA_TYPE_MACRO(type_str) \ | 250 #define PER_DATA_TYPE_MACRO(type_str) \ |
| 272 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "ConfigureFailure", result, \ | 251 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "ConfigureFailure", result, \ |
| 273 MAX_CONFIGURE_RESULT); | 252 MAX_CONFIGURE_RESULT); |
| 274 SYNC_DATA_TYPE_HISTOGRAM(type()); | 253 SYNC_DATA_TYPE_HISTOGRAM(type()); |
| 275 #undef PER_DATA_TYPE_MACRO | 254 #undef PER_DATA_TYPE_MACRO |
| 276 } | 255 } |
| 277 | 256 |
| 257 BridgeProvider ModelTypeController::GetBridgeProvider() { |
| 258 // Get the bridge eagerly, and capture the weak pointer. |
| 259 base::WeakPtr<ModelTypeSyncBridge> bridge = |
| 260 sync_client_->GetSyncBridgeForModelType(type()); |
| 261 return base::Bind(&ReturnCapturedBridge, bridge); |
| 262 } |
| 263 |
| 264 void ModelTypeController::PostBridgeTask( |
| 265 const tracked_objects::Location& location, |
| 266 const BridgeTask& task) { |
| 267 model_thread_->PostTask( |
| 268 location, base::Bind(&RunBridgeTask, GetBridgeProvider(), task)); |
| 269 } |
| 278 } // namespace syncer | 270 } // namespace syncer |
| OLD | NEW |