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/search_suggestion_parser.h" | 5 #include "components/omnibox/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.h" | 13 #include "base/metrics/histogram.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/autocomplete_input.h" | 18 #include "components/omnibox/autocomplete_input.h" |
19 #include "components/omnibox/url_prefix.h" | 19 #include "components/omnibox/url_prefix.h" |
20 #include "components/url_fixer/url_fixer.h" | 20 #include "components/url_fixer/url_fixer.h" |
21 #include "net/base/net_util.h" | 21 #include "components/url_formatter/url_formatter.h" |
22 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
23 #include "net/url_request/url_fetcher.h" | 23 #include "net/url_request/url_fetcher.h" |
24 #include "url/url_constants.h" | 24 #include "url/url_constants.h" |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { | 28 AutocompleteMatchType::Type GetAutocompleteMatchType(const std::string& type) { |
29 if (type == "CALCULATOR") | 29 if (type == "CALCULATOR") |
30 return AutocompleteMatchType::CALCULATOR; | 30 return AutocompleteMatchType::CALCULATOR; |
31 if (type == "ENTITY") | 31 if (type == "ENTITY") |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 const std::string& deletion_url, | 220 const std::string& deletion_url, |
221 bool from_keyword_provider, | 221 bool from_keyword_provider, |
222 int relevance, | 222 int relevance, |
223 bool relevance_from_server, | 223 bool relevance_from_server, |
224 const base::string16& input_text, | 224 const base::string16& input_text, |
225 const std::string& languages) | 225 const std::string& languages) |
226 : Result(from_keyword_provider, relevance, relevance_from_server, type, | 226 : Result(from_keyword_provider, relevance, relevance_from_server, type, |
227 deletion_url), | 227 deletion_url), |
228 url_(url), | 228 url_(url), |
229 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( | 229 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
230 url, net::FormatUrl(url, languages, | 230 url, |
231 net::kFormatUrlOmitAll & ~net::kFormatUrlOmitHTTP, | 231 url_formatter::FormatUrl( |
232 net::UnescapeRule::SPACES, NULL, NULL, NULL), | 232 url, languages, |
| 233 url_formatter::kFormatUrlOmitAll & |
| 234 ~url_formatter::kFormatUrlOmitHTTP, |
| 235 net::UnescapeRule::SPACES, nullptr, nullptr, nullptr), |
233 scheme_classifier)), | 236 scheme_classifier)), |
234 description_(description) { | 237 description_(description) { |
235 DCHECK(url_.is_valid()); | 238 DCHECK(url_.is_valid()); |
236 CalculateAndClassifyMatchContents(true, input_text, languages); | 239 CalculateAndClassifyMatchContents(true, input_text, languages); |
237 } | 240 } |
238 | 241 |
239 SearchSuggestionParser::NavigationResult::~NavigationResult() {} | 242 SearchSuggestionParser::NavigationResult::~NavigationResult() {} |
240 | 243 |
241 void | 244 void |
242 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( | 245 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( |
243 const bool allow_bolding_nothing, | 246 const bool allow_bolding_nothing, |
244 const base::string16& input_text, | 247 const base::string16& input_text, |
245 const std::string& languages) { | 248 const std::string& languages) { |
246 if (input_text.empty()) { | 249 if (input_text.empty()) { |
247 // In case of zero-suggest results, do not highlight matches. | 250 // In case of zero-suggest results, do not highlight matches. |
248 match_contents_class_.push_back( | 251 match_contents_class_.push_back( |
249 ACMatchClassification(0, ACMatchClassification::NONE)); | 252 ACMatchClassification(0, ACMatchClassification::NONE)); |
250 return; | 253 return; |
251 } | 254 } |
252 | 255 |
253 // 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 |
254 // 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 |
255 // scheme. | 258 // scheme. |
256 const URLPrefix* prefix = | 259 const URLPrefix* prefix = |
257 URLPrefix::BestURLPrefix(formatted_url_, input_text); | 260 URLPrefix::BestURLPrefix(formatted_url_, input_text); |
258 size_t match_start = (prefix == NULL) ? | 261 size_t match_start = (prefix == NULL) ? |
259 formatted_url_.find(input_text) : prefix->prefix.length(); | 262 formatted_url_.find(input_text) : prefix->prefix.length(); |
260 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && | 263 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && |
261 (!prefix || (match_start != 0)); | 264 (!prefix || (match_start != 0)); |
262 const net::FormatUrlTypes format_types = | 265 const url_formatter::FormatUrlTypes format_types = |
263 net::kFormatUrlOmitAll & ~(trim_http ? 0 : net::kFormatUrlOmitHTTP); | 266 url_formatter::kFormatUrlOmitAll & |
| 267 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP); |
264 | 268 |
265 base::string16 match_contents = net::FormatUrl(url_, languages, format_types, | 269 base::string16 match_contents = url_formatter::FormatUrl( |
266 net::UnescapeRule::SPACES, NULL, NULL, &match_start); | 270 url_, languages, format_types, net::UnescapeRule::SPACES, NULL, NULL, |
| 271 &match_start); |
267 // If the first match in the untrimmed string was inside a scheme that we | 272 // If the first match in the untrimmed string was inside a scheme that we |
268 // trimmed, look for a subsequent match. | 273 // trimmed, look for a subsequent match. |
269 if (match_start == base::string16::npos) | 274 if (match_start == base::string16::npos) |
270 match_start = match_contents.find(input_text); | 275 match_start = match_contents.find(input_text); |
271 // Update |match_contents_| and |match_contents_class_| if it's allowed. | 276 // Update |match_contents_| and |match_contents_class_| if it's allowed. |
272 if (allow_bolding_nothing || (match_start != base::string16::npos)) { | 277 if (allow_bolding_nothing || (match_start != base::string16::npos)) { |
273 match_contents_ = match_contents; | 278 match_contents_ = match_contents; |
274 // Safe if |match_start| is npos; also safe if the input is longer than the | 279 // Safe if |match_start| is npos; also safe if the input is longer than the |
275 // remaining contents after |match_start|. | 280 // remaining contents after |match_start|. |
276 AutocompleteMatch::ClassifyLocationInString(match_start, | 281 AutocompleteMatch::ClassifyLocationInString(match_start, |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 base::CollapseWhitespace(suggestion, false), match_type, | 539 base::CollapseWhitespace(suggestion, false), match_type, |
535 base::CollapseWhitespace(match_contents, false), | 540 base::CollapseWhitespace(match_contents, false), |
536 match_contents_prefix, annotation, answer_contents, answer_type_str, | 541 match_contents_prefix, annotation, answer_contents, answer_type_str, |
537 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, | 542 answer.Pass(), suggest_query_params, deletion_url, is_keyword_result, |
538 relevance, relevances != NULL, should_prefetch, trimmed_input)); | 543 relevance, relevances != NULL, should_prefetch, trimmed_input)); |
539 } | 544 } |
540 } | 545 } |
541 results->relevances_from_server = relevances != NULL; | 546 results->relevances_from_server = relevances != NULL; |
542 return true; | 547 return true; |
543 } | 548 } |
OLD | NEW |