b8f967d7df76a4fb55d474586e2542dd9c2d1252
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / search_engine_data_type_controller.cc
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/search_engine_data_type_controller.h"
6
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"
14
15 using content::BrowserThread;
16
17 namespace browser_sync {
18
19 SearchEngineDataTypeController::SearchEngineDataTypeController(
20     SyncApiComponentFactory* sync_factory,
21     Profile* profile,
22     const DisableTypeCallback& disable_callback)
23     : UIDataTypeController(
24           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
25           base::Bind(&ChromeReportUnrecoverableError),
26           disable_callback,
27           syncer::SEARCH_ENGINES,
28           sync_factory),
29       profile_(profile) {
30 }
31
32 SearchEngineDataTypeController::~SearchEngineDataTypeController() {}
33
34 // We want to start the TemplateURLService before we begin associating.
35 bool SearchEngineDataTypeController::StartModels() {
36   // If the TemplateURLService is loaded, continue with association. We force
37   // a load here to prevent the rest of Sync from waiting on
38   // TemplateURLService's lazy load.
39   TemplateURLService* turl_service =
40       TemplateURLServiceFactory::GetForProfile(profile_);
41   DCHECK(turl_service);
42   turl_service->Load();
43   if (turl_service->loaded()) {
44     return true;  // Continue to Associate().
45   }
46
47   // Register a callback and continue when the TemplateURLService is loaded.
48   template_url_subscription_ = turl_service->RegisterOnLoadedCallback(
49       base::Bind(&SearchEngineDataTypeController::OnTemplateURLServiceLoaded,
50                  this));
51
52   return false;  // Don't continue Start.
53 }
54
55 void SearchEngineDataTypeController::OnTemplateURLServiceLoaded() {
56   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57   DCHECK_EQ(state_, MODEL_STARTING);
58   template_url_subscription_.reset();
59   OnModelLoaded();
60 }
61
62 }  // namespace browser_sync