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
3// found in the LICENSE file.
4
5// The InMemoryHistoryBackend is a wrapper around the in-memory URL database.
6// It maintains an in-memory cache of a subset of history that is required for
7// low-latency operations, such as in-line autocomplete.
8//
9// The in-memory cache provides the following guarantees:
10//  (1.) It will always contain URLRows that either have a |typed_count| > 0; or
11//       that have a corresponding search term, in which case information about
12//       the search term is also stored.
13//  (2.) It will be an actual subset, i.e., it will contain verbatim data, and
14//       will never contain more data that can be found in the main database.
15//
16// The InMemoryHistoryBackend is created on the history thread and passed to the
17// main thread where operations can be completed synchronously. It listens for
18// notifications from the "regular" history backend and keeps itself in sync.
19
20#ifndef CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
21#define CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
22
23#include <string>
24
25#include "base/basictypes.h"
26#include "base/gtest_prod_util.h"
27#include "base/memory/scoped_ptr.h"
28#include "components/history/core/browser/keyword_id.h"
29#include "content/public/browser/notification_observer.h"
30#include "content/public/browser/notification_registrar.h"
31
32class Profile;
33
34namespace base {
35class FilePath;
36}
37
38namespace history {
39
40class InMemoryDatabase;
41struct KeywordSearchUpdatedDetails;
42struct KeywordSearchDeletedDetails;
43class URLDatabase;
44class URLRow;
45struct URLsDeletedDetails;
46struct URLsModifiedDetails;
47
48class InMemoryHistoryBackend : public content::NotificationObserver {
49 public:
50  InMemoryHistoryBackend();
51  virtual ~InMemoryHistoryBackend();
52
53  // Initializes the backend from the history database pointed to by the
54  // full path in |history_filename|.
55  bool Init(const base::FilePath& history_filename);
56
57  // Does initialization work when this object is attached to the history
58  // system on the main thread. The argument is the profile with which the
59  // attached history service is under.
60  void AttachToHistoryService(Profile* profile);
61
62  // Deletes all search terms for the specified keyword.
63  void DeleteAllSearchTermsForKeyword(KeywordID keyword_id);
64
65  // Returns the underlying database associated with this backend. The current
66  // autocomplete code was written fro this, but it should probably be removed
67  // so that it can deal directly with this object, rather than the DB.
68  InMemoryDatabase* db() const {
69    return db_.get();
70  }
71
72  // Notification callback.
73  virtual void Observe(int type,
74                       const content::NotificationSource& source,
75                       const content::NotificationDetails& details) OVERRIDE;
76
77 private:
78  FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
79
80  // Handler for HISTORY_URL_VISITED and HISTORY_URLS_MODIFIED.
81  void OnURLVisitedOrModified(const URLRow& url_row);
82
83  // Handler for HISTORY_URLS_DELETED.
84  void OnURLsDeleted(const URLsDeletedDetails& details);
85
86  // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
87  void OnKeywordSearchTermUpdated(const KeywordSearchUpdatedDetails& details);
88
89  // Handler for HISTORY_KEYWORD_SEARCH_TERM_DELETED.
90  void OnKeywordSearchTermDeleted(const KeywordSearchDeletedDetails& details);
91
92  content::NotificationRegistrar registrar_;
93
94  scoped_ptr<InMemoryDatabase> db_;
95
96  // The profile that this object is attached. May be NULL before
97  // initialization.
98  Profile* profile_;
99
100  DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
101};
102
103}  // namespace history
104
105#endif  // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
106