Home | History | Annotate | Download | only in glue
      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.
      4 
      5 #include "chrome/browser/sync/glue/frontend_data_type_controller.h"
      6 
      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"
     18 
     19 using content::BrowserThread;
     20 
     21 namespace browser_sync {
     22 
     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,
     31     Profile* profile,
     32     ProfileSyncService* sync_service)
     33     : DataTypeController(ui_thread, error_callback),
     34       profile_sync_factory_(profile_sync_factory),
     35       profile_(profile),
     36       sync_service_(sync_service),
     37       state_(NOT_RUNNING) {
     38   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     39   DCHECK(profile_sync_factory);
     40   DCHECK(profile);
     41   DCHECK(sync_service);
     42 }
     43 
     44 void FrontendDataTypeController::LoadModels(
     45     const ModelLoadCallback& model_load_callback) {
     46   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     47   model_load_callback_ = model_load_callback;
     48 
     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",
     54                                               type()));
     55     return;
     56   }
     57 
     58   state_ = MODEL_STARTING;
     59   if (!StartModels()) {
     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);
     64     return;
     65   }
     66 
     67   OnModelLoaded();
     68 }
     69 
     70 void FrontendDataTypeController::OnModelLoaded() {
     71   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     72   DCHECK_EQ(state_, MODEL_STARTING);
     73 
     74   state_ = MODEL_LOADED;
     75   model_load_callback_.Run(type(), syncer::SyncError());
     76 }
     77 
     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);
     83 
     84   start_callback_ = start_callback;
     85   state_ = ASSOCIATING;
     86   if (!Associate()) {
     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);
     90     return;
     91   }
     92   DCHECK_EQ(state_, RUNNING);
     93 }
     94 
     95 void FrontendDataTypeController::Stop() {
     96   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     97 
     98   if (state_ == NOT_RUNNING)
     99     return;
    100 
    101   State prev_state = state_;
    102   state_ = STOPPING;
    103 
    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) {
    107     AbortModelLoad();
    108     // We can just return here since we haven't performed association if we're
    109     // still in MODEL_STARTING.
    110     return;
    111   }
    112 
    113   CleanUpState();
    114 
    115   sync_service_->DeactivateDataType(type());
    116 
    117   if (model_associator()) {
    118     syncer::SyncError error;  // Not used.
    119     error = model_associator()->DisassociateModels();
    120   }
    121 
    122   set_model_associator(NULL);
    123   change_processor_.reset();
    124 
    125   state_ = NOT_RUNNING;
    126 }
    127 
    128 syncer::ModelSafeGroup FrontendDataTypeController::model_safe_group()
    129     const {
    130   return syncer::GROUP_UI;
    131 }
    132 
    133 std::string FrontendDataTypeController::name() const {
    134   // For logging only.
    135   return syncer::ModelTypeToString(type());
    136 }
    137 
    138 sync_driver::DataTypeController::State FrontendDataTypeController::state()
    139     const {
    140   return state_;
    141 }
    142 
    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(
    151         FROM_HERE,
    152         base::Bind(model_load_callback_, type(), error));
    153   }
    154 }
    155 
    156 FrontendDataTypeController::FrontendDataTypeController()
    157     : DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
    158       profile_sync_factory_(NULL),
    159       profile_(NULL),
    160       sync_service_(NULL),
    161       state_(NOT_RUNNING) {
    162 }
    163 
    164 FrontendDataTypeController::~FrontendDataTypeController() {
    165   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    166 }
    167 
    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.
    172   return true;
    173 }
    174 
    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);
    185 
    186   if (!error_callback_.is_null())
    187     error_callback_.Run();
    188 }
    189 
    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);
    197     return false;
    198   }
    199 
    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",
    205                             type());
    206     local_merge_result.set_error(error);
    207     StartDone(UNRECOVERABLE_ERROR, local_merge_result, syncer_merge_result);
    208     return false;
    209   }
    210 
    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(
    215       &local_merge_result,
    216       &syncer_merge_result);
    217   // TODO(lipalani): crbug.com/122690 - handle abort.
    218   RecordAssociationTime(base::TimeTicks::Now() - start_time);
    219   if (error.IsSet()) {
    220     local_merge_result.set_error(error);
    221     StartDone(ASSOCIATION_FAILED, local_merge_result, syncer_merge_result);
    222     return false;
    223   }
    224 
    225   state_ = RUNNING;
    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
    228   // error.
    229   StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK,
    230             local_merge_result,
    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;
    238 }
    239 
    240 void FrontendDataTypeController::CleanUpState() {
    241   // Do nothing by default.
    242 }
    243 
    244 void FrontendDataTypeController::CleanUp() {
    245   CleanUpState();
    246   set_model_associator(NULL);
    247   change_processor_.reset();
    248 }
    249 
    250 void FrontendDataTypeController::AbortModelLoad() {
    251   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    252   CleanUp();
    253   state_ = NOT_RUNNING;
    254 }
    255 
    256 void FrontendDataTypeController::StartDone(
    257     ConfigureResult start_result,
    258     const syncer::SyncMergeResult& local_merge_result,
    259     const syncer::SyncMergeResult& syncer_merge_result) {
    260   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    261   if (!IsSuccessfulResult(start_result)) {
    262     if (IsUnrecoverableResult(start_result))
    263       RecordUnrecoverableError(FROM_HERE, "StartFailed");
    264 
    265     CleanUp();
    266     if (start_result == ASSOCIATION_FAILED) {
    267       state_ = DISABLED;
    268     } else {
    269       state_ = NOT_RUNNING;
    270     }
    271     RecordStartFailure(start_result);
    272   }
    273 
    274   start_callback_.Run(start_result, local_merge_result, syncer_merge_result);
    275 }
    276 
    277 void FrontendDataTypeController::RecordAssociationTime(base::TimeDelta time) {
    278   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    279 #define PER_DATA_TYPE_MACRO(type_str) \
    280     UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
    281   SYNC_DATA_TYPE_HISTOGRAM(type());
    282 #undef PER_DATA_TYPE_MACRO
    283 }
    284 
    285 void FrontendDataTypeController::RecordStartFailure(ConfigureResult result) {
    286   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    287   UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
    288                             ModelTypeToHistogramInt(type()),
    289                             syncer::MODEL_TYPE_COUNT);
    290 #define PER_DATA_TYPE_MACRO(type_str) \
    291     UMA_HISTOGRAM_ENUMERATION("Sync." type_str "StartFailure", result, \
    292                               MAX_START_RESULT);
    293   SYNC_DATA_TYPE_HISTOGRAM(type());
    294 #undef PER_DATA_TYPE_MACRO
    295 }
    296 
    297 sync_driver::AssociatorInterface* FrontendDataTypeController::model_associator()
    298     const {
    299   return model_associator_.get();
    300 }
    301 
    302 void FrontendDataTypeController::set_model_associator(
    303     sync_driver::AssociatorInterface* model_associator) {
    304   model_associator_.reset(model_associator);
    305 }
    306 
    307 sync_driver::ChangeProcessor* FrontendDataTypeController::GetChangeProcessor()
    308     const {
    309   return change_processor_.get();
    310 }
    311 
    312 void FrontendDataTypeController::set_change_processor(
    313     sync_driver::ChangeProcessor* change_processor) {
    314   change_processor_.reset(change_processor);
    315 }
    316 
    317 }  // namespace browser_sync
    318