- add sources.
[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/profile_sync_components_factory.h"
11 #include "chrome/browser/sync/profile_sync_service.h"
12 #include "sync/api/syncable_service.h"
13
14 using content::BrowserThread;
15
16 namespace browser_sync {
17
18 SearchEngineDataTypeController::SearchEngineDataTypeController(
19     ProfileSyncComponentsFactory* profile_sync_factory,
20     Profile* profile,
21     ProfileSyncService* sync_service)
22     : UIDataTypeController(syncer::SEARCH_ENGINES,
23                            profile_sync_factory,
24                            profile,
25                            sync_service) {
26 }
27
28 SearchEngineDataTypeController::~SearchEngineDataTypeController() {}
29
30 // We want to start the TemplateURLService before we begin associating.
31 bool SearchEngineDataTypeController::StartModels() {
32   // If the TemplateURLService is loaded, continue with association. We force
33   // a load here to prevent the rest of Sync from waiting on
34   // TemplateURLService's lazy load.
35   TemplateURLService* turl_service =
36       TemplateURLServiceFactory::GetForProfile(profile_);
37   DCHECK(turl_service);
38   turl_service->Load();
39   if (turl_service->loaded()) {
40     return true;  // Continue to Associate().
41   }
42
43   // Register a callback and continue when the TemplateURLService is loaded.
44   template_url_subscription_ = turl_service->RegisterOnLoadedCallback(
45       base::Bind(&SearchEngineDataTypeController::OnTemplateURLServiceLoaded,
46                  this));
47
48   return false;  // Don't continue Start.
49 }
50
51 void SearchEngineDataTypeController::OnTemplateURLServiceLoaded() {
52   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
53   DCHECK_EQ(state_, MODEL_STARTING);
54   template_url_subscription_.reset();
55   OnModelLoaded();
56 }
57
58 }  // namespace browser_sync