Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide custom filters textbox if dev mode is off and there're no custom filters. #27201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class AdBlockSubpage extends AdBlockSubpageBase {
'brave_adblock.onGetListSubscriptions', (value: SubscriptionInfo[]) => {
this.subscriptionList_ = value
})
this.browserProxy_.addWebUiListener(
'brave_adblock.onCustomFiltersChanged',
(value: string) => {
this.customFilters_ = value
}
)
}

private handleShowList_() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@
</style>

<div>
<div class="editor-box">
<textarea
disabled="[[!prefs.brave.ad_block.developer_mode.value]]"
type="text"
spellcheck="false"
class="textbox"
on-input="handleInputChange_"
value="{{value}}">
</textarea>
</div>
<template is="dom-if" if="[[shouldShowEditor]]">
<div class="editor-box">
<textarea
disabled="[[!prefs.brave.ad_block.developer_mode.value]]"
type="text"
spellcheck="false"
class="textbox"
on-input="handleInputChange_"
value="{{value}}">
</textarea>
</div>
</template>
<cr-button
on-click="handleSave_"
disabled="[[!prefs.brave.ad_block.developer_mode.value]]">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@ class AdBlockFiltersEditor extends AdBlockFiltersEditorBase {
return {
value: {
type: String
},
shouldShowEditor: {
type: Boolean
}
}
}

static get observers() {
return ['updateState_(prefs.brave.ad_block.developer_mode.value, value)']
}

private value: string
private shouldShowEditor: boolean

override ready() {
super.ready()
Expand All @@ -45,6 +53,10 @@ class AdBlockFiltersEditor extends AdBlockFiltersEditorBase {
handleSave_() {
this.fire('save', { value: this.value })
}

private updateState_(dev_mode: boolean, value: string) {
this.shouldShowEditor = dev_mode || value.trim().length !== 0
}
}

customElements.define(AdBlockFiltersEditor.is, AdBlockFiltersEditor)
21 changes: 16 additions & 5 deletions browser/ui/webui/settings/brave_adblock_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "brave/browser/ui/webui/settings/brave_adblock_handler.h"

#include <memory>
#include <string>
#include <utility>

Expand All @@ -20,11 +19,9 @@
#include "brave/components/brave_shields/core/browser/ad_block_component_service_manager.h"
#include "brave/components/brave_shields/core/browser/ad_block_custom_resource_provider.h"
#include "brave/components/brave_shields/core/common/features.h"
#include "brave/components/brave_shields/core/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_navigator_params.h"
#include "chrome/browser/ui/singleton_tabs.h"
#include "content/public/browser/web_ui.h"
#include "ui/base/l10n/time_format.h"
Expand All @@ -34,7 +31,6 @@ BraveAdBlockHandler::BraveAdBlockHandler() = default;
BraveAdBlockHandler::~BraveAdBlockHandler() = default;

void BraveAdBlockHandler::RegisterMessages() {
profile_ = Profile::FromWebUI(web_ui());
web_ui()->RegisterMessageCallback(
"brave_adblock.getRegionalLists",
base::BindRepeating(&BraveAdBlockHandler::GetRegionalLists,
Expand Down Expand Up @@ -109,15 +105,21 @@ void BraveAdBlockHandler::RegisterMessages() {
"brave_adblock.removeCustomScriptlet",
base::BindRepeating(&BraveAdBlockHandler::RemoveCustomScriptlet,
base::Unretained(this)));
pref_change_registrar_.Init(g_browser_process->local_state());
}

void BraveAdBlockHandler::OnJavascriptAllowed() {
service_observer_.Observe(g_brave_browser_process->ad_block_service()
->subscription_service_manager());
pref_change_registrar_.Add(
brave_shields::prefs::kAdBlockCustomFilters,
base::BindRepeating(&BraveAdBlockHandler::RefreshCustomFilters,
weak_factory_.GetWeakPtr()));
}

void BraveAdBlockHandler::OnJavascriptDisallowed() {
service_observer_.Reset();
pref_change_registrar_.RemoveAll();
}

void BraveAdBlockHandler::OnServiceUpdateEvent() {
Expand Down Expand Up @@ -364,6 +366,15 @@ void BraveAdBlockHandler::RefreshSubscriptionsList() {
FireWebUIListener("brave_adblock.onGetListSubscriptions", GetSubscriptions());
}

void BraveAdBlockHandler::RefreshCustomFilters() {
const std::string& custom_filters =
g_brave_browser_process->ad_block_service()
->custom_filters_provider()
->GetCustomFilters();
FireWebUIListener("brave_adblock.onCustomFiltersChanged",
base::Value(custom_filters));
}

base::Value::List BraveAdBlockHandler::GetSubscriptions() {
auto list_subscriptions = g_brave_browser_process->ad_block_service()
->subscription_service_manager()
Expand Down
7 changes: 4 additions & 3 deletions browser/ui/webui/settings/brave_adblock_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

#include <string>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "brave/components/brave_shields/content/browser/ad_block_subscription_service_manager.h"
#include "brave/components/brave_shields/content/browser/ad_block_subscription_service_manager_observer.h"
#include "brave/components/brave_shields/core/browser/ad_block_custom_resource_provider.h"
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
#include "components/prefs/pref_change_registrar.h"

class Profile;
using brave_shields::AdBlockSubscriptionServiceManager;
Expand Down Expand Up @@ -60,17 +60,18 @@ class BraveAdBlockHandler : public settings::SettingsPageUIHandler,
brave_shields::AdBlockCustomResourceProvider::ErrorCode error_code);

void RefreshSubscriptionsList();
void RefreshCustomFilters();

base::Value::List GetSubscriptions();

void OnFilterListsUpdated(std::string callback_id, bool success);

raw_ptr<Profile> profile_ = nullptr;

base::ScopedObservation<AdBlockSubscriptionServiceManager,
AdBlockSubscriptionServiceManagerObserver>
service_observer_{this};

PrefChangeRegistrar pref_change_registrar_;

base::WeakPtrFactory<BraveAdBlockHandler> weak_factory_{this};
};

Expand Down
Loading