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_ui_data_type_controller.h" | 5 #include "components/sync/driver/non_ui_data_type_controller.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
9 #include "components/sync/api/data_type_error_handler_impl.h" | 9 #include "components/sync/api/data_type_error_handler_impl.h" |
10 #include "components/sync/api/sync_error.h" | 10 #include "components/sync/api/sync_error.h" |
11 #include "components/sync/api/sync_merge_result.h" | 11 #include "components/sync/api/sync_merge_result.h" |
12 #include "components/sync/api/syncable_service.h" | 12 #include "components/sync/api/syncable_service.h" |
13 #include "components/sync/base/bind_to_task_runner.h" | 13 #include "components/sync/base/bind_to_task_runner.h" |
14 #include "components/sync/base/data_type_histogram.h" | 14 #include "components/sync/base/data_type_histogram.h" |
15 #include "components/sync/base/model_type.h" | 15 #include "components/sync/base/model_type.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/sync_api_component_factory.h" | 17 #include "components/sync/driver/sync_api_component_factory.h" |
18 #include "components/sync/driver/sync_client.h" | 18 #include "components/sync/driver/sync_client.h" |
19 #include "components/sync/driver/sync_service.h" | 19 #include "components/sync/driver/sync_service.h" |
20 | 20 |
21 namespace syncer { | 21 namespace sync_driver { |
22 | 22 |
23 SharedChangeProcessor* NonUIDataTypeController::CreateSharedChangeProcessor() { | 23 SharedChangeProcessor* NonUIDataTypeController::CreateSharedChangeProcessor() { |
24 return new SharedChangeProcessor(type()); | 24 return new SharedChangeProcessor(type()); |
25 } | 25 } |
26 | 26 |
27 NonUIDataTypeController::NonUIDataTypeController( | 27 NonUIDataTypeController::NonUIDataTypeController( |
28 ModelType type, | 28 syncer::ModelType type, |
29 const base::Closure& dump_stack, | 29 const base::Closure& dump_stack, |
30 SyncClient* sync_client) | 30 SyncClient* sync_client) |
31 : DirectoryDataTypeController(type, dump_stack, sync_client), | 31 : DirectoryDataTypeController(type, dump_stack, sync_client), |
32 state_(NOT_RUNNING) {} | 32 state_(NOT_RUNNING) {} |
33 | 33 |
34 void NonUIDataTypeController::LoadModels( | 34 void NonUIDataTypeController::LoadModels( |
35 const ModelLoadCallback& model_load_callback) { | 35 const ModelLoadCallback& model_load_callback) { |
36 DCHECK(CalledOnValidThread()); | 36 DCHECK(CalledOnValidThread()); |
37 model_load_callback_ = model_load_callback; | 37 model_load_callback_ = model_load_callback; |
38 if (state() != NOT_RUNNING) { | 38 if (state() != NOT_RUNNING) { |
39 model_load_callback.Run(type(), | 39 model_load_callback.Run( |
40 SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, | 40 type(), syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
41 "Model already running", type())); | 41 "Model already running", type())); |
42 return; | 42 return; |
43 } | 43 } |
44 | 44 |
45 state_ = MODEL_STARTING; | 45 state_ = MODEL_STARTING; |
46 // Since we can't be called multiple times before Stop() is called, | 46 // Since we can't be called multiple times before Stop() is called, |
47 // |shared_change_processor_| must be NULL here. | 47 // |shared_change_processor_| must be NULL here. |
48 DCHECK(!shared_change_processor_.get()); | 48 DCHECK(!shared_change_processor_.get()); |
49 shared_change_processor_ = CreateSharedChangeProcessor(); | 49 shared_change_processor_ = CreateSharedChangeProcessor(); |
50 DCHECK(shared_change_processor_.get()); | 50 DCHECK(shared_change_processor_.get()); |
51 if (!StartModels()) { | 51 if (!StartModels()) { |
52 // If we are waiting for some external service to load before associating | 52 // If we are waiting for some external service to load before associating |
53 // or we failed to start the models, we exit early. | 53 // or we failed to start the models, we exit early. |
54 DCHECK(state() == MODEL_STARTING || state() == NOT_RUNNING); | 54 DCHECK(state() == MODEL_STARTING || state() == NOT_RUNNING); |
55 return; | 55 return; |
56 } | 56 } |
57 | 57 |
58 OnModelLoaded(); | 58 OnModelLoaded(); |
59 } | 59 } |
60 | 60 |
61 void NonUIDataTypeController::OnModelLoaded() { | 61 void NonUIDataTypeController::OnModelLoaded() { |
62 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
63 DCHECK_EQ(state_, MODEL_STARTING); | 63 DCHECK_EQ(state_, MODEL_STARTING); |
64 state_ = MODEL_LOADED; | 64 state_ = MODEL_LOADED; |
65 model_load_callback_.Run(type(), SyncError()); | 65 model_load_callback_.Run(type(), syncer::SyncError()); |
66 } | 66 } |
67 | 67 |
68 bool NonUIDataTypeController::StartModels() { | 68 bool NonUIDataTypeController::StartModels() { |
69 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
70 DCHECK_EQ(state_, MODEL_STARTING); | 70 DCHECK_EQ(state_, MODEL_STARTING); |
71 // By default, no additional services need to be started before we can proceed | 71 // By default, no additional services need to be started before we can proceed |
72 // with model association. | 72 // with model association. |
73 return true; | 73 return true; |
74 } | 74 } |
75 | 75 |
76 void NonUIDataTypeController::StopModels() { | 76 void NonUIDataTypeController::StopModels() { |
77 DCHECK(CalledOnValidThread()); | 77 DCHECK(CalledOnValidThread()); |
78 } | 78 } |
79 | 79 |
80 void NonUIDataTypeController::StartAssociating( | 80 void NonUIDataTypeController::StartAssociating( |
81 const StartCallback& start_callback) { | 81 const StartCallback& start_callback) { |
82 DCHECK(CalledOnValidThread()); | 82 DCHECK(CalledOnValidThread()); |
83 DCHECK(!start_callback.is_null()); | 83 DCHECK(!start_callback.is_null()); |
84 DCHECK_EQ(state_, MODEL_LOADED); | 84 DCHECK_EQ(state_, MODEL_LOADED); |
85 state_ = ASSOCIATING; | 85 state_ = ASSOCIATING; |
86 | 86 |
87 // Store UserShare now while on UI thread to avoid potential race | 87 // Store UserShare now while on UI thread to avoid potential race |
88 // condition in StartAssociationWithSharedChangeProcessor. | 88 // condition in StartAssociationWithSharedChangeProcessor. |
89 DCHECK(sync_client_->GetSyncService()); | 89 DCHECK(sync_client_->GetSyncService()); |
90 user_share_ = sync_client_->GetSyncService()->GetUserShare(); | 90 user_share_ = sync_client_->GetSyncService()->GetUserShare(); |
91 | 91 |
92 start_callback_ = start_callback; | 92 start_callback_ = start_callback; |
93 if (!StartAssociationAsync()) { | 93 if (!StartAssociationAsync()) { |
94 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, | 94 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
95 "Failed to post StartAssociation", type()); | 95 "Failed to post StartAssociation", type()); |
96 SyncMergeResult local_merge_result(type()); | 96 syncer::SyncMergeResult local_merge_result(type()); |
97 local_merge_result.set_error(error); | 97 local_merge_result.set_error(error); |
98 StartDone(ASSOCIATION_FAILED, local_merge_result, SyncMergeResult(type())); | 98 StartDone(ASSOCIATION_FAILED, local_merge_result, |
| 99 syncer::SyncMergeResult(type())); |
99 // StartDone should have cleared the SharedChangeProcessor. | 100 // StartDone should have cleared the SharedChangeProcessor. |
100 DCHECK(!shared_change_processor_.get()); | 101 DCHECK(!shared_change_processor_.get()); |
101 return; | 102 return; |
102 } | 103 } |
103 } | 104 } |
104 | 105 |
105 void NonUIDataTypeController::Stop() { | 106 void NonUIDataTypeController::Stop() { |
106 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
107 | 108 |
108 if (state() == NOT_RUNNING) | 109 if (state() == NOT_RUNNING) |
109 return; | 110 return; |
110 | 111 |
111 // Disconnect the change processor. At this point, the | 112 // Disconnect the change processor. At this point, the |
112 // SyncableService can no longer interact with the Syncer, even if | 113 // syncer::SyncableService can no longer interact with the Syncer, even if |
113 // it hasn't finished MergeDataAndStartSyncing. | 114 // it hasn't finished MergeDataAndStartSyncing. |
114 DisconnectSharedChangeProcessor(); | 115 DisconnectSharedChangeProcessor(); |
115 | 116 |
116 // If we haven't finished starting, we need to abort the start. | 117 // If we haven't finished starting, we need to abort the start. |
117 bool service_started = state() == ASSOCIATING || state() == RUNNING; | 118 bool service_started = state() == ASSOCIATING || state() == RUNNING; |
118 state_ = service_started ? STOPPING : NOT_RUNNING; | 119 state_ = service_started ? STOPPING : NOT_RUNNING; |
119 StopModels(); | 120 StopModels(); |
120 | 121 |
121 if (service_started) | 122 if (service_started) |
122 StopSyncableService(); | 123 StopSyncableService(); |
123 | 124 |
124 shared_change_processor_ = nullptr; | 125 shared_change_processor_ = nullptr; |
125 state_ = NOT_RUNNING; | 126 state_ = NOT_RUNNING; |
126 } | 127 } |
127 | 128 |
128 std::string NonUIDataTypeController::name() const { | 129 std::string NonUIDataTypeController::name() const { |
129 // For logging only. | 130 // For logging only. |
130 return ModelTypeToString(type()); | 131 return syncer::ModelTypeToString(type()); |
131 } | 132 } |
132 | 133 |
133 DataTypeController::State NonUIDataTypeController::state() const { | 134 DataTypeController::State NonUIDataTypeController::state() const { |
134 return state_; | 135 return state_; |
135 } | 136 } |
136 | 137 |
137 NonUIDataTypeController::NonUIDataTypeController() | 138 NonUIDataTypeController::NonUIDataTypeController() |
138 : DirectoryDataTypeController(UNSPECIFIED, base::Closure(), nullptr) {} | 139 : DirectoryDataTypeController(syncer::UNSPECIFIED, |
| 140 base::Closure(), |
| 141 nullptr) {} |
139 | 142 |
140 NonUIDataTypeController::~NonUIDataTypeController() {} | 143 NonUIDataTypeController::~NonUIDataTypeController() {} |
141 | 144 |
142 void NonUIDataTypeController::StartDone( | 145 void NonUIDataTypeController::StartDone( |
143 DataTypeController::ConfigureResult start_result, | 146 DataTypeController::ConfigureResult start_result, |
144 const SyncMergeResult& local_merge_result, | 147 const syncer::SyncMergeResult& local_merge_result, |
145 const SyncMergeResult& syncer_merge_result) { | 148 const syncer::SyncMergeResult& syncer_merge_result) { |
146 DCHECK(CalledOnValidThread()); | 149 DCHECK(CalledOnValidThread()); |
147 | 150 |
148 DataTypeController::State new_state; | 151 DataTypeController::State new_state; |
149 if (IsSuccessfulResult(start_result)) { | 152 if (IsSuccessfulResult(start_result)) { |
150 new_state = RUNNING; | 153 new_state = RUNNING; |
151 } else { | 154 } else { |
152 new_state = (start_result == ASSOCIATION_FAILED ? DISABLED : NOT_RUNNING); | 155 new_state = (start_result == ASSOCIATION_FAILED ? DISABLED : NOT_RUNNING); |
153 } | 156 } |
154 | 157 |
155 // If we failed to start up, and we haven't been stopped yet, we need to | 158 // If we failed to start up, and we haven't been stopped yet, we need to |
(...skipping 17 matching lines...) Expand all Loading... |
173 StopModels(); | 176 StopModels(); |
174 RecordStartFailure(start_result); | 177 RecordStartFailure(start_result); |
175 } | 178 } |
176 | 179 |
177 start_callback_.Run(start_result, local_merge_result, syncer_merge_result); | 180 start_callback_.Run(start_result, local_merge_result, syncer_merge_result); |
178 } | 181 } |
179 | 182 |
180 void NonUIDataTypeController::RecordStartFailure(ConfigureResult result) { | 183 void NonUIDataTypeController::RecordStartFailure(ConfigureResult result) { |
181 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
182 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures", | 185 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures", |
183 ModelTypeToHistogramInt(type()), MODEL_TYPE_COUNT); | 186 ModelTypeToHistogramInt(type()), |
| 187 syncer::MODEL_TYPE_COUNT); |
184 #define PER_DATA_TYPE_MACRO(type_str) \ | 188 #define PER_DATA_TYPE_MACRO(type_str) \ |
185 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "ConfigureFailure", result, \ | 189 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "ConfigureFailure", result, \ |
186 MAX_CONFIGURE_RESULT); | 190 MAX_CONFIGURE_RESULT); |
187 SYNC_DATA_TYPE_HISTOGRAM(type()); | 191 SYNC_DATA_TYPE_HISTOGRAM(type()); |
188 #undef PER_DATA_TYPE_MACRO | 192 #undef PER_DATA_TYPE_MACRO |
189 } | 193 } |
190 | 194 |
191 void NonUIDataTypeController::DisableImpl(const SyncError& error) { | 195 void NonUIDataTypeController::DisableImpl(const syncer::SyncError& error) { |
192 DCHECK(CalledOnValidThread()); | 196 DCHECK(CalledOnValidThread()); |
193 if (!model_load_callback_.is_null()) { | 197 if (!model_load_callback_.is_null()) { |
194 model_load_callback_.Run(type(), error); | 198 model_load_callback_.Run(type(), error); |
195 } | 199 } |
196 } | 200 } |
197 | 201 |
198 bool NonUIDataTypeController::StartAssociationAsync() { | 202 bool NonUIDataTypeController::StartAssociationAsync() { |
199 DCHECK(CalledOnValidThread()); | 203 DCHECK(CalledOnValidThread()); |
200 DCHECK_EQ(state(), ASSOCIATING); | 204 DCHECK_EQ(state(), ASSOCIATING); |
201 return PostTaskOnBackendThread( | 205 return PostTaskOnBackendThread( |
202 FROM_HERE, | 206 FROM_HERE, |
203 base::Bind( | 207 base::Bind( |
204 &SharedChangeProcessor::StartAssociation, shared_change_processor_, | 208 &SharedChangeProcessor::StartAssociation, shared_change_processor_, |
205 BindToCurrentThread(base::Bind(&NonUIDataTypeController::StartDone, | 209 syncer::BindToCurrentThread(base::Bind( |
206 base::AsWeakPtr(this))), | 210 &NonUIDataTypeController::StartDone, base::AsWeakPtr(this))), |
207 sync_client_, user_share_, base::Passed(CreateErrorHandler()))); | 211 sync_client_, user_share_, base::Passed(CreateErrorHandler()))); |
208 } | 212 } |
209 | 213 |
210 ChangeProcessor* NonUIDataTypeController::GetChangeProcessor() const { | 214 ChangeProcessor* NonUIDataTypeController::GetChangeProcessor() const { |
211 DCHECK(CalledOnValidThread()); | 215 DCHECK(CalledOnValidThread()); |
212 DCHECK_EQ(state_, RUNNING); | 216 DCHECK_EQ(state_, RUNNING); |
213 return shared_change_processor_->generic_change_processor(); | 217 return shared_change_processor_->generic_change_processor(); |
214 } | 218 } |
215 | 219 |
216 void NonUIDataTypeController::DisconnectSharedChangeProcessor() { | 220 void NonUIDataTypeController::DisconnectSharedChangeProcessor() { |
217 DCHECK(CalledOnValidThread()); | 221 DCHECK(CalledOnValidThread()); |
218 // |shared_change_processor_| can already be NULL if Stop() is | 222 // |shared_change_processor_| can already be NULL if Stop() is |
219 // called after StartDone(_, DISABLED, _). | 223 // called after StartDone(_, DISABLED, _). |
220 if (shared_change_processor_.get()) { | 224 if (shared_change_processor_.get()) { |
221 shared_change_processor_->Disconnect(); | 225 shared_change_processor_->Disconnect(); |
222 } | 226 } |
223 } | 227 } |
224 | 228 |
225 void NonUIDataTypeController::StopSyncableService() { | 229 void NonUIDataTypeController::StopSyncableService() { |
226 DCHECK(CalledOnValidThread()); | 230 DCHECK(CalledOnValidThread()); |
227 if (shared_change_processor_.get()) { | 231 if (shared_change_processor_.get()) { |
228 PostTaskOnBackendThread(FROM_HERE, | 232 PostTaskOnBackendThread(FROM_HERE, |
229 base::Bind(&SharedChangeProcessor::StopLocalService, | 233 base::Bind(&SharedChangeProcessor::StopLocalService, |
230 shared_change_processor_)); | 234 shared_change_processor_)); |
231 } | 235 } |
232 } | 236 } |
233 | 237 |
234 std::unique_ptr<DataTypeErrorHandler> | 238 std::unique_ptr<syncer::DataTypeErrorHandler> |
235 NonUIDataTypeController::CreateErrorHandler() { | 239 NonUIDataTypeController::CreateErrorHandler() { |
236 DCHECK(CalledOnValidThread()); | 240 DCHECK(CalledOnValidThread()); |
237 return base::MakeUnique<DataTypeErrorHandlerImpl>( | 241 return base::MakeUnique<syncer::DataTypeErrorHandlerImpl>( |
238 base::ThreadTaskRunnerHandle::Get(), dump_stack_, | 242 base::ThreadTaskRunnerHandle::Get(), dump_stack_, |
239 base::Bind(&NonUIDataTypeController::DisableImpl, base::AsWeakPtr(this))); | 243 base::Bind(&NonUIDataTypeController::DisableImpl, base::AsWeakPtr(this))); |
240 } | 244 } |
241 | 245 |
242 } // namespace syncer | 246 } // namespace sync_driver |
OLD | NEW |