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 <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/icu_string_conversions.h" | 9 #include "base/i18n/icu_string_conversions.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
17 #include "base/values.h" | 17 #include "base/values.h" |
18 #include "components/omnibox/browser/autocomplete_i18n.h" | 18 #include "components/omnibox/browser/autocomplete_i18n.h" |
19 #include "components/omnibox/browser/autocomplete_input.h" | 19 #include "components/omnibox/browser/autocomplete_input.h" |
20 #include "components/omnibox/browser/url_prefix.h" | 20 #include "components/omnibox/browser/url_prefix.h" |
21 #include "components/url_formatter/url_fixer.h" | 21 #include "components/url_fixer/url_fixer.h" |
22 #include "components/url_formatter/url_formatter.h" | 22 #include "net/base/net_util.h" |
23 #include "net/http/http_response_headers.h" | 23 #include "net/http/http_response_headers.h" |
24 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
25 #include "url/url_constants.h" | 25 #include "url/url_constants.h" |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { | 29 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { |
30 if (type == "CALCULATOR") | 30 if (type == "CALCULATOR") |
31 return AutocompleteMatchType::CALCULATOR; | 31 return AutocompleteMatchType::CALCULATOR; |
32 if (type == "ENTITY") | 32 if (type == "ENTITY") |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 const AutocompleteSchemeClassifier& scheme_classifier, | 219 const AutocompleteSchemeClassifier& scheme_classifier, |
220 const GURL& url, | 220 const GURL& url, |
221 AutocompleteMatchType::Type type, | 221 AutocompleteMatchType::Type type, |
222 const base::string16& description, | 222 const base::string16& description, |
223 const std::string& deletion_url, | 223 const std::string& deletion_url, |
224 bool from_keyword_provider, | 224 bool from_keyword_provider, |
225 int relevance, | 225 int relevance, |
226 bool relevance_from_server, | 226 bool relevance_from_server, |
227 const base::string16& input_text, | 227 const base::string16& input_text, |
228 const std::string& languages) | 228 const std::string& languages) |
229 : Result(from_keyword_provider, | 229 : Result(from_keyword_provider, relevance, relevance_from_server, type, |
230 relevance, | |
231 relevance_from_server, | |
232 type, | |
233 deletion_url), | 230 deletion_url), |
234 url_(url), | 231 url_(url), |
235 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( | 232 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
236 url, | 233 url, net::FormatUrl(url, languages, |
237 url_formatter::FormatUrl(url, | 234 net::kFormatUrlOmitAll & ~net::kFormatUrlOmitHTTP, |
238 languages, | 235 net::UnescapeRule::SPACES, NULL, NULL, NULL), |
239 url_formatter::kFormatUrlOmitAll & | |
240 ~url_formatter::kFormatUrlOmitHTTP, | |
241 net::UnescapeRule::SPACES, | |
242 nullptr, | |
243 nullptr, | |
244 nullptr), | |
245 scheme_classifier)), | 236 scheme_classifier)), |
246 description_(description) { | 237 description_(description) { |
247 DCHECK(url_.is_valid()); | 238 DCHECK(url_.is_valid()); |
248 CalculateAndClassifyMatchContents(true, input_text, languages); | 239 CalculateAndClassifyMatchContents(true, input_text, languages); |
249 } | 240 } |
250 | 241 |
251 SearchSuggestionParser::NavigationResult::~NavigationResult() {} | 242 SearchSuggestionParser::NavigationResult::~NavigationResult() {} |
252 | 243 |
253 void | 244 void |
254 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( | 245 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( |
255 const bool allow_bolding_nothing, | 246 const bool allow_bolding_nothing, |
256 const base::string16& input_text, | 247 const base::string16& input_text, |
257 const std::string& languages) { | 248 const std::string& languages) { |
258 if (input_text.empty()) { | 249 if (input_text.empty()) { |
259 // In case of zero-suggest results, do not highlight matches. | 250 // In case of zero-suggest results, do not highlight matches. |
260 match_contents_class_.push_back( | 251 match_contents_class_.push_back( |
261 ACMatchClassification(0, ACMatchClassification::NONE)); | 252 ACMatchClassification(0, ACMatchClassification::NONE)); |
262 return; | 253 return; |
263 } | 254 } |
264 | 255 |
265 // First look for the user's input inside the formatted url as it would be | 256 // First look for the user's input inside the formatted url as it would be |
266 // without trimming the scheme, so we can find matches at the beginning of the | 257 // without trimming the scheme, so we can find matches at the beginning of the |
267 // scheme. | 258 // scheme. |
268 const URLPrefix* prefix = | 259 const URLPrefix* prefix = |
269 URLPrefix::BestURLPrefix(formatted_url_, input_text); | 260 URLPrefix::BestURLPrefix(formatted_url_, input_text); |
270 size_t match_start = (prefix == NULL) ? | 261 size_t match_start = (prefix == NULL) ? |
271 formatted_url_.find(input_text) : prefix->prefix.length(); | 262 formatted_url_.find(input_text) : prefix->prefix.length(); |
272 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && | 263 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && |
273 (!prefix || (match_start != 0)); | 264 (!prefix || (match_start != 0)); |
274 const url_formatter::FormatUrlTypes format_types = | 265 const net::FormatUrlTypes format_types = |
275 url_formatter::kFormatUrlOmitAll & | 266 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP); |
276 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP); | |
277 | 267 |
278 base::string16 match_contents = url_formatter::FormatUrl( | 268 base::string16 match_contents = net::FormatUrl(url_, languages, format_types, |
279 url_, languages, format_types, net::UnescapeRule::SPACES, nullptr, | 269 net::UnescapeRule::SPACES, NULL, NULL, &match_start); |
280 nullptr, &match_start); | |
281 // If the first match in the untrimmed string was inside a scheme that we | 270 // If the first match in the untrimmed string was inside a scheme that we |
282 // trimmed, look for a subsequent match. | 271 // trimmed, look for a subsequent match. |
283 if (match_start == base::string16::npos) | 272 if (match_start == base::string16::npos) |
284 match_start = match_contents.find(input_text); | 273 match_start = match_contents.find(input_text); |
285 // Update |match_contents_| and |match_contents_class_| if it's allowed. | 274 // Update |match_contents_| and |match_contents_class_| if it's allowed. |
286 if (allow_bolding_nothing || (match_start != base::string16::npos)) { | 275 if (allow_bolding_nothing || (match_start != base::string16::npos)) { |
287 match_contents_ = match_contents; | 276 match_contents_ = match_contents; |
288 // Safe if |match_start| is npos; also safe if the input is longer than the | 277 // Safe if |match_start| is npos; also safe if the input is longer than the |
289 // remaining contents after |match_start|. | 278 // remaining contents after |match_start|. |
290 AutocompleteMatch::ClassifyLocationInString(match_start, | 279 AutocompleteMatch::ClassifyLocationInString(match_start, |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 const base::DictionaryValue* suggestion_detail = NULL; | 463 const base::DictionaryValue* suggestion_detail = NULL; |
475 std::string deletion_url; | 464 std::string deletion_url; |
476 | 465 |
477 if (suggestion_details && | 466 if (suggestion_details && |
478 suggestion_details->GetDictionary(index, &suggestion_detail)) | 467 suggestion_details->GetDictionary(index, &suggestion_detail)) |
479 suggestion_detail->GetString("du", &deletion_url); | 468 suggestion_detail->GetString("du", &deletion_url); |
480 | 469 |
481 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || | 470 if ((match_type == AutocompleteMatchType::NAVSUGGEST) || |
482 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { | 471 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { |
483 // Do not blindly trust the URL coming from the server to be valid. | 472 // Do not blindly trust the URL coming from the server to be valid. |
484 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), | 473 GURL url( |
485 std::string())); | 474 url_fixer::FixupURL(base::UTF16ToUTF8(suggestion), std::string())); |
486 if (url.is_valid() && allow_navsuggest) { | 475 if (url.is_valid() && allow_navsuggest) { |
487 base::string16 title; | 476 base::string16 title; |
488 if (descriptions != NULL) | 477 if (descriptions != NULL) |
489 descriptions->GetString(index, &title); | 478 descriptions->GetString(index, &title); |
490 results->navigation_results.push_back(NavigationResult( | 479 results->navigation_results.push_back(NavigationResult( |
491 scheme_classifier, url, match_type, title, deletion_url, | 480 scheme_classifier, url, match_type, title, deletion_url, |
492 is_keyword_result, relevance, relevances != NULL, input.text(), | 481 is_keyword_result, relevance, relevances != NULL, input.text(), |
493 languages)); | 482 languages)); |
494 } | 483 } |
495 } else { | 484 } else { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
548 base::CollapseWhitespace(suggestion, false), match_type, | 537 base::CollapseWhitespace(suggestion, false), match_type, |
549 base::CollapseWhitespace(match_contents, false), | 538 base::CollapseWhitespace(match_contents, false), |
550 match_contents_prefix, annotation, answer_contents, answer_type_str, | 539 match_contents_prefix, annotation, answer_contents, answer_type_str, |
551 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, | 540 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, |
552 relevance, relevances != NULL, should_prefetch, trimmed_input)); | 541 relevance, relevances != NULL, should_prefetch, trimmed_input)); |
553 } | 542 } |
554 } | 543 } |
555 results->relevances_from_server = relevances != NULL; | 544 results->relevances_from_server = relevances != NULL; |
556 return true; | 545 return true; |
557 } | 546 } |
OLD | NEW |