Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "chrome/browser/history/url_index_private_data.h" | 5 #include "chrome/browser/history/url_index_private_data.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <iterator> | 9 #include <iterator> |
| 10 #include <limits> | 10 #include <limits> |
| 11 #include <numeric> | 11 #include <numeric> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 16 #include "base/i18n/case_conversion.h" | 16 #include "base/i18n/case_conversion.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/string_util.h" | 18 #include "base/string_util.h" |
| 19 #include "base/time.h" | 19 #include "base/time.h" |
| 20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
| 21 #include "chrome/browser/api/bookmarks/bookmark_service.h" | |
| 21 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 22 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 22 #include "chrome/browser/autocomplete/url_prefix.h" | 23 #include "chrome/browser/autocomplete/url_prefix.h" |
| 24 // TODO(mrossetti): Remove these two includes one BookmarkService is available. | |
|
Peter Kasting
2012/09/05 19:06:30
Nit: one -> once
I'm not actually sure what this
mrossetti
2012/09/05 20:15:43
Done. At some point, there will be an accessor to
| |
| 25 #include "chrome/browser/bookmarks/bookmark_model.h" | |
| 26 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | |
| 23 #include "chrome/browser/history/history_database.h" | 27 #include "chrome/browser/history/history_database.h" |
| 24 #include "chrome/browser/history/in_memory_url_index.h" | 28 #include "chrome/browser/history/in_memory_url_index.h" |
| 29 #include "chrome/browser/profiles/profile.h" | |
| 25 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/notification_details.h" | 31 #include "content/public/browser/notification_details.h" |
| 27 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
| 29 #include "net/base/net_util.h" | 34 #include "net/base/net_util.h" |
| 30 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" | 35 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" |
| 31 | 36 |
| 32 using google::protobuf::RepeatedField; | 37 using google::protobuf::RepeatedField; |
| 33 using google::protobuf::RepeatedPtrField; | 38 using google::protobuf::RepeatedPtrField; |
| 34 using in_memory_url_index::InMemoryURLIndexCacheItem; | 39 using in_memory_url_index::InMemoryURLIndexCacheItem; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 55 | 60 |
| 56 // Algorithm Functions --------------------------------------------------------- | 61 // Algorithm Functions --------------------------------------------------------- |
| 57 | 62 |
| 58 // Comparison function for sorting search terms by descending length. | 63 // Comparison function for sorting search terms by descending length. |
| 59 bool LengthGreater(const string16& string_a, const string16& string_b) { | 64 bool LengthGreater(const string16& string_a, const string16& string_b) { |
| 60 return string_a.length() > string_b.length(); | 65 return string_a.length() > string_b.length(); |
| 61 } | 66 } |
| 62 | 67 |
| 63 // Public Functions ------------------------------------------------------------ | 68 // Public Functions ------------------------------------------------------------ |
| 64 | 69 |
| 65 URLIndexPrivateData::URLIndexPrivateData() | 70 URLIndexPrivateData::URLIndexPrivateData(Profile* profile) |
| 66 : restored_cache_version_(0), | 71 : profile_(profile), |
| 72 restored_cache_version_(0), | |
| 67 saved_cache_version_(kCurrentCacheFileVersion), | 73 saved_cache_version_(kCurrentCacheFileVersion), |
| 68 pre_filter_item_count_(0), | 74 pre_filter_item_count_(0), |
| 69 post_filter_item_count_(0), | 75 post_filter_item_count_(0), |
| 70 post_scoring_item_count_(0) { | 76 post_scoring_item_count_(0) { |
| 71 } | 77 } |
| 72 | 78 |
| 73 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( | 79 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( |
| 74 const string16& search_string) { | 80 const string16& search_string) { |
| 75 pre_filter_item_count_ = 0; | 81 pre_filter_item_count_ = 0; |
| 76 post_filter_item_count_ = 0; | 82 post_filter_item_count_ = 0; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 HistoryInfoMapItemHasURL(url)); | 258 HistoryInfoMapItemHasURL(url)); |
| 253 if (pos == history_info_map_.end()) | 259 if (pos == history_info_map_.end()) |
| 254 return false; | 260 return false; |
| 255 RemoveRowFromIndex(pos->second); | 261 RemoveRowFromIndex(pos->second); |
| 256 search_term_cache_.clear(); // This invalidates the cache. | 262 search_term_cache_.clear(); // This invalidates the cache. |
| 257 return true; | 263 return true; |
| 258 } | 264 } |
| 259 | 265 |
| 260 // static | 266 // static |
| 261 void URLIndexPrivateData::RestoreFromFileTask( | 267 void URLIndexPrivateData::RestoreFromFileTask( |
| 268 Profile* profile, | |
| 262 const FilePath& file_path, | 269 const FilePath& file_path, |
| 263 scoped_refptr<URLIndexPrivateData> private_data, | 270 scoped_refptr<URLIndexPrivateData> private_data, |
| 264 const std::string& languages) { | 271 const std::string& languages) { |
| 265 private_data = URLIndexPrivateData::RestoreFromFile(file_path, languages); | 272 private_data = |
| 273 URLIndexPrivateData::RestoreFromFile(profile, file_path, languages); | |
| 266 } | 274 } |
| 267 | 275 |
| 268 // static | 276 // static |
| 269 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RebuildFromHistory( | 277 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RebuildFromHistory( |
| 278 Profile* profile, | |
| 270 HistoryDatabase* history_db, | 279 HistoryDatabase* history_db, |
| 271 const std::string& languages, | 280 const std::string& languages, |
| 272 const std::set<std::string>& scheme_whitelist) { | 281 const std::set<std::string>& scheme_whitelist) { |
| 273 if (!history_db) | 282 if (!history_db) |
| 274 return NULL; | 283 return NULL; |
| 275 | 284 |
| 276 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 285 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 277 | 286 |
| 278 scoped_refptr<URLIndexPrivateData> rebuilt_data(new URLIndexPrivateData); | 287 scoped_refptr<URLIndexPrivateData> |
| 288 rebuilt_data(new URLIndexPrivateData(profile)); | |
| 279 URLDatabase::URLEnumerator history_enum; | 289 URLDatabase::URLEnumerator history_enum; |
| 280 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) | 290 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) |
| 281 return NULL; | 291 return NULL; |
| 282 for (URLRow row; history_enum.GetNextURL(&row); ) | 292 for (URLRow row; history_enum.GetNextURL(&row); ) |
| 283 rebuilt_data->IndexRow(row, languages, scheme_whitelist); | 293 rebuilt_data->IndexRow(row, languages, scheme_whitelist); |
| 284 | 294 |
| 285 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", | 295 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", |
| 286 base::TimeTicks::Now() - beginning_time); | 296 base::TimeTicks::Now() - beginning_time); |
| 287 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", | 297 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", |
| 288 rebuilt_data->history_id_word_map_.size()); | 298 rebuilt_data->history_id_word_map_.size()); |
| 289 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", | 299 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLWords", |
| 290 rebuilt_data->word_map_.size()); | 300 rebuilt_data->word_map_.size()); |
| 291 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", | 301 UMA_HISTOGRAM_COUNTS_10000("History.InMemoryURLChars", |
| 292 rebuilt_data->char_word_map_.size()); | 302 rebuilt_data->char_word_map_.size()); |
| 293 return rebuilt_data; | 303 return rebuilt_data; |
| 294 } | 304 } |
| 295 | 305 |
| 296 // static | 306 // static |
| 297 void URLIndexPrivateData::WritePrivateDataToCacheFileTask( | 307 void URLIndexPrivateData::WritePrivateDataToCacheFileTask( |
| 298 scoped_refptr<URLIndexPrivateData> private_data, | 308 scoped_refptr<URLIndexPrivateData> private_data, |
| 299 const FilePath& file_path, | 309 const FilePath& file_path, |
| 300 scoped_refptr<RefCountedBool> succeeded) { | 310 scoped_refptr<RefCountedBool> succeeded) { |
| 301 DCHECK(private_data.get()); | 311 DCHECK(private_data.get()); |
| 302 DCHECK(!file_path.empty()); | 312 DCHECK(!file_path.empty()); |
| 303 succeeded->set_value(private_data->SaveToFile(file_path)); | 313 succeeded->set_value(private_data->SaveToFile(file_path)); |
| 304 } | 314 } |
| 305 | 315 |
| 306 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const { | 316 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const { |
| 307 scoped_refptr<URLIndexPrivateData> data_copy = new URLIndexPrivateData; | 317 scoped_refptr<URLIndexPrivateData> data_copy = |
| 318 new URLIndexPrivateData(profile_); | |
| 308 data_copy->word_list_ = word_list_; | 319 data_copy->word_list_ = word_list_; |
| 309 data_copy->available_words_ = available_words_; | 320 data_copy->available_words_ = available_words_; |
| 310 data_copy->word_map_ = word_map_; | 321 data_copy->word_map_ = word_map_; |
| 311 data_copy->char_word_map_ = char_word_map_; | 322 data_copy->char_word_map_ = char_word_map_; |
| 312 data_copy->word_id_history_map_ = word_id_history_map_; | 323 data_copy->word_id_history_map_ = word_id_history_map_; |
| 313 data_copy->history_id_word_map_ = history_id_word_map_; | 324 data_copy->history_id_word_map_ = history_id_word_map_; |
| 314 data_copy->history_info_map_ = history_info_map_; | 325 data_copy->history_info_map_ = history_info_map_; |
| 315 data_copy->word_starts_map_ = word_starts_map_; | 326 data_copy->word_starts_map_ = word_starts_map_; |
| 316 return data_copy; | 327 return data_copy; |
| 317 // Not copied: | 328 // Not copied: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 | 381 |
| 371 void URLIndexPrivateData::AddHistoryMatch::operator()( | 382 void URLIndexPrivateData::AddHistoryMatch::operator()( |
| 372 const HistoryID history_id) { | 383 const HistoryID history_id) { |
| 373 HistoryInfoMap::const_iterator hist_pos = | 384 HistoryInfoMap::const_iterator hist_pos = |
| 374 private_data_.history_info_map_.find(history_id); | 385 private_data_.history_info_map_.find(history_id); |
| 375 if (hist_pos != private_data_.history_info_map_.end()) { | 386 if (hist_pos != private_data_.history_info_map_.end()) { |
| 376 const URLRow& hist_item = hist_pos->second; | 387 const URLRow& hist_item = hist_pos->second; |
| 377 WordStartsMap::const_iterator starts_pos = | 388 WordStartsMap::const_iterator starts_pos = |
| 378 private_data_.word_starts_map_.find(history_id); | 389 private_data_.word_starts_map_.find(history_id); |
| 379 DCHECK(starts_pos != private_data_.word_starts_map_.end()); | 390 DCHECK(starts_pos != private_data_.word_starts_map_.end()); |
| 391 // TODO(mrossetti): Change this to retrieve BookmarkService when available. | |
| 392 BookmarkService* bookmark_service = | |
| 393 BookmarkModelFactory::GetForProfile(private_data_.profile_); | |
| 380 ScoredHistoryMatch match(hist_item, lower_string_, lower_terms_, | 394 ScoredHistoryMatch match(hist_item, lower_string_, lower_terms_, |
| 381 starts_pos->second, now_); | 395 starts_pos->second, now_, bookmark_service); |
| 382 if (match.raw_score > 0) | 396 if (match.raw_score > 0) |
| 383 scored_matches_.push_back(match); | 397 scored_matches_.push_back(match); |
| 384 } | 398 } |
| 385 } | 399 } |
| 386 | 400 |
| 387 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- | 401 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- |
| 388 | 402 |
| 389 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( | 403 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( |
| 390 const HistoryInfoMap& history_info_map) | 404 const HistoryInfoMap& history_info_map) |
| 391 : history_info_map_(history_info_map) { | 405 : history_info_map_(history_info_map) { |
| (...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 921 for (WordStarts::const_iterator i = word_starts.title_word_starts_.begin(); | 935 for (WordStarts::const_iterator i = word_starts.title_word_starts_.begin(); |
| 922 i != word_starts.title_word_starts_.end(); ++i) | 936 i != word_starts.title_word_starts_.end(); ++i) |
| 923 map_entry->add_title_word_starts(*i); | 937 map_entry->add_title_word_starts(*i); |
| 924 } | 938 } |
| 925 } | 939 } |
| 926 | 940 |
| 927 // Cache Restoring ------------------------------------------------------------- | 941 // Cache Restoring ------------------------------------------------------------- |
| 928 | 942 |
| 929 // static | 943 // static |
| 930 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RestoreFromFile( | 944 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RestoreFromFile( |
| 945 Profile* profile, | |
| 931 const FilePath& file_path, | 946 const FilePath& file_path, |
| 932 const std::string& languages) { | 947 const std::string& languages) { |
| 933 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 948 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 934 if (!file_util::PathExists(file_path)) | 949 if (!file_util::PathExists(file_path)) |
| 935 return NULL; | 950 return NULL; |
| 936 std::string data; | 951 std::string data; |
| 937 // If there is no cache file then simply give up. This will cause us to | 952 // If there is no cache file then simply give up. This will cause us to |
| 938 // attempt to rebuild from the history database. | 953 // attempt to rebuild from the history database. |
| 939 if (!file_util::ReadFileToString(file_path, &data)) | 954 if (!file_util::ReadFileToString(file_path, &data)) |
| 940 return NULL; | 955 return NULL; |
| 941 | 956 |
| 942 scoped_refptr<URLIndexPrivateData> restored_data(new URLIndexPrivateData); | 957 scoped_refptr<URLIndexPrivateData> |
| 958 restored_data(new URLIndexPrivateData(profile)); | |
| 943 InMemoryURLIndexCacheItem index_cache; | 959 InMemoryURLIndexCacheItem index_cache; |
| 944 if (!index_cache.ParseFromArray(data.c_str(), data.size())) { | 960 if (!index_cache.ParseFromArray(data.c_str(), data.size())) { |
| 945 LOG(WARNING) << "Failed to parse URLIndexPrivateData cache data read from " | 961 LOG(WARNING) << "Failed to parse URLIndexPrivateData cache data read from " |
| 946 << file_path.value(); | 962 << file_path.value(); |
| 947 return restored_data; | 963 return restored_data; |
| 948 } | 964 } |
| 949 | 965 |
| 950 if (!restored_data->RestorePrivateData(index_cache, languages)) | 966 if (!restored_data->RestorePrivateData(index_cache, languages)) |
| 951 return NULL; | 967 return NULL; |
| 952 | 968 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1144 } | 1160 } |
| 1145 | 1161 |
| 1146 // static | 1162 // static |
| 1147 bool URLIndexPrivateData::URLSchemeIsWhitelisted( | 1163 bool URLIndexPrivateData::URLSchemeIsWhitelisted( |
| 1148 const GURL& gurl, | 1164 const GURL& gurl, |
| 1149 const std::set<std::string>& whitelist) { | 1165 const std::set<std::string>& whitelist) { |
| 1150 return whitelist.find(gurl.scheme()) != whitelist.end(); | 1166 return whitelist.find(gurl.scheme()) != whitelist.end(); |
| 1151 } | 1167 } |
| 1152 | 1168 |
| 1153 } // namespace history | 1169 } // namespace history |
| OLD | NEW |