Chromium Code Reviews| 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" |
| 20 #include "base/string16.h" | 19 #include "base/string16.h" |
| 21 #include "chrome/browser/autocomplete/autocomplete_match.h" | 20 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 22 #include "chrome/browser/autocomplete/history_provider_util.h" | 21 #include "chrome/browser/autocomplete/history_provider_util.h" |
| 22 #include "chrome/browser/cancelable_request.h" | |
| 23 #include "chrome/browser/history/history.h" | |
| 23 #include "chrome/browser/history/history_types.h" | 24 #include "chrome/browser/history/history_types.h" |
| 24 #include "chrome/browser/history/in_memory_url_index_types.h" | 25 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 25 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" | 26 #include "content/public/browser/notification_observer.h" |
| 26 #include "chrome/browser/history/url_index_private_data.h" | 27 #include "content/public/browser/notification_registrar.h" |
| 27 #include "sql/connection.h" | 28 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 28 | 29 |
| 29 namespace base { | 30 class HistoryQuickProviderTest; |
| 30 class Time; | 31 class Profile; |
| 31 } | |
| 32 | |
| 33 namespace in_memory_url_index { | |
| 34 class InMemoryURLIndexCacheItem; | |
| 35 } | |
| 36 | 32 |
| 37 namespace history { | 33 namespace history { |
| 38 | 34 |
| 39 namespace imui = in_memory_url_index; | 35 class InMemoryURLIndexTest; |
| 40 | |
| 41 class URLDatabase; | 36 class URLDatabase; |
| 37 class URLIndexPrivateData; | |
| 38 struct URLVisitedDetails; | |
| 39 struct URLsModifiedDetails; | |
| 40 struct URLsDeletedDetails; | |
| 42 | 41 |
| 43 // The URL history source. | 42 // The URL history source. |
| 44 // Holds portions of the URL database in memory in an indexed form. Used to | 43 // 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 | 44 // quickly look up matching URLs for a given query string. Used by |
| 46 // the HistoryURLProvider for inline autocomplete and to provide URL | 45 // the HistoryURLProvider for inline autocomplete and to provide URL |
| 47 // matches to the omnibox. | 46 // matches to the omnibox. |
| 48 // | 47 // |
| 49 // Note about multi-byte codepoints and the data structures in the | 48 // Note about multi-byte codepoints and the data structures in the |
| 50 // InMemoryURLIndex class: One will quickly notice that no effort is made to | 49 // InMemoryURLIndex class: One will quickly notice that no effort is made to |
| 51 // insure that multi-byte character boundaries are detected when indexing the | 50 // insure that multi-byte character boundaries are detected when indexing the |
| 52 // words and characters in the URL history database except when converting | 51 // words and characters in the URL history database except when converting |
| 53 // URL strings to lowercase. Multi-byte-edness makes no difference when | 52 // URL strings to lowercase. Multi-byte-edness makes no difference when |
| 54 // indexing or when searching the index as the final filtering of results | 53 // 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 | 54 // 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 | 55 // 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 | 56 // |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 | 57 // 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 | 58 // 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 | 59 // is being searched on and which character occurs as the second char16 of a |
| 61 // multi-char16 instance. | 60 // multi-char16 instance. |
| 62 class InMemoryURLIndex { | 61 class InMemoryURLIndex : public base::RefCountedThreadSafe<InMemoryURLIndex>, |
|
Peter Kasting
2012/01/14 00:12:49
We chatted some in IM about avoiding this by vendi
mrossetti
2012/03/03 05:05:56
Done. Notifications are now employed.
| |
| 62 public content::NotificationObserver { | |
| 63 public: | 63 public: |
| 64 // |history_dir| is a path to the directory containing the history database | 64 // Defines an abstract class which is notified upon completion of restoring |
| 65 // within the profile wherein the cache and transaction journals will be | 65 // the index's private data either by reading from the cache file or by |
| 66 // stored. | 66 // rebuilding from the history database. |
| 67 explicit InMemoryURLIndex(const FilePath& history_dir); | 67 class RestoreCacheObserver { |
| 68 public: | |
| 69 virtual ~RestoreCacheObserver(); | |
| 70 | |
| 71 // Callback that lets the observer know that the restore operation has | |
| 72 // completed. |succeeded| indicates if the restore was successful. This is | |
| 73 // called on the UI thread. | |
| 74 virtual void OnCacheRestoreFinished(bool succeeded) = 0; | |
| 75 }; | |
| 76 | |
| 77 // Defines an abstract class which is notified upon completion of saving | |
| 78 // the index's private data to the cache file. | |
| 79 class SaveCacheObserver { | |
| 80 public: | |
| 81 virtual ~SaveCacheObserver(); | |
| 82 | |
| 83 // Callback that lets the observer know that the save succeeded. | |
| 84 virtual void OnCacheSaveFinished() = 0; // Is called on the IO thread. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Since this is subtle, it might make sense to
mrossetti
2012/03/03 05:05:56
This has been postponed to the next CL when the FI
| |
| 85 | |
| 86 // Callback that lets the observer know that the save failed. | |
| 87 virtual void OnCacheSaveFailed() = 0; // Is called on the UI thread. | |
| 88 }; | |
| 89 | |
| 90 // |profile|, which may be NULL during unit testing, is used to register for | |
| 91 // history changes. |history_dir| is a path to the directory containing the | |
| 92 // history database within the profile wherein the cache and transaction | |
| 93 // journals will be stored. | |
| 94 InMemoryURLIndex(Profile* profile, const FilePath& history_dir); | |
| 68 virtual ~InMemoryURLIndex(); | 95 virtual ~InMemoryURLIndex(); |
| 69 | 96 |
| 70 // Opens and indexes the URL history database. If the index private data | 97 // Opens and initializes the index from a cache file. If the cache file is |
| 71 // cannot be restored from its cache file then it is rebuilt from the | 98 // not present or empty then the index will eventually be rebuilt from the |
| 72 // |history_db|. |languages| gives a list of language encodings by which URLs | 99 // history database associated with the profile. |languages| gives a list of |
| 73 // and omnibox searches are broken down into words and characters. | 100 // language encodings with which the history URLs and omnibox searches are |
| 74 bool Init(URLDatabase* history_db, const std::string& languages); | 101 // interpreted, i.e. when each is broken down into words and each word is |
| 102 // broken down into characters. | |
| 103 void Init(const std::string& languages); | |
| 75 | 104 |
| 76 // Signals that any outstanding initialization should be canceled and | 105 // Signals that any outstanding initialization should be canceled and |
| 77 // flushes the cache to disk. | 106 // flushes the cache to disk. |
| 78 void ShutDown(); | 107 void ShutDown(); |
| 79 | 108 |
| 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 | 109 // 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 | 110 // history items. This entry point simply forwards the call on to the |
| 86 // URLIndexPrivateData class. For a complete description of this function | 111 // URLIndexPrivateData class. For a complete description of this function |
| 87 // refer to that class. | 112 // refer to that class. |
| 88 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); | 113 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); |
| 89 | 114 |
| 90 // Updates or adds an history item to the index if it meets the minimum | 115 // Sets the optional observers for completion of restoral and saving of the |
| 91 // 'quick' criteria. | 116 // index's private data. |
| 92 void UpdateURL(URLID row_id, const URLRow& row); | 117 void set_restore_cache_observer( |
| 118 RestoreCacheObserver* restore_cache_observer) { | |
| 119 restore_cache_observer_ = restore_cache_observer; | |
| 120 } | |
| 121 void set_save_cache_observer(SaveCacheObserver* save_cache_observer) { | |
| 122 save_cache_observer_ = save_cache_observer; | |
| 123 } | |
| 93 | 124 |
| 94 // Deletes indexing data for an history item. The item may not have actually | 125 // Forwards to the URLIndexPrivateData. For unit testing only. |
| 95 // been indexed (which is the case if it did not previously meet minimum | 126 void UpdateURL(const URLRow& row); |
| 96 // 'quick' criteria). | 127 void DeleteURL(const GURL& url); |
| 97 void DeleteURL(URLID row_id); | |
| 98 | 128 |
| 99 private: | 129 private: |
| 100 friend class InMemoryURLIndexTest; | 130 friend class HistoryQuickProviderTest; |
| 131 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, AddNewRows); | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Does it make sense to build some base class f
mrossetti
2012/03/03 05:05:56
Done.
| |
| 101 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath); | 132 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath); |
| 133 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, DeleteRows); | |
| 102 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore); | 134 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore); |
| 103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, HugeResultSet); | 135 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, HugeResultSet); |
| 136 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, NonUniqueTermCharacterSets); | |
| 137 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, ProperStringMatching); | |
| 138 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, Retrieval); | |
| 139 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TitleChange); | |
| 104 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TitleSearch); | 140 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TitleSearch); |
| 105 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TypedCharacterCaching); | 141 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TypedCharacterCaching); |
| 106 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, WhitelistedURLs); | 142 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, WhitelistedURLs); |
| 107 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 143 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
| 108 | 144 |
| 109 // Creating one of me without a history path is not allowed (tests excepted). | 145 // Creating one of me without a history path is not allowed (tests excepted). |
| 110 InMemoryURLIndex(); | 146 InMemoryURLIndex(); |
| 111 | 147 |
| 148 // HistoryDBTask used to rebuild our private data from the history database. | |
| 149 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { | |
| 150 public: | |
| 151 // We take ownership of the |private_data|. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: |private_data| is not a variable defined here
mrossetti
2012/03/03 05:05:56
Done.
| |
| 152 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); | |
| 153 | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: No newline
mrossetti
2012/03/03 05:05:56
Done.
| |
| 154 virtual ~RebuildPrivateDataFromHistoryDBTask(); | |
| 155 | |
| 156 virtual bool RunOnDBThread(HistoryBackend* backend, | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Optional: "// HistoryDBTask"
| |
| 157 history::HistoryDatabase* db) OVERRIDE; | |
| 158 | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Newline unnecessary
mrossetti
2012/03/03 05:05:56
Done.
| |
| 159 virtual void DoneRunOnMainThread() OVERRIDE; | |
| 160 | |
| 161 private: | |
| 162 InMemoryURLIndex* index_; // Call back to this index at completion. | |
| 163 bool succeeded_; // Indicates if the rebuild was successful. | |
| 164 scoped_ptr<URLIndexPrivateData> data_; // The rebuilt private data. | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); | |
| 167 }; | |
| 168 | |
| 112 // Initializes all index data members in preparation for restoring the index | 169 // Initializes all index data members in preparation for restoring the index |
| 113 // from the cache or a complete rebuild from the history database. | 170 // from the cache or a complete rebuild from the history database. |
| 114 void ClearPrivateData(); | 171 void ClearPrivateData(); |
| 115 | 172 |
| 173 // Utility functions supporting RestoreFromCache and SaveToCache. | |
| 174 | |
| 116 // Construct a file path for the cache file within the same directory where | 175 // Construct a file path for the cache file within the same directory where |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Construct -> Constructs (while you're here)
mrossetti
2012/03/03 05:05:56
Done.
| |
| 117 // the history database is kept and saves that path to |file_path|. Returns | 176 // the history database is kept and saves that path to |file_path|. Returns |
| 118 // true if |file_path| can be successfully constructed. (This function | 177 // true if |file_path| can be successfully constructed. (This function |
| 119 // provided as a hook for unit testing.) | 178 // provided as a hook for unit testing.) |
| 120 bool GetCacheFilePath(FilePath* file_path); | 179 bool GetCacheFilePath(FilePath* file_path); |
| 121 | 180 |
| 181 // Restores the index's private data from the cache file stored in the | |
| 182 // profile directory. | |
| 183 void RestoreFromCacheFile(); | |
| 184 | |
| 185 // Restores private_data_ from the given |path|. Runs on the UI thread. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: You might want an explicit comment here or ab
mrossetti
2012/03/03 05:05:56
Done.
| |
| 186 void DoRestoreFromCacheFile(const FilePath& path); | |
| 187 | |
| 188 // Called by DoRestoreFromCacheFile and retrieves the private data from the | |
| 189 // given |path|. If the restore fails then spawn a task to rebuild the | |
| 190 // private data from the history database. | |
| 191 void ReadPrivateDataFromCacheFile(const FilePath path); | |
| 192 | |
| 193 // Caches the index private data and writes the cache file to the profile | |
| 194 // directory. | |
| 195 void SaveToCacheFile(); | |
| 196 | |
| 197 // Saves private_data_ to the given |path|. Runs on the UI thread. | |
| 198 void DoSaveToCacheFile(const FilePath& path); | |
| 199 | |
| 200 // Called by DoSaveToCacheFile and saves |private_data| to the given |path|. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: and saves -> to save
mrossetti
2012/03/03 05:05:56
Postponed to the FILE/pool change.
| |
| 201 // Runs on the FILE thread. | |
| 202 void WritePrivateDataToCacheFile(const FilePath path, | |
| 203 URLIndexPrivateData* private_data); | |
| 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); | |
| 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. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Missing space
mrossetti
2012/03/03 05:05:56
Done.
| |
| 213 void DoneRebuidingPrivateDataFromHistoryDB(bool succeeded, | |
| 214 URLIndexPrivateData* data); | |
| 215 | |
| 216 // Callback function that sets the private data from the just-restored-from- | |
| 217 // file |private_data| if |succeeded| otherwise clears the private data. | |
| 218 // Notifies any |restore_cache_observer_| of success status. | |
| 219 void OnCacheRestored(URLIndexPrivateData* private_data, bool succeeded); | |
| 220 | |
| 221 // Notifications ------------------------------------------------------------- | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Kinda weird to have ---- here but not above (
mrossetti
2012/03/03 05:05:56
Done.
| |
| 222 | |
| 223 // Handle notifications of history loading, and all history changes. | |
| 224 virtual void Observe(int notification_type, | |
| 225 const content::NotificationSource& source, | |
| 226 const content::NotificationDetails& details) OVERRIDE; | |
| 227 | |
| 228 // Notification handlers. | |
| 229 void OnURLVisited(const URLVisitedDetails* details); | |
| 230 void OnURLsModified(const URLsModifiedDetails* details); | |
| 231 void OnURLsDeleted(const URLsDeletedDetails* details); | |
| 232 | |
| 233 // Rebuilds the history index from the history database in |history_db|. | |
| 234 // Used for unit testing only. | |
| 235 void RebuildFromHistory(URLDatabase* history_db); | |
| 236 | |
| 237 // The profile, may be null when testing. | |
| 238 Profile* profile_; | |
| 239 | |
| 122 // Directory where cache file resides. This is, except when unit testing, | 240 // 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 | 241 // the same directory in which the profile's history database is found. It |
| 124 // should never be empty. | 242 // should never be empty. |
| 125 FilePath history_dir_; | 243 FilePath history_dir_; |
| 126 | 244 |
| 127 // The index's durable private data. | 245 // The index's durable private data. |
| 128 scoped_ptr<URLIndexPrivateData> private_data_; | 246 scoped_ptr<URLIndexPrivateData> private_data_; |
| 129 | 247 |
| 130 // Set to true at shutdown when the cache has been written to disk. Used | 248 // Observers to notify upon restoral or save of the private data cache. |
|
Peter Kasting
2012/01/14 00:12:49
Nit: restoral or save -> restoration or saving
mrossetti
2012/03/03 05:05:56
Postponed to the FILE/pool change.
| |
| 131 // as a temporary safety check to insure that the cache is saved before | 249 RestoreCacheObserver* restore_cache_observer_; |
| 132 // the index has been destructed. | 250 SaveCacheObserver* save_cache_observer_; |
| 251 | |
| 252 CancelableRequestConsumer cache_reader_consumer_; | |
| 253 | |
| 254 // Remember to unregister from notifications on destruction. | |
|
Peter Kasting
2012/01/14 00:12:49
Nit: Comment unnecessary
mrossetti
2012/03/03 05:05:56
Done.
| |
| 255 content::NotificationRegistrar registrar_; | |
| 256 | |
| 257 // Set to true once the shutdown process has begun. | |
| 258 bool shutdown_; | |
| 259 | |
| 260 // Set to true when changes to the index have been made and the index needs | |
| 261 // to be cached. Set to false when the index has been cached. Used as a | |
| 262 // temporary safety check to insure that the cache is saved before the | |
| 263 // index has been destructed. | |
| 133 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. | 264 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. |
| 134 // http://crbug.com/83659 | 265 // http://crbug.com/83659 |
| 135 bool cached_at_shutdown_; | 266 bool needs_to_be_cached_; |
| 136 | 267 |
| 137 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 268 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
| 138 }; | 269 }; |
| 139 | 270 |
| 140 } // namespace history | 271 } // namespace history |
| 141 | 272 |
| 142 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 273 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
| OLD | NEW |