Skip to content

Commit

Permalink
Migrate away from content::WebUI::RegisterDeprecatedMessageCallback()
Browse files Browse the repository at this point in the history
This is the second part of the migration started with the upgrade to
Chromium 95.0.4628.3, which actually removes any usage of this method
and replaces it with RegisterMessageCallback() + the proper callbacks.

Chromium change:

https://source.chromium.org/chromium/chromium/src/+/720e8acd2ca08ea8579b7d4438fe1a15cd577d2c

commit 720e8acd2ca08ea8579b7d4438fe1a15cd577d2c
Author: cammie <cammie@chromium.org>
Date:   Wed Aug 25 19:15:45 2021 +0000

    Code Health: Rename/replace content::WebUI::RegisterMessageCallback().

    Special thanks to thestig@chromium.org for writing the original
    version of this patch at https://crrev.com/c/2496505.

    This CL renames the original RegisterMessageCallback() as
    RegisterDeprecatedMessageCallback() and adds a new version of
    RegisterMessageCallback() that passes a base::Value::ConstListView in
    the callback instead of const base::ListValue*, as base::ListValue is
    now deprecated.

    Most of the many callsites of the original RegisterMessageCallback()
    are converted to use RegisterDeprecatedMessageCallback() for the
    moment so that the migration to the new RegisterMessageCallback()
    can be split into future CLs (with tracking bug
    https://crbug.com/1243386).

    We do, however, use the new API in a couple spots
    (chrome/browser/ui/webui/crashes_ui.cc and
    chrome/browser/ui/webui/settings/chromeos/ambient_mode_handler.cc)
    to show that it works.

    We also update the examples for RegisterMessageCallback in
    docs/webui_explainer.md and docs/webui_in_components.md.

    AX-Relnotes: N/A.
    Bug: 1142540,1187062,1243386
  • Loading branch information
mariospr committed Sep 3, 2021
1 parent 94818d2 commit 49f1ace
Show file tree
Hide file tree
Showing 26 changed files with 1,020 additions and 1,142 deletions.
135 changes: 69 additions & 66 deletions browser/ui/webui/brave_adblock_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <utility>

#include "base/scoped_observation.h"
#include "base/values.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/common/webui_url_constants.h"
Expand Down Expand Up @@ -50,16 +51,16 @@ class AdblockDOMHandler
void OnJavascriptDisallowed() override;

private:
void HandleEnableFilterList(const base::ListValue* args);
void HandleGetCustomFilters(const base::ListValue* args);
void HandleGetRegionalLists(const base::ListValue* args);
void HandleGetListSubscriptions(const base::ListValue* args);
void HandleUpdateCustomFilters(const base::ListValue* args);
void HandleSubmitNewSubscription(const base::ListValue* args);
void HandleSetSubscriptionEnabled(const base::ListValue* args);
void HandleDeleteSubscription(const base::ListValue* args);
void HandleRefreshSubscription(const base::ListValue* args);
void HandleViewSubscriptionSource(const base::ListValue* args);
void HandleEnableFilterList(base::Value::ConstListView args);
void HandleGetCustomFilters(base::Value::ConstListView args);
void HandleGetRegionalLists(base::Value::ConstListView args);
void HandleGetListSubscriptions(base::Value::ConstListView args);
void HandleUpdateCustomFilters(base::Value::ConstListView args);
void HandleSubmitNewSubscription(base::Value::ConstListView args);
void HandleSetSubscriptionEnabled(base::Value::ConstListView args);
void HandleDeleteSubscription(base::Value::ConstListView args);
void HandleRefreshSubscription(base::Value::ConstListView args);
void HandleViewSubscriptionSource(base::Value::ConstListView args);

void RefreshSubscriptionsList();

Expand All @@ -76,43 +77,43 @@ AdblockDOMHandler::AdblockDOMHandler() {}
AdblockDOMHandler::~AdblockDOMHandler() {}

void AdblockDOMHandler::RegisterMessages() {
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.enableFilterList",
base::BindRepeating(&AdblockDOMHandler::HandleEnableFilterList,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.getCustomFilters",
base::BindRepeating(&AdblockDOMHandler::HandleGetCustomFilters,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.getRegionalLists",
base::BindRepeating(&AdblockDOMHandler::HandleGetRegionalLists,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.getListSubscriptions",
base::BindRepeating(&AdblockDOMHandler::HandleGetListSubscriptions,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.updateCustomFilters",
base::BindRepeating(&AdblockDOMHandler::HandleUpdateCustomFilters,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.submitNewSubscription",
base::BindRepeating(&AdblockDOMHandler::HandleSubmitNewSubscription,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.setSubscriptionEnabled",
base::BindRepeating(&AdblockDOMHandler::HandleSetSubscriptionEnabled,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.deleteSubscription",
base::BindRepeating(&AdblockDOMHandler::HandleDeleteSubscription,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.refreshSubscription",
base::BindRepeating(&AdblockDOMHandler::HandleRefreshSubscription,
base::Unretained(this)));
web_ui()->RegisterDeprecatedMessageCallback(
web_ui()->RegisterMessageCallback(
"brave_adblock.viewSubscriptionSource",
base::BindRepeating(&AdblockDOMHandler::HandleViewSubscriptionSource,
base::Unretained(this)));
Expand All @@ -134,20 +135,21 @@ void AdblockDOMHandler::OnServiceUpdateEvent() {
RefreshSubscriptionsList();
}

void AdblockDOMHandler::HandleEnableFilterList(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 2U);
std::string uuid;
if (!args->GetString(0, &uuid))
return;
bool enabled;
if (!args->GetBoolean(1, &enabled))
void AdblockDOMHandler::HandleEnableFilterList(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 2U);
if (!args[0].is_string() || !args[1].is_bool())
return;

std::string uuid = args[0].GetString();
bool enabled = args[1].GetBool();
g_brave_browser_process->ad_block_regional_service_manager()
->EnableFilterList(uuid, enabled);
}

void AdblockDOMHandler::HandleGetCustomFilters(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 0U);
void AdblockDOMHandler::HandleGetCustomFilters(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 0U);
AllowJavascript();
const std::string custom_filters =
g_brave_browser_process->ad_block_custom_filters_service()
Expand All @@ -156,8 +158,9 @@ void AdblockDOMHandler::HandleGetCustomFilters(const base::ListValue* args) {
base::Value(custom_filters));
}

void AdblockDOMHandler::HandleGetRegionalLists(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 0U);
void AdblockDOMHandler::HandleGetRegionalLists(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 0U);
AllowJavascript();
std::unique_ptr<base::ListValue> regional_lists =
g_brave_browser_process->ad_block_regional_service_manager()
Expand All @@ -166,30 +169,31 @@ void AdblockDOMHandler::HandleGetRegionalLists(const base::ListValue* args) {
}

void AdblockDOMHandler::HandleGetListSubscriptions(
const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 0U);
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 0U);
AllowJavascript();
RefreshSubscriptionsList();
}

void AdblockDOMHandler::HandleUpdateCustomFilters(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
std::string custom_filters;
if (!args->GetString(0, &custom_filters))
void AdblockDOMHandler::HandleUpdateCustomFilters(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 1U);
if (!args[0].is_string())
return;

std::string custom_filters = args[0].GetString();
g_brave_browser_process->ad_block_custom_filters_service()
->UpdateCustomFilters(custom_filters);
}

void AdblockDOMHandler::HandleSubmitNewSubscription(
const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 1U);
AllowJavascript();
std::string subscription_url_string;
if (!args->GetString(0, &subscription_url_string)) {
if (!args[0].is_string())
return;
}

std::string subscription_url_string = args[0].GetString();
const GURL subscription_url = GURL(subscription_url_string);
if (!subscription_url.is_valid()) {
return;
Expand All @@ -202,17 +206,14 @@ void AdblockDOMHandler::HandleSubmitNewSubscription(
}

void AdblockDOMHandler::HandleSetSubscriptionEnabled(
const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 2U);
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 2U);
AllowJavascript();
std::string subscription_url_string;
if (!args->GetString(0, &subscription_url_string)) {
if (!args[0].is_string() || !args[1].is_bool())
return;
}
bool enabled;
if (!args->GetBoolean(1, &enabled)) {
return;
}

std::string subscription_url_string = args[0].GetString();
bool enabled = args[1].GetBool();
const GURL subscription_url = GURL(subscription_url_string);
if (!subscription_url.is_valid()) {
return;
Expand All @@ -223,13 +224,14 @@ void AdblockDOMHandler::HandleSetSubscriptionEnabled(
RefreshSubscriptionsList();
}

void AdblockDOMHandler::HandleDeleteSubscription(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
void AdblockDOMHandler::HandleDeleteSubscription(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 1U);
AllowJavascript();
std::string subscription_url_string;
if (!args->GetString(0, &subscription_url_string)) {
if (!args[0].is_string())
return;
}

std::string subscription_url_string = args[0].GetString();
const GURL subscription_url = GURL(subscription_url_string);
if (!subscription_url.is_valid()) {
return;
Expand All @@ -240,15 +242,16 @@ void AdblockDOMHandler::HandleDeleteSubscription(const base::ListValue* args) {
RefreshSubscriptionsList();
}

void AdblockDOMHandler::HandleRefreshSubscription(const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
void AdblockDOMHandler::HandleRefreshSubscription(
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 1U);
// This handler does not call Javascript directly, but refreshing the
// subscription will trigger the observer later, which will require it.
AllowJavascript();
std::string subscription_url_string;
if (!args->GetString(0, &subscription_url_string)) {
if (!args[0].is_string())
return;
}

std::string subscription_url_string = args[0].GetString();
const GURL subscription_url = GURL(subscription_url_string);
if (!subscription_url.is_valid()) {
return;
Expand All @@ -259,12 +262,12 @@ void AdblockDOMHandler::HandleRefreshSubscription(const base::ListValue* args) {
}

void AdblockDOMHandler::HandleViewSubscriptionSource(
const base::ListValue* args) {
DCHECK_EQ(args->GetSize(), 1U);
std::string subscription_url_string;
if (!args->GetString(0, &subscription_url_string)) {
base::Value::ConstListView args) {
DCHECK_EQ(args.size(), 1U);
if (!args[0].is_string())
return;
}

std::string subscription_url_string = args[0].GetString();
const GURL subscription_url = GURL(subscription_url_string);
if (!subscription_url.is_valid()) {
return;
Expand Down
Loading

0 comments on commit 49f1ace

Please sign in to comment.