Upstream version 9.37.195.0
[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 TemplateURLService::Subscription*
33 SearchEngineDataTypeController::GetSubscriptionForTesting() {
34   return template_url_subscription_.get();
35 }
36
37 SearchEngineDataTypeController::~SearchEngineDataTypeController() {}
38
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_);
46   DCHECK(turl_service);
47   turl_service->Load();
48   if (turl_service->loaded()) {
49     return true;  // Continue to Associate().
50   }
51
52   // Register a callback and continue when the TemplateURLService is loaded.
53   template_url_subscription_ = turl_service->RegisterOnLoadedCallback(
54       base::Bind(&SearchEngineDataTypeController::OnTemplateURLServiceLoaded,
55                  this));
56
57   return false;  // Don't continue Start.
58 }
59
60 void SearchEngineDataTypeController::StopModels() {
61   template_url_subscription_.reset();
62 }
63
64 void SearchEngineDataTypeController::OnTemplateURLServiceLoaded() {
65   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66   DCHECK_EQ(state_, MODEL_STARTING);
67   template_url_subscription_.reset();
68   OnModelLoaded();
69 }
70
71 }  // namespace browser_sync