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" | |
19 #include "content/public/browser/notification_source.h" | |
20 | 18 |
21 using content::BrowserThread; | 19 using content::BrowserThread; |
22 | 20 |
23 namespace browser_sync { | 21 namespace browser_sync { |
24 | 22 |
25 BookmarkDataTypeController::BookmarkDataTypeController( | 23 BookmarkDataTypeController::BookmarkDataTypeController( |
26 ProfileSyncComponentsFactory* profile_sync_factory, | 24 ProfileSyncComponentsFactory* profile_sync_factory, |
27 Profile* profile, | 25 Profile* profile, |
28 ProfileSyncService* sync_service) | 26 ProfileSyncService* sync_service) |
29 : FrontendDataTypeController( | 27 : FrontendDataTypeController( |
30 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 28 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
31 base::Bind(&ChromeReportUnrecoverableError), | 29 base::Bind(&ChromeReportUnrecoverableError), |
32 profile_sync_factory, | 30 profile_sync_factory, |
33 profile, | 31 profile, |
34 sync_service), | 32 sync_service), |
35 bookmark_model_(NULL), | 33 bookmark_model_(NULL), |
36 installed_bookmark_observer_(false) { | 34 history_service_observer_(this), |
35 bookmark_model_observer_(this) { | |
37 } | 36 } |
38 | 37 |
39 syncer::ModelType BookmarkDataTypeController::type() const { | 38 syncer::ModelType BookmarkDataTypeController::type() const { |
40 return syncer::BOOKMARKS; | 39 return syncer::BOOKMARKS; |
41 } | 40 } |
42 | 41 |
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() { | 42 BookmarkDataTypeController::~BookmarkDataTypeController() { |
62 if (installed_bookmark_observer_ && bookmark_model_) { | |
63 DCHECK(profile_); | |
64 bookmark_model_->RemoveObserver(this); | |
65 } | |
66 } | 43 } |
67 | 44 |
68 bool BookmarkDataTypeController::StartModels() { | 45 bool BookmarkDataTypeController::StartModels() { |
69 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); | 46 bookmark_model_ = BookmarkModelFactory::GetForProfile(profile_); |
70 if (!DependentsLoaded()) { | 47 if (!DependentsLoaded()) { |
71 bookmark_model_->AddObserver(this); | 48 bookmark_model_observer_.Add(bookmark_model_); |
72 installed_bookmark_observer_ = true; | 49 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
73 | 50 profile_, Profile::EXPLICIT_ACCESS); |
74 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | 51 history_service_observer_.Add(history_service); |
75 content::Source<Profile>(sync_service_->profile())); | |
76 return false; | 52 return false; |
77 } | 53 } |
78 return true; | 54 return true; |
79 } | 55 } |
80 | 56 |
81 // Cleanup for our extra registrar usage. | |
82 void BookmarkDataTypeController::CleanUpState() { | 57 void BookmarkDataTypeController::CleanUpState() { |
83 registrar_.RemoveAll(); | 58 history_service_observer_.RemoveAll(); |
84 if (bookmark_model_ && installed_bookmark_observer_) { | 59 bookmark_model_observer_.RemoveAll(); |
85 bookmark_model_->RemoveObserver(this); | |
86 installed_bookmark_observer_ = false; | |
87 } | |
88 } | 60 } |
89 | 61 |
90 void BookmarkDataTypeController::CreateSyncComponents() { | 62 void BookmarkDataTypeController::CreateSyncComponents() { |
91 ProfileSyncComponentsFactory::SyncComponents sync_components = | 63 ProfileSyncComponentsFactory::SyncComponents sync_components = |
92 profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_, | 64 profile_sync_factory_->CreateBookmarkSyncComponents(sync_service_, |
93 this); | 65 this); |
94 set_model_associator(sync_components.model_associator); | 66 set_model_associator(sync_components.model_associator); |
95 set_change_processor(sync_components.change_processor); | 67 set_change_processor(sync_components.change_processor); |
96 } | 68 } |
97 | 69 |
98 void BookmarkDataTypeController::BookmarkModelChanged() { | 70 void BookmarkDataTypeController::BookmarkModelChanged() { |
99 } | 71 } |
100 | 72 |
101 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model, | 73 void BookmarkDataTypeController::BookmarkModelLoaded(BookmarkModel* model, |
102 bool ids_reassigned) { | 74 bool ids_reassigned) { |
103 DCHECK(model->loaded()); | 75 DCHECK(model->loaded()); |
104 model->RemoveObserver(this); | 76 bookmark_model_observer_.RemoveAll(); |
105 installed_bookmark_observer_ = false; | |
106 | 77 |
107 if (!DependentsLoaded()) | 78 if (!DependentsLoaded()) |
108 return; | 79 return; |
109 | 80 |
110 registrar_.RemoveAll(); | 81 history_service_observer_.RemoveAll(); |
111 OnModelLoaded(); | 82 OnModelLoaded(); |
112 } | 83 } |
113 | 84 |
114 void BookmarkDataTypeController::BookmarkModelBeingDeleted( | 85 void BookmarkDataTypeController::BookmarkModelBeingDeleted( |
115 BookmarkModel* model) { | 86 BookmarkModel* model) { |
116 installed_bookmark_observer_ = false; | 87 CleanUpState(); |
117 } | 88 } |
118 | 89 |
119 // Check that both the bookmark model and the history service (for favicons) | 90 // Check that both the bookmark model and the history service (for favicons) |
120 // are loaded. | 91 // are loaded. |
121 bool BookmarkDataTypeController::DependentsLoaded() { | 92 bool BookmarkDataTypeController::DependentsLoaded() { |
122 if (!bookmark_model_ || !bookmark_model_->loaded()) | 93 if (!bookmark_model_ || !bookmark_model_->loaded()) |
123 return false; | 94 return false; |
124 | 95 |
125 HistoryService* history = HistoryServiceFactory::GetForProfile( | 96 HistoryService* history = HistoryServiceFactory::GetForProfile( |
126 profile_, Profile::EXPLICIT_ACCESS); | 97 profile_, Profile::EXPLICIT_ACCESS); |
127 if (!history || !history->BackendLoaded()) | 98 if (!history || !history->BackendLoaded()) |
128 return false; | 99 return false; |
129 | 100 |
130 // All necessary services are loaded. | 101 // All necessary services are loaded. |
131 return true; | 102 return true; |
132 } | 103 } |
133 | 104 |
105 void BookmarkDataTypeController::OnHistoryServiceLoaded( | |
brettw
2014/11/14 21:13:07
order
nshaik
2014/11/15 07:04:15
Done.
| |
106 HistoryService* service) { | |
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
108 DCHECK_EQ(state_, MODEL_STARTING); | |
109 history_service_observer_.RemoveAll(); | |
110 | |
111 if (!DependentsLoaded()) | |
112 return; | |
113 | |
114 bookmark_model_observer_.RemoveAll(); | |
115 OnModelLoaded(); | |
116 } | |
117 | |
118 void BookmarkDataTypeController::HistoryServiceBeingDeleted( | |
119 HistoryService* history_service) { | |
120 CleanUpState(); | |
121 } | |
122 | |
134 } // namespace browser_sync | 123 } // namespace browser_sync |
OLD | NEW |