diff --git a/browser/BUILD.gn b/browser/BUILD.gn index e8d963be59f8..2761f9de421b 100644 --- a/browser/BUILD.gn +++ b/browser/BUILD.gn @@ -27,6 +27,8 @@ source_set("browser_process") { sources = [ "autocomplete/brave_autocomplete_provider_client.cc", "autocomplete/brave_autocomplete_provider_client.h", + "autocomplete/brave_autocomplete_provider_client_for_classifier.cc", + "autocomplete/brave_autocomplete_provider_client_for_classifier.h", "autocomplete/brave_autocomplete_scheme_classifier.cc", "autocomplete/brave_autocomplete_scheme_classifier.h", "brave_rewards/rewards_tab_helper.cc", diff --git a/browser/autocomplete/brave_autocomplete_provider_client.cc b/browser/autocomplete/brave_autocomplete_provider_client.cc index 03724301ff0f..eb7c62a84199 100644 --- a/browser/autocomplete/brave_autocomplete_provider_client.cc +++ b/browser/autocomplete/brave_autocomplete_provider_client.cc @@ -1,4 +1,5 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -6,28 +7,15 @@ #include "base/strings/utf_string_conversions.h" #include "brave/common/webui_url_constants.h" -#include "chrome/browser/profiles/profile.h" -#include "chrome/browser/search_engines/template_url_service_factory.h" #include "chrome/common/webui_url_constants.h" BraveAutocompleteProviderClient::BraveAutocompleteProviderClient( - Profile* profile) - : ChromeAutocompleteProviderClient(profile->GetOriginalProfile()), - profile_(profile) { + Profile* profile) : ChromeAutocompleteProviderClient(profile) { } BraveAutocompleteProviderClient::~BraveAutocompleteProviderClient() { } -TemplateURLService* BraveAutocompleteProviderClient::GetTemplateURLService() { - return TemplateURLServiceFactory::GetForProfile(profile_); -} - -const TemplateURLService* -BraveAutocompleteProviderClient::GetTemplateURLService() const { - return TemplateURLServiceFactory::GetForProfile(profile_); -} - std::vector BraveAutocompleteProviderClient::GetBuiltinURLs() { std::vector v = ChromeAutocompleteProviderClient::GetBuiltinURLs(); diff --git a/browser/autocomplete/brave_autocomplete_provider_client.h b/browser/autocomplete/brave_autocomplete_provider_client.h index 4970ab6ea371..dfe437b1852d 100644 --- a/browser/autocomplete/brave_autocomplete_provider_client.h +++ b/browser/autocomplete/brave_autocomplete_provider_client.h @@ -1,43 +1,25 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_AUTOCOMPLETE_PROVIDER_CLIENT_H_ #define BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_AUTOCOMPLETE_PROVIDER_CLIENT_H_ +#include +#include + #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" -// In brave, different AutocompleteClassifiers are created for normal and -// incognito profile by changing -// AutocompleteClassifierFactory::GetBrowserContextToUse(). -// This changes are needed to use different search engine used by web search in -// web page context menu. -// When context menu handles web search it gets search engine url from -// ChromeAutocompleteProviderClient via AutocompleteClassifiers. -// Because of this, private window will use same search engine url of normal -// window if same AutocompleteClassifiers are used on normal and incognito. -// So, we made this change. -// However, ChromeAutocompleteProviderClient exposes many other services based -// on profiles. -// We don't want to change other services. Only wanted to get proper -// TemplateURLService. To achieve this, BraveAutocompleteProviderClient is -// introduced. It initializes ChromeAutocompleteProviderClient with original -// profile and only overrided TemplateURLService getter. -// BraveAutocompleteSchemeClassifier also initialize -// ChromeAutocompleteSchemeClassifier with original profile for same reason. class BraveAutocompleteProviderClient : public ChromeAutocompleteProviderClient { public: explicit BraveAutocompleteProviderClient(Profile* profile); ~BraveAutocompleteProviderClient() override; - TemplateURLService* GetTemplateURLService() override; - const TemplateURLService* GetTemplateURLService() const override; std::vector GetBuiltinURLs() override; private: - Profile* profile_; - DISALLOW_COPY_AND_ASSIGN(BraveAutocompleteProviderClient); }; diff --git a/browser/autocomplete/brave_autocomplete_provider_client_browsertest.cc b/browser/autocomplete/brave_autocomplete_provider_client_browsertest.cc index 293fc07ea2aa..3225f7b49f81 100644 --- a/browser/autocomplete/brave_autocomplete_provider_client_browsertest.cc +++ b/browser/autocomplete/brave_autocomplete_provider_client_browsertest.cc @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "brave/browser/autocomplete/brave_autocomplete_provider_client.h" +#include "brave/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h" #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" #include "chrome/browser/profiles/profile.h" @@ -18,15 +18,15 @@ using BraveAutocompleteProviderClientTest = InProcessBrowserTest; IN_PROC_BROWSER_TEST_F(BraveAutocompleteProviderClientTest, DependentServiceCheckTest) { Profile* profile = browser()->profile(); - Profile* incognito_profile = profile->GetOffTheRecordProfile(); + Profile* otr_profile = profile->GetOffTheRecordProfile(); // Brave initiates different AutocompleteClassifier service for // normal/incognito profile. EXPECT_NE(AutocompleteClassifierFactory::GetForProfile(profile), - AutocompleteClassifierFactory::GetForProfile(incognito_profile)); + AutocompleteClassifierFactory::GetForProfile(otr_profile)); - BraveAutocompleteProviderClient normal_client(profile); - BraveAutocompleteProviderClient incognito_client(incognito_profile); + BraveAutocompleteProviderClientForClassifier normal_client(profile); + BraveAutocompleteProviderClientForClassifier incognito_client(otr_profile); // Check different TemplateURLService is used. EXPECT_NE(normal_client.GetTemplateURLService(), diff --git a/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.cc b/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.cc new file mode 100644 index 000000000000..a8a9790bf5ed --- /dev/null +++ b/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.cc @@ -0,0 +1,30 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h" + +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/search_engines/template_url_service_factory.h" + +BraveAutocompleteProviderClientForClassifier:: +BraveAutocompleteProviderClientForClassifier( + Profile* profile) + : BraveAutocompleteProviderClient(profile->GetOriginalProfile()), + profile_(profile) { +} + +BraveAutocompleteProviderClientForClassifier:: +~BraveAutocompleteProviderClientForClassifier() { +} + +TemplateURLService* + BraveAutocompleteProviderClientForClassifier::GetTemplateURLService() { + return TemplateURLServiceFactory::GetForProfile(profile_); +} + +const TemplateURLService* +BraveAutocompleteProviderClientForClassifier::GetTemplateURLService() const { + return TemplateURLServiceFactory::GetForProfile(profile_); +} diff --git a/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h b/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h new file mode 100644 index 000000000000..11e528ae4624 --- /dev/null +++ b/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h @@ -0,0 +1,44 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_AUTOCOMPLETE_PROVIDER_CLIENT_FOR_CLASSIFIER_H_ +#define BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_AUTOCOMPLETE_PROVIDER_CLIENT_FOR_CLASSIFIER_H_ + +#include "brave/browser/autocomplete/brave_autocomplete_provider_client.h" + +// In brave, different AutocompleteClassifiers are created for normal and +// incognito profile by changing +// AutocompleteClassifierFactory::GetBrowserContextToUse(). +// This changes are needed to use different search engine used by web search in +// web page context menu. +// When context menu handles web search it gets search engine url from +// ChromeAutocompleteProviderClient via AutocompleteClassifiers. +// Because of this, private window will use same search engine url of normal +// window if same AutocompleteClassifiers are used on normal and incognito. +// So, we made this change. +// However, ChromeAutocompleteProviderClient exposes many other services based +// on profiles. +// We don't want to change other services. Only wanted to get proper +// TemplateURLService. To achieve this, BraveAutocompleteProviderClient is +// introduced. It initializes ChromeAutocompleteProviderClient with original +// profile and only overrided TemplateURLService getter. +// BraveAutocompleteSchemeClassifier also initialize +// ChromeAutocompleteSchemeClassifier with original profile for same reason. +class BraveAutocompleteProviderClientForClassifier + : public BraveAutocompleteProviderClient { + public: + explicit BraveAutocompleteProviderClientForClassifier(Profile* profile); + ~BraveAutocompleteProviderClientForClassifier() override; + + TemplateURLService* GetTemplateURLService() override; + const TemplateURLService* GetTemplateURLService() const override; + + private: + Profile* profile_; + + DISALLOW_COPY_AND_ASSIGN(BraveAutocompleteProviderClientForClassifier); +}; + +#endif // BRAVE_BROWSER_AUTOCOMPLETE_BRAVE_AUTOCOMPLETE_PROVIDER_CLIENT_FOR_CLASSIFIER_H_ diff --git a/chromium_src/chrome/browser/autocomplete/autocomplete_classifier_factory.cc b/chromium_src/chrome/browser/autocomplete/autocomplete_classifier_factory.cc index 098e9ab0db05..6b9567740f12 100644 --- a/chromium_src/chrome/browser/autocomplete/autocomplete_classifier_factory.cc +++ b/chromium_src/chrome/browser/autocomplete/autocomplete_classifier_factory.cc @@ -1,15 +1,16 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "brave/browser/autocomplete/brave_autocomplete_provider_client.h" +#include "brave/browser/autocomplete/brave_autocomplete_provider_client_for_classifier.h" #include "brave/browser/autocomplete/brave_autocomplete_scheme_classifier.h" #include "brave/components/omnibox/browser/brave_autocomplete_controller.h" #include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_controller.h" #define AutocompleteController BraveAutocompleteController -#define ChromeAutocompleteProviderClient BraveAutocompleteProviderClient +#define ChromeAutocompleteProviderClient BraveAutocompleteProviderClientForClassifier // NOLINT #define ChromeAutocompleteSchemeClassifier BraveAutocompleteSchemeClassifier #include "../../../../../chrome/browser/autocomplete/autocomplete_classifier_factory.cc" #undef ChromeAutocompleteSchemeClassifier