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