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