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.
5 #include "components/sync_driver/ui_data_type_controller.h"
7 #include "base/location.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/sync_driver/generic_change_processor_factory.h"
14 #include "components/sync_driver/shared_change_processor_ref.h"
15 #include "components/sync_driver/sync_client.h"
16 #include "components/sync_driver/sync_service.h"
17 #include "sync/api/sync_error.h"
18 #include "sync/api/sync_merge_result.h"
19 #include "sync/api/syncable_service.h"
20 #include "sync/internal_api/public/base/model_type.h"
21 #include "sync/util/data_type_histogram.h"
23 namespace sync_driver
{
25 UIDataTypeController::UIDataTypeController()
26 : DirectoryDataTypeController(base::ThreadTaskRunnerHandle::Get(),
30 type_(syncer::UNSPECIFIED
) {}
32 UIDataTypeController::UIDataTypeController(
33 const scoped_refptr
<base::SingleThreadTaskRunner
>& ui_thread
,
34 const base::Closure
& error_callback
,
35 syncer::ModelType type
,
36 SyncClient
* sync_client
)
37 : DirectoryDataTypeController(ui_thread
, error_callback
),
38 sync_client_(sync_client
),
41 processor_factory_(new GenericChangeProcessorFactory()),
42 ui_thread_(ui_thread
) {
43 DCHECK(ui_thread_
->BelongsToCurrentThread());
44 DCHECK(syncer::IsRealDataType(type_
));
47 void UIDataTypeController::SetGenericChangeProcessorFactoryForTest(
48 scoped_ptr
<GenericChangeProcessorFactory
> factory
) {
49 DCHECK_EQ(state_
, NOT_RUNNING
);
50 processor_factory_
= factory
.Pass();
53 UIDataTypeController::~UIDataTypeController() {
54 DCHECK(ui_thread_
->BelongsToCurrentThread());
57 void UIDataTypeController::LoadModels(
58 const ModelLoadCallback
& model_load_callback
) {
59 DCHECK(ui_thread_
->BelongsToCurrentThread());
60 DCHECK(syncer::IsRealDataType(type_
));
61 model_load_callback_
= model_load_callback
;
62 if (state_
!= NOT_RUNNING
) {
63 model_load_callback
.Run(type(),
64 syncer::SyncError(FROM_HERE
,
65 syncer::SyncError::DATATYPE_ERROR
,
66 "Model already loaded",
70 // Since we can't be called multiple times before Stop() is called,
71 // |shared_change_processor_| must be NULL here.
72 DCHECK(!shared_change_processor_
.get());
73 shared_change_processor_
= new SharedChangeProcessor();
75 state_
= MODEL_STARTING
;
77 // If we are waiting for some external service to load before associating
78 // or we failed to start the models, we exit early. state_ will control
79 // what we perform next.
80 DCHECK(state_
== NOT_RUNNING
|| state_
== MODEL_STARTING
);
84 state_
= MODEL_LOADED
;
85 model_load_callback_
.Run(type(), syncer::SyncError());
88 void UIDataTypeController::OnModelLoaded() {
89 DCHECK(ui_thread_
->BelongsToCurrentThread());
90 DCHECK_EQ(state_
, MODEL_STARTING
);
92 state_
= MODEL_LOADED
;
93 model_load_callback_
.Run(type(), syncer::SyncError());
96 void UIDataTypeController::StartAssociating(
97 const StartCallback
& start_callback
) {
98 DCHECK(ui_thread_
->BelongsToCurrentThread());
99 DCHECK(!start_callback
.is_null());
100 DCHECK_EQ(state_
, MODEL_LOADED
);
102 start_callback_
= start_callback
;
103 state_
= ASSOCIATING
;
104 base::ThreadTaskRunnerHandle::Get()->PostTask(
105 FROM_HERE
, base::Bind(&UIDataTypeController::Associate
, this));
108 bool UIDataTypeController::StartModels() {
109 DCHECK_EQ(state_
, MODEL_STARTING
);
110 // By default, no additional services need to be started before we can proceed
111 // with model association.
115 void UIDataTypeController::Associate() {
116 if (state_
!= ASSOCIATING
) {
117 // Stop() must have been called while Associate() task have been waiting.
118 DCHECK_EQ(state_
, NOT_RUNNING
);
122 syncer::SyncMergeResult
local_merge_result(type());
123 syncer::SyncMergeResult
syncer_merge_result(type());
124 base::WeakPtrFactory
<syncer::SyncMergeResult
> weak_ptr_factory(
125 &syncer_merge_result
);
127 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
129 tracked_objects::ScopedTracker
tracking_profile1(
130 FROM_HERE_WITH_EXPLICIT_FUNCTION(
131 "471403 UIDataTypeController::Associate1"));
133 // Connect |shared_change_processor_| to the syncer and get the
134 // syncer::SyncableService associated with type().
135 DCHECK(sync_client_
->GetSyncService());
136 local_service_
= shared_change_processor_
->Connect(
137 sync_client_
, processor_factory_
.get(),
138 sync_client_
->GetSyncService()->GetUserShare(), this, type(),
139 weak_ptr_factory
.GetWeakPtr());
140 if (!local_service_
.get()) {
141 syncer::SyncError
error(FROM_HERE
,
142 syncer::SyncError::DATATYPE_ERROR
,
143 "Failed to connect to syncer.",
145 local_merge_result
.set_error(error
);
146 StartDone(ASSOCIATION_FAILED
,
148 syncer_merge_result
);
152 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
154 tracked_objects::ScopedTracker
tracking_profile2(
155 FROM_HERE_WITH_EXPLICIT_FUNCTION(
156 "471403 UIDataTypeController::Associate2"));
157 if (!shared_change_processor_
->CryptoReadyIfNecessary()) {
158 syncer::SyncError
error(FROM_HERE
,
159 syncer::SyncError::CRYPTO_ERROR
,
162 local_merge_result
.set_error(error
);
163 StartDone(NEEDS_CRYPTO
,
165 syncer_merge_result
);
169 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
171 tracked_objects::ScopedTracker
tracking_profile3(
172 FROM_HERE_WITH_EXPLICIT_FUNCTION(
173 "471403 UIDataTypeController::Associate3"));
174 bool sync_has_nodes
= false;
175 if (!shared_change_processor_
->SyncModelHasUserCreatedNodes(
177 syncer::SyncError
error(FROM_HERE
,
178 syncer::SyncError::UNRECOVERABLE_ERROR
,
179 "Failed to load sync nodes",
181 local_merge_result
.set_error(error
);
182 StartDone(UNRECOVERABLE_ERROR
,
184 syncer_merge_result
);
188 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
190 tracked_objects::ScopedTracker
tracking_profile4(
191 FROM_HERE_WITH_EXPLICIT_FUNCTION(
192 "471403 UIDataTypeController::Associate4"));
193 base::TimeTicks start_time
= base::TimeTicks::Now();
194 syncer::SyncDataList initial_sync_data
;
195 syncer::SyncError error
=
196 shared_change_processor_
->GetAllSyncDataReturnError(
197 type(), &initial_sync_data
);
199 local_merge_result
.set_error(error
);
200 StartDone(ASSOCIATION_FAILED
,
202 syncer_merge_result
);
206 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
208 tracked_objects::ScopedTracker
tracking_profile5(
209 FROM_HERE_WITH_EXPLICIT_FUNCTION(
210 "471403 UIDataTypeController::Associate5"));
211 std::string datatype_context
;
212 if (shared_change_processor_
->GetDataTypeContext(&datatype_context
)) {
213 local_service_
->UpdateDataTypeContext(
214 type(), syncer::SyncChangeProcessor::NO_REFRESH
, datatype_context
);
217 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
219 tracked_objects::ScopedTracker
tracking_profile6(
220 FROM_HERE_WITH_EXPLICIT_FUNCTION(
221 "471403 UIDataTypeController::Associate6"));
222 syncer_merge_result
.set_num_items_before_association(
223 initial_sync_data
.size());
224 // Passes a reference to |shared_change_processor_|.
225 local_merge_result
= local_service_
->MergeDataAndStartSyncing(
228 scoped_ptr
<syncer::SyncChangeProcessor
>(
229 new SharedChangeProcessorRef(shared_change_processor_
)),
230 scoped_ptr
<syncer::SyncErrorFactory
>(
231 new SharedChangeProcessorRef(shared_change_processor_
)));
232 RecordAssociationTime(base::TimeTicks::Now() - start_time
);
233 if (local_merge_result
.error().IsSet()) {
234 StartDone(ASSOCIATION_FAILED
,
236 syncer_merge_result
);
240 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
242 tracked_objects::ScopedTracker
tracking_profile7(
243 FROM_HERE_WITH_EXPLICIT_FUNCTION(
244 "471403 UIDataTypeController::Associate7"));
245 syncer_merge_result
.set_num_items_after_association(
246 shared_change_processor_
->GetSyncCount());
249 StartDone(sync_has_nodes
? OK
: OK_FIRST_RUN
,
251 syncer_merge_result
);
254 ChangeProcessor
* UIDataTypeController::GetChangeProcessor() const {
255 DCHECK_EQ(state_
, RUNNING
);
256 return shared_change_processor_
->generic_change_processor();
259 void UIDataTypeController::AbortModelLoad() {
260 DCHECK(ui_thread_
->BelongsToCurrentThread());
261 state_
= NOT_RUNNING
;
263 if (shared_change_processor_
.get()) {
264 shared_change_processor_
= NULL
;
267 // We don't want to continue loading models (e.g OnModelLoaded should never be
268 // called after we've decided to abort).
272 void UIDataTypeController::StartDone(
273 ConfigureResult start_result
,
274 const syncer::SyncMergeResult
& local_merge_result
,
275 const syncer::SyncMergeResult
& syncer_merge_result
) {
276 DCHECK(ui_thread_
->BelongsToCurrentThread());
278 // TODO(robliao): Remove ScopedTracker below once https://crbug.com/471403 is
280 tracked_objects::ScopedTracker
tracking_profile(
281 FROM_HERE_WITH_EXPLICIT_FUNCTION(
282 "471403 UIDataTypeController::StartDone"));
284 if (!IsSuccessfulResult(start_result
)) {
286 if (start_result
== ASSOCIATION_FAILED
) {
289 state_
= NOT_RUNNING
;
291 RecordStartFailure(start_result
);
293 if (shared_change_processor_
.get()) {
294 shared_change_processor_
->Disconnect();
295 shared_change_processor_
= NULL
;
299 start_callback_
.Run(start_result
, local_merge_result
, syncer_merge_result
);
302 void UIDataTypeController::Stop() {
303 DCHECK(ui_thread_
->BelongsToCurrentThread());
304 DCHECK(syncer::IsRealDataType(type_
));
306 if (state_
== NOT_RUNNING
)
309 State prev_state
= state_
;
312 if (shared_change_processor_
.get()) {
313 shared_change_processor_
->Disconnect();
314 shared_change_processor_
= NULL
;
317 // If Stop() is called while Start() is waiting for the datatype model to
318 // load, abort the start.
319 if (prev_state
== MODEL_STARTING
) {
321 // We can just return here since we haven't performed association if we're
322 // still in MODEL_STARTING.
328 if (local_service_
.get()) {
329 local_service_
->StopSyncing(type());
332 state_
= NOT_RUNNING
;
335 syncer::ModelType
UIDataTypeController::type() const {
336 DCHECK(syncer::IsRealDataType(type_
));
340 void UIDataTypeController::StopModels() {
341 // Do nothing by default.
344 syncer::ModelSafeGroup
UIDataTypeController::model_safe_group() const {
345 DCHECK(syncer::IsRealDataType(type_
));
346 return syncer::GROUP_UI
;
349 std::string
UIDataTypeController::name() const {
351 return syncer::ModelTypeToString(type());
354 DataTypeController::State
UIDataTypeController::state() const {
358 void UIDataTypeController::OnSingleDataTypeUnrecoverableError(
359 const syncer::SyncError
& error
) {
360 DCHECK_EQ(type(), error
.model_type());
361 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeRunFailures",
362 ModelTypeToHistogramInt(type()),
363 syncer::MODEL_TYPE_COUNT
);
364 // TODO(tim): We double-upload some errors. See bug 383480.
365 if (!error_callback_
.is_null())
366 error_callback_
.Run();
367 if (!model_load_callback_
.is_null()) {
368 base::ThreadTaskRunnerHandle::Get()->PostTask(
369 FROM_HERE
, base::Bind(model_load_callback_
, type(), error
));
373 void UIDataTypeController::RecordAssociationTime(base::TimeDelta time
) {
374 DCHECK(ui_thread_
->BelongsToCurrentThread());
375 #define PER_DATA_TYPE_MACRO(type_str) \
376 UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
377 SYNC_DATA_TYPE_HISTOGRAM(type());
378 #undef PER_DATA_TYPE_MACRO
381 void UIDataTypeController::RecordStartFailure(ConfigureResult result
) {
382 DCHECK(ui_thread_
->BelongsToCurrentThread());
383 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
384 ModelTypeToHistogramInt(type()),
385 syncer::MODEL_TYPE_COUNT
);
386 #define PER_DATA_TYPE_MACRO(type_str) \
387 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "ConfigureFailure", result, \
388 MAX_CONFIGURE_RESULT);
389 SYNC_DATA_TYPE_HISTOGRAM(type());
390 #undef PER_DATA_TYPE_MACRO
393 } // namespace sync_driver