Skip to content

Commit

Permalink
review changes + feature flag
Browse files Browse the repository at this point in the history
Signed-off-by: Vadym Struts <[email protected]>
  • Loading branch information
vadimstruts committed Dec 10, 2024
1 parent 8ebc86d commit 4e78f12
Show file tree
Hide file tree
Showing 23 changed files with 270 additions and 81 deletions.
1 change: 1 addition & 0 deletions android/java/org/chromium/base/BraveFeatureList.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ public abstract class BraveFeatureList {
public static final String BRAVE_DAY_ZERO_EXPERIMENT = "BraveDayZeroExperiment";
public static final String BRAVE_FALLBACK_DOH_PROVIDER = "BraveFallbackDoHProvider";
public static final String BRAVE_BLOCK_ALL_COOKIES_TOGGLE = "BlockAllCookiesToggle";
public static final String BRAVE_BLOCK_ELEMENTS_FEATURE = "BlockElementFeature";
}
Original file line number Diff line number Diff line change
Expand Up @@ -1625,7 +1625,7 @@ public void openBraveContentFilteringSettings() {
settingsLauncher.startSettings(this, ContentFilteringFragment.class);
}

public boolean isNightlyModeEnabled() {
public boolean isNightModeEnabled() {
return GlobalNightModeStateProviderHolder.getInstance().isInNightMode();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public static int getThemeBackgroundColor() {
}

@CalledByNative
public static boolean isNightlyModeEnabled() {
public static boolean isNightModeEnabled() {
boolean isDarkModeEnabled = false;
try {
isDarkModeEnabled = BraveActivity.getBraveActivity().isNightlyModeEnabled();
isDarkModeEnabled = BraveActivity.getBraveActivity().isNightModeEnabled();
} catch (BraveActivity.BraveActivityNotFoundException e) {
Log.e(TAG, "Get nightly mode status" + e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ private void setUpSwitchLayouts() {

TextView blockElementsText =
mSecondaryLayout.findViewById(R.id.brave_shields_block_element_text);
blockElementsText.setVisibility(
ChromeFeatureList.isEnabled(BraveFeatureList.BRAVE_BLOCK_ELEMENTS_FEATURE)
? View.VISIBLE
: View.GONE);
blockElementsText.setOnClickListener(
new View.OnClickListener() {
@Override
Expand Down
3 changes: 1 addition & 2 deletions android/java/res/layout/brave_shields_secondary_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
android:id="@+id/brave_shields_block_element_text"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:paddingHorizontal="16dp"
android:gravity="start|center_vertical"
android:textColor="@color/shield_text_color"
android:textSize="14sp"
Expand Down
8 changes: 8 additions & 0 deletions browser/about_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,14 @@
kOsAll, \
FEATURE_VALUE_TYPE(brave_shields::features::kBlockAllCookiesToggle), \
}, \
{ \
"block-element-feature", \
"Enable Block Element feature", \
"Allows to block sekected HTML element on the page" \
"marked as a known threat.", \
kOsAll, \
FEATURE_VALUE_TYPE(brave_shields::features::kBlockElementFeature), \
}, \
{ \
"brave-super-referral", \
"Enable Brave Super Referral", \
Expand Down
2 changes: 1 addition & 1 deletion browser/android/cosmetic_filters/cosmetic_filters_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int32_t GetThemeBackgroundColor() {

bool IsDarkModeEnabled() {
JNIEnv* env = base::android::AttachCurrentThread();
return Java_BraveCosmeticFiltersUtils_isNightlyModeEnabled(env);
return Java_BraveCosmeticFiltersUtils_isNightModeEnabled(env);
}

} // namespace cosmetic_filters
12 changes: 11 additions & 1 deletion browser/cosmetic_filters/cosmetic_filters_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#if !BUILDFLAG(IS_ANDROID)
#include "brave/browser/ui/brave_pages.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
Expand Down Expand Up @@ -110,6 +109,17 @@ void CosmeticFiltersTabHelper::GetElementPickerThemeInfo(
#endif // !BUILDFLAG(IS_ANDROID)
}

void CosmeticFiltersTabHelper::InitElementPicker(
InitElementPickerCallback callback) {
std::move(callback).Run(
#if !BUILDFLAG(IS_ANDROID)
mojom::RunningPlatform::kDesktop
#else // !BUILDFLAG(IS_ANDROID)
mojom::RunningPlatform::kAndroid
#endif // !BUILDFLAG(IS_ANDROID)
);
}

CosmeticFiltersTabHelper::CosmeticFiltersTabHelper(
content::WebContents* web_contents)
: content::WebContentsUserData<CosmeticFiltersTabHelper>(*web_contents),
Expand Down
1 change: 1 addition & 0 deletions browser/cosmetic_filters/cosmetic_filters_tab_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CosmeticFiltersTabHelper
void ManageCustomFilters() override;
void GetElementPickerThemeInfo(
GetElementPickerThemeInfoCallback callback) override;
void InitElementPicker(InitElementPickerCallback callback) override;

friend class content::WebContentsUserData<CosmeticFiltersTabHelper>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
&net::features::kBraveForgetFirstPartyStorage, \
&brave_shields::features::kBraveShowStrictFingerprintingMode, \
&brave_shields::features::kBraveLocalhostAccessPermission, \
&brave_shields::features::kBlockAllCookiesToggle
&brave_shields::features::kBlockAllCookiesToggle, \
&brave_shields::features::kBlockElementFeature

// clang-format on

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "brave/browser/tor/tor_profile_service_factory.h"
#endif

#include "base/feature_list.h"
#include "brave/browser/ai_chat/ai_chat_service_factory.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/misc_metrics/process_misc_metrics.h"
Expand All @@ -55,6 +56,7 @@
#include "brave/components/ai_chat/core/common/features.h"
#include "brave/components/ai_chat/core/common/mojom/ai_chat.mojom.h"
#include "brave/components/ai_chat/core/common/pref_names.h"
#include "brave/components/brave_shields/core/common/features.h"
#include "components/grit/brave_components_strings.h"

#if BUILDFLAG(ENABLE_AI_REWRITER)
Expand Down Expand Up @@ -714,6 +716,8 @@ void BraveRenderViewContextMenu::AppendDeveloperItems() {

const auto page_url = source_web_contents_->GetLastCommittedURL();
add_block_elements &= page_url.SchemeIsHTTPOrHTTPS();
add_block_elements &= base::FeatureList::IsEnabled(
brave_shields::features::kBlockElementFeature);
if (add_block_elements) {
std::optional<size_t> inspect_index =
menu_model_.GetIndexOfCommandId(IDC_CONTENT_CONTEXT_INSPECTELEMENT);
Expand Down
5 changes: 4 additions & 1 deletion components/brave_shields/core/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ static_library("common") {
sources += [ "brave_shield_localized_strings.h" ]
}

public_deps = [ "//brave/components/resources:strings" ]
public_deps = [
"//brave/components/brave_shields/core/common/buildflags",
"//brave/components/resources:strings",
]

deps = [
"//base",
Expand Down
12 changes: 12 additions & 0 deletions components/brave_shields/core/common/buildflags/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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/.

import("//brave/components/brave_shields/core/common/buildflags/buildflags.gni")
import("//build/buildflag_header.gni")

buildflag_header("buildflags") {
header = "buildflags.h"
flags = [ "ENABLE_BLOCK_ELEMENT_FEATURE=$enable_block_elements_feature" ]
}
12 changes: 12 additions & 0 deletions components/brave_shields/core/common/buildflags/buildflags.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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/.

import("//brave/build/config.gni")

declare_args() {
enable_block_elements_feature =
(brave_channel == "development" || brave_channel == "nightly" ||
brave_channel == "beta" || (is_official_build && !is_android)) && !is_ios
}
9 changes: 9 additions & 0 deletions components/brave_shields/core/common/features.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "brave/components/brave_shields/core/common/features.h"

#include "base/feature_list.h"
#include "brave/components/brave_shields/core/common/buildflags/buildflags.h"

namespace brave_shields::features {

Expand Down Expand Up @@ -128,6 +129,14 @@ BASE_FEATURE(kCosmeticFilteringSyncLoad,
BASE_FEATURE(kBlockAllCookiesToggle,
"BlockAllCookiesToggle",
base::FEATURE_DISABLED_BY_DEFAULT);
// when enabled, allow to select and block HTML elements
BASE_FEATURE(kBlockElementFeature,
"BlockElementFeature",
#if BUILDFLAG(ENABLE_BLOCK_ELEMENT_FEATURE)
base::FEATURE_ENABLED_BY_DEFAULT);
#else
base::FEATURE_DISABLED_BY_DEFAULT);
#endif

// Enables extra TRACE_EVENTs in content filter js. The feature is
// primary designed for local debugging.
Expand Down
1 change: 1 addition & 0 deletions components/brave_shields/core/common/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ BASE_DECLARE_FEATURE(kCosmeticFilteringExtraPerfMetrics);
BASE_DECLARE_FEATURE(kCosmeticFilteringJsPerformance);
BASE_DECLARE_FEATURE(kCosmeticFilteringSyncLoad);
BASE_DECLARE_FEATURE(kBlockAllCookiesToggle);
BASE_DECLARE_FEATURE(kBlockElementFeature);
extern const base::FeatureParam<int> kComponentUpdateCheckIntervalMins;
extern const base::FeatureParam<std::string>
kCosmeticFilteringSubFrameFirstSelectorsPollingDelayMs;
Expand Down
7 changes: 7 additions & 0 deletions components/cosmetic_filters/common/cosmetic_filters.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ interface CosmeticFiltersResources {
mojo_base.mojom.Value result);
};

enum RunningPlatform {
kDesktop,
kAndroid
};

interface CosmeticFiltersHandler {
// Adds a user cosmetic rule for the current site.
// (currently from the content picker feature).
Expand All @@ -27,6 +32,8 @@ interface CosmeticFiltersHandler {

GetElementPickerThemeInfo() => (
bool is_nightly_mode_enabled, int32 background_color);

InitElementPicker() => (RunningPlatform platform);
};

// An interface to render frame agent `CosmeticFiltersJSHandler` to control
Expand Down
Loading

0 comments on commit 4e78f12

Please sign in to comment.