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/memory/weak_ptr.h" | |
20 #include "base/string16.h" | 21 #include "base/string16.h" |
21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 22 #include "chrome/browser/autocomplete/autocomplete_match.h" |
22 #include "chrome/browser/autocomplete/history_provider_util.h" | 23 #include "chrome/browser/autocomplete/history_provider_util.h" |
23 #include "chrome/browser/cancelable_request.h" | 24 #include "chrome/browser/cancelable_request.h" |
24 #include "chrome/browser/history/history.h" | 25 #include "chrome/browser/history/history.h" |
25 #include "chrome/browser/history/history_types.h" | 26 #include "chrome/browser/history/history_types.h" |
26 #include "chrome/browser/history/in_memory_url_index_types.h" | 27 #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" | 28 #include "content/public/browser/notification_observer.h" |
29 #include "content/public/browser/notification_registrar.h" | 29 #include "content/public/browser/notification_registrar.h" |
30 #include "sql/connection.h" | 30 #include "sql/connection.h" |
31 | 31 |
32 class HistoryQuickProviderTest; | 32 class HistoryQuickProviderTest; |
33 class Profile; | 33 class Profile; |
34 | 34 |
35 namespace base { | 35 namespace base { |
36 class Time; | 36 class Time; |
37 } | 37 } |
(...skipping 24 matching lines...) Expand all Loading... | |
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 // Called by DoSaveToCacheFile to delete any old cache file at |path| when | |
206 // there is no private data to save. Runs on the FILE thread. | |
207 static void DeleteCacheFile(const FilePath& path); | |
brettw
2012/03/08 22:03:54
I'd probably make this a file static since you don
mrossetti
2012/03/10 00:03:46
Done.
| |
208 | |
209 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion | |
210 // or rebuilding our private data from the history database. |succeeded| | |
211 // will be true if the rebuild was successful. |data| will point to a new | |
212 // instanceof the private data just rebuilt. | |
213 void DoneRebuidingPrivateDataFromHistoryDB(bool succeeded, | |
214 URLIndexPrivateData* data); | |
215 | |
216 // Rebuilds the history index from the history database in |history_db|. | |
217 // Used for unit testing only. | |
218 void RebuildFromHistory(URLDatabase* history_db); | |
219 | |
220 // Determines if the private data was successfully reloaded from the cache | |
221 // file or if the private data must be rebuilt from the history database. | |
222 // |private_data| will be NULL if the cache file load failed. | |
223 void OnCacheLoadDone(URLIndexPrivateData* private_data); | |
224 | |
225 // Callback function that sets the private data from the just-restored-from- | |
226 // file |private_data| if |succeeded| otherwise clears the private data. | |
227 // Notifies any |restore_cache_observer_| of success status. | |
228 void OnCacheRestored(URLIndexPrivateData* private_data, bool succeeded); | |
229 | |
230 // Notifications ------------------------------------------------------------- | |
231 | |
166 // Handles notifications of history changes. | 232 // Handles notifications of history changes. |
167 virtual void Observe(int notification_type, | 233 virtual void Observe(int notification_type, |
168 const content::NotificationSource& source, | 234 const content::NotificationSource& source, |
169 const content::NotificationDetails& details) OVERRIDE; | 235 const content::NotificationDetails& details) OVERRIDE; |
170 | 236 |
171 // Notification handlers. | 237 // Notification handlers. |
172 void OnURLVisited(const URLVisitedDetails* details); | 238 void OnURLVisited(const URLVisitedDetails* details); |
173 void OnURLsModified(const URLsModifiedDetails* details); | 239 void OnURLsModified(const URLsModifiedDetails* details); |
174 void OnURLsDeleted(const URLsDeletedDetails* details); | 240 void OnURLsDeleted(const URLsDeletedDetails* details); |
175 | 241 |
176 // Returns a pointer to our private data. For unit testing only. | 242 // Returns a pointer to our private data. For unit testing only. |
177 URLIndexPrivateData* private_data() { return private_data_.get(); } | 243 URLIndexPrivateData* private_data() { return private_data_.get(); } |
178 | 244 |
179 // The profile, may be null when testing. | 245 // The profile, may be null when testing. |
180 Profile* profile_; | 246 Profile* profile_; |
181 | 247 |
182 // Directory where cache file resides. This is, except when unit testing, | 248 // 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 | 249 // the same directory in which the profile's history database is found. It |
184 // should never be empty. | 250 // should never be empty. |
185 FilePath history_dir_; | 251 FilePath history_dir_; |
186 | 252 |
187 // The index's durable private data. | 253 // The index's durable private data. |
188 scoped_ptr<URLIndexPrivateData> private_data_; | 254 scoped_ptr<URLIndexPrivateData> private_data_; |
189 | 255 |
256 // Observers to notify upon restoral or save of the private data cache. | |
257 RestoreCacheObserver* restore_cache_observer_; | |
258 SaveCacheObserver* save_cache_observer_; | |
259 | |
260 CancelableRequestConsumer cache_reader_consumer_; | |
261 content::NotificationRegistrar registrar_; | |
262 | |
190 // Set to true once the shutdown process has begun. | 263 // Set to true once the shutdown process has begun. |
191 bool shutdown_; | 264 bool shutdown_; |
192 | 265 |
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 | 266 // 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 | 267 // 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 | 268 // temporary safety check to insure that the cache is saved before the |
199 // index has been destructed. | 269 // index has been destructed. |
200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. | 270 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. |
201 // http://crbug.com/83659 | 271 // http://crbug.com/83659 |
202 bool needs_to_be_cached_; | 272 bool needs_to_be_cached_; |
203 | 273 |
204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 274 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
205 }; | 275 }; |
206 | 276 |
207 } // namespace history | 277 } // namespace history |
208 | 278 |
209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 279 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
OLD | NEW |