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/search_engine_data_type_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/search_engines/template_url_service_factory.h"
10 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "sync/api/syncable_service.h"
15 using content::BrowserThread;
17 namespace browser_sync {
19 SearchEngineDataTypeController::SearchEngineDataTypeController(
20 SyncApiComponentFactory* sync_factory,
22 const DisableTypeCallback& disable_callback)
23 : UIDataTypeController(
24 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
25 base::Bind(&ChromeReportUnrecoverableError),
27 syncer::SEARCH_ENGINES,
32 TemplateURLService::Subscription*
33 SearchEngineDataTypeController::GetSubscriptionForTesting() {
34 return template_url_subscription_.get();
37 SearchEngineDataTypeController::~SearchEngineDataTypeController() {}
39 // We want to start the TemplateURLService before we begin associating.
40 bool SearchEngineDataTypeController::StartModels() {
41 // If the TemplateURLService is loaded, continue with association. We force
42 // a load here to prevent the rest of Sync from waiting on
43 // TemplateURLService's lazy load.
44 TemplateURLService* turl_service =
45 TemplateURLServiceFactory::GetForProfile(profile_);
48 if (turl_service->loaded()) {
49 return true; // Continue to Associate().
52 // Register a callback and continue when the TemplateURLService is loaded.
53 template_url_subscription_ = turl_service->RegisterOnLoadedCallback(
54 base::Bind(&SearchEngineDataTypeController::OnTemplateURLServiceLoaded,
57 return false; // Don't continue Start.
60 void SearchEngineDataTypeController::StopModels() {
61 template_url_subscription_.reset();
64 void SearchEngineDataTypeController::OnTemplateURLServiceLoaded() {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66 DCHECK_EQ(state_, MODEL_STARTING);
67 template_url_subscription_.reset();
71 } // namespace browser_sync