Skip to content

Commit

Permalink
Use mojo for panel API interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zenparsing committed May 26, 2022
1 parent 8b04c1c commit 4744d77
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 317 deletions.
4 changes: 4 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,13 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#include "brave/browser/new_tab/new_tab_shows_navigation_throttle.h"
#include "brave/browser/ui/webui/brave_federated/federated_internals.mojom.h"
#include "brave/browser/ui/webui/brave_federated/federated_internals_ui.h"
#include "brave/browser/ui/webui/brave_rewards/rewards_panel_ui.h"
#include "brave/browser/ui/webui/brave_shields/shields_panel_ui.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_page_ui.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_panel_ui.h"
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_ui.h"
#include "brave/components/brave_new_tab_ui/brave_new_tab_page.mojom.h"
#include "brave/components/brave_rewards/common/brave_rewards_panel.mojom.h"
#include "brave/components/brave_shields/common/brave_shields_panel.mojom.h"
#include "brave/components/brave_today/common/brave_news.mojom.h"
#include "brave/components/brave_today/common/features.h"
Expand Down Expand Up @@ -528,6 +530,8 @@ void BraveContentBrowserClient::RegisterBrowserInterfaceBindersForFrame(
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_shields::mojom::PanelHandlerFactory, ShieldsPanelUI>(map);
}
chrome::internal::RegisterWebUIControllerInterfaceBinder<
brave_rewards::mojom::PanelHandlerFactory, RewardsPanelUI>(map);
if (base::FeatureList::IsEnabled(
brave_federated::features::kFederatedLearning)) {
chrome::internal::RegisterWebUIControllerInterfaceBinder<
Expand Down
1 change: 1 addition & 0 deletions browser/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ brave_chrome_browser_deps = [
"//brave/components/brave_federated",
"//brave/components/brave_perf_predictor/browser",
"//brave/components/brave_referrals/buildflags",
"//brave/components/brave_rewards/common:mojom",
"//brave/components/brave_rewards/common/buildflags",
"//brave/components/brave_search/browser",
"//brave/components/brave_search/common",
Expand Down
10 changes: 8 additions & 2 deletions browser/ui/views/brave_actions/brave_rewards_action_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/ui/brave_actions/brave_action_icon_with_badge_image_source.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/grit/brave_generated_resources.h"
Expand Down Expand Up @@ -253,8 +254,13 @@ void BraveRewardsActionView::OnPublisherUpdated(
if (!publisher_id.empty()) {
if (auto* rewards_service = GetRewardsService()) {
// TODO(zenparsing): When rewards is enabled, should we automatically
// check this again? Unfortunately we don't have a way to listen for
// Rewards being enabled. Perhaps initialized will work?
// check this again? We can use |OnRewardsInitialized|, but it doesn't
// really work because the publisher index has not been fully populated
// yet. We may need to introduce an event that is triggered when the
// publisher index has been updated.
// TODO(zenparsing): We also need to update the badge when the user
// manually refreshes the publisher status from the Rewards panel UI. It
// looks like we'll need another event for that.
if (rewards_service->IsRewardsEnabled()) {
status_pending = true;
rewards_service->IsPublisherRegistered(
Expand Down
1 change: 0 additions & 1 deletion browser/ui/views/brave_actions/brave_rewards_action_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include "brave/browser/ui/webui/brave_rewards/rewards_panel_ui.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/views/bubble/webui_bubble_manager.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
Expand Down
458 changes: 213 additions & 245 deletions browser/ui/webui/brave_rewards/rewards_panel_ui.cc

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions browser/ui/webui/brave_rewards/rewards_panel_ui.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
/* Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_REWARDS_PANEL_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_REWARDS_PANEL_UI_H_

#include <memory>

#include "brave/components/brave_rewards/common/brave_rewards_panel.mojom.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "ui/webui/mojo_bubble_web_ui_controller.h"

class RewardsPanelUI : public ui::MojoBubbleWebUIController {
class RewardsPanelUI : public ui::MojoBubbleWebUIController,
public brave_rewards::mojom::PanelHandlerFactory {
public:
explicit RewardsPanelUI(content::WebUI* web_ui);
~RewardsPanelUI() override;

RewardsPanelUI(const RewardsPanelUI&) = delete;
RewardsPanelUI& operator=(const RewardsPanelUI&) = delete;

void BindInterface(mojo::PendingReceiver<PanelHandlerFactory> receiver);

private:
// brave_rewards::mojom::PanelHandlerFactory:
void CreatePanelHandler(
mojo::PendingReceiver<brave_rewards::mojom::PanelHandler> panel_handler,
mojo::PendingRemote<brave_rewards::mojom::UIHandler> ui_handler) override;

std::unique_ptr<brave_rewards::mojom::PanelHandler> panel_handler_;
mojo::Receiver<PanelHandlerFactory> panel_factory_receiver_{this};

WEB_UI_CONTROLLER_TYPE_DECL();
};

Expand Down
8 changes: 8 additions & 0 deletions components/brave_rewards/common/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import("//mojo/public/tools/bindings/mojom.gni")

source_set("features") {
sources = [
"features.cc",
Expand All @@ -20,3 +22,9 @@ static_library("common") {

deps = [ ":features" ]
}

mojom("mojom") {
sources = [ "brave_rewards_panel.mojom" ]

public_deps = [ "//mojo/public/mojom/base" ]
}
34 changes: 34 additions & 0 deletions components/brave_rewards/common/brave_rewards_panel.mojom
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2022 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 http://mozilla.org/MPL/2.0/.

module brave_rewards.mojom;

// Used by the WebUI page to bootstrap bidirectional communication.
interface PanelHandlerFactory {
// The WebUI calls this method when the page is first initialized.
CreatePanelHandler(pending_receiver<PanelHandler> panel_handler,
pending_remote<UIHandler> ui_handler);
};

// Browser-side handler for requests from WebUI page.
interface PanelHandler {
// Notify the browser that the UI is ready to be shown.
ShowUI();

// Notify the browser that the panel should be closed.
CloseUI();

// Starts the Rewards services if necessary.
StartRewards() => ();
};

// WebUI-side handler for requests from the browser.
// TODO(zenparsing): This name is not specific enough, if we add more mojo
// WebUI interfaces.
interface UIHandler {
// Called when a browser component has requested that the Rewards panel be
// displayed to the user.
OnRewardsPanelRequested(string panel_args);
};
2 changes: 2 additions & 0 deletions components/brave_rewards/resources/rewards_panel/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import("//brave/components/common/typescript.gni")
import("//mojo/public/tools/bindings/mojom.gni")

transpile_web_ui("brave_rewards_panel") {
entry_points = [ [
"brave_rewards_panel",
rebase_path("main.tsx"),
] ]
resource_name = "brave_rewards_panel"
deps = [ "//brave/components/brave_rewards/common:mojom_js" ]
}

pack_web_resources("brave_rewards_panel_generated") {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */


import { addWebUIListener } from 'chrome://resources/js/cr.m'
import * as mojom from 'gen/brave/components/brave_rewards/common/brave_rewards_panel.mojom.m.js'

import { Host, GrantCaptchaStatus } from './interfaces'
import { GrantInfo } from '../../shared/lib/grant_info'
Expand Down Expand Up @@ -49,19 +48,37 @@ function openTab (url: string) {
chrome.tabs.create({ url })
}

function closePanel () {
chrome.send('hideUI')
}

function getString (key: string) {
return String(self['loadTimeData'].getString(key) || '')
return String((window as any).loadTimeData.getString(key) || '')
}

export function createHost (): Host {
const stateManager = createStateManager(getInitialState())
const storage = createLocalStorageScope<LocalStorageKey>('rewards-panel')
const grants = new Map<string, GrantInfo>()

const panelHandler = new mojom.PanelHandlerRemote()

const uiHandler = new mojom.UIHandlerReceiver({
onRewardsPanelRequested (panelArgs) {
loadPanelData().then(() => {
stateManager.update({
openTime: Date.now(),
requestedView: null
})
handleRewardsPanelArgs(String(panelArgs || ''))
}).catch(console.error)
}
})

mojom.PanelHandlerFactory.getRemote().createPanelHandler(
panelHandler.$.bindNewPipeAndPassReceiver(),
uiHandler.$.bindNewPipeAndPassRemote())

function closePanel () {
panelHandler.closeUI()
}

async function updatePublisherInfo () {
const tabInfo = await getCurrentTabInfo()
if (tabInfo) {
Expand Down Expand Up @@ -187,42 +204,44 @@ export function createHost (): Host {
const shouldLoadAdaptiveCaptcha = storage.readJSON('load-adaptive-captcha')
if (shouldLoadAdaptiveCaptcha) {
loadAdaptiveCaptcha()
return
return true
}

const storedGrantId = storage.readJSON('catcha-grant-id')
if (storedGrantId && typeof storedGrantId === 'string') {
loadGrantCaptcha(storedGrantId, 'pending')
return
return true
}

return false
}

function handleRewardsPanelArgs (args: string) {
// TODO(zenparsing): Consider making these keys into an enumerated type.
const params = new URLSearchParams(args)

stateManager.update({ requestedView: null })

const grantId = params.get('claim-grant')
if (grantId) {
loadGrantCaptcha(grantId, 'pending')
return
return true
}

if (params.has('adaptive-captcha')) {
loadAdaptiveCaptcha()
return
return true
}

if (params.has('rewards-tour')) {
stateManager.update({ requestedView: 'rewards-tour'})
return
stateManager.update({ requestedView: 'rewards-tour' })
return true
}

if (params.has('brave-talk-opt-in')) {
stateManager.update({ requestedView: 'brave-talk-opt-in'})
return
stateManager.update({ requestedView: 'brave-talk-opt-in' })
return true
}

return false
}

function updateGrants (list: GrantInfo[]) {
Expand Down Expand Up @@ -263,17 +282,6 @@ export function createHost (): Host {
}

function addListeners () {
addWebUIListener('error', (type: any) => {
console.error(new Error(`WebUI error "${type}"`))
})

addWebUIListener('rewardsPanelRequested', (args: any) => {
loadPanelData().then(() => {
stateManager.update({ openTime: Date.now() })
handleRewardsPanelArgs(String(args || ''))
}).catch(console.error)
})

// Update user settings and other data after rewards has been enabled.
apiAdapter.onRewardsEnabled(() => {
stateManager.update({ rewardsEnabled: true })
Expand Down Expand Up @@ -367,11 +375,7 @@ export function createHost (): Host {
// the data that we have, rather than a stalled loading indicator.
setTimeout(() => { stateManager.update({ loading: false }) }, 3000)

chrome.send('pageReady')

await new Promise<void>((resolve) => {
addWebUIListener('rewardsStarted', resolve)
})
await panelHandler.startRewards()

await loadPanelData()

Expand Down Expand Up @@ -597,7 +601,7 @@ export function createHost (): Host {
openTab,

onAppRendered () {
chrome.send('showUI')
panelHandler.showUI()
}
}
}

0 comments on commit 4744d77

Please sign in to comment.