1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/in_memory_url_index.h"
#include "base/files/file_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task_runner_util.h"
#include "base/trace_event/trace_event.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/url_database.h"
#include "components/omnibox/browser/url_index_private_data.h"
using in_memory_url_index::InMemoryURLIndexCacheItem;
// Initializes a whitelist of URL schemes.
void InitializeSchemeWhitelist(
SchemeSet* whitelist,
const SchemeSet& client_schemes_to_whitelist) {
DCHECK(whitelist);
if (!whitelist->empty())
return; // Nothing to do, already initialized.
whitelist->insert(client_schemes_to_whitelist.begin(),
client_schemes_to_whitelist.end());
whitelist->insert(std::string(url::kAboutScheme));
whitelist->insert(std::string(url::kFileScheme));
whitelist->insert(std::string(url::kFtpScheme));
whitelist->insert(std::string(url::kHttpScheme));
whitelist->insert(std::string(url::kHttpsScheme));
whitelist->insert(std::string(url::kMailToScheme));
}
// Restore/SaveCacheObserver ---------------------------------------------------
InMemoryURLIndex::RestoreCacheObserver::~RestoreCacheObserver() {
}
InMemoryURLIndex::SaveCacheObserver::~SaveCacheObserver() {
}
// RebuildPrivateDataFromHistoryDBTask -----------------------------------------
InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::
RebuildPrivateDataFromHistoryDBTask(
InMemoryURLIndex* index,
const SchemeSet& scheme_whitelist)
: index_(index),
scheme_whitelist_(scheme_whitelist),
succeeded_(false) {
}
bool InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::RunOnDBThread(
history::HistoryBackend* backend,
history::HistoryDatabase* db) {
data_ = URLIndexPrivateData::RebuildFromHistory(db, scheme_whitelist_);
succeeded_ = data_.get() && !data_->Empty();
if (!succeeded_ && data_.get())
data_->Clear();
return true;
}
void InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::
DoneRunOnMainThread() {
index_->DoneRebuidingPrivateDataFromHistoryDB(succeeded_, data_);
}
InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask::
~RebuildPrivateDataFromHistoryDBTask() {
}
// InMemoryURLIndex ------------------------------------------------------------
InMemoryURLIndex::InMemoryURLIndex(
bookmarks::BookmarkModel* bookmark_model,
history::HistoryService* history_service,
TemplateURLService* template_url_service,
base::SequencedWorkerPool* worker_pool,
const base::FilePath& history_dir,
const SchemeSet& client_schemes_to_whitelist)
: bookmark_model_(bookmark_model),
history_service_(history_service),
template_url_service_(template_url_service),
history_dir_(history_dir),
private_data_(new URLIndexPrivateData),
restore_cache_observer_(NULL),
save_cache_observer_(NULL),
task_runner_(
worker_pool->GetSequencedTaskRunner(worker_pool->GetSequenceToken())),
shutdown_(false),
restored_(false),
needs_to_be_cached_(false),
listen_to_history_service_loaded_(false) {
InitializeSchemeWhitelist(&scheme_whitelist_, client_schemes_to_whitelist);
// TODO(mrossetti): Register for language change notifications.
if (history_service_)
history_service_->AddObserver(this);
}
InMemoryURLIndex::~InMemoryURLIndex() {
// If there was a history directory (which there won't be for some unit tests)
// then insure that the cache has already been saved.
DCHECK(history_dir_.empty() || !needs_to_be_cached_);
DCHECK(!history_service_);
DCHECK(shutdown_);
}
void InMemoryURLIndex::Init() {
PostRestoreFromCacheFileTask();
}
void InMemoryURLIndex::ClearPrivateData() {
private_data_->Clear();
}
bool InMemoryURLIndex::GetCacheFilePath(base::FilePath* file_path) {
if (history_dir_.empty())
return false;
*file_path = history_dir_.Append(FILE_PATH_LITERAL("History Provider Cache"));
return true;
}
// Querying --------------------------------------------------------------------
ScoredHistoryMatches InMemoryURLIndex::HistoryItemsForTerms(
const base::string16& term_string,
size_t cursor_position,
size_t max_matches) {
return private_data_->HistoryItemsForTerms(
term_string, cursor_position, max_matches, bookmark_model_,
template_url_service_);
}
// Updating --------------------------------------------------------------------
void InMemoryURLIndex::DeleteURL(const GURL& url) {
private_data_->DeleteURL(url);
}
void InMemoryURLIndex::OnURLVisited(history::HistoryService* history_service,
ui::PageTransition transition,
const history::URLRow& row,
const history::RedirectList& redirects,
base::Time visit_time) {
DCHECK_EQ(history_service_, history_service);
needs_to_be_cached_ |= private_data_->UpdateURL(history_service_,
row,
scheme_whitelist_,
&private_data_tracker_);
}
void InMemoryURLIndex::OnURLsModified(history::HistoryService* history_service,
const history::URLRows& changed_urls) {
DCHECK_EQ(history_service_, history_service);
for (const auto& row : changed_urls) {
needs_to_be_cached_ |= private_data_->UpdateURL(history_service_,
row,
scheme_whitelist_,
&private_data_tracker_);
}
}
void InMemoryURLIndex::OnURLsDeleted(history::HistoryService* history_service,
bool all_history,
bool expired,
const history::URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) {
if (all_history) {
ClearPrivateData();
needs_to_be_cached_ = true;
} else {
for (const auto& row : deleted_rows)
needs_to_be_cached_ |= private_data_->DeleteURL(row.url());
}
// If we made changes, destroy the previous cache. Otherwise, if we go
// through an unclean shutdown (and therefore fail to write a new cache file),
// when Chrome restarts and we restore from the previous cache, we'll end up
// searching over URLs that may be deleted. This would be wrong, and
// surprising to the user who bothered to delete some URLs from their
// history. In this situation, deleting the cache is a better solution than
// writing a new cache (after deleting the URLs from the in-memory structure)
// because deleting the cache forces it to be rebuilt from history upon
// startup. If we instead write a new, updated cache then at the time of next
// startup (after an unclean shutdown) we will not rebuild the in-memory data
// structures from history but rather use the cache. This solution is
// mediocre because this cache may not have the most-recently-visited URLs
// in it (URLs visited after user deleted some URLs from history), which
// would be odd and confusing. It's better to force a rebuild.
base::FilePath path;
if (needs_to_be_cached_ && GetCacheFilePath(&path))
task_runner_->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(base::DeleteFile), path, false));
}
void InMemoryURLIndex::OnHistoryServiceLoaded(
history::HistoryService* history_service) {
if (listen_to_history_service_loaded_)
ScheduleRebuildFromHistory();
listen_to_history_service_loaded_ = false;
}
// Restoring from Cache --------------------------------------------------------
void InMemoryURLIndex::PostRestoreFromCacheFileTask() {
DCHECK(thread_checker_.CalledOnValidThread());
TRACE_EVENT0("browser", "InMemoryURLIndex::PostRestoreFromCacheFileTask");
base::FilePath path;
if (!GetCacheFilePath(&path) || shutdown_) {
restored_ = true;
if (restore_cache_observer_)
restore_cache_observer_->OnCacheRestoreFinished(false);
return;
}
base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(&URLIndexPrivateData::RestoreFromFile, path),
base::Bind(&InMemoryURLIndex::OnCacheLoadDone, AsWeakPtr()));
}
void InMemoryURLIndex::OnCacheLoadDone(
scoped_refptr<URLIndexPrivateData> private_data) {
if (private_data.get() && !private_data->Empty()) {
private_data_tracker_.TryCancelAll();
private_data_ = private_data;
restored_ = true;
if (restore_cache_observer_)
restore_cache_observer_->OnCacheRestoreFinished(true);
} else if (history_service_) {
// When unable to restore from the cache file delete the cache file, if
// it exists, and then rebuild from the history database if it's available,
// otherwise wait until the history database loaded and then rebuild.
base::FilePath path;
if (!GetCacheFilePath(&path) || shutdown_)
return;
task_runner_->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(base::DeleteFile), path, false));
if (history_service_->backend_loaded()) {
ScheduleRebuildFromHistory();
} else {
listen_to_history_service_loaded_ = true;
}
}
}
// Cleanup ---------------------------------------------------------------------
void InMemoryURLIndex::Shutdown() {
if (history_service_) {
history_service_->RemoveObserver(this);
history_service_ = nullptr;
}
cache_reader_tracker_.TryCancelAll();
shutdown_ = true;
base::FilePath path;
if (!GetCacheFilePath(&path))
return;
private_data_tracker_.TryCancelAll();
task_runner_->PostTask(
FROM_HERE,
base::Bind(
base::IgnoreResult(
&URLIndexPrivateData::WritePrivateDataToCacheFileTask),
private_data_, path));
needs_to_be_cached_ = false;
}
// Restoring from the History DB -----------------------------------------------
void InMemoryURLIndex::ScheduleRebuildFromHistory() {
DCHECK(history_service_);
history_service_->ScheduleDBTask(
std::unique_ptr<history::HistoryDBTask>(
new InMemoryURLIndex::RebuildPrivateDataFromHistoryDBTask(
this, scheme_whitelist_)),
&cache_reader_tracker_);
}
void InMemoryURLIndex::DoneRebuidingPrivateDataFromHistoryDB(
bool succeeded,
scoped_refptr<URLIndexPrivateData> private_data) {
DCHECK(thread_checker_.CalledOnValidThread());
if (succeeded) {
private_data_tracker_.TryCancelAll();
private_data_ = private_data;
PostSaveToCacheFileTask(); // Cache the newly rebuilt index.
} else {
private_data_->Clear(); // Dump the old private data.
// There is no need to do anything with the cache file as it was deleted
// when the rebuild from the history operation was kicked off.
}
restored_ = true;
if (restore_cache_observer_)
restore_cache_observer_->OnCacheRestoreFinished(succeeded);
}
void InMemoryURLIndex::RebuildFromHistory(
history::HistoryDatabase* history_db) {
private_data_tracker_.TryCancelAll();
private_data_ = URLIndexPrivateData::RebuildFromHistory(history_db,
scheme_whitelist_);
}
// Saving to Cache -------------------------------------------------------------
void InMemoryURLIndex::PostSaveToCacheFileTask() {
base::FilePath path;
if (!GetCacheFilePath(&path))
return;
// If there is anything in our private data then make a copy of it and tell
// it to save itself to a file.
if (private_data_.get() && !private_data_->Empty()) {
// Note that ownership of the copy of our private data is passed to the
// completion closure below.
scoped_refptr<URLIndexPrivateData> private_data_copy =
private_data_->Duplicate();
base::PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
base::Bind(&URLIndexPrivateData::WritePrivateDataToCacheFileTask,
private_data_copy, path),
base::Bind(&InMemoryURLIndex::OnCacheSaveDone, AsWeakPtr()));
} else {
// If there is no data in our index then delete any existing cache file.
task_runner_->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(base::DeleteFile), path, false));
}
}
void InMemoryURLIndex::OnCacheSaveDone(bool succeeded) {
if (save_cache_observer_)
save_cache_observer_->OnCacheSaveFinished(succeeded);
}
|