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