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 |
| 11 #include "base/i18n/icu_string_conversions.h" | 11 #include "base/i18n/icu_string_conversions.h" |
| 12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/json/json_writer.h" | 14 #include "base/json/json_writer.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "components/omnibox/browser/autocomplete_i18n.h" | 21 #include "components/omnibox/browser/autocomplete_i18n.h" |
| 22 #include "components/omnibox/browser/autocomplete_input.h" | 22 #include "components/omnibox/browser/autocomplete_input.h" |
| 23 #include "components/omnibox/browser/omnibox_field_trial.h" | |
|
Mark P
2017/03/21 19:54:25
Why do you need this?
gcomanici
2017/03/22 02:39:03
Removed.
| |
| 23 #include "components/omnibox/browser/url_prefix.h" | 24 #include "components/omnibox/browser/url_prefix.h" |
| 24 #include "components/url_formatter/url_fixer.h" | 25 #include "components/url_formatter/url_fixer.h" |
| 25 #include "components/url_formatter/url_formatter.h" | 26 #include "components/url_formatter/url_formatter.h" |
| 26 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
| 27 #include "net/url_request/url_fetcher.h" | 28 #include "net/url_request/url_fetcher.h" |
| 28 #include "url/url_constants.h" | 29 #include "url/url_constants.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { | 33 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 | 223 |
| 223 SearchSuggestionParser::NavigationResult::NavigationResult( | 224 SearchSuggestionParser::NavigationResult::NavigationResult( |
| 224 const AutocompleteSchemeClassifier& scheme_classifier, | 225 const AutocompleteSchemeClassifier& scheme_classifier, |
| 225 const GURL& url, | 226 const GURL& url, |
| 226 AutocompleteMatchType::Type type, | 227 AutocompleteMatchType::Type type, |
| 227 const base::string16& description, | 228 const base::string16& description, |
| 228 const std::string& deletion_url, | 229 const std::string& deletion_url, |
| 229 bool from_keyword_provider, | 230 bool from_keyword_provider, |
| 230 int relevance, | 231 int relevance, |
| 231 bool relevance_from_server, | 232 bool relevance_from_server, |
| 232 const base::string16& input_text) | 233 const base::string16& input_text, |
| 234 int specific_type_identifier) | |
| 233 : Result(from_keyword_provider, | 235 : Result(from_keyword_provider, |
| 234 relevance, | 236 relevance, |
| 235 relevance_from_server, | 237 relevance_from_server, |
| 236 type, | 238 type, |
| 237 deletion_url), | 239 deletion_url), |
| 238 url_(url), | 240 url_(url), |
| 239 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( | 241 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 240 url, | 242 url, |
| 241 url_formatter::FormatUrl(url, | 243 url_formatter::FormatUrl(url, |
| 242 url_formatter::kFormatUrlOmitAll & | 244 url_formatter::kFormatUrlOmitAll & |
| 243 ~url_formatter::kFormatUrlOmitHTTP, | 245 ~url_formatter::kFormatUrlOmitHTTP, |
| 244 net::UnescapeRule::SPACES, | 246 net::UnescapeRule::SPACES, |
| 245 nullptr, | 247 nullptr, |
| 246 nullptr, | 248 nullptr, |
| 247 nullptr), | 249 nullptr), |
| 248 scheme_classifier)), | 250 scheme_classifier)), |
| 249 description_(description) { | 251 description_(description), |
| 252 specific_type_identifier_(specific_type_identifier) { | |
| 250 DCHECK(url_.is_valid()); | 253 DCHECK(url_.is_valid()); |
| 251 CalculateAndClassifyMatchContents(true, input_text); | 254 CalculateAndClassifyMatchContents(true, input_text); |
| 252 } | 255 } |
| 253 | 256 |
| 257 SearchSuggestionParser::NavigationResult::NavigationResult( | |
| 258 const NavigationResult& nav) = default; | |
| 254 SearchSuggestionParser::NavigationResult::~NavigationResult() {} | 259 SearchSuggestionParser::NavigationResult::~NavigationResult() {} |
| 255 | 260 |
| 256 void | 261 void |
| 257 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( | 262 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( |
| 258 const bool allow_bolding_nothing, | 263 const bool allow_bolding_nothing, |
| 259 const base::string16& input_text) { | 264 const base::string16& input_text) { |
| 260 if (input_text.empty()) { | 265 if (input_text.empty()) { |
| 261 // In case of zero-suggest results, do not highlight matches. | 266 // In case of zero-suggest results, do not highlight matches. |
| 262 match_contents_class_.push_back( | 267 match_contents_class_.push_back( |
| 263 ACMatchClassification(0, ACMatchClassification::NONE)); | 268 ACMatchClassification(0, ACMatchClassification::NONE)); |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 | 413 |
| 409 // 4th element: Disregard the query URL list for now. | 414 // 4th element: Disregard the query URL list for now. |
| 410 | 415 |
| 411 // Reset suggested relevance information. | 416 // Reset suggested relevance information. |
| 412 results->verbatim_relevance = -1; | 417 results->verbatim_relevance = -1; |
| 413 | 418 |
| 414 // 5th element: Optional key-value pairs from the Suggest server. | 419 // 5th element: Optional key-value pairs from the Suggest server. |
| 415 const base::ListValue* types = NULL; | 420 const base::ListValue* types = NULL; |
| 416 const base::ListValue* relevances = NULL; | 421 const base::ListValue* relevances = NULL; |
| 417 const base::ListValue* suggestion_details = NULL; | 422 const base::ListValue* suggestion_details = NULL; |
| 423 const base::ListValue* specific_type_identifiers = NULL; | |
| 418 const base::DictionaryValue* extras = NULL; | 424 const base::DictionaryValue* extras = NULL; |
| 419 int prefetch_index = -1; | 425 int prefetch_index = -1; |
| 420 if (root_list->GetDictionary(4, &extras)) { | 426 if (root_list->GetDictionary(4, &extras)) { |
| 421 extras->GetList("google:suggesttype", &types); | 427 extras->GetList("google:suggesttype", &types); |
| 422 | 428 |
| 423 // Discard this list if its size does not match that of the suggestions. | 429 // Discard this list if its size does not match that of the suggestions. |
| 424 if (extras->GetList("google:suggestrelevance", &relevances) && | 430 if (extras->GetList("google:suggestrelevance", &relevances) && |
| 425 (relevances->GetSize() != results_list->GetSize())) | 431 (relevances->GetSize() != results_list->GetSize())) |
| 426 relevances = NULL; | 432 relevances = NULL; |
| 427 extras->GetInteger("google:verbatimrelevance", | 433 extras->GetInteger("google:verbatimrelevance", |
| 428 &results->verbatim_relevance); | 434 &results->verbatim_relevance); |
| 429 | 435 |
| 430 // Check if the active suggest field trial (if any) has triggered either | 436 // Check if the active suggest field trial (if any) has triggered either |
| 431 // for the default provider or keyword provider. | 437 // for the default provider or keyword provider. |
| 432 results->field_trial_triggered = false; | 438 results->field_trial_triggered = false; |
| 433 extras->GetBoolean("google:fieldtrialtriggered", | 439 extras->GetBoolean("google:fieldtrialtriggered", |
| 434 &results->field_trial_triggered); | 440 &results->field_trial_triggered); |
| 435 | 441 |
| 436 const base::DictionaryValue* client_data = NULL; | 442 const base::DictionaryValue* client_data = NULL; |
| 437 if (extras->GetDictionary("google:clientdata", &client_data) && client_data) | 443 if (extras->GetDictionary("google:clientdata", &client_data) && client_data) |
| 438 client_data->GetInteger("phi", &prefetch_index); | 444 client_data->GetInteger("phi", &prefetch_index); |
| 439 | 445 |
| 440 if (extras->GetList("google:suggestdetail", &suggestion_details) && | 446 if (extras->GetList("google:suggestdetail", &suggestion_details) && |
| 441 suggestion_details->GetSize() != results_list->GetSize()) | 447 suggestion_details->GetSize() != results_list->GetSize()) |
| 442 suggestion_details = NULL; | 448 suggestion_details = NULL; |
| 443 | 449 |
| 450 // Get specific type identifiers. | |
| 451 if (extras->GetList("specifictypeid", &specific_type_identifiers) && | |
|
Mark P
2017/03/21 19:54:25
Let's use the "google:" as with everything else he
gcomanici
2017/03/22 02:39:03
Done.
| |
| 452 specific_type_identifiers->GetSize() != results_list->GetSize()) { | |
| 453 specific_type_identifiers = NULL; | |
| 454 } | |
| 455 | |
| 444 // Store the metadata that came with the response in case we need to pass it | 456 // Store the metadata that came with the response in case we need to pass it |
| 445 // along with the prefetch query to Instant. | 457 // along with the prefetch query to Instant. |
| 446 JSONStringValueSerializer json_serializer(&results->metadata); | 458 JSONStringValueSerializer json_serializer(&results->metadata); |
| 447 json_serializer.Serialize(*extras); | 459 json_serializer.Serialize(*extras); |
| 448 } | 460 } |
| 449 | 461 |
| 450 // Clear the previous results now that new results are available. | 462 // Clear the previous results now that new results are available. |
| 451 results->suggest_results.clear(); | 463 results->suggest_results.clear(); |
| 452 results->navigation_results.clear(); | 464 results->navigation_results.clear(); |
| 453 results->answers_image_urls.clear(); | 465 results->answers_image_urls.clear(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 479 | 491 |
| 480 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || | 492 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || |
| 481 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { | 493 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { |
| 482 // Do not blindly trust the URL coming from the server to be valid. | 494 // Do not blindly trust the URL coming from the server to be valid. |
| 483 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), | 495 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), |
| 484 std::string())); | 496 std::string())); |
| 485 if (url.is_valid()) { | 497 if (url.is_valid()) { |
| 486 base::string16 title; | 498 base::string16 title; |
| 487 if (descriptions != NULL) | 499 if (descriptions != NULL) |
| 488 descriptions->GetString(index, &title); | 500 descriptions->GetString(index, &title); |
| 501 // add zero suggest subtype | |
|
Mark P
2017/03/21 19:54:25
Correct comment (not just zero suggest; we're not
gcomanici
2017/03/22 02:39:03
I removed it, as the code is self explanatory.
| |
| 502 int specific_type_identifier = 0; | |
| 503 if (specific_type_identifiers) { | |
| 504 specific_type_identifiers->GetInteger(index, | |
| 505 &specific_type_identifier); | |
| 506 } | |
| 489 results->navigation_results.push_back(NavigationResult( | 507 results->navigation_results.push_back(NavigationResult( |
| 490 scheme_classifier, url, match_type, title, deletion_url, | 508 scheme_classifier, url, match_type, title, deletion_url, |
| 491 is_keyword_result, relevance, relevances != NULL, input.text())); | 509 is_keyword_result, relevance, relevances != NULL, input.text(), |
| 510 specific_type_identifier)); | |
| 492 } | 511 } |
| 493 } else { | 512 } else { |
| 494 // TODO(dschuyler) If the "= " is no longer sent from the back-end | 513 // TODO(dschuyler) If the "= " is no longer sent from the back-end |
| 495 // then this may be removed. | 514 // then this may be removed. |
| 496 if ((match_type == AutocompleteMatchType::CALCULATOR) && | 515 if ((match_type == AutocompleteMatchType::CALCULATOR) && |
| 497 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) | 516 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) |
| 498 suggestion.erase(0, 2); | 517 suggestion.erase(0, 2); |
| 499 | 518 |
| 500 base::string16 match_contents = suggestion; | 519 base::string16 match_contents = suggestion; |
| 501 base::string16 match_contents_prefix; | 520 base::string16 match_contents_prefix; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 |