Skip to content

Commit ffcfa29

Browse files
committed
Add button in AdBlock settings to update AdBlock components
1 parent 3d901ec commit ffcfa29

13 files changed

+189
-9
lines changed

app/brave_settings_strings.grdp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@
423423
<message name="IDS_BRAVE_ADBLOCK_SHOW_FULL_LISTS_BUTTON_LABEL" desc="A button label to expand full custom filter lists">
424424
Show full list
425425
</message>
426+
<message name="IDS_BRAVE_ADBLOCK_UPDATE_LISTS_BUTTON_LABEL" desc="A button label to update all active filter lists">
427+
Update lists
428+
</message>
429+
<message name="IDS_BRAVE_ADBLOCK_UPDATE_LISTS_RETRY_BUTTON_LABEL" desc="A button label to retry the update of all active filter lists">
430+
Update failed - Retry
431+
</message>
432+
<message name="IDS_BRAVE_ADBLOCK_UPDATE_LISTS_UPDATED_BUTTON_LABEL" desc="A button label when list updates have finished">
433+
Lists updated
434+
</message>
435+
<message name="IDS_BRAVE_ADBLOCK_UPDATE_LISTS_UPDATING_BUTTON_LABEL" desc="A button label when lists are updating">
436+
Lists updating
437+
</message>
426438
<message name="IDS_BRAVE_ADBLOCK_CUSTOM_FILTER_LISTS_INPUT_PLACEHOLDER" desc="A placeholder label for the input form to ask the user to enter a URL">
427439
Enter filter list URL
428440
</message>

browser/resources/settings/default_brave_shields_page/brave_adblock_browser_proxy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { sendWithPromise, addWebUiListener } from 'chrome://resources/js/cr.js';
1010
export interface BraveAdblockBrowserProxy {
1111
getRegionalLists(): Promise<any[]> // TODO(petemill): Define the expected type
1212
enableFilterList(uuid: string, enabled: boolean)
13+
updateFilterList(uuid: string): Promise<boolean>
1314
getListSubscriptions(): Promise<any> // TODO(petemill): Define the expected type
1415
getCustomFilters(): Promise<any> // TODO(petemill): Define the expected type
1516
setSubscriptionEnabled(url: string, enabled: boolean)
@@ -44,6 +45,11 @@ export class BraveAdblockBrowserProxyImpl implements BraveAdblockBrowserProxy {
4445
chrome.send('brave_adblock.enableFilterList', [uuid, enabled])
4546
}
4647

48+
/** @returns {Promise<boolean>} */
49+
updateFilterLists () {
50+
return sendWithPromise('brave_adblock.updateFilterLists')
51+
}
52+
4753
setSubscriptionEnabled (url, enabled) {
4854
chrome.send('brave_adblock.setSubscriptionEnabled', [url, enabled])
4955
}

browser/resources/settings/default_brave_shields_page/brave_adblock_subpage.html

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,18 @@
8686
font-weight: 500;
8787
}
8888

89-
.show-list-button-box {
89+
.list-actions-box {
90+
display: flex;
9091
margin-bottom: 10px;
92+
gap: 8px;
93+
}
94+
95+
.update-lists-updated {
96+
--leo-button-color: var(--leo-color-systemfeedback-success-text);
97+
}
98+
99+
.update-lists-failed {
100+
--leo-button-color: var(--leo-color-systemfeedback-error-text);
91101
}
92102

93103
.subscribe-url-form {
@@ -186,11 +196,38 @@
186196
</div>
187197
</template>
188198
</div>
189-
<template is="dom-if" if="[[!hasListExpanded_]]">
190-
<div class="show-list-button-box">
191-
<cr-button on-click="handleShowList_">$i18n{adblockShowFullListsButtonLabel}</cr-button>
199+
<div class="list-actions-box">
200+
<template is="dom-if" if="[[!hasListExpanded_]]">
201+
<div>
202+
<cr-button on-click="handleShowList_">$i18n{adblockShowFullListsButtonLabel}</cr-button>
203+
</div>
204+
</template>
205+
<div class$="update-lists-[[listsUpdatingState_]]">
206+
<template is="dom-if" if="[[!listsUpdatingState_]]">
207+
<cr-button on-click="handleUpdateLists_">
208+
<span>$i18n{adblockUpdateListsButtonLabel}</span>
209+
</cr-button>
210+
</template>
211+
<template is="dom-if" if="[[isEqual_(listsUpdatingState_, 'updating')]]">
212+
<cr-button on-click="handleUpdateLists_">
213+
<leo-progressring slot="prefix-icon"></leo-progressring>
214+
<span>$i18n{adblockUpdateListsUpdatingButtonLabel}</span>
215+
</cr-button>
216+
</template>
217+
<template is="dom-if" if="[[isEqual_(listsUpdatingState_, 'updated')]]">
218+
<cr-button on-click="handleUpdateLists_">
219+
<leo-icon name="check-circle-outline" slot="prefix-icon"></leo-icon>
220+
<span>$i18n{adblockUpdateListsUpdatedButtonLabel}</span>
221+
</cr-button>
222+
</template>
223+
<template is="dom-if" if="[[isEqual_(listsUpdatingState_, 'failed')]]">
224+
<cr-button on-click="handleUpdateLists_">
225+
<leo-icon name="warning-circle-outline" slot="prefix-icon"></leo-icon>
226+
<span>$i18n{adblockUpdateListsRetryButtonLabel}</span>
227+
</cr-button>
228+
</template>
192229
</div>
193-
</template>
230+
</div>
194231
</div>
195232
</div>
196233
<div class="settings-box">

browser/resources/settings/default_brave_shields_page/brave_adblock_subpage.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AdBlockSubpage extends AdBlockSubpageBase {
3636
subscriptionList_: Array,
3737
customFilters_: String,
3838
subscribeUrl_: String,
39+
listsUpdatingState_: String,
3940
hasListExpanded_: {
4041
type: Boolean,
4142
value: false
@@ -47,6 +48,9 @@ class AdBlockSubpage extends AdBlockSubpageBase {
4748

4849
ready() {
4950
super.ready()
51+
52+
this.listsUpdatingState_ = ''
53+
5054
this.browserProxy_.getRegionalLists().then(value => {
5155
this.filterList_ = value
5256
})
@@ -71,6 +75,20 @@ class AdBlockSubpage extends AdBlockSubpageBase {
7175
}
7276
}
7377

78+
handleUpdateLists_() {
79+
if (this.listsUpdatingState_ === 'updating') {
80+
return
81+
}
82+
83+
this.listsUpdatingState_ = 'updating'
84+
85+
this.browserProxy_.updateFilterLists().then(() => {
86+
this.listsUpdatingState_ = 'updated'
87+
}, () => {
88+
this.listsUpdatingState_ = 'failed'
89+
})
90+
}
91+
7492
searchListBy_(title) {
7593
if (!title) {
7694
this.hasListExpanded_ = false

browser/ui/webui/settings/brave_adblock_handler.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void BraveAdBlockHandler::RegisterMessages() {
4242
base::BindRepeating(&BraveAdBlockHandler::EnableFilterList,
4343
base::Unretained(this)));
4444

45+
web_ui()->RegisterMessageCallback(
46+
"brave_adblock.updateFilterLists",
47+
base::BindRepeating(&BraveAdBlockHandler::UpdateFilterLists,
48+
base::Unretained(this)));
49+
4550
web_ui()->RegisterMessageCallback(
4651
"brave_adblock.getListSubscriptions",
4752
base::BindRepeating(&BraveAdBlockHandler::GetListSubscriptions,
@@ -123,6 +128,23 @@ void BraveAdBlockHandler::EnableFilterList(const base::Value::List& args) {
123128
->EnableFilterList(uuid, enabled);
124129
}
125130

131+
void BraveAdBlockHandler::UpdateFilterLists(const base::Value::List& args) {
132+
AllowJavascript();
133+
134+
DCHECK_EQ(args.size(), 1U);
135+
if (!args[0].is_string()) {
136+
return;
137+
}
138+
139+
std::string callback_id = args[0].GetString();
140+
141+
g_brave_browser_process->ad_block_service()
142+
->component_service_manager()
143+
->UpdateFilterLists(
144+
base::BindOnce(&BraveAdBlockHandler::OnFilterListsUpdated,
145+
weak_factory_.GetWeakPtr(), std::move(callback_id)));
146+
}
147+
126148
void BraveAdBlockHandler::GetListSubscriptions(const base::Value::List& args) {
127149
AllowJavascript();
128150
ResolveJavascriptCallback(args[0], GetSubscriptions());
@@ -284,3 +306,12 @@ base::Value::List BraveAdBlockHandler::GetSubscriptions() {
284306

285307
return list_value;
286308
}
309+
310+
void BraveAdBlockHandler::OnFilterListsUpdated(std::string callback_id,
311+
bool success) {
312+
if (success) {
313+
ResolveJavascriptCallback(base::Value(callback_id), base::Value());
314+
} else {
315+
RejectJavascriptCallback(base::Value(callback_id), base::Value());
316+
}
317+
}

browser/ui/webui/settings/brave_adblock_handler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
#ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_ADBLOCK_HANDLER_H_
77
#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_ADBLOCK_HANDLER_H_
88

9+
#include <string>
10+
911
#include "base/scoped_observation.h"
1012

1113
#include "base/memory/raw_ptr.h"
14+
#include "base/memory/weak_ptr.h"
1215
#include "brave/components/brave_shields/content/browser/ad_block_subscription_service_manager.h"
1316
#include "brave/components/brave_shields/content/browser/ad_block_subscription_service_manager_observer.h"
1417
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
@@ -37,6 +40,7 @@ class BraveAdBlockHandler : public settings::SettingsPageUIHandler,
3740

3841
void GetRegionalLists(const base::Value::List& args);
3942
void EnableFilterList(const base::Value::List& args);
43+
void UpdateFilterLists(const base::Value::List& args);
4044
void GetListSubscriptions(const base::Value::List& args);
4145
void GetCustomFilters(const base::Value::List& args);
4246
void AddSubscription(const base::Value::List& args);
@@ -50,11 +54,15 @@ class BraveAdBlockHandler : public settings::SettingsPageUIHandler,
5054

5155
base::Value::List GetSubscriptions();
5256

57+
void OnFilterListsUpdated(std::string callback_id, bool success);
58+
5359
raw_ptr<Profile> profile_ = nullptr;
5460

5561
base::ScopedObservation<AdBlockSubscriptionServiceManager,
5662
AdBlockSubscriptionServiceManagerObserver>
5763
service_observer_{this};
64+
65+
base::WeakPtrFactory<BraveAdBlockHandler> weak_factory_{this};
5866
};
5967

6068
#endif // BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_ADBLOCK_HANDLER_H_

browser/ui/webui/settings/brave_settings_localized_strings_provider.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,14 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
689689
{"adblockAddListsButtonLabel", IDS_BRAVE_ADBLOCK_ADD_LISTS_BUTTON_LABEL},
690690
{"adblockShowFullListsButtonLabel",
691691
IDS_BRAVE_ADBLOCK_SHOW_FULL_LISTS_BUTTON_LABEL},
692+
{"adblockUpdateListsButtonLabel",
693+
IDS_BRAVE_ADBLOCK_UPDATE_LISTS_BUTTON_LABEL},
694+
{"adblockUpdateListsRetryButtonLabel",
695+
IDS_BRAVE_ADBLOCK_UPDATE_LISTS_RETRY_BUTTON_LABEL},
696+
{"adblockUpdateListsUpdatedButtonLabel",
697+
IDS_BRAVE_ADBLOCK_UPDATE_LISTS_UPDATED_BUTTON_LABEL},
698+
{"adblockUpdateListsUpdatingButtonLabel",
699+
IDS_BRAVE_ADBLOCK_UPDATE_LISTS_UPDATING_BUTTON_LABEL},
692700
{"adblockFilterListsInputURLPlaceholder",
693701
IDS_BRAVE_ADBLOCK_CUSTOM_FILTER_LISTS_INPUT_PLACEHOLDER},
694702
{"adblockContentFiltersLabel", IDS_BRAVE_ADBLOCK_CONTENT_FILTERS},

chromium_src/components/component_updater/component_updater_service.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@ class BraveComponentUpdaterAndroid;
1414
}
1515
} // namespace chrome
1616

17+
namespace brave_shields {
18+
class AdBlockComponentFiltersProvider;
19+
}
20+
1721
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ \
1822
friend class ::IPFSDOMHandler; \
1923
friend class ::chrome::android::BraveComponentUpdaterAndroid;
2024

21-
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER \
22-
private: \
23-
friend void BraveOnDemandUpdate(const std::string&); \
24-
\
25+
#define BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER \
26+
private: \
27+
friend void BraveOnDemandUpdate(const std::string&); \
28+
friend class brave_shields::AdBlockComponentFiltersProvider; \
29+
\
2530
public:
2631
#include "src/components/component_updater/component_updater_service.h" // IWYU pragma: export
2732
#undef BRAVE_COMPONENT_UPDATER_SERVICE_H_ON_DEMAND_UPDATER

components/brave_shields/core/browser/ad_block_component_filters_provider.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,21 @@ void AdBlockComponentFiltersProvider::LoadFilterSet(
125125
base::BindOnce(&OnReadDATFileData, std::move(cb), permission_mask_));
126126
}
127127

128+
void AdBlockComponentFiltersProvider::UpdateComponent(
129+
base::OnceCallback<void(bool)> callback) {
130+
if (!component_updater_service_) {
131+
std::move(callback).Run(false);
132+
return;
133+
}
134+
135+
auto on_updated = [](decltype(callback) cb, update_client::Error error) {
136+
std::move(cb).Run(error == update_client::Error::NONE ||
137+
error == update_client::Error::UPDATE_IN_PROGRESS);
138+
};
139+
140+
component_updater_service_->GetOnDemandUpdater().OnDemandUpdate(
141+
component_id_, component_updater::OnDemandUpdater::Priority::FOREGROUND,
142+
base::BindOnce(on_updated, std::move(callback)));
143+
}
144+
128145
} // namespace brave_shields

components/brave_shields/core/browser/ad_block_component_filters_provider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class AdBlockComponentFiltersProvider : public AdBlockFiltersProvider {
5555
base::OnceCallback<void(
5656
base::OnceCallback<void(rust::Box<adblock::FilterSet>*)>)>) override;
5757

58+
// Updates the component, and when complete executes the specified callback
59+
// with a value indicating success or failure.
60+
void UpdateComponent(base::OnceCallback<void(bool)> callback);
61+
5862
// Remove the component. This will force it to be redownloaded next time it
5963
// is registered.
6064
void UnregisterComponent();

components/brave_shields/core/browser/ad_block_component_service_manager.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include <utility>
1010
#include <vector>
1111

12+
#include "base/barrier_callback.h"
1213
#include "base/feature_list.h"
1314
#include "base/memory/raw_ref.h"
1415
#include "base/metrics/histogram_macros.h"
16+
#include "base/ranges/algorithm.h"
1517
#include "base/values.h"
1618
#include "brave/components/brave_shields/core/browser/ad_block_component_filters_provider.h"
1719
#include "brave/components/brave_shields/core/browser/ad_block_filters_provider_manager.h"
@@ -252,6 +254,35 @@ void AdBlockComponentServiceManager::EnableFilterList(const std::string& uuid,
252254
UpdateFilterListPrefs(uuid, enabled);
253255
}
254256

257+
void AdBlockComponentServiceManager::UpdateFilterLists(
258+
base::OnceCallback<void(bool)> callback) {
259+
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
260+
261+
// If there are currently no components to update, then run the callback with
262+
// a success value in a future turn.
263+
if (component_filters_providers_.empty()) {
264+
std::move(callback).Run(true);
265+
return;
266+
}
267+
268+
// This callback will be executed by our barrier callback once all filter
269+
// list components have been updated.
270+
auto on_all_updated = [](decltype(callback) cb, std::vector<bool> results) {
271+
std::move(cb).Run(
272+
base::ranges::all_of(results, [](bool result) { return result; }));
273+
};
274+
275+
// This barrier callback maintains a "completed count". When it has been
276+
// called the expected number of times, it will execute `on_all_updated`.
277+
auto barrier_callback = base::BarrierCallback<bool>(
278+
component_filters_providers_.size(),
279+
base::BindOnce(on_all_updated, std::move(callback)));
280+
281+
for (auto& [key, provider] : component_filters_providers_) {
282+
provider->UpdateComponent(barrier_callback);
283+
}
284+
}
285+
255286
void AdBlockComponentServiceManager::SetFilterListCatalog(
256287
std::vector<FilterListCatalogEntry> catalog) {
257288
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

components/brave_shields/core/browser/ad_block_component_service_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class AdBlockComponentServiceManager
5454
bool IsFilterListEnabled(const std::string& uuid) const;
5555
void EnableFilterList(const std::string& uuid, bool enabled);
5656

57+
void UpdateFilterLists(base::OnceCallback<void(bool)> callback);
58+
5759
// AdBlockFilterListCatalogProvider::Observer
5860
void OnFilterListCatalogLoaded(const std::string& catalog_json) override;
5961

ui/webui/resources/leo/web_components.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import '@brave/leo/web-components/button'
99
import '@brave/leo/web-components/dropdown'
1010
import '@brave/leo/web-components/checkbox'
11+
import '@brave/leo/web-components/progressRing'
1112
import '@brave/leo/web-components/toggle'
1213
import { setIconBasePath } from '@brave/leo/web-components/icon'
1314
import iconsMeta from '@brave/leo/icons/meta'

0 commit comments

Comments
 (0)