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 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 221 |
222 SearchSuggestionParser::NavigationResult::NavigationResult( | 222 SearchSuggestionParser::NavigationResult::NavigationResult( |
223 const AutocompleteSchemeClassifier& scheme_classifier, | 223 const AutocompleteSchemeClassifier& scheme_classifier, |
224 const GURL& url, | 224 const GURL& url, |
225 AutocompleteMatchType::Type type, | 225 AutocompleteMatchType::Type type, |
226 const base::string16& description, | 226 const base::string16& description, |
227 const std::string& deletion_url, | 227 const std::string& deletion_url, |
228 bool from_keyword_provider, | 228 bool from_keyword_provider, |
229 int relevance, | 229 int relevance, |
230 bool relevance_from_server, | 230 bool relevance_from_server, |
231 const base::string16& input_text, | 231 const base::string16& input_text) |
232 const std::string& languages) | |
233 : Result(from_keyword_provider, | 232 : Result(from_keyword_provider, |
234 relevance, | 233 relevance, |
235 relevance_from_server, | 234 relevance_from_server, |
236 type, | 235 type, |
237 deletion_url), | 236 deletion_url), |
238 url_(url), | 237 url_(url), |
239 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( | 238 formatted_url_(AutocompleteInput::FormattedStringWithEquivalentMeaning( |
240 url, | 239 url, |
241 url_formatter::FormatUrl(url, | 240 url_formatter::FormatUrl(url, |
242 languages, | |
243 url_formatter::kFormatUrlOmitAll & | 241 url_formatter::kFormatUrlOmitAll & |
244 ~url_formatter::kFormatUrlOmitHTTP, | 242 ~url_formatter::kFormatUrlOmitHTTP, |
245 net::UnescapeRule::SPACES, | 243 net::UnescapeRule::SPACES, |
246 nullptr, | 244 nullptr, |
247 nullptr, | 245 nullptr, |
248 nullptr), | 246 nullptr), |
249 scheme_classifier)), | 247 scheme_classifier)), |
250 description_(description) { | 248 description_(description) { |
251 DCHECK(url_.is_valid()); | 249 DCHECK(url_.is_valid()); |
252 CalculateAndClassifyMatchContents(true, input_text, languages); | 250 CalculateAndClassifyMatchContents(true, input_text); |
253 } | 251 } |
254 | 252 |
255 SearchSuggestionParser::NavigationResult::~NavigationResult() {} | 253 SearchSuggestionParser::NavigationResult::~NavigationResult() {} |
256 | 254 |
257 void | 255 void |
258 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( | 256 SearchSuggestionParser::NavigationResult::CalculateAndClassifyMatchContents( |
259 const bool allow_bolding_nothing, | 257 const bool allow_bolding_nothing, |
260 const base::string16& input_text, | 258 const base::string16& input_text) { |
261 const std::string& languages) { | |
262 if (input_text.empty()) { | 259 if (input_text.empty()) { |
263 // In case of zero-suggest results, do not highlight matches. | 260 // In case of zero-suggest results, do not highlight matches. |
264 match_contents_class_.push_back( | 261 match_contents_class_.push_back( |
265 ACMatchClassification(0, ACMatchClassification::NONE)); | 262 ACMatchClassification(0, ACMatchClassification::NONE)); |
266 return; | 263 return; |
267 } | 264 } |
268 | 265 |
269 // First look for the user's input inside the formatted url as it would be | 266 // First look for the user's input inside the formatted url as it would be |
270 // without trimming the scheme, so we can find matches at the beginning of the | 267 // without trimming the scheme, so we can find matches at the beginning of the |
271 // scheme. | 268 // scheme. |
272 const URLPrefix* prefix = | 269 const URLPrefix* prefix = |
273 URLPrefix::BestURLPrefix(formatted_url_, input_text); | 270 URLPrefix::BestURLPrefix(formatted_url_, input_text); |
274 size_t match_start = (prefix == NULL) ? | 271 size_t match_start = (prefix == NULL) ? |
275 formatted_url_.find(input_text) : prefix->prefix.length(); | 272 formatted_url_.find(input_text) : prefix->prefix.length(); |
276 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && | 273 bool trim_http = !AutocompleteInput::HasHTTPScheme(input_text) && |
277 (!prefix || (match_start != 0)); | 274 (!prefix || (match_start != 0)); |
278 const url_formatter::FormatUrlTypes format_types = | 275 const url_formatter::FormatUrlTypes format_types = |
279 url_formatter::kFormatUrlOmitAll & | 276 url_formatter::kFormatUrlOmitAll & |
280 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP); | 277 ~(trim_http ? 0 : url_formatter::kFormatUrlOmitHTTP); |
281 | 278 |
282 base::string16 match_contents = url_formatter::FormatUrl( | 279 base::string16 match_contents = url_formatter::FormatUrl( |
283 url_, languages, format_types, net::UnescapeRule::SPACES, nullptr, | 280 url_, format_types, net::UnescapeRule::SPACES, nullptr, |
284 nullptr, &match_start); | 281 nullptr, &match_start); |
285 // If the first match in the untrimmed string was inside a scheme that we | 282 // If the first match in the untrimmed string was inside a scheme that we |
286 // trimmed, look for a subsequent match. | 283 // trimmed, look for a subsequent match. |
287 if (match_start == base::string16::npos) | 284 if (match_start == base::string16::npos) |
288 match_start = match_contents.find(input_text); | 285 match_start = match_contents.find(input_text); |
289 // Update |match_contents_| and |match_contents_class_| if it's allowed. | 286 // Update |match_contents_| and |match_contents_class_| if it's allowed. |
290 if (allow_bolding_nothing || (match_start != base::string16::npos)) { | 287 if (allow_bolding_nothing || (match_start != base::string16::npos)) { |
291 match_contents_ = match_contents; | 288 match_contents_ = match_contents; |
292 // Safe if |match_start| is npos; also safe if the input is longer than the | 289 // Safe if |match_start| is npos; also safe if the input is longer than the |
293 // remaining contents after |match_start|. | 290 // remaining contents after |match_start|. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 } | 383 } |
387 return scoped_ptr<base::Value>(); | 384 return scoped_ptr<base::Value>(); |
388 } | 385 } |
389 | 386 |
390 // static | 387 // static |
391 bool SearchSuggestionParser::ParseSuggestResults( | 388 bool SearchSuggestionParser::ParseSuggestResults( |
392 const base::Value& root_val, | 389 const base::Value& root_val, |
393 const AutocompleteInput& input, | 390 const AutocompleteInput& input, |
394 const AutocompleteSchemeClassifier& scheme_classifier, | 391 const AutocompleteSchemeClassifier& scheme_classifier, |
395 int default_result_relevance, | 392 int default_result_relevance, |
396 const std::string& languages, | |
397 bool is_keyword_result, | 393 bool is_keyword_result, |
398 Results* results) { | 394 Results* results) { |
399 base::string16 query; | 395 base::string16 query; |
400 const base::ListValue* root_list = NULL; | 396 const base::ListValue* root_list = NULL; |
401 const base::ListValue* results_list = NULL; | 397 const base::ListValue* results_list = NULL; |
402 | 398 |
403 if (!root_val.GetAsList(&root_list) || !root_list->GetString(0, &query) || | 399 if (!root_val.GetAsList(&root_list) || !root_list->GetString(0, &query) || |
404 query != input.text() || !root_list->GetList(1, &results_list)) | 400 query != input.text() || !root_list->GetList(1, &results_list)) |
405 return false; | 401 return false; |
406 | 402 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { | 482 (match_type == AutocompleteMatchType::NAVSUGGEST_PERSONALIZED)) { |
487 // 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. |
488 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), | 484 GURL url(url_formatter::FixupURL(base::UTF16ToUTF8(suggestion), |
489 std::string())); | 485 std::string())); |
490 if (url.is_valid() && allow_navsuggest) { | 486 if (url.is_valid() && allow_navsuggest) { |
491 base::string16 title; | 487 base::string16 title; |
492 if (descriptions != NULL) | 488 if (descriptions != NULL) |
493 descriptions->GetString(index, &title); | 489 descriptions->GetString(index, &title); |
494 results->navigation_results.push_back(NavigationResult( | 490 results->navigation_results.push_back(NavigationResult( |
495 scheme_classifier, url, match_type, title, deletion_url, | 491 scheme_classifier, url, match_type, title, deletion_url, |
496 is_keyword_result, relevance, relevances != NULL, input.text(), | 492 is_keyword_result, relevance, relevances != NULL, input.text())); |
497 languages)); | |
498 } | 493 } |
499 } else { | 494 } else { |
500 // TODO(dschuyler) If the "= " is no longer sent from the back-end | 495 // TODO(dschuyler) If the "= " is no longer sent from the back-end |
501 // then this may be removed. | 496 // then this may be removed. |
502 if ((match_type == AutocompleteMatchType::CALCULATOR) && | 497 if ((match_type == AutocompleteMatchType::CALCULATOR) && |
503 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) | 498 !suggestion.compare(0, 2, base::UTF8ToUTF16("= "))) |
504 suggestion.erase(0, 2); | 499 suggestion.erase(0, 2); |
505 | 500 |
506 base::string16 match_contents = suggestion; | 501 base::string16 match_contents = suggestion; |
507 base::string16 match_contents_prefix; | 502 base::string16 match_contents_prefix; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 base::CollapseWhitespace(match_contents, false), | 548 base::CollapseWhitespace(match_contents, false), |
554 match_contents_prefix, annotation, answer_contents, answer_type_str, | 549 match_contents_prefix, annotation, answer_contents, answer_type_str, |
555 std::move(answer), suggest_query_params, deletion_url, | 550 std::move(answer), suggest_query_params, deletion_url, |
556 is_keyword_result, relevance, relevances != NULL, should_prefetch, | 551 is_keyword_result, relevance, relevances != NULL, should_prefetch, |
557 trimmed_input)); | 552 trimmed_input)); |
558 } | 553 } |
559 } | 554 } |
560 results->relevances_from_server = relevances != NULL; | 555 results->relevances_from_server = relevances != NULL; |
561 return true; | 556 return true; |
562 } | 557 } |
OLD | NEW |