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/change_processor.h"
10 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
11 #include "chrome/browser/sync/glue/model_associator.h"
12 #include "chrome/browser/sync/profile_sync_components_factory.h"
13 #include "chrome/browser/sync/profile_sync_service.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 FrontendDataTypeController::FrontendDataTypeController(
24 ProfileSyncComponentsFactory* profile_sync_factory,
26 ProfileSyncService* sync_service)
27 : profile_sync_factory_(profile_sync_factory),
29 sync_service_(sync_service),
31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32 DCHECK(profile_sync_factory);
37 void FrontendDataTypeController::LoadModels(
38 const ModelLoadCallback& model_load_callback) {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
40 DCHECK(!model_load_callback.is_null());
42 if (state_ != NOT_RUNNING) {
43 model_load_callback.Run(type(),
44 syncer::SyncError(FROM_HERE,
45 syncer::SyncError::DATATYPE_ERROR,
46 "Model already running",
51 DCHECK(model_load_callback_.is_null());
53 model_load_callback_ = model_load_callback;
54 state_ = MODEL_STARTING;
56 // If we are waiting for some external service to load before associating
57 // or we failed to start the models, we exit early. state_ will control
58 // what we perform next.
59 DCHECK(state_ == NOT_RUNNING || state_ == MODEL_STARTING);
66 void FrontendDataTypeController::OnModelLoaded() {
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
68 DCHECK(!model_load_callback_.is_null());
69 DCHECK_EQ(state_, MODEL_STARTING);
71 state_ = MODEL_LOADED;
72 ModelLoadCallback model_load_callback = model_load_callback_;
73 model_load_callback_.Reset();
74 model_load_callback.Run(type(), syncer::SyncError());
77 void FrontendDataTypeController::StartAssociating(
78 const StartCallback& start_callback) {
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 DCHECK(!start_callback.is_null());
81 DCHECK(start_callback_.is_null());
82 DCHECK_EQ(state_, MODEL_LOADED);
84 start_callback_ = start_callback;
87 // We failed to associate and are aborting.
88 DCHECK(state_ == DISABLED || state_ == NOT_RUNNING);
91 DCHECK_EQ(state_, RUNNING);
94 void FrontendDataTypeController::Stop() {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
97 State prev_state = state_;
100 // If Stop() is called while Start() is waiting for the datatype model to
101 // load, abort the start.
102 if (prev_state == MODEL_STARTING) {
104 // We can just return here since we haven't performed association if we're
105 // still in MODEL_STARTING.
108 DCHECK(start_callback_.is_null());
112 sync_service_->DeactivateDataType(type());
114 if (model_associator()) {
115 syncer::SyncError error; // Not used.
116 error = model_associator()->DisassociateModels();
119 set_model_associator(NULL);
120 change_processor_.reset();
122 state_ = NOT_RUNNING;
125 syncer::ModelSafeGroup FrontendDataTypeController::model_safe_group()
127 return syncer::GROUP_UI;
130 std::string FrontendDataTypeController::name() const {
132 return syncer::ModelTypeToString(type());
135 DataTypeController::State FrontendDataTypeController::state() const {
139 void FrontendDataTypeController::OnSingleDatatypeUnrecoverableError(
140 const tracked_objects::Location& from_here, const std::string& message) {
141 RecordUnrecoverableError(from_here, message);
142 sync_service_->DisableBrokenDatatype(type(), from_here, message);
145 FrontendDataTypeController::FrontendDataTypeController()
146 : profile_sync_factory_(NULL),
149 state_(NOT_RUNNING) {
152 FrontendDataTypeController::~FrontendDataTypeController() {
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156 bool FrontendDataTypeController::StartModels() {
157 DCHECK_EQ(state_, MODEL_STARTING);
158 // By default, no additional services need to be started before we can proceed
159 // with model association.
163 bool FrontendDataTypeController::Associate() {
164 DCHECK_EQ(state_, ASSOCIATING);
165 syncer::SyncMergeResult local_merge_result(type());
166 syncer::SyncMergeResult syncer_merge_result(type());
167 CreateSyncComponents();
168 if (!model_associator()->CryptoReadyIfNecessary()) {
169 StartDone(NEEDS_CRYPTO, local_merge_result, syncer_merge_result);
173 bool sync_has_nodes = false;
174 if (!model_associator()->SyncModelHasUserCreatedNodes(&sync_has_nodes)) {
175 syncer::SyncError error(FROM_HERE,
176 syncer::SyncError::UNRECOVERABLE_ERROR,
177 "Failed to load sync nodes",
179 local_merge_result.set_error(error);
180 StartDone(UNRECOVERABLE_ERROR, local_merge_result, syncer_merge_result);
184 // TODO(zea): Have AssociateModels fill the local and syncer merge results.
185 base::TimeTicks start_time = base::TimeTicks::Now();
186 syncer::SyncError error;
187 error = model_associator()->AssociateModels(
189 &syncer_merge_result);
190 // TODO(lipalani): crbug.com/122690 - handle abort.
191 RecordAssociationTime(base::TimeTicks::Now() - start_time);
193 local_merge_result.set_error(error);
194 StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
198 sync_service_->ActivateDataType(type(), model_safe_group(),
201 // FinishStart() invokes the DataTypeManager callback, which can lead to a
202 // call to Stop() if one of the other data types being started generates an
204 StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK,
206 syncer_merge_result);
207 // Return false if we're not in the RUNNING state (due to Stop() being called
208 // from FinishStart()).
209 // TODO(zea/atwilson): Should we maybe move the call to FinishStart() out of
210 // Associate() and into Start(), so we don't need this logic here? It seems
211 // cleaner to call FinishStart() from Start().
212 return state_ == RUNNING;
215 void FrontendDataTypeController::CleanUpState() {
216 // Do nothing by default.
219 void FrontendDataTypeController::CleanUp() {
221 set_model_associator(NULL);
222 change_processor_.reset();
225 void FrontendDataTypeController::AbortModelLoad() {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
228 state_ = NOT_RUNNING;
229 ModelLoadCallback model_load_callback = model_load_callback_;
230 model_load_callback_.Reset();
231 model_load_callback.Run(type(),
232 syncer::SyncError(FROM_HERE,
233 syncer::SyncError::DATATYPE_ERROR,
238 void FrontendDataTypeController::StartDone(
239 StartResult start_result,
240 const syncer::SyncMergeResult& local_merge_result,
241 const syncer::SyncMergeResult& syncer_merge_result) {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
243 if (!IsSuccessfulResult(start_result)) {
244 if (IsUnrecoverableResult(start_result))
245 RecordUnrecoverableError(FROM_HERE, "StartFailed");
248 if (start_result == ASSOCIATION_FAILED) {
251 state_ = NOT_RUNNING;
253 RecordStartFailure(start_result);
256 // We have to release the callback before we call it, since it's possible
257 // invoking the callback will trigger a call to STOP(), which will get
258 // confused by the non-NULL start_callback_.
259 StartCallback callback = start_callback_;
260 start_callback_.Reset();
261 callback.Run(start_result, local_merge_result, syncer_merge_result);
264 void FrontendDataTypeController::RecordAssociationTime(base::TimeDelta time) {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
266 #define PER_DATA_TYPE_MACRO(type_str) \
267 UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
268 SYNC_DATA_TYPE_HISTOGRAM(type());
269 #undef PER_DATA_TYPE_MACRO
272 void FrontendDataTypeController::RecordStartFailure(StartResult result) {
273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
274 UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
275 ModelTypeToHistogramInt(type()),
276 syncer::MODEL_TYPE_COUNT);
277 #define PER_DATA_TYPE_MACRO(type_str) \
278 UMA_HISTOGRAM_ENUMERATION("Sync." type_str "StartFailure", result, \
280 SYNC_DATA_TYPE_HISTOGRAM(type());
281 #undef PER_DATA_TYPE_MACRO
284 AssociatorInterface* FrontendDataTypeController::model_associator() const {
285 return model_associator_.get();
288 void FrontendDataTypeController::set_model_associator(
289 AssociatorInterface* model_associator) {
290 model_associator_.reset(model_associator);
293 ChangeProcessor* FrontendDataTypeController::change_processor() const {
294 return change_processor_.get();
297 void FrontendDataTypeController::set_change_processor(
298 ChangeProcessor* change_processor) {
299 change_processor_.reset(change_processor);
302 } // namespace browser_sync