OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
6 #define CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 6 #define CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <functional> | 9 #include <functional> |
10 #include <map> | 10 #include <map> |
11 #include <set> | 11 #include <set> |
12 #include <string> | 12 #include <string> |
13 #include <vector> | 13 #include <vector> |
14 | 14 |
15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
16 #include "base/file_path.h" | 16 #include "base/file_path.h" |
17 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
18 #include "base/memory/linked_ptr.h" | 18 #include "base/memory/linked_ptr.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/string16.h" | 20 #include "base/string16.h" |
21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 21 #include "chrome/browser/autocomplete/autocomplete_match.h" |
22 #include "chrome/browser/autocomplete/history_provider_util.h" | 22 #include "chrome/browser/autocomplete/history_provider_util.h" |
| 23 #include "chrome/browser/cancelable_request.h" |
| 24 #include "chrome/browser/history/history.h" |
23 #include "chrome/browser/history/history_types.h" | 25 #include "chrome/browser/history/history_types.h" |
24 #include "chrome/browser/history/in_memory_url_index_types.h" | 26 #include "chrome/browser/history/in_memory_url_index_types.h" |
25 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" | 27 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" |
26 #include "chrome/browser/history/url_index_private_data.h" | 28 #include "content/public/browser/notification_observer.h" |
| 29 #include "content/public/browser/notification_registrar.h" |
27 #include "sql/connection.h" | 30 #include "sql/connection.h" |
28 | 31 |
| 32 class HistoryQuickProviderTest; |
| 33 class Profile; |
| 34 |
29 namespace base { | 35 namespace base { |
30 class Time; | 36 class Time; |
31 } | 37 } |
32 | 38 |
33 namespace in_memory_url_index { | 39 namespace in_memory_url_index { |
34 class InMemoryURLIndexCacheItem; | 40 class InMemoryURLIndexCacheItem; |
35 } | 41 } |
36 | 42 |
37 namespace history { | 43 namespace history { |
38 | 44 |
39 namespace imui = in_memory_url_index; | 45 namespace imui = in_memory_url_index; |
40 | 46 |
41 class URLDatabase; | 47 class HistoryDatabase; |
| 48 class URLIndexPrivateData; |
| 49 struct URLVisitedDetails; |
| 50 struct URLsModifiedDetails; |
| 51 struct URLsDeletedDetails; |
42 | 52 |
43 // The URL history source. | 53 // The URL history source. |
44 // Holds portions of the URL database in memory in an indexed form. Used to | 54 // Holds portions of the URL database in memory in an indexed form. Used to |
45 // quickly look up matching URLs for a given query string. Used by | 55 // quickly look up matching URLs for a given query string. Used by |
46 // the HistoryURLProvider for inline autocomplete and to provide URL | 56 // the HistoryURLProvider for inline autocomplete and to provide URL |
47 // matches to the omnibox. | 57 // matches to the omnibox. |
48 // | 58 // |
49 // Note about multi-byte codepoints and the data structures in the | 59 // Note about multi-byte codepoints and the data structures in the |
50 // InMemoryURLIndex class: One will quickly notice that no effort is made to | 60 // InMemoryURLIndex class: One will quickly notice that no effort is made to |
51 // insure that multi-byte character boundaries are detected when indexing the | 61 // insure that multi-byte character boundaries are detected when indexing the |
52 // words and characters in the URL history database except when converting | 62 // words and characters in the URL history database except when converting |
53 // URL strings to lowercase. Multi-byte-edness makes no difference when | 63 // URL strings to lowercase. Multi-byte-edness makes no difference when |
54 // indexing or when searching the index as the final filtering of results | 64 // indexing or when searching the index as the final filtering of results |
55 // is dependent on the comparison of a string of bytes, not individual | 65 // is dependent on the comparison of a string of bytes, not individual |
56 // characters. While the lookup of those bytes during a search in the | 66 // characters. While the lookup of those bytes during a search in the |
57 // |char_word_map_| could serve up words in which the individual char16 | 67 // |char_word_map_| could serve up words in which the individual char16 |
58 // occurs as a portion of a composite character the next filtering step | 68 // occurs as a portion of a composite character the next filtering step |
59 // will eliminate such words except in the case where a single character | 69 // will eliminate such words except in the case where a single character |
60 // is being searched on and which character occurs as the second char16 of a | 70 // is being searched on and which character occurs as the second char16 of a |
61 // multi-char16 instance. | 71 // multi-char16 instance. |
62 class InMemoryURLIndex { | 72 class InMemoryURLIndex : public content::NotificationObserver { |
63 public: | 73 public: |
64 // |history_dir| is a path to the directory containing the history database | 74 // |profile|, which may be NULL during unit testing, is used to register for |
65 // within the profile wherein the cache and transaction journals will be | 75 // history changes. |history_dir| is a path to the directory containing the |
66 // stored. | 76 // history database within the profile wherein the cache and transaction |
67 explicit InMemoryURLIndex(const FilePath& history_dir); | 77 // journals will be stored. |languages| gives a list of language encodings by |
| 78 // which URLs and omnibox searches are broken down into words and characters. |
| 79 InMemoryURLIndex(Profile* profile, |
| 80 const FilePath& history_dir, |
| 81 const std::string& languages); |
68 virtual ~InMemoryURLIndex(); | 82 virtual ~InMemoryURLIndex(); |
69 | 83 |
70 // Opens and indexes the URL history database. If the index private data | 84 // Opens and prepares the index of historical URL visits. If the index private |
71 // cannot be restored from its cache file then it is rebuilt from the | 85 // data cannot be restored from its cache file then it is rebuilt from the |
72 // |history_db|. |languages| gives a list of language encodings by which URLs | 86 // history database. |
73 // and omnibox searches are broken down into words and characters. | 87 void Init(); |
74 bool Init(URLDatabase* history_db, const std::string& languages); | |
75 | 88 |
76 // Signals that any outstanding initialization should be canceled and | 89 // Signals that any outstanding initialization should be canceled and |
77 // flushes the cache to disk. | 90 // flushes the cache to disk. |
78 void ShutDown(); | 91 void ShutDown(); |
79 | 92 |
80 // Reloads the history index from |history_db| ignoring any cache file that | |
81 // may be available, clears the cache and saves the cache after reloading. | |
82 bool ReloadFromHistory(history::URLDatabase* history_db); | |
83 | |
84 // Scans the history index and returns a vector with all scored, matching | 93 // Scans the history index and returns a vector with all scored, matching |
85 // history items. This entry point simply forwards the call on to the | 94 // history items. This entry point simply forwards the call on to the |
86 // URLIndexPrivateData class. For a complete description of this function | 95 // URLIndexPrivateData class. For a complete description of this function |
87 // refer to that class. | 96 // refer to that class. |
88 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); | 97 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); |
89 | 98 |
90 // Updates or adds an history item to the index if it meets the minimum | |
91 // 'quick' criteria. | |
92 void UpdateURL(URLID row_id, const URLRow& row); | |
93 | |
94 // Deletes indexing data for an history item. The item may not have actually | |
95 // been indexed (which is the case if it did not previously meet minimum | |
96 // 'quick' criteria). | |
97 void DeleteURL(URLID row_id); | |
98 | |
99 private: | 99 private: |
| 100 friend class ::HistoryQuickProviderTest; |
100 friend class InMemoryURLIndexTest; | 101 friend class InMemoryURLIndexTest; |
101 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath); | |
102 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore); | |
103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, HugeResultSet); | |
104 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TitleSearch); | |
105 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TypedCharacterCaching); | |
106 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, WhitelistedURLs); | |
107 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 102 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
| 103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexCacheTest, CacheFilePath); |
108 | 104 |
109 // Creating one of me without a history path is not allowed (tests excepted). | 105 // Creating one of me without a history path is not allowed (tests excepted). |
110 InMemoryURLIndex(); | 106 InMemoryURLIndex(); |
111 | 107 |
| 108 // HistoryDBTask used to rebuild our private data from the history database. |
| 109 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { |
| 110 public: |
| 111 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); |
| 112 virtual ~RebuildPrivateDataFromHistoryDBTask(); |
| 113 |
| 114 virtual bool RunOnDBThread(HistoryBackend* backend, |
| 115 history::HistoryDatabase* db) OVERRIDE; |
| 116 virtual void DoneRunOnMainThread() OVERRIDE; |
| 117 |
| 118 private: |
| 119 InMemoryURLIndex* index_; // Call back to this index at completion. |
| 120 bool succeeded_; // Indicates if the rebuild was successful. |
| 121 scoped_ptr<URLIndexPrivateData> data_; // The rebuilt private data. |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); |
| 124 }; |
| 125 |
112 // Initializes all index data members in preparation for restoring the index | 126 // Initializes all index data members in preparation for restoring the index |
113 // from the cache or a complete rebuild from the history database. | 127 // from the cache or a complete rebuild from the history database. |
114 void ClearPrivateData(); | 128 void ClearPrivateData(); |
115 | 129 |
116 // Construct a file path for the cache file within the same directory where | 130 // Constructs a file path for the cache file within the same directory where |
117 // the history database is kept and saves that path to |file_path|. Returns | 131 // the history database is kept and saves that path to |file_path|. Returns |
118 // true if |file_path| can be successfully constructed. (This function | 132 // true if |file_path| can be successfully constructed. (This function |
119 // provided as a hook for unit testing.) | 133 // provided as a hook for unit testing.) |
120 bool GetCacheFilePath(FilePath* file_path); | 134 bool GetCacheFilePath(FilePath* file_path); |
121 | 135 |
| 136 // Restores the index's private data from the cache file stored in the |
| 137 // profile directory. |
| 138 void RestoreFromCacheFile(); |
| 139 |
| 140 // Restores private_data_ from the given |path|. Runs on the UI thread. |
| 141 // Provided for unit testing so that a test cache file can be used. |
| 142 void DoRestoreFromCacheFile(const FilePath& path); |
| 143 |
| 144 // Schedules a history task to rebuild our private data from the history |
| 145 // database. |
| 146 void ScheduleRebuildFromHistory(); |
| 147 |
| 148 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion |
| 149 // or rebuilding our private data from the history database. |data| points to |
| 150 // a new instance of the private data just rebuilt. This callback is only |
| 151 // called upon a successful restore from the history database. |
| 152 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data); |
| 153 |
| 154 // Rebuilds the history index from the history database in |history_db|. |
| 155 // Used for unit testing only. |
| 156 void RebuildFromHistory(HistoryDatabase* history_db); |
| 157 |
| 158 // Caches the index private data and writes the cache file to the profile |
| 159 // directory. |
| 160 void SaveToCacheFile(); |
| 161 |
| 162 // Saves private_data_ to the given |path|. Runs on the UI thread. |
| 163 // Provided for unit testing so that a test cache file can be used. |
| 164 void DoSaveToCacheFile(const FilePath& path); |
| 165 |
| 166 // Handles notifications of history changes. |
| 167 virtual void Observe(int notification_type, |
| 168 const content::NotificationSource& source, |
| 169 const content::NotificationDetails& details) OVERRIDE; |
| 170 |
| 171 // Notification handlers. |
| 172 void OnURLVisited(const URLVisitedDetails* details); |
| 173 void OnURLsModified(const URLsModifiedDetails* details); |
| 174 void OnURLsDeleted(const URLsDeletedDetails* details); |
| 175 |
| 176 // Returns a pointer to our private data. For unit testing only. |
| 177 URLIndexPrivateData* private_data() { return private_data_.get(); } |
| 178 |
| 179 // The profile, may be null when testing. |
| 180 Profile* profile_; |
| 181 |
122 // Directory where cache file resides. This is, except when unit testing, | 182 // Directory where cache file resides. This is, except when unit testing, |
123 // the same directory in which the profile's history database is found. It | 183 // the same directory in which the profile's history database is found. It |
124 // should never be empty. | 184 // should never be empty. |
125 FilePath history_dir_; | 185 FilePath history_dir_; |
126 | 186 |
127 // The index's durable private data. | 187 // The index's durable private data. |
128 scoped_ptr<URLIndexPrivateData> private_data_; | 188 scoped_ptr<URLIndexPrivateData> private_data_; |
129 | 189 |
130 // Set to true at shutdown when the cache has been written to disk. Used | 190 // Set to true once the shutdown process has begun. |
131 // as a temporary safety check to insure that the cache is saved before | 191 bool shutdown_; |
132 // the index has been destructed. | 192 |
| 193 CancelableRequestConsumer cache_reader_consumer_; |
| 194 content::NotificationRegistrar registrar_; |
| 195 |
| 196 // Set to true when changes to the index have been made and the index needs |
| 197 // to be cached. Set to false when the index has been cached. Used as a |
| 198 // temporary safety check to insure that the cache is saved before the |
| 199 // index has been destructed. |
133 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. | 200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. |
134 // http://crbug.com/83659 | 201 // http://crbug.com/83659 |
135 bool cached_at_shutdown_; | 202 bool needs_to_be_cached_; |
136 | 203 |
137 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
138 }; | 205 }; |
139 | 206 |
140 } // namespace history | 207 } // namespace history |
141 | 208 |
142 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
OLD | NEW |