OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 const char SearchProvider::kFalse[] = "false"; | 244 const char SearchProvider::kFalse[] = "false"; |
245 | 245 |
246 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, | 246 SearchProvider::SearchProvider(AutocompleteProviderListener* listener, |
247 Profile* profile) | 247 Profile* profile) |
248 : AutocompleteProvider(listener, profile, | 248 : AutocompleteProvider(listener, profile, |
249 AutocompleteProvider::TYPE_SEARCH), | 249 AutocompleteProvider::TYPE_SEARCH), |
250 providers_(TemplateURLServiceFactory::GetForProfile(profile)), | 250 providers_(TemplateURLServiceFactory::GetForProfile(profile)), |
251 suggest_results_pending_(0), | 251 suggest_results_pending_(0), |
252 field_trial_triggered_(false), | 252 field_trial_triggered_(false), |
253 field_trial_triggered_in_session_(false), | 253 field_trial_triggered_in_session_(false), |
254 omnibox_start_margin_(-1), | 254 omnibox_start_margin_(-1) { |
255 prevent_search_history_inlining_( | |
256 OmniboxFieldTrial::SearchHistoryPreventInlining()), | |
257 disable_search_history_( | |
258 OmniboxFieldTrial::SearchHistoryDisable()) { | |
259 } | 255 } |
260 | 256 |
261 // static | 257 // static |
262 AutocompleteMatch SearchProvider::CreateSearchSuggestion( | 258 AutocompleteMatch SearchProvider::CreateSearchSuggestion( |
263 AutocompleteProvider* autocomplete_provider, | 259 AutocompleteProvider* autocomplete_provider, |
264 int relevance, | 260 int relevance, |
265 AutocompleteMatch::Type type, | 261 AutocompleteMatch::Type type, |
266 const TemplateURL* template_url, | 262 const TemplateURL* template_url, |
267 const string16& query_string, | 263 const string16& query_string, |
268 const string16& input_text, | 264 const string16& input_text, |
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
623 | 619 |
624 void SearchProvider::DoHistoryQuery(bool minimal_changes) { | 620 void SearchProvider::DoHistoryQuery(bool minimal_changes) { |
625 // The history query results are synchronous, so if minimal_changes is true, | 621 // The history query results are synchronous, so if minimal_changes is true, |
626 // we still have the last results and don't need to do anything. | 622 // we still have the last results and don't need to do anything. |
627 if (minimal_changes) | 623 if (minimal_changes) |
628 return; | 624 return; |
629 | 625 |
630 keyword_history_results_.clear(); | 626 keyword_history_results_.clear(); |
631 default_history_results_.clear(); | 627 default_history_results_.clear(); |
632 | 628 |
633 if (disable_search_history_) | 629 if (OmniboxFieldTrial::SearchHistoryDisable( |
630 input_.current_page_classification())) { | |
Peter Kasting
2013/08/01 22:44:48
Nit: Indent 4, not 8. I would remove {} too since
Mark P
2013/08/02 00:44:36
Hey, I previously indented 4 and Alexei told me to
| |
634 return; | 631 return; |
632 } | |
635 | 633 |
636 HistoryService* const history_service = | 634 HistoryService* const history_service = |
637 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 635 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
638 history::URLDatabase* url_db = history_service ? | 636 history::URLDatabase* url_db = history_service ? |
639 history_service->InMemoryDatabase() : NULL; | 637 history_service->InMemoryDatabase() : NULL; |
640 if (!url_db) | 638 if (!url_db) |
641 return; | 639 return; |
642 | 640 |
643 // Request history for both the keyword and default provider. We grab many | 641 // Request history for both the keyword and default provider. We grab many |
644 // more matches than we'll ultimately clamp to so that if there are several | 642 // more matches than we'll ultimately clamp to so that if there are several |
(...skipping 551 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1196 | 1194 |
1197 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( | 1195 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( |
1198 const HistoryResults& results, | 1196 const HistoryResults& results, |
1199 bool base_prevent_inline_autocomplete, | 1197 bool base_prevent_inline_autocomplete, |
1200 bool input_multiple_words, | 1198 bool input_multiple_words, |
1201 const string16& input_text, | 1199 const string16& input_text, |
1202 bool is_keyword) { | 1200 bool is_keyword) { |
1203 AutocompleteClassifier* classifier = | 1201 AutocompleteClassifier* classifier = |
1204 AutocompleteClassifierFactory::GetForProfile(profile_); | 1202 AutocompleteClassifierFactory::GetForProfile(profile_); |
1205 SuggestResults scored_results; | 1203 SuggestResults scored_results; |
1204 const bool prevent_search_history_inlining = | |
1205 OmniboxFieldTrial::SearchHistoryPreventInlining( | |
1206 input_.current_page_classification()); | |
1206 for (HistoryResults::const_iterator i(results.begin()); i != results.end(); | 1207 for (HistoryResults::const_iterator i(results.begin()); i != results.end(); |
1207 ++i) { | 1208 ++i) { |
1208 // Don't autocomplete multi-word queries that have only been seen once | 1209 // Don't autocomplete multi-word queries that have only been seen once |
1209 // unless the user has typed more than one word. | 1210 // unless the user has typed more than one word. |
1210 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || | 1211 bool prevent_inline_autocomplete = base_prevent_inline_autocomplete || |
1211 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term)); | 1212 (!input_multiple_words && (i->visits < 2) && HasMultipleWords(i->term)); |
1212 | 1213 |
1213 // Don't autocomplete search terms that would normally be treated as URLs | 1214 // Don't autocomplete search terms that would normally be treated as URLs |
1214 // when typed. For example, if the user searched for "google.com" and types | 1215 // when typed. For example, if the user searched for "google.com" and types |
1215 // "goog", don't autocomplete to the search term "google.com". Otherwise, | 1216 // "goog", don't autocomplete to the search term "google.com". Otherwise, |
1216 // the input will look like a URL but act like a search, which is confusing. | 1217 // the input will look like a URL but act like a search, which is confusing. |
1217 // NOTE: We don't check this in the following cases: | 1218 // NOTE: We don't check this in the following cases: |
1218 // * When inline autocomplete is disabled, we won't be inline | 1219 // * When inline autocomplete is disabled, we won't be inline |
1219 // autocompleting this term, so we don't need to worry about confusion as | 1220 // autocompleting this term, so we don't need to worry about confusion as |
1220 // much. This also prevents calling Classify() again from inside the | 1221 // much. This also prevents calling Classify() again from inside the |
1221 // classifier (which will corrupt state and likely crash), since the | 1222 // classifier (which will corrupt state and likely crash), since the |
1222 // classifier always disables inline autocomplete. | 1223 // classifier always disables inline autocomplete. |
1223 // * When the user has typed the whole term, the "what you typed" history | 1224 // * When the user has typed the whole term, the "what you typed" history |
1224 // match will outrank us for URL-like inputs anyway, so we need not do | 1225 // match will outrank us for URL-like inputs anyway, so we need not do |
1225 // anything special. | 1226 // anything special. |
1226 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) { | 1227 if (!prevent_inline_autocomplete && classifier && (i->term != input_text)) { |
1227 AutocompleteMatch match; | 1228 AutocompleteMatch match; |
1228 classifier->Classify(i->term, false, false, &match, NULL); | 1229 classifier->Classify(i->term, false, false, &match, NULL); |
1229 prevent_inline_autocomplete = | 1230 prevent_inline_autocomplete = |
1230 !AutocompleteMatch::IsSearchType(match.type); | 1231 !AutocompleteMatch::IsSearchType(match.type); |
1231 } | 1232 } |
1232 | 1233 |
1233 int relevance = CalculateRelevanceForHistory(i->time, is_keyword, | 1234 int relevance = CalculateRelevanceForHistory( |
1234 prevent_inline_autocomplete); | 1235 i->time, is_keyword, prevent_inline_autocomplete, |
1236 prevent_search_history_inlining); | |
1235 scored_results.push_back( | 1237 scored_results.push_back( |
1236 SuggestResult(i->term, is_keyword, relevance, false)); | 1238 SuggestResult(i->term, is_keyword, relevance, false)); |
1237 } | 1239 } |
1238 | 1240 |
1239 // History returns results sorted for us. However, we may have docked some | 1241 // History returns results sorted for us. However, we may have docked some |
1240 // results' scores, so things are no longer in order. Do a stable sort to get | 1242 // results' scores, so things are no longer in order. Do a stable sort to get |
1241 // things back in order without otherwise disturbing results with equal | 1243 // things back in order without otherwise disturbing results with equal |
1242 // scores, then force the scores to be unique, so that the order in which | 1244 // scores, then force the scores to be unique, so that the order in which |
1243 // they're shown is deterministic. | 1245 // they're shown is deterministic. |
1244 std::stable_sort(scored_results.begin(), scored_results.end(), | 1246 std::stable_sort(scored_results.begin(), scored_results.end(), |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1329 *relevance_from_server = use_server_relevance; | 1331 *relevance_from_server = use_server_relevance; |
1330 return use_server_relevance ? | 1332 return use_server_relevance ? |
1331 keyword_results_.verbatim_relevance : | 1333 keyword_results_.verbatim_relevance : |
1332 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(), | 1334 CalculateRelevanceForKeywordVerbatim(keyword_input_.type(), |
1333 keyword_input_.prefer_keyword()); | 1335 keyword_input_.prefer_keyword()); |
1334 } | 1336 } |
1335 | 1337 |
1336 int SearchProvider::CalculateRelevanceForHistory( | 1338 int SearchProvider::CalculateRelevanceForHistory( |
1337 const base::Time& time, | 1339 const base::Time& time, |
1338 bool is_keyword, | 1340 bool is_keyword, |
1339 bool prevent_inline_autocomplete) const { | 1341 bool prevent_inline_autocomplete, |
1342 bool prevent_search_history_inlining) const { | |
1340 // The relevance of past searches falls off over time. There are two distinct | 1343 // The relevance of past searches falls off over time. There are two distinct |
1341 // equations used. If the first equation is used (searches to the primary | 1344 // equations used. If the first equation is used (searches to the primary |
1342 // provider that we want to inline autocomplete), the score is in the range | 1345 // provider that we want to inline autocomplete), the score is in the range |
1343 // 1300-1599 (unless |prevent_search_history_inlining_|, in which case | 1346 // 1300-1599 (unless |prevent_search_history_inlining|, in which case |
1344 // it's in the range 1200-1299). If the second equation is used the | 1347 // it's in the range 1200-1299). If the second equation is used the |
1345 // relevance of a search 15 minutes ago is discounted 50 points, while the | 1348 // relevance of a search 15 minutes ago is discounted 50 points, while the |
1346 // relevance of a search two weeks ago is discounted 450 points. | 1349 // relevance of a search two weeks ago is discounted 450 points. |
1347 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0); | 1350 double elapsed_time = std::max((base::Time::Now() - time).InSecondsF(), 0.0); |
1348 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider(); | 1351 bool is_primary_provider = is_keyword || !providers_.has_keyword_provider(); |
1349 if (is_primary_provider && !prevent_inline_autocomplete) { | 1352 if (is_primary_provider && !prevent_inline_autocomplete) { |
1350 // Searches with the past two days get a different curve. | 1353 // Searches with the past two days get a different curve. |
1351 const double autocomplete_time = 2 * 24 * 60 * 60; | 1354 const double autocomplete_time = 2 * 24 * 60 * 60; |
1352 if (elapsed_time < autocomplete_time) { | 1355 if (elapsed_time < autocomplete_time) { |
1353 int max_score = is_keyword ? 1599 : 1399; | 1356 int max_score = is_keyword ? 1599 : 1399; |
1354 if (prevent_search_history_inlining_) | 1357 if (prevent_search_history_inlining) |
1355 max_score = 1299; | 1358 max_score = 1299; |
1356 return max_score - static_cast<int>(99 * | 1359 return max_score - static_cast<int>(99 * |
1357 std::pow(elapsed_time / autocomplete_time, 2.5)); | 1360 std::pow(elapsed_time / autocomplete_time, 2.5)); |
1358 } | 1361 } |
1359 elapsed_time -= autocomplete_time; | 1362 elapsed_time -= autocomplete_time; |
1360 } | 1363 } |
1361 | 1364 |
1362 const int score_discount = | 1365 const int score_discount = |
1363 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); | 1366 static_cast<int>(6.5 * std::pow(elapsed_time, 0.3)); |
1364 | 1367 |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1514 it->set_relevance(max_query_relevance); | 1517 it->set_relevance(max_query_relevance); |
1515 it->set_relevance_from_server(relevance_from_server); | 1518 it->set_relevance_from_server(relevance_from_server); |
1516 } | 1519 } |
1517 } | 1520 } |
1518 | 1521 |
1519 void SearchProvider::UpdateDone() { | 1522 void SearchProvider::UpdateDone() { |
1520 // We're done when the timer isn't running, there are no suggest queries | 1523 // We're done when the timer isn't running, there are no suggest queries |
1521 // pending, and we're not waiting on Instant. | 1524 // pending, and we're not waiting on Instant. |
1522 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); | 1525 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
1523 } | 1526 } |
OLD | NEW |