| 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" | 19 #include "base/scoped_observer.h" |
| 20 #include "base/strings/string16.h" | 20 #include "base/strings/string16.h" |
| 21 #include "base/task/cancelable_task_tracker.h" | 21 #include "base/task/cancelable_task_tracker.h" |
| 22 #include "chrome/browser/history/history_db_task.h" | 22 #include "chrome/browser/history/history_db_task.h" |
| 23 #include "chrome/browser/history/in_memory_url_index_types.h" | 23 #include "chrome/browser/history/in_memory_url_index_types.h" |
| 24 #include "chrome/browser/history/scored_history_match.h" | 24 #include "chrome/browser/history/scored_history_match.h" |
| 25 #include "components/history/core/browser/history_service_observer.h" | 25 #include "components/history/core/browser/history_service_observer.h" |
| 26 #include "components/history/core/browser/history_types.h" | 26 #include "components/history/core/browser/history_types.h" |
| 27 #include "content/public/browser/notification_observer.h" | |
| 28 #include "content/public/browser/notification_registrar.h" | |
| 29 #include "sql/connection.h" | 27 #include "sql/connection.h" |
| 30 | 28 |
| 31 class HistoryService; | 29 class HistoryService; |
| 32 class HistoryQuickProviderTest; | 30 class HistoryQuickProviderTest; |
| 33 class Profile; | 31 class Profile; |
| 34 | 32 |
| 35 namespace base { | 33 namespace base { |
| 36 class Time; | 34 class Time; |
| 37 } | 35 } |
| 38 | 36 |
| 39 namespace in_memory_url_index { | 37 namespace in_memory_url_index { |
| 40 class InMemoryURLIndexCacheItem; | 38 class InMemoryURLIndexCacheItem; |
| 41 } | 39 } |
| 42 | 40 |
| 43 namespace history { | 41 namespace history { |
| 44 | 42 |
| 45 namespace imui = in_memory_url_index; | 43 namespace imui = in_memory_url_index; |
| 46 | 44 |
| 47 class HistoryClient; | 45 class HistoryClient; |
| 48 class HistoryDatabase; | 46 class HistoryDatabase; |
| 49 class URLIndexPrivateData; | 47 class URLIndexPrivateData; |
| 50 struct URLsDeletedDetails; | |
| 51 struct URLsModifiedDetails; | |
| 52 | 48 |
| 53 // The URL history source. | 49 // The URL history source. |
| 54 // Holds portions of the URL database in memory in an indexed form. Used to | 50 // 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 | 51 // quickly look up matching URLs for a given query string. Used by |
| 56 // the HistoryURLProvider for inline autocomplete and to provide URL | 52 // the HistoryURLProvider for inline autocomplete and to provide URL |
| 57 // matches to the omnibox. | 53 // matches to the omnibox. |
| 58 // | 54 // |
| 59 // Note about multi-byte codepoints and the data structures in the | 55 // Note about multi-byte codepoints and the data structures in the |
| 60 // InMemoryURLIndex class: One will quickly notice that no effort is made to | 56 // InMemoryURLIndex class: One will quickly notice that no effort is made to |
| 61 // insure that multi-byte character boundaries are detected when indexing the | 57 // insure that multi-byte character boundaries are detected when indexing the |
| 62 // words and characters in the URL history database except when converting | 58 // words and characters in the URL history database except when converting |
| 63 // URL strings to lowercase. Multi-byte-edness makes no difference when | 59 // URL strings to lowercase. Multi-byte-edness makes no difference when |
| 64 // indexing or when searching the index as the final filtering of results | 60 // 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 | 61 // 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 | 62 // 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 | 63 // |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 | 64 // 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 | 65 // 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 | 66 // is being searched on and which character occurs as the second char16 of a |
| 71 // multi-char16 instance. | 67 // multi-char16 instance. |
| 72 class InMemoryURLIndex : public HistoryServiceObserver, | 68 class InMemoryURLIndex : public HistoryServiceObserver, |
| 73 public content::NotificationObserver, | |
| 74 public base::SupportsWeakPtr<InMemoryURLIndex> { | 69 public base::SupportsWeakPtr<InMemoryURLIndex> { |
| 75 public: | 70 public: |
| 76 // Defines an abstract class which is notified upon completion of restoring | 71 // Defines an abstract class which is notified upon completion of restoring |
| 77 // the index's private data either by reading from the cache file or by | 72 // the index's private data either by reading from the cache file or by |
| 78 // rebuilding from the history database. | 73 // rebuilding from the history database. |
| 79 class RestoreCacheObserver { | 74 class RestoreCacheObserver { |
| 80 public: | 75 public: |
| 81 virtual ~RestoreCacheObserver(); | 76 virtual ~RestoreCacheObserver(); |
| 82 | 77 |
| 83 // Callback that lets the observer know that the restore operation has | 78 // Callback that lets the observer know that the restore operation has |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 139 |
| 145 // Indicates that the index restoration is complete. | 140 // Indicates that the index restoration is complete. |
| 146 bool restored() const { | 141 bool restored() const { |
| 147 return restored_; | 142 return restored_; |
| 148 } | 143 } |
| 149 | 144 |
| 150 private: | 145 private: |
| 151 friend class ::HistoryQuickProviderTest; | 146 friend class ::HistoryQuickProviderTest; |
| 152 friend class InMemoryURLIndexTest; | 147 friend class InMemoryURLIndexTest; |
| 153 friend class InMemoryURLIndexCacheTest; | 148 friend class InMemoryURLIndexCacheTest; |
| 149 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, ExpireRow); |
| 154 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 150 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
| 155 | 151 |
| 156 // Creating one of me without a history path is not allowed (tests excepted). | 152 // Creating one of me without a history path is not allowed (tests excepted). |
| 157 InMemoryURLIndex(); | 153 InMemoryURLIndex(); |
| 158 | 154 |
| 159 // HistoryDBTask used to rebuild our private data from the history database. | 155 // HistoryDBTask used to rebuild our private data from the history database. |
| 160 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { | 156 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { |
| 161 public: | 157 public: |
| 162 explicit RebuildPrivateDataFromHistoryDBTask( | 158 explicit RebuildPrivateDataFromHistoryDBTask( |
| 163 InMemoryURLIndex* index, | 159 InMemoryURLIndex* index, |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 void PostSaveToCacheFileTask(); | 224 void PostSaveToCacheFileTask(); |
| 229 | 225 |
| 230 // Saves private_data_ to the given |path|. Runs on the UI thread. | 226 // Saves private_data_ to the given |path|. Runs on the UI thread. |
| 231 // Provided for unit testing so that a test cache file can be used. | 227 // Provided for unit testing so that a test cache file can be used. |
| 232 void DoSaveToCacheFile(const base::FilePath& path); | 228 void DoSaveToCacheFile(const base::FilePath& path); |
| 233 | 229 |
| 234 // Notifies the observer, if any, of the success of the private data caching. | 230 // Notifies the observer, if any, of the success of the private data caching. |
| 235 // |succeeded| is true on a successful save. | 231 // |succeeded| is true on a successful save. |
| 236 void OnCacheSaveDone(bool succeeded); | 232 void OnCacheSaveDone(bool succeeded); |
| 237 | 233 |
| 238 // Handles notifications of history changes. | |
| 239 void Observe(int notification_type, | |
| 240 const content::NotificationSource& source, | |
| 241 const content::NotificationDetails& details) override; | |
| 242 | |
| 243 // HistoryServiceObserver: | 234 // HistoryServiceObserver: |
| 244 void OnURLVisited(HistoryService* history_service, | 235 void OnURLVisited(HistoryService* history_service, |
| 245 ui::PageTransition transition, | 236 ui::PageTransition transition, |
| 246 const URLRow& row, | 237 const URLRow& row, |
| 247 const RedirectList& redirects, | 238 const RedirectList& redirects, |
| 248 base::Time visit_time) override; | 239 base::Time visit_time) override; |
| 249 void OnURLsModified(HistoryService* history_service, | 240 void OnURLsModified(HistoryService* history_service, |
| 250 const URLRows& changed_urls) override; | 241 const URLRows& changed_urls) override; |
| 242 void OnURLsDeleted(HistoryService* history_service, |
| 243 bool all_history, |
| 244 bool expired, |
| 245 const URLRows& deleted_rows, |
| 246 const std::set<GURL>& favicon_urls) override; |
| 251 void OnHistoryServiceLoaded(HistoryService* history_service) override; | 247 void OnHistoryServiceLoaded(HistoryService* history_service) override; |
| 252 | 248 |
| 253 // Notification handlers. | |
| 254 void OnURLsDeleted(const URLsDeletedDetails* details); | |
| 255 | |
| 256 // Sets the directory wherein the cache file will be maintained. | 249 // Sets the directory wherein the cache file will be maintained. |
| 257 // For unit test usage only. | 250 // For unit test usage only. |
| 258 void set_history_dir(const base::FilePath& dir_path) { | 251 void set_history_dir(const base::FilePath& dir_path) { |
| 259 history_dir_ = dir_path; | 252 history_dir_ = dir_path; |
| 260 } | 253 } |
| 261 | 254 |
| 262 // Returns a pointer to our private data. For unit testing only. | 255 // Returns a pointer to our private data. For unit testing only. |
| 263 URLIndexPrivateData* private_data() { return private_data_.get(); } | 256 URLIndexPrivateData* private_data() { return private_data_.get(); } |
| 264 | 257 |
| 265 // Returns a pointer to our private data cancelable request tracker. For | 258 // Returns a pointer to our private data cancelable request tracker. For |
| (...skipping 25 matching lines...) Expand all Loading... |
| 291 | 284 |
| 292 // The index's durable private data. | 285 // The index's durable private data. |
| 293 scoped_refptr<URLIndexPrivateData> private_data_; | 286 scoped_refptr<URLIndexPrivateData> private_data_; |
| 294 | 287 |
| 295 // Observers to notify upon restoral or save of the private data cache. | 288 // Observers to notify upon restoral or save of the private data cache. |
| 296 RestoreCacheObserver* restore_cache_observer_; | 289 RestoreCacheObserver* restore_cache_observer_; |
| 297 SaveCacheObserver* save_cache_observer_; | 290 SaveCacheObserver* save_cache_observer_; |
| 298 | 291 |
| 299 base::CancelableTaskTracker private_data_tracker_; | 292 base::CancelableTaskTracker private_data_tracker_; |
| 300 base::CancelableTaskTracker cache_reader_tracker_; | 293 base::CancelableTaskTracker cache_reader_tracker_; |
| 301 content::NotificationRegistrar registrar_; | |
| 302 | 294 |
| 303 // Set to true once the shutdown process has begun. | 295 // Set to true once the shutdown process has begun. |
| 304 bool shutdown_; | 296 bool shutdown_; |
| 305 | 297 |
| 306 // Set to true once the index restoration is complete. | 298 // Set to true once the index restoration is complete. |
| 307 bool restored_; | 299 bool restored_; |
| 308 | 300 |
| 309 // Set to true when changes to the index have been made and the index needs | 301 // Set to true when changes to the index have been made and the index needs |
| 310 // to be cached. Set to false when the index has been cached. Used as a | 302 // to be cached. Set to false when the index has been cached. Used as a |
| 311 // temporary safety check to insure that the cache is saved before the | 303 // temporary safety check to insure that the cache is saved before the |
| 312 // index has been destructed. | 304 // index has been destructed. |
| 313 bool needs_to_be_cached_; | 305 bool needs_to_be_cached_; |
| 314 | 306 |
| 315 ScopedObserver<HistoryService, HistoryServiceObserver> | 307 ScopedObserver<HistoryService, HistoryServiceObserver> |
| 316 history_service_observer_; | 308 history_service_observer_; |
| 317 | 309 |
| 318 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 310 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
| 319 }; | 311 }; |
| 320 | 312 |
| 321 } // namespace history | 313 } // namespace history |
| 322 | 314 |
| 323 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 315 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
| OLD | NEW |