| OLD | NEW |
| (Empty) |
| 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_IN_MEMORY_URL_INDEX_H_ | |
| 6 #define CHROME_BROWSER_AUTOCOMPLETE_IN_MEMORY_URL_INDEX_H_ | |
| 7 | |
| 8 #include <functional> | |
| 9 #include <map> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/gtest_prod_util.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/memory/weak_ptr.h" | |
| 19 #include "base/strings/string16.h" | |
| 20 #include "base/task/cancelable_task_tracker.h" | |
| 21 #include "base/threading/sequenced_worker_pool.h" | |
| 22 #include "base/threading/thread_checker.h" | |
| 23 #include "components/history/core/browser/history_db_task.h" | |
| 24 #include "components/history/core/browser/history_service_observer.h" | |
| 25 #include "components/history/core/browser/history_types.h" | |
| 26 #include "components/keyed_service/core/keyed_service.h" | |
| 27 #include "components/omnibox/scored_history_match.h" | |
| 28 | |
| 29 class HistoryQuickProviderTest; | |
| 30 | |
| 31 namespace base { | |
| 32 class SequencedTaskRunner; | |
| 33 class Time; | |
| 34 } | |
| 35 | |
| 36 namespace bookmarks { | |
| 37 class BookmarkModel; | |
| 38 } | |
| 39 | |
| 40 namespace in_memory_url_index { | |
| 41 class InMemoryURLIndexCacheItem; | |
| 42 } | |
| 43 | |
| 44 namespace history { | |
| 45 class HistoryDatabase; | |
| 46 class HistoryService; | |
| 47 } | |
| 48 | |
| 49 class URLIndexPrivateData; | |
| 50 | |
| 51 // The URL history source. | |
| 52 // Holds portions of the URL database in memory in an indexed form. Used to | |
| 53 // quickly look up matching URLs for a given query string. Used by | |
| 54 // the HistoryURLProvider for inline autocomplete and to provide URL | |
| 55 // matches to the omnibox. | |
| 56 // | |
| 57 // Note about multi-byte codepoints and the data structures in the | |
| 58 // InMemoryURLIndex class: One will quickly notice that no effort is made to | |
| 59 // insure that multi-byte character boundaries are detected when indexing the | |
| 60 // words and characters in the URL history database except when converting | |
| 61 // URL strings to lowercase. Multi-byte-edness makes no difference when | |
| 62 // indexing or when searching the index as the final filtering of results | |
| 63 // is dependent on the comparison of a string of bytes, not individual | |
| 64 // characters. While the lookup of those bytes during a search in the | |
| 65 // |char_word_map_| could serve up words in which the individual char16 | |
| 66 // occurs as a portion of a composite character the next filtering step | |
| 67 // will eliminate such words except in the case where a single character | |
| 68 // is being searched on and which character occurs as the second char16 of a | |
| 69 // multi-char16 instance. | |
| 70 class InMemoryURLIndex : public KeyedService, | |
| 71 public history::HistoryServiceObserver, | |
| 72 public base::SupportsWeakPtr<InMemoryURLIndex> { | |
| 73 public: | |
| 74 // Defines an abstract class which is notified upon completion of restoring | |
| 75 // the index's private data either by reading from the cache file or by | |
| 76 // rebuilding from the history database. | |
| 77 class RestoreCacheObserver { | |
| 78 public: | |
| 79 virtual ~RestoreCacheObserver(); | |
| 80 | |
| 81 // Callback that lets the observer know that the restore operation has | |
| 82 // completed. |succeeded| indicates if the restore was successful. This is | |
| 83 // called on the UI thread. | |
| 84 virtual void OnCacheRestoreFinished(bool succeeded) = 0; | |
| 85 }; | |
| 86 | |
| 87 // Defines an abstract class which is notified upon completion of saving | |
| 88 // the index's private data to the cache file. | |
| 89 class SaveCacheObserver { | |
| 90 public: | |
| 91 virtual ~SaveCacheObserver(); | |
| 92 | |
| 93 // Callback that lets the observer know that the save succeeded. | |
| 94 // This is called on the UI thread. | |
| 95 virtual void OnCacheSaveFinished(bool succeeded) = 0; | |
| 96 }; | |
| 97 | |
| 98 // |history_service| which may be null during unit testing is used to register | |
| 99 // |as an HistoryServiceObserver. |history_dir| is a path to the directory | |
| 100 // containing the history database within the profile wherein the cache and | |
| 101 // transaction journals will be stored. |languages| gives a list of language | |
| 102 // encodings by which URLs and omnibox searches are broken down into words and | |
| 103 // characters. | |
| 104 InMemoryURLIndex(bookmarks::BookmarkModel* bookmark_model, | |
| 105 history::HistoryService* history_service, | |
| 106 base::SequencedWorkerPool* worker_pool, | |
| 107 const base::FilePath& history_dir, | |
| 108 const std::string& languages, | |
| 109 const std::set<std::string>& client_schemes_to_whitelist); | |
| 110 ~InMemoryURLIndex() override; | |
| 111 | |
| 112 // Opens and prepares the index of historical URL visits. If the index private | |
| 113 // data cannot be restored from its cache file then it is rebuilt from the | |
| 114 // history database. | |
| 115 void Init(); | |
| 116 | |
| 117 // Scans the history index and returns a vector with all scored, matching | |
| 118 // history items. This entry point simply forwards the call on to the | |
| 119 // URLIndexPrivateData class. For a complete description of this function | |
| 120 // refer to that class. If |cursor_position| is base::string16::npos, the | |
| 121 // function doesn't do anything special with the cursor; this is equivalent | |
| 122 // to the cursor being at the end. In total, |max_matches| of items will be | |
| 123 // returned in the |ScoredHistoryMatches| vector. | |
| 124 ScoredHistoryMatches HistoryItemsForTerms(const base::string16& term_string, | |
| 125 size_t cursor_position, | |
| 126 size_t max_matches); | |
| 127 | |
| 128 // Deletes the index entry, if any, for the given |url|. | |
| 129 void DeleteURL(const GURL& url); | |
| 130 | |
| 131 // Sets the optional observers for completion of restoral and saving of the | |
| 132 // index's private data. | |
| 133 void set_restore_cache_observer( | |
| 134 RestoreCacheObserver* restore_cache_observer) { | |
| 135 restore_cache_observer_ = restore_cache_observer; | |
| 136 } | |
| 137 void set_save_cache_observer(SaveCacheObserver* save_cache_observer) { | |
| 138 save_cache_observer_ = save_cache_observer; | |
| 139 } | |
| 140 | |
| 141 // Indicates that the index restoration is complete. | |
| 142 bool restored() const { | |
| 143 return restored_; | |
| 144 } | |
| 145 | |
| 146 private: | |
| 147 friend class ::HistoryQuickProviderTest; | |
| 148 friend class InMemoryURLIndexTest; | |
| 149 friend class InMemoryURLIndexCacheTest; | |
| 150 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, ExpireRow); | |
| 151 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | |
| 152 | |
| 153 // HistoryDBTask used to rebuild our private data from the history database. | |
| 154 class RebuildPrivateDataFromHistoryDBTask : public history::HistoryDBTask { | |
| 155 public: | |
| 156 explicit RebuildPrivateDataFromHistoryDBTask( | |
| 157 InMemoryURLIndex* index, | |
| 158 const std::string& languages, | |
| 159 const std::set<std::string>& scheme_whitelist); | |
| 160 | |
| 161 bool RunOnDBThread(history::HistoryBackend* backend, | |
| 162 history::HistoryDatabase* db) override; | |
| 163 void DoneRunOnMainThread() override; | |
| 164 | |
| 165 private: | |
| 166 ~RebuildPrivateDataFromHistoryDBTask() override; | |
| 167 | |
| 168 InMemoryURLIndex* index_; // Call back to this index at completion. | |
| 169 std::string languages_; // Languages for word-breaking. | |
| 170 std::set<std::string> scheme_whitelist_; // Schemes to be indexed. | |
| 171 bool succeeded_; // Indicates if the rebuild was successful. | |
| 172 scoped_refptr<URLIndexPrivateData> data_; // The rebuilt private data. | |
| 173 | |
| 174 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); | |
| 175 }; | |
| 176 | |
| 177 // Initializes all index data members in preparation for restoring the index | |
| 178 // from the cache or a complete rebuild from the history database. | |
| 179 void ClearPrivateData(); | |
| 180 | |
| 181 // Constructs a file path for the cache file within the same directory where | |
| 182 // the history database is kept and saves that path to |file_path|. Returns | |
| 183 // true if |file_path| can be successfully constructed. (This function | |
| 184 // provided as a hook for unit testing.) | |
| 185 bool GetCacheFilePath(base::FilePath* file_path); | |
| 186 | |
| 187 // Restores the index's private data from the cache file stored in the history | |
| 188 // directory. | |
| 189 void PostRestoreFromCacheFileTask(); | |
| 190 | |
| 191 // Schedules a history task to rebuild our private data from the history | |
| 192 // database. | |
| 193 void ScheduleRebuildFromHistory(); | |
| 194 | |
| 195 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion | |
| 196 // or rebuilding our private data from the history database. |succeeded| | |
| 197 // will be true if the rebuild was successful. |data| will point to a new | |
| 198 // instanceof the private data just rebuilt. | |
| 199 void DoneRebuidingPrivateDataFromHistoryDB( | |
| 200 bool succeeded, | |
| 201 scoped_refptr<URLIndexPrivateData> private_data); | |
| 202 | |
| 203 // Rebuilds the history index from the history database in |history_db|. | |
| 204 // Used for unit testing only. | |
| 205 void RebuildFromHistory(history::HistoryDatabase* history_db); | |
| 206 | |
| 207 // Determines if the private data was successfully reloaded from the cache | |
| 208 // file or if the private data must be rebuilt from the history database. | |
| 209 // |private_data_ptr|'s data will be NULL if the cache file load failed. If | |
| 210 // successful, sets the private data and notifies any | |
| 211 // |restore_cache_observer_|. Otherwise, kicks off a rebuild from the history | |
| 212 // database. | |
| 213 void OnCacheLoadDone(scoped_refptr<URLIndexPrivateData> private_data_ptr); | |
| 214 | |
| 215 // Callback function that sets the private data from the just-restored-from- | |
| 216 // file |private_data|. Notifies any |restore_cache_observer_| that the | |
| 217 // restore has succeeded. | |
| 218 void OnCacheRestored(URLIndexPrivateData* private_data); | |
| 219 | |
| 220 // Posts a task to cache the index private data and write the cache file to | |
| 221 // the history directory. | |
| 222 void PostSaveToCacheFileTask(); | |
| 223 | |
| 224 // Saves private_data_ to the given |path|. Runs on the UI thread. | |
| 225 // Provided for unit testing so that a test cache file can be used. | |
| 226 void DoSaveToCacheFile(const base::FilePath& path); | |
| 227 | |
| 228 // Notifies the observer, if any, of the success of the private data caching. | |
| 229 // |succeeded| is true on a successful save. | |
| 230 void OnCacheSaveDone(bool succeeded); | |
| 231 | |
| 232 // KeyedService: | |
| 233 // Signals that any outstanding initialization should be canceled and | |
| 234 // flushes the cache to disk. | |
| 235 void Shutdown() override; | |
| 236 | |
| 237 // HistoryServiceObserver: | |
| 238 void OnURLVisited(history::HistoryService* history_service, | |
| 239 ui::PageTransition transition, | |
| 240 const history::URLRow& row, | |
| 241 const history::RedirectList& redirects, | |
| 242 base::Time visit_time) override; | |
| 243 void OnURLsModified(history::HistoryService* history_service, | |
| 244 const history::URLRows& changed_urls) override; | |
| 245 void OnURLsDeleted(history::HistoryService* history_service, | |
| 246 bool all_history, | |
| 247 bool expired, | |
| 248 const history::URLRows& deleted_rows, | |
| 249 const std::set<GURL>& favicon_urls) override; | |
| 250 void OnHistoryServiceLoaded( | |
| 251 history::HistoryService* history_service) override; | |
| 252 | |
| 253 // Sets the directory wherein the cache file will be maintained. | |
| 254 // For unit test usage only. | |
| 255 void set_history_dir(const base::FilePath& dir_path) { | |
| 256 history_dir_ = dir_path; | |
| 257 } | |
| 258 | |
| 259 // Returns a pointer to our private data. For unit testing only. | |
| 260 URLIndexPrivateData* private_data() { return private_data_.get(); } | |
| 261 | |
| 262 // Returns a pointer to our private data cancelable request tracker. For | |
| 263 // unit testing only. | |
| 264 base::CancelableTaskTracker* private_data_tracker() { | |
| 265 return &private_data_tracker_; | |
| 266 } | |
| 267 | |
| 268 // Returns the set of whitelisted schemes. For unit testing only. | |
| 269 const std::set<std::string>& scheme_whitelist() { return scheme_whitelist_; } | |
| 270 | |
| 271 // The BookmarkModel; may be null when testing. | |
| 272 bookmarks::BookmarkModel* bookmark_model_; | |
| 273 | |
| 274 // The HistoryService; may be null when testing. | |
| 275 history::HistoryService* history_service_; | |
| 276 | |
| 277 // Directory where cache file resides. This is, except when unit testing, | |
| 278 // the same directory in which the history database is found. It should never | |
| 279 // be empty. | |
| 280 base::FilePath history_dir_; | |
| 281 | |
| 282 // Languages used during the word-breaking process during indexing. | |
| 283 std::string languages_; | |
| 284 | |
| 285 // Only URLs with a whitelisted scheme are indexed. | |
| 286 std::set<std::string> scheme_whitelist_; | |
| 287 | |
| 288 // The index's durable private data. | |
| 289 scoped_refptr<URLIndexPrivateData> private_data_; | |
| 290 | |
| 291 // Observers to notify upon restoral or save of the private data cache. | |
| 292 RestoreCacheObserver* restore_cache_observer_; | |
| 293 SaveCacheObserver* save_cache_observer_; | |
| 294 | |
| 295 // Task runner from the worker pool, used for operations which require disk | |
| 296 // access. | |
| 297 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 298 | |
| 299 base::CancelableTaskTracker private_data_tracker_; | |
| 300 base::CancelableTaskTracker cache_reader_tracker_; | |
| 301 | |
| 302 // Set to true once the shutdown process has begun. | |
| 303 bool shutdown_; | |
| 304 | |
| 305 // Set to true once the index restoration is complete. | |
| 306 bool restored_; | |
| 307 | |
| 308 // Set to true when changes to the index have been made and the index needs | |
| 309 // to be cached. Set to false when the index has been cached. Used as a | |
| 310 // temporary safety check to insure that the cache is saved before the | |
| 311 // index has been destructed. | |
| 312 bool needs_to_be_cached_; | |
| 313 | |
| 314 // This flag is set to true if we want to listen to the | |
| 315 // HistoryServiceLoaded Notification. | |
| 316 bool listen_to_history_service_loaded_; | |
| 317 | |
| 318 base::ThreadChecker thread_checker_; | |
| 319 | |
| 320 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | |
| 321 }; | |
| 322 | |
| 323 #endif // CHROME_BROWSER_AUTOCOMPLETE_IN_MEMORY_URL_INDEX_H_ | |
| OLD | NEW |