File: in_memory_history_backend.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (159 lines) | stat: -rw-r--r-- 5,596 bytes parent folder | download
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
// 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/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/history/core/browser/in_memory_database.h"
#include "components/history/core/browser/url_database.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"

namespace history {

InMemoryHistoryBackend::InMemoryHistoryBackend()
    : profile_(nullptr),
      history_service_observer_(this),
      history_service_(nullptr) {
}

InMemoryHistoryBackend::~InMemoryHistoryBackend() {
}

bool InMemoryHistoryBackend::Init(const base::FilePath& history_filename) {
  db_.reset(new InMemoryDatabase);
  return db_->InitFromDisk(history_filename);
}

void InMemoryHistoryBackend::AttachToHistoryService(
    Profile* profile,
    HistoryService* history_service) {
  if (!db_) {
    NOTREACHED();
    return;
  }

  profile_ = profile;

  DCHECK(history_service);
  history_service_observer_.Add(history_service);
  history_service_ = history_service;

  // TODO(evanm): this is currently necessitated by generate_profile, which
  // runs without a browser process. generate_profile should really create
  // a browser process, at which point this check can then be nuked.
  if (!g_browser_process)
    return;

  // Register for the notifications we care about.
  // We only want notifications for the associated profile.
  content::Source<Profile> source(profile_);
  registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED, source);
  registrar_.Add(
      this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, source);
  registrar_.Add(
      this, chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED, source);
}

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::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED:
      OnKeywordSearchTermUpdated(
          *content::Details<KeywordSearchUpdatedDetails>(details).ptr());
      break;
    case chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED:
      OnKeywordSearchTermDeleted(
          *content::Details<KeywordSearchDeletedDetails>(details).ptr());
      break;
    case chrome::NOTIFICATION_HISTORY_URLS_DELETED:
      OnURLsDeleted(*content::Details<URLsDeletedDetails>(details).ptr());
      break;
    default:
      // For simplicity, the unit tests send us all notifications, even when
      // we haven't registered for them, so don't assert here.
      break;
  }
}

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());
}

void InMemoryHistoryBackend::OnURLsDeleted(const URLsDeletedDetails& details) {
  DCHECK(db_);

  if (details.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 (URLRows::const_iterator row = details.rows.begin();
       row != details.rows.end(); ++row) {
    // 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(
    const KeywordSearchUpdatedDetails& details) {
  DCHECK(details.url_row.id());
  db_->InsertOrUpdateURLRowByID(details.url_row);
  db_->SetKeywordSearchTermsForURL(
      details.url_row.id(), details.keyword_id, details.term);
}

void InMemoryHistoryBackend::OnKeywordSearchTermDeleted(
    const KeywordSearchDeletedDetails& details) {
  // For simplicity, this will not remove the corresponding URLRow, but this is
  // okay, as the main database does not do so either.
  db_->DeleteKeywordSearchTermForURL(details.url_row_id);
}

}  // namespace history