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/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 } | 351 } |
352 | 352 |
353 // static | 353 // static |
354 bool AutocompleteMatch::IsSearchType(Type type) { | 354 bool AutocompleteMatch::IsSearchType(Type type) { |
355 return type == SEARCH_WHAT_YOU_TYPED || | 355 return type == SEARCH_WHAT_YOU_TYPED || |
356 type == SEARCH_HISTORY || | 356 type == SEARCH_HISTORY || |
357 type == SEARCH_SUGGEST || | 357 type == SEARCH_SUGGEST || |
358 type == SEARCH_OTHER_ENGINE; | 358 type == SEARCH_OTHER_ENGINE; |
359 } | 359 } |
360 | 360 |
361 void AutocompleteMatch::ComputeStrippedDestinationURL() { | 361 void AutocompleteMatch::ComputeStrippedDestinationURL(Profile* profile) { |
362 stripped_destination_url = destination_url; | 362 stripped_destination_url = destination_url; |
363 if (!stripped_destination_url.is_valid()) | 363 if (!stripped_destination_url.is_valid()) |
364 return; | 364 return; |
365 | 365 |
| 366 // If the destination URL looks like it was generated from a TemplateURL, |
| 367 // remove all substitutions other than the search terms. This allows us |
| 368 // to eliminate cases like past search URLs from history that differ only |
| 369 // by some obscure query param from each other or from the search/keyword |
| 370 // provider matches. |
| 371 TemplateURL* template_url = GetTemplateURL(profile, true); |
| 372 if (template_url != NULL && template_url->SupportsReplacement()) { |
| 373 string16 search_terms; |
| 374 if (template_url->ExtractSearchTermsFromURL(stripped_destination_url, |
| 375 &search_terms)) { |
| 376 stripped_destination_url = |
| 377 GURL(template_url->url_ref().ReplaceSearchTerms( |
| 378 TemplateURLRef::SearchTermsArgs(search_terms))); |
| 379 } |
| 380 } |
| 381 |
366 // |replacements| keeps all the substitions we're going to make to | 382 // |replacements| keeps all the substitions we're going to make to |
367 // from {destination_url} to {stripped_destination_url}. |need_replacement| | 383 // from {destination_url} to {stripped_destination_url}. |need_replacement| |
368 // is a helper variable that helps us keep track of whether we need | 384 // is a helper variable that helps us keep track of whether we need |
369 // to apply the replacement. | 385 // to apply the replacement. |
370 bool needs_replacement = false; | 386 bool needs_replacement = false; |
371 GURL::Replacements replacements; | 387 GURL::Replacements replacements; |
372 | 388 |
373 // Remove the www. prefix from the host. | 389 // Remove the www. prefix from the host. |
374 static const char prefix[] = "www."; | 390 static const char prefix[] = "www."; |
375 static const size_t prefix_len = arraysize(prefix) - 1; | 391 static const size_t prefix_len = arraysize(prefix) - 1; |
376 std::string host = destination_url.host(); | 392 std::string host = stripped_destination_url.host(); |
377 if (host.compare(0, prefix_len, prefix) == 0) { | 393 if (host.compare(0, prefix_len, prefix) == 0) { |
378 host = host.substr(prefix_len); | 394 host = host.substr(prefix_len); |
379 replacements.SetHostStr(host); | 395 replacements.SetHostStr(host); |
380 needs_replacement = true; | 396 needs_replacement = true; |
381 } | 397 } |
382 | 398 |
383 // Replace https protocol with http protocol. | 399 // Replace https protocol with http protocol. |
384 if (stripped_destination_url.SchemeIs(chrome::kHttpsScheme)) { | 400 if (stripped_destination_url.SchemeIs(chrome::kHttpsScheme)) { |
385 replacements.SetScheme( | 401 replacements.SetScheme( |
386 chrome::kHttpScheme, | 402 chrome::kHttpScheme, |
387 url_parse::Component(0, strlen(chrome::kHttpScheme))); | 403 url_parse::Component(0, strlen(chrome::kHttpScheme))); |
388 needs_replacement = true; | 404 needs_replacement = true; |
389 } | 405 } |
390 | 406 |
391 if (needs_replacement) | 407 if (needs_replacement) |
392 stripped_destination_url = destination_url.ReplaceComponents(replacements); | 408 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
| 409 replacements); |
393 } | 410 } |
394 | 411 |
395 void AutocompleteMatch::GetKeywordUIState(Profile* profile, | 412 void AutocompleteMatch::GetKeywordUIState(Profile* profile, |
396 string16* keyword, | 413 string16* keyword, |
397 bool* is_keyword_hint) const { | 414 bool* is_keyword_hint) const { |
398 *is_keyword_hint = associated_keyword.get() != NULL; | 415 *is_keyword_hint = associated_keyword.get() != NULL; |
399 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : | 416 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : |
400 GetSubstitutingExplicitlyInvokedKeyword(profile)); | 417 GetSubstitutingExplicitlyInvokedKeyword(profile)); |
401 } | 418 } |
402 | 419 |
403 string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( | 420 string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( |
404 Profile* profile) const { | 421 Profile* profile) const { |
405 if (transition != content::PAGE_TRANSITION_KEYWORD) | 422 if (transition != content::PAGE_TRANSITION_KEYWORD) |
406 return string16(); | 423 return string16(); |
407 const TemplateURL* t_url = GetTemplateURL(profile); | 424 const TemplateURL* t_url = GetTemplateURL(profile, false); |
408 return (t_url && t_url->SupportsReplacement()) ? keyword : string16(); | 425 return (t_url && t_url->SupportsReplacement()) ? keyword : string16(); |
409 } | 426 } |
410 | 427 |
411 TemplateURL* AutocompleteMatch::GetTemplateURL(Profile* profile) const { | 428 TemplateURL* AutocompleteMatch::GetTemplateURL( |
| 429 Profile* profile, bool allow_fallback_to_destination_host) const { |
412 DCHECK(profile); | 430 DCHECK(profile); |
413 return keyword.empty() ? NULL : | 431 TemplateURLService* template_url_service = |
414 TemplateURLServiceFactory::GetForProfile(profile)-> | 432 TemplateURLServiceFactory::GetForProfile(profile); |
415 GetTemplateURLForKeyword(keyword); | 433 if (template_url_service == NULL) |
| 434 return NULL; |
| 435 TemplateURL* template_url = keyword.empty() ? NULL : |
| 436 template_url_service->GetTemplateURLForKeyword(keyword); |
| 437 if (template_url == NULL && allow_fallback_to_destination_host) { |
| 438 template_url = template_url_service->GetTemplateURLForHost( |
| 439 destination_url.host()); |
| 440 } |
| 441 return template_url; |
416 } | 442 } |
417 | 443 |
418 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 444 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
419 const std::string& value) { | 445 const std::string& value) { |
420 DCHECK(property.size()); | 446 DCHECK(property.size()); |
421 DCHECK(value.size()); | 447 DCHECK(value.size()); |
422 additional_info[property] = value; | 448 additional_info[property] = value; |
423 } | 449 } |
424 | 450 |
425 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 451 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 << " is unsorted in relation to last offset of " << last_offset | 490 << " is unsorted in relation to last offset of " << last_offset |
465 << ". Provider: " << provider_name << "."; | 491 << ". Provider: " << provider_name << "."; |
466 DCHECK_LT(i->offset, text.length()) | 492 DCHECK_LT(i->offset, text.length()) |
467 << " Classification of [" << i->offset << "," << text.length() | 493 << " Classification of [" << i->offset << "," << text.length() |
468 << "] is out of bounds for \"" << text << "\". Provider: " | 494 << "] is out of bounds for \"" << text << "\". Provider: " |
469 << provider_name << "."; | 495 << provider_name << "."; |
470 last_offset = i->offset; | 496 last_offset = i->offset; |
471 } | 497 } |
472 } | 498 } |
473 #endif | 499 #endif |
OLD | NEW |