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 // Attempt to normalize URL for de-dupping purpose by removing non-essential | |
367 // term substitutions like aqs/oq/aq. | |
Peter Kasting
2012/10/26 22:13:49
Nit: URL -> the URL, purpose -> purposes.
Don't r
Bart N.
2012/10/26 23:36:55
Done.
| |
368 // We try to find a template URL using this match's keyword. If not found, | |
369 // we pick a template that matches the destination URL's host. | |
Peter Kasting
2012/10/26 22:13:49
Nit: This part can be omitted as it's documented i
Bart N.
2012/10/26 23:36:55
Done.
| |
370 // Finally, we check if the template supports substitutions and if so, | |
371 // we use it to generate a stripped destination URL. | |
372 TemplateURL* template_url = GetTemplateURL(profile, true); | |
373 if (template_url != NULL && template_url->SupportsReplacement()) { | |
374 string16 search_terms; | |
375 if (template_url->ExtractSearchTermsFromURL(stripped_destination_url, | |
376 &search_terms)) { | |
377 // Rewrite the URL by using the extracted search terms and ignoring other | |
378 // arguments (it's OK, because it doesn't affect the destination URL and | |
379 // is sufficient from de-dupping perspective). | |
Peter Kasting
2012/10/26 22:13:49
Nit: I'd remove this comment entirely; the suggest
Bart N.
2012/10/26 23:36:55
Done.
| |
380 stripped_destination_url = | |
381 GURL(template_url->url_ref().ReplaceSearchTerms( | |
382 TemplateURLRef::SearchTermsArgs(search_terms))); | |
383 } | |
384 } | |
385 | |
366 // |replacements| keeps all the substitions we're going to make to | 386 // |replacements| keeps all the substitions we're going to make to |
367 // from {destination_url} to {stripped_destination_url}. |need_replacement| | 387 // from {destination_url} to {stripped_destination_url}. |need_replacement| |
368 // is a helper variable that helps us keep track of whether we need | 388 // is a helper variable that helps us keep track of whether we need |
369 // to apply the replacement. | 389 // to apply the replacement. |
370 bool needs_replacement = false; | 390 bool needs_replacement = false; |
371 GURL::Replacements replacements; | 391 GURL::Replacements replacements; |
372 | 392 |
373 // Remove the www. prefix from the host. | 393 // Remove the www. prefix from the host. |
374 static const char prefix[] = "www."; | 394 static const char prefix[] = "www."; |
375 static const size_t prefix_len = arraysize(prefix) - 1; | 395 static const size_t prefix_len = arraysize(prefix) - 1; |
376 std::string host = destination_url.host(); | 396 std::string host = stripped_destination_url.host(); |
377 if (host.compare(0, prefix_len, prefix) == 0) { | 397 if (host.compare(0, prefix_len, prefix) == 0) { |
378 host = host.substr(prefix_len); | 398 host = host.substr(prefix_len); |
379 replacements.SetHostStr(host); | 399 replacements.SetHostStr(host); |
380 needs_replacement = true; | 400 needs_replacement = true; |
381 } | 401 } |
382 | 402 |
383 // Replace https protocol with http protocol. | 403 // Replace https protocol with http protocol. |
384 if (stripped_destination_url.SchemeIs(chrome::kHttpsScheme)) { | 404 if (stripped_destination_url.SchemeIs(chrome::kHttpsScheme)) { |
385 replacements.SetScheme( | 405 replacements.SetScheme( |
386 chrome::kHttpScheme, | 406 chrome::kHttpScheme, |
387 url_parse::Component(0, strlen(chrome::kHttpScheme))); | 407 url_parse::Component(0, strlen(chrome::kHttpScheme))); |
388 needs_replacement = true; | 408 needs_replacement = true; |
389 } | 409 } |
390 | 410 |
391 if (needs_replacement) | 411 if (needs_replacement) |
392 stripped_destination_url = destination_url.ReplaceComponents(replacements); | 412 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
413 replacements); | |
393 } | 414 } |
394 | 415 |
395 void AutocompleteMatch::GetKeywordUIState(Profile* profile, | 416 void AutocompleteMatch::GetKeywordUIState(Profile* profile, |
396 string16* keyword, | 417 string16* keyword, |
397 bool* is_keyword_hint) const { | 418 bool* is_keyword_hint) const { |
398 *is_keyword_hint = associated_keyword.get() != NULL; | 419 *is_keyword_hint = associated_keyword.get() != NULL; |
399 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : | 420 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : |
400 GetSubstitutingExplicitlyInvokedKeyword(profile)); | 421 GetSubstitutingExplicitlyInvokedKeyword(profile)); |
401 } | 422 } |
402 | 423 |
403 string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( | 424 string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( |
404 Profile* profile) const { | 425 Profile* profile) const { |
405 if (transition != content::PAGE_TRANSITION_KEYWORD) | 426 if (transition != content::PAGE_TRANSITION_KEYWORD) |
406 return string16(); | 427 return string16(); |
407 const TemplateURL* t_url = GetTemplateURL(profile); | 428 const TemplateURL* t_url = GetTemplateURL(profile); |
408 return (t_url && t_url->SupportsReplacement()) ? keyword : string16(); | 429 return (t_url && t_url->SupportsReplacement()) ? keyword : string16(); |
409 } | 430 } |
410 | 431 |
411 TemplateURL* AutocompleteMatch::GetTemplateURL(Profile* profile) const { | 432 TemplateURL* AutocompleteMatch::GetTemplateURL(Profile* profile) const { |
433 return GetTemplateURL(profile, false); | |
434 } | |
435 | |
436 TemplateURL* AutocompleteMatch::GetTemplateURL(Profile* profile, | |
437 bool use_host) const { | |
412 DCHECK(profile); | 438 DCHECK(profile); |
413 return keyword.empty() ? NULL : | 439 TemplateURLService* template_url_service = |
414 TemplateURLServiceFactory::GetForProfile(profile)-> | 440 TemplateURLServiceFactory::GetForProfile(profile); |
415 GetTemplateURLForKeyword(keyword); | 441 TemplateURL* template_url = keyword.empty() ? NULL : |
442 template_url_service->GetTemplateURLForKeyword(keyword); | |
443 if (use_host && template_url == NULL) { | |
444 template_url = template_url_service->GetTemplateURLForHost( | |
445 destination_url.host()); | |
446 } | |
447 return template_url; | |
416 } | 448 } |
417 | 449 |
418 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 450 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
419 const std::string& value) { | 451 const std::string& value) { |
420 DCHECK(property.size()); | 452 DCHECK(property.size()); |
421 DCHECK(value.size()); | 453 DCHECK(value.size()); |
422 additional_info[property] = value; | 454 additional_info[property] = value; |
423 } | 455 } |
424 | 456 |
425 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 457 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 | 496 << " is unsorted in relation to last offset of " << last_offset |
465 << ". Provider: " << provider_name << "."; | 497 << ". Provider: " << provider_name << "."; |
466 DCHECK_LT(i->offset, text.length()) | 498 DCHECK_LT(i->offset, text.length()) |
467 << " Classification of [" << i->offset << "," << text.length() | 499 << " Classification of [" << i->offset << "," << text.length() |
468 << "] is out of bounds for \"" << text << "\". Provider: " | 500 << "] is out of bounds for \"" << text << "\". Provider: " |
469 << provider_name << "."; | 501 << provider_name << "."; |
470 last_offset = i->offset; | 502 last_offset = i->offset; |
471 } | 503 } |
472 } | 504 } |
473 #endif | 505 #endif |
OLD | NEW |