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