Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / glue / bookmark_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/bookmark_data_type_controller.h"
6
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"
20
21 using content::BrowserThread;
22
23 namespace browser_sync {
24
25 BookmarkDataTypeController::BookmarkDataTypeController(
26     ProfileSyncComponentsFactory* profile_sync_factory,
27     Profile* profile,
28     ProfileSyncService* sync_service)
29     : FrontendDataTypeController(
30           BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI),
31           base::Bind(&ChromeReportUnrecoverableError),
32           profile_sync_factory,
33           profile,
34           sync_service),
35       bookmark_model_(NULL),
36       installed_bookmark_observer_(false) {
37 }
38
39 syncer::ModelType BookmarkDataTypeController::type() const {
40   return syncer::BOOKMARKS;
41 }
42
43 void BookmarkDataTypeController::Observe(
44     int type,
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);
50
51   if (!DependentsLoaded())
52     return;
53
54   bookmark_model_->RemoveObserver(this);
55   installed_bookmark_observer_ = false;
56
57   registrar_.RemoveAll();
58   OnModelLoaded();
59 }
60
61 BookmarkDataTypeController::~BookmarkDataTypeController() {
62   if (installed_bookmark_observer_ && bookmark_model_) {
63     DCHECK(profile_);
64     bookmark_model_->RemoveObserver(this);
65   }
66 }
67
68 bool BookmarkDataTypeController::StartModels() {
69   bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_);
70   if (!DependentsLoaded()) {
71     bookmark_model_->AddObserver(this);
72     installed_bookmark_observer_ = true;
73
74     registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
75                    content::Source<Profile>(sync_service_->profile()));
76     return false;
77   }
78   return true;
79 }
80
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;
87   }
88 }
89
90 void BookmarkDataTypeController::CreateSyncComponents() {
91   ProfileSyncComponentsFactory::SyncComponents sync_components =
92       profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_,
93                                                           this);
94   set_model_associator(sync_components.model_associator);
95   set_change_processor(sync_components.change_processor);
96 }
97
98 void BookmarkDataTypeController::BookmarkModelChanged() {
99 }
100
101 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model,
102                                                      bool ids_reassigned) {
103   DCHECK(model->loaded());
104   model->RemoveObserver(this);
105   installed_bookmark_observer_ = false;
106
107   if (!DependentsLoaded())
108     return;
109
110   registrar_.RemoveAll();
111   OnModelLoaded();
112 }
113
114 void BookmarkDataTypeController::BookmarkModelBeingDeleted(
115     BookmarkModel* model) {
116   installed_bookmark_observer_ = false;
117 }
118
119 // Check that both the bookmark model and the history service (for favicons)
120 // are loaded.
121 bool BookmarkDataTypeController::DependentsLoaded() {
122   if (!bookmark_model_ || !bookmark_model_->loaded())
123     return false;
124
125   HistoryService* history = HistoryServiceFactory::GetForProfile(
126       profile_, Profile::EXPLICIT_ACCESS);
127   if (!history || !history->BackendLoaded())
128     return false;
129
130   // All necessary services are loaded.
131   return true;
132 }
133
134 }  // namespace browser_sync