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 COMPONENTS_HISTORY_CORE_BROWSER_IN_MEMORY_HISTORY_BACKEND_H_
21 #define COMPONENTS_HISTORY_CORE_BROWSER_IN_MEMORY_HISTORY_BACKEND_H_
22 
23 #include <memory>
24 #include <string>
25 
26 #include "base/gtest_prod_util.h"
27 #include "base/macros.h"
28 #include "base/scoped_observer.h"
29 #include "components/history/core/browser/history_service.h"
30 #include "components/history/core/browser/history_service_observer.h"
31 #include "components/history/core/browser/keyword_id.h"
32 
33 namespace base {
34 class FilePath;
35 }
36 
37 namespace history {
38 
39 class HistoryBackendTestBase;
40 class InMemoryDatabase;
41 class InMemoryHistoryBackendTest;
42 class URLRow;
43 
44 class InMemoryHistoryBackend : public HistoryServiceObserver {
45  public:
46   InMemoryHistoryBackend();
47   ~InMemoryHistoryBackend() override;
48 
49   // Initializes the backend from the history database pointed to by the
50   // full path in |history_filename|.
51   bool Init(const base::FilePath& history_filename);
52 
53   // Does initialization work when this object is attached to the history
54   // system on the main thread. The argument is the profile with which the
55   // attached history service is under.
56   void AttachToHistoryService(HistoryService* history_service);
57 
58   // Deletes all search terms for the specified keyword.
59   void DeleteAllSearchTermsForKeyword(KeywordID keyword_id);
60 
61   // Returns the underlying database associated with this backend. The current
62   // autocomplete code was written fro this, but it should probably be removed
63   // so that it can deal directly with this object, rather than the DB.
db()64   InMemoryDatabase* db() const { return db_.get(); }
65 
66  private:
67   FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
68   FRIEND_TEST_ALL_PREFIXES(InMemoryHistoryBackendTest, OnURLsDeletedEnMasse);
69   friend class HistoryBackendTestBase;
70   friend class InMemoryHistoryBackendTest;
71 
72   // HistoryServiceObserver:
73   void OnURLVisited(HistoryService* history_service,
74                     ui::PageTransition transition,
75                     const URLRow& row,
76                     const RedirectList& redirects,
77                     base::Time visit_time) override;
78   void OnURLsModified(HistoryService* history_service,
79                       const URLRows& changed_urls) override;
80   void OnURLsDeleted(HistoryService* history_service,
81                      const DeletionInfo& deletion_info) override;
82   void OnKeywordSearchTermUpdated(HistoryService* history_service,
83                                   const URLRow& row,
84                                   KeywordID keyword_id,
85                                   const base::string16& term) override;
86   void OnKeywordSearchTermDeleted(HistoryService* history_service,
87                                   URLID url_id) override;
88 
89   // Handler for HISTORY_URL_VISITED and HISTORY_URLS_MODIFIED.
90   void OnURLVisitedOrModified(const URLRow& url_row);
91 
92   std::unique_ptr<InMemoryDatabase> db_;
93 
94   ScopedObserver<HistoryService, HistoryServiceObserver>
95       history_service_observer_{this};
96 
97   DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
98 };
99 
100 }  // namespace history
101 
102 #endif  // COMPONENTS_HISTORY_CORE_BROWSER_IN_MEMORY_HISTORY_BACKEND_H_
103