app_data_type_controller.cc revision dc0f95d653279beabeb9817299e2902918ba123e
1// Copyright (c) 2010 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/app_data_type_controller.h" 6 7#include "base/metrics/histogram.h" 8#include "base/logging.h" 9#include "base/time.h" 10#include "chrome/browser/profiles/profile.h" 11#include "chrome/browser/sync/profile_sync_service.h" 12#include "chrome/browser/sync/profile_sync_factory.h" 13#include "chrome/browser/sync/unrecoverable_error_handler.h" 14#include "content/browser/browser_thread.h" 15 16namespace browser_sync { 17 18AppDataTypeController::AppDataTypeController( 19 ProfileSyncFactory* profile_sync_factory, 20 Profile* profile, 21 ProfileSyncService* sync_service) 22 : profile_sync_factory_(profile_sync_factory), 23 profile_(profile), 24 sync_service_(sync_service), 25 state_(NOT_RUNNING) { 26 DCHECK(profile_sync_factory); 27 DCHECK(sync_service); 28} 29 30AppDataTypeController::~AppDataTypeController() { 31} 32 33void AppDataTypeController::Start(StartCallback* start_callback) { 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 35 DCHECK(start_callback); 36 if (state_ != NOT_RUNNING) { 37 start_callback->Run(BUSY); 38 delete start_callback; 39 return; 40 } 41 42 start_callback_.reset(start_callback); 43 44 profile_->InitExtensions(); 45 ProfileSyncFactory::SyncComponents sync_components = 46 profile_sync_factory_->CreateAppSyncComponents(sync_service_, 47 this); 48 model_associator_.reset(sync_components.model_associator); 49 change_processor_.reset(sync_components.change_processor); 50 51 bool sync_has_nodes = false; 52 if (!model_associator_->SyncModelHasUserCreatedNodes(&sync_has_nodes)) { 53 StartFailed(UNRECOVERABLE_ERROR); 54 return; 55 } 56 57 base::TimeTicks start_time = base::TimeTicks::Now(); 58 bool merge_success = model_associator_->AssociateModels(); 59 UMA_HISTOGRAM_TIMES("Sync.AppAssociationTime", 60 base::TimeTicks::Now() - start_time); 61 if (!merge_success) { 62 StartFailed(ASSOCIATION_FAILED); 63 return; 64 } 65 66 sync_service_->ActivateDataType(this, change_processor_.get()); 67 state_ = RUNNING; 68 FinishStart(!sync_has_nodes ? OK_FIRST_RUN : OK); 69} 70 71void AppDataTypeController::Stop() { 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 73 74 if (change_processor_ != NULL) 75 sync_service_->DeactivateDataType(this, change_processor_.get()); 76 77 if (model_associator_ != NULL) 78 model_associator_->DisassociateModels(); 79 80 change_processor_.reset(); 81 model_associator_.reset(); 82 start_callback_.reset(); 83 84 state_ = NOT_RUNNING; 85} 86 87bool AppDataTypeController::enabled() { 88 return true; 89} 90 91syncable::ModelType AppDataTypeController::type() { 92 return syncable::APPS; 93} 94 95browser_sync::ModelSafeGroup AppDataTypeController::model_safe_group() { 96 return browser_sync::GROUP_UI; 97} 98 99const char* AppDataTypeController::name() const { 100 // For logging only. 101 return "app"; 102} 103 104DataTypeController::State AppDataTypeController::state() { 105 return state_; 106} 107 108void AppDataTypeController::OnUnrecoverableError( 109 const tracked_objects::Location& from_here, 110 const std::string& message) { 111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 112 UMA_HISTOGRAM_COUNTS("Sync.AppRunFailures", 1); 113 sync_service_->OnUnrecoverableError(from_here, message); 114} 115 116void AppDataTypeController::FinishStart(StartResult result) { 117 start_callback_->Run(result); 118 start_callback_.reset(); 119} 120 121void AppDataTypeController::StartFailed(StartResult result) { 122 model_associator_.reset(); 123 change_processor_.reset(); 124 start_callback_->Run(result); 125 start_callback_.reset(); 126 UMA_HISTOGRAM_ENUMERATION("Sync.AppStartFailures", 127 result, 128 MAX_START_RESULT); 129} 130 131} // namespace browser_sync 132