blob: 27a3b881a918f3ff9b06c1019e16b478ca50cde9 [file] [log] [blame]
// 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 "chrome/browser/history/in_memory_history_backend.h"
#include <set>
#include <vector>
#include "base/command_line.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/history/history_service.h"
#include "components/history/core/browser/in_memory_database.h"
#include "components/history/core/browser/url_database.h"
namespace history {
InMemoryHistoryBackend::InMemoryHistoryBackend()
: history_service_observer_(this) {
}
InMemoryHistoryBackend::~InMemoryHistoryBackend() {
}
bool InMemoryHistoryBackend::Init(const base::FilePath& history_filename) {
db_.reset(new InMemoryDatabase);
return db_->InitFromDisk(history_filename);
}
void InMemoryHistoryBackend::AttachToHistoryService(
HistoryService* history_service) {
DCHECK(db_);
DCHECK(history_service);
history_service_observer_.Add(history_service);
}
void InMemoryHistoryBackend::DeleteAllSearchTermsForKeyword(
KeywordID keyword_id) {
// For simplicity, this will not remove the corresponding URLRows, but
// this is okay, as the main database does not do so either.
db_->DeleteAllSearchTermsForKeyword(keyword_id);
}
void InMemoryHistoryBackend::OnURLVisited(HistoryService* history_service,
ui::PageTransition transition,
const URLRow& row,
const RedirectList& redirects,
base::Time visit_time) {
OnURLVisitedOrModified(row);
}
void InMemoryHistoryBackend::OnURLsModified(HistoryService* history_service,
const URLRows& changed_urls) {
for (const auto& row : changed_urls) {
OnURLVisitedOrModified(row);
}
}
void InMemoryHistoryBackend::OnURLsDeleted(HistoryService* history_service,
bool all_history,
bool expired,
const URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) {
DCHECK(db_);
if (all_history) {
// When all history is deleted, the individual URLs won't be listed. Just
// create a new database to quickly clear everything out.
db_.reset(new InMemoryDatabase);
if (!db_->InitFromScratch())
db_.reset();
return;
}
// Delete all matching URLs in our database.
for (const auto& row : deleted_rows) {
// This will also delete the corresponding keyword search term.
// Ignore errors, as we typically only cache a subset of URLRows.
db_->DeleteURLRow(row.id());
}
}
void InMemoryHistoryBackend::OnKeywordSearchTermUpdated(
HistoryService* history_service,
const URLRow& row,
KeywordID keyword_id,
const base::string16& term) {
DCHECK(row.id());
db_->InsertOrUpdateURLRowByID(row);
db_->SetKeywordSearchTermsForURL(row.id(), keyword_id, term);
}
void InMemoryHistoryBackend::OnKeywordSearchTermDeleted(
HistoryService* history_service,
URLID url_id) {
// For simplicity, this will not remove the corresponding URLRow, but this is
// okay, as the main database does not do so either.
db_->DeleteKeywordSearchTermForURL(url_id);
}
void InMemoryHistoryBackend::OnURLVisitedOrModified(const URLRow& url_row) {
DCHECK(db_);
DCHECK(url_row.id());
if (url_row.typed_count() || db_->GetKeywordSearchTermRow(url_row.id(), NULL))
db_->InsertOrUpdateURLRowByID(url_row);
else
db_->DeleteURLRow(url_row.id());
}
} // namespace history