Skip to content

Commit

Permalink
Add extension API for checking WDP native feature, use single pref fo…
Browse files Browse the repository at this point in the history
…r Web Discovery native + extension
  • Loading branch information
DJAndries committed Oct 25, 2024
1 parent fecbbfd commit da9920d
Show file tree
Hide file tree
Showing 31 changed files with 166 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions browser/brave_tab_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 2 additions & 11 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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] =
Expand Down
27 changes: 27 additions & 0 deletions browser/extensions/api/web_discovery_api.cc
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions browser/extensions/api/web_discovery_api.h
Original file line number Diff line number Diff line change
@@ -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_
9 changes: 0 additions & 9 deletions browser/profiles/brave_profile_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@
sub-label="$i18n{searchSuggestDesc}">
</settings-toggle-button>
</template>
<if expr="enable_extensions">
<template is="dom-if" if="[[!isWebDiscoveryNativeEnabled_()]]" restamp>
<settings-toggle-button id="webDiscoveryEnabledExtension"
class="cr-row"
pref="{{prefs.brave.web_discovery_enabled}}"
label="$i18n{braveWebDiscoveryLabel}"
sub-label="$i18n{braveWebDiscoverySubLabel}"
learn-more-url="$i18n{webDiscoveryLearnMoreURL}">
</settings-toggle-button>
</template>
</if>
<if expr="enable_web_discovery_native">
<template is="dom-if" if="[[isWebDiscoveryNativeEnabled_()]]" restamp>
<settings-toggle-button id="webDiscoveryEnabledNative"
class="cr-row"
pref="{{prefs.brave.web_discovery.wdp_native_enabled}}"
label="$i18n{braveWebDiscoveryLabel}"
sub-label="$i18n{braveWebDiscoverySubLabel}"
learn-more-url="$i18n{webDiscoveryLearnMoreURL}">
</settings-toggle-button>
</template>
<if expr="enable_extensions or enable_web_discovery_native">
<settings-toggle-button id="webDiscoveryEnabledExtension"
class="cr-row"
pref="{{prefs.brave.web_discovery_enabled}}"
label="$i18n{braveWebDiscoveryLabel}"
sub-label="$i18n{braveWebDiscoverySubLabel}"
learn-more-url="$i18n{webDiscoveryLearnMoreURL}">
</settings-toggle-button>
</if>
<settings-toggle-button id="otherSearchEnginesEnabled"
class="cr-row"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ class BraveSearchEnginesPage extends BraveSearchEnginesPageBase {
return !loadTimeData.getBoolean('isGuest')
}

isWebDiscoveryNativeEnabled_() {
return loadTimeData.getBoolean('isWebDiscoveryNativeEnabled');
}

shouldShowPrivateSearchProvider_(prefs) {
// When default search engine is enforced, configured provider is not used.
// If we install search provider extension, that extension will be used on normal and
Expand Down
27 changes: 2 additions & 25 deletions browser/search_engines/search_engine_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/pref_registry/pref_registry_syncable.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 {

// Preference name switch events are stored under.
Expand Down Expand Up @@ -187,18 +182,10 @@ SearchEngineTracker::SearchEngineTracker(
#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
RecordWebDiscoveryEnabledP3A();
pref_change_registrar_.Init(profile_prefs);
#if BUILDFLAG(ENABLE_EXTENSIONS)
pref_change_registrar_.Add(
kWebDiscoveryExtensionEnabled,
base::BindRepeating(&SearchEngineTracker::RecordWebDiscoveryEnabledP3A,
base::Unretained(this)));
#endif
#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
pref_change_registrar_.Add(
web_discovery::kWebDiscoveryNativeEnabled,
kWebDiscoveryEnabled,
base::BindRepeating(&SearchEngineTracker::RecordWebDiscoveryEnabledP3A,
base::Unretained(this)));
#endif
pref_change_registrar_.Add(
brave_ads::prefs::kOptedInToNotificationAds,
base::BindRepeating(&SearchEngineTracker::RecordWebDiscoveryEnabledP3A,
Expand Down Expand Up @@ -242,17 +229,7 @@ void SearchEngineTracker::OnTemplateURLServiceChanged() {

#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
void SearchEngineTracker::RecordWebDiscoveryEnabledP3A() {
bool enabled = false;
#if BUILDFLAG(ENABLE_EXTENSIONS)
enabled = profile_prefs_->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,
Expand Down
8 changes: 6 additions & 2 deletions browser/search_engines/search_engine_tracker_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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:
Expand Down Expand Up @@ -159,21 +161,23 @@ 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);

histogram_tester_->ExpectUniqueSample(kWebDiscoveryAndAdsMetric, 0, 2);
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
11 changes: 0 additions & 11 deletions browser/ui/webui/brave_settings_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 3 additions & 2 deletions browser/ui/webui/welcome_page/welcome_dom_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit da9920d

Please sign in to comment.