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// Contains the history backend wrapper around the in-memory URL database. This
6// object maintains an in-memory cache of the subset of history required to do
7// in-line autocomplete.
8//
9// It is created on the history thread and passed to the main thread where
10// operations can be completed synchronously. It listens for notifications
11// from the "regular" history backend and keeps itself in sync.
12
13#ifndef CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
14#define CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
15
16#include <string>
17
18#include "base/basictypes.h"
19#include "base/gtest_prod_util.h"
20#include "base/memory/scoped_ptr.h"
21#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
23
24class GURL;
25class Profile;
26
27namespace base {
28class FilePath;
29}
30
31namespace history {
32
33class InMemoryDatabase;
34class InMemoryURLIndex;
35struct KeywordSearchTermDetails;
36class URLDatabase;
37struct URLsDeletedDetails;
38struct URLsModifiedDetails;
39
40class InMemoryHistoryBackend : public content::NotificationObserver {
41 public:
42  InMemoryHistoryBackend();
43  virtual ~InMemoryHistoryBackend();
44
45  // Initializes the backend from the history database pointed to by the
46  // full path in |history_filename|. |db| is used for setting up the
47  // InMemoryDatabase.
48  bool Init(const base::FilePath& history_filename, URLDatabase* db);
49
50  // Does initialization work when this object is attached to the history
51  // system on the main thread. The argument is the profile with which the
52  // attached history service is under.
53  void AttachToHistoryService(Profile* profile);
54
55  // Returns the underlying database associated with this backend. The current
56  // autocomplete code was written fro this, but it should probably be removed
57  // so that it can deal directly with this object, rather than the DB.
58  InMemoryDatabase* db() const {
59    return db_.get();
60  }
61
62  // Notification callback.
63  virtual void Observe(int type,
64                       const content::NotificationSource& source,
65                       const content::NotificationDetails& details) OVERRIDE;
66
67 private:
68  FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
69
70  // Handler for NOTIFY_HISTORY_TYPED_URLS_MODIFIED.
71  void OnTypedURLsModified(const URLsModifiedDetails& details);
72
73  // Handler for NOTIFY_HISTORY_URLS_DELETED.
74  void OnURLsDeleted(const URLsDeletedDetails& details);
75
76  // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
77  void OnKeywordSearchTermUpdated(const KeywordSearchTermDetails& details);
78
79  // Returns true if there is a keyword associated with the specified url.
80  bool HasKeyword(const GURL& url);
81
82  content::NotificationRegistrar registrar_;
83
84  scoped_ptr<InMemoryDatabase> db_;
85
86  // The profile that this object is attached. May be NULL before
87  // initialization.
88  Profile* profile_;
89
90  DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
91};
92
93}  // namespace history
94
95#endif  // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
96