1// Copyright (c) 2011 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#pragma once
16
17#include <string>
18
19#include "base/basictypes.h"
20#include "base/gtest_prod_util.h"
21#include "base/memory/scoped_ptr.h"
22#include "content/common/notification_observer.h"
23#include "content/common/notification_registrar.h"
24
25class FilePath;
26class GURL;
27class HistoryDatabase;
28class Profile;
29
30namespace history {
31
32class InMemoryDatabase;
33class InMemoryURLIndex;
34struct KeywordSearchTermDetails;
35class URLDatabase;
36struct URLsDeletedDetails;
37struct URLsModifiedDetails;
38
39class InMemoryHistoryBackend : public NotificationObserver {
40 public:
41  InMemoryHistoryBackend();
42  ~InMemoryHistoryBackend();
43
44  // Initializes the backend from the history database pointed to by the
45  // full path in |history_filename|. |history_dir| is the path to the
46  // directory containing the history database and is also used
47  // as the directory where the InMemoryURLIndex's cache is kept. |db| is
48  // used for building the InMemoryURLIndex. |languages| gives the
49  // preferred user languages with which URLs and page titles are
50  // interpreted while decomposing into words and characters during indexing.
51  bool Init(const FilePath& history_filename,
52            const FilePath& history_dir,
53            URLDatabase* db,
54            const std::string& languages);
55
56  // Does initialization work when this object is attached to the history
57  // system on the main thread. The argument is the profile with which the
58  // attached history service is under.
59  void AttachToHistoryService(Profile* profile);
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.
64  InMemoryDatabase* db() const {
65    return db_.get();
66  }
67
68  // Notification callback.
69  virtual void Observe(NotificationType type,
70                       const NotificationSource& source,
71                       const NotificationDetails& details);
72
73  // Return the quick history index.
74  history::InMemoryURLIndex* InMemoryIndex() const { return index_.get(); }
75
76 private:
77  FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
78
79  // Handler for NOTIFY_HISTORY_TYPED_URLS_MODIFIED.
80  void OnTypedURLsModified(const URLsModifiedDetails& details);
81
82  // Handler for NOTIFY_HISTORY_URLS_DELETED.
83  void OnURLsDeleted(const URLsDeletedDetails& details);
84
85  // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
86  void OnKeywordSearchTermUpdated(const KeywordSearchTermDetails& details);
87
88  // Returns true if there is a keyword associated with the specified url.
89  bool HasKeyword(const GURL& url);
90
91  NotificationRegistrar registrar_;
92
93  scoped_ptr<InMemoryDatabase> db_;
94
95  // The profile that this object is attached. May be NULL before
96  // initialization.
97  Profile* profile_;
98
99  // The index used for quick history lookups.
100  scoped_ptr<history::InMemoryURLIndex> index_;
101
102  DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
103};
104
105}  // namespace history
106
107#endif  // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
108