1 // Copyright (c) 2012 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 "chrome/browser/sync/glue/frontend_data_type_controller.h"
7 #include "base/logging.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
10 #include "chrome/browser/sync/profile_sync_components_factory.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "components/sync_driver/change_processor.h"
13 #include "components/sync_driver/model_associator.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "sync/api/sync_error.h"
16 #include "sync/internal_api/public/base/model_type.h"
17 #include "sync/util/data_type_histogram.h"
19 using content::BrowserThread;
21 namespace browser_sync {
23 // TODO(tim): Legacy controllers are being left behind in componentization
24 // effort for now, hence passing null DisableTypeCallback and still having
25 // a dependency on ProfileSyncService. That dep can probably be removed
26 // without too much work.
27 FrontendDataTypeController::FrontendDataTypeController(
28 scoped_refptr<base::MessageLoopProxy> ui_thread,
29 const base::Closure& error_callback,
30 ProfileSyncComponentsFactory* profile_sync_factory,
32 ProfileSyncService* sync_service)
33 : DataTypeController(ui_thread, error_callback),
34 profile_sync_factory_(profile_sync_factory),
36 sync_service_(sync_service),
38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
39 DCHECK(profile_sync_factory);
44 void FrontendDataTypeController::LoadModels(
45 const ModelLoadCallback& model_load_callback) {
46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
47 model_load_callback_ = model_load_callback;
49 if (state_ != NOT_RUNNING) {
50 model_load_callback.Run(type(),
51 syncer::SyncError(FROM_HERE,
52 syncer::SyncError::DATATYPE_ERROR,
53 "Model already running",
58 state_ = MODEL_STARTING;
60 // If we are waiting for some external service to load before associating
61 // or we failed to start the models, we exit early. state_ will control
62 // what we perform next.
63 DCHECK(state_ == NOT_RUNNING || state_ == MODEL_STARTING);
70 void FrontendDataTypeController::OnModelLoaded() {
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72 DCHECK_EQ(state_, MODEL_STARTING);
74 state_ = MODEL_LOADED;
75 model_load_callback_.Run(type(), syncer::SyncError());
78 void FrontendDataTypeController::StartAssociating(
79 const StartCallback& start_callback) {
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
81 DCHECK(!start_callback.is_null());
82 DCHECK_EQ(state_, MODEL_LOADED);
84 start_callback_ = start_callback;
87 // It's possible StartDone(..) resulted in a Stop() call, or that
88 // association failed, so we just verify that the state has moved forward.
89 DCHECK_NE(state_, ASSOCIATING);
92 DCHECK_EQ(state_, RUNNING);
95 void FrontendDataTypeController::Stop() {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 if (state_ == NOT_RUNNING)
101 State prev_state = state_;
104 // If Stop() is called while Start() is waiting for the datatype model to
105 // load, abort the start.
106 if (prev_state == MODEL_STARTING) {
108 // We can just return here since we haven't performed association if we're
109 // still in MODEL_STARTING.
115 sync_service_->DeactivateDataType(type());
117 if (model_associator()) {
118 syncer::SyncError error; // Not used.
119 error = model_associator()->DisassociateModels();
122 set_model_associator(NULL);
123 change_processor_.reset();
125 state_ = NOT_RUNNING;
128 syncer::ModelSafeGroup FrontendDataTypeController::model_safe_group()
130 return syncer::GROUP_UI;
133 std::string FrontendDataTypeController::name() const {
135 return syncer::ModelTypeToString(type());
138 sync_driver::DataTypeController::State FrontendDataTypeController::state()
143 void FrontendDataTypeController::OnSingleDataTypeUnrecoverableError(
144 const syncer::SyncError& error) {
145 DCHECK_EQ(type(), error.model_type());
146 RecordUnrecoverableError(error.location(), error.message());
147 if (!model_load_callback_.is_null()) {
148 syncer::SyncMergeResult local_merge_result(type());
149 local_merge_result.set_error(error);
150 base::MessageLoop::current()->PostTask(
152 base::Bind(model_load_callback_, type(), error));
156 FrontendDataTypeController::FrontendDataTypeController()
157 : DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
158 profile_sync_factory_(NULL),
161 state_(NOT_RUNNING) {
164 FrontendDataTypeController::~FrontendDataTypeController() {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
168 bool FrontendDataTypeController::StartModels() {
169 DCHECK_EQ(state_, MODEL_STARTING);
170 // By default, no additional services need to be started before we can proceed
171 // with model association.
175 void FrontendDataTypeController::RecordUnrecoverableError(
176 const tracked_objects::Location& from_here,
177 const std::string& message) {
178 DVLOG(1) << "Datatype Controller failed for type "
179 << ModelTypeToString(type()) << " "
180 << message << " at location "
181 << from_here.ToString();
182 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeRunFailures",
183 ModelTypeToHistogramInt(type()),
184 syncer::MODEL_TYPE_COUNT);
186 if (!error_callback_.is_null())
187 error_callback_.Run();
190 bool FrontendDataTypeController::Associate() {
191 DCHECK_EQ(state_, ASSOCIATING);
192 syncer::SyncMergeResult local_merge_result(type());
193 syncer::SyncMergeResult syncer_merge_result(type());
194 CreateSyncComponents();
195 if (!model_associator()->CryptoReadyIfNecessary()) {
196 StartDone(NEEDS_CRYPTO, local_merge_result, syncer_merge_result);
200 bool sync_has_nodes = false;
201 if (!model_associator()->SyncModelHasUserCreatedNodes(&sync_has_nodes)) {
202 syncer::SyncError error(FROM_HERE,
203 syncer::SyncError::UNRECOVERABLE_ERROR,
204 "Failed to load sync nodes",
206 local_merge_result.set_error(error);
207 StartDone(UNRECOVERABLE_ERROR, local_merge_result, syncer_merge_result);
211 // TODO(zea): Have AssociateModels fill the local and syncer merge results.
212 base::TimeTicks start_time = base::TimeTicks::Now();
213 syncer::SyncError error;
214 error = model_associator()->AssociateModels(
216 &syncer_merge_result);
217 // TODO(lipalani): crbug.com/122690 - handle abort.
218 RecordAssociationTime(base::TimeTicks::Now() - start_time);
220 local_merge_result.set_error(error);
221 StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
226 // FinishStart() invokes the DataTypeManager callback, which can lead to a
227 // call to Stop() if one of the other data types being started generates an
229 StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK,
231 syncer_merge_result);
232 // Return false if we're not in the RUNNING state (due to Stop() being called
233 // from FinishStart()).
234 // TODO(zea/atwilson): Should we maybe move the call to FinishStart() out of
235 // Associate() and into Start(), so we don't need this logic here? It seems
236 // cleaner to call FinishStart() from Start().
237 return state_ == RUNNING;
240 void FrontendDataTypeController::CleanUpState() {
241 // Do nothing by default.
244 void FrontendDataTypeController::CleanUp() {
246 set_model_associator(NULL);
247 change_processor_.reset();
250 void FrontendDataTypeController::AbortModelLoad() {
251 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
253 state_ = NOT_RUNNING;
254 model_load_callback_.Run(
257 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, "Aborted", type()));
260 void FrontendDataTypeController::StartDone(
261 ConfigureResult start_result,
262 const syncer::SyncMergeResult& local_merge_result,
263 const syncer::SyncMergeResult& syncer_merge_result) {
264 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 if (!IsSuccessfulResult(start_result)) {
266 if (IsUnrecoverableResult(start_result))
267 RecordUnrecoverableError(FROM_HERE, "StartFailed");
270 if (start_result == ASSOCIATION_FAILED) {
273 state_ = NOT_RUNNING;
275 RecordStartFailure(start_result);
278 start_callback_.Run(start_result, local_merge_result, syncer_merge_result);
281 void FrontendDataTypeController::RecordAssociationTime(base::TimeDelta time) {
282 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
283 #define PER_DATA_TYPE_MACRO(type_str) \
284 UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
285 SYNC_DATA_TYPE_HISTOGRAM(type());
286 #undef PER_DATA_TYPE_MACRO
289 void FrontendDataTypeController::RecordStartFailure(ConfigureResult result) {
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
291 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
292 ModelTypeToHistogramInt(type()),
293 syncer::MODEL_TYPE_COUNT);
294 #define PER_DATA_TYPE_MACRO(type_str) \
295 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "StartFailure", result, \
297 SYNC_DATA_TYPE_HISTOGRAM(type());
298 #undef PER_DATA_TYPE_MACRO
301 sync_driver::AssociatorInterface* FrontendDataTypeController::model_associator()
303 return model_associator_.get();
306 void FrontendDataTypeController::set_model_associator(
307 sync_driver::AssociatorInterface* model_associator) {
308 model_associator_.reset(model_associator);
311 sync_driver::ChangeProcessor* FrontendDataTypeController::GetChangeProcessor()
313 return change_processor_.get();
316 void FrontendDataTypeController::set_change_processor(
317 sync_driver::ChangeProcessor* change_processor) {
318 change_processor_.reset(change_processor);
321 } // namespace browser_sync