in_memory_history_backend.h revision 513209b27ff55e2841eac0e4120199c23acce758
1// Copyright (c) 2010 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 listenes 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/scoped_ptr.h"
22#include "chrome/common/notification_observer.h"
23#include "chrome/common/notification_registrar.h"
24
25class FilePath;
26class HistoryDatabase;
27class Profile;
28
29namespace history {
30
31class InMemoryDatabase;
32class InMemoryURLIndex;
33struct KeywordSearchTermDetails;
34class URLDatabase;
35struct URLsDeletedDetails;
36struct URLsModifiedDetails;
37
38class InMemoryHistoryBackend : public NotificationObserver {
39 public:
40  InMemoryHistoryBackend();
41  ~InMemoryHistoryBackend();
42
43  // Initializes with data from the given history database.
44  bool Init(const FilePath& history_filename,
45            URLDatabase* db,
46            const std::string& languages);
47
48  // Does initialization work when this object is attached to the history
49  // system on the main thread. The argument is the profile with which the
50  // attached history service is under.
51  void AttachToHistoryService(Profile* profile);
52
53  // Returns the underlying database associated with this backend. The current
54  // autocomplete code was written fro this, but it should probably be removed
55  // so that it can deal directly with this object, rather than the DB.
56  InMemoryDatabase* db() const {
57    return db_.get();
58  }
59
60  // Notification callback.
61  virtual void Observe(NotificationType type,
62                       const NotificationSource& source,
63                       const NotificationDetails& details);
64
65  // Return the quick history index.
66  history::InMemoryURLIndex* InMemoryIndex() const { return index_.get(); }
67
68 private:
69  FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
70
71  // Handler for NOTIFY_HISTORY_TYPED_URLS_MODIFIED.
72  void OnTypedURLsModified(const URLsModifiedDetails& details);
73
74  // Handler for NOTIFY_HISTORY_URLS_DELETED.
75  void OnURLsDeleted(const URLsDeletedDetails& details);
76
77  // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED.
78  void OnKeywordSearchTermUpdated(const KeywordSearchTermDetails& details);
79
80  NotificationRegistrar registrar_;
81
82  scoped_ptr<InMemoryDatabase> db_;
83
84  // The profile that this object is attached. May be NULL before
85  // initialization.
86  Profile* profile_;
87
88  // The index used for quick history lookups.
89  scoped_ptr<history::InMemoryURLIndex> index_;
90
91  DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend);
92};
93
94}  // namespace history
95
96#endif  // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_
97