Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/history/in_memory_url_index.cc

Issue 573553004: Eliminate NOTIFICATION_HISTORY_LOADED notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/history/in_memory_url_index.h" 5 #include "chrome/browser/history/in_memory_url_index.h"
6 6
7 #include "base/debug/trace_event.h" 7 #include "base/debug/trace_event.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 : profile_(profile), 96 : profile_(profile),
97 history_service_(history_service), 97 history_service_(history_service),
98 history_client_(history_client), 98 history_client_(history_client),
99 history_dir_(history_dir), 99 history_dir_(history_dir),
100 languages_(languages), 100 languages_(languages),
101 private_data_(new URLIndexPrivateData), 101 private_data_(new URLIndexPrivateData),
102 restore_cache_observer_(NULL), 102 restore_cache_observer_(NULL),
103 save_cache_observer_(NULL), 103 save_cache_observer_(NULL),
104 shutdown_(false), 104 shutdown_(false),
105 restored_(false), 105 restored_(false),
106 needs_to_be_cached_(false) { 106 needs_to_be_cached_(false),
107 history_service_observer_(this) {
107 InitializeSchemeWhitelist(&scheme_whitelist_); 108 InitializeSchemeWhitelist(&scheme_whitelist_);
108 if (profile) { 109 if (profile) {
109 // TODO(mrossetti): Register for language change notifications. 110 // TODO(mrossetti): Register for language change notifications.
110 content::Source<Profile> source(profile); 111 content::Source<Profile> source(profile);
111 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED, 112 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
112 source); 113 source);
113 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); 114 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source);
114 } 115 }
115 if (history_service_) 116 if (history_service_)
116 history_service_->AddObserver(this); 117 history_service_observer_.Add(history_service_);
117 } 118 }
118 119
119 // Called only by unit tests. 120 // Called only by unit tests.
120 InMemoryURLIndex::InMemoryURLIndex() 121 InMemoryURLIndex::InMemoryURLIndex()
121 : profile_(NULL), 122 : profile_(NULL),
122 history_service_(nullptr), 123 history_service_(nullptr),
123 history_client_(NULL), 124 history_client_(NULL),
124 private_data_(new URLIndexPrivateData), 125 private_data_(new URLIndexPrivateData),
125 restore_cache_observer_(NULL), 126 restore_cache_observer_(NULL),
126 save_cache_observer_(NULL), 127 save_cache_observer_(NULL),
127 shutdown_(false), 128 shutdown_(false),
128 restored_(false), 129 restored_(false),
129 needs_to_be_cached_(false) { 130 needs_to_be_cached_(false),
131 history_service_observer_(this) {
130 InitializeSchemeWhitelist(&scheme_whitelist_); 132 InitializeSchemeWhitelist(&scheme_whitelist_);
131 } 133 }
132 134
133 InMemoryURLIndex::~InMemoryURLIndex() { 135 InMemoryURLIndex::~InMemoryURLIndex() {
134 // If there was a history directory (which there won't be for some unit tests) 136 // If there was a history directory (which there won't be for some unit tests)
135 // then insure that the cache has already been saved. 137 // then insure that the cache has already been saved.
136 DCHECK(history_dir_.empty() || !needs_to_be_cached_); 138 DCHECK(history_dir_.empty() || !needs_to_be_cached_);
137 } 139 }
138 140
139 void InMemoryURLIndex::Init() { 141 void InMemoryURLIndex::Init() {
140 PostRestoreFromCacheFileTask(); 142 PostRestoreFromCacheFileTask();
141 } 143 }
142 144
143 void InMemoryURLIndex::ShutDown() { 145 void InMemoryURLIndex::ShutDown() {
144 if (history_service_) 146 history_service_observer_.RemoveAll();
145 history_service_->RemoveObserver(this);
146 registrar_.RemoveAll(); 147 registrar_.RemoveAll();
147 cache_reader_tracker_.TryCancelAll(); 148 cache_reader_tracker_.TryCancelAll();
148 shutdown_ = true; 149 shutdown_ = true;
149 base::FilePath path; 150 base::FilePath path;
150 if (!GetCacheFilePath(&path)) 151 if (!GetCacheFilePath(&path))
151 return; 152 return;
152 private_data_tracker_.TryCancelAll(); 153 private_data_tracker_.TryCancelAll();
153 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); 154 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path);
154 needs_to_be_cached_ = false; 155 needs_to_be_cached_ = false;
155 } 156 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 const content::NotificationDetails& details) { 191 const content::NotificationDetails& details) {
191 switch (notification_type) { 192 switch (notification_type) {
192 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED: 193 case chrome::NOTIFICATION_HISTORY_URLS_MODIFIED:
193 OnURLsModified( 194 OnURLsModified(
194 content::Details<history::URLsModifiedDetails>(details).ptr()); 195 content::Details<history::URLsModifiedDetails>(details).ptr());
195 break; 196 break;
196 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: 197 case chrome::NOTIFICATION_HISTORY_URLS_DELETED:
197 OnURLsDeleted( 198 OnURLsDeleted(
198 content::Details<history::URLsDeletedDetails>(details).ptr()); 199 content::Details<history::URLsDeletedDetails>(details).ptr());
199 break; 200 break;
200 case chrome::NOTIFICATION_HISTORY_LOADED:
201 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED,
202 content::Source<Profile>(profile_));
203 ScheduleRebuildFromHistory();
204 break;
205 default: 201 default:
206 // For simplicity, the unit tests send us all notifications, even when 202 // For simplicity, the unit tests send us all notifications, even when
207 // we haven't registered for them, so don't assert here. 203 // we haven't registered for them, so don't assert here.
208 break; 204 break;
209 } 205 }
210 } 206 }
211 207
212 void InMemoryURLIndex::OnURLVisited(HistoryService* history_service, 208 void InMemoryURLIndex::OnURLVisited(HistoryService* history_service,
213 ui::PageTransition transition, 209 ui::PageTransition transition,
214 const URLRow& row, 210 const URLRow& row,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 // When unable to restore from the cache file delete the cache file, if 293 // When unable to restore from the cache file delete the cache file, if
298 // it exists, and then rebuild from the history database if it's available, 294 // it exists, and then rebuild from the history database if it's available,
299 // otherwise wait until the history database loaded and then rebuild. 295 // otherwise wait until the history database loaded and then rebuild.
300 base::FilePath path; 296 base::FilePath path;
301 if (!GetCacheFilePath(&path) || shutdown_) 297 if (!GetCacheFilePath(&path) || shutdown_)
302 return; 298 return;
303 content::BrowserThread::PostBlockingPoolTask( 299 content::BrowserThread::PostBlockingPoolTask(
304 FROM_HERE, base::Bind(DeleteCacheFile, path)); 300 FROM_HERE, base::Bind(DeleteCacheFile, path));
305 HistoryService* service = 301 HistoryService* service =
306 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); 302 HistoryServiceFactory::GetForProfileWithoutCreating(profile_);
307 if (service && service->backend_loaded()) { 303 if (service) {
308 ScheduleRebuildFromHistory(); 304 if (!service->backend_loaded()) {
309 } else { 305 history_service_observer_.Add(service);
310 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, 306 } else {
311 content::Source<Profile>(profile_)); 307 ScheduleRebuildFromHistory();
308 }
312 } 309 }
313 } 310 }
314 } 311 }
315 312
316 // Restoring from the History DB ----------------------------------------------- 313 // Restoring from the History DB -----------------------------------------------
317 314
318 void InMemoryURLIndex::ScheduleRebuildFromHistory() { 315 void InMemoryURLIndex::ScheduleRebuildFromHistory() {
319 HistoryService* service = 316 HistoryService* service =
320 HistoryServiceFactory::GetForProfile(profile_, 317 HistoryServiceFactory::GetForProfile(profile_,
321 Profile::EXPLICIT_ACCESS); 318 Profile::EXPLICIT_ACCESS);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 FROM_HERE, 372 FROM_HERE,
376 base::Bind(DeleteCacheFile, path)); 373 base::Bind(DeleteCacheFile, path));
377 } 374 }
378 } 375 }
379 376
380 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { 377 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) {
381 if (save_cache_observer_) 378 if (save_cache_observer_)
382 save_cache_observer_->OnCacheSaveFinished(succeeded); 379 save_cache_observer_->OnCacheSaveFinished(succeeded);
383 } 380 }
384 381
382 void InMemoryURLIndex::OnHistoryServiceLoaded(HistoryService* history_service) {
383 ScheduleRebuildFromHistory();
384 history_service_observer_.Remove(history_service);
385 }
386
385 } // namespace history 387 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698