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 "components/omnibox/browser/url_index_private_data.h" | 5 #include "components/omnibox/browser/url_index_private_data.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <functional> | 9 #include <functional> |
| 10 #include <iterator> | 10 #include <iterator> |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 WordStartsMapEntry; | 74 WordStartsMapEntry; |
| 75 | 75 |
| 76 // Algorithm Functions --------------------------------------------------------- | 76 // Algorithm Functions --------------------------------------------------------- |
| 77 | 77 |
| 78 // Comparison function for sorting search terms by descending length. | 78 // Comparison function for sorting search terms by descending length. |
| 79 bool LengthGreater(const base::string16& string_a, | 79 bool LengthGreater(const base::string16& string_a, |
| 80 const base::string16& string_b) { | 80 const base::string16& string_b) { |
| 81 return string_a.length() > string_b.length(); | 81 return string_a.length() > string_b.length(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 | |
| 85 // UpdateRecentVisitsFromHistoryDBTask ----------------------------------------- | 84 // UpdateRecentVisitsFromHistoryDBTask ----------------------------------------- |
| 86 | 85 |
| 87 // HistoryDBTask used to update the recent visit data for a particular | 86 // HistoryDBTask used to update the recent visit data for a particular |
| 88 // row from the history database. | 87 // row from the history database. |
| 89 class UpdateRecentVisitsFromHistoryDBTask : public history::HistoryDBTask { | 88 class UpdateRecentVisitsFromHistoryDBTask : public history::HistoryDBTask { |
| 90 public: | 89 public: |
| 91 explicit UpdateRecentVisitsFromHistoryDBTask( | 90 explicit UpdateRecentVisitsFromHistoryDBTask( |
| 92 URLIndexPrivateData* private_data, | 91 URLIndexPrivateData* private_data, |
| 93 history::URLID url_id); | 92 history::URLID url_id); |
| 94 | 93 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 } | 133 } |
| 135 | 134 |
| 136 void UpdateRecentVisitsFromHistoryDBTask::DoneRunOnMainThread() { | 135 void UpdateRecentVisitsFromHistoryDBTask::DoneRunOnMainThread() { |
| 137 if (succeeded_) | 136 if (succeeded_) |
| 138 private_data_->UpdateRecentVisits(url_id_, recent_visits_); | 137 private_data_->UpdateRecentVisits(url_id_, recent_visits_); |
| 139 } | 138 } |
| 140 | 139 |
| 141 UpdateRecentVisitsFromHistoryDBTask::~UpdateRecentVisitsFromHistoryDBTask() { | 140 UpdateRecentVisitsFromHistoryDBTask::~UpdateRecentVisitsFromHistoryDBTask() { |
| 142 } | 141 } |
| 143 | 142 |
| 144 | |
| 145 // URLIndexPrivateData --------------------------------------------------------- | 143 // URLIndexPrivateData --------------------------------------------------------- |
| 146 | 144 |
| 147 URLIndexPrivateData::URLIndexPrivateData() | 145 URLIndexPrivateData::URLIndexPrivateData() |
| 148 : restored_cache_version_(0), | 146 : restored_cache_version_(0), |
| 149 saved_cache_version_(kCurrentCacheFileVersion), | 147 saved_cache_version_(kCurrentCacheFileVersion), |
| 150 pre_filter_item_count_(0), | 148 pre_filter_item_count_(0), |
| 151 post_filter_item_count_(0), | 149 post_filter_item_count_(0), |
| 152 post_scoring_item_count_(0) { | 150 post_scoring_item_count_(0) { |
| 153 } | 151 } |
| 154 | 152 |
| 155 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( | 153 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms( |
| 156 base::string16 search_string, | 154 base::string16 original_search_string, |
| 157 size_t cursor_position, | 155 size_t cursor_position, |
| 158 size_t max_matches, | 156 size_t max_matches, |
| 159 bookmarks::BookmarkModel* bookmark_model, | 157 bookmarks::BookmarkModel* bookmark_model, |
| 160 TemplateURLService* template_url_service) { | 158 TemplateURLService* template_url_service) { |
| 161 // If cursor position is set and useful (not at either end of the | |
| 162 // string), allow the search string to be broken at cursor position. | |
| 163 // We do this by pretending there's a space where the cursor is. | |
| 164 if ((cursor_position != base::string16::npos) && | |
| 165 (cursor_position < search_string.length()) && | |
| 166 (cursor_position > 0)) { | |
| 167 search_string.insert(cursor_position, base::ASCIIToUTF16(" ")); | |
| 168 } | |
| 169 pre_filter_item_count_ = 0; | 159 pre_filter_item_count_ = 0; |
| 170 post_filter_item_count_ = 0; | 160 post_filter_item_count_ = 0; |
| 171 post_scoring_item_count_ = 0; | 161 post_scoring_item_count_ = 0; |
| 172 // The search string we receive may contain escaped characters. For reducing | |
| 173 // the index we need individual, lower-cased words, ignoring escapings. For | |
| 174 // the final filtering we need whitespace separated substrings possibly | |
| 175 // containing escaped characters. | |
| 176 base::string16 lower_raw_string(base::i18n::ToLower(search_string)); | |
| 177 base::string16 lower_unescaped_string = | |
| 178 net::UnescapeURLComponent(lower_raw_string, | |
| 179 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | | |
| 180 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS); | |
| 181 // Extract individual 'words' (as opposed to 'terms'; see below) from the | |
| 182 // search string. When the user types "colspec=ID%20Mstone Release" we get | |
| 183 // four 'words': "colspec", "id", "mstone" and "release". | |
| 184 String16Vector lower_words( | |
| 185 String16VectorFromString16(lower_unescaped_string, false, nullptr)); | |
| 186 ScoredHistoryMatches scored_items; | |
| 187 | 162 |
| 188 // Do nothing if we have indexed no words (probably because we've not been | 163 // This list will contain the original search string and any other string |
| 189 // initialized yet) or the search string has no words. | 164 // transformations. |
| 190 if (word_list_.empty() || lower_words.empty()) { | 165 String16Vector search_strings; |
| 191 search_term_cache_.clear(); // Invalidate the term cache. | 166 search_strings.push_back(original_search_string); |
| 192 return scored_items; | 167 if ((cursor_position != base::string16::npos) && |
| 168 (cursor_position < original_search_string.length()) && | |
| 169 (cursor_position > 0)) { | |
| 170 // The original search_string broken at cursor position. This is one type of | |
| 171 // transformation. | |
| 172 base::string16 transformed_search_string(original_search_string); | |
| 173 transformed_search_string.insert(cursor_position, base::ASCIIToUTF16(" ")); | |
| 174 search_strings.push_back(transformed_search_string); | |
| 193 } | 175 } |
| 194 | 176 |
| 195 // Reset used_ flags for search_term_cache_. We use a basic mark-and-sweep | 177 // Invalidate the term cache or reset used_ flags for search_term_cache_ using |
| 196 // approach. | 178 // a basic mark-and-sweep approach. |
| 197 ResetSearchTermCache(); | 179 word_list_.empty() ? search_term_cache_.clear() : ResetSearchTermCache(); |
|
Mark P
2016/09/16 23:18:06
1. Please return early if word_list_ is empty (as
| |
| 198 | 180 |
| 199 HistoryIDSet history_id_set = HistoryIDSetFromWords(lower_words); | 181 ScoredHistoryMatches scored_items; |
| 182 for (const base::string16& search_string : search_strings) { | |
| 183 // The search string we receive may contain escaped characters. For reducing | |
| 184 // the index we need individual, lower-cased words, ignoring escapings. For | |
| 185 // the final filtering we need whitespace separated substrings possibly | |
| 186 // containing escaped characters. | |
| 187 base::string16 lower_raw_string(base::i18n::ToLower(search_string)); | |
| 188 base::string16 lower_unescaped_string = net::UnescapeURLComponent( | |
| 189 lower_raw_string, | |
| 190 net::UnescapeRule::SPACES | net::UnescapeRule::PATH_SEPARATORS | | |
| 191 net::UnescapeRule::URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS); | |
| 200 | 192 |
| 201 // Trim the candidate pool if it is large. Note that we do not filter out | 193 // Extract individual 'words' (as opposed to 'terms'; see below) from the |
| 202 // items that do not contain the search terms as proper substrings -- doing | 194 // search string. When the user types "colspec=ID%20Mstone Release" we get |
| 203 // so is the performance-costly operation we are trying to avoid in order | 195 // four 'words': "colspec", "id", "mstone" and "release". |
| 204 // to maintain omnibox responsiveness. | 196 String16Vector lower_words( |
| 205 const size_t kItemsToScoreLimit = 500; | 197 String16VectorFromString16(lower_unescaped_string, false, nullptr)); |
| 206 pre_filter_item_count_ = history_id_set.size(); | 198 if (lower_words.empty()) |
| 207 // If we trim the results set we do not want to cache the results for next | 199 continue; |
| 208 // time as the user's ultimately desired result could easily be eliminated | 200 HistoryIDSet history_id_set = HistoryIDSetFromWords(lower_words); |
| 209 // in this early rough filter. | 201 // Trim the candidate pool if it is large. Note that we do not filter out |
| 210 bool was_trimmed = (pre_filter_item_count_ > kItemsToScoreLimit); | 202 // items that do not contain the search terms as proper substrings -- |
| 211 if (was_trimmed) { | 203 // doing so is the performance-costly operation we are trying to avoid in |
| 212 HistoryIDVector history_ids; | 204 // order to maintain omnibox responsiveness. |
| 213 std::copy(history_id_set.begin(), history_id_set.end(), | 205 const size_t kItemsToScoreLimit = 500; |
| 214 std::back_inserter(history_ids)); | 206 pre_filter_item_count_ += history_id_set.size(); |
| 215 // Trim down the set by sorting by typed-count, visit-count, and last | 207 // If we trim the results set we do not want to cache the results for next |
| 216 // visit. | 208 // time as the user's ultimately desired result could easily be eliminated |
| 217 HistoryItemFactorGreater | 209 // in this early rough filter. |
| 218 item_factor_functor(history_info_map_); | 210 if (pre_filter_item_count_ > kItemsToScoreLimit) { |
| 219 std::partial_sort(history_ids.begin(), | 211 HistoryIDVector history_ids; |
| 220 history_ids.begin() + kItemsToScoreLimit, | 212 std::copy(history_id_set.begin(), history_id_set.end(), |
| 221 history_ids.end(), | 213 std::back_inserter(history_ids)); |
| 222 item_factor_functor); | 214 // Trim down the set by sorting by typed-count, visit-count, and last |
| 223 history_id_set.clear(); | 215 // visit. |
| 224 std::copy(history_ids.begin(), history_ids.begin() + kItemsToScoreLimit, | 216 HistoryItemFactorGreater item_factor_functor(history_info_map_); |
| 225 std::inserter(history_id_set, history_id_set.end())); | 217 std::partial_sort(history_ids.begin(), |
| 226 post_filter_item_count_ = history_id_set.size(); | 218 history_ids.begin() + kItemsToScoreLimit, |
| 227 } | 219 history_ids.end(), item_factor_functor); |
| 220 history_id_set.clear(); | |
| 221 std::copy(history_ids.begin(), history_ids.begin() + kItemsToScoreLimit, | |
| 222 std::inserter(history_id_set, history_id_set.end())); | |
| 223 post_filter_item_count_ = post_filter_item_count_ + history_id_set.size(); | |
| 224 } | |
| 225 // Pass over all of the candidates filtering out any without a proper | |
| 226 // substring match, inserting those which pass in order by score. Note that | |
| 227 // in this step we are using the raw search string complete with escaped | |
| 228 // URL elements. When the user has specifically typed something akin to | |
| 229 // "sort=pri&colspec=ID%20Mstone%20Release" we want to make sure that that | |
| 230 // specific substring appears in the URL or page title. | |
| 228 | 231 |
| 229 // Pass over all of the candidates filtering out any without a proper | 232 // We call these 'terms' (as opposed to 'words'; see above) as in this case |
| 230 // substring match, inserting those which pass in order by score. Note that | 233 // we only want to break up the search string on 'true' whitespace rather |
| 231 // in this step we are using the raw search string complete with escaped | 234 // than escaped whitespace. When the user types "colspec=ID%20Mstone |
| 232 // URL elements. When the user has specifically typed something akin to | 235 // Release" we get two 'terms': "colspec=id%20mstone" and "release". |
| 233 // "sort=pri&colspec=ID%20Mstone%20Release" we want to make sure that that | 236 String16Vector lower_raw_terms = |
| 234 // specific substring appears in the URL or page title. | 237 base::SplitString(lower_raw_string, base::kWhitespaceUTF16, |
| 238 base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 235 | 239 |
| 236 // We call these 'terms' (as opposed to 'words'; see above) as in this case | |
| 237 // we only want to break up the search string on 'true' whitespace rather than | |
| 238 // escaped whitespace. When the user types "colspec=ID%20Mstone Release" we | |
| 239 // get two 'terms': "colspec=id%20mstone" and "release". | |
| 240 String16Vector lower_raw_terms = base::SplitString( | |
| 241 lower_raw_string, base::kWhitespaceUTF16, base::KEEP_WHITESPACE, | |
| 242 base::SPLIT_WANT_NONEMPTY); | |
| 243 if (lower_raw_terms.empty()) { | |
| 244 // Don't score matches when there are no terms to score against. (It's | 240 // Don't score matches when there are no terms to score against. (It's |
| 245 // possible that the word break iterater that extracts words to search | 241 // possible that the word break iterater that extracts words to search |
| 246 // for in the database allows some whitespace "words" whereas SplitString | 242 // for in the database allows some whitespace "words" whereas SplitString |
| 247 // excludes a long list of whitespace.) One could write a scoring | 243 // excludes a long list of whitespace.) One could write a scoring function |
| 248 // function that gives a reasonable order to matches when there | 244 // that gives a reasonable order to matches when there are no terms (i.e., |
| 249 // are no terms (i.e., all the words are some form of whitespace), | 245 // all the words are some form of whitespace), but this is such a rare edge |
| 250 // but this is such a rare edge case that it's not worth the time. | 246 // case that it's not worth the time. |
| 251 return scored_items; | 247 if (lower_raw_terms.empty()) |
| 248 continue; | |
| 249 ScoredHistoryMatches temp_scored_items = | |
| 250 std::for_each(history_id_set.begin(), history_id_set.end(), | |
| 251 AddHistoryMatch(bookmark_model, template_url_service, | |
| 252 *this, lower_raw_string, lower_raw_terms, | |
| 253 base::Time::Now())) | |
| 254 .ScoredMatches(); | |
| 255 scored_items.insert(scored_items.end(), temp_scored_items.begin(), | |
| 256 temp_scored_items.end()); | |
| 252 } | 257 } |
| 253 scored_items = | |
| 254 std::for_each( | |
| 255 history_id_set.begin(), history_id_set.end(), | |
| 256 AddHistoryMatch(bookmark_model, template_url_service, *this, | |
| 257 lower_raw_string, lower_raw_terms, | |
| 258 base::Time::Now())).ScoredMatches(); | |
| 259 | |
| 260 // Select and sort only the top |max_matches| results. | 258 // Select and sort only the top |max_matches| results. |
| 261 if (scored_items.size() > max_matches) { | 259 if (scored_items.size() > max_matches) { |
| 262 std::partial_sort(scored_items.begin(), | 260 std::partial_sort(scored_items.begin(), scored_items.begin() + max_matches, |
| 263 scored_items.begin() + | |
| 264 max_matches, | |
| 265 scored_items.end(), | 261 scored_items.end(), |
| 266 ScoredHistoryMatch::MatchScoreGreater); | 262 ScoredHistoryMatch::MatchScoreGreater); |
| 267 scored_items.resize(max_matches); | 263 scored_items.resize(max_matches); |
| 268 } else { | 264 } else { |
| 269 std::sort(scored_items.begin(), scored_items.end(), | 265 std::sort(scored_items.begin(), scored_items.end(), |
| 270 ScoredHistoryMatch::MatchScoreGreater); | 266 ScoredHistoryMatch::MatchScoreGreater); |
| 271 } | 267 } |
| 272 post_scoring_item_count_ = scored_items.size(); | 268 post_scoring_item_count_ = scored_items.size(); |
| 273 | 269 if (pre_filter_item_count_ > post_filter_item_count_) { |
| 274 if (was_trimmed) { | |
| 275 search_term_cache_.clear(); // Invalidate the term cache. | 270 search_term_cache_.clear(); // Invalidate the term cache. |
| 276 } else { | 271 } else { |
| 277 // Remove any stale SearchTermCacheItems. | 272 // Remove any stale SearchTermCacheItems. |
| 278 for (SearchTermCacheMap::iterator cache_iter = search_term_cache_.begin(); | 273 for (SearchTermCacheMap::iterator cache_iter = search_term_cache_.begin(); |
| 279 cache_iter != search_term_cache_.end(); ) { | 274 cache_iter != search_term_cache_.end(); ) { |
| 280 if (!cache_iter->second.used_) | 275 if (!cache_iter->second.used_) |
| 281 search_term_cache_.erase(cache_iter++); | 276 search_term_cache_.erase(cache_iter++); |
| 282 else | 277 else |
| 283 ++cache_iter; | 278 ++cache_iter; |
| 284 } | 279 } |
| (...skipping 959 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1244 return true; | 1239 return true; |
| 1245 } | 1240 } |
| 1246 | 1241 |
| 1247 // static | 1242 // static |
| 1248 bool URLIndexPrivateData::URLSchemeIsWhitelisted( | 1243 bool URLIndexPrivateData::URLSchemeIsWhitelisted( |
| 1249 const GURL& gurl, | 1244 const GURL& gurl, |
| 1250 const std::set<std::string>& whitelist) { | 1245 const std::set<std::string>& whitelist) { |
| 1251 return whitelist.find(gurl.scheme()) != whitelist.end(); | 1246 return whitelist.find(gurl.scheme()) != whitelist.end(); |
| 1252 } | 1247 } |
| 1253 | 1248 |
| 1254 | |
| 1255 // SearchTermCacheItem --------------------------------------------------------- | 1249 // SearchTermCacheItem --------------------------------------------------------- |
| 1256 | 1250 |
| 1257 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem( | 1251 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem( |
| 1258 const WordIDSet& word_id_set, | 1252 const WordIDSet& word_id_set, |
| 1259 const HistoryIDSet& history_id_set) | 1253 const HistoryIDSet& history_id_set) |
| 1260 : word_id_set_(word_id_set), history_id_set_(history_id_set), used_(true) { | 1254 : word_id_set_(word_id_set), history_id_set_(history_id_set), used_(true) { |
| 1261 } | 1255 } |
| 1262 | 1256 |
| 1263 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() : used_(true) { | 1257 URLIndexPrivateData::SearchTermCacheItem::SearchTermCacheItem() : used_(true) { |
| 1264 } | 1258 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1354 // First cut: typed count, visit count, recency. | 1348 // First cut: typed count, visit count, recency. |
| 1355 // TODO(mrossetti): This is too simplistic. Consider an approach which ranks | 1349 // TODO(mrossetti): This is too simplistic. Consider an approach which ranks |
| 1356 // recently visited (within the last 12/24 hours) as highly important. Get | 1350 // recently visited (within the last 12/24 hours) as highly important. Get |
| 1357 // input from mpearson. | 1351 // input from mpearson. |
| 1358 if (r1.typed_count() != r2.typed_count()) | 1352 if (r1.typed_count() != r2.typed_count()) |
| 1359 return (r1.typed_count() > r2.typed_count()); | 1353 return (r1.typed_count() > r2.typed_count()); |
| 1360 if (r1.visit_count() != r2.visit_count()) | 1354 if (r1.visit_count() != r2.visit_count()) |
| 1361 return (r1.visit_count() > r2.visit_count()); | 1355 return (r1.visit_count() > r2.visit_count()); |
| 1362 return (r1.last_visit() > r2.last_visit()); | 1356 return (r1.last_visit() > r2.last_visit()); |
| 1363 } | 1357 } |
| OLD | NEW |