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/non_blocking_data_type_controller.h" | 5 #include "components/sync_driver/non_blocking_data_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" | 10 #include "base/bind_helpers.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 NonBlockingDataTypeController::NonBlockingDataTypeController( | 23 NonBlockingDataTypeController::NonBlockingDataTypeController( |
24 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, | 24 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread, |
25 const base::Closure& error_callback, | 25 const base::Closure& error_callback, |
26 syncer::ModelType model_type, | 26 syncer::ModelType model_type, |
27 sync_driver::SyncClient* sync_client) | 27 sync_driver::SyncClient* sync_client) |
28 : sync_driver::DataTypeController(ui_thread, error_callback), | 28 : sync_driver::DataTypeController(ui_thread, error_callback), |
29 model_type_(model_type), | 29 model_type_(model_type), |
30 sync_client_(sync_client), | 30 sync_client_(sync_client), |
31 state_(NOT_RUNNING) { | 31 state_(NOT_RUNNING) { |
32 // TODO(gangwu): should initial processor somewhere else in | 32 DCHECK(BelongsToUIThread()); |
33 // NonBlockingDataTypeController | |
34 InitializeProcessor(); | |
35 } | 33 } |
36 | 34 |
37 NonBlockingDataTypeController::~NonBlockingDataTypeController() {} | 35 NonBlockingDataTypeController::~NonBlockingDataTypeController() {} |
38 | 36 |
39 void NonBlockingDataTypeController::LoadModels( | 37 void NonBlockingDataTypeController::LoadModels( |
40 const ModelLoadCallback& model_load_callback) { | 38 const ModelLoadCallback& model_load_callback) { |
41 DCHECK(ui_thread()->BelongsToCurrentThread()); | 39 DCHECK(ui_thread()->BelongsToCurrentThread()); |
42 DCHECK(!model_load_callback.is_null()); | 40 DCHECK(!model_load_callback.is_null()); |
43 model_load_callback_ = model_load_callback; | 41 model_load_callback_ = model_load_callback; |
44 | 42 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 85 } |
88 | 86 |
89 if (!model_load_callback_.is_null()) { | 87 if (!model_load_callback_.is_null()) { |
90 model_load_callback_.Run(type(), error); | 88 model_load_callback_.Run(type(), error); |
91 } | 89 } |
92 } | 90 } |
93 | 91 |
94 void NonBlockingDataTypeController::OnProcessorStarted( | 92 void NonBlockingDataTypeController::OnProcessorStarted( |
95 syncer::SyncError error, | 93 syncer::SyncError error, |
96 scoped_ptr<syncer_v2::ActivationContext> activation_context) { | 94 scoped_ptr<syncer_v2::ActivationContext> activation_context) { |
97 if (BelongsToUIThread()) { | 95 RunOnUIThread( |
98 // Hold on to the activation context until ActivateDataType is called. | 96 FROM_HERE, |
99 if (state_ == MODEL_STARTING) { | 97 base::Bind(&NonBlockingDataTypeController::OnProcessorStartedOnUIThread, |
100 activation_context_ = std::move(activation_context); | 98 this, error, base::Passed(std::move(activation_context)))); |
101 } | 99 } |
102 // TODO(stanisc): Figure out if UNRECOVERABLE_ERROR is OK in this case. | 100 |
103 ConfigureResult result = error.IsSet() ? UNRECOVERABLE_ERROR : OK; | 101 void NonBlockingDataTypeController::OnProcessorStartedOnUIThread( |
104 LoadModelsDone(result, error); | 102 syncer::SyncError error, |
105 } else { | 103 scoped_ptr<syncer_v2::ActivationContext> activation_context) { |
106 RunOnUIThread( | 104 DCHECK(BelongsToUIThread()); |
107 FROM_HERE, | 105 // Hold on to the activation context until ActivateDataType is called. |
108 base::Bind(&NonBlockingDataTypeController::OnProcessorStarted, this, | 106 if (state_ == MODEL_STARTING) { |
109 error, base::Passed(std::move(activation_context)))); | 107 activation_context_ = std::move(activation_context); |
110 } | 108 } |
| 109 // TODO(stanisc): Figure out if UNRECOVERABLE_ERROR is OK in this case. |
| 110 ConfigureResult result = error.IsSet() ? UNRECOVERABLE_ERROR : OK; |
| 111 LoadModelsDone(result, error); |
111 } | 112 } |
112 | 113 |
113 void NonBlockingDataTypeController::StartAssociating( | 114 void NonBlockingDataTypeController::StartAssociating( |
114 const StartCallback& start_callback) { | 115 const StartCallback& start_callback) { |
115 DCHECK(BelongsToUIThread()); | 116 DCHECK(BelongsToUIThread()); |
116 DCHECK(!start_callback.is_null()); | 117 DCHECK(!start_callback.is_null()); |
117 | 118 |
118 state_ = RUNNING; | 119 state_ = RUNNING; |
119 | 120 |
120 // There is no association, just call back promptly. | 121 // There is no association, just call back promptly. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 161 |
161 sync_driver::DataTypeController::State NonBlockingDataTypeController::state() | 162 sync_driver::DataTypeController::State NonBlockingDataTypeController::state() |
162 const { | 163 const { |
163 return state_; | 164 return state_; |
164 } | 165 } |
165 | 166 |
166 bool NonBlockingDataTypeController::BelongsToUIThread() const { | 167 bool NonBlockingDataTypeController::BelongsToUIThread() const { |
167 return ui_thread()->BelongsToCurrentThread(); | 168 return ui_thread()->BelongsToCurrentThread(); |
168 } | 169 } |
169 | 170 |
170 void NonBlockingDataTypeController::RunOnUIThread( | |
171 const tracked_objects::Location& from_here, | |
172 const base::Closure& task) { | |
173 DCHECK(!BelongsToUIThread()); | |
174 ui_thread()->PostTask(from_here, task); | |
175 } | |
176 | |
177 void NonBlockingDataTypeController::OnSingleDataTypeUnrecoverableError( | 171 void NonBlockingDataTypeController::OnSingleDataTypeUnrecoverableError( |
178 const syncer::SyncError& error) { | 172 const syncer::SyncError& error) { |
179 RecordUnrecoverableError(); | 173 RecordUnrecoverableError(); |
180 if (!error_callback_.is_null()) | 174 if (!error_callback_.is_null()) |
181 error_callback_.Run(); | 175 error_callback_.Run(); |
182 | 176 |
183 ReportLoadModelError(UNRECOVERABLE_ERROR, error); | 177 ReportLoadModelError(UNRECOVERABLE_ERROR, error); |
184 } | 178 } |
185 | 179 |
186 void NonBlockingDataTypeController::ReportLoadModelError( | 180 void NonBlockingDataTypeController::ReportLoadModelError( |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 215 |
222 base::WeakPtr<syncer_v2::SharedModelTypeProcessor> | 216 base::WeakPtr<syncer_v2::SharedModelTypeProcessor> |
223 NonBlockingDataTypeController::type_processor() const { | 217 NonBlockingDataTypeController::type_processor() const { |
224 return type_processor_; | 218 return type_processor_; |
225 } | 219 } |
226 | 220 |
227 syncer::ModelType NonBlockingDataTypeController::type() const { | 221 syncer::ModelType NonBlockingDataTypeController::type() const { |
228 return model_type_; | 222 return model_type_; |
229 } | 223 } |
230 | 224 |
231 void NonBlockingDataTypeController::InitializeProcessor() { | 225 void NonBlockingDataTypeController::InitializeProcessorOnModelThread() { |
232 base::WeakPtr<syncer_v2::ModelTypeService> model_type_service = | 226 base::WeakPtr<syncer_v2::ModelTypeService> model_type_service = |
233 sync_client_->GetModelTypeServiceForType(type()); | 227 sync_client_->GetModelTypeServiceForType(type()); |
234 if (!model_type_service.get()) { | 228 if (!model_type_service.get()) { |
235 LOG(WARNING) << "ModelTypeService destroyed before " | 229 LOG(WARNING) << "ModelTypeService destroyed before " |
236 "ModelTypeController was started."; | 230 "ModelTypeController was started."; |
237 // TODO(gangwu): Add SyncError and then call start_callback with it. also | 231 // TODO(gangwu): Add SyncError and then call start_callback with it. also |
238 // set an error state to |state_|. | 232 // set an error state to |state_|. |
239 } | 233 } |
240 | 234 |
241 scoped_ptr<syncer_v2::SharedModelTypeProcessor> shared_model_type_processor( | 235 scoped_ptr<syncer_v2::SharedModelTypeProcessor> shared_model_type_processor( |
242 make_scoped_ptr(new syncer_v2::SharedModelTypeProcessor( | 236 make_scoped_ptr(new syncer_v2::SharedModelTypeProcessor( |
243 type(), model_type_service.get()))); | 237 type(), model_type_service.get()))); |
244 type_processor_ = shared_model_type_processor->AsWeakPtrForUI(); | 238 type_processor_ = shared_model_type_processor->AsWeakPtrForUI(); |
245 model_type_service->set_change_processor( | 239 model_type_service->set_change_processor( |
246 std::move(shared_model_type_processor)); | 240 std::move(shared_model_type_processor)); |
247 } | 241 } |
248 | 242 |
| 243 void NonBlockingDataTypeController::InitializeProcessor() { |
| 244 DCHECK(BelongsToUIThread()); |
| 245 RunOnModelThread( |
| 246 FROM_HERE, |
| 247 base::Bind( |
| 248 &NonBlockingDataTypeController::InitializeProcessorOnModelThread, |
| 249 this)); |
| 250 } |
| 251 |
249 } // namespace sync_driver_v2 | 252 } // namespace sync_driver_v2 |
OLD | NEW |