Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(176)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_match.cc

Issue 354773002: Reduces dependency from AutocompleteMatch to Profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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,
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
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 template_url_service == NULL) {
413 return base::string16(); 414 return base::string16();
414 const TemplateURL* t_url = GetTemplateURL(profile, false); 415 }
416
417 const TemplateURL* t_url = GetTemplateURL(template_url_service, false);
415 return (t_url && 418 return (t_url &&
416 t_url->SupportsReplacement(UIThreadSearchTermsData(profile))) ? 419 t_url->SupportsReplacement(
420 template_url_service->search_terms_data())) ?
417 keyword : base::string16(); 421 keyword : base::string16();
418 } 422 }
419 423
420 TemplateURL* AutocompleteMatch::GetTemplateURL( 424 TemplateURL* AutocompleteMatch::GetTemplateURL(
421 Profile* profile, bool allow_fallback_to_destination_host) const { 425 TemplateURLService* template_url_service,
422 DCHECK(profile); 426 bool allow_fallback_to_destination_host) const {
423 TemplateURLService* template_url_service =
424 TemplateURLServiceFactory::GetForProfile(profile);
425 if (template_url_service == NULL) 427 if (template_url_service == NULL)
426 return NULL; 428 return NULL;
427 TemplateURL* template_url = keyword.empty() ? NULL : 429 TemplateURL* template_url = keyword.empty() ? NULL :
428 template_url_service->GetTemplateURLForKeyword(keyword); 430 template_url_service->GetTemplateURLForKeyword(keyword);
429 if (template_url == NULL && allow_fallback_to_destination_host) { 431 if (template_url == NULL && allow_fallback_to_destination_host) {
430 template_url = template_url_service->GetTemplateURLForHost( 432 template_url = template_url_service->GetTemplateURLForHost(
431 destination_url.host()); 433 destination_url.host());
432 } 434 }
433 return template_url; 435 return template_url;
434 } 436 }
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 << " is unsorted in relation to last offset of " << last_offset 513 << " is unsorted in relation to last offset of " << last_offset
512 << ". Provider: " << provider_name << "."; 514 << ". Provider: " << provider_name << ".";
513 DCHECK_LT(i->offset, text.length()) 515 DCHECK_LT(i->offset, text.length())
514 << " Classification of [" << i->offset << "," << text.length() 516 << " Classification of [" << i->offset << "," << text.length()
515 << "] is out of bounds for \"" << text << "\". Provider: " 517 << "] is out of bounds for \"" << text << "\". Provider: "
516 << provider_name << "."; 518 << provider_name << ".";
517 last_offset = i->offset; 519 last_offset = i->offset;
518 } 520 }
519 } 521 }
520 #endif 522 #endif
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/autocomplete_match.h ('k') | chrome/browser/autocomplete/autocomplete_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698