| 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 | 7 |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/scoped_observer.h" | |
| 20 #include "base/strings/string16.h" | 19 #include "base/strings/string16.h" |
| 21 #include "base/task/cancelable_task_tracker.h" | 20 #include "base/task/cancelable_task_tracker.h" |
| 22 #include "chrome/browser/history/scored_history_match.h" | 21 #include "chrome/browser/history/scored_history_match.h" |
| 23 #include "components/history/core/browser/history_db_task.h" | 22 #include "components/history/core/browser/history_db_task.h" |
| 24 #include "components/history/core/browser/history_service_observer.h" | 23 #include "components/history/core/browser/history_service_observer.h" |
| 25 #include "components/history/core/browser/history_types.h" | 24 #include "components/history/core/browser/history_types.h" |
| 26 #include "sql/connection.h" | 25 #include "sql/connection.h" |
| 27 | 26 |
| 28 class HistoryService; | 27 class HistoryService; |
| 29 class HistoryQuickProviderTest; | 28 class HistoryQuickProviderTest; |
| 30 class Profile; | |
| 31 | 29 |
| 32 namespace base { | 30 namespace base { |
| 33 class Time; | 31 class Time; |
| 34 } | 32 } |
| 35 | 33 |
| 36 namespace in_memory_url_index { | 34 namespace in_memory_url_index { |
| 37 class InMemoryURLIndexCacheItem; | 35 class InMemoryURLIndexCacheItem; |
| 38 } | 36 } |
| 39 | 37 |
| 40 namespace history { | 38 namespace history { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // the index's private data to the cache file. | 82 // the index's private data to the cache file. |
| 85 class SaveCacheObserver { | 83 class SaveCacheObserver { |
| 86 public: | 84 public: |
| 87 virtual ~SaveCacheObserver(); | 85 virtual ~SaveCacheObserver(); |
| 88 | 86 |
| 89 // Callback that lets the observer know that the save succeeded. | 87 // Callback that lets the observer know that the save succeeded. |
| 90 // This is called on the UI thread. | 88 // This is called on the UI thread. |
| 91 virtual void OnCacheSaveFinished(bool succeeded) = 0; | 89 virtual void OnCacheSaveFinished(bool succeeded) = 0; |
| 92 }; | 90 }; |
| 93 | 91 |
| 94 // |profile|, which may be NULL during unit testing, is used to register for | 92 // |history_service| which may be null during unit testing is used to register |
| 95 // history changes. |history_dir| is a path to the directory containing the | 93 // |as an HistoryServiceObserver. |history_dir| is a path to the directory |
| 96 // history database within the profile wherein the cache and transaction | 94 // containing the history database within the profile wherein the cache and |
| 97 // journals will be stored. |languages| gives a list of language encodings by | 95 // transaction journals will be stored. |languages| gives a list of language |
| 98 // which URLs and omnibox searches are broken down into words and characters. | 96 // encodings by which URLs and omnibox searches are broken down into words and |
| 99 InMemoryURLIndex(Profile* profile, | 97 // characters. |
| 100 HistoryService* history_service, | 98 InMemoryURLIndex(HistoryService* history_service, |
| 101 const base::FilePath& history_dir, | 99 const base::FilePath& history_dir, |
| 102 const std::string& languages, | 100 const std::string& languages, |
| 103 HistoryClient* client); | 101 HistoryClient* client); |
| 104 ~InMemoryURLIndex() override; | 102 ~InMemoryURLIndex() override; |
| 105 | 103 |
| 106 // Opens and prepares the index of historical URL visits. If the index private | 104 // Opens and prepares the index of historical URL visits. If the index private |
| 107 // data cannot be restored from its cache file then it is rebuilt from the | 105 // data cannot be restored from its cache file then it is rebuilt from the |
| 108 // history database. | 106 // history database. |
| 109 void Init(); | 107 void Init(); |
| 110 | 108 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 141 return restored_; | 139 return restored_; |
| 142 } | 140 } |
| 143 | 141 |
| 144 private: | 142 private: |
| 145 friend class ::HistoryQuickProviderTest; | 143 friend class ::HistoryQuickProviderTest; |
| 146 friend class InMemoryURLIndexTest; | 144 friend class InMemoryURLIndexTest; |
| 147 friend class InMemoryURLIndexCacheTest; | 145 friend class InMemoryURLIndexCacheTest; |
| 148 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, ExpireRow); | 146 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, ExpireRow); |
| 149 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 147 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
| 150 | 148 |
| 151 // Creating one of me without a history path is not allowed (tests excepted). | |
| 152 InMemoryURLIndex(); | |
| 153 | |
| 154 // HistoryDBTask used to rebuild our private data from the history database. | 149 // HistoryDBTask used to rebuild our private data from the history database. |
| 155 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { | 150 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { |
| 156 public: | 151 public: |
| 157 explicit RebuildPrivateDataFromHistoryDBTask( | 152 explicit RebuildPrivateDataFromHistoryDBTask( |
| 158 InMemoryURLIndex* index, | 153 InMemoryURLIndex* index, |
| 159 const std::string& languages, | 154 const std::string& languages, |
| 160 const std::set<std::string>& scheme_whitelist); | 155 const std::set<std::string>& scheme_whitelist); |
| 161 | 156 |
| 162 bool RunOnDBThread(HistoryBackend* backend, | 157 bool RunOnDBThread(HistoryBackend* backend, |
| 163 history::HistoryDatabase* db) override; | 158 history::HistoryDatabase* db) override; |
| 164 void DoneRunOnMainThread() override; | 159 void DoneRunOnMainThread() override; |
| 165 | 160 |
| 166 private: | 161 private: |
| 167 ~RebuildPrivateDataFromHistoryDBTask() override; | 162 ~RebuildPrivateDataFromHistoryDBTask() override; |
| 168 | 163 |
| 169 InMemoryURLIndex* index_; // Call back to this index at completion. | 164 InMemoryURLIndex* index_; // Call back to this index at completion. |
| 170 std::string languages_; // Languages for word-breaking. | 165 std::string languages_; // Languages for word-breaking. |
| 171 std::set<std::string> scheme_whitelist_; // Schemes to be indexed. | 166 std::set<std::string> scheme_whitelist_; // Schemes to be indexed. |
| 172 bool succeeded_; // Indicates if the rebuild was successful. | 167 bool succeeded_; // Indicates if the rebuild was successful. |
| 173 scoped_refptr<URLIndexPrivateData> data_; // The rebuilt private data. | 168 scoped_refptr<URLIndexPrivateData> data_; // The rebuilt private data. |
| 174 | 169 |
| 175 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); | 170 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); |
| 176 }; | 171 }; |
| 177 | 172 |
| 178 // Initializes all index data members in preparation for restoring the index | 173 // Initializes all index data members in preparation for restoring the index |
| 179 // from the cache or a complete rebuild from the history database. | 174 // from the cache or a complete rebuild from the history database. |
| 180 void ClearPrivateData(); | 175 void ClearPrivateData(); |
| 181 | 176 |
| 182 // Constructs a file path for the cache file within the same directory where | 177 // Constructs a file path for the cache file within the same directory where |
| 183 // the history database is kept and saves that path to |file_path|. Returns | 178 // the history database is kept and saves that path to |file_path|. Returns |
| 184 // true if |file_path| can be successfully constructed. (This function | 179 // true if |file_path| can be successfully constructed. (This function |
| 185 // provided as a hook for unit testing.) | 180 // provided as a hook for unit testing.) |
| 186 bool GetCacheFilePath(base::FilePath* file_path); | 181 bool GetCacheFilePath(base::FilePath* file_path); |
| 187 | 182 |
| 188 // Restores the index's private data from the cache file stored in the | 183 // Restores the index's private data from the cache file stored in the history |
| 189 // profile directory. | 184 // directory. |
| 190 void PostRestoreFromCacheFileTask(); | 185 void PostRestoreFromCacheFileTask(); |
| 191 | 186 |
| 192 // Schedules a history task to rebuild our private data from the history | 187 // Schedules a history task to rebuild our private data from the history |
| 193 // database. | 188 // database. |
| 194 void ScheduleRebuildFromHistory(); | 189 void ScheduleRebuildFromHistory(); |
| 195 | 190 |
| 196 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion | 191 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion |
| 197 // or rebuilding our private data from the history database. |succeeded| | 192 // or rebuilding our private data from the history database. |succeeded| |
| 198 // will be true if the rebuild was successful. |data| will point to a new | 193 // will be true if the rebuild was successful. |data| will point to a new |
| 199 // instanceof the private data just rebuilt. | 194 // instanceof the private data just rebuilt. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 212 // |restore_cache_observer_|. Otherwise, kicks off a rebuild from the history | 207 // |restore_cache_observer_|. Otherwise, kicks off a rebuild from the history |
| 213 // database. | 208 // database. |
| 214 void OnCacheLoadDone(scoped_refptr<URLIndexPrivateData> private_data_ptr); | 209 void OnCacheLoadDone(scoped_refptr<URLIndexPrivateData> private_data_ptr); |
| 215 | 210 |
| 216 // Callback function that sets the private data from the just-restored-from- | 211 // Callback function that sets the private data from the just-restored-from- |
| 217 // file |private_data|. Notifies any |restore_cache_observer_| that the | 212 // file |private_data|. Notifies any |restore_cache_observer_| that the |
| 218 // restore has succeeded. | 213 // restore has succeeded. |
| 219 void OnCacheRestored(URLIndexPrivateData* private_data); | 214 void OnCacheRestored(URLIndexPrivateData* private_data); |
| 220 | 215 |
| 221 // Posts a task to cache the index private data and write the cache file to | 216 // Posts a task to cache the index private data and write the cache file to |
| 222 // the profile directory. | 217 // the history directory. |
| 223 void PostSaveToCacheFileTask(); | 218 void PostSaveToCacheFileTask(); |
| 224 | 219 |
| 225 // Saves private_data_ to the given |path|. Runs on the UI thread. | 220 // Saves private_data_ to the given |path|. Runs on the UI thread. |
| 226 // Provided for unit testing so that a test cache file can be used. | 221 // Provided for unit testing so that a test cache file can be used. |
| 227 void DoSaveToCacheFile(const base::FilePath& path); | 222 void DoSaveToCacheFile(const base::FilePath& path); |
| 228 | 223 |
| 229 // Notifies the observer, if any, of the success of the private data caching. | 224 // Notifies the observer, if any, of the success of the private data caching. |
| 230 // |succeeded| is true on a successful save. | 225 // |succeeded| is true on a successful save. |
| 231 void OnCacheSaveDone(bool succeeded); | 226 void OnCacheSaveDone(bool succeeded); |
| 232 | 227 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 256 | 251 |
| 257 // Returns a pointer to our private data cancelable request tracker. For | 252 // Returns a pointer to our private data cancelable request tracker. For |
| 258 // unit testing only. | 253 // unit testing only. |
| 259 base::CancelableTaskTracker* private_data_tracker() { | 254 base::CancelableTaskTracker* private_data_tracker() { |
| 260 return &private_data_tracker_; | 255 return &private_data_tracker_; |
| 261 } | 256 } |
| 262 | 257 |
| 263 // Returns the set of whitelisted schemes. For unit testing only. | 258 // Returns the set of whitelisted schemes. For unit testing only. |
| 264 const std::set<std::string>& scheme_whitelist() { return scheme_whitelist_; } | 259 const std::set<std::string>& scheme_whitelist() { return scheme_whitelist_; } |
| 265 | 260 |
| 266 // The profile, may be null when testing. | 261 // The HistoryService; may be null when testing. |
| 267 Profile* profile_; | |
| 268 HistoryService* history_service_; | 262 HistoryService* history_service_; |
| 269 | 263 |
| 270 // The HistoryClient; may be NULL when testing. | 264 // The HistoryClient; may be null when testing. |
| 271 HistoryClient* history_client_; | 265 HistoryClient* history_client_; |
| 272 | 266 |
| 273 // Directory where cache file resides. This is, except when unit testing, | 267 // Directory where cache file resides. This is, except when unit testing, |
| 274 // the same directory in which the profile's history database is found. It | 268 // the same directory in which the history database is found. It should never |
| 275 // should never be empty. | 269 // be empty. |
| 276 base::FilePath history_dir_; | 270 base::FilePath history_dir_; |
| 277 | 271 |
| 278 // Languages used during the word-breaking process during indexing. | 272 // Languages used during the word-breaking process during indexing. |
| 279 std::string languages_; | 273 std::string languages_; |
| 280 | 274 |
| 281 // Only URLs with a whitelisted scheme are indexed. | 275 // Only URLs with a whitelisted scheme are indexed. |
| 282 std::set<std::string> scheme_whitelist_; | 276 std::set<std::string> scheme_whitelist_; |
| 283 | 277 |
| 284 // The index's durable private data. | 278 // The index's durable private data. |
| 285 scoped_refptr<URLIndexPrivateData> private_data_; | 279 scoped_refptr<URLIndexPrivateData> private_data_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 296 | 290 |
| 297 // Set to true once the index restoration is complete. | 291 // Set to true once the index restoration is complete. |
| 298 bool restored_; | 292 bool restored_; |
| 299 | 293 |
| 300 // Set to true when changes to the index have been made and the index needs | 294 // Set to true when changes to the index have been made and the index needs |
| 301 // to be cached. Set to false when the index has been cached. Used as a | 295 // to be cached. Set to false when the index has been cached. Used as a |
| 302 // temporary safety check to insure that the cache is saved before the | 296 // temporary safety check to insure that the cache is saved before the |
| 303 // index has been destructed. | 297 // index has been destructed. |
| 304 bool needs_to_be_cached_; | 298 bool needs_to_be_cached_; |
| 305 | 299 |
| 306 ScopedObserver<HistoryService, HistoryServiceObserver> | |
| 307 history_service_observer_; | |
| 308 | |
| 309 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 300 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
| 310 }; | 301 }; |
| 311 | 302 |
| 312 } // namespace history | 303 } // namespace history |
| 313 | 304 |
| 314 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 305 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
| OLD | NEW |