| 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" |
| 23 #include "chrome/browser/history/history_database.h" | 24 #include "chrome/browser/history/history_database.h" |
| 24 #include "chrome/browser/history/in_memory_url_index.h" | 25 #include "chrome/browser/history/in_memory_url_index.h" |
| 25 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
| 27 #include "content/public/browser/notification_service.h" | 28 #include "content/public/browser/notification_service.h" |
| 28 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
| 29 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
| 30 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" | 31 #include "third_party/protobuf/src/google/protobuf/repeated_field.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 65 |
| 65 URLIndexPrivateData::URLIndexPrivateData() | 66 URLIndexPrivateData::URLIndexPrivateData() |
| 66 : restored_cache_version_(0), | 67 : restored_cache_version_(0), |
| 67 saved_cache_version_(kCurrentCacheFileVersion), | 68 saved_cache_version_(kCurrentCacheFileVersion), |
| 68 pre_filter_item_count_(0), | 69 pre_filter_item_count_(0), |
| 69 post_filter_item_count_(0), | 70 post_filter_item_count_(0), |
| 70 post_scoring_item_count_(0) { | 71 post_scoring_item_count_(0) { |
| 71 } | 72 } |
| 72 | 73 |
| 73 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( | 74 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( |
| 74 const string16& search_string) { | 75 const string16& search_string, |
| 76 BookmarkService* bookmark_service) { |
| 75 pre_filter_item_count_ = 0; | 77 pre_filter_item_count_ = 0; |
| 76 post_filter_item_count_ = 0; | 78 post_filter_item_count_ = 0; |
| 77 post_scoring_item_count_ = 0; | 79 post_scoring_item_count_ = 0; |
| 78 // The search string we receive may contain escaped characters. For reducing | 80 // The search string we receive may contain escaped characters. For reducing |
| 79 // the index we need individual, lower-cased words, ignoring escapings. For | 81 // the index we need individual, lower-cased words, ignoring escapings. For |
| 80 // the final filtering we need whitespace separated substrings possibly | 82 // the final filtering we need whitespace separated substrings possibly |
| 81 // containing escaped characters. | 83 // containing escaped characters. |
| 82 string16 lower_raw_string(base::i18n::ToLower(search_string)); | 84 string16 lower_raw_string(base::i18n::ToLower(search_string)); |
| 83 string16 lower_unescaped_string = | 85 string16 lower_unescaped_string = |
| 84 net::UnescapeURLComponent(lower_raw_string, | 86 net::UnescapeURLComponent(lower_raw_string, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 // "sort=pri&colspec=ID%20Mstone%20Release" we want to make sure that that | 140 // "sort=pri&colspec=ID%20Mstone%20Release" we want to make sure that that |
| 139 // specific substring appears in the URL or page title. | 141 // specific substring appears in the URL or page title. |
| 140 | 142 |
| 141 // We call these 'terms' (as opposed to 'words'; see above) as in this case | 143 // We call these 'terms' (as opposed to 'words'; see above) as in this case |
| 142 // we only want to break up the search string on 'true' whitespace rather than | 144 // we only want to break up the search string on 'true' whitespace rather than |
| 143 // escaped whitespace. When the user types "colspec=ID%20Mstone Release" we | 145 // escaped whitespace. When the user types "colspec=ID%20Mstone Release" we |
| 144 // get two 'terms': "colspec=id%20mstone" and "release". | 146 // get two 'terms': "colspec=id%20mstone" and "release". |
| 145 history::String16Vector lower_raw_terms; | 147 history::String16Vector lower_raw_terms; |
| 146 Tokenize(lower_raw_string, kWhitespaceUTF16, &lower_raw_terms); | 148 Tokenize(lower_raw_string, kWhitespaceUTF16, &lower_raw_terms); |
| 147 scored_items = std::for_each(history_id_set.begin(), history_id_set.end(), | 149 scored_items = std::for_each(history_id_set.begin(), history_id_set.end(), |
| 148 AddHistoryMatch(*this, lower_raw_string, | 150 AddHistoryMatch(*this, bookmark_service, lower_raw_string, |
| 149 lower_raw_terms, base::Time::Now())).ScoredMatches(); | 151 lower_raw_terms, base::Time::Now())).ScoredMatches(); |
| 150 | 152 |
| 151 // Select and sort only the top kMaxMatches results. | 153 // Select and sort only the top kMaxMatches results. |
| 152 if (scored_items.size() > AutocompleteProvider::kMaxMatches) { | 154 if (scored_items.size() > AutocompleteProvider::kMaxMatches) { |
| 153 std::partial_sort(scored_items.begin(), | 155 std::partial_sort(scored_items.begin(), |
| 154 scored_items.begin() + | 156 scored_items.begin() + |
| 155 AutocompleteProvider::kMaxMatches, | 157 AutocompleteProvider::kMaxMatches, |
| 156 scored_items.end(), | 158 scored_items.end(), |
| 157 ScoredHistoryMatch::MatchScoreGreater); | 159 ScoredHistoryMatch::MatchScoreGreater); |
| 158 scored_items.resize(AutocompleteProvider::kMaxMatches); | 160 scored_items.resize(AutocompleteProvider::kMaxMatches); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 // static | 270 // static |
| 269 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RebuildFromHistory( | 271 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RebuildFromHistory( |
| 270 HistoryDatabase* history_db, | 272 HistoryDatabase* history_db, |
| 271 const std::string& languages, | 273 const std::string& languages, |
| 272 const std::set<std::string>& scheme_whitelist) { | 274 const std::set<std::string>& scheme_whitelist) { |
| 273 if (!history_db) | 275 if (!history_db) |
| 274 return NULL; | 276 return NULL; |
| 275 | 277 |
| 276 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 278 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 277 | 279 |
| 278 scoped_refptr<URLIndexPrivateData> rebuilt_data(new URLIndexPrivateData); | 280 scoped_refptr<URLIndexPrivateData> |
| 281 rebuilt_data(new URLIndexPrivateData); |
| 279 URLDatabase::URLEnumerator history_enum; | 282 URLDatabase::URLEnumerator history_enum; |
| 280 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) | 283 if (!history_db->InitURLEnumeratorForSignificant(&history_enum)) |
| 281 return NULL; | 284 return NULL; |
| 282 for (URLRow row; history_enum.GetNextURL(&row); ) | 285 for (URLRow row; history_enum.GetNextURL(&row); ) |
| 283 rebuilt_data->IndexRow(row, languages, scheme_whitelist); | 286 rebuilt_data->IndexRow(row, languages, scheme_whitelist); |
| 284 | 287 |
| 285 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", | 288 UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime", |
| 286 base::TimeTicks::Now() - beginning_time); | 289 base::TimeTicks::Now() - beginning_time); |
| 287 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", | 290 UMA_HISTOGRAM_COUNTS("History.InMemoryURLHistoryItems", |
| 288 rebuilt_data->history_id_word_map_.size()); | 291 rebuilt_data->history_id_word_map_.size()); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 354 |
| 352 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() | 355 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() |
| 353 : used_(true) {} | 356 : used_(true) {} |
| 354 | 357 |
| 355 URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {} | 358 URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {} |
| 356 | 359 |
| 357 // URLIndexPrivateData::AddHistoryMatch ---------------------------------------- | 360 // URLIndexPrivateData::AddHistoryMatch ---------------------------------------- |
| 358 | 361 |
| 359 URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch( | 362 URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch( |
| 360 const URLIndexPrivateData& private_data, | 363 const URLIndexPrivateData& private_data, |
| 364 BookmarkService* bookmark_service, |
| 361 const string16& lower_string, | 365 const string16& lower_string, |
| 362 const String16Vector& lower_terms, | 366 const String16Vector& lower_terms, |
| 363 const base::Time now) | 367 const base::Time now) |
| 364 : private_data_(private_data), | 368 : private_data_(private_data), |
| 369 bookmark_service_(bookmark_service), |
| 365 lower_string_(lower_string), | 370 lower_string_(lower_string), |
| 366 lower_terms_(lower_terms), | 371 lower_terms_(lower_terms), |
| 367 now_(now) {} | 372 now_(now) {} |
| 368 | 373 |
| 369 URLIndexPrivateData::AddHistoryMatch::~AddHistoryMatch() {} | 374 URLIndexPrivateData::AddHistoryMatch::~AddHistoryMatch() {} |
| 370 | 375 |
| 371 void URLIndexPrivateData::AddHistoryMatch::operator()( | 376 void URLIndexPrivateData::AddHistoryMatch::operator()( |
| 372 const HistoryID history_id) { | 377 const HistoryID history_id) { |
| 373 HistoryInfoMap::const_iterator hist_pos = | 378 HistoryInfoMap::const_iterator hist_pos = |
| 374 private_data_.history_info_map_.find(history_id); | 379 private_data_.history_info_map_.find(history_id); |
| 375 if (hist_pos != private_data_.history_info_map_.end()) { | 380 if (hist_pos != private_data_.history_info_map_.end()) { |
| 376 const URLRow& hist_item = hist_pos->second; | 381 const URLRow& hist_item = hist_pos->second; |
| 377 WordStartsMap::const_iterator starts_pos = | 382 WordStartsMap::const_iterator starts_pos = |
| 378 private_data_.word_starts_map_.find(history_id); | 383 private_data_.word_starts_map_.find(history_id); |
| 379 DCHECK(starts_pos != private_data_.word_starts_map_.end()); | 384 DCHECK(starts_pos != private_data_.word_starts_map_.end()); |
| 380 ScoredHistoryMatch match(hist_item, lower_string_, lower_terms_, | 385 ScoredHistoryMatch match(hist_item, lower_string_, lower_terms_, |
| 381 starts_pos->second, now_); | 386 starts_pos->second, now_, bookmark_service_); |
| 382 if (match.raw_score > 0) | 387 if (match.raw_score > 0) |
| 383 scored_matches_.push_back(match); | 388 scored_matches_.push_back(match); |
| 384 } | 389 } |
| 385 } | 390 } |
| 386 | 391 |
| 387 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- | 392 // URLIndexPrivateData::HistoryItemFactorGreater ------------------------------- |
| 388 | 393 |
| 389 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( | 394 URLIndexPrivateData::HistoryItemFactorGreater::HistoryItemFactorGreater( |
| 390 const HistoryInfoMap& history_info_map) | 395 const HistoryInfoMap& history_info_map) |
| 391 : history_info_map_(history_info_map) { | 396 : history_info_map_(history_info_map) { |
| (...skipping 752 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1144 } | 1149 } |
| 1145 | 1150 |
| 1146 // static | 1151 // static |
| 1147 bool URLIndexPrivateData::URLSchemeIsWhitelisted( | 1152 bool URLIndexPrivateData::URLSchemeIsWhitelisted( |
| 1148 const GURL& gurl, | 1153 const GURL& gurl, |
| 1149 const std::set<std::string>& whitelist) { | 1154 const std::set<std::string>& whitelist) { |
| 1150 return whitelist.find(gurl.scheme()) != whitelist.end(); | 1155 return whitelist.find(gurl.scheme()) != whitelist.end(); |
| 1151 } | 1156 } |
| 1152 | 1157 |
| 1153 } // namespace history | 1158 } // namespace history |
| OLD | NEW |