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

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

Issue 54203008: Store xsrf token received with psuggest results. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added test case 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 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 202 }
203 203
204 204
205 // SearchProvider::SuggestResult ---------------------------------------------- 205 // SearchProvider::SuggestResult ----------------------------------------------
206 206
207 SearchProvider::SuggestResult::SuggestResult( 207 SearchProvider::SuggestResult::SuggestResult(
208 const string16& suggestion, 208 const string16& suggestion,
209 const string16& match_contents, 209 const string16& match_contents,
210 const string16& annotation, 210 const string16& annotation,
211 const std::string& suggest_query_params, 211 const std::string& suggest_query_params,
212 const std::string& xsrf_token,
212 bool from_keyword_provider, 213 bool from_keyword_provider,
213 int relevance, 214 int relevance,
214 bool relevance_from_server, 215 bool relevance_from_server,
215 bool should_prefetch) 216 bool should_prefetch)
216 : Result(from_keyword_provider, relevance, relevance_from_server), 217 : Result(from_keyword_provider, relevance, relevance_from_server),
217 suggestion_(suggestion), 218 suggestion_(suggestion),
218 match_contents_(match_contents), 219 match_contents_(match_contents),
219 annotation_(annotation), 220 annotation_(annotation),
220 suggest_query_params_(suggest_query_params), 221 suggest_query_params_(suggest_query_params),
222 xsrf_token_(xsrf_token),
221 should_prefetch_(should_prefetch) { 223 should_prefetch_(should_prefetch) {
222 } 224 }
223 225
224 SearchProvider::SuggestResult::~SuggestResult() { 226 SearchProvider::SuggestResult::~SuggestResult() {
225 } 227 }
226 228
227 bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const { 229 bool SearchProvider::SuggestResult::IsInlineable(const string16& input) const {
228 return StartsWith(suggestion_, input, false); 230 return StartsWith(suggestion_, input, false);
229 } 231 }
230 232
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 const AutocompleteInput& input, 347 const AutocompleteInput& input,
346 const string16& input_text, 348 const string16& input_text,
347 int relevance, 349 int relevance,
348 AutocompleteMatch::Type type, 350 AutocompleteMatch::Type type,
349 bool is_keyword, 351 bool is_keyword,
350 const string16& match_contents, 352 const string16& match_contents,
351 const string16& annotation, 353 const string16& annotation,
352 const TemplateURL* template_url, 354 const TemplateURL* template_url,
353 const string16& query_string, 355 const string16& query_string,
354 const std::string& suggest_query_params, 356 const std::string& suggest_query_params,
357 const std::string& xsrf_token,
355 int accepted_suggestion, 358 int accepted_suggestion,
356 int omnibox_start_margin, 359 int omnibox_start_margin,
357 bool append_extra_query_params) { 360 bool append_extra_query_params) {
358 AutocompleteMatch match(autocomplete_provider, relevance, false, type); 361 AutocompleteMatch match(autocomplete_provider, relevance, false, type);
359 362
360 if (!template_url) 363 if (!template_url)
361 return match; 364 return match;
362 match.keyword = template_url->keyword(); 365 match.keyword = template_url->keyword();
363 366
364 SetAndClassifyMatchContents( 367 SetAndClassifyMatchContents(
(...skipping 18 matching lines...) Expand all
383 const TemplateURLRef& search_url = template_url->url_ref(); 386 const TemplateURLRef& search_url = template_url->url_ref();
384 DCHECK(search_url.SupportsReplacement()); 387 DCHECK(search_url.SupportsReplacement());
385 match.search_terms_args.reset( 388 match.search_terms_args.reset(
386 new TemplateURLRef::SearchTermsArgs(query_string)); 389 new TemplateURLRef::SearchTermsArgs(query_string));
387 match.search_terms_args->original_query = input_text; 390 match.search_terms_args->original_query = input_text;
388 match.search_terms_args->accepted_suggestion = accepted_suggestion; 391 match.search_terms_args->accepted_suggestion = accepted_suggestion;
389 match.search_terms_args->omnibox_start_margin = omnibox_start_margin; 392 match.search_terms_args->omnibox_start_margin = omnibox_start_margin;
390 match.search_terms_args->suggest_query_params = suggest_query_params; 393 match.search_terms_args->suggest_query_params = suggest_query_params;
391 match.search_terms_args->append_extra_query_params = 394 match.search_terms_args->append_extra_query_params =
392 append_extra_query_params; 395 append_extra_query_params;
396 match.search_terms_args->xsrf_token = xsrf_token;
393 // This is the destination URL sans assisted query stats. This must be set 397 // This is the destination URL sans assisted query stats. This must be set
394 // so the AutocompleteController can properly de-dupe; the controller will 398 // so the AutocompleteController can properly de-dupe; the controller will
395 // eventually overwrite it before it reaches the user. 399 // eventually overwrite it before it reaches the user.
396 match.destination_url = 400 match.destination_url =
397 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get())); 401 GURL(search_url.ReplaceSearchTerms(*match.search_terms_args.get()));
398 402
399 // Search results don't look like URLs. 403 // Search results don't look like URLs.
400 match.transition = is_keyword ? 404 match.transition = is_keyword ?
401 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED; 405 content::PAGE_TRANSITION_KEYWORD : content::PAGE_TRANSITION_GENERATED;
402 406
(...skipping 632 matching lines...) Expand 10 before | Expand all | Expand 10 after
1035 results->navigation_results.push_back(NavigationResult( 1039 results->navigation_results.push_back(NavigationResult(
1036 *this, url, title, is_keyword, relevance, true)); 1040 *this, url, title, is_keyword, relevance, true));
1037 } 1041 }
1038 } else { 1042 } else {
1039 bool should_prefetch = static_cast<int>(index) == prefetch_index; 1043 bool should_prefetch = static_cast<int>(index) == prefetch_index;
1040 DictionaryValue* suggestion_detail = NULL; 1044 DictionaryValue* suggestion_detail = NULL;
1041 string16 match_contents = suggestion; 1045 string16 match_contents = suggestion;
1042 string16 disambiguating_query; 1046 string16 disambiguating_query;
1043 string16 annotation; 1047 string16 annotation;
1044 std::string suggest_query_params; 1048 std::string suggest_query_params;
1049 std::string xsrf_token;
1045 if (suggestion_details && (type == "ENTITY") && 1050 if (suggestion_details && (type == "ENTITY") &&
1046 suggestion_details->GetDictionary(index, &suggestion_detail) && 1051 suggestion_details->GetDictionary(index, &suggestion_detail) &&
1047 suggestion_detail) { 1052 suggestion_detail) {
1048 suggestion_detail->GetString("a", &annotation); 1053 suggestion_detail->GetString("a", &annotation);
1049 if (suggestion_detail->GetString("dq", &disambiguating_query) && 1054 if (suggestion_detail->GetString("dq", &disambiguating_query) &&
1050 !disambiguating_query.empty()) 1055 !disambiguating_query.empty())
1051 suggestion = disambiguating_query; 1056 suggestion = disambiguating_query;
1052 suggestion_detail->GetString("q", &suggest_query_params); 1057 suggestion_detail->GetString("q", &suggest_query_params);
1058 } else if (suggestion_details && (type == "PERSONALIZED_QUERY") &&
1059 suggestion_details->GetDictionary(index, &suggestion_detail) &&
1060 suggestion_detail) {
1061 suggestion_detail->GetString("x", &xsrf_token);
1053 } 1062 }
1054 // TODO(kochi): Improve calculator suggestion presentation. 1063 // TODO(kochi): Improve calculator suggestion presentation.
1055 results->suggest_results.push_back(SuggestResult( 1064 results->suggest_results.push_back(SuggestResult(
1056 suggestion, match_contents, annotation, suggest_query_params, 1065 suggestion, match_contents, annotation, suggest_query_params,
1057 is_keyword, relevance, true, should_prefetch)); 1066 xsrf_token, is_keyword, relevance, true, should_prefetch));
1058 } 1067 }
1059 } 1068 }
1060 1069
1061 // Apply calculated relevance scores if a valid list was not provided. 1070 // Apply calculated relevance scores if a valid list was not provided.
1062 if (relevances == NULL) { 1071 if (relevances == NULL) {
1063 ApplyCalculatedSuggestRelevance(&results->suggest_results); 1072 ApplyCalculatedSuggestRelevance(&results->suggest_results);
1064 ApplyCalculatedNavigationRelevance(&results->navigation_results); 1073 ApplyCalculatedNavigationRelevance(&results->navigation_results);
1065 } 1074 }
1066 // Keep the result lists sorted. 1075 // Keep the result lists sorted.
1067 const CompareScoredResults comparator = CompareScoredResults(); 1076 const CompareScoredResults comparator = CompareScoredResults();
(...skipping 29 matching lines...) Expand all
1097 relevance_from_server, 1106 relevance_from_server,
1098 false, 1107 false,
1099 std::string(), 1108 std::string(),
1100 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED, 1109 AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED,
1101 false, 1110 false,
1102 input_.text(), 1111 input_.text(),
1103 string16(), 1112 string16(),
1104 input_.text(), 1113 input_.text(),
1105 did_not_accept_default_suggestion, 1114 did_not_accept_default_suggestion,
1106 std::string(), 1115 std::string(),
1116 std::string(),
1107 &map); 1117 &map);
1108 } 1118 }
1109 if (!keyword_input_.text().empty()) { 1119 if (!keyword_input_.text().empty()) {
1110 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL(); 1120 const TemplateURL* keyword_url = providers_.GetKeywordProviderURL();
1111 // We only create the verbatim search query match for a keyword 1121 // We only create the verbatim search query match for a keyword
1112 // if it's not an extension keyword. Extension keywords are handled 1122 // if it's not an extension keyword. Extension keywords are handled
1113 // in KeywordProvider::Start(). (Extensions are complicated...) 1123 // in KeywordProvider::Start(). (Extensions are complicated...)
1114 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond 1124 // Note: in this provider, SEARCH_OTHER_ENGINE must correspond
1115 // to the keyword verbatim search query. Do not create other matches 1125 // to the keyword verbatim search query. Do not create other matches
1116 // of type SEARCH_OTHER_ENGINE. 1126 // of type SEARCH_OTHER_ENGINE.
1117 if (keyword_url && 1127 if (keyword_url &&
1118 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) { 1128 (keyword_url->GetType() != TemplateURL::OMNIBOX_API_EXTENSION)) {
1119 bool keyword_relevance_from_server; 1129 bool keyword_relevance_from_server;
1120 const int keyword_verbatim_relevance = 1130 const int keyword_verbatim_relevance =
1121 GetKeywordVerbatimRelevance(&keyword_relevance_from_server); 1131 GetKeywordVerbatimRelevance(&keyword_relevance_from_server);
1122 if (keyword_verbatim_relevance > 0) { 1132 if (keyword_verbatim_relevance > 0) {
1123 AddMatchToMap(keyword_input_.text(), 1133 AddMatchToMap(keyword_input_.text(),
1124 keyword_verbatim_relevance, 1134 keyword_verbatim_relevance,
1125 keyword_relevance_from_server, 1135 keyword_relevance_from_server,
1126 false, 1136 false,
1127 std::string(), 1137 std::string(),
1128 AutocompleteMatchType::SEARCH_OTHER_ENGINE, 1138 AutocompleteMatchType::SEARCH_OTHER_ENGINE,
1129 true, 1139 true,
1130 keyword_input_.text(), 1140 keyword_input_.text(),
1131 string16(), 1141 string16(),
1132 keyword_input_.text(), 1142 keyword_input_.text(),
1133 did_not_accept_keyword_suggestion, 1143 did_not_accept_keyword_suggestion,
1134 std::string(), 1144 std::string(),
1145 std::string(),
1135 &map); 1146 &map);
1136 } 1147 }
1137 } 1148 }
1138 } 1149 }
1139 AddHistoryResultsToMap(keyword_history_results_, true, 1150 AddHistoryResultsToMap(keyword_history_results_, true,
1140 did_not_accept_keyword_suggestion, &map); 1151 did_not_accept_keyword_suggestion, &map);
1141 AddHistoryResultsToMap(default_history_results_, false, 1152 AddHistoryResultsToMap(default_history_results_, false,
1142 did_not_accept_default_suggestion, &map); 1153 did_not_accept_default_suggestion, &map);
1143 1154
1144 AddSuggestResultsToMap(keyword_results_.suggest_results, 1155 AddSuggestResultsToMap(keyword_results_.suggest_results,
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 false, 1384 false,
1374 false, 1385 false,
1375 std::string(), 1386 std::string(),
1376 AutocompleteMatchType::SEARCH_HISTORY, 1387 AutocompleteMatchType::SEARCH_HISTORY,
1377 is_keyword, 1388 is_keyword,
1378 i->suggestion(), 1389 i->suggestion(),
1379 string16(), 1390 string16(),
1380 i->suggestion(), 1391 i->suggestion(),
1381 did_not_accept_suggestion, 1392 did_not_accept_suggestion,
1382 std::string(), 1393 std::string(),
1394 std::string(),
1383 map); 1395 map);
1384 } 1396 }
1385 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime", 1397 UMA_HISTOGRAM_TIMES("Omnibox.SearchProvider.AddHistoryResultsTime",
1386 base::TimeTicks::Now() - start_time); 1398 base::TimeTicks::Now() - start_time);
1387 } 1399 }
1388 1400
1389 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults( 1401 SearchProvider::SuggestResults SearchProvider::ScoreHistoryResults(
1390 const HistoryResults& results, 1402 const HistoryResults& results,
1391 bool base_prevent_inline_autocomplete, 1403 bool base_prevent_inline_autocomplete,
1392 bool input_multiple_words, 1404 bool input_multiple_words,
(...skipping 30 matching lines...) Expand all
1423 classifier->Classify(i->term, false, false, &match, NULL); 1435 classifier->Classify(i->term, false, false, &match, NULL);
1424 prevent_inline_autocomplete = 1436 prevent_inline_autocomplete =
1425 !AutocompleteMatch::IsSearchType(match.type); 1437 !AutocompleteMatch::IsSearchType(match.type);
1426 } 1438 }
1427 1439
1428 int relevance = CalculateRelevanceForHistory( 1440 int relevance = CalculateRelevanceForHistory(
1429 i->time, is_keyword, !prevent_inline_autocomplete, 1441 i->time, is_keyword, !prevent_inline_autocomplete,
1430 prevent_search_history_inlining); 1442 prevent_search_history_inlining);
1431 scored_results.push_back( 1443 scored_results.push_back(
1432 SuggestResult(i->term, string16(), string16(), std::string(), 1444 SuggestResult(i->term, string16(), string16(), std::string(),
1433 is_keyword, relevance, false, false)); 1445 std::string(), is_keyword, relevance, false, false));
1434 } 1446 }
1435 1447
1436 // History returns results sorted for us. However, we may have docked some 1448 // History returns results sorted for us. However, we may have docked some
1437 // results' scores, so things are no longer in order. Do a stable sort to get 1449 // results' scores, so things are no longer in order. Do a stable sort to get
1438 // things back in order without otherwise disturbing results with equal 1450 // things back in order without otherwise disturbing results with equal
1439 // scores, then force the scores to be unique, so that the order in which 1451 // scores, then force the scores to be unique, so that the order in which
1440 // they're shown is deterministic. 1452 // they're shown is deterministic.
1441 std::stable_sort(scored_results.begin(), scored_results.end(), 1453 std::stable_sort(scored_results.begin(), scored_results.end(),
1442 CompareScoredResults()); 1454 CompareScoredResults());
1443 int last_relevance = 0; 1455 int last_relevance = 0;
(...skipping 18 matching lines...) Expand all
1462 results[i].relevance_from_server(), 1474 results[i].relevance_from_server(),
1463 results[i].should_prefetch(), 1475 results[i].should_prefetch(),
1464 metadata, 1476 metadata,
1465 AutocompleteMatchType::SEARCH_SUGGEST, 1477 AutocompleteMatchType::SEARCH_SUGGEST,
1466 is_keyword, 1478 is_keyword,
1467 results[i].match_contents(), 1479 results[i].match_contents(),
1468 results[i].annotation(), 1480 results[i].annotation(),
1469 results[i].suggestion(), 1481 results[i].suggestion(),
1470 i, 1482 i,
1471 results[i].suggest_query_params(), 1483 results[i].suggest_query_params(),
1484 results[i].xsrf_token(),
1472 map); 1485 map);
1473 } 1486 }
1474 } 1487 }
1475 1488
1476 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const { 1489 int SearchProvider::GetVerbatimRelevance(bool* relevance_from_server) const {
1477 // Use the suggested verbatim relevance score if it is non-negative (valid), 1490 // Use the suggested verbatim relevance score if it is non-negative (valid),
1478 // if inline autocomplete isn't prevented (always show verbatim on backspace), 1491 // if inline autocomplete isn't prevented (always show verbatim on backspace),
1479 // and if it won't suppress verbatim, leaving no default provider matches. 1492 // and if it won't suppress verbatim, leaving no default provider matches.
1480 // Otherwise, if the default provider returned no matches and was still able 1493 // Otherwise, if the default provider returned no matches and was still able
1481 // to suppress verbatim, the user would have no search/nav matches and may be 1494 // to suppress verbatim, the user would have no search/nav matches and may be
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 bool relevance_from_server, 1599 bool relevance_from_server,
1587 bool should_prefetch, 1600 bool should_prefetch,
1588 const std::string& metadata, 1601 const std::string& metadata,
1589 AutocompleteMatch::Type type, 1602 AutocompleteMatch::Type type,
1590 bool is_keyword, 1603 bool is_keyword,
1591 const string16& match_contents, 1604 const string16& match_contents,
1592 const string16& annotation, 1605 const string16& annotation,
1593 const string16& query_string, 1606 const string16& query_string,
1594 int accepted_suggestion, 1607 int accepted_suggestion,
1595 const std::string& suggest_query_params, 1608 const std::string& suggest_query_params,
1609 const std::string& xsrf_token,
1596 MatchMap* map) { 1610 MatchMap* map) {
1597 // On non-mobile, ask the instant controller for the appropriate start margin. 1611 // On non-mobile, ask the instant controller for the appropriate start margin.
1598 // On mobile the start margin is unused, so leave the value as default there. 1612 // On mobile the start margin is unused, so leave the value as default there.
1599 int omnibox_start_margin = chrome::kDisableStartMargin; 1613 int omnibox_start_margin = chrome::kDisableStartMargin;
1600 #if !defined(OS_ANDROID) && !defined(IOS) 1614 #if !defined(OS_ANDROID) && !defined(IOS)
1601 if (chrome::IsInstantExtendedAPIEnabled()) { 1615 if (chrome::IsInstantExtendedAPIEnabled()) {
1602 Browser* browser = 1616 Browser* browser =
1603 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop()); 1617 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop());
1604 if (browser && browser->instant_controller() && 1618 if (browser && browser->instant_controller() &&
1605 browser->instant_controller()->instant()) { 1619 browser->instant_controller()->instant()) {
1606 omnibox_start_margin = 1620 omnibox_start_margin =
1607 browser->instant_controller()->instant()->omnibox_bounds().x(); 1621 browser->instant_controller()->instant()->omnibox_bounds().x();
1608 } 1622 }
1609 } 1623 }
1610 #endif // !defined(OS_ANDROID) && !defined(IOS) 1624 #endif // !defined(OS_ANDROID) && !defined(IOS)
1611 1625
1612 const TemplateURL* template_url = is_keyword ? 1626 const TemplateURL* template_url = is_keyword ?
1613 providers_.GetKeywordProviderURL() : providers_.GetDefaultProviderURL(); 1627 providers_.GetKeywordProviderURL() : providers_.GetDefaultProviderURL();
1614 AutocompleteMatch match = CreateSearchSuggestion( 1628 AutocompleteMatch match = CreateSearchSuggestion(
1615 this, input_, input_text, relevance, type, is_keyword, match_contents, 1629 this, input_, input_text, relevance, type, is_keyword, match_contents,
1616 annotation, template_url, query_string, suggest_query_params, 1630 annotation, template_url, query_string, suggest_query_params,
1617 accepted_suggestion, omnibox_start_margin, 1631 xsrf_token, accepted_suggestion, omnibox_start_margin,
1618 !is_keyword || providers_.default_provider().empty()); 1632 !is_keyword || providers_.default_provider().empty());
1619 if (!match.destination_url.is_valid()) 1633 if (!match.destination_url.is_valid())
1620 return; 1634 return;
1621 match.search_terms_args->bookmark_bar_pinned = 1635 match.search_terms_args->bookmark_bar_pinned =
1622 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar); 1636 profile_->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar);
1623 match.RecordAdditionalInfo(kRelevanceFromServerKey, 1637 match.RecordAdditionalInfo(kRelevanceFromServerKey,
1624 relevance_from_server ? kTrue : kFalse); 1638 relevance_from_server ? kTrue : kFalse);
1625 match.RecordAdditionalInfo(kShouldPrefetchKey, 1639 match.RecordAdditionalInfo(kShouldPrefetchKey,
1626 should_prefetch ? kTrue : kFalse); 1640 should_prefetch ? kTrue : kFalse);
1627 1641
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1779 it->set_relevance(max_query_relevance); 1793 it->set_relevance(max_query_relevance);
1780 it->set_relevance_from_server(relevance_from_server); 1794 it->set_relevance_from_server(relevance_from_server);
1781 } 1795 }
1782 } 1796 }
1783 1797
1784 void SearchProvider::UpdateDone() { 1798 void SearchProvider::UpdateDone() {
1785 // We're done when the timer isn't running, there are no suggest queries 1799 // We're done when the timer isn't running, there are no suggest queries
1786 // pending, and we're not waiting on Instant. 1800 // pending, and we're not waiting on Instant.
1787 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); 1801 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0);
1788 } 1802 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698