OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/autocomplete/autocomplete_match.h" | 5 #include "chrome/browser/autocomplete/autocomplete_match.h" |
6 | 6 |
7 #include "base/i18n/time_formatting.h" | 7 #include "base/i18n/time_formatting.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 | 338 |
339 // static | 339 // static |
340 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { | 340 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { |
341 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | 341 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || |
342 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || | 342 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || |
343 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || | 343 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || |
344 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || | 344 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || |
345 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; | 345 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; |
346 } | 346 } |
347 | 347 |
348 void AutocompleteMatch::ComputeStrippedDestinationURL(Profile* profile) { | 348 // static |
349 stripped_destination_url = destination_url; | 349 GURL AutocompleteMatch::GURLToStrippedGURL( |
350 if (!stripped_destination_url.is_valid()) | 350 const GURL& url, Profile* profile, const base::string16& keyword) { |
Peter Kasting
2014/06/30 23:02:58
Nit: One arg per line
Mark P
2014/06/30 23:27:36
Done.
| |
351 return; | 351 if (!url.is_valid()) |
352 return url; | |
353 | |
354 GURL stripped_destination_url = url; | |
352 | 355 |
353 // If the destination URL looks like it was generated from a TemplateURL, | 356 // If the destination URL looks like it was generated from a TemplateURL, |
354 // remove all substitutions other than the search terms. This allows us | 357 // remove all substitutions other than the search terms. This allows us |
355 // to eliminate cases like past search URLs from history that differ only | 358 // to eliminate cases like past search URLs from history that differ only |
356 // by some obscure query param from each other or from the search/keyword | 359 // by some obscure query param from each other or from the search/keyword |
357 // provider matches. | 360 // provider matches. |
358 TemplateURL* template_url = GetTemplateURL(profile, true); | 361 TemplateURL* template_url = GetTemplateURLWithKeyword( |
362 profile, keyword, stripped_destination_url.host()); | |
359 UIThreadSearchTermsData search_terms_data(profile); | 363 UIThreadSearchTermsData search_terms_data(profile); |
360 if (template_url != NULL && | 364 if (template_url != NULL && |
361 template_url->SupportsReplacement(search_terms_data)) { | 365 template_url->SupportsReplacement(search_terms_data)) { |
362 base::string16 search_terms; | 366 base::string16 search_terms; |
363 if (template_url->ExtractSearchTermsFromURL(stripped_destination_url, | 367 if (template_url->ExtractSearchTermsFromURL(stripped_destination_url, |
364 search_terms_data, | 368 search_terms_data, |
365 &search_terms)) { | 369 &search_terms)) { |
366 stripped_destination_url = | 370 stripped_destination_url = |
367 GURL(template_url->url_ref().ReplaceSearchTerms( | 371 GURL(template_url->url_ref().ReplaceSearchTerms( |
368 TemplateURLRef::SearchTermsArgs(search_terms), | 372 TemplateURLRef::SearchTermsArgs(search_terms), |
(...skipping 21 matching lines...) Expand all Loading... | |
390 // Replace https protocol with http protocol. | 394 // Replace https protocol with http protocol. |
391 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { | 395 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { |
392 replacements.SetScheme(url::kHttpScheme, | 396 replacements.SetScheme(url::kHttpScheme, |
393 url::Component(0, strlen(url::kHttpScheme))); | 397 url::Component(0, strlen(url::kHttpScheme))); |
394 needs_replacement = true; | 398 needs_replacement = true; |
395 } | 399 } |
396 | 400 |
397 if (needs_replacement) | 401 if (needs_replacement) |
398 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 402 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
399 replacements); | 403 replacements); |
404 return stripped_destination_url; | |
405 } | |
406 | |
407 void AutocompleteMatch::ComputeStrippedDestinationURL(Profile* profile) { | |
408 stripped_destination_url = | |
409 GURLToStrippedGURL(destination_url, profile, keyword); | |
400 } | 410 } |
401 | 411 |
402 void AutocompleteMatch::GetKeywordUIState(Profile* profile, | 412 void AutocompleteMatch::GetKeywordUIState(Profile* profile, |
403 base::string16* keyword, | 413 base::string16* keyword, |
404 bool* is_keyword_hint) const { | 414 bool* is_keyword_hint) const { |
405 *is_keyword_hint = associated_keyword.get() != NULL; | 415 *is_keyword_hint = associated_keyword.get() != NULL; |
406 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : | 416 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : |
407 GetSubstitutingExplicitlyInvokedKeyword(profile)); | 417 GetSubstitutingExplicitlyInvokedKeyword(profile)); |
408 } | 418 } |
409 | 419 |
410 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( | 420 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( |
411 Profile* profile) const { | 421 Profile* profile) const { |
412 if (transition != content::PAGE_TRANSITION_KEYWORD) | 422 if (transition != content::PAGE_TRANSITION_KEYWORD) |
413 return base::string16(); | 423 return base::string16(); |
414 const TemplateURL* t_url = GetTemplateURL(profile, false); | 424 const TemplateURL* t_url = GetTemplateURL(profile, false); |
415 return (t_url && | 425 return (t_url && |
416 t_url->SupportsReplacement(UIThreadSearchTermsData(profile))) ? | 426 t_url->SupportsReplacement(UIThreadSearchTermsData(profile))) ? |
417 keyword : base::string16(); | 427 keyword : base::string16(); |
418 } | 428 } |
419 | 429 |
420 TemplateURL* AutocompleteMatch::GetTemplateURL( | 430 TemplateURL* AutocompleteMatch::GetTemplateURL( |
421 Profile* profile, bool allow_fallback_to_destination_host) const { | 431 Profile* profile, bool allow_fallback_to_destination_host) const { |
432 return GetTemplateURLWithKeyword( | |
433 profile, keyword, | |
434 allow_fallback_to_destination_host ? destination_url.host() | |
Peter Kasting
2014/06/30 23:02:58
Nit: Break after '?' instead of before ':'
(aka I
Mark P
2014/06/30 23:27:36
Done.
| |
435 : std::string()); | |
436 } | |
437 | |
438 // static | |
439 TemplateURL* AutocompleteMatch::GetTemplateURLWithKeyword( | |
440 Profile* profile, | |
441 const base::string16& keyword, | |
442 const std::string& host) { | |
422 DCHECK(profile); | 443 DCHECK(profile); |
423 TemplateURLService* template_url_service = | 444 TemplateURLService* template_url_service = |
424 TemplateURLServiceFactory::GetForProfile(profile); | 445 TemplateURLServiceFactory::GetForProfile(profile); |
425 if (template_url_service == NULL) | 446 if (template_url_service == NULL) |
426 return NULL; | 447 return NULL; |
427 TemplateURL* template_url = keyword.empty() ? NULL : | 448 TemplateURL* template_url = keyword.empty() ? NULL : |
428 template_url_service->GetTemplateURLForKeyword(keyword); | 449 template_url_service->GetTemplateURLForKeyword(keyword); |
429 if (template_url == NULL && allow_fallback_to_destination_host) { | 450 if (template_url == NULL && !host.empty()) { |
430 template_url = template_url_service->GetTemplateURLForHost( | 451 template_url = template_url_service->GetTemplateURLForHost(host); |
431 destination_url.host()); | |
432 } | 452 } |
433 return template_url; | 453 return template_url; |
434 } | 454 } |
435 | 455 |
436 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 456 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
437 const std::string& value) { | 457 const std::string& value) { |
438 DCHECK(!property.empty()); | 458 DCHECK(!property.empty()); |
439 DCHECK(!value.empty()); | 459 DCHECK(!value.empty()); |
440 additional_info[property] = value; | 460 additional_info[property] = value; |
441 } | 461 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
511 << " is unsorted in relation to last offset of " << last_offset | 531 << " is unsorted in relation to last offset of " << last_offset |
512 << ". Provider: " << provider_name << "."; | 532 << ". Provider: " << provider_name << "."; |
513 DCHECK_LT(i->offset, text.length()) | 533 DCHECK_LT(i->offset, text.length()) |
514 << " Classification of [" << i->offset << "," << text.length() | 534 << " Classification of [" << i->offset << "," << text.length() |
515 << "] is out of bounds for \"" << text << "\". Provider: " | 535 << "] is out of bounds for \"" << text << "\". Provider: " |
516 << provider_name << "."; | 536 << provider_name << "."; |
517 last_offset = i->offset; | 537 last_offset = i->offset; |
518 } | 538 } |
519 } | 539 } |
520 #endif | 540 #endif |
OLD | NEW |