diff --git a/android/java/org/chromium/chrome/browser/privacy/settings/BravePrivacySettings.java b/android/java/org/chromium/chrome/browser/privacy/settings/BravePrivacySettings.java index f545dd43e523..366e6030c54c 100644 --- a/android/java/org/chromium/chrome/browser/privacy/settings/BravePrivacySettings.java +++ b/android/java/org/chromium/chrome/browser/privacy/settings/BravePrivacySettings.java @@ -536,7 +536,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { BraveLocalState.commitPendingWrite(); } else if (PREF_SEND_WEB_DISCOVERY.equals(key)) { UserPrefs.get(ProfileManager.getLastUsedRegularProfile()) - .setBoolean(WebDiscoveryPrefs.WEB_DISCOVERY_NATIVE_ENABLED, (boolean) newValue); + .setBoolean(WebDiscoveryPrefs.WEB_DISCOVERY_ENABLED, (boolean) newValue); } else if (PREF_SEND_CRASH_REPORTS.equals(key)) { UmaSessionStats.changeMetricsReportingConsent( (boolean) newValue, ChangeMetricsReportingStateCalledFrom.UI_SETTINGS); @@ -704,7 +704,7 @@ private void updateBravePreferences() { getActivity().getResources().getString(R.string.send_web_discovery_summary)); mSendWebDiscovery.setChecked( UserPrefs.get(ProfileManager.getLastUsedRegularProfile()) - .getBoolean(WebDiscoveryPrefs.WEB_DISCOVERY_NATIVE_ENABLED)); + .getBoolean(WebDiscoveryPrefs.WEB_DISCOVERY_ENABLED)); } mSendCrashReports.setChecked(mPrivacyPrefManager.isUsageAndCrashReportingPermittedByUser()); diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 8aa1659e45d4..7b6f5e146507 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -434,8 +434,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { prefs::kBraveDefaultSearchVersion, TemplateURLPrepopulateData::kBraveCurrentDataVersion); -#if BUILDFLAG(ENABLE_EXTENSIONS) - registry->RegisterBooleanPref(kWebDiscoveryExtensionEnabled, false); +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) + registry->RegisterBooleanPref(kWebDiscoveryEnabled, false); registry->RegisterDictionaryPref(kWebDiscoveryCTAState); #endif diff --git a/browser/brave_tab_helpers.cc b/browser/brave_tab_helpers.cc index 924fa3cd0c85..f205805a2a57 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -30,6 +30,7 @@ #include "brave/components/request_otr/common/buildflags/buildflags.h" #include "brave/components/speedreader/common/buildflags/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/web_discovery/buildflags/buildflags.h" #include "build/build_config.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/profiles/profile.h" @@ -87,7 +88,7 @@ #include "brave/components/tor/tor_tab_helper.h" #endif -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) #include "brave/browser/web_discovery/web_discovery_tab_helper.h" #endif @@ -163,7 +164,7 @@ void AttachTabHelpers(content::WebContents* web_contents) { web_contents); psst::PsstTabHelper::MaybeCreateForWebContents( web_contents, ISOLATED_WORLD_ID_BRAVE_INTERNAL); -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents); #endif diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index 526823f77265..dd7841db2164 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -11,6 +11,7 @@ import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni") import("//brave/components/playlist/common/buildflags/buildflags.gni") import("//brave/components/speedreader/common/buildflags/buildflags.gni") import("//brave/components/tor/buildflags/buildflags.gni") +import("//brave/components/web_discovery/buildflags/buildflags.gni") import("//build/config/features.gni") import("//build/config/ui.gni") import("//components/gcm_driver/config.gni") @@ -173,6 +174,7 @@ source_set("extensions") { "//brave/components/request_otr/common", "//brave/components/sidebar/browser", "//brave/components/tor/buildflags", + "//brave/components/web_discovery/buildflags", "//chrome/browser:browser_process", "//chrome/browser:browser_public_dependencies", "//chrome/browser/extensions", @@ -229,6 +231,10 @@ source_set("extensions") { ] } + if (enable_web_discovery_native) { + deps += [ "//brave/components/web_discovery/common" ] + } + # It seems like this brave_wallet_api should be renamed to ethereum_remote_client_api. # However this is not possible right now because the ethereum-remote-client extension # uses chrome.braveWallet, so the API is intentionally not being renamed now. diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index 119639f19050..c5efe7b0460d 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -63,10 +63,6 @@ #include "brave/components/playlist/browser/pref_names.h" #endif -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) -#include "brave/components/web_discovery/browser/pref_names.h" -#endif - namespace extensions { using ntp_background_images::prefs::kNewTabPageShowBackgroundImage; @@ -189,14 +185,9 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetAllowlistedKeys() { settings_api::PrefType::kBoolean; (*s_brave_allowlist)[kNewTabPageShowsOptions] = settings_api::PrefType::kNumber; -#if BUILDFLAG(ENABLE_EXTENSIONS) +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) // Web discovery prefs - (*s_brave_allowlist)[kWebDiscoveryExtensionEnabled] = - settings_api::PrefType::kBoolean; -#endif -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - (*s_brave_allowlist)[web_discovery::kWebDiscoveryNativeEnabled] = - settings_api::PrefType::kBoolean; + (*s_brave_allowlist)[kWebDiscoveryEnabled] = settings_api::PrefType::kBoolean; #endif // Clear browsing data on exit prefs. (*s_brave_allowlist)[browsing_data::prefs::kDeleteBrowsingHistoryOnExit] = diff --git a/browser/extensions/api/web_discovery_api.cc b/browser/extensions/api/web_discovery_api.cc new file mode 100644 index 000000000000..7deec9539ee4 --- /dev/null +++ b/browser/extensions/api/web_discovery_api.cc @@ -0,0 +1,27 @@ +/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/extensions/api/web_discovery_api.h" + +#include "brave/components/web_discovery/buildflags/buildflags.h" + +#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) +#include "base/feature_list.h" +#include "brave/components/web_discovery/common/features.h" +#endif + +namespace extensions::api { + +ExtensionFunction::ResponseAction +WebDiscoveryIsWebDiscoveryNativeEnabledFunction::Run() { + bool result = false; +#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) + result = base::FeatureList::IsEnabled( + web_discovery::features::kBraveWebDiscoveryNative); +#endif + return RespondNow(WithArguments(result)); +} + +} // namespace extensions::api diff --git a/browser/extensions/api/web_discovery_api.h b/browser/extensions/api/web_discovery_api.h new file mode 100644 index 000000000000..26b584b630af --- /dev/null +++ b/browser/extensions/api/web_discovery_api.h @@ -0,0 +1,29 @@ +/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_EXTENSIONS_API_WEB_DISCOVERY_API_H_ +#define BRAVE_BROWSER_EXTENSIONS_API_WEB_DISCOVERY_API_H_ + +#include "extensions/browser/extension_function.h" + +namespace extensions { +namespace api { + +class WebDiscoveryIsWebDiscoveryNativeEnabledFunction + : public ExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("webDiscovery.isWebDiscoveryNativeEnabled", + UNKNOWN) + + protected: + ~WebDiscoveryIsWebDiscoveryNativeEnabledFunction() override {} + + ResponseAction Run() override; +}; + +} // namespace api +} // namespace extensions + +#endif // BRAVE_BROWSER_EXTENSIONS_API_WEB_DISCOVERY_API_H_ diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc index fed51ca57853..9077bd1308ae 100644 --- a/browser/profiles/brave_profile_manager.cc +++ b/browser/profiles/brave_profile_manager.cc @@ -26,7 +26,6 @@ #include "brave/components/ntp_background_images/common/pref_names.h" #include "brave/components/request_otr/common/buildflags/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" -#include "brave/components/web_discovery/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/content_settings/host_content_settings_map_factory.h" #include "chrome/browser/profiles/profile_attributes_entry.h" @@ -47,10 +46,6 @@ #include "brave/components/tor/tor_constants.h" #endif -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) -#include "brave/components/web_discovery/browser/web_discovery_service.h" -#endif - using brave_shields::ControlType; using content::BrowserThread; using ntp_background_images::prefs::kNewTabPageShowBackgroundImage; @@ -143,10 +138,6 @@ void BraveProfileManager::InitProfileUserPrefs(Profile* profile) { brave::SetDefaultThirdPartyCookieBlockValue(profile); perf::MaybeEnableBraveFeatureForPerfTesting(profile); MigrateHttpsUpgradeSettings(profile); -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - web_discovery::WebDiscoveryService::SetExtensionPrefIfNativeDisabled( - profile->GetPrefs()); -#endif } void BraveProfileManager::DoFinalInitForServices(Profile* profile, diff --git a/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.html b/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.html index 7cb5c2619c64..bceeb9c5ee20 100644 --- a/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.html +++ b/browser/resources/settings/brave_search_engines_page/brave_search_engines_page.html @@ -17,27 +17,14 @@ sub-label="$i18n{searchSuggestDesc}"> - - - - - + + + GetBoolean(kWebDiscoveryExtensionEnabled); -#endif -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - if (base::FeatureList::IsEnabled( - web_discovery::features::kBraveWebDiscoveryNative)) { - enabled = - profile_prefs_->GetBoolean(web_discovery::kWebDiscoveryNativeEnabled); - } -#endif + bool enabled = profile_prefs_->GetBoolean(kWebDiscoveryEnabled); UMA_HISTOGRAM_BOOLEAN(kWebDiscoveryEnabledMetric, enabled); UMA_HISTOGRAM_BOOLEAN( kWebDiscoveryAndAdsMetric, diff --git a/browser/search_engines/search_engine_tracker_browsertest.cc b/browser/search_engines/search_engine_tracker_browsertest.cc index 714aa69c61cf..30b2cd4c04df 100644 --- a/browser/search_engines/search_engine_tracker_browsertest.cc +++ b/browser/search_engines/search_engine_tracker_browsertest.cc @@ -13,6 +13,7 @@ #include "brave/components/constants/pref_names.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" #include "brave/components/tor/buildflags/buildflags.h" +#include "brave/components/web_discovery/buildflags/buildflags.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engine_choice/search_engine_choice_service_factory.h" #include "chrome/browser/search_engines/template_url_service_factory.h" @@ -24,6 +25,7 @@ #include "components/search_engines/search_engine_choice/search_engine_choice_service.h" #include "components/search_engines/template_url_prepopulate_data.h" #include "content/public/test/browser_test.h" +#include "extensions/buildflags/buildflags.h" class SearchEngineProviderP3ATest : public InProcessBrowserTest { public: @@ -159,11 +161,12 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderP3ATest, SwitchSearchEngineP3A) { histogram_tester_->ExpectTotalCount(kSwitchSearchEngineMetric, 8); } +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) IN_PROC_BROWSER_TEST_F(SearchEngineProviderP3ATest, WebDiscoveryEnabledP3A) { histogram_tester_->ExpectBucketCount(kWebDiscoveryEnabledMetric, 0, 1); PrefService* prefs = browser()->profile()->GetPrefs(); - prefs->SetBoolean(kWebDiscoveryExtensionEnabled, true); + prefs->SetBoolean(kWebDiscoveryEnabled, true); histogram_tester_->ExpectBucketCount(kWebDiscoveryEnabledMetric, 1, 1); @@ -171,9 +174,10 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderP3ATest, WebDiscoveryEnabledP3A) { prefs->SetBoolean(brave_ads::prefs::kOptedInToNotificationAds, true); histogram_tester_->ExpectBucketCount(kWebDiscoveryAndAdsMetric, 1, 1); - prefs->SetBoolean(kWebDiscoveryExtensionEnabled, false); + prefs->SetBoolean(kWebDiscoveryEnabled, false); histogram_tester_->ExpectBucketCount(kWebDiscoveryEnabledMetric, 0, 2); histogram_tester_->ExpectBucketCount(kWebDiscoveryAndAdsMetric, 0, 3); histogram_tester_->ExpectTotalCount(kWebDiscoveryAndAdsMetric, 4); } +#endif diff --git a/browser/ui/webui/brave_settings_ui.cc b/browser/ui/webui/brave_settings_ui.cc index de31ce02cfb4..c81298d8861d 100644 --- a/browser/ui/webui/brave_settings_ui.cc +++ b/browser/ui/webui/brave_settings_ui.cc @@ -45,7 +45,6 @@ #include "brave/components/speedreader/common/buildflags/buildflags.h" #include "brave/components/tor/buildflags/buildflags.h" #include "brave/components/version_info/version_info.h" -#include "brave/components/web_discovery/buildflags/buildflags.h" #include "build/build_config.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/webui/settings/metrics_reporting_handler.h" @@ -92,10 +91,6 @@ #include "brave/components/playlist/common/features.h" #endif -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) -#include "brave/components/web_discovery/common/features.h" -#endif - using ntp_background_images::ViewCounterServiceFactory; BraveSettingsUI::BraveSettingsUI(content::WebUI* web_ui) : SettingsUI(web_ui) { @@ -197,12 +192,6 @@ void BraveSettingsUI::AddResources(content::WebUIDataSource* html_source, ShouldExposeElementsForTesting()); html_source->AddBoolean("enable_extensions", BUILDFLAG(ENABLE_EXTENSIONS)); -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - html_source->AddBoolean( - "isWebDiscoveryNativeEnabled", - base::FeatureList::IsEnabled( - web_discovery::features::kBraveWebDiscoveryNative)); -#endif html_source->AddBoolean("extensionsManifestV2Feature", base::FeatureList::IsEnabled(kExtensionsManifestV2)); diff --git a/browser/ui/webui/welcome_page/welcome_dom_handler.cc b/browser/ui/webui/welcome_page/welcome_dom_handler.cc index b828846b04e9..de6bbce226f9 100644 --- a/browser/ui/webui/welcome_page/welcome_dom_handler.cc +++ b/browser/ui/webui/welcome_page/welcome_dom_handler.cc @@ -12,6 +12,7 @@ #include "brave/common/importer/importer_constants.h" #include "brave/components/constants/pref_names.h" #include "brave/components/p3a/pref_names.h" +#include "brave/components/web_discovery/buildflags/buildflags.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/metrics/metrics_reporting_state.h" #include "chrome/browser/profiles/profile.h" @@ -171,8 +172,8 @@ void WelcomeDOMHandler::HandleSetMetricsReportingEnabled( void WelcomeDOMHandler::HandleEnableWebDiscovery( const base::Value::List& args) { DCHECK(profile_); -#if BUILDFLAG(ENABLE_EXTENSIONS) - profile_->GetPrefs()->SetBoolean(kWebDiscoveryExtensionEnabled, true); +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) + profile_->GetPrefs()->SetBoolean(kWebDiscoveryEnabled, true); #endif } diff --git a/browser/web_discovery/BUILD.gn b/browser/web_discovery/BUILD.gn index 991df6173997..bcce68d20e25 100644 --- a/browser/web_discovery/BUILD.gn +++ b/browser/web_discovery/BUILD.gn @@ -28,7 +28,7 @@ if (enable_web_discovery_native) { } source_set("unit_tests") { - if (enable_extensions) { + if (enable_extensions || enable_web_discovery_native) { testonly = true sources = [ "web_discovery_unittest.cc" ] @@ -49,7 +49,7 @@ source_set("unit_tests") { } source_set("browser_tests") { - if (enable_extensions) { + if (enable_extensions || enable_web_discovery_native) { testonly = true defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] diff --git a/browser/web_discovery/sources.gni b/browser/web_discovery/sources.gni index 4547374948cb..866ca2b7fd7b 100644 --- a/browser/web_discovery/sources.gni +++ b/browser/web_discovery/sources.gni @@ -3,12 +3,13 @@ # 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/. +import("//brave/components/web_discovery/buildflags/buildflags.gni") import("//extensions/buildflags/buildflags.gni") brave_browser_web_discovery_sources = [] brave_browser_web_discovery_deps = [] -if (enable_extensions) { +if (enable_extensions || enable_web_discovery_native) { brave_browser_web_discovery_sources += [ "//brave/browser/web_discovery/web_discovery_cta_util.cc", "//brave/browser/web_discovery/web_discovery_cta_util.h", diff --git a/browser/web_discovery/web_discovery_cta_util.cc b/browser/web_discovery/web_discovery_cta_util.cc index 4bb473a53294..9251daac6c70 100644 --- a/browser/web_discovery/web_discovery_cta_util.cc +++ b/browser/web_discovery/web_discovery_cta_util.cc @@ -14,7 +14,6 @@ #include "brave/components/constants/pref_names.h" #include "brave/components/constants/url_constants.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" -#include "brave/components/web_discovery/buildflags/buildflags.h" #include "components/prefs/pref_service.h" #include "components/prefs/scoped_user_pref_update.h" #include "components/search_engines/template_url.h" @@ -22,11 +21,6 @@ #include "net/base/url_util.h" #include "url/gurl.h" -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) -#include "brave/components/web_discovery/browser/pref_names.h" -#include "brave/components/web_discovery/common/features.h" -#endif - namespace { constexpr int kMaxDisplayCount = 5; @@ -93,17 +87,8 @@ bool ShouldShowWebDiscoveryInfoBar(TemplateURLService* service, PrefService* prefs, const WebDiscoveryCTAState& state, base::Clock* test_clock) { - const char* enabled_pref_name = kWebDiscoveryExtensionEnabled; -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - if (base::FeatureList::IsEnabled( - web_discovery::features::kBraveWebDiscoveryNative)) { - enabled_pref_name = web_discovery::kWebDiscoveryNativeEnabled; - } -#endif - - if (prefs->GetBoolean(enabled_pref_name)) { + if (prefs->GetBoolean(kWebDiscoveryEnabled)) return false; - } if (!service || !IsBraveSearchDefault(service)) return false; diff --git a/browser/web_discovery/web_discovery_infobar_delegate.cc b/browser/web_discovery/web_discovery_infobar_delegate.cc index 6b8f934e6d31..c9db24eaa35b 100644 --- a/browser/web_discovery/web_discovery_infobar_delegate.cc +++ b/browser/web_discovery/web_discovery_infobar_delegate.cc @@ -7,15 +7,9 @@ #include "brave/browser/web_discovery/web_discovery_cta_util.h" #include "brave/components/constants/pref_names.h" -#include "brave/components/web_discovery/buildflags/buildflags.h" #include "components/infobars/core/infobar.h" #include "components/prefs/pref_service.h" -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) -#include "brave/components/web_discovery/browser/pref_names.h" -#include "brave/components/web_discovery/common/features.h" -#endif - WebDiscoveryInfoBarDelegate::WebDiscoveryInfoBarDelegate(PrefService* prefs) : prefs_(prefs) {} @@ -48,13 +42,6 @@ void WebDiscoveryInfoBarDelegate::Close(bool dismiss) { } void WebDiscoveryInfoBarDelegate::EnableWebDiscovery() { - const char* pref_name = kWebDiscoveryExtensionEnabled; -#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) - if (base::FeatureList::IsEnabled( - web_discovery::features::kBraveWebDiscoveryNative)) { - pref_name = web_discovery::kWebDiscoveryNativeEnabled; - } -#endif - prefs_->SetBoolean(pref_name, true); + prefs_->SetBoolean(kWebDiscoveryEnabled, true); infobar()->RemoveSelf(); } diff --git a/browser/web_discovery/web_discovery_unittest.cc b/browser/web_discovery/web_discovery_unittest.cc index f0c01983bedf..a52a74702100 100644 --- a/browser/web_discovery/web_discovery_unittest.cc +++ b/browser/web_discovery/web_discovery_unittest.cc @@ -104,7 +104,7 @@ class WebDiscoveryCTATest : public testing::Test { }; TEST_F(WebDiscoveryCTATest, InitialDataTest) { - EXPECT_FALSE(prefs()->GetBoolean(kWebDiscoveryExtensionEnabled)); + EXPECT_FALSE(prefs()->GetBoolean(kWebDiscoveryEnabled)); const auto& info_value = prefs()->GetDict(kWebDiscoveryCTAState); EXPECT_TRUE(info_value.empty()); @@ -137,10 +137,10 @@ TEST_F(WebDiscoveryCTATest, ShouldShowInfoBarTest) { EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); // Don't show if already enabled. - prefs()->SetBoolean(kWebDiscoveryExtensionEnabled, true); + prefs()->SetBoolean(kWebDiscoveryEnabled, true); EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); - prefs()->SetBoolean(kWebDiscoveryExtensionEnabled, false); + prefs()->SetBoolean(kWebDiscoveryEnabled, false); EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); WebDiscoveryCTAState state = GetCurrentCTAState(); diff --git a/common/extensions/api/_api_features.json b/common/extensions/api/_api_features.json index 4804af72f729..2245a7ef931d 100644 --- a/common/extensions/api/_api_features.json +++ b/common/extensions/api/_api_features.json @@ -87,5 +87,13 @@ "matches": [ "chrome://newtab/*" ] - }] + }], + "webDiscovery": { + "channel": "stable", + "component_extensions_auto_granted": false, + "contexts": ["privileged_extension"], + "allowlist": [ + "A321D47A2B4CA86898167A55CA8B2E02385EA7CD" + ] + } } diff --git a/common/extensions/api/api_sources.gni b/common/extensions/api/api_sources.gni index 8d5d8eada427..f6c300ed3f13 100644 --- a/common/extensions/api/api_sources.gni +++ b/common/extensions/api/api_sources.gni @@ -16,6 +16,7 @@ schema_sources_ = [ "brave_theme.json", "greaselion.json", "rewards_notifications.json", + "web_discovery.json", ] if (ethereum_remote_client_enabled) { diff --git a/common/extensions/api/web_discovery.json b/common/extensions/api/web_discovery.json new file mode 100644 index 000000000000..c5f692f31b8f --- /dev/null +++ b/common/extensions/api/web_discovery.json @@ -0,0 +1,31 @@ +// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. + +[ + { + "namespace": "webDiscovery", + "description": "Includes Web Discovery functions for the Brave extension", + "compiler_options": { + "implemented_in": "brave/browser/extensions/api/web_discovery_api.h" + }, + "functions": [ + { + "name": "isWebDiscoveryNativeEnabled", + "type": "function", + "description": "Returns true if Web Discovery native feature is active", + "parameters": [], + "returns_async": { + "name": "callback", + "parameters": [ + { + "name": "enabled", + "type": "boolean" + } + ] + } + } + ] + } +] diff --git a/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts b/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts index fd91ce8eb61c..178c72b817a0 100644 --- a/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts +++ b/components/brave_extension/extension/brave_extension/background/webDiscoveryProject.ts @@ -79,12 +79,17 @@ if (!chrome.extension.inIncognitoContext) { } } - chrome.settingsPrivate.onPrefsChanged.addListener((prefs: chrome.settingsPrivate.PrefObject[]) => { - const pref = prefs.find(p => p.key === WEB_DISCOVERY_PREF_KEY) - toggleWebDiscovery(pref) - }) + chrome.webDiscovery.isWebDiscoveryNativeEnabled((nativeEnabled) => { + if (nativeEnabled) { + return + } + chrome.settingsPrivate.onPrefsChanged.addListener((prefs: chrome.settingsPrivate.PrefObject[]) => { + const pref = prefs.find(p => p.key === WEB_DISCOVERY_PREF_KEY) + toggleWebDiscovery(pref) + }) - chrome.settingsPrivate.getPref(WEB_DISCOVERY_PREF_KEY, (pref: chrome.settingsPrivate.PrefObject) => { - toggleWebDiscovery(pref) + chrome.settingsPrivate.getPref(WEB_DISCOVERY_PREF_KEY, (pref: chrome.settingsPrivate.PrefObject) => { + toggleWebDiscovery(pref) + }) }) } diff --git a/components/brave_extension/extension/brave_extension/manifest.json b/components/brave_extension/extension/brave_extension/manifest.json index 15acb6c99c05..639e7a7b4000 100644 --- a/components/brave_extension/extension/brave_extension/manifest.json +++ b/components/brave_extension/extension/brave_extension/manifest.json @@ -46,6 +46,7 @@ "webRequest", "*://*/*", "chrome://favicon/*", + "webDiscovery", "webRequestBlocking", "unlimitedStorage", "" diff --git a/components/constants/BUILD.gn b/components/constants/BUILD.gn index 0c078e684f71..a3aea520e863 100644 --- a/components/constants/BUILD.gn +++ b/components/constants/BUILD.gn @@ -31,6 +31,7 @@ source_set("constants") { public_deps = [ ":brave_services_key" ] deps = [ "//base", + "//brave/components/web_discovery/buildflags", "//extensions/buildflags", ] } diff --git a/components/constants/pref_names.h b/components/constants/pref_names.h index 8c459d21b0ed..2e6138af42f0 100644 --- a/components/constants/pref_names.h +++ b/components/constants/pref_names.h @@ -6,6 +6,7 @@ #ifndef BRAVE_COMPONENTS_CONSTANTS_PREF_NAMES_H_ #define BRAVE_COMPONENTS_CONSTANTS_PREF_NAMES_H_ +#include "brave/components/web_discovery/buildflags/buildflags.h" #include "build/build_config.h" #include "extensions/buildflags/buildflags.h" @@ -86,9 +87,8 @@ inline constexpr char kBraveShieldsSettingsVersion[] = inline constexpr char kDefaultBrowserPromptEnabled[] = "brave.default_browser_prompt_enabled"; -#if BUILDFLAG(ENABLE_EXTENSIONS) -inline constexpr char kWebDiscoveryExtensionEnabled[] = - "brave.web_discovery_enabled"; +#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE) +inline constexpr char kWebDiscoveryEnabled[] = "brave.web_discovery_enabled"; #endif inline constexpr char kWebDiscoveryCTAState[] = "brave.web_discovery.cta_state"; inline constexpr char kDontAskEnableWebDiscovery[] = diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 2c57f6d4302b..ac1050e574fe 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -248,3 +248,8 @@ declare namespace cf_worker { declare namespace chrome.test { const sendMessage: (message: string) => {} } + +declare namespace chrome.webDiscovery { + type WebDiscoveryNativeEnabledCallback = (enabled: boolean) => void + const isWebDiscoveryNativeEnabled: (callback: WebDiscoveryNativeEnabledCallback) => void +} diff --git a/components/web_discovery/browser/pref_names.h b/components/web_discovery/browser/pref_names.h index 6f6e1ecb3e7f..aba8c307d0a5 100644 --- a/components/web_discovery/browser/pref_names.h +++ b/components/web_discovery/browser/pref_names.h @@ -9,8 +9,7 @@ namespace web_discovery { // Profile prefs -inline constexpr char kWebDiscoveryNativeEnabled[] = - "brave.web_discovery.wdp_native_enabled"; +inline constexpr char kWebDiscoveryEnabled[] = "brave.web_discovery_enabled"; // The following pref values are used for generating // anonymous signatures for user submissions. diff --git a/components/web_discovery/browser/web_discovery_service.cc b/components/web_discovery/browser/web_discovery_service.cc index 2be6eee2c7c0..b2eb0821fbc5 100644 --- a/components/web_discovery/browser/web_discovery_service.cc +++ b/components/web_discovery/browser/web_discovery_service.cc @@ -5,18 +5,12 @@ #include "brave/components/web_discovery/browser/web_discovery_service.h" -#include - -#include "base/feature_list.h" #include "base/functional/bind.h" -#include "brave/components/constants/pref_names.h" #include "brave/components/web_discovery/browser/pref_names.h" #include "brave/components/web_discovery/browser/server_config_loader.h" -#include "brave/components/web_discovery/common/features.h" #include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_service.h" #include "components/prefs/scoped_user_pref_update.h" -#include "extensions/buildflags/buildflags.h" #include "services/network/public/cpp/shared_url_loader_factory.h" namespace web_discovery { @@ -30,20 +24,13 @@ WebDiscoveryService::WebDiscoveryService( profile_prefs_(profile_prefs), user_data_dir_(user_data_dir), shared_url_loader_factory_(shared_url_loader_factory) { -#if BUILDFLAG(ENABLE_EXTENSIONS) - if (profile_prefs_->GetBoolean(kWebDiscoveryExtensionEnabled)) { - profile_prefs_->ClearPref(kWebDiscoveryExtensionEnabled); - profile_prefs_->SetBoolean(kWebDiscoveryNativeEnabled, true); - } -#endif - pref_change_registrar_.Init(profile_prefs); pref_change_registrar_.Add( - kWebDiscoveryNativeEnabled, + kWebDiscoveryEnabled, base::BindRepeating(&WebDiscoveryService::OnEnabledChange, base::Unretained(this))); - if (profile_prefs_->GetBoolean(kWebDiscoveryNativeEnabled)) { + if (profile_prefs_->GetBoolean(kWebDiscoveryEnabled)) { Start(); } } @@ -56,21 +43,10 @@ void WebDiscoveryService::RegisterLocalStatePrefs( } void WebDiscoveryService::RegisterProfilePrefs(PrefRegistrySimple* registry) { - registry->RegisterBooleanPref(kWebDiscoveryNativeEnabled, false); registry->RegisterDictionaryPref(kAnonymousCredentialsDict); registry->RegisterStringPref(kCredentialRSAPrivateKey, {}); } -void WebDiscoveryService::SetExtensionPrefIfNativeDisabled( - PrefService* profile_prefs) { -#if BUILDFLAG(ENABLE_EXTENSIONS) - if (!base::FeatureList::IsEnabled(features::kBraveWebDiscoveryNative) && - profile_prefs->GetBoolean(kWebDiscoveryNativeEnabled)) { - profile_prefs->SetBoolean(kWebDiscoveryExtensionEnabled, true); - } -#endif -} - void WebDiscoveryService::Shutdown() { Stop(); pref_change_registrar_.RemoveAll(); @@ -104,7 +80,7 @@ void WebDiscoveryService::ClearPrefs() { } void WebDiscoveryService::OnEnabledChange() { - if (profile_prefs_->GetBoolean(kWebDiscoveryNativeEnabled)) { + if (profile_prefs_->GetBoolean(kWebDiscoveryEnabled)) { Start(); } else { Stop(); diff --git a/components/web_discovery/browser/web_discovery_service.h b/components/web_discovery/browser/web_discovery_service.h index c30f03917ed0..88c6837d60bd 100644 --- a/components/web_discovery/browser/web_discovery_service.h +++ b/components/web_discovery/browser/web_discovery_service.h @@ -47,11 +47,6 @@ class WebDiscoveryService : public KeyedService { static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); static void RegisterProfilePrefs(PrefRegistrySimple* registry); - // Sets the extension preference to true if the preference for the native - // implementation is set to true and the feature is disabled. - // Relevant for a Griffin/variations rollback. - static void SetExtensionPrefIfNativeDisabled(PrefService* profile_prefs); - // KeyedService: void Shutdown() override; diff --git a/test/BUILD.gn b/test/BUILD.gn index 842b873e57e9..6de317247535 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -930,6 +930,7 @@ test("brave_browser_tests") { "//brave/components/speedreader/common/buildflags", "//brave/components/tor/buildflags", "//brave/components/url_sanitizer/browser", + "//brave/components/web_discovery/buildflags", "//brave/components/webcompat/content/browser", "//brave/components/webcompat/core/common", "//brave/renderer/skus:browser_tests",