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/bookmark_data_type_controller.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/history/history_service.h"
11 #include "chrome/browser/history/history_service_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h"
14 #include "chrome/browser/sync/profile_sync_components_factory.h"
15 #include "chrome/browser/sync/profile_sync_service.h"
16 #include "components/bookmarks/core/browser/bookmark_model.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
21 using content::BrowserThread;
23 namespace browser_sync {
25 BookmarkDataTypeController::BookmarkDataTypeController(
26 ProfileSyncComponentsFactory* profile_sync_factory,
28 ProfileSyncService* sync_service)
29 : FrontendDataTypeController(
30 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
31 base::Bind(&ChromeReportUnrecoverableError),
35 bookmark_model_(NULL),
36 installed_bookmark_observer_(false) {
39 syncer::ModelType BookmarkDataTypeController::type() const {
40 return syncer::BOOKMARKS;
43 void BookmarkDataTypeController::Observe(
45 const content::NotificationSource& source,
46 const content::NotificationDetails& details) {
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
48 DCHECK_EQ(state_, MODEL_STARTING);
49 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_LOADED, type);
51 if (!DependentsLoaded())
54 bookmark_model_->RemoveObserver(this);
55 installed_bookmark_observer_ = false;
57 registrar_.RemoveAll();
61 BookmarkDataTypeController::~BookmarkDataTypeController() {
62 if (installed_bookmark_observer_ && bookmark_model_) {
64 bookmark_model_->RemoveObserver(this);
68 bool BookmarkDataTypeController::StartModels() {
69 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_);
70 if (!DependentsLoaded()) {
71 bookmark_model_->AddObserver(this);
72 installed_bookmark_observer_ = true;
74 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
75 content::Source<Profile>(sync_service_->profile()));
81 // Cleanup for our extra registrar usage.
82 void BookmarkDataTypeController::CleanUpState() {
83 registrar_.RemoveAll();
84 if (bookmark_model_ && installed_bookmark_observer_) {
85 bookmark_model_->RemoveObserver(this);
86 installed_bookmark_observer_ = false;
90 void BookmarkDataTypeController::CreateSyncComponents() {
91 ProfileSyncComponentsFactory::SyncComponents sync_components =
92 profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_,
94 set_model_associator(sync_components.model_associator);
95 set_change_processor(sync_components.change_processor);
98 void BookmarkDataTypeController::BookmarkModelChanged() {
101 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model,
102 bool ids_reassigned) {
103 DCHECK(model->loaded());
104 model->RemoveObserver(this);
105 installed_bookmark_observer_ = false;
107 if (!DependentsLoaded())
110 registrar_.RemoveAll();
114 void BookmarkDataTypeController::BookmarkModelBeingDeleted(
115 BookmarkModel* model) {
116 installed_bookmark_observer_ = false;
119 // Check that both the bookmark model and the history service (for favicons)
121 bool BookmarkDataTypeController::DependentsLoaded() {
122 if (!bookmark_model_ || !bookmark_model_->loaded())
125 HistoryService* history = HistoryServiceFactory::GetForProfile(
126 profile_, Profile::EXPLICIT_ACCESS);
127 if (!history || !history->BackendLoaded())
130 // All necessary services are loaded.
134 } // namespace browser_sync