Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/search_suggestion_parser.h" | 5 #include "components/omnibox/browser/search_suggestion_parser.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 48 } | 48 } |
| 49 | 49 |
| 50 } // namespace | 50 } // namespace |
| 51 | 51 |
| 52 // SearchSuggestionParser::Result ---------------------------------------------- | 52 // SearchSuggestionParser::Result ---------------------------------------------- |
| 53 | 53 |
| 54 SearchSuggestionParser::Result::Result(bool from_keyword_provider, | 54 SearchSuggestionParser::Result::Result(bool from_keyword_provider, |
| 55 int relevance, | 55 int relevance, |
| 56 bool relevance_from_server, | 56 bool relevance_from_server, |
| 57 AutocompleteMatchType::Type type, | 57 AutocompleteMatchType::Type type, |
| 58 int result_subtype_identifier, | |
| 58 const std::string& deletion_url) | 59 const std::string& deletion_url) |
| 59 : from_keyword_provider_(from_keyword_provider), | 60 : from_keyword_provider_(from_keyword_provider), |
| 60 type_(type), | 61 type_(type), |
| 62 result_subtype_identifier_(result_subtype_identifier), | |
| 61 relevance_(relevance), | 63 relevance_(relevance), |
| 62 relevance_from_server_(relevance_from_server), | 64 relevance_from_server_(relevance_from_server), |
| 63 received_after_last_keystroke_(true), | 65 received_after_last_keystroke_(true), |
| 64 deletion_url_(deletion_url) {} | 66 deletion_url_(deletion_url) {} |
| 65 | 67 |
| 66 SearchSuggestionParser::Result::Result(const Result& other) = default; | 68 SearchSuggestionParser::Result::Result(const Result& other) = default; |
| 67 | 69 |
| 68 SearchSuggestionParser::Result::~Result() {} | 70 SearchSuggestionParser::Result::~Result() {} |
| 69 | 71 |
| 70 // SearchSuggestionParser::SuggestResult --------------------------------------- | 72 // SearchSuggestionParser::SuggestResult --------------------------------------- |
| 71 | 73 |
| 72 SearchSuggestionParser::SuggestResult::SuggestResult( | 74 SearchSuggestionParser::SuggestResult::SuggestResult( |
| 73 const base::string16& suggestion, | 75 const base::string16& suggestion, |
| 74 AutocompleteMatchType::Type type, | 76 AutocompleteMatchType::Type type, |
| 77 int result_subtype_identifier, | |
| 75 const base::string16& match_contents, | 78 const base::string16& match_contents, |
| 76 const base::string16& match_contents_prefix, | 79 const base::string16& match_contents_prefix, |
| 77 const base::string16& annotation, | 80 const base::string16& annotation, |
| 78 const base::string16& answer_contents, | 81 const base::string16& answer_contents, |
| 79 const base::string16& answer_type, | 82 const base::string16& answer_type, |
| 80 std::unique_ptr<SuggestionAnswer> answer, | 83 std::unique_ptr<SuggestionAnswer> answer, |
| 81 const std::string& suggest_query_params, | 84 const std::string& suggest_query_params, |
| 82 const std::string& deletion_url, | 85 const std::string& deletion_url, |
| 83 bool from_keyword_provider, | 86 bool from_keyword_provider, |
| 84 int relevance, | 87 int relevance, |
| 85 bool relevance_from_server, | 88 bool relevance_from_server, |
| 86 bool should_prefetch, | 89 bool should_prefetch, |
| 87 const base::string16& input_text) | 90 const base::string16& input_text) |
| 88 : Result(from_keyword_provider, | 91 : Result(from_keyword_provider, |
| 89 relevance, | 92 relevance, |
| 90 relevance_from_server, | 93 relevance_from_server, |
| 91 type, | 94 type, |
| 95 result_subtype_identifier, | |
| 92 deletion_url), | 96 deletion_url), |
| 93 suggestion_(suggestion), | 97 suggestion_(suggestion), |
| 94 match_contents_prefix_(match_contents_prefix), | 98 match_contents_prefix_(match_contents_prefix), |
| 95 annotation_(annotation), | 99 annotation_(annotation), |
| 96 suggest_query_params_(suggest_query_params), | 100 suggest_query_params_(suggest_query_params), |
| 97 answer_contents_(answer_contents), | 101 answer_contents_(answer_contents), |
| 98 answer_type_(answer_type), | 102 answer_type_(answer_type), |
| 99 answer_(std::move(answer)), | 103 answer_(std::move(answer)), |
| 100 should_prefetch_(should_prefetch) { | 104 should_prefetch_(should_prefetch) { |
| 101 match_contents_ = match_contents; | 105 match_contents_ = match_contents; |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 return 100; | 221 return 100; |
| 218 return ((input.type() == metrics::OmniboxInputType::URL) ? 300 : 600); | 222 return ((input.type() == metrics::OmniboxInputType::URL) ? 300 : 600); |
| 219 } | 223 } |
| 220 | 224 |
| 221 // SearchSuggestionParser::NavigationResult ------------------------------------ | 225 // SearchSuggestionParser::NavigationResult ------------------------------------ |
| 222 | 226 |
| 223 SearchSuggestionParser::NavigationResult::NavigationResult( | 227 SearchSuggestionParser::NavigationResult::NavigationResult( |
| 224 const AutocompleteSchemeClassifier& scheme_classifier, | 228 const AutocompleteSchemeClassifier& scheme_classifier, |
| 225 const GURL& url, | 229 const GURL& url, |
| 226 AutocompleteMatchType::Type type, | 230 AutocompleteMatchType::Type type, |
| 231 int result_subtype_identifier, | |
| 227 const base::string16& description, | 232 const base::string16& description, |
| 228 const std::string& deletion_url, | 233 const std::string& deletion_url, |
| 229 bool from_keyword_provider, | 234 bool from_keyword_provider, |
| 230 int relevance, | 235 int relevance, |
| 231 bool relevance_from_server, | 236 bool relevance_from_server, |
| 232 const base::string16& input_text) | 237 const base::string16& input_text) |
| 233 : Result(from_keyword_provider, | 238 : Result(from_keyword_provider, |
| 234 relevance, | 239 relevance, |
| 235 relevance_from_server, | 240 relevance_from_server, |
| 236 type, | 241 type, |
| 242 result_subtype_identifier, | |
| 237 deletion_url), | 243 deletion_url), |
| 238 url_(url), | 244 url_(url), |
| 239 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( | 245 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 240 url, | 246 url, |
| 241 url_formatter::FormatUrl(url, | 247 url_formatter::FormatUrl(url, |
| 242 url_formatter::kFormatUrlOmitAll & | 248 url_formatter::kFormatUrlOmitAll & |
| 243 ~url_formatter::kFormatUrlOmitHTTP, | 249 ~url_formatter::kFormatUrlOmitHTTP, |
| 244 net::UnescapeRule::SPACES, | 250 net::UnescapeRule::SPACES, |
| 245 nullptr, | 251 nullptr, |
| 246 nullptr, | 252 nullptr, |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 | 414 |
| 409 // 4th element: Disregard the query URL list for now. | 415 // 4th element: Disregard the query URL list for now. |
| 410 | 416 |
| 411 // Reset suggested relevance information. | 417 // Reset suggested relevance information. |
| 412 results->verbatim_relevance = -1; | 418 results->verbatim_relevance = -1; |
| 413 | 419 |
| 414 // 5th element: Optional key-value pairs from the Suggest server. | 420 // 5th element: Optional key-value pairs from the Suggest server. |
| 415 const base::ListValue* types = NULL; | 421 const base::ListValue* types = NULL; |
| 416 const base::ListValue* relevances = NULL; | 422 const base::ListValue* relevances = NULL; |
| 417 const base::ListValue* suggestion_details = NULL; | 423 const base::ListValue* suggestion_details = NULL; |
| 424 const base::ListValue* result_subtype_identifiers = NULL; | |
| 418 const base::DictionaryValue* extras = NULL; | 425 const base::DictionaryValue* extras = NULL; |
| 419 int prefetch_index = -1; | 426 int prefetch_index = -1; |
| 420 if (root_list->GetDictionary(4, &extras)) { | 427 if (root_list->GetDictionary(4, &extras)) { |
| 421 extras->GetList("google:suggesttype", &types); | 428 extras->GetList("google:suggesttype", &types); |
| 422 | 429 |
| 423 // Discard this list if its size does not match that of the suggestions. | 430 // Discard this list if its size does not match that of the suggestions. |
| 424 if (extras->GetList("google:suggestrelevance", &relevances) && | 431 if (extras->GetList("google:suggestrelevance", &relevances) && |
| 425 (relevances->GetSize() != results_list->GetSize())) | 432 (relevances->GetSize() != results_list->GetSize())) |
| 426 relevances = NULL; | 433 relevances = NULL; |
| 427 extras->GetInteger("google:verbatimrelevance", | 434 extras->GetInteger("google:verbatimrelevance", |
| 428 &results->verbatim_relevance); | 435 &results->verbatim_relevance); |
| 429 | 436 |
| 430 // Check if the active suggest field trial (if any) has triggered either | 437 // Check if the active suggest field trial (if any) has triggered either |
| 431 // for the default provider or keyword provider. | 438 // for the default provider or keyword provider. |
| 432 results->field_trial_triggered = false; | 439 results->field_trial_triggered = false; |
| 433 extras->GetBoolean("google:fieldtrialtriggered", | 440 extras->GetBoolean("google:fieldtrialtriggered", |
| 434 &results->field_trial_triggered); | 441 &results->field_trial_triggered); |
| 435 | 442 |
| 436 const base::DictionaryValue* client_data = NULL; | 443 const base::DictionaryValue* client_data = NULL; |
| 437 if (extras->GetDictionary("google:clientdata", &client_data) && client_data) | 444 if (extras->GetDictionary("google:clientdata", &client_data) && client_data) |
| 438 client_data->GetInteger("phi", &prefetch_index); | 445 client_data->GetInteger("phi", &prefetch_index); |
| 439 | 446 |
| 440 if (extras->GetList("google:suggestdetail", &suggestion_details) && | 447 if (extras->GetList("google:suggestdetail", &suggestion_details) && |
| 441 suggestion_details->GetSize() != results_list->GetSize()) | 448 suggestion_details->GetSize() != results_list->GetSize()) |
| 442 suggestion_details = NULL; | 449 suggestion_details = NULL; |
| 443 | 450 |
| 451 // Get specific type identifiers. | |
| 452 if (extras->GetList("google:specifictypeid", &result_subtype_identifiers) && | |
|
Mark P
2017/03/31 17:36:43
Please choose a new string name that corresponds w
gcomanici
2017/03/31 19:03:58
Done.
| |
| 453 result_subtype_identifiers->GetSize() != results_list->GetSize()) { | |
| 454 result_subtype_identifiers = NULL; | |
| 455 } | |
| 456 | |
| 444 // Store the metadata that came with the response in case we need to pass it | 457 // Store the metadata that came with the response in case we need to pass it |
| 445 // along with the prefetch query to Instant. | 458 // along with the prefetch query to Instant. |
| 446 JSONStringValueSerializer json_serializer(&results->metadata); | 459 JSONStringValueSerializer json_serializer(&results->metadata); |
| 447 json_serializer.Serialize(*extras); | 460 json_serializer.Serialize(*extras); |
| 448 } | 461 } |
| 449 | 462 |
| 450 // Clear the previous results now that new results are available. | 463 // Clear the previous results now that new results are available. |
| 451 results->suggest_results.clear(); | 464 results->suggest_results.clear(); |
| 452 results->navigation_results.clear(); | 465 results->navigation_results.clear(); |
| 453 results->answers_image_urls.clear(); | 466 results->answers_image_urls.clear(); |
| 454 | 467 |
| 455 base::string16 suggestion; | 468 base::string16 suggestion; |
| 456 std::string type; | 469 std::string type; |
| 457 int relevance = default_result_relevance; | 470 int relevance = default_result_relevance; |
| 458 const base::string16& trimmed_input = | 471 const base::string16& trimmed_input = |
| 459 base::CollapseWhitespace(input.text(), false); | 472 base::CollapseWhitespace(input.text(), false); |
| 460 for (size_t index = 0; results_list->GetString(index, &suggestion); ++index) { | 473 for (size_t index = 0; results_list->GetString(index, &suggestion); ++index) { |
| 461 // Google search may return empty suggestions for weird input characters, | 474 // Google search may return empty suggestions for weird input characters, |
| 462 // they make no sense at all and can cause problems in our code. | 475 // they make no sense at all and can cause problems in our code. |
| 463 if (suggestion.empty()) | 476 if (suggestion.empty()) |
| 464 continue; | 477 continue; |
| 465 | 478 |
| 466 // Apply valid suggested relevance scores; discard invalid lists. | 479 // Apply valid suggested relevance scores; discard invalid lists. |
| 467 if (relevances != NULL && !relevances->GetInteger(index, &relevance)) | 480 if (relevances != NULL && !relevances->GetInteger(index, &relevance)) |
| 468 relevances = NULL; | 481 relevances = NULL; |
| 469 AutocompleteMatchType::Type match_type = | 482 AutocompleteMatchType::Type match_type = |
| 470 AutocompleteMatchType::SEARCH_SUGGEST; | 483 AutocompleteMatchType::SEARCH_SUGGEST; |
| 484 int result_subtype_identifier = 0; | |
| 485 if (result_subtype_identifiers) { | |
| 486 result_subtype_identifiers->GetInteger(index, &result_subtype_identifier); | |
| 487 } | |
| 471 if (types && types->GetString(index, &type)) | 488 if (types && types->GetString(index, &type)) |
| 472 match_type = GetAutocompleteMatchType(type); | 489 match_type = GetAutocompleteMatchType(type); |
| 473 const base::DictionaryValue* suggestion_detail = NULL; | 490 const base::DictionaryValue* suggestion_detail = NULL; |
| 474 std::string deletion_url; | 491 std::string deletion_url; |
| 475 | 492 |
| 476 if (suggestion_details && | 493 if (suggestion_details && |
| 477 suggestion_details->GetDictionary(index, &suggestion_detail)) | 494 suggestion_details->GetDictionary(index, &suggestion_detail)) |
| 478 suggestion_detail->GetString("du", &deletion_url); | 495 suggestion_detail->GetString("du", &deletion_url); |
| 479 | 496 |
| 480 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || | 497 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || |
| 481 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { | 498 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { |
| 482 // Do not blindly trust the URL coming from the server to be valid. | 499 // Do not blindly trust the URL coming from the server to be valid. |
| 483 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), | 500 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), |
| 484 std::string())); | 501 std::string())); |
| 485 if (url.is_valid()) { | 502 if (url.is_valid()) { |
| 486 base::string16 title; | 503 base::string16 title; |
| 487 if (descriptions != NULL) | 504 if (descriptions != NULL) |
| 488 descriptions->GetString(index, &title); | 505 descriptions->GetString(index, &title); |
| 489 results->navigation_results.push_back(NavigationResult( | 506 results->navigation_results.push_back(NavigationResult( |
| 490 scheme_classifier, url, match_type, title, deletion_url, | 507 scheme_classifier, url, match_type, result_subtype_identifier, |
| 491 is_keyword_result, relevance, relevances != NULL, input.text())); | 508 title, deletion_url, is_keyword_result, relevance, |
| 509 relevances != NULL, input.text())); | |
| 492 } | 510 } |
| 493 } else { | 511 } else { |
| 494 // TODO(dschuyler) If the "= " is no longer sent from the back-end | 512 // TODO(dschuyler) If the "= " is no longer sent from the back-end |
| 495 // then this may be removed. | 513 // then this may be removed. |
| 496 if ((match_type == AutocompleteMatchType::CALCULATOR) && | 514 if ((match_type == AutocompleteMatchType::CALCULATOR) && |
| 497 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) | 515 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) |
| 498 suggestion.erase(0, 2); | 516 suggestion.erase(0, 2); |
| 499 | 517 |
| 500 base::string16 match_contents = suggestion; | 518 base::string16 match_contents = suggestion; |
| 501 base::string16 match_contents_prefix; | 519 base::string16 match_contents_prefix; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 539 } | 557 } |
| 540 UMA_HISTOGRAM_BOOLEAN("Omnibox.AnswerParseSuccess", | 558 UMA_HISTOGRAM_BOOLEAN("Omnibox.AnswerParseSuccess", |
| 541 answer_parsed_successfully); | 559 answer_parsed_successfully); |
| 542 } | 560 } |
| 543 } | 561 } |
| 544 } | 562 } |
| 545 | 563 |
| 546 bool should_prefetch = static_cast<int>(index) == prefetch_index; | 564 bool should_prefetch = static_cast<int>(index) == prefetch_index; |
| 547 results->suggest_results.push_back(SuggestResult( | 565 results->suggest_results.push_back(SuggestResult( |
| 548 base::CollapseWhitespace(suggestion, false), match_type, | 566 base::CollapseWhitespace(suggestion, false), match_type, |
| 567 result_subtype_identifier, | |
| 549 base::CollapseWhitespace(match_contents, false), | 568 base::CollapseWhitespace(match_contents, false), |
| 550 match_contents_prefix, annotation, answer_contents, answer_type_str, | 569 match_contents_prefix, annotation, answer_contents, answer_type_str, |
| 551 std::move(answer), suggest_query_params, deletion_url, | 570 std::move(answer), suggest_query_params, deletion_url, |
| 552 is_keyword_result, relevance, relevances != NULL, should_prefetch, | 571 is_keyword_result, relevance, relevances != NULL, should_prefetch, |
| 553 trimmed_input)); | 572 trimmed_input)); |
| 554 } | 573 } |
| 555 } | 574 } |
| 556 results->relevances_from_server = relevances != NULL; | 575 results->relevances_from_server = relevances != NULL; |
| 557 return true; | 576 return true; |
| 558 } | 577 } |
| OLD | NEW |