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

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

Issue 9316109: Move Ownership of IMUI to HistoryService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 10 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/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"
24 #include "chrome/browser/history/history.h"
23 #include "chrome/browser/history/history_types.h" 25 #include "chrome/browser/history/history_types.h"
24 #include "chrome/browser/history/in_memory_url_index_types.h" 26 #include "chrome/browser/history/in_memory_url_index_types.h"
25 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" 27 #include "chrome/browser/history/in_memory_url_index_cache.pb.h"
26 #include "chrome/browser/history/url_index_private_data.h" 28 #include "content/public/browser/notification_observer.h"
29 #include "content/public/browser/notification_registrar.h"
27 #include "sql/connection.h" 30 #include "sql/connection.h"
28 31
32 class Profile;
33
29 namespace base { 34 namespace base {
30 class Time; 35 class Time;
31 } 36 }
32 37
33 namespace in_memory_url_index { 38 namespace in_memory_url_index {
34 class InMemoryURLIndexCacheItem; 39 class InMemoryURLIndexCacheItem;
35 } 40 }
36 41
37 namespace history { 42 namespace history {
38 43
39 namespace imui = in_memory_url_index; 44 namespace imui = in_memory_url_index;
40 45
41 class URLDatabase; 46 class HistoryDatabase;
47 class URLIndexPrivateData;
48 struct URLVisitedDetails;
49 struct URLsModifiedDetails;
50 struct URLsDeletedDetails;
42 51
43 // The URL history source. 52 // The URL history source.
44 // Holds portions of the URL database in memory in an indexed form. Used to 53 // 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 54 // quickly look up matching URLs for a given query string. Used by
46 // the HistoryURLProvider for inline autocomplete and to provide URL 55 // the HistoryURLProvider for inline autocomplete and to provide URL
47 // matches to the omnibox. 56 // matches to the omnibox.
48 // 57 //
49 // Note about multi-byte codepoints and the data structures in the 58 // Note about multi-byte codepoints and the data structures in the
50 // InMemoryURLIndex class: One will quickly notice that no effort is made to 59 // InMemoryURLIndex class: One will quickly notice that no effort is made to
51 // insure that multi-byte character boundaries are detected when indexing the 60 // insure that multi-byte character boundaries are detected when indexing the
52 // words and characters in the URL history database except when converting 61 // words and characters in the URL history database except when converting
53 // URL strings to lowercase. Multi-byte-edness makes no difference when 62 // URL strings to lowercase. Multi-byte-edness makes no difference when
54 // indexing or when searching the index as the final filtering of results 63 // 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 64 // 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 65 // 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 66 // |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 67 // 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 68 // 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 69 // is being searched on and which character occurs as the second char16 of a
61 // multi-char16 instance. 70 // multi-char16 instance.
62 class InMemoryURLIndex { 71 class InMemoryURLIndex : public content::NotificationObserver {
63 public: 72 public:
64 // |history_dir| is a path to the directory containing the history database 73 // |profile|, which may be NULL during unit testing, is used to register for
65 // within the profile wherein the cache and transaction journals will be 74 // history changes. |history_dir| is a path to the directory containing the
66 // stored. 75 // history database within the profile wherein the cache and transaction
67 explicit InMemoryURLIndex(const FilePath& history_dir); 76 // journals will be stored. |languages| gives a list of language encodings by
77 // which URLs and omnibox searches are broken down into words and characters.
78 InMemoryURLIndex(Profile* profile,
79 const FilePath& history_dir,
80 const std::string& languages);
68 virtual ~InMemoryURLIndex(); 81 virtual ~InMemoryURLIndex();
69 82
70 // Opens and indexes the URL history database. If the index private data 83 // Opens and prepares the index of historical URL visits. If the index private
71 // cannot be restored from its cache file then it is rebuilt from the 84 // data cannot be restored from its cache file then it is rebuilt from the
72 // |history_db|. |languages| gives a list of language encodings by which URLs 85 // history database.
73 // and omnibox searches are broken down into words and characters. 86 void Init();
74 bool Init(URLDatabase* history_db, const std::string& languages);
75 87
76 // Signals that any outstanding initialization should be canceled and 88 // Signals that any outstanding initialization should be canceled and
77 // flushes the cache to disk. 89 // flushes the cache to disk.
78 void ShutDown(); 90 void ShutDown();
79 91
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 92 // 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 93 // history items. This entry point simply forwards the call on to the
86 // URLIndexPrivateData class. For a complete description of this function 94 // URLIndexPrivateData class. For a complete description of this function
87 // refer to that class. 95 // refer to that class.
88 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string); 96 ScoredHistoryMatches HistoryItemsForTerms(const string16& term_string);
89 97
90 // Updates or adds an history item to the index if it meets the minimum 98 // Forwards to the URLIndexPrivateData. Returns true if the index was
91 // 'quick' criteria. 99 // actually changed. For unit testing only.
Peter Kasting 2012/02/04 02:20:31 Nit: If this is the case maybe these function name
mrossetti 2012/02/04 04:30:53 Ack! They weren't used anywhere else except the un
92 void UpdateURL(URLID row_id, const URLRow& row); 100 bool UpdateURL(const URLRow& row);
93 101 bool DeleteURL(const GURL& url);
94 // Deletes indexing data for an history item. The item may not have actually
95 // been indexed (which is the case if it did not previously meet minimum
96 // 'quick' criteria).
97 void DeleteURL(URLID row_id);
98 102
99 private: 103 private:
100 friend class InMemoryURLIndexTest; 104 friend class InMemoryURLIndexTest;
101 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath);
102 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore);
103 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, HugeResultSet);
104 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TitleSearch);
105 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, TypedCharacterCaching);
106 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, WhitelistedURLs);
107 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); 105 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization);
106 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexCacheTest, CacheFilePath);
108 107
109 // Creating one of me without a history path is not allowed (tests excepted). 108 // Creating one of me without a history path is not allowed (tests excepted).
110 InMemoryURLIndex(); 109 InMemoryURLIndex();
111 110
111 // HistoryDBTask used to rebuild our private data from the history database.
112 class RebuildPrivateDataFromHistoryDBTask : public HistoryDBTask {
113 public:
114 explicit RebuildPrivateDataFromHistoryDBTask(InMemoryURLIndex* index);
115 virtual ~RebuildPrivateDataFromHistoryDBTask();
116
117 virtual bool RunOnDBThread(HistoryBackend* backend,
118 history::HistoryDatabase* db) OVERRIDE;
119 virtual void DoneRunOnMainThread() OVERRIDE;
120
121 private:
122 InMemoryURLIndex* index_; // Call back to this index at completion.
123 bool succeeded_; // Indicates if the rebuild was successful.
124 scoped_ptr<URLIndexPrivateData> data_; // The rebuilt private data.
125
126 DISALLOW_COPY_AND_ASSIGN(RebuildPrivateDataFromHistoryDBTask);
127 };
128
112 // Initializes all index data members in preparation for restoring the index 129 // Initializes all index data members in preparation for restoring the index
113 // from the cache or a complete rebuild from the history database. 130 // from the cache or a complete rebuild from the history database.
114 void ClearPrivateData(); 131 void ClearPrivateData();
115 132
116 // Construct a file path for the cache file within the same directory where 133 // Constructs a file path for the cache file within the same directory where
117 // the history database is kept and saves that path to |file_path|. Returns 134 // the history database is kept and saves that path to |file_path|. Returns
118 // true if |file_path| can be successfully constructed. (This function 135 // true if |file_path| can be successfully constructed. (This function
119 // provided as a hook for unit testing.) 136 // provided as a hook for unit testing.)
120 bool GetCacheFilePath(FilePath* file_path); 137 bool GetCacheFilePath(FilePath* file_path);
121 138
139 // Restores the index's private data from the cache file stored in the
140 // profile directory.
141 void RestoreFromCacheFile();
142
143 // Restores private_data_ from the given |path|. Runs on the UI thread.
144 // Provided for unit testing so that a test cache file can be used.
145 void DoRestoreFromCacheFile(const FilePath& path);
146
147 // Schedules a history task to rebuild our private data from the history
148 // database.
149 void ScheduleRebuildFromHistory();
150
151 // Callback used by RebuildPrivateDataFromHistoryDBTask to signal completion
152 // or rebuilding our private data from the history database. |data| points to
153 // a new instance of the private data just rebuilt. This callback is only
154 // called upon a successful restore from the history database.
155 void DoneRebuidingPrivateDataFromHistoryDB(URLIndexPrivateData* data);
156
157 // Rebuilds the history index from the history database in |history_db|.
158 // Used for unit testing only.
159 void RebuildFromHistory(HistoryDatabase* history_db);
160
161 // Caches the index private data and writes the cache file to the profile
162 // directory.
163 void SaveToCacheFile();
164
165 // Saves private_data_ to the given |path|. Runs on the UI thread.
166 // Provided for unit testing so that a test cache file can be used.
167 void DoSaveToCacheFile(const FilePath& path);
168
169 // Handles notifications of history changes.
170 virtual void Observe(int notification_type,
171 const content::NotificationSource& source,
172 const content::NotificationDetails& details) OVERRIDE;
173
174 // Notification handlers.
175 void OnURLVisited(const URLVisitedDetails* details);
176 void OnURLsModified(const URLsModifiedDetails* details);
177 void OnURLsDeleted(const URLsDeletedDetails* details);
178
179 // The profile, may be null when testing.
180 Profile* profile_;
181
122 // Directory where cache file resides. This is, except when unit testing, 182 // 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 183 // the same directory in which the profile's history database is found. It
124 // should never be empty. 184 // should never be empty.
125 FilePath history_dir_; 185 FilePath history_dir_;
126 186
127 // The index's durable private data. 187 // The index's durable private data.
128 scoped_ptr<URLIndexPrivateData> private_data_; 188 scoped_ptr<URLIndexPrivateData> private_data_;
129 189
130 // Set to true at shutdown when the cache has been written to disk. Used 190 // Set to true once the shutdown process has begun.
131 // as a temporary safety check to insure that the cache is saved before 191 bool shutdown_;
132 // the index has been destructed. 192
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
197 // 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
199 // index has been destructed.
133 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. 200 // TODO(mrossetti): Eliminate once the transition to SQLite has been done.
134 // http://crbug.com/83659 201 // http://crbug.com/83659
135 bool cached_at_shutdown_; 202 bool needs_to_be_cached_;
136 203
137 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); 204 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex);
138 }; 205 };
139 206
140 } // namespace history 207 } // namespace history
141 208
142 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ 209 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698