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