summaryrefslogtreecommitdiff
path: root/chromium/components/history/core/browser/in_memory_history_backend.cc
blob: 9e11553e1eae067bb3fc8235da1ec7eee0c9df4e (plain)
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
// 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/history/core/browser/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 "components/history/core/browser/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(), nullptr))
    db_->InsertOrUpdateURLRowByID(url_row);
  else
    db_->DeleteURLRow(url_row.id());
}

}  // namespace history