Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: chrome/browser/autocomplete/search_provider.cc

Issue 67693004: Omnibox: Don't Let Users Escape Keyword Mode Accidentally (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Peter's latest comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 1289 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 ACMatches::const_iterator SearchProvider::FindTopMatch( 1300 ACMatches::const_iterator SearchProvider::FindTopMatch(
1301 bool autocomplete_result_will_reorder_for_default_match) const { 1301 bool autocomplete_result_will_reorder_for_default_match) const {
1302 if (!autocomplete_result_will_reorder_for_default_match) 1302 if (!autocomplete_result_will_reorder_for_default_match)
1303 return matches_.begin(); 1303 return matches_.begin();
1304 ACMatches::const_iterator it = matches_.begin(); 1304 ACMatches::const_iterator it = matches_.begin();
1305 while ((it != matches_.end()) && !it->allowed_to_be_default_match) 1305 while ((it != matches_.end()) && !it->allowed_to_be_default_match)
1306 ++it; 1306 ++it;
1307 return it; 1307 return it;
1308 } 1308 }
1309 1309
1310 bool SearchProvider::IsTopMatchNavigationInKeywordMode( 1310 bool SearchProvider::IsTopMatchNavigation(
msw 2013/11/20 21:05:03 Keep this as IsTopMatchNavigationInKeywordMode. I
Mark P 2013/11/21 21:30:22 Okay. I don't feel strongly.
1311 bool autocomplete_result_will_reorder_for_default_match) const { 1311 bool autocomplete_result_will_reorder_for_default_match) const {
1312 ACMatches::const_iterator first_match = 1312 ACMatches::const_iterator first_match =
1313 FindTopMatch(autocomplete_result_will_reorder_for_default_match); 1313 FindTopMatch(autocomplete_result_will_reorder_for_default_match);
1314 return !providers_.keyword_provider().empty() && 1314 return (first_match != matches_.end()) &&
1315 (first_match != matches_.end()) &&
1316 (first_match->type == AutocompleteMatchType::NAVSUGGEST); 1315 (first_match->type == AutocompleteMatchType::NAVSUGGEST);
1317 } 1316 }
1318 1317
1318 bool SearchProvider::HasKeywordMatchThatCanBeDefault() const {
msw 2013/11/20 21:05:03 Rename this to HasValidDefaultMatchInKeywordMode t
Mark P 2013/11/21 21:30:22 Revised it to something similar to your suggested
1319 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
1320 if (keyword_url == NULL) // not in keyword mode -> say everything's okay
msw 2013/11/20 21:05:03 nit: Please make this comment more formal.
Mark P 2013/11/21 21:30:22 Done.
1321 return true;
1322 for (ACMatches::const_iterator it = matches_.begin(); it != matches_.end();
1323 ++it) {
1324 if ((it->keyword == keyword_url->keyword()) &&
1325 it->allowed_to_be_default_match)
1326 return true;
1327 }
1328 return false;
1329 }
1330
1319 bool SearchProvider::IsTopMatchScoreTooLow( 1331 bool SearchProvider::IsTopMatchScoreTooLow(
1320 bool autocomplete_result_will_reorder_for_default_match) const { 1332 bool autocomplete_result_will_reorder_for_default_match) const {
1321 // In reorder mode, there's no such thing as a score that's too low. 1333 // In reorder mode, there's no such thing as a score that's too low.
1322 if (autocomplete_result_will_reorder_for_default_match) 1334 if (autocomplete_result_will_reorder_for_default_match)
1323 return false; 1335 return false;
1324 1336
1325 // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState() 1337 // Here we use CalculateRelevanceForVerbatimIgnoringKeywordModeState()
1326 // rather than CalculateRelevanceForVerbatim() because the latter returns 1338 // rather than CalculateRelevanceForVerbatim() because the latter returns
1327 // a very low score (250) if keyword mode is active. This is because 1339 // a very low score (250) if keyword mode is active. This is because
1328 // when keyword mode is active the user probably wants the keyword matches, 1340 // when keyword mode is active the user probably wants the keyword matches,
(...skipping 28 matching lines...) Expand all
1357 if (it->allowed_to_be_default_match) 1369 if (it->allowed_to_be_default_match)
1358 return true; 1370 return true;
1359 if (!autocomplete_result_will_reorder_for_default_match) 1371 if (!autocomplete_result_will_reorder_for_default_match)
1360 return false; 1372 return false;
1361 } 1373 }
1362 return false; 1374 return false;
1363 } 1375 }
1364 1376
1365 void SearchProvider::UpdateMatches() { 1377 void SearchProvider::UpdateMatches() {
1366 base::TimeTicks update_matches_start_time(base::TimeTicks::Now()); 1378 base::TimeTicks update_matches_start_time(base::TimeTicks::Now());
1379 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
1367 ConvertResultsToAutocompleteMatches(); 1380 ConvertResultsToAutocompleteMatches();
1368 1381
1382 // True if the omnibox will reorder matches as necessary to make the first
msw 2013/11/20 21:05:03 nit: you don't check this outside the if block; le
Mark P 2013/11/21 21:30:22 Good point. That code movement comes from an earl
1383 // one something that is allowed to be the default match.
1384 const bool omnibox_will_reorder_for_legal_default_match =
1385 OmniboxFieldTrial::ReorderForLegalDefaultMatch(
1386 input_.current_page_classification());
1387
1369 // Check constraints that may be violated by suggested relevances. 1388 // Check constraints that may be violated by suggested relevances.
1370 if (!matches_.empty() && 1389 if (!matches_.empty() &&
1371 (default_results_.HasServerProvidedScores() || 1390 (default_results_.HasServerProvidedScores() ||
1372 keyword_results_.HasServerProvidedScores())) { 1391 keyword_results_.HasServerProvidedScores())) {
1373 // These blocks attempt to repair undesirable behavior by suggested 1392 // These blocks attempt to repair undesirable behavior by suggested
1374 // relevances with minimal impact, preserving other suggested relevances. 1393 // relevances with minimal impact, preserving other suggested relevances.
1375 1394
1376 // True if the omnibox will reorder matches as necessary to make the first 1395 if ((keyword_url != NULL) &&
1377 // one something that is allowed to be the default match. 1396 IsTopMatchNavigation(omnibox_will_reorder_for_legal_default_match)) {
1378 const bool omnibox_will_reorder_for_legal_default_match =
1379 OmniboxFieldTrial::ReorderForLegalDefaultMatch(
1380 input_.current_page_classification());
1381 if (IsTopMatchNavigationInKeywordMode(
1382 omnibox_will_reorder_for_legal_default_match)) {
1383 // Correct the suggested relevance scores if the top match is a 1397 // Correct the suggested relevance scores if the top match is a
1384 // navigation in keyword mode, since inlining a navigation match 1398 // navigation in keyword mode, since inlining a navigation match
1385 // would break the user out of keyword mode. By the way, if the top 1399 // would break the user out of keyword mode. This will only be
1386 // match is a non-keyword match (query or navsuggestion) in keyword 1400 // triggered in regular (non-reorder) mode; in reorder mode,
1387 // mode, the user would also break out of keyword mode. However, 1401 // navigation matches are marked as not allowed to be the default
1388 // that situation is impossible given the current scoring paradigm 1402 // match and hence IsTopMatchNavigation() will always return false.
1389 // and the fact that only one search engine (Google) provides suggested 1403 DCHECK(!omnibox_will_reorder_for_legal_default_match);
1390 // relevance scores at this time.
1391 DemoteKeywordNavigationMatchesPastTopQuery(); 1404 DemoteKeywordNavigationMatchesPastTopQuery();
1392 ConvertResultsToAutocompleteMatches(); 1405 ConvertResultsToAutocompleteMatches();
1393 DCHECK(!IsTopMatchNavigationInKeywordMode( 1406 DCHECK(!IsTopMatchNavigation(
msw 2013/11/20 21:05:03 nit: remove this DCHECK; it's redundant with the o
Mark P 2013/11/21 21:30:22 I'd prefer to keep it to verify that DemoteKeyword
msw 2013/11/21 22:11:11 Good call!
Mark P 2013/11/22 01:17:57 Acknowledged.
1394 omnibox_will_reorder_for_legal_default_match)); 1407 omnibox_will_reorder_for_legal_default_match));
1395 } 1408 }
1409 if ((keyword_url != NULL) && !HasKeywordMatchThatCanBeDefault()) {
msw 2013/11/20 21:05:03 Like IsTopMatchNavigation[InKeywordMode], I'd pref
Mark P 2013/11/21 21:30:22 Okay.
1410 // In keyword mode, disregard the keyword verbatim suggested relevance
1411 // if necessary so there at least one keyword match that's allowed to
1412 // be the default match.
1413 keyword_results_.verbatim_relevance = -1;
1414 ConvertResultsToAutocompleteMatches();
1415 }
1396 if (IsTopMatchScoreTooLow(omnibox_will_reorder_for_legal_default_match)) { 1416 if (IsTopMatchScoreTooLow(omnibox_will_reorder_for_legal_default_match)) {
1397 // Disregard the suggested verbatim relevance if the top score is below 1417 // Disregard the suggested verbatim relevance if the top score is below
1398 // the usual verbatim value. For example, a BarProvider may rely on 1418 // the usual verbatim value. For example, a BarProvider may rely on
1399 // SearchProvider's verbatim or inlineable matches for input "foo" (all 1419 // SearchProvider's verbatim or inlineable matches for input "foo" (all
1400 // allowed to be default match) to always outrank its own lowly-ranked 1420 // allowed to be default match) to always outrank its own lowly-ranked
1401 // "bar" matches that shouldn't be the default match. 1421 // "bar" matches that shouldn't be the default match.
1402 default_results_.verbatim_relevance = -1; 1422 default_results_.verbatim_relevance = -1;
1403 keyword_results_.verbatim_relevance = -1; 1423 keyword_results_.verbatim_relevance = -1;
1404 ConvertResultsToAutocompleteMatches(); 1424 ConvertResultsToAutocompleteMatches();
1405 } 1425 }
(...skipping 16 matching lines...) Expand all
1422 // match or inlinable). For example, input "foo" should not invoke a 1442 // match or inlinable). For example, input "foo" should not invoke a
1423 // search for "bar", which would happen if the "bar" search match 1443 // search for "bar", which would happen if the "bar" search match
1424 // outranked all other matches. On the other hand, if the omnibox will 1444 // outranked all other matches. On the other hand, if the omnibox will
1425 // reorder matches as necessary to put a legal default match at the top, 1445 // reorder matches as necessary to put a legal default match at the top,
1426 // all we need to guarantee is that SearchProvider returns a legal 1446 // all we need to guarantee is that SearchProvider returns a legal
1427 // default match. (The omnibox always needs at least one legal default 1447 // default match. (The omnibox always needs at least one legal default
1428 // match, and it relies on SearchProvider to always return one.) 1448 // match, and it relies on SearchProvider to always return one.)
1429 ApplyCalculatedRelevance(); 1449 ApplyCalculatedRelevance();
1430 ConvertResultsToAutocompleteMatches(); 1450 ConvertResultsToAutocompleteMatches();
1431 } 1451 }
1432 DCHECK(!IsTopMatchNavigationInKeywordMode( 1452 DCHECK((keyword_url == NULL) ||
1433 omnibox_will_reorder_for_legal_default_match)); 1453 !IsTopMatchNavigation(omnibox_will_reorder_for_legal_default_match));
1454 DCHECK((keyword_url == NULL) || HasKeywordMatchThatCanBeDefault());
1434 DCHECK(!IsTopMatchScoreTooLow( 1455 DCHECK(!IsTopMatchScoreTooLow(
1435 omnibox_will_reorder_for_legal_default_match)); 1456 omnibox_will_reorder_for_legal_default_match));
1436 DCHECK(!IsTopMatchSearchWithURLInput( 1457 DCHECK(!IsTopMatchSearchWithURLInput(
1437 omnibox_will_reorder_for_legal_default_match)); 1458 omnibox_will_reorder_for_legal_default_match));
1438 DCHECK(HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match)); 1459 DCHECK(HasValidDefaultMatch(omnibox_will_reorder_for_legal_default_match));
1439 } 1460 }
1440 1461
1462 if ((keyword_url != NULL) && HasKeywordMatchThatCanBeDefault()) {
1463 // If there is a keyword match that is allowed to be the default match,
1464 // then prohibit default provider matches from being the defaul match lest
msw 2013/11/20 21:05:03 nit: defaul->default
Mark P 2013/11/21 21:30:22 Done.
1465 // such matches cause the user to break out of keyword mode.
1466 for (ACMatches::iterator it = matches_.begin(); it != matches_.end();
1467 ++it) {
1468 if (it->keyword != keyword_url->keyword())
1469 it->allowed_to_be_default_match = false;
1470 }
1471 }
1472
1441 base::TimeTicks update_starred_start_time(base::TimeTicks::Now()); 1473 base::TimeTicks update_starred_start_time(base::TimeTicks::Now());
1442 UpdateStarredStateOfMatches(); 1474 UpdateStarredStateOfMatches();
1443 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime", 1475 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateStarredTime",
1444 base::TimeTicks::Now() - update_starred_start_time); 1476 base::TimeTicks::Now() - update_starred_start_time);
1445 UpdateDone(); 1477 UpdateDone();
1446 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateMatchesTime", 1478 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.UpdateMatchesTime",
1447 base::TimeTicks::Now() - update_matches_start_time); 1479 base::TimeTicks::Now() - update_matches_start_time);
1448 } 1480 }
1449 1481
1450 void SearchProvider::AddNavigationResultsToMatches( 1482 void SearchProvider::AddNavigationResultsToMatches(
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
1832 // Preserve the forced query '?' prefix in |match.fill_into_edit|. 1864 // Preserve the forced query '?' prefix in |match.fill_into_edit|.
1833 // Otherwise, user edits to a suggestion would show non-Search results. 1865 // Otherwise, user edits to a suggestion would show non-Search results.
1834 if (input_.type() == AutocompleteInput::FORCED_QUERY) { 1866 if (input_.type() == AutocompleteInput::FORCED_QUERY) {
1835 match.fill_into_edit.insert(0, ASCIIToUTF16("?")); 1867 match.fill_into_edit.insert(0, ASCIIToUTF16("?"));
1836 if (inline_autocomplete_offset != string16::npos) 1868 if (inline_autocomplete_offset != string16::npos)
1837 ++inline_autocomplete_offset; 1869 ++inline_autocomplete_offset;
1838 } 1870 }
1839 if (!input_.prevent_inline_autocomplete() && 1871 if (!input_.prevent_inline_autocomplete() &&
1840 (inline_autocomplete_offset != string16::npos)) { 1872 (inline_autocomplete_offset != string16::npos)) {
1841 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length()); 1873 DCHECK(inline_autocomplete_offset <= match.fill_into_edit.length());
1842 match.allowed_to_be_default_match = true; 1874 // A navsuggestion can only be the default match when there is no
1875 // keyword provider active, lest it appear first and break the user
1876 // out of keyword mode.
1877 match.allowed_to_be_default_match =
1878 (providers_.GetKeywordProviderURL() == NULL);
1843 match.inline_autocompletion = 1879 match.inline_autocompletion =
1844 match.fill_into_edit.substr(inline_autocomplete_offset); 1880 match.fill_into_edit.substr(inline_autocomplete_offset);
1845 } 1881 }
1846 1882
1847 match.contents = net::FormatUrl(navigation.url(), languages, 1883 match.contents = net::FormatUrl(navigation.url(), languages,
1848 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start); 1884 format_types, net::UnescapeRule::SPACES, NULL, NULL, &match_start);
1849 // If the first match in the untrimmed string was inside a scheme that we 1885 // If the first match in the untrimmed string was inside a scheme that we
1850 // trimmed, look for a subsequent match. 1886 // trimmed, look for a subsequent match.
1851 if (match_start == string16::npos) 1887 if (match_start == string16::npos)
1852 match_start = match.contents.find(input); 1888 match_start = match.contents.find(input);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1907 it->set_relevance(max_query_relevance); 1943 it->set_relevance(max_query_relevance);
1908 it->set_relevance_from_server(relevance_from_server); 1944 it->set_relevance_from_server(relevance_from_server);
1909 } 1945 }
1910 } 1946 }
1911 1947
1912 void SearchProvider::UpdateDone() { 1948 void SearchProvider::UpdateDone() {
1913 // We're done when the timer isn't running, there are no suggest queries 1949 // We're done when the timer isn't running, there are no suggest queries
1914 // pending, and we're not waiting on Instant. 1950 // pending, and we're not waiting on Instant.
1915 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); 1951 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
1916 } 1952 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698