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.
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.
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.
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.
20 #ifndef CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
21 #define CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
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"
40 class InMemoryDatabase;
41 struct KeywordSearchUpdatedDetails;
42 struct KeywordSearchDeletedDetails;
45 struct URLsDeletedDetails;
46 struct URLsModifiedDetails;
48 class InMemoryHistoryBackend : public content::NotificationObserver {
50 InMemoryHistoryBackend();
51 virtual ~InMemoryHistoryBackend();
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);
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);
62 // Deletes all search terms for the specified keyword.
63 void DeleteAllSearchTermsForKeyword(KeywordID keyword_id);
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 {
72 // Notification callback.
73 virtual void Observe(int type,
74 const content::NotificationSource& source,
75 const content::NotificationDetails& details) OVERRIDE;
78 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
80 // Handler for HISTORY_URL_VISITED and HISTORY_URLS_MODIFIED.
81 void OnURLVisitedOrModified(const URLRow& url_row);
83 // Handler for HISTORY_URLS_DELETED.
84 void OnURLsDeleted(const URLsDeletedDetails& details);
86 // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
87 void OnKeywordSearchTermUpdated(const KeywordSearchUpdatedDetails& details);
89 // Handler for HISTORY_KEYWORD_SEARCH_TERM_DELETED.
90 void OnKeywordSearchTermDeleted(const KeywordSearchDeletedDetails& details);
92 content::NotificationRegistrar registrar_;
94 scoped_ptr<InMemoryDatabase> db_;
96 // The profile that this object is attached. May be NULL before
100 DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
103 } // namespace history
105 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_