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 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 | 335 |
336 // static | 336 // static |
337 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { | 337 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { |
338 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | 338 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || |
339 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || | 339 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || |
340 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || | 340 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || |
341 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || | 341 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || |
342 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; | 342 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; |
343 } | 343 } |
344 | 344 |
345 void AutocompleteMatch::ComputeStrippedDestinationURL( | 345 // static |
346 TemplateURLService* template_url_service) { | 346 TemplateURL* AutocompleteMatch::GetTemplateURLWithKeyword( |
347 stripped_destination_url = destination_url; | 347 TemplateURLService* template_url_service, |
348 if (!stripped_destination_url.is_valid()) | 348 const base::string16& keyword, |
349 return; | 349 const std::string& host) { |
| 350 if (template_url_service == NULL) |
| 351 return NULL; |
| 352 TemplateURL* template_url = keyword.empty() ? |
| 353 NULL : template_url_service->GetTemplateURLForKeyword(keyword); |
| 354 return (template_url || host.empty()) ? |
| 355 template_url : template_url_service->GetTemplateURLForHost(host); |
| 356 } |
| 357 |
| 358 // static |
| 359 GURL AutocompleteMatch::GURLToStrippedGURL( |
| 360 const GURL& url, |
| 361 TemplateURLService* template_url_service, |
| 362 const base::string16& keyword) { |
| 363 if (!url.is_valid()) |
| 364 return url; |
| 365 |
| 366 GURL stripped_destination_url = url; |
350 | 367 |
351 // If the destination URL looks like it was generated from a TemplateURL, | 368 // If the destination URL looks like it was generated from a TemplateURL, |
352 // remove all substitutions other than the search terms. This allows us | 369 // remove all substitutions other than the search terms. This allows us |
353 // to eliminate cases like past search URLs from history that differ only | 370 // to eliminate cases like past search URLs from history that differ only |
354 // by some obscure query param from each other or from the search/keyword | 371 // by some obscure query param from each other or from the search/keyword |
355 // provider matches. | 372 // provider matches. |
356 TemplateURL* template_url = GetTemplateURL(template_url_service, true); | 373 TemplateURL* template_url = GetTemplateURLWithKeyword( |
| 374 template_url_service, keyword, stripped_destination_url.host()); |
357 if (template_url != NULL && | 375 if (template_url != NULL && |
358 template_url->SupportsReplacement( | 376 template_url->SupportsReplacement( |
359 template_url_service->search_terms_data())) { | 377 template_url_service->search_terms_data())) { |
360 base::string16 search_terms; | 378 base::string16 search_terms; |
361 if (template_url->ExtractSearchTermsFromURL( | 379 if (template_url->ExtractSearchTermsFromURL( |
362 stripped_destination_url, | 380 stripped_destination_url, |
363 template_url_service->search_terms_data(), | 381 template_url_service->search_terms_data(), |
364 &search_terms)) { | 382 &search_terms)) { |
365 stripped_destination_url = | 383 stripped_destination_url = |
366 GURL(template_url->url_ref().ReplaceSearchTerms( | 384 GURL(template_url->url_ref().ReplaceSearchTerms( |
(...skipping 22 matching lines...) Expand all Loading... |
389 // Replace https protocol with http protocol. | 407 // Replace https protocol with http protocol. |
390 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { | 408 if (stripped_destination_url.SchemeIs(url::kHttpsScheme)) { |
391 replacements.SetScheme(url::kHttpScheme, | 409 replacements.SetScheme(url::kHttpScheme, |
392 url::Component(0, strlen(url::kHttpScheme))); | 410 url::Component(0, strlen(url::kHttpScheme))); |
393 needs_replacement = true; | 411 needs_replacement = true; |
394 } | 412 } |
395 | 413 |
396 if (needs_replacement) | 414 if (needs_replacement) |
397 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 415 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
398 replacements); | 416 replacements); |
| 417 return stripped_destination_url; |
| 418 } |
| 419 |
| 420 void AutocompleteMatch::ComputeStrippedDestinationURL( |
| 421 TemplateURLService* template_url_service) { |
| 422 stripped_destination_url = |
| 423 GURLToStrippedGURL(destination_url, template_url_service, keyword); |
| 424 } |
| 425 |
| 426 void AutocompleteMatch::EnsureUWYTIsAllowedToBeDefault( |
| 427 const GURL& canonical_input_url, |
| 428 TemplateURLService* template_url_service) { |
| 429 if (!allowed_to_be_default_match) { |
| 430 const GURL& stripped_canonical_input_url = |
| 431 AutocompleteMatch::GURLToStrippedGURL( |
| 432 canonical_input_url, template_url_service, base::string16()); |
| 433 ComputeStrippedDestinationURL(template_url_service); |
| 434 allowed_to_be_default_match = |
| 435 stripped_canonical_input_url == stripped_destination_url; |
| 436 } |
399 } | 437 } |
400 | 438 |
401 void AutocompleteMatch::GetKeywordUIState( | 439 void AutocompleteMatch::GetKeywordUIState( |
402 TemplateURLService* template_url_service, | 440 TemplateURLService* template_url_service, |
403 base::string16* keyword, | 441 base::string16* keyword, |
404 bool* is_keyword_hint) const { | 442 bool* is_keyword_hint) const { |
405 *is_keyword_hint = associated_keyword.get() != NULL; | 443 *is_keyword_hint = associated_keyword.get() != NULL; |
406 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : | 444 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : |
407 GetSubstitutingExplicitlyInvokedKeyword(template_url_service)); | 445 GetSubstitutingExplicitlyInvokedKeyword(template_url_service)); |
408 } | 446 } |
409 | 447 |
410 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( | 448 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( |
411 TemplateURLService* template_url_service) const { | 449 TemplateURLService* template_url_service) const { |
412 if (transition != content::PAGE_TRANSITION_KEYWORD || | 450 if (transition != content::PAGE_TRANSITION_KEYWORD || |
413 template_url_service == NULL) { | 451 template_url_service == NULL) { |
414 return base::string16(); | 452 return base::string16(); |
415 } | 453 } |
416 | 454 |
417 const TemplateURL* t_url = GetTemplateURL(template_url_service, false); | 455 const TemplateURL* t_url = GetTemplateURL(template_url_service, false); |
418 return (t_url && | 456 return (t_url && |
419 t_url->SupportsReplacement( | 457 t_url->SupportsReplacement( |
420 template_url_service->search_terms_data())) ? | 458 template_url_service->search_terms_data())) ? |
421 keyword : base::string16(); | 459 keyword : base::string16(); |
422 } | 460 } |
423 | 461 |
424 TemplateURL* AutocompleteMatch::GetTemplateURL( | 462 TemplateURL* AutocompleteMatch::GetTemplateURL( |
425 TemplateURLService* template_url_service, | 463 TemplateURLService* template_url_service, |
426 bool allow_fallback_to_destination_host) const { | 464 bool allow_fallback_to_destination_host) const { |
427 if (template_url_service == NULL) | 465 return GetTemplateURLWithKeyword( |
428 return NULL; | 466 template_url_service, keyword, |
429 TemplateURL* template_url = keyword.empty() ? NULL : | 467 allow_fallback_to_destination_host ? |
430 template_url_service->GetTemplateURLForKeyword(keyword); | 468 destination_url.host() : std::string()); |
431 if (template_url == NULL && allow_fallback_to_destination_host) { | |
432 template_url = template_url_service->GetTemplateURLForHost( | |
433 destination_url.host()); | |
434 } | |
435 return template_url; | |
436 } | 469 } |
437 | 470 |
438 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 471 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
439 const std::string& value) { | 472 const std::string& value) { |
440 DCHECK(!property.empty()); | 473 DCHECK(!property.empty()); |
441 DCHECK(!value.empty()); | 474 DCHECK(!value.empty()); |
442 additional_info[property] = value; | 475 additional_info[property] = value; |
443 } | 476 } |
444 | 477 |
445 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, | 478 void AutocompleteMatch::RecordAdditionalInfo(const std::string& property, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 << " is unsorted in relation to last offset of " << last_offset | 546 << " is unsorted in relation to last offset of " << last_offset |
514 << ". Provider: " << provider_name << "."; | 547 << ". Provider: " << provider_name << "."; |
515 DCHECK_LT(i->offset, text.length()) | 548 DCHECK_LT(i->offset, text.length()) |
516 << " Classification of [" << i->offset << "," << text.length() | 549 << " Classification of [" << i->offset << "," << text.length() |
517 << "] is out of bounds for \"" << text << "\". Provider: " | 550 << "] is out of bounds for \"" << text << "\". Provider: " |
518 << provider_name << "."; | 551 << provider_name << "."; |
519 last_offset = i->offset; | 552 last_offset = i->offset; |
520 } | 553 } |
521 } | 554 } |
522 #endif | 555 #endif |
OLD | NEW |