OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/history/history_types.h" | 23 #include "chrome/browser/history/history_types.h" |
24 #include "chrome/browser/history/in_memory_url_index_types.h" | 24 #include "chrome/browser/history/in_memory_url_index_types.h" |
25 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" | 25 #include "chrome/browser/history/in_memory_url_index_cache.pb.h" |
26 #include "content/public/browser/notification_observer.h" | |
27 #include "content/public/browser/notification_registrar.h" | |
28 #include "sql/connection.h" | 26 #include "sql/connection.h" |
29 #include "testing/gtest/include/gtest/gtest_prod.h" | 27 #include "testing/gtest/include/gtest/gtest_prod.h" |
30 | 28 |
31 class Profile; | 29 class Profile; |
32 | 30 |
| 31 namespace base { |
| 32 class Time; |
| 33 } |
| 34 |
33 namespace in_memory_url_index { | 35 namespace in_memory_url_index { |
34 class InMemoryURLIndexCacheItem; | 36 class InMemoryURLIndexCacheItem; |
35 } | 37 } |
36 | 38 |
37 namespace history { | 39 namespace history { |
38 | 40 |
39 namespace imui = in_memory_url_index; | 41 namespace imui = in_memory_url_index; |
40 | 42 |
41 class URLDatabase; | 43 class URLDatabase; |
42 struct URLsDeletedDetails; | |
43 struct URLsModifiedDetails; | |
44 struct URLVisitedDetails; | |
45 | 44 |
46 // The URL history source. | 45 // The URL history source. |
47 // Holds portions of the URL database in memory in an indexed form. Used to | 46 // Holds portions of the URL database in memory in an indexed form. Used to |
48 // quickly look up matching URLs for a given query string. Used by | 47 // quickly look up matching URLs for a given query string. Used by |
49 // the HistoryURLProvider for inline autocomplete and to provide URL | 48 // the HistoryURLProvider for inline autocomplete and to provide URL |
50 // matches to the omnibox. | 49 // matches to the omnibox. |
51 // | 50 // |
52 // Note about multi-byte codepoints and the data structures in the | 51 // Note about multi-byte codepoints and the data structures in the |
53 // InMemoryURLIndex class: One will quickly notice that no effort is made to | 52 // InMemoryURLIndex class: One will quickly notice that no effort is made to |
54 // insure that multi-byte character boundaries are detected when indexing the | 53 // insure that multi-byte character boundaries are detected when indexing the |
55 // words and characters in the URL history database except when converting | 54 // words and characters in the URL history database except when converting |
56 // URL strings to lowercase. Multi-byte-edness makes no difference when | 55 // URL strings to lowercase. Multi-byte-edness makes no difference when |
57 // indexing or when searching the index as the final filtering of results | 56 // indexing or when searching the index as the final filtering of results |
58 // is dependent on the comparison of a string of bytes, not individual | 57 // is dependent on the comparison of a string of bytes, not individual |
59 // characters. While the lookup of those bytes during a search in the | 58 // characters. While the lookup of those bytes during a search in the |
60 // |char_word_map_| could serve up words in which the individual char16 | 59 // |char_word_map_| could serve up words in which the individual char16 |
61 // occurs as a portion of a composite character the next filtering step | 60 // occurs as a portion of a composite character the next filtering step |
62 // will eliminate such words except in the case where a single character | 61 // will eliminate such words except in the case where a single character |
63 // is being searched on and which character occurs as the second char16 of a | 62 // is being searched on and which character occurs as the second char16 of a |
64 // multi-char16 instance. | 63 // multi-char16 instance. |
65 class InMemoryURLIndex : public content::NotificationObserver { | 64 class InMemoryURLIndex { |
66 public: | 65 public: |
67 // |history_dir| is a path to the directory containing the history database | 66 // |history_dir| is a path to the directory containing the history database |
68 // within the profile wherein the cache and transaction journals will be | 67 // within the profile wherein the cache and transaction journals will be |
69 // stored. | 68 // stored. |
70 explicit InMemoryURLIndex(Profile* profile, | 69 explicit InMemoryURLIndex(const FilePath& history_dir); |
71 const FilePath& history_dir); | |
72 virtual ~InMemoryURLIndex(); | 70 virtual ~InMemoryURLIndex(); |
73 | 71 |
74 // Opens and indexes the URL history database. | 72 // Opens and indexes the URL history database. |
75 // |languages| gives a list of language encodings with which the history | 73 // |languages| gives a list of language encodings with which the history |
76 // URLs and omnibox searches are interpreted, i.e. when each is broken | 74 // URLs and omnibox searches are interpreted, i.e. when each is broken |
77 // down into words and each word is broken down into characters. | 75 // down into words and each word is broken down into characters. |
78 bool Init(URLDatabase* history_db, const std::string& languages); | 76 bool Init(URLDatabase* history_db, const std::string& languages); |
79 | 77 |
80 // Reloads the history index. Attempts to reload from the cache unless | 78 // Reloads the history index. Attempts to reload from the cache unless |
81 // |clear_cache| is true. If the cache is unavailable then reload the | 79 // |clear_cache| is true. If the cache is unavailable then reload the |
(...skipping 20 matching lines...) Expand all Loading... |
102 // from |terms| may contain punctuation but should not contain spaces. | 100 // from |terms| may contain punctuation but should not contain spaces. |
103 // A search request which results in more than |kItemsToScoreLimit| total | 101 // A search request which results in more than |kItemsToScoreLimit| total |
104 // candidate items returns no matches (though the results set will be | 102 // candidate items returns no matches (though the results set will be |
105 // retained and used for subsequent calls to this function) as the scoring | 103 // retained and used for subsequent calls to this function) as the scoring |
106 // of such a large number of candidates may cause perceptible typing response | 104 // of such a large number of candidates may cause perceptible typing response |
107 // delays in the omnibox. This is likely to occur for short omnibox terms | 105 // delays in the omnibox. This is likely to occur for short omnibox terms |
108 // such as 'h' and 'w' which will be found in nearly all history candidates. | 106 // such as 'h' and 'w' which will be found in nearly all history candidates. |
109 ScoredHistoryMatches HistoryItemsForTerms(const String16Vector& terms); | 107 ScoredHistoryMatches HistoryItemsForTerms(const String16Vector& terms); |
110 | 108 |
111 // Updates or adds an history item to the index if it meets the minimum | 109 // Updates or adds an history item to the index if it meets the minimum |
112 // selection criteria. | 110 // 'quick' criteria. |
113 void UpdateURL(const URLRow& row); | 111 void UpdateURL(URLID row_id, const URLRow& row); |
114 | 112 |
115 // Deletes indexing data for an history item. The item may not have actually | 113 // Deletes indexing data for an history item. The item may not have actually |
116 // been indexed (which is the case if it did not previously meet minimum | 114 // been indexed (which is the case if it did not previously meet minimum |
117 // 'quick' criteria). | 115 // 'quick' criteria). |
118 void DeleteURL(const URLRow& row); | 116 void DeleteURL(URLID row_id); |
119 | |
120 // Notification callback. | |
121 virtual void Observe(int type, | |
122 const content::NotificationSource& source, | |
123 const content::NotificationDetails& details); | |
124 | 117 |
125 private: | 118 private: |
126 friend class AddHistoryMatch; | 119 friend class AddHistoryMatch; |
127 friend class InMemoryURLIndexTest; | 120 friend class InMemoryURLIndexTest; |
128 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); | 121 FRIEND_TEST_ALL_PREFIXES(LimitedInMemoryURLIndexTest, Initialization); |
129 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath); | 122 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheFilePath); |
130 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore); | 123 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, CacheSaveRestore); |
131 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, Char16Utilities); | 124 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, Char16Utilities); |
132 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, NonUniqueTermCharacterSets); | 125 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, NonUniqueTermCharacterSets); |
133 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, Scoring); | 126 FRIEND_TEST_ALL_PREFIXES(InMemoryURLIndexTest, Scoring); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 246 |
254 // Calculates a component score based on position, ordering and total | 247 // Calculates a component score based on position, ordering and total |
255 // substring match size using metrics recorded in |matches|. |max_length| | 248 // substring match size using metrics recorded in |matches|. |max_length| |
256 // is the length of the string against which the terms are being searched. | 249 // is the length of the string against which the terms are being searched. |
257 static int ScoreComponentForMatches(const TermMatches& matches, | 250 static int ScoreComponentForMatches(const TermMatches& matches, |
258 size_t max_length); | 251 size_t max_length); |
259 | 252 |
260 // Determines if |gurl| has a whitelisted scheme and returns true if so. | 253 // Determines if |gurl| has a whitelisted scheme and returns true if so. |
261 bool URLSchemeIsWhitelisted(const GURL& gurl) const; | 254 bool URLSchemeIsWhitelisted(const GURL& gurl) const; |
262 | 255 |
263 // Notification handlers. | |
264 void OnURLVisited(const URLVisitedDetails* details); | |
265 void OnURLsModified(const URLsModifiedDetails* details); | |
266 void OnURLsDeleted(const URLsDeletedDetails* details); | |
267 | |
268 // Utility functions supporting RestoreFromCache and SaveToCache. | 256 // Utility functions supporting RestoreFromCache and SaveToCache. |
269 | 257 |
270 // Construct a file path for the cache file within the same directory where | 258 // Construct a file path for the cache file within the same directory where |
271 // the history database is kept and saves that path to |file_path|. Returns | 259 // the history database is kept and saves that path to |file_path|. Returns |
272 // true if |file_path| can be successfully constructed. (This function | 260 // true if |file_path| can be successfully constructed. (This function |
273 // provided as a hook for unit testing.) | 261 // provided as a hook for unit testing.) |
274 bool GetCacheFilePath(FilePath* file_path); | 262 bool GetCacheFilePath(FilePath* file_path); |
275 | 263 |
276 // Encode a data structure into the protobuf |cache|. | 264 // Encode a data structure into the protobuf |cache|. |
277 void SavePrivateData(imui::InMemoryURLIndexCacheItem* cache) const; | 265 void SavePrivateData(imui::InMemoryURLIndexCacheItem* cache) const; |
278 void SaveWordList(imui::InMemoryURLIndexCacheItem* cache) const; | 266 void SaveWordList(imui::InMemoryURLIndexCacheItem* cache) const; |
279 void SaveWordMap(imui::InMemoryURLIndexCacheItem* cache) const; | 267 void SaveWordMap(imui::InMemoryURLIndexCacheItem* cache) const; |
280 void SaveCharWordMap(imui::InMemoryURLIndexCacheItem* cache) const; | 268 void SaveCharWordMap(imui::InMemoryURLIndexCacheItem* cache) const; |
281 void SaveWordIDHistoryMap(imui::InMemoryURLIndexCacheItem* cache) const; | 269 void SaveWordIDHistoryMap(imui::InMemoryURLIndexCacheItem* cache) const; |
282 void SaveHistoryInfoMap(imui::InMemoryURLIndexCacheItem* cache) const; | 270 void SaveHistoryInfoMap(imui::InMemoryURLIndexCacheItem* cache) const; |
283 | 271 |
284 // Decode a data structure from the protobuf |cache|. Return false if there | 272 // Decode a data structure from the protobuf |cache|. Return false if there |
285 // is any kind of failure. | 273 // is any kind of failure. |
286 bool RestorePrivateData(const imui::InMemoryURLIndexCacheItem& cache); | 274 bool RestorePrivateData(const imui::InMemoryURLIndexCacheItem& cache); |
287 bool RestoreWordList(const imui::InMemoryURLIndexCacheItem& cache); | 275 bool RestoreWordList(const imui::InMemoryURLIndexCacheItem& cache); |
288 bool RestoreWordMap(const imui::InMemoryURLIndexCacheItem& cache); | 276 bool RestoreWordMap(const imui::InMemoryURLIndexCacheItem& cache); |
289 bool RestoreCharWordMap(const imui::InMemoryURLIndexCacheItem& cache); | 277 bool RestoreCharWordMap(const imui::InMemoryURLIndexCacheItem& cache); |
290 bool RestoreWordIDHistoryMap(const imui::InMemoryURLIndexCacheItem& cache); | 278 bool RestoreWordIDHistoryMap(const imui::InMemoryURLIndexCacheItem& cache); |
291 bool RestoreHistoryInfoMap(const imui::InMemoryURLIndexCacheItem& cache); | 279 bool RestoreHistoryInfoMap(const imui::InMemoryURLIndexCacheItem& cache); |
292 | 280 |
293 content::NotificationRegistrar registrar_; | |
294 | |
295 // Directory where cache file resides. This is, except when unit testing, | 281 // Directory where cache file resides. This is, except when unit testing, |
296 // the same directory in which the profile's history database is found. It | 282 // the same directory in which the profile's history database is found. It |
297 // should never be empty. | 283 // should never be empty. |
298 FilePath history_dir_; | 284 FilePath history_dir_; |
299 | 285 |
300 // The timestamp of when the cache was last saved. This is used to validate | 286 // The timestamp of when the cache was last saved. This is used to validate |
301 // the transaction journal's applicability to the cache. The timestamp is | 287 // the transaction journal's applicability to the cache. The timestamp is |
302 // initialized to the NULL time, indicating that the cache was not used with | 288 // initialized to the NULL time, indicating that the cache was not used with |
303 // the InMemoryURLIndex was last populated. | 289 // the InMemoryURLIndex was last populated. |
304 base::Time last_saved_; | 290 base::Time last_saved_; |
(...skipping 16 matching lines...) Expand all Loading... |
321 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. | 307 // TODO(mrossetti): Eliminate once the transition to SQLite has been done. |
322 // http://crbug.com/83659 | 308 // http://crbug.com/83659 |
323 bool cached_at_shutdown_; | 309 bool cached_at_shutdown_; |
324 | 310 |
325 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); | 311 DISALLOW_COPY_AND_ASSIGN(InMemoryURLIndex); |
326 }; | 312 }; |
327 | 313 |
328 } // namespace history | 314 } // namespace history |
329 | 315 |
330 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ | 316 #endif // CHROME_BROWSER_HISTORY_IN_MEMORY_URL_INDEX_H_ |
OLD | NEW |