OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 5 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 8 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/history/history_service.h" | 10 #include "chrome/browser/history/history_service.h" |
11 #include "chrome/browser/history/history_service_factory.h" | 11 #include "chrome/browser/history/history_service_factory.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h" | 13 #include "chrome/browser/sync/glue/chrome_report_unrecoverable_error.h" |
14 #include "chrome/browser/sync/profile_sync_components_factory.h" | 14 #include "chrome/browser/sync/profile_sync_components_factory.h" |
15 #include "chrome/browser/sync/profile_sync_service.h" | 15 #include "chrome/browser/sync/profile_sync_service.h" |
16 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
19 #include "content/public/browser/notification_service.h" | |
19 #include "content/public/browser/notification_source.h" | 20 #include "content/public/browser/notification_source.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 | 23 |
23 namespace browser_sync { | 24 namespace browser_sync { |
24 | 25 |
25 BookmarkDataTypeController::BookmarkDataTypeController( | 26 BookmarkDataTypeController::BookmarkDataTypeController( |
26 ProfileSyncComponentsFactory* profile_sync_factory, | 27 ProfileSyncComponentsFactory* profile_sync_factory, |
27 Profile* profile, | 28 Profile* profile, |
28 ProfileSyncService* sync_service) | 29 ProfileSyncService* sync_service) |
29 : FrontendDataTypeController( | 30 : FrontendDataTypeController( |
30 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 31 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
31 base::Bind(&ChromeReportUnrecoverableError), | 32 base::Bind(&ChromeReportUnrecoverableError), |
32 profile_sync_factory, | 33 profile_sync_factory, |
33 profile, | 34 profile, |
34 sync_service), | 35 sync_service), |
35 bookmark_model_(NULL), | 36 bookmark_model_(NULL), |
36 installed_bookmark_observer_(false) { | 37 history_service_observer_(this), |
38 bookmark_model_observer_(this) { | |
39 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
sdefresne
2014/11/07 16:39:42
Another option would be to add a HistoryServiceBei
nshaik
2014/11/07 23:45:32
Done. For now I am not listening to this notificat
| |
40 content::NotificationService::AllSources()); | |
37 } | 41 } |
38 | 42 |
39 syncer::ModelType BookmarkDataTypeController::type() const { | 43 syncer::ModelType BookmarkDataTypeController::type() const { |
40 return syncer::BOOKMARKS; | 44 return syncer::BOOKMARKS; |
41 } | 45 } |
42 | 46 |
47 BookmarkDataTypeController::~BookmarkDataTypeController() { | |
48 } | |
49 | |
43 void BookmarkDataTypeController::Observe( | 50 void BookmarkDataTypeController::Observe( |
44 int type, | 51 int type, |
45 const content::NotificationSource& source, | 52 const content::NotificationSource& source, |
46 const content::NotificationDetails& details) { | 53 const content::NotificationDetails& details) { |
47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
48 DCHECK_EQ(state_, MODEL_STARTING); | 55 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); |
49 DCHECK_EQ(chrome::NOTIFICATION_HISTORY_LOADED, type); | |
50 | 56 |
51 if (!DependentsLoaded()) | 57 CleanUpState(); |
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 } | 58 } |
67 | 59 |
68 bool BookmarkDataTypeController::StartModels() { | 60 bool BookmarkDataTypeController::StartModels() { |
69 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); | 61 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); |
70 if (!DependentsLoaded()) { | 62 if (!DependentsLoaded()) { |
71 bookmark_model_->AddObserver(this); | 63 bookmark_model_observer_.Add(bookmark_model_); |
72 installed_bookmark_observer_ = true; | 64 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
73 | 65 profile_, Profile::EXPLICIT_ACCESS); |
74 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | 66 history_service_observer_.Add(history_service); |
75 content::Source<Profile>(sync_service_->profile())); | |
76 return false; | 67 return false; |
77 } | 68 } |
78 return true; | 69 return true; |
79 } | 70 } |
80 | 71 |
81 // Cleanup for our extra registrar usage. | |
82 void BookmarkDataTypeController::CleanUpState() { | 72 void BookmarkDataTypeController::CleanUpState() { |
83 registrar_.RemoveAll(); | 73 history_service_observer_.RemoveAll(); |
84 if (bookmark_model_ && installed_bookmark_observer_) { | 74 bookmark_model_observer_.RemoveAll(); |
85 bookmark_model_->RemoveObserver(this); | |
86 installed_bookmark_observer_ = false; | |
87 } | |
88 } | 75 } |
89 | 76 |
90 void BookmarkDataTypeController::CreateSyncComponents() { | 77 void BookmarkDataTypeController::CreateSyncComponents() { |
91 ProfileSyncComponentsFactory::SyncComponents sync_components = | 78 ProfileSyncComponentsFactory::SyncComponents sync_components = |
92 profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_, | 79 profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_, |
93 this); | 80 this); |
94 set_model_associator(sync_components.model_associator); | 81 set_model_associator(sync_components.model_associator); |
95 set_change_processor(sync_components.change_processor); | 82 set_change_processor(sync_components.change_processor); |
96 } | 83 } |
97 | 84 |
98 void BookmarkDataTypeController::BookmarkModelChanged() { | 85 void BookmarkDataTypeController::BookmarkModelChanged() { |
99 } | 86 } |
100 | 87 |
101 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model, | 88 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model, |
102 bool ids_reassigned) { | 89 bool ids_reassigned) { |
103 DCHECK(model->loaded()); | 90 DCHECK(model->loaded()); |
104 model->RemoveObserver(this); | 91 bookmark_model_observer_.RemoveAll(); |
105 installed_bookmark_observer_ = false; | |
106 | 92 |
107 if (!DependentsLoaded()) | 93 if (!DependentsLoaded()) |
108 return; | 94 return; |
109 | 95 |
110 registrar_.RemoveAll(); | 96 history_service_observer_.RemoveAll(); |
111 OnModelLoaded(); | 97 OnModelLoaded(); |
112 } | 98 } |
113 | 99 |
114 void BookmarkDataTypeController::BookmarkModelBeingDeleted( | 100 void BookmarkDataTypeController::BookmarkModelBeingDeleted( |
115 BookmarkModel* model) { | 101 BookmarkModel* model) { |
116 installed_bookmark_observer_ = false; | 102 CleanUpState(); |
117 } | 103 } |
118 | 104 |
119 // Check that both the bookmark model and the history service (for favicons) | 105 // Check that both the bookmark model and the history service (for favicons) |
120 // are loaded. | 106 // are loaded. |
121 bool BookmarkDataTypeController::DependentsLoaded() { | 107 bool BookmarkDataTypeController::DependentsLoaded() { |
122 if (!bookmark_model_ || !bookmark_model_->loaded()) | 108 if (!bookmark_model_ || !bookmark_model_->loaded()) |
123 return false; | 109 return false; |
124 | 110 |
125 HistoryService* history = HistoryServiceFactory::GetForProfile( | 111 HistoryService* history = HistoryServiceFactory::GetForProfile( |
126 profile_, Profile::EXPLICIT_ACCESS); | 112 profile_, Profile::EXPLICIT_ACCESS); |
127 if (!history || !history->BackendLoaded()) | 113 if (!history || !history->BackendLoaded()) |
128 return false; | 114 return false; |
129 | 115 |
130 // All necessary services are loaded. | 116 // All necessary services are loaded. |
131 return true; | 117 return true; |
132 } | 118 } |
133 | 119 |
120 void BookmarkDataTypeController::OnHistoryServiceLoaded( | |
121 HistoryService* service) { | |
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
123 DCHECK_EQ(state_, MODEL_STARTING); | |
124 history_service_observer_.RemoveAll(); | |
125 | |
126 if (!DependentsLoaded()) | |
127 return; | |
128 | |
129 bookmark_model_observer_.RemoveAll(); | |
130 OnModelLoaded(); | |
131 } | |
132 | |
134 } // namespace browser_sync | 133 } // namespace browser_sync |
OLD | NEW |