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" | |
19 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/memory/weak_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" | 23 #include "chrome/browser/cancelable_request.h" |
24 #include "chrome/browser/history/history.h" | 24 #include "chrome/browser/history/history.h" |
25 #include "chrome/browser/history/history_types.h" | 25 #include "chrome/browser/history/history_types.h" |
26 #include "chrome/browser/history/in_memory_url_index_types.h" | 26 #include "chrome/browser/history/in_memory_url_index_types.h" |
27 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" | |
28 #include "content/public/browser/notification_observer.h" | 27 #include "content/public/browser/notification_observer.h" |
29 #include "content/public/browser/notification_registrar.h" | 28 #include "content/public/browser/notification_registrar.h" |
30 #include "sql/connection.h" | 29 #include "sql/connection.h" |
31 | 30 |
32 class HistoryQuickProviderTest; | 31 class HistoryQuickProviderTest; |
33 class Profile; | 32 class Profile; |
34 | 33 |
35 namespace base { | 34 namespace base { |
36 class Time; | 35 class Time; |
37 } | 36 } |
38 | 37 |
39 namespace in_memory_url_index { | 38 namespace in_memory_url_index { |
40 class InMemoryURLIndexCacheItem; | 39 class InMemoryURLIndexCacheItem; |
41 } | 40 } |
42 | 41 |
43 namespace history { | 42 namespace history { |
44 | 43 |
45 namespace imui = in_memory_url_index; | 44 namespace imui = in_memory_url_index; |
46 | 45 |
47 class HistoryDatabase; | 46 class HistoryDatabase; |
| 47 class RefCountedURLIndexPrivateDataPtr; |
48 class URLIndexPrivateData; | 48 class URLIndexPrivateData; |
49 struct URLVisitedDetails; | 49 struct URLVisitedDetails; |
50 struct URLsModifiedDetails; | 50 struct URLsModifiedDetails; |
51 struct URLsDeletedDetails; | 51 struct URLsDeletedDetails; |
52 | 52 |
53 // The URL history source. | 53 // The URL history source. |
54 // 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 |
55 // 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 |
56 // the HistoryURLProvider for inline autocomplete and to provide URL | 56 // the HistoryURLProvider for inline autocomplete and to provide URL |
57 // matches to the omnibox. | 57 // matches to the omnibox. |
58 // | 58 // |
59 // Note about multi-byte codepoints and the data structures in the | 59 // Note about multi-byte codepoints and the data structures in the |
60 // 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 |
61 // insure that multi-byte character boundaries are detected when indexing the | 61 // insure that multi-byte character boundaries are detected when indexing the |
62 // words and characters in the URL history database except when converting | 62 // words and characters in the URL history database except when converting |
63 // URL strings to lowercase. Multi-byte-edness makes no difference when | 63 // URL strings to lowercase. Multi-byte-edness makes no difference when |
64 // 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 |
65 // 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 |
66 // 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 |
67 // |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 |
68 // 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 |
69 // 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 |
70 // 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 |
71 // multi-char16 instance. | 71 // multi-char16 instance. |
72 class InMemoryURLIndex : public content::NotificationObserver { | 72 class InMemoryURLIndex : public content::NotificationObserver, |
| 73 public base::SupportsWeakPtr<InMemoryURLIndex> { |
73 public: | 74 public: |
| 75 // Defines an abstract class which is notified upon completion of restoring |
| 76 // the index's private data either by reading from the cache file or by |
| 77 // rebuilding from the history database. |
| 78 class RestoreCacheObserver { |
| 79 public: |
| 80 virtual ~RestoreCacheObserver(); |
| 81 |
| 82 // Callback that lets the observer know that the restore operation has |
| 83 // completed. |succeeded| indicates if the restore was successful. This is |
| 84 // called on the UI thread. |
| 85 virtual void OnCacheRestoreFinished(bool succeeded) = 0; |
| 86 }; |
| 87 |
| 88 // Defines an abstract class which is notified upon completion of saving |
| 89 // the index's private data to the cache file. |
| 90 class SaveCacheObserver { |
| 91 public: |
| 92 virtual ~SaveCacheObserver(); |
| 93 |
| 94 // Callback that lets the observer know that the save succeeded. |
| 95 // This is called on the UI thread. |
| 96 virtual void OnCacheSaveFinished(bool succeeded) = 0; |
| 97 }; |
| 98 |
74 // |profile|, which may be NULL during unit testing, is used to register for | 99 // |profile|, which may be NULL during unit testing, is used to register for |
75 // history changes. |history_dir| is a path to the directory containing the | 100 // history changes. |history_dir| is a path to the directory containing the |
76 // history database within the profile wherein the cache and transaction | 101 // history database within the profile wherein the cache and transaction |
77 // journals will be stored. |languages| gives a list of language encodings by | 102 // 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. | 103 // which URLs and omnibox searches are broken down into words and characters. |
79 InMemoryURLIndex(Profile* profile, | 104 InMemoryURLIndex(Profile* profile, |
80 const FilePath& history_dir, | 105 const FilePath& history_dir, |
81 const std::string& languages); | 106 const std::string& languages); |
82 virtual ~InMemoryURLIndex(); | 107 virtual ~InMemoryURLIndex(); |
83 | 108 |
84 // Opens and prepares the index of historical URL visits. If the index private | 109 // Opens and prepares the index of historical URL visits. If the index private |
85 // data cannot be restored from its cache file then it is rebuilt from the | 110 // data cannot be restored from its cache file then it is rebuilt from the |
86 // history database. | 111 // history database. |
87 void Init(); | 112 void Init(); |
88 | 113 |
89 // Signals that any outstanding initialization should be canceled and | 114 // Signals that any outstanding initialization should be canceled and |
90 // flushes the cache to disk. | 115 // flushes the cache to disk. |
91 void ShutDown(); | 116 void ShutDown(); |
92 | 117 |
93 // Scans the history index and returns a vector with all scored, matching | 118 // Scans the history index and returns a vector with all scored, matching |
94 // history items. This entry point simply forwards the call on to the | 119 // history items. This entry point simply forwards the call on to the |
95 // URLIndexPrivateData class. For a complete description of this function | 120 // URLIndexPrivateData class. For a complete description of this function |
96 // refer to that class. | 121 // refer to that class. |
97 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); | 122 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); |
98 | 123 |
| 124 // Sets the optional observers for completion of restoral and saving of the |
| 125 // index's private data. |
| 126 void set_restore_cache_observer( |
| 127 RestoreCacheObserver* restore_cache_observer) { |
| 128 restore_cache_observer_ = restore_cache_observer; |
| 129 } |
| 130 void set_save_cache_observer(SaveCacheObserver* save_cache_observer) { |
| 131 save_cache_observer_ = save_cache_observer; |
| 132 } |
| 133 |
99 private: | 134 private: |
100 friend class ::HistoryQuickProviderTest; | 135 friend class ::HistoryQuickProviderTest; |
101 friend class InMemoryURLIndexTest; | 136 friend class InMemoryURLIndexTest; |
| 137 friend class InMemoryURLIndexCacheTest; |
102 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 138 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexCacheTest, CacheFilePath); | |
104 | 139 |
105 // Creating one of me without a history path is not allowed (tests excepted). | 140 // Creating one of me without a history path is not allowed (tests excepted). |
106 InMemoryURLIndex(); | 141 InMemoryURLIndex(); |
107 | 142 |
108 // HistoryDBTask used to rebuild our private data from the history database. | 143 // HistoryDBTask used to rebuild our private data from the history database. |
109 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { | 144 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { |
110 public: | 145 public: |
111 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); | 146 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); |
112 virtual ~RebuildPrivateDataFromHistoryDBTask(); | 147 virtual ~RebuildPrivateDataFromHistoryDBTask(); |
113 | 148 |
(...skipping 12 matching lines...) Expand all Loading... |
126 // Initializes all index data members in preparation for restoring the index | 161 // Initializes all index data members in preparation for restoring the index |
127 // from the cache or a complete rebuild from the history database. | 162 // from the cache or a complete rebuild from the history database. |
128 void ClearPrivateData(); | 163 void ClearPrivateData(); |
129 | 164 |
130 // Constructs a file path for the cache file within the same directory where | 165 // Constructs a file path for the cache file within the same directory where |
131 // the history database is kept and saves that path to |file_path|. Returns | 166 // the history database is kept and saves that path to |file_path|. Returns |
132 // true if |file_path| can be successfully constructed. (This function | 167 // true if |file_path| can be successfully constructed. (This function |
133 // provided as a hook for unit testing.) | 168 // provided as a hook for unit testing.) |
134 bool GetCacheFilePath(FilePath* file_path); | 169 bool GetCacheFilePath(FilePath* file_path); |
135 | 170 |
| 171 // Sets the directory wherein the cache file will be maintained. |
| 172 // For unit test usage only. |
| 173 void set_history_dir(const FilePath& dir_path) { history_dir_ = dir_path; } |
| 174 |
136 // Restores the index's private data from the cache file stored in the | 175 // Restores the index's private data from the cache file stored in the |
137 // profile directory. | 176 // profile directory. |
138 void RestoreFromCacheFile(); | 177 void PostRestoreFromCacheFileTask(); |
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 | 178 |
144 // Schedules a history task to rebuild our private data from the history | 179 // Schedules a history task to rebuild our private data from the history |
145 // database. | 180 // database. |
146 void ScheduleRebuildFromHistory(); | 181 void ScheduleRebuildFromHistory(); |
147 | 182 |
148 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion | 183 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion |
149 // or rebuilding our private data from the history database. |data| points to | 184 // 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 | 185 // a new instance of the private data just rebuilt. This callback is only |
151 // called upon a successful restore from the history database. | 186 // called upon a successful restore from the history database. |
152 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data); | 187 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data); |
153 | 188 |
154 // Rebuilds the history index from the history database in |history_db|. | 189 // Rebuilds the history index from the history database in |history_db|. |
155 // Used for unit testing only. | 190 // Used for unit testing only. |
156 void RebuildFromHistory(HistoryDatabase* history_db); | 191 void RebuildFromHistory(HistoryDatabase* history_db); |
157 | 192 |
158 // Caches the index private data and writes the cache file to the profile | 193 // Posts a task to cache the index private data and write the cache file to |
159 // directory. | 194 // the profile directory. |
160 void SaveToCacheFile(); | 195 void PostSaveToCacheFileTask(); |
161 | 196 |
162 // Saves private_data_ to the given |path|. Runs on the UI thread. | 197 // 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. | 198 // Provided for unit testing so that a test cache file can be used. |
164 void DoSaveToCacheFile(const FilePath& path); | 199 void DoSaveToCacheFile(const FilePath& path); |
165 | 200 |
| 201 // Notifies the observer, if any, of the success of the private data caching. |
| 202 // |succeeded| is true on a successful save. |
| 203 void OnCacheSaveDone(scoped_refptr<RefCountedBool> succeeded); |
| 204 |
| 205 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion |
| 206 // or rebuilding our private data from the history database. |succeeded| |
| 207 // will be true if the rebuild was successful. |data| will point to a new |
| 208 // instanceof the private data just rebuilt. |
| 209 void DoneRebuidingPrivateDataFromHistoryDB(bool succeeded, |
| 210 URLIndexPrivateData* data); |
| 211 |
| 212 // Rebuilds the history index from the history database in |history_db|. |
| 213 // Used for unit testing only. |
| 214 void RebuildFromHistory(URLDatabase* history_db); |
| 215 |
| 216 // Determines if the private data was successfully reloaded from the cache |
| 217 // file or if the private data must be rebuilt from the history database. |
| 218 // |private_data_ptr|'s data will be NULL if the cache file load failed. |
| 219 void OnCacheLoadDone( |
| 220 scoped_refptr<RefCountedURLIndexPrivateDataPtr> private_data_ptr); |
| 221 |
| 222 // Callback function that sets the private data from the just-restored-from- |
| 223 // file |private_data| if |succeeded| otherwise clears the private data. |
| 224 // Notifies any |restore_cache_observer_| of success status. |
| 225 void OnCacheRestored(URLIndexPrivateData* private_data, bool succeeded); |
| 226 |
| 227 // Notifications ------------------------------------------------------------- |
| 228 |
166 // Handles notifications of history changes. | 229 // Handles notifications of history changes. |
167 virtual void Observe(int notification_type, | 230 virtual void Observe(int notification_type, |
168 const content::NotificationSource& source, | 231 const content::NotificationSource& source, |
169 const content::NotificationDetails& details) OVERRIDE; | 232 const content::NotificationDetails& details) OVERRIDE; |
170 | 233 |
171 // Notification handlers. | 234 // Notification handlers. |
172 void OnURLVisited(const URLVisitedDetails* details); | 235 void OnURLVisited(const URLVisitedDetails* details); |
173 void OnURLsModified(const URLsModifiedDetails* details); | 236 void OnURLsModified(const URLsModifiedDetails* details); |
174 void OnURLsDeleted(const URLsDeletedDetails* details); | 237 void OnURLsDeleted(const URLsDeletedDetails* details); |
175 | 238 |
176 // Returns a pointer to our private data. For unit testing only. | 239 // Returns a pointer to our private data. For unit testing only. |
177 URLIndexPrivateData* private_data() { return private_data_.get(); } | 240 URLIndexPrivateData* private_data() { return private_data_.get(); } |
178 | 241 |
179 // The profile, may be null when testing. | 242 // The profile, may be null when testing. |
180 Profile* profile_; | 243 Profile* profile_; |
181 | 244 |
182 // Directory where cache file resides. This is, except when unit testing, | 245 // Directory where cache file resides. This is, except when unit testing, |
183 // the same directory in which the profile's history database is found. It | 246 // the same directory in which the profile's history database is found. It |
184 // should never be empty. | 247 // should never be empty. |
185 FilePath history_dir_; | 248 FilePath history_dir_; |
186 | 249 |
187 // The index's durable private data. | 250 // The index's durable private data. |
188 scoped_ptr<URLIndexPrivateData> private_data_; | 251 scoped_ptr<URLIndexPrivateData> private_data_; |
189 | 252 |
| 253 // Observers to notify upon restoral or save of the private data cache. |
| 254 RestoreCacheObserver* restore_cache_observer_; |
| 255 SaveCacheObserver* save_cache_observer_; |
| 256 |
| 257 CancelableRequestConsumer cache_reader_consumer_; |
| 258 content::NotificationRegistrar registrar_; |
| 259 |
190 // Set to true once the shutdown process has begun. | 260 // Set to true once the shutdown process has begun. |
191 bool shutdown_; | 261 bool shutdown_; |
192 | 262 |
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 | 263 // 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 | 264 // 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 | 265 // temporary safety check to insure that the cache is saved before the |
199 // index has been destructed. | 266 // index has been destructed. |
200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. | 267 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. |
201 // http://crbug.com/83659 | 268 // http://crbug.com/83659 |
202 bool needs_to_be_cached_; | 269 bool needs_to_be_cached_; |
203 | 270 |
204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 271 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
205 }; | 272 }; |
206 | 273 |
207 } // namespace history | 274 } // namespace history |
208 | 275 |
209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 276 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
OLD | NEW |