| 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/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 Loading... |
| 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_DELETED, source); | 112 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source); |
| 112 } | 113 } |
| 113 if (history_service_) | 114 if (history_service_) |
| 114 history_service_->AddObserver(this); | 115 history_service_observer_.Add(history_service_); |
| 115 } | 116 } |
| 116 | 117 |
| 117 // Called only by unit tests. | 118 // Called only by unit tests. |
| 118 InMemoryURLIndex::InMemoryURLIndex() | 119 InMemoryURLIndex::InMemoryURLIndex() |
| 119 : profile_(NULL), | 120 : profile_(NULL), |
| 120 history_service_(nullptr), | 121 history_service_(nullptr), |
| 121 history_client_(NULL), | 122 history_client_(NULL), |
| 122 private_data_(new URLIndexPrivateData), | 123 private_data_(new URLIndexPrivateData), |
| 123 restore_cache_observer_(NULL), | 124 restore_cache_observer_(NULL), |
| 124 save_cache_observer_(NULL), | 125 save_cache_observer_(NULL), |
| 125 shutdown_(false), | 126 shutdown_(false), |
| 126 restored_(false), | 127 restored_(false), |
| 127 needs_to_be_cached_(false) { | 128 needs_to_be_cached_(false), |
| 129 history_service_observer_(this) { |
| 128 InitializeSchemeWhitelist(&scheme_whitelist_); | 130 InitializeSchemeWhitelist(&scheme_whitelist_); |
| 129 } | 131 } |
| 130 | 132 |
| 131 InMemoryURLIndex::~InMemoryURLIndex() { | 133 InMemoryURLIndex::~InMemoryURLIndex() { |
| 132 // If there was a history directory (which there won't be for some unit tests) | 134 // If there was a history directory (which there won't be for some unit tests) |
| 133 // then insure that the cache has already been saved. | 135 // then insure that the cache has already been saved. |
| 134 DCHECK(history_dir_.empty() || !needs_to_be_cached_); | 136 DCHECK(history_dir_.empty() || !needs_to_be_cached_); |
| 135 } | 137 } |
| 136 | 138 |
| 137 void InMemoryURLIndex::Init() { | 139 void InMemoryURLIndex::Init() { |
| 138 PostRestoreFromCacheFileTask(); | 140 PostRestoreFromCacheFileTask(); |
| 139 } | 141 } |
| 140 | 142 |
| 141 void InMemoryURLIndex::ShutDown() { | 143 void InMemoryURLIndex::ShutDown() { |
| 142 if (history_service_) | 144 history_service_observer_.RemoveAll(); |
| 143 history_service_->RemoveObserver(this); | |
| 144 registrar_.RemoveAll(); | 145 registrar_.RemoveAll(); |
| 145 cache_reader_tracker_.TryCancelAll(); | 146 cache_reader_tracker_.TryCancelAll(); |
| 146 shutdown_ = true; | 147 shutdown_ = true; |
| 147 base::FilePath path; | 148 base::FilePath path; |
| 148 if (!GetCacheFilePath(&path)) | 149 if (!GetCacheFilePath(&path)) |
| 149 return; | 150 return; |
| 150 private_data_tracker_.TryCancelAll(); | 151 private_data_tracker_.TryCancelAll(); |
| 151 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); | 152 URLIndexPrivateData::WritePrivateDataToCacheFileTask(private_data_, path); |
| 152 needs_to_be_cached_ = false; | 153 needs_to_be_cached_ = false; |
| 153 } | 154 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 184 } | 185 } |
| 185 | 186 |
| 186 void InMemoryURLIndex::Observe(int notification_type, | 187 void InMemoryURLIndex::Observe(int notification_type, |
| 187 const content::NotificationSource& source, | 188 const content::NotificationSource& source, |
| 188 const content::NotificationDetails& details) { | 189 const content::NotificationDetails& details) { |
| 189 switch (notification_type) { | 190 switch (notification_type) { |
| 190 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: | 191 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: |
| 191 OnURLsDeleted( | 192 OnURLsDeleted( |
| 192 content::Details<history::URLsDeletedDetails>(details).ptr()); | 193 content::Details<history::URLsDeletedDetails>(details).ptr()); |
| 193 break; | 194 break; |
| 194 case chrome::NOTIFICATION_HISTORY_LOADED: | |
| 195 registrar_.Remove(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
| 196 content::Source<Profile>(profile_)); | |
| 197 ScheduleRebuildFromHistory(); | |
| 198 break; | |
| 199 default: | 195 default: |
| 200 // For simplicity, the unit tests send us all notifications, even when | 196 // For simplicity, the unit tests send us all notifications, even when |
| 201 // we haven't registered for them, so don't assert here. | 197 // we haven't registered for them, so don't assert here. |
| 202 break; | 198 break; |
| 203 } | 199 } |
| 204 } | 200 } |
| 205 | 201 |
| 206 void InMemoryURLIndex::OnURLVisited(HistoryService* history_service, | 202 void InMemoryURLIndex::OnURLVisited(HistoryService* history_service, |
| 207 ui::PageTransition transition, | 203 ui::PageTransition transition, |
| 208 const URLRow& row, | 204 const URLRow& row, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 221 DCHECK_EQ(history_service_, history_service); | 217 DCHECK_EQ(history_service_, history_service); |
| 222 for (const auto& row : changed_urls) { | 218 for (const auto& row : changed_urls) { |
| 223 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, | 219 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, |
| 224 row, | 220 row, |
| 225 languages_, | 221 languages_, |
| 226 scheme_whitelist_, | 222 scheme_whitelist_, |
| 227 &private_data_tracker_); | 223 &private_data_tracker_); |
| 228 } | 224 } |
| 229 } | 225 } |
| 230 | 226 |
| 227 void InMemoryURLIndex::OnHistoryServiceLoaded(HistoryService* history_service) { |
| 228 ScheduleRebuildFromHistory(); |
| 229 } |
| 230 |
| 231 void InMemoryURLIndex::OnURLsDeleted(const URLsDeletedDetails* details) { | 231 void InMemoryURLIndex::OnURLsDeleted(const URLsDeletedDetails* details) { |
| 232 if (details->all_history) { | 232 if (details->all_history) { |
| 233 ClearPrivateData(); | 233 ClearPrivateData(); |
| 234 needs_to_be_cached_ = true; | 234 needs_to_be_cached_ = true; |
| 235 } else { | 235 } else { |
| 236 for (URLRows::const_iterator row = details->rows.begin(); | 236 for (URLRows::const_iterator row = details->rows.begin(); |
| 237 row != details->rows.end(); ++row) | 237 row != details->rows.end(); ++row) |
| 238 needs_to_be_cached_ |= private_data_->DeleteURL(row->url()); | 238 needs_to_be_cached_ |= private_data_->DeleteURL(row->url()); |
| 239 } | 239 } |
| 240 // If we made changes, destroy the previous cache. Otherwise, if we go | 240 // If we made changes, destroy the previous cache. Otherwise, if we go |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // When unable to restore from the cache file delete the cache file, if | 291 // When unable to restore from the cache file delete the cache file, if |
| 292 // it exists, and then rebuild from the history database if it's available, | 292 // it exists, and then rebuild from the history database if it's available, |
| 293 // otherwise wait until the history database loaded and then rebuild. | 293 // otherwise wait until the history database loaded and then rebuild. |
| 294 base::FilePath path; | 294 base::FilePath path; |
| 295 if (!GetCacheFilePath(&path) || shutdown_) | 295 if (!GetCacheFilePath(&path) || shutdown_) |
| 296 return; | 296 return; |
| 297 content::BrowserThread::PostBlockingPoolTask( | 297 content::BrowserThread::PostBlockingPoolTask( |
| 298 FROM_HERE, base::Bind(DeleteCacheFile, path)); | 298 FROM_HERE, base::Bind(DeleteCacheFile, path)); |
| 299 HistoryService* service = | 299 HistoryService* service = |
| 300 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 300 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
| 301 if (service && service->backend_loaded()) { | 301 if (service) { |
| 302 ScheduleRebuildFromHistory(); | 302 if (!service->backend_loaded()) { |
| 303 } else { | 303 if (!history_service_observer_.IsObserving(service)) |
| 304 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | 304 history_service_observer_.Add(service); |
| 305 content::Source<Profile>(profile_)); | 305 } else { |
| 306 ScheduleRebuildFromHistory(); |
| 307 } |
| 306 } | 308 } |
| 307 } | 309 } |
| 308 } | 310 } |
| 309 | 311 |
| 310 // Restoring from the History DB ----------------------------------------------- | 312 // Restoring from the History DB ----------------------------------------------- |
| 311 | 313 |
| 312 void InMemoryURLIndex::ScheduleRebuildFromHistory() { | 314 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
| 313 HistoryService* service = | 315 HistoryService* service = |
| 314 HistoryServiceFactory::GetForProfile(profile_, | 316 HistoryServiceFactory::GetForProfile(profile_, |
| 315 Profile::EXPLICIT_ACCESS); | 317 Profile::EXPLICIT_ACCESS); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 base::Bind(DeleteCacheFile, path)); | 372 base::Bind(DeleteCacheFile, path)); |
| 371 } | 373 } |
| 372 } | 374 } |
| 373 | 375 |
| 374 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 376 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
| 375 if (save_cache_observer_) | 377 if (save_cache_observer_) |
| 376 save_cache_observer_->OnCacheSaveFinished(succeeded); | 378 save_cache_observer_->OnCacheSaveFinished(succeeded); |
| 377 } | 379 } |
| 378 | 380 |
| 379 } // namespace history | 381 } // namespace history |
| OLD | NEW |