Chromium Code Reviews| 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 "components/omnibox/browser/in_memory_url_index.h" | 5 #include "components/omnibox/browser/in_memory_url_index.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "components/history/core/browser/history_service.h" | 10 #include "components/history/core/browser/history_service.h" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 38 } | 38 } |
| 39 | 39 |
| 40 InMemoryURLIndex::SaveCacheObserver::~SaveCacheObserver() { | 40 InMemoryURLIndex::SaveCacheObserver::~SaveCacheObserver() { |
| 41 } | 41 } |
| 42 | 42 |
| 43 // RebuildPrivateDataFromHistoryDBTask ----------------------------------------- | 43 // RebuildPrivateDataFromHistoryDBTask ----------------------------------------- |
| 44 | 44 |
| 45 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: | 45 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
| 46 RebuildPrivateDataFromHistoryDBTask( | 46 RebuildPrivateDataFromHistoryDBTask( |
| 47 InMemoryURLIndex* index, | 47 InMemoryURLIndex* index, |
| 48 const std::string& languages, | |
| 49 const SchemeSet& scheme_whitelist) | 48 const SchemeSet& scheme_whitelist) |
| 50 : index_(index), | 49 : index_(index), |
| 51 languages_(languages), | |
| 52 scheme_whitelist_(scheme_whitelist), | 50 scheme_whitelist_(scheme_whitelist), |
| 53 succeeded_(false) { | 51 succeeded_(false) { |
| 54 } | 52 } |
| 55 | 53 |
| 56 bool InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::RunOnDBThread( | 54 bool InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::RunOnDBThread( |
| 57 history::HistoryBackend* backend, | 55 history::HistoryBackend* backend, |
| 58 history::HistoryDatabase* db) { | 56 history::HistoryDatabase* db) { |
| 59 data_ = URLIndexPrivateData::RebuildFromHistory(db, languages_, | 57 data_ = URLIndexPrivateData::RebuildFromHistory(db, scheme_whitelist_); |
| 60 scheme_whitelist_); | |
| 61 succeeded_ = data_.get() && !data_->Empty(); | 58 succeeded_ = data_.get() && !data_->Empty(); |
| 62 if (!succeeded_ && data_.get()) | 59 if (!succeeded_ && data_.get()) |
| 63 data_->Clear(); | 60 data_->Clear(); |
| 64 return true; | 61 return true; |
| 65 } | 62 } |
| 66 | 63 |
| 67 void InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: | 64 void InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
| 68 DoneRunOnMainThread() { | 65 DoneRunOnMainThread() { |
| 69 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); | 66 index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_); |
| 70 } | 67 } |
| 71 | 68 |
| 72 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: | 69 InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask:: |
| 73 ~RebuildPrivateDataFromHistoryDBTask() { | 70 ~RebuildPrivateDataFromHistoryDBTask() { |
| 74 } | 71 } |
| 75 | 72 |
| 76 // InMemoryURLIndex ------------------------------------------------------------ | 73 // InMemoryURLIndex ------------------------------------------------------------ |
| 77 | 74 |
| 78 InMemoryURLIndex::InMemoryURLIndex( | 75 InMemoryURLIndex::InMemoryURLIndex( |
| 79 bookmarks::BookmarkModel* bookmark_model, | 76 bookmarks::BookmarkModel* bookmark_model, |
| 80 history::HistoryService* history_service, | 77 history::HistoryService* history_service, |
| 81 TemplateURLService* template_url_service, | 78 TemplateURLService* template_url_service, |
| 82 base::SequencedWorkerPool* worker_pool, | 79 base::SequencedWorkerPool* worker_pool, |
| 83 const base::FilePath& history_dir, | 80 const base::FilePath& history_dir, |
| 84 const std::string& languages, | |
| 85 const SchemeSet& client_schemes_to_whitelist) | 81 const SchemeSet& client_schemes_to_whitelist) |
| 86 : bookmark_model_(bookmark_model), | 82 : bookmark_model_(bookmark_model), |
| 87 history_service_(history_service), | 83 history_service_(history_service), |
| 88 template_url_service_(template_url_service), | 84 template_url_service_(template_url_service), |
| 89 history_dir_(history_dir), | 85 history_dir_(history_dir), |
| 90 languages_(languages), | |
| 91 private_data_(new URLIndexPrivateData), | 86 private_data_(new URLIndexPrivateData), |
| 92 restore_cache_observer_(NULL), | 87 restore_cache_observer_(NULL), |
| 93 save_cache_observer_(NULL), | 88 save_cache_observer_(NULL), |
| 94 task_runner_( | 89 task_runner_( |
| 95 worker_pool->GetSequencedTaskRunner(worker_pool->GetSequenceToken())), | 90 worker_pool->GetSequencedTaskRunner(worker_pool->GetSequenceToken())), |
| 96 shutdown_(false), | 91 shutdown_(false), |
| 97 restored_(false), | 92 restored_(false), |
| 98 needs_to_be_cached_(false), | 93 needs_to_be_cached_(false), |
| 99 listen_to_history_service_loaded_(false) { | 94 listen_to_history_service_loaded_(false) { |
| 100 InitializeSchemeWhitelist(&scheme_whitelist_, client_schemes_to_whitelist); | 95 InitializeSchemeWhitelist(&scheme_whitelist_, client_schemes_to_whitelist); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 126 return true; | 121 return true; |
| 127 } | 122 } |
| 128 | 123 |
| 129 // Querying -------------------------------------------------------------------- | 124 // Querying -------------------------------------------------------------------- |
| 130 | 125 |
| 131 ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms( | 126 ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms( |
| 132 const base::string16& term_string, | 127 const base::string16& term_string, |
| 133 size_t cursor_position, | 128 size_t cursor_position, |
| 134 size_t max_matches) { | 129 size_t max_matches) { |
| 135 return private_data_->HistoryItemsForTerms( | 130 return private_data_->HistoryItemsForTerms( |
| 136 term_string, cursor_position, max_matches, languages_, bookmark_model_, | 131 term_string, cursor_position, max_matches, bookmark_model_, |
| 137 template_url_service_); | 132 template_url_service_); |
| 138 } | 133 } |
| 139 | 134 |
| 140 // Updating -------------------------------------------------------------------- | 135 // Updating -------------------------------------------------------------------- |
| 141 | 136 |
| 142 void InMemoryURLIndex::DeleteURL(const GURL& url) { | 137 void InMemoryURLIndex::DeleteURL(const GURL& url) { |
| 143 private_data_->DeleteURL(url); | 138 private_data_->DeleteURL(url); |
| 144 } | 139 } |
| 145 | 140 |
| 146 void InMemoryURLIndex::OnURLVisited(history::HistoryService* history_service, | 141 void InMemoryURLIndex::OnURLVisited(history::HistoryService* history_service, |
| 147 ui::PageTransition transition, | 142 ui::PageTransition transition, |
| 148 const history::URLRow& row, | 143 const history::URLRow& row, |
| 149 const history::RedirectList& redirects, | 144 const history::RedirectList& redirects, |
| 150 base::Time visit_time) { | 145 base::Time visit_time) { |
| 151 DCHECK_EQ(history_service_, history_service); | 146 DCHECK_EQ(history_service_, history_service); |
| 152 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, | 147 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, |
| 153 row, | 148 row, |
| 154 languages_, | |
| 155 scheme_whitelist_, | 149 scheme_whitelist_, |
| 156 &private_data_tracker_); | 150 &private_data_tracker_); |
| 157 } | 151 } |
| 158 | 152 |
| 159 void InMemoryURLIndex::OnURLsModified(history::HistoryService* history_service, | 153 void InMemoryURLIndex::OnURLsModified(history::HistoryService* history_service, |
| 160 const history::URLRows& changed_urls) { | 154 const history::URLRows& changed_urls) { |
| 161 DCHECK_EQ(history_service_, history_service); | 155 DCHECK_EQ(history_service_, history_service); |
| 162 for (const auto& row : changed_urls) { | 156 for (const auto& row : changed_urls) { |
| 163 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, | 157 needs_to_be_cached_ |= private_data_->UpdateURL(history_service_, |
| 164 row, | 158 row, |
| 165 languages_, | |
| 166 scheme_whitelist_, | 159 scheme_whitelist_, |
| 167 &private_data_tracker_); | 160 &private_data_tracker_); |
| 168 } | 161 } |
| 169 } | 162 } |
| 170 | 163 |
| 171 void InMemoryURLIndex::OnURLsDeleted(history::HistoryService* history_service, | 164 void InMemoryURLIndex::OnURLsDeleted(history::HistoryService* history_service, |
| 172 bool all_history, | 165 bool all_history, |
| 173 bool expired, | 166 bool expired, |
| 174 const history::URLRows& deleted_rows, | 167 const history::URLRows& deleted_rows, |
| 175 const std::set<GURL>& favicon_urls) { | 168 const std::set<GURL>& favicon_urls) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 218 if (!GetCacheFilePath(&path) || shutdown_) { | 211 if (!GetCacheFilePath(&path) || shutdown_) { |
| 219 restored_ = true; | 212 restored_ = true; |
| 220 if (restore_cache_observer_) | 213 if (restore_cache_observer_) |
| 221 restore_cache_observer_->OnCacheRestoreFinished(false); | 214 restore_cache_observer_->OnCacheRestoreFinished(false); |
| 222 return; | 215 return; |
| 223 } | 216 } |
| 224 | 217 |
| 225 base::PostTaskAndReplyWithResult( | 218 base::PostTaskAndReplyWithResult( |
| 226 task_runner_.get(), | 219 task_runner_.get(), |
| 227 FROM_HERE, | 220 FROM_HERE, |
| 228 base::Bind(&URLIndexPrivateData::RestoreFromFile, path, languages_), | 221 base::Bind(&URLIndexPrivateData::RestoreFromFile, path), |
| 229 base::Bind(&InMemoryURLIndex::OnCacheLoadDone, AsWeakPtr())); | 222 base::Bind(&InMemoryURLIndex::OnCacheLoadDone, AsWeakPtr())); |
| 230 } | 223 } |
| 231 | 224 |
| 232 void InMemoryURLIndex::OnCacheLoadDone( | 225 void InMemoryURLIndex::OnCacheLoadDone( |
| 233 scoped_refptr<URLIndexPrivateData> private_data) { | 226 scoped_refptr<URLIndexPrivateData> private_data) { |
| 234 if (private_data.get() && !private_data->Empty()) { | 227 if (private_data.get() && !private_data->Empty()) { |
| 235 private_data_tracker_.TryCancelAll(); | 228 private_data_tracker_.TryCancelAll(); |
| 236 private_data_ = private_data; | 229 private_data_ = private_data; |
| 237 restored_ = true; | 230 restored_ = true; |
| 238 if (restore_cache_observer_) | 231 if (restore_cache_observer_) |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 needs_to_be_cached_ = false; | 270 needs_to_be_cached_ = false; |
| 278 } | 271 } |
| 279 | 272 |
| 280 // Restoring from the History DB ----------------------------------------------- | 273 // Restoring from the History DB ----------------------------------------------- |
| 281 | 274 |
| 282 void InMemoryURLIndex::ScheduleRebuildFromHistory() { | 275 void InMemoryURLIndex::ScheduleRebuildFromHistory() { |
| 283 DCHECK(history_service_); | 276 DCHECK(history_service_); |
| 284 history_service_->ScheduleDBTask( | 277 history_service_->ScheduleDBTask( |
| 285 scoped_ptr<history::HistoryDBTask>( | 278 scoped_ptr<history::HistoryDBTask>( |
| 286 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( | 279 new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask( |
| 287 this, languages_, scheme_whitelist_)), | 280 this, scheme_whitelist_)), |
|
Peter Kasting
2016/04/05 02:42:32
Nit: Extra space
jungshik at Google
2016/04/05 18:56:19
Done.
| |
| 288 &cache_reader_tracker_); | 281 &cache_reader_tracker_); |
| 289 } | 282 } |
| 290 | 283 |
| 291 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( | 284 void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB( |
| 292 bool succeeded, | 285 bool succeeded, |
| 293 scoped_refptr<URLIndexPrivateData> private_data) { | 286 scoped_refptr<URLIndexPrivateData> private_data) { |
| 294 DCHECK(thread_checker_.CalledOnValidThread()); | 287 DCHECK(thread_checker_.CalledOnValidThread()); |
| 295 if (succeeded) { | 288 if (succeeded) { |
| 296 private_data_tracker_.TryCancelAll(); | 289 private_data_tracker_.TryCancelAll(); |
| 297 private_data_ = private_data; | 290 private_data_ = private_data; |
| 298 PostSaveToCacheFileTask(); // Cache the newly rebuilt index. | 291 PostSaveToCacheFileTask(); // Cache the newly rebuilt index. |
| 299 } else { | 292 } else { |
| 300 private_data_->Clear(); // Dump the old private data. | 293 private_data_->Clear(); // Dump the old private data. |
| 301 // There is no need to do anything with the cache file as it was deleted | 294 // There is no need to do anything with the cache file as it was deleted |
| 302 // when the rebuild from the history operation was kicked off. | 295 // when the rebuild from the history operation was kicked off. |
| 303 } | 296 } |
| 304 restored_ = true; | 297 restored_ = true; |
| 305 if (restore_cache_observer_) | 298 if (restore_cache_observer_) |
| 306 restore_cache_observer_->OnCacheRestoreFinished(succeeded); | 299 restore_cache_observer_->OnCacheRestoreFinished(succeeded); |
| 307 } | 300 } |
| 308 | 301 |
| 309 void InMemoryURLIndex::RebuildFromHistory( | 302 void InMemoryURLIndex::RebuildFromHistory( |
| 310 history::HistoryDatabase* history_db) { | 303 history::HistoryDatabase* history_db) { |
| 311 private_data_tracker_.TryCancelAll(); | 304 private_data_tracker_.TryCancelAll(); |
| 312 private_data_ = URLIndexPrivateData::RebuildFromHistory(history_db, | 305 private_data_ = URLIndexPrivateData::RebuildFromHistory(history_db, |
| 313 languages_, | |
| 314 scheme_whitelist_); | 306 scheme_whitelist_); |
| 315 } | 307 } |
| 316 | 308 |
| 317 // Saving to Cache ------------------------------------------------------------- | 309 // Saving to Cache ------------------------------------------------------------- |
| 318 | 310 |
| 319 void InMemoryURLIndex::PostSaveToCacheFileTask() { | 311 void InMemoryURLIndex::PostSaveToCacheFileTask() { |
| 320 base::FilePath path; | 312 base::FilePath path; |
| 321 if (!GetCacheFilePath(&path)) | 313 if (!GetCacheFilePath(&path)) |
| 322 return; | 314 return; |
| 323 // If there is anything in our private data then make a copy of it and tell | 315 // If there is anything in our private data then make a copy of it and tell |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 338 task_runner_->PostTask( | 330 task_runner_->PostTask( |
| 339 FROM_HERE, | 331 FROM_HERE, |
| 340 base::Bind(base::IgnoreResult(base::DeleteFile), path, false)); | 332 base::Bind(base::IgnoreResult(base::DeleteFile), path, false)); |
| 341 } | 333 } |
| 342 } | 334 } |
| 343 | 335 |
| 344 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { | 336 void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) { |
| 345 if (save_cache_observer_) | 337 if (save_cache_observer_) |
| 346 save_cache_observer_->OnCacheSaveFinished(succeeded); | 338 save_cache_observer_->OnCacheSaveFinished(succeeded); |
| 347 } | 339 } |
| OLD | NEW |