Stop depending on TemplateURLServiceFactory from SearchProvider

BUG=388515

Review URL: https://codereview.chromium.org/439243004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287487 0039d316-1c4b-4281-b951-d872f2087c98
parent b3ffb4c6
......@@ -200,13 +200,14 @@ AutocompleteController::AutocompleteController(
}
#endif
if (provider_types & AutocompleteProvider::TYPE_SEARCH) {
search_provider_ = new SearchProvider(this, profile);
search_provider_ = new SearchProvider(this, template_url_service, profile);
providers_.push_back(search_provider_);
}
if (provider_types & AutocompleteProvider::TYPE_SHORTCUTS)
providers_.push_back(new ShortcutsProvider(profile));
if (provider_types & AutocompleteProvider::TYPE_ZERO_SUGGEST) {
zero_suggest_provider_ = ZeroSuggestProvider::Create(this, profile);
zero_suggest_provider_ = ZeroSuggestProvider::Create(
this, template_url_service, profile);
if (zero_suggest_provider_)
providers_.push_back(zero_suggest_provider_);
}
......
......@@ -16,9 +16,6 @@
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/omnibox/omnibox_field_trial.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/search_engines/ui_thread_search_terms_data.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/common/pref_names.h"
......@@ -29,7 +26,6 @@
#include "components/search_engines/template_url_prepopulate_data.h"
#include "components/search_engines/template_url_service.h"
#include "components/sync_driver/sync_prefs.h"
#include "content/public/common/url_constants.h"
#include "net/base/escape.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/url_request/url_fetcher.h"
......@@ -99,10 +95,12 @@ const int BaseSearchProvider::kKeywordProviderURLFetcherID = 2;
const int BaseSearchProvider::kDeletionURLFetcherID = 3;
BaseSearchProvider::BaseSearchProvider(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile,
AutocompleteProvider::Type type)
: AutocompleteProvider(type),
listener_(listener),
template_url_service_(template_url_service),
profile_(profile),
field_trial_triggered_(false),
field_trial_triggered_in_session_(false),
......@@ -149,9 +147,8 @@ void BaseSearchProvider::DeleteMatch(const AutocompleteMatch& match) {
HistoryService* const history_service =
HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
TemplateURLService* template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_);
TemplateURL* template_url = match.GetTemplateURL(template_url_service, false);
TemplateURL* template_url =
match.GetTemplateURL(template_url_service_, false);
// This may be NULL if the template corresponding to the keyword has been
// deleted or there is no keyword set.
if (template_url != NULL) {
......@@ -197,12 +194,11 @@ void BaseSearchProvider::SetDeletionURL(const std::string& deletion_url,
AutocompleteMatch* match) {
if (deletion_url.empty())
return;
TemplateURLService* template_service =
TemplateURLServiceFactory::GetForProfile(profile_);
if (!template_service)
if (!template_url_service_)
return;
GURL url = template_service->GetDefaultSearchProvider()->GenerateSearchURL(
template_service->search_terms_data());
GURL url =
template_url_service_->GetDefaultSearchProvider()->GenerateSearchURL(
template_url_service_->search_terms_data());
url = url.GetOrigin().Resolve(deletion_url);
if (url.is_valid()) {
match->RecordAdditionalInfo(BaseSearchProvider::kDeletionUrlKey,
......@@ -295,6 +291,7 @@ bool BaseSearchProvider::ZeroSuggestEnabled(
const GURL& suggest_url,
const TemplateURL* template_url,
OmniboxEventProto::PageClassification page_classification,
const SearchTermsData& search_terms_data,
Profile* profile) {
if (!OmniboxFieldTrial::InZeroSuggestFieldTrial())
return false;
......@@ -324,7 +321,6 @@ bool BaseSearchProvider::ZeroSuggestEnabled(
// Only make the request if we know that the provider supports zero suggest
// (currently only the prepopulated Google provider).
UIThreadSearchTermsData search_terms_data(profile);
if (template_url == NULL ||
!template_url->SupportsReplacement(search_terms_data) ||
TemplateURLPrepopulateData::GetEngineType(
......@@ -340,9 +336,10 @@ bool BaseSearchProvider::CanSendURL(
const GURL& suggest_url,
const TemplateURL* template_url,
OmniboxEventProto::PageClassification page_classification,
const SearchTermsData& search_terms_data,
Profile* profile) {
if (!ZeroSuggestEnabled(suggest_url, template_url, page_classification,
profile))
search_terms_data, profile))
return false;
if (!current_page_url.is_valid())
......@@ -412,7 +409,7 @@ void BaseSearchProvider::AddMatchToMap(
AutocompleteMatch match = CreateSearchSuggestion(
this, GetInput(result.from_keyword_provider()), result,
GetTemplateURL(result.from_keyword_provider()),
UIThreadSearchTermsData(profile_), accepted_suggestion,
template_url_service_->search_terms_data(), accepted_suggestion,
ShouldAppendExtraParams(result));
if (!match.destination_url.is_valid())
return;
......
......@@ -26,8 +26,10 @@
class AutocompleteProviderListener;
class GURL;
class Profile;
class SearchTermsData;
class SuggestionDeletionHandler;
class TemplateURL;
class TemplateURLService;
namespace base {
class DictionaryValue;
......@@ -51,6 +53,7 @@ class BaseSearchProvider : public AutocompleteProvider,
static const int kDeletionURLFetcherID;
BaseSearchProvider(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile,
AutocompleteProvider::Type type);
......@@ -142,6 +145,7 @@ class BaseSearchProvider : public AutocompleteProvider,
const GURL& suggest_url,
const TemplateURL* template_url,
metrics::OmniboxEventProto::PageClassification page_classification,
const SearchTermsData& search_terms_data,
Profile* profile);
// Returns whether we can send the URL of the current page in any suggest
......@@ -165,6 +169,7 @@ class BaseSearchProvider : public AutocompleteProvider,
const GURL& suggest_url,
const TemplateURL* template_url,
metrics::OmniboxEventProto::PageClassification page_classification,
const SearchTermsData& search_terms_data,
Profile* profile);
// net::URLFetcherDelegate:
......@@ -252,6 +257,7 @@ class BaseSearchProvider : public AutocompleteProvider,
virtual void UpdateMatches() = 0;
AutocompleteProviderListener* listener_;
TemplateURLService* template_url_service_;
Profile* profile_;
// Whether a field trial, if any, has triggered in the most recent
......
......@@ -28,9 +28,6 @@
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/omnibox/omnibox_field_trial.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/search/instant_controller.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/autocomplete/autocomplete_provider_listener.h"
#include "components/autocomplete/url_prefix.h"
......@@ -133,9 +130,11 @@ class SearchProvider::CompareScoredResults {
int SearchProvider::kMinimumTimeBetweenSuggestQueriesMs = 100;
SearchProvider::SearchProvider(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile)
: BaseSearchProvider(listener, profile, AutocompleteProvider::TYPE_SEARCH),
providers_(TemplateURLServiceFactory::GetForProfile(profile)) {
: BaseSearchProvider(listener, template_url_service, profile,
AutocompleteProvider::TYPE_SEARCH),
providers_(template_url_service) {
}
// static
......@@ -667,7 +666,8 @@ net::URLFetcher* SearchProvider::CreateSuggestFetcher(
// Send the current page URL if user setting and URL requirements are met and
// the user is in the field trial.
if (CanSendURL(current_page_url_, suggest_url, template_url,
input.current_page_classification(), profile_) &&
input.current_page_classification(),
template_url_service_->search_terms_data(), profile_) &&
OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial()) {
search_term_args.current_page_url = current_page_url_.spec();
// Create the suggest URL again with the current page URL.
......
......@@ -45,7 +45,9 @@ class URLFetcher;
// suggestions.
class SearchProvider : public BaseSearchProvider {
public:
SearchProvider(AutocompleteProviderListener* listener, Profile* profile);
SearchProvider(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile);
// Extracts the suggest response metadata which SearchProvider previously
// stored for |match|.
......
......@@ -39,6 +39,7 @@
#include "components/metrics/proto/omnibox_event.pb.h"
#include "components/search_engines/search_engine_type.h"
#include "components/search_engines/search_engines_switches.h"
#include "components/search_engines/search_terms_data.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "components/signin/core/browser/signin_manager.h"
......@@ -66,8 +67,8 @@ ACMatches::const_iterator FindDefaultMatch(const ACMatches& matches) {
class SuggestionDeletionHandler;
class SearchProviderForTest : public SearchProvider {
public:
SearchProviderForTest(
AutocompleteProviderListener* listener,
SearchProviderForTest(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile);
bool is_success() { return is_success_; };
......@@ -82,8 +83,10 @@ class SearchProviderForTest : public SearchProvider {
SearchProviderForTest::SearchProviderForTest(
AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile)
: SearchProvider(listener, profile), is_success_(false) {
: SearchProvider(listener, template_url_service, profile),
is_success_(false) {
}
SearchProviderForTest::~SearchProviderForTest() {
......@@ -285,7 +288,7 @@ void SearchProviderTest::SetUp() {
// requests to ensure the InMemoryDatabase is the state we expect it.
profile_.BlockUntilHistoryProcessesPendingRequests();
provider_ = new SearchProviderForTest(this, &profile_);
provider_ = new SearchProviderForTest(this, turl_model, &profile_);
provider_->kMinimumTimeBetweenSuggestQueriesMs = 0;
}
......@@ -2945,7 +2948,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
SigninManagerBase* signin = SigninManagerFactory::GetForProfile(&profile_);
signin->SetAuthenticatedUsername("test");
......@@ -2953,7 +2956,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_TRUE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
// Not in field trial.
ResetFieldTrialList();
......@@ -2961,7 +2964,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
ResetFieldTrialList();
CreateZeroSuggestFieldTrial(true);
......@@ -2969,59 +2972,61 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("badpageurl"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
// Invalid page classification.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS,
&profile_));
SearchTermsData(), &profile_));
// Invalid page classification.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::INSTANT_NTP_WITH_OMNIBOX_AS_STARTING_FOCUS,
&profile_));
SearchTermsData(), &profile_));
// HTTPS page URL on same domain as provider.
EXPECT_TRUE(SearchProvider::CanSendURL(
GURL("https://www.google.com/search"),
GURL("https://www.google.com/complete/search"),
&google_template_url, metrics::OmniboxEventProto::OTHER, &profile_));
&google_template_url, metrics::OmniboxEventProto::OTHER,
SearchTermsData(), &profile_));
// Non-HTTP[S] page URL on same domain as provider.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("ftp://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
// Non-HTTP page URL on different domain.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("https://www.notgoogle.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
// Non-HTTPS provider.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("http://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
// Suggest disabled.
profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, false);
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
profile_.GetPrefs()->SetBoolean(prefs::kSearchSuggestEnabled, true);
// Incognito.
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, profile_.GetOffTheRecordProfile()));
metrics::OmniboxEventProto::OTHER, SearchTermsData(),
profile_.GetOffTheRecordProfile()));
// Tab sync not enabled.
profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncKeepEverythingSynced,
......@@ -3030,7 +3035,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
profile_.GetPrefs()->SetBoolean(sync_driver::prefs::kSyncTabs, true);
// Tab sync is encrypted.
......@@ -3042,7 +3047,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_FALSE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
encrypted_types.Remove(syncer::SESSIONS);
service->OnEncryptedTypesChanged(encrypted_types, false);
......@@ -3050,7 +3055,7 @@ TEST_F(SearchProviderTest, CanSendURL) {
EXPECT_TRUE(SearchProvider::CanSendURL(
GURL("http://www.google.com/search"),
GURL("https://www.google.com/complete/search"), &google_template_url,
metrics::OmniboxEventProto::OTHER, &profile_));
metrics::OmniboxEventProto::OTHER, SearchTermsData(), &profile_));
}
TEST_F(SearchProviderTest, TestDeleteMatch) {
......
......@@ -22,9 +22,7 @@
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/omnibox/omnibox_field_trial.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/autocomplete/autocomplete_input.h"
#include "components/autocomplete/autocomplete_match.h"
#include "components/autocomplete/autocomplete_provider_listener.h"
......@@ -76,8 +74,9 @@ const int kDefaultZeroSuggestRelevance = 100;
// static
ZeroSuggestProvider* ZeroSuggestProvider::Create(
AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile) {
return new ZeroSuggestProvider(listener, profile);
return new ZeroSuggestProvider(listener, template_url_service, profile);
}
// static
......@@ -118,7 +117,8 @@ void ZeroSuggestProvider::Start(const AutocompleteInput& input,
// No need to send the current page URL in personalized suggest field trial.
if (CanSendURL(input.current_url(), suggest_url, default_provider,
current_page_classification_, profile_) &&
current_page_classification_,
template_url_service_->search_terms_data(), profile_) &&
!OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial()) {
// Update suggest_url to include the current_page_url.
search_term_args.current_page_url = current_query_;
......@@ -164,10 +164,10 @@ void ZeroSuggestProvider::ModifyProviderInfo(
ZeroSuggestProvider::ZeroSuggestProvider(
AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile)
: BaseSearchProvider(listener, profile,
: BaseSearchProvider(listener, template_url_service, profile,
AutocompleteProvider::TYPE_ZERO_SUGGEST),
template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)),
results_from_cache_(false),
weak_ptr_factory_(this) {
}
......@@ -428,7 +428,8 @@ bool ZeroSuggestProvider::CanShowZeroSuggestWithoutSendingURL(
const GURL& current_page_url) const {
if (!ZeroSuggestEnabled(suggest_url,
template_url_service_->GetDefaultSearchProvider(),
current_page_classification_, profile_))
current_page_classification_,
template_url_service_->search_terms_data(), profile_))
return false;
// If we cannot send URLs, then only the MostVisited and Personalized
......
......@@ -51,6 +51,7 @@ class ZeroSuggestProvider : public BaseSearchProvider {
public:
// Creates and returns an instance of this provider.
static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile);
// Registers a preference used to cache zero suggest results.
......@@ -71,6 +72,7 @@ class ZeroSuggestProvider : public BaseSearchProvider {
private:
ZeroSuggestProvider(AutocompleteProviderListener* listener,
TemplateURLService* template_url_service,
Profile* profile);
virtual ~ZeroSuggestProvider();
......@@ -132,9 +134,6 @@ class ZeroSuggestProvider : public BaseSearchProvider {
// populates |matches_| with cached results.
void MaybeUseCachedSuggestions();
// Used to build default search engine URLs for suggested queries.
TemplateURLService* template_url_service_;
// The URL for which a suggestion fetch is pending.
std::string current_query_;
......
......@@ -86,7 +86,7 @@ void ZeroSuggestProviderTest::SetUp() {
turl_model->Add(default_t_url_);
turl_model->SetUserSelectedDefaultSearchProvider(default_t_url_);
provider_ = ZeroSuggestProvider::Create(this, &profile_);
provider_ = ZeroSuggestProvider::Create(this, turl_model, &profile_);
}
void ZeroSuggestProviderTest::TearDown() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment