Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/browser/history/in_memory_url_index.h

Issue 9030031: Move InMemoryURLIndex Caching Operations to FILE Thread (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Syncing with hopes of pleasing trybot update Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/ref_counted.h"
19 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/weak_ptr.h"
20 #include "base/string16.h" 20 #include "base/string16.h"
21 #include "chrome/browser/autocomplete/autocomplete_match.h" 21 #include "chrome/browser/autocomplete/autocomplete_match.h"
22 #include "chrome/browser/autocomplete/history_provider_util.h" 22 #include "chrome/browser/autocomplete/history_provider_util.h"
23 #include "chrome/browser/cancelable_request.h" 23 #include "chrome/browser/cancelable_request.h"
24 #include "chrome/browser/history/history.h" 24 #include "chrome/browser/history/history.h"
25 #include "chrome/browser/history/history_types.h" 25 #include "chrome/browser/history/history_types.h"
26 #include "chrome/browser/history/in_memory_url_index_types.h" 26 #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" 27 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h" 28 #include "content/public/browser/notification_registrar.h"
30 #include "sql/connection.h" 29 #include "sql/connection.h"
31 30
32 class HistoryQuickProviderTest; 31 class HistoryQuickProviderTest;
33 class Profile; 32 class Profile;
34 33
35 namespace base { 34 namespace base {
36 class Time; 35 class Time;
37 } 36 }
38 37
39 namespace in_memory_url_index { 38 namespace in_memory_url_index {
40 class InMemoryURLIndexCacheItem; 39 class InMemoryURLIndexCacheItem;
41 } 40 }
42 41
43 namespace history { 42 namespace history {
44 43
45 namespace imui = in_memory_url_index; 44 namespace imui = in_memory_url_index;
46 45
47 class HistoryDatabase; 46 class HistoryDatabase;
48 class URLIndexPrivateData; 47 class URLIndexPrivateData;
49 struct URLVisitedDetails; 48 struct URLVisitedDetails;
50 struct URLsModifiedDetails; 49 struct URLsModifiedDetails;
51 struct URLsDeletedDetails; 50 struct URLsDeletedDetails;
52 51
52 // A RefCountedThreadSafe class that manages a bool used for passing around
53 // success when saving the persistent data for the InMemoryURLIndex in a cache.
54 class RefCountedBool : public base::RefCountedThreadSafe<RefCountedBool> {
55 public:
56 explicit RefCountedBool(bool value) : value_(value) {}
57
58 bool value() const { return value_; }
59 void set_value(bool value) { value_ = value; }
60
61 private:
62 friend class base::RefCountedThreadSafe<RefCountedBool>;
63 virtual ~RefCountedBool();
64
65 bool value_;
66
67 DISALLOW_COPY_AND_ASSIGN(RefCountedBool);
68 };
69
53 // The URL history source. 70 // The URL history source.
54 // Holds portions of the URL database in memory in an indexed form. Used to 71 // 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 72 // quickly look up matching URLs for a given query string. Used by
56 // the HistoryURLProvider for inline autocomplete and to provide URL 73 // the HistoryURLProvider for inline autocomplete and to provide URL
57 // matches to the omnibox. 74 // matches to the omnibox.
58 // 75 //
59 // Note about multi-byte codepoints and the data structures in the 76 // Note about multi-byte codepoints and the data structures in the
60 // InMemoryURLIndex class: One will quickly notice that no effort is made to 77 // InMemoryURLIndex class: One will quickly notice that no effort is made to
61 // insure that multi-byte character boundaries are detected when indexing the 78 // insure that multi-byte character boundaries are detected when indexing the
62 // words and characters in the URL history database except when converting 79 // words and characters in the URL history database except when converting
63 // URL strings to lowercase. Multi-byte-edness makes no difference when 80 // URL strings to lowercase. Multi-byte-edness makes no difference when
64 // indexing or when searching the index as the final filtering of results 81 // 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 82 // 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 83 // 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 84 // |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 85 // 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 86 // 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 87 // is being searched on and which character occurs as the second char16 of a
71 // multi-char16 instance. 88 // multi-char16 instance.
72 class InMemoryURLIndex : public content::NotificationObserver { 89 class InMemoryURLIndex : public content::NotificationObserver,
90 public base::SupportsWeakPtr<InMemoryURLIndex> {
73 public: 91 public:
92 // Defines an abstract class which is notified upon completion of restoring
93 // the index's private data either by reading from the cache file or by
94 // rebuilding from the history database.
95 class RestoreCacheObserver {
96 public:
97 virtual ~RestoreCacheObserver();
98
99 // Callback that lets the observer know that the restore operation has
100 // completed. |succeeded| indicates if the restore was successful. This is
101 // called on the UI thread.
102 virtual void OnCacheRestoreFinished(bool succeeded) = 0;
103 };
104
105 // Defines an abstract class which is notified upon completion of saving
106 // the index's private data to the cache file.
107 class SaveCacheObserver {
108 public:
109 virtual ~SaveCacheObserver();
110
111 // Callback that lets the observer know that the save succeeded.
112 // This is called on the UI thread.
113 virtual void OnCacheSaveFinished(bool succeeded) = 0;
114 };
115
74 // |profile|, which may be NULL during unit testing, is used to register for 116 // |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 117 // history changes. |history_dir| is a path to the directory containing the
76 // history database within the profile wherein the cache and transaction 118 // history database within the profile wherein the cache and transaction
77 // journals will be stored. |languages| gives a list of language encodings by 119 // 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. 120 // which URLs and omnibox searches are broken down into words and characters.
79 InMemoryURLIndex(Profile* profile, 121 InMemoryURLIndex(Profile* profile,
80 const FilePath& history_dir, 122 const FilePath& history_dir,
81 const std::string& languages); 123 const std::string& languages);
82 virtual ~InMemoryURLIndex(); 124 virtual ~InMemoryURLIndex();
83 125
84 // Opens and prepares the index of historical URL visits. If the index private 126 // 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 127 // data cannot be restored from its cache file then it is rebuilt from the
86 // history database. 128 // history database.
87 void Init(); 129 void Init();
88 130
89 // Signals that any outstanding initialization should be canceled and 131 // Signals that any outstanding initialization should be canceled and
90 // flushes the cache to disk. 132 // flushes the cache to disk.
91 void ShutDown(); 133 void ShutDown();
92 134
93 // Scans the history index and returns a vector with all scored, matching 135 // 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 136 // history items. This entry point simply forwards the call on to the
95 // URLIndexPrivateData class. For a complete description of this function 137 // URLIndexPrivateData class. For a complete description of this function
96 // refer to that class. 138 // refer to that class.
97 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); 139 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string);
98 140
141 // Sets the optional observers for completion of restoral and saving of the
142 // index's private data.
143 void set_restore_cache_observer(
144 RestoreCacheObserver* restore_cache_observer) {
145 restore_cache_observer_ = restore_cache_observer;
146 }
147 void set_save_cache_observer(SaveCacheObserver* save_cache_observer) {
148 save_cache_observer_ = save_cache_observer;
149 }
150
99 private: 151 private:
100 friend class ::HistoryQuickProviderTest; 152 friend class ::HistoryQuickProviderTest;
101 friend class InMemoryURLIndexTest; 153 friend class InMemoryURLIndexTest;
154 friend class InMemoryURLIndexCacheTest;
102 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); 155 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization);
103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexCacheTest, CacheFilePath);
104 156
105 // Creating one of me without a history path is not allowed (tests excepted). 157 // Creating one of me without a history path is not allowed (tests excepted).
106 InMemoryURLIndex(); 158 InMemoryURLIndex();
107 159
108 // HistoryDBTask used to rebuild our private data from the history database. 160 // HistoryDBTask used to rebuild our private data from the history database.
109 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { 161 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask {
110 public: 162 public:
111 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); 163 explicit RebuildPrivateDataFromHistoryDBTask(
164 InMemoryURLIndex* index,
165 const std::string& languages,
166 const std::set<std::string>& scheme_whitelist);
112 virtual ~RebuildPrivateDataFromHistoryDBTask(); 167 virtual ~RebuildPrivateDataFromHistoryDBTask();
113 168
114 virtual bool RunOnDBThread(HistoryBackend* backend, 169 virtual bool RunOnDBThread(HistoryBackend* backend,
115 history::HistoryDatabase* db) OVERRIDE; 170 history::HistoryDatabase* db) OVERRIDE;
116 virtual void DoneRunOnMainThread() OVERRIDE; 171 virtual void DoneRunOnMainThread() OVERRIDE;
117 172
118 private: 173 private:
119 InMemoryURLIndex* index_; // Call back to this index at completion. 174 InMemoryURLIndex* index_; // Call back to this index at completion.
175 std::string languages_; // Languages for word-breaking.
176 std::set<std::string> scheme_whitelist_; // Schemes to be indexed.
120 bool succeeded_; // Indicates if the rebuild was successful. 177 bool succeeded_; // Indicates if the rebuild was successful.
121 scoped_ptr<URLIndexPrivateData> data_; // The rebuilt private data. 178 scoped_refptr<URLIndexPrivateData> data_; // The rebuilt private data.
122 179
123 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask); 180 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask);
124 }; 181 };
125 182
126 // Initializes all index data members in preparation for restoring the index 183 // Initializes all index data members in preparation for restoring the index
127 // from the cache or a complete rebuild from the history database. 184 // from the cache or a complete rebuild from the history database.
128 void ClearPrivateData(); 185 void ClearPrivateData();
129 186
130 // Constructs a file path for the cache file within the same directory where 187 // 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 188 // the history database is kept and saves that path to |file_path|. Returns
132 // true if |file_path| can be successfully constructed. (This function 189 // true if |file_path| can be successfully constructed. (This function
133 // provided as a hook for unit testing.) 190 // provided as a hook for unit testing.)
134 bool GetCacheFilePath(FilePath* file_path); 191 bool GetCacheFilePath(FilePath* file_path);
135 192
136 // Restores the index's private data from the cache file stored in the 193 // Restores the index's private data from the cache file stored in the
137 // profile directory. 194 // profile directory.
138 void RestoreFromCacheFile(); 195 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 196
144 // Schedules a history task to rebuild our private data from the history 197 // Schedules a history task to rebuild our private data from the history
145 // database. 198 // database.
146 void ScheduleRebuildFromHistory(); 199 void ScheduleRebuildFromHistory();
147 200
148 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion 201 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion
149 // or rebuilding our private data from the history database. |data| points to 202 // 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 203 // a new instance of the private data just rebuilt. This callback is only
151 // called upon a successful restore from the history database. 204 // called upon a successful restore from the history database.
152 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data); 205 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data);
153 206
207 // Posts a task to cache the index private data and write the cache file to
208 // the profile directory.
209 void PostSaveToCacheFileTask();
210
211 // Saves private_data_ to the given |path|. Runs on the UI thread.
212 // Provided for unit testing so that a test cache file can be used.
213 void DoSaveToCacheFile(const FilePath& path);
214
215 // Notifies the observer, if any, of the success of the private data caching.
216 // |succeeded| is true on a successful save.
217 void OnCacheSaveDone(scoped_refptr<RefCountedBool> succeeded);
218
219 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion
220 // or rebuilding our private data from the history database. |succeeded|
221 // will be true if the rebuild was successful. |data| will point to a new
222 // instanceof the private data just rebuilt.
223 void DoneRebuidingPrivateDataFromHistoryDB(bool succeeded,
224 URLIndexPrivateData* data);
225
154 // Rebuilds the history index from the history database in |history_db|. 226 // Rebuilds the history index from the history database in |history_db|.
155 // Used for unit testing only. 227 // Used for unit testing only.
156 void RebuildFromHistory(HistoryDatabase* history_db); 228 void RebuildFromHistory(HistoryDatabase* history_db);
157 229
158 // Caches the index private data and writes the cache file to the profile 230 // Determines if the private data was successfully reloaded from the cache
159 // directory. 231 // file or if the private data must be rebuilt from the history database.
160 void SaveToCacheFile(); 232 // |private_data_ptr|'s data will be NULL if the cache file load failed. If
233 // successful, sets the private data and notifies any
234 // |restore_cache_observer_|. Otherwise, kicks off a rebuild from the history
235 // database.
236 void OnCacheLoadDone(
237 scoped_refptr<URLIndexPrivateData> private_data_ptr);
161 238
162 // Saves private_data_ to the given |path|. Runs on the UI thread. 239 // Callback function that sets the private data from the just-restored-from-
163 // Provided for unit testing so that a test cache file can be used. 240 // file |private_data|. Notifies any |restore_cache_observer_| that the
164 void DoSaveToCacheFile(const FilePath& path); 241 // restore has succeeded.
242 void OnCacheRestored(URLIndexPrivateData* private_data);
165 243
166 // Handles notifications of history changes. 244 // Handles notifications of history changes.
167 virtual void Observe(int notification_type, 245 virtual void Observe(int notification_type,
168 const content::NotificationSource& source, 246 const content::NotificationSource& source,
169 const content::NotificationDetails& details) OVERRIDE; 247 const content::NotificationDetails& details) OVERRIDE;
170 248
171 // Notification handlers. 249 // Notification handlers.
172 void OnURLVisited(const URLVisitedDetails* details); 250 void OnURLVisited(const URLVisitedDetails* details);
173 void OnURLsModified(const URLsModifiedDetails* details); 251 void OnURLsModified(const URLsModifiedDetails* details);
174 void OnURLsDeleted(const URLsDeletedDetails* details); 252 void OnURLsDeleted(const URLsDeletedDetails* details);
175 253
254 // Sets the directory wherein the cache file will be maintained.
255 // For unit test usage only.
256 void set_history_dir(const FilePath& dir_path) { history_dir_ = dir_path; }
257
258 // Initializes the whitelist of URL schemes.
259 static void InitializeSchemeWhitelist(std::set<std::string>* whitelist);
260
176 // Returns a pointer to our private data. For unit testing only. 261 // Returns a pointer to our private data. For unit testing only.
177 URLIndexPrivateData* private_data() { return private_data_.get(); } 262 URLIndexPrivateData* private_data() { return private_data_.get(); }
178 263
264 // Returns the set of whitelisted schemes. For unit testing only.
265 const std::set<std::string>& scheme_whitelist() { return scheme_whitelist_; }
266
179 // The profile, may be null when testing. 267 // The profile, may be null when testing.
180 Profile* profile_; 268 Profile* profile_;
181 269
182 // Directory where cache file resides. This is, except when unit testing, 270 // 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 271 // the same directory in which the profile's history database is found. It
184 // should never be empty. 272 // should never be empty.
185 FilePath history_dir_; 273 FilePath history_dir_;
186 274
275 // Languages used during the word-breaking process during indexing.
276 std::string languages_;
277
278 // Only URLs with a whitelisted scheme are indexed.
279 std::set<std::string> scheme_whitelist_;
280
187 // The index's durable private data. 281 // The index's durable private data.
188 scoped_ptr<URLIndexPrivateData> private_data_; 282 scoped_refptr<URLIndexPrivateData> private_data_;
283
284 // Observers to notify upon restoral or save of the private data cache.
285 RestoreCacheObserver* restore_cache_observer_;
286 SaveCacheObserver* save_cache_observer_;
287
288 CancelableRequestConsumer cache_reader_consumer_;
289 content::NotificationRegistrar registrar_;
189 290
190 // Set to true once the shutdown process has begun. 291 // Set to true once the shutdown process has begun.
191 bool shutdown_; 292 bool shutdown_;
192 293
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 294 // 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 295 // 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 296 // temporary safety check to insure that the cache is saved before the
199 // index has been destructed. 297 // index has been destructed.
200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. 298 // TODO(mrossetti): Eliminate once the transition to SQLite has been done.
201 // http://crbug.com/83659 299 // http://crbug.com/83659
202 bool needs_to_be_cached_; 300 bool needs_to_be_cached_;
203 301
204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); 302 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex);
205 }; 303 };
206 304
207 } // namespace history 305 } // namespace history
208 306
209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ 307 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/history_quick_provider_unittest.cc ('k') | chrome/browser/history/in_memory_url_index.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698