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

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: Pass bool value, not pointer. Sync to clear up Linux fails (hopefully). 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/linked_ptr.h"
19 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
20 #include "base/string16.h" 21 #include "base/string16.h"
21 #include "chrome/browser/autocomplete/autocomplete_match.h" 22 #include "chrome/browser/autocomplete/autocomplete_match.h"
22 #include "chrome/browser/autocomplete/history_provider_util.h" 23 #include "chrome/browser/autocomplete/history_provider_util.h"
23 #include "chrome/browser/cancelable_request.h" 24 #include "chrome/browser/cancelable_request.h"
24 #include "chrome/browser/history/history.h" 25 #include "chrome/browser/history/history.h"
25 #include "chrome/browser/history/history_types.h" 26 #include "chrome/browser/history/history_types.h"
26 #include "chrome/browser/history/in_memory_url_index_types.h" 27 #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" 28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h" 29 #include "content/public/browser/notification_registrar.h"
30 #include "sql/connection.h" 30 #include "sql/connection.h"
31 31
32 class HistoryQuickProviderTest; 32 class HistoryQuickProviderTest;
33 class Profile; 33 class Profile;
34 34
35 namespace base { 35 namespace base {
36 class Time; 36 class Time;
37 } 37 }
(...skipping 24 matching lines...) Expand all
62 // words and characters in the URL history database except when converting 62 // words and characters in the URL history database except when converting
63 // URL strings to lowercase. Multi-byte-edness makes no difference when 63 // URL strings to lowercase. Multi-byte-edness makes no difference when
64 // indexing or when searching the index as the final filtering of results 64 // 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 65 // 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 66 // 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 67 // |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 68 // 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 69 // 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 70 // is being searched on and which character occurs as the second char16 of a
71 // multi-char16 instance. 71 // multi-char16 instance.
72 class InMemoryURLIndex : public content::NotificationObserver { 72 class InMemoryURLIndex : public content::NotificationObserver,
73 public base::SupportsWeakPtr<InMemoryURLIndex> {
73 public: 74 public:
75 // Defines an abstract class which is notified upon completion of restoring
76 // the index's private data either by reading from the cache file or by
77 // rebuilding from the history database.
78 class RestoreCacheObserver {
79 public:
80 virtual ~RestoreCacheObserver();
81
82 // Callback that lets the observer know that the restore operation has
83 // completed. |succeeded| indicates if the restore was successful. This is
84 // called on the UI thread.
85 virtual void OnCacheRestoreFinished(bool succeeded) = 0;
86 };
87
88 // Defines an abstract class which is notified upon completion of saving
89 // the index's private data to the cache file.
90 class SaveCacheObserver {
91 public:
92 virtual ~SaveCacheObserver();
93
94 // Callback that lets the observer know that the save succeeded.
95 // This is called on the UI thread.
96 virtual void OnCacheSaveFinished(bool succeeded) = 0;
97 };
98
74 // |profile|, which may be NULL during unit testing, is used to register for 99 // |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 100 // history changes. |history_dir| is a path to the directory containing the
76 // history database within the profile wherein the cache and transaction 101 // history database within the profile wherein the cache and transaction
77 // journals will be stored. |languages| gives a list of language encodings by 102 // 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. 103 // which URLs and omnibox searches are broken down into words and characters.
79 InMemoryURLIndex(Profile* profile, 104 InMemoryURLIndex(Profile* profile,
80 const FilePath& history_dir, 105 const FilePath& history_dir,
81 const std::string& languages); 106 const std::string& languages);
82 virtual ~InMemoryURLIndex(); 107 virtual ~InMemoryURLIndex();
83 108
84 // Opens and prepares the index of historical URL visits. If the index private 109 // 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 110 // data cannot be restored from its cache file then it is rebuilt from the
86 // history database. 111 // history database.
87 void Init(); 112 void Init();
88 113
89 // Signals that any outstanding initialization should be canceled and 114 // Signals that any outstanding initialization should be canceled and
90 // flushes the cache to disk. 115 // flushes the cache to disk.
91 void ShutDown(); 116 void ShutDown();
92 117
93 // Scans the history index and returns a vector with all scored, matching 118 // 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 119 // history items. This entry point simply forwards the call on to the
95 // URLIndexPrivateData class. For a complete description of this function 120 // URLIndexPrivateData class. For a complete description of this function
96 // refer to that class. 121 // refer to that class.
97 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); 122 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string);
98 123
124 // Sets the optional observers for completion of restoral and saving of the
125 // index's private data.
126 void set_restore_cache_observer(
127 RestoreCacheObserver* restore_cache_observer) {
128 restore_cache_observer_ = restore_cache_observer;
129 }
130 void set_save_cache_observer(SaveCacheObserver* save_cache_observer) {
131 save_cache_observer_ = save_cache_observer;
132 }
133
99 private: 134 private:
100 friend class ::HistoryQuickProviderTest; 135 friend class ::HistoryQuickProviderTest;
101 friend class InMemoryURLIndexTest; 136 friend class InMemoryURLIndexTest;
137 friend class InMemoryURLIndexCacheTest;
102 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); 138 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization);
103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexCacheTest, CacheFilePath);
104 139
105 // Creating one of me without a history path is not allowed (tests excepted). 140 // Creating one of me without a history path is not allowed (tests excepted).
106 InMemoryURLIndex(); 141 InMemoryURLIndex();
107 142
108 // HistoryDBTask used to rebuild our private data from the history database. 143 // HistoryDBTask used to rebuild our private data from the history database.
109 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask { 144 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask {
110 public: 145 public:
111 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index); 146 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index);
112 virtual ~RebuildPrivateDataFromHistoryDBTask(); 147 virtual ~RebuildPrivateDataFromHistoryDBTask();
113 148
(...skipping 12 matching lines...) Expand all
126 // Initializes all index data members in preparation for restoring the index 161 // Initializes all index data members in preparation for restoring the index
127 // from the cache or a complete rebuild from the history database. 162 // from the cache or a complete rebuild from the history database.
128 void ClearPrivateData(); 163 void ClearPrivateData();
129 164
130 // Constructs a file path for the cache file within the same directory where 165 // 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 166 // the history database is kept and saves that path to |file_path|. Returns
132 // true if |file_path| can be successfully constructed. (This function 167 // true if |file_path| can be successfully constructed. (This function
133 // provided as a hook for unit testing.) 168 // provided as a hook for unit testing.)
134 bool GetCacheFilePath(FilePath* file_path); 169 bool GetCacheFilePath(FilePath* file_path);
135 170
171 // Sets the directory wherein the cache file will be maintained.
172 // For unit test usage only.
173 void set_history_dir(const FilePath& dir_path) { history_dir_ = dir_path; }
174
136 // Restores the index's private data from the cache file stored in the 175 // Restores the index's private data from the cache file stored in the
137 // profile directory. 176 // profile directory.
138 void RestoreFromCacheFile(); 177 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 178
144 // Schedules a history task to rebuild our private data from the history 179 // Schedules a history task to rebuild our private data from the history
145 // database. 180 // database.
146 void ScheduleRebuildFromHistory(); 181 void ScheduleRebuildFromHistory();
147 182
148 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion 183 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion
149 // or rebuilding our private data from the history database. |data| points to 184 // 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 185 // a new instance of the private data just rebuilt. This callback is only
151 // called upon a successful restore from the history database. 186 // called upon a successful restore from the history database.
152 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data); 187 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data);
153 188
154 // Rebuilds the history index from the history database in |history_db|. 189 // Rebuilds the history index from the history database in |history_db|.
155 // Used for unit testing only. 190 // Used for unit testing only.
156 void RebuildFromHistory(HistoryDatabase* history_db); 191 void RebuildFromHistory(HistoryDatabase* history_db);
157 192
158 // Caches the index private data and writes the cache file to the profile 193 // Posts a task to cache the index private data and write the cache file to
159 // directory. 194 // the profile directory.
160 void SaveToCacheFile(); 195 void PostSaveToCacheFileTask();
161 196
162 // Saves private_data_ to the given |path|. Runs on the UI thread. 197 // Saves private_data_ to the given |path|. Runs on the UI thread.
163 // Provided for unit testing so that a test cache file can be used. 198 // Provided for unit testing so that a test cache file can be used.
164 void DoSaveToCacheFile(const FilePath& path); 199 void DoSaveToCacheFile(const FilePath& path);
165 200
201 // Notifies the observer, if any, of the success of the private data caching.
202 // |succeeded| is true on a successful save. |copy| is a pointer to the
203 // temporary copy of the private data and is passed into this function only
204 // for the purpose of deletion.
205 void OnCacheSaveDone(bool* succeeded, URLIndexPrivateData* copy);
206
207 // Called by DoSaveToCacheFile to delete any old cache file at |path| when
208 // there is no private data to save. Runs on the FILE thread.
209 static void DeleteCacheFile(const FilePath path);
brettw 2012/03/05 22:13:05 Pass by ref.
mrossetti 2012/03/06 03:49:30 Done.
210
211 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion
212 // or rebuilding our private data from the history database. |succeeded|
213 // will be true if the rebuild was successful. |data| will point to a new
214 // instanceof the private data just rebuilt.
215 void DoneRebuidingPrivateDataFromHistoryDB(bool succeeded,
216 URLIndexPrivateData* data);
217
218 // Rebuilds the history index from the history database in |history_db|.
219 // Used for unit testing only.
220 void RebuildFromHistory(URLDatabase* history_db);
221
222 // Determines if the private data was successfully reloaded from the cache
223 // file or if the private data must be rebuilt from the history database.
224 // |private_data| will be NULL if the cache file load failed.
225 void OnCacheLoadDone(URLIndexPrivateData* private_data);
226
227 // Callback function that sets the private data from the just-restored-from-
228 // file |private_data| if |succeeded| otherwise clears the private data.
229 // Notifies any |restore_cache_observer_| of success status.
230 void OnCacheRestored(URLIndexPrivateData* private_data, bool succeeded);
231
232 // Notifications -------------------------------------------------------------
233
166 // Handles notifications of history changes. 234 // Handles notifications of history changes.
167 virtual void Observe(int notification_type, 235 virtual void Observe(int notification_type,
168 const content::NotificationSource& source, 236 const content::NotificationSource& source,
169 const content::NotificationDetails& details) OVERRIDE; 237 const content::NotificationDetails& details) OVERRIDE;
170 238
171 // Notification handlers. 239 // Notification handlers.
172 void OnURLVisited(const URLVisitedDetails* details); 240 void OnURLVisited(const URLVisitedDetails* details);
173 void OnURLsModified(const URLsModifiedDetails* details); 241 void OnURLsModified(const URLsModifiedDetails* details);
174 void OnURLsDeleted(const URLsDeletedDetails* details); 242 void OnURLsDeleted(const URLsDeletedDetails* details);
175 243
176 // Returns a pointer to our private data. For unit testing only. 244 // Returns a pointer to our private data. For unit testing only.
177 URLIndexPrivateData* private_data() { return private_data_.get(); } 245 URLIndexPrivateData* private_data() { return private_data_.get(); }
178 246
179 // The profile, may be null when testing. 247 // The profile, may be null when testing.
180 Profile* profile_; 248 Profile* profile_;
181 249
182 // Directory where cache file resides. This is, except when unit testing, 250 // 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 251 // the same directory in which the profile's history database is found. It
184 // should never be empty. 252 // should never be empty.
185 FilePath history_dir_; 253 FilePath history_dir_;
186 254
187 // The index's durable private data. 255 // The index's durable private data.
188 scoped_ptr<URLIndexPrivateData> private_data_; 256 scoped_ptr<URLIndexPrivateData> private_data_;
189 257
258 // Observers to notify upon restoral or save of the private data cache.
259 RestoreCacheObserver* restore_cache_observer_;
260 SaveCacheObserver* save_cache_observer_;
261
262 CancelableRequestConsumer cache_reader_consumer_;
263 content::NotificationRegistrar registrar_;
264
190 // Set to true once the shutdown process has begun. 265 // Set to true once the shutdown process has begun.
191 bool shutdown_; 266 bool shutdown_;
192 267
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 268 // 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 269 // 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 270 // temporary safety check to insure that the cache is saved before the
199 // index has been destructed. 271 // index has been destructed.
200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. 272 // TODO(mrossetti): Eliminate once the transition to SQLite has been done.
201 // http://crbug.com/83659 273 // http://crbug.com/83659
202 bool needs_to_be_cached_; 274 bool needs_to_be_cached_;
203 275
204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); 276 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex);
205 }; 277 };
206 278
207 } // namespace history 279 } // namespace history
208 280
209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ 281 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/history/in_memory_url_index.cc » ('j') | chrome/browser/history/in_memory_url_index.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698