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 CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_ 21 #define CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_ 22 23 #include <string> 24 25 #include "base/basictypes.h" 26 #include "base/gtest_prod_util.h" 27 #include "base/memory/scoped_ptr.h" 28 #include "content/public/browser/notification_observer.h" 29 #include "content/public/browser/notification_registrar.h" 30 31 class Profile; 32 33 namespace base { 34 class FilePath; 35 } 36 37 namespace history { 38 39 class InMemoryDatabase; 40 struct KeywordSearchUpdatedDetails; 41 struct KeywordSearchDeletedDetails; 42 class URLDatabase; 43 class URLRow; 44 struct URLsDeletedDetails; 45 struct URLsModifiedDetails; 46 47 class InMemoryHistoryBackend : public content::NotificationObserver { 48 public: 49 InMemoryHistoryBackend(); 50 virtual ~InMemoryHistoryBackend(); 51 52 // Initializes the backend from the history database pointed to by the 53 // full path in |history_filename|. |db| is used for setting up the 54 // InMemoryDatabase. 55 bool Init(const base::FilePath& history_filename, URLDatabase* db); 56 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); 61 62 // Returns the underlying database associated with this backend. The current 63 // autocomplete code was written fro this, but it should probably be removed 64 // so that it can deal directly with this object, rather than the DB. 65 InMemoryDatabase* db() const { 66 return db_.get(); 67 } 68 69 // Notification callback. 70 virtual void Observe(int type, 71 const content::NotificationSource& source, 72 const content::NotificationDetails& details) OVERRIDE; 73 74 private: 75 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll); 76 77 // Handler for HISTORY_URL_VISITED and HISTORY_URLS_MODIFIED. 78 void OnURLVisitedOrModified(const URLRow& url_row); 79 80 // Handler for HISTORY_URLS_DELETED. 81 void OnURLsDeleted(const URLsDeletedDetails& details); 82 83 // Handler for HISTORY_KEYWORD_SEARCH_TERM_UPDATED. 84 void OnKeywordSearchTermUpdated(const KeywordSearchUpdatedDetails& details); 85 86 // Handler for HISTORY_KEYWORD_SEARCH_TERM_DELETED. 87 void OnKeywordSearchTermDeleted(const KeywordSearchDeletedDetails& details); 88 89 content::NotificationRegistrar registrar_; 90 91 scoped_ptr<InMemoryDatabase> db_; 92 93 // The profile that this object is attached. May be NULL before 94 // initialization. 95 Profile* profile_; 96 97 DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackend); 98 }; 99 100 } // namespace history 101 102 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_HISTORY_BACKEND_H_ 103