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" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "chrome/browser/autocomplete/autocomplete_provider.h" | 14 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
15 #include "chrome/browser/search_engines/template_url_service.h" | 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 17 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" |
16 #include "components/search_engines/template_url.h" | 18 #include "components/search_engines/template_url.h" |
| 19 #include "content/public/common/url_constants.h" |
17 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
18 | 21 |
19 namespace { | 22 namespace { |
20 | 23 |
21 bool IsTrivialClassification(const ACMatchClassifications& classifications) { | 24 bool IsTrivialClassification(const ACMatchClassifications& classifications) { |
22 return classifications.empty() || | 25 return classifications.empty() || |
23 ((classifications.size() == 1) && | 26 ((classifications.size() == 1) && |
24 (classifications.back().style == ACMatchClassification::NONE)); | 27 (classifications.back().style == ACMatchClassification::NONE)); |
25 } | 28 } |
26 | 29 |
(...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
335 | 338 |
336 // static | 339 // static |
337 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { | 340 bool AutocompleteMatch::IsSpecializedSearchType(Type type) { |
338 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || | 341 return type == AutocompleteMatchType::SEARCH_SUGGEST_ENTITY || |
339 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || | 342 type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE || |
340 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || | 343 type == AutocompleteMatchType::SEARCH_SUGGEST_PERSONALIZED || |
341 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || | 344 type == AutocompleteMatchType::SEARCH_SUGGEST_PROFILE || |
342 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; | 345 type == AutocompleteMatchType::SEARCH_SUGGEST_ANSWER; |
343 } | 346 } |
344 | 347 |
345 void AutocompleteMatch::ComputeStrippedDestinationURL( | 348 void AutocompleteMatch::ComputeStrippedDestinationURL(Profile* profile) { |
346 TemplateURLService* template_url_service) { | |
347 stripped_destination_url = destination_url; | 349 stripped_destination_url = destination_url; |
348 if (!stripped_destination_url.is_valid()) | 350 if (!stripped_destination_url.is_valid()) |
349 return; | 351 return; |
350 | 352 |
351 // If the destination URL looks like it was generated from a TemplateURL, | 353 // If the destination URL looks like it was generated from a TemplateURL, |
352 // remove all substitutions other than the search terms. This allows us | 354 // remove all substitutions other than the search terms. This allows us |
353 // to eliminate cases like past search URLs from history that differ only | 355 // 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 | 356 // by some obscure query param from each other or from the search/keyword |
355 // provider matches. | 357 // provider matches. |
356 TemplateURL* template_url = GetTemplateURL(template_url_service, true); | 358 TemplateURL* template_url = GetTemplateURL(profile, true); |
| 359 UIThreadSearchTermsData search_terms_data(profile); |
357 if (template_url != NULL && | 360 if (template_url != NULL && |
358 template_url->SupportsReplacement( | 361 template_url->SupportsReplacement(search_terms_data)) { |
359 template_url_service->search_terms_data())) { | |
360 base::string16 search_terms; | 362 base::string16 search_terms; |
361 if (template_url->ExtractSearchTermsFromURL( | 363 if (template_url->ExtractSearchTermsFromURL(stripped_destination_url, |
362 stripped_destination_url, | 364 search_terms_data, |
363 template_url_service->search_terms_data(), | 365 &search_terms)) { |
364 &search_terms)) { | |
365 stripped_destination_url = | 366 stripped_destination_url = |
366 GURL(template_url->url_ref().ReplaceSearchTerms( | 367 GURL(template_url->url_ref().ReplaceSearchTerms( |
367 TemplateURLRef::SearchTermsArgs(search_terms), | 368 TemplateURLRef::SearchTermsArgs(search_terms), |
368 template_url_service->search_terms_data())); | 369 search_terms_data)); |
369 } | 370 } |
370 } | 371 } |
371 | 372 |
372 // |replacements| keeps all the substitions we're going to make to | 373 // |replacements| keeps all the substitions we're going to make to |
373 // from {destination_url} to {stripped_destination_url}. |need_replacement| | 374 // from {destination_url} to {stripped_destination_url}. |need_replacement| |
374 // is a helper variable that helps us keep track of whether we need | 375 // is a helper variable that helps us keep track of whether we need |
375 // to apply the replacement. | 376 // to apply the replacement. |
376 bool needs_replacement = false; | 377 bool needs_replacement = false; |
377 GURL::Replacements replacements; | 378 GURL::Replacements replacements; |
378 | 379 |
(...skipping 12 matching lines...) Expand all Loading... |
391 replacements.SetScheme(url::kHttpScheme, | 392 replacements.SetScheme(url::kHttpScheme, |
392 url::Component(0, strlen(url::kHttpScheme))); | 393 url::Component(0, strlen(url::kHttpScheme))); |
393 needs_replacement = true; | 394 needs_replacement = true; |
394 } | 395 } |
395 | 396 |
396 if (needs_replacement) | 397 if (needs_replacement) |
397 stripped_destination_url = stripped_destination_url.ReplaceComponents( | 398 stripped_destination_url = stripped_destination_url.ReplaceComponents( |
398 replacements); | 399 replacements); |
399 } | 400 } |
400 | 401 |
401 void AutocompleteMatch::GetKeywordUIState( | 402 void AutocompleteMatch::GetKeywordUIState(Profile* profile, |
402 TemplateURLService* template_url_service, | 403 base::string16* keyword, |
403 base::string16* keyword, | 404 bool* is_keyword_hint) const { |
404 bool* is_keyword_hint) const { | |
405 *is_keyword_hint = associated_keyword.get() != NULL; | 405 *is_keyword_hint = associated_keyword.get() != NULL; |
406 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : | 406 keyword->assign(*is_keyword_hint ? associated_keyword->keyword : |
407 GetSubstitutingExplicitlyInvokedKeyword(template_url_service)); | 407 GetSubstitutingExplicitlyInvokedKeyword(profile)); |
408 } | 408 } |
409 | 409 |
410 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( | 410 base::string16 AutocompleteMatch::GetSubstitutingExplicitlyInvokedKeyword( |
411 TemplateURLService* template_url_service) const { | 411 Profile* profile) const { |
412 if (transition != content::PAGE_TRANSITION_KEYWORD || | 412 if (transition != content::PAGE_TRANSITION_KEYWORD) |
413 template_url_service == NULL) { | |
414 return base::string16(); | 413 return base::string16(); |
415 } | 414 const TemplateURL* t_url = GetTemplateURL(profile, false); |
416 | |
417 const TemplateURL* t_url = GetTemplateURL(template_url_service, false); | |
418 return (t_url && | 415 return (t_url && |
419 t_url->SupportsReplacement( | 416 t_url->SupportsReplacement(UIThreadSearchTermsData(profile))) ? |
420 template_url_service->search_terms_data())) ? | |
421 keyword : base::string16(); | 417 keyword : base::string16(); |
422 } | 418 } |
423 | 419 |
424 TemplateURL* AutocompleteMatch::GetTemplateURL( | 420 TemplateURL* AutocompleteMatch::GetTemplateURL( |
425 TemplateURLService* template_url_service, | 421 Profile* profile, bool allow_fallback_to_destination_host) const { |
426 bool allow_fallback_to_destination_host) const { | 422 DCHECK(profile); |
| 423 TemplateURLService* template_url_service = |
| 424 TemplateURLServiceFactory::GetForProfile(profile); |
427 if (template_url_service == NULL) | 425 if (template_url_service == NULL) |
428 return NULL; | 426 return NULL; |
429 TemplateURL* template_url = keyword.empty() ? NULL : | 427 TemplateURL* template_url = keyword.empty() ? NULL : |
430 template_url_service->GetTemplateURLForKeyword(keyword); | 428 template_url_service->GetTemplateURLForKeyword(keyword); |
431 if (template_url == NULL && allow_fallback_to_destination_host) { | 429 if (template_url == NULL && allow_fallback_to_destination_host) { |
432 template_url = template_url_service->GetTemplateURLForHost( | 430 template_url = template_url_service->GetTemplateURLForHost( |
433 destination_url.host()); | 431 destination_url.host()); |
434 } | 432 } |
435 return template_url; | 433 return template_url; |
436 } | 434 } |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
513 << " is unsorted in relation to last offset of " << last_offset | 511 << " is unsorted in relation to last offset of " << last_offset |
514 << ". Provider: " << provider_name << "."; | 512 << ". Provider: " << provider_name << "."; |
515 DCHECK_LT(i->offset, text.length()) | 513 DCHECK_LT(i->offset, text.length()) |
516 << " Classification of [" << i->offset << "," << text.length() | 514 << " Classification of [" << i->offset << "," << text.length() |
517 << "] is out of bounds for \"" << text << "\". Provider: " | 515 << "] is out of bounds for \"" << text << "\". Provider: " |
518 << provider_name << "."; | 516 << provider_name << "."; |
519 last_offset = i->offset; | 517 last_offset = i->offset; |
520 } | 518 } |
521 } | 519 } |
522 #endif | 520 #endif |
OLD | NEW |