From 4744d77e1a434cbd4019c51b2ecbbdf11302c891 Mon Sep 17 00:00:00 2001 From: zenparsing Date: Thu, 26 May 2022 09:05:00 -0400 Subject: [PATCH] Use mojo for panel API interfaces --- browser/brave_content_browser_client.cc | 4 + browser/sources.gni | 1 + .../brave_rewards_action_view.cc | 10 +- .../brave_actions/brave_rewards_action_view.h | 1 - .../webui/brave_rewards/rewards_panel_ui.cc | 458 ++++++++---------- .../ui/webui/brave_rewards/rewards_panel_ui.h | 20 +- components/brave_rewards/common/BUILD.gn | 8 + .../common/brave_rewards_panel.mojom | 34 ++ .../resources/rewards_panel/BUILD.gn | 2 + .../rewards_panel/lib/bubble_events.ts | 33 -- .../rewards_panel/lib/extension_host.ts | 72 +-- 11 files changed, 326 insertions(+), 317 deletions(-) create mode 100644 components/brave_rewards/common/brave_rewards_panel.mojom delete mode 100644 components/brave_rewards/resources/rewards_panel/lib/bubble_events.ts diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 20448972ac66..40295c31d1fc 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -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" @@ -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< diff --git a/browser/sources.gni b/browser/sources.gni index 003f847ab144..f524e6dfe8c5 100644 --- a/browser/sources.gni +++ b/browser/sources.gni @@ -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", diff --git a/browser/ui/views/brave_actions/brave_rewards_action_view.cc b/browser/ui/views/brave_actions/brave_rewards_action_view.cc index 9a375e2fc7cb..85800df5ea27 100644 --- a/browser/ui/views/brave_actions/brave_rewards_action_view.cc +++ b/browser/ui/views/brave_actions/brave_rewards_action_view.cc @@ -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" @@ -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( diff --git a/browser/ui/views/brave_actions/brave_rewards_action_view.h b/browser/ui/views/brave_actions/brave_rewards_action_view.h index 2071a29295b3..5745f516608b 100644 --- a/browser/ui/views/brave_actions/brave_rewards_action_view.h +++ b/browser/ui/views/brave_actions/brave_rewards_action_view.h @@ -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" diff --git a/browser/ui/webui/brave_rewards/rewards_panel_ui.cc b/browser/ui/webui/brave_rewards/rewards_panel_ui.cc index 4529caa71775..744844d2f9a6 100644 --- a/browser/ui/webui/brave_rewards/rewards_panel_ui.cc +++ b/browser/ui/webui/brave_rewards/rewards_panel_ui.cc @@ -1,4 +1,4 @@ -/* 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/. */ @@ -9,12 +9,15 @@ #include #include +#include "base/memory/raw_ptr.h" +#include "base/memory/weak_ptr.h" #include "brave/browser/brave_rewards/rewards_panel_service.h" #include "brave/browser/brave_rewards/rewards_panel_service_factory.h" #include "brave/browser/brave_rewards/rewards_service_factory.h" #include "brave/browser/brave_rewards/rewards_tab_helper.h" #include "brave/common/webui_url_constants.h" #include "brave/components/brave_adaptive_captcha/server_util.h" +#include "brave/components/brave_rewards/browser/rewards_service.h" #include "brave/components/brave_rewards/resources/grit/brave_rewards_panel_generated_map.h" #include "brave/components/brave_rewards/resources/grit/brave_rewards_resources.h" #include "chrome/browser/profiles/profile.h" @@ -26,7 +29,6 @@ #include "components/grit/brave_components_strings.h" #include "content/public/browser/web_contents.h" #include "content/public/browser/web_ui.h" -#include "content/public/browser/web_ui_message_handler.h" #include "services/network/public/mojom/content_security_policy.mojom.h" namespace { @@ -40,8 +42,8 @@ Functional issues: that happened. - When we press "Refresh" to update a publisher status, the badge is not updated as expected. -- Switching between verified publisher tabs should not result in a visible flash - of the verified checkmark. +- After "starting" rewards and downloading the publisher index, the checkmark + should update without having to switch tabs. Other issues: @@ -54,98 +56,68 @@ Other issues: to listen for that? Can we do that on tab reloads instead? - Better (decent) debugging for panel "spinner" stalls and the Rewards panel in general. -- Replace WebUI message handlers with a Mojo thing. - Rewards button context menu has different padding and corner radius */ -using brave_rewards::RewardsPanelService; -using brave_rewards::RewardsPanelServiceFactory; -using brave_rewards::RewardsServiceFactory; -using brave_rewards::RewardsTabHelper; - -// TODO(zenparsing): Alphabetize this list. static constexpr webui::LocalizedString kStrings[] = { - {"summary", IDS_REWARDS_PANEL_SUMMARY}, - {"tip", IDS_REWARDS_PANEL_TIP}, - {"unverifiedCreator", IDS_REWARDS_PANEL_UNVERIFIED_CREATOR}, - {"verifiedCreator", IDS_REWARDS_PANEL_VERIFIED_CREATOR}, - {"refreshStatus", IDS_REWARDS_PANEL_REFRESH_STATUS}, - {"pendingTipText", IDS_REWARDS_PANEL_PENDING_TIP_TEXT}, - {"pendingTipTitle", IDS_REWARDS_PANEL_PENDING_TIP_TITLE}, - {"pendingTipTitleRegistered", - IDS_REWARDS_PANEL_PENDING_TIP_TITLE_REGISTERED}, - {"platformPublisherTitle", IDS_REWARDS_PANEL_PLATFORM_PUBLISHER_TITLE}, {"attention", IDS_REWARDS_PANEL_ATTENTION}, - {"sendTip", IDS_REWARDS_PANEL_SEND_TIP}, - {"includeInAutoContribute", IDS_REWARDS_PANEL_INCLUDE_IN_AUTO_CONTRIBUTE}, - {"monthlyTip", IDS_REWARDS_PANEL_MONTHLY_TIP}, - {"ok", IDS_REWARDS_PANEL_OK}, - {"set", IDS_REWARDS_PANEL_SET}, - {"changeAmount", IDS_REWARDS_PANEL_CHANGE_AMOUNT}, + {"braveTalkBraveRewardsDescription", + IDS_REWARDS_BRAVE_TALK_BRAVE_REWARDS_DESCRIPTION}, + {"braveTalkCanStartFreeCall", IDS_REWARDS_BRAVE_TALK_CAN_START_FREE_CALL}, + {"braveTalkClickAnywhereToBraveTalk", + IDS_REWARDS_BRAVE_TALK_CLICK_ANYWHERE_TO_BRAVE_TALK}, + {"braveTalkOptInRewardsTerms", IDS_REWARDS_BRAVE_TALK_OPT_IN_REWARDS_TERMS}, + {"braveTalkPrivateAdsDescription", + IDS_REWARDS_BRAVE_TALK_PRIVATE_ADS_DESCRIPTION}, + {"braveTalkTurnOnPrivateAds", IDS_REWARDS_BRAVE_TALK_TURN_ON_PRIVATE_ADS}, + {"braveTalkTurnOnPrivateAdsToStartCall", + IDS_REWARDS_BRAVE_TALK_TURN_ON_PRIVATE_ADS_TO_START_CALL}, + {"braveTalkTurnOnRewards", IDS_REWARDS_BRAVE_TALK_TURN_ON_REWARDS}, + {"braveTalkTurnOnRewardsToStartCall", + IDS_REWARDS_BRAVE_TALK_TURN_ON_REWARDS_TO_START_CALL}, + {"braveTalkWantLearnMore", IDS_REWARDS_BRAVE_TALK_WANT_LEARN_MORE}, {"cancel", IDS_REWARDS_PANEL_CANCEL}, - {"grantCaptchaTitle", IDS_REWARDS_GRANT_CAPTCHA_TITLE}, + {"captchaContactSupport", IDS_REWARDS_CAPTCHA_CONTACT_SUPPORT}, + {"captchaDismiss", IDS_REWARDS_CAPTCHA_DISMISS}, + {"captchaMaxAttemptsExceededText", + IDS_REWARDS_CAPTCHA_MAX_ATTEMPTS_EXCEEDED_TEXT}, + {"captchaMaxAttemptsExceededTitle", + IDS_REWARDS_CAPTCHA_MAX_ATTEMPTS_EXCEEDED_TITLE}, + {"captchaSolvedText", IDS_REWARDS_CAPTCHA_SOLVED_TEXT}, + {"captchaSolvedTitle", IDS_REWARDS_CAPTCHA_SOLVED_TITLE}, + {"changeAmount", IDS_REWARDS_PANEL_CHANGE_AMOUNT}, + {"grantCaptchaAmountAds", IDS_REWARDS_GRANT_CAPTCHA_AMOUNT_ADS}, + {"grantCaptchaAmountUGP", IDS_REWARDS_GRANT_CAPTCHA_AMOUNT_UGP}, + {"grantCaptchaErrorText", IDS_REWARDS_GRANT_CAPTCHA_ERROR_TEXT}, + {"grantCaptchaErrorTitle", IDS_REWARDS_GRANT_CAPTCHA_ERROR_TITLE}, + {"grantCaptchaExpiration", IDS_REWARDS_GRANT_CAPTCHA_EXPIRATION}, {"grantCaptchaFailedTitle", IDS_REWARDS_GRANT_CAPTCHA_FAILED_TITLE}, {"grantCaptchaHint", IDS_REWARDS_GRANT_CAPTCHA_HINT}, - {"grantCaptchaPassedTitleUGP", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TITLE_UGP}, + {"grantCaptchaPassedTextAds", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TEXT_ADS}, {"grantCaptchaPassedTextUGP", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TEXT_UGP}, - {"grantCaptchaAmountUGP", IDS_REWARDS_GRANT_CAPTCHA_AMOUNT_UGP}, {"grantCaptchaPassedTitleAds", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TITLE_ADS}, - {"grantCaptchaPassedTextAds", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TEXT_ADS}, - {"grantCaptchaAmountAds", IDS_REWARDS_GRANT_CAPTCHA_AMOUNT_ADS}, - {"grantCaptchaExpiration", IDS_REWARDS_GRANT_CAPTCHA_EXPIRATION}, - {"grantCaptchaErrorTitle", IDS_REWARDS_GRANT_CAPTCHA_ERROR_TITLE}, - {"grantCaptchaErrorText", IDS_REWARDS_GRANT_CAPTCHA_ERROR_TEXT}, - {"rewardsLogInToSeeBalance", IDS_REWARDS_LOG_IN_TO_SEE_BALANCE}, - {"rewardsPaymentCheckStatus", IDS_REWARDS_PAYMENT_CHECK_STATUS}, - {"rewardsPaymentCompleted", IDS_REWARDS_PAYMENT_COMPLETED}, - {"rewardsPaymentPending", IDS_REWARDS_PAYMENT_PENDING}, - {"rewardsPaymentProcessing", IDS_REWARDS_PAYMENT_PROCESSING}, - {"walletAccountLink", IDS_REWARDS_WALLET_ACCOUNT_LINK}, - {"walletAddFunds", IDS_REWARDS_WALLET_ADD_FUNDS}, - {"walletAutoContribute", IDS_REWARDS_WALLET_AUTO_CONTRIBUTE}, - {"walletDisconnected", IDS_REWARDS_WALLET_DISCONNECTED}, - {"walletDisconnectLink", IDS_REWARDS_WALLET_DISCONNECT_LINK}, - {"walletEstimatedEarnings", IDS_REWARDS_WALLET_ESTIMATED_EARNINGS}, - {"walletLogIntoYourAccount", IDS_REWARDS_WALLET_LOG_INTO_YOUR_ACCOUNT}, - {"walletMonthlyTips", IDS_REWARDS_WALLET_MONTHLY_TIPS}, - {"walletOneTimeTips", IDS_REWARDS_WALLET_ONE_TIME_TIPS}, - {"walletPending", IDS_REWARDS_WALLET_PENDING}, - {"walletPendingText", IDS_REWARDS_WALLET_PENDING_TEXT}, - {"walletRewardsFromAds", IDS_REWARDS_WALLET_REWARDS_FROM_ADS}, - {"walletRewardsSummary", IDS_REWARDS_WALLET_REWARDS_SUMMARY}, - {"walletUnverified", IDS_REWARDS_WALLET_UNVERIFIED}, - {"walletVerified", IDS_REWARDS_WALLET_VERIFIED}, - {"walletYourBalance", IDS_REWARDS_WALLET_YOUR_BALANCE}, + {"grantCaptchaPassedTitleUGP", IDS_REWARDS_GRANT_CAPTCHA_PASSED_TITLE_UGP}, + {"grantCaptchaTitle", IDS_REWARDS_GRANT_CAPTCHA_TITLE}, + {"includeInAutoContribute", IDS_REWARDS_PANEL_INCLUDE_IN_AUTO_CONTRIBUTE}, + {"monthlyTip", IDS_REWARDS_PANEL_MONTHLY_TIP}, {"notificationAddFunds", IDS_REWARDS_NOTIFICATION_ADD_FUNDS}, - {"notificationReconnect", IDS_REWARDS_NOTIFICATION_RECONNECT}, - {"notificationClaimRewards", IDS_REWARDS_NOTIFICATION_CLAIM_REWARDS}, - {"notificationClaimTokens", IDS_REWARDS_NOTIFICATION_CLAIM_TOKENS}, - {"notificationAddFundsTitle", IDS_REWARDS_NOTIFICATION_ADD_FUNDS_TITLE}, {"notificationAddFundsText", IDS_REWARDS_NOTIFICATION_ADD_FUNDS_TEXT}, - {"notificationAutoContributeCompletedTitle", - IDS_REWARDS_NOTIFICATION_AUTO_CONTRIBUTE_COMPLETED_TITLE}, + {"notificationAddFundsTitle", IDS_REWARDS_NOTIFICATION_ADD_FUNDS_TITLE}, + {"notificationAdGrantAmount", IDS_REWARDS_NOTIFICATION_AD_GRANT_AMOUNT}, + {"notificationAdGrantTitle", IDS_REWARDS_NOTIFICATION_AD_GRANT_TITLE}, {"notificationAutoContributeCompletedText", IDS_REWARDS_NOTIFICATION_AUTO_CONTRIBUTE_COMPLETED_TEXT}, - {"notificationBackupWalletTitle", - IDS_REWARDS_NOTIFICATION_BACKUP_WALLET_TITLE}, - {"notificationBackupWalletText", - IDS_REWARDS_NOTIFICATION_BACKUP_WALLET_TEXT}, + {"notificationAutoContributeCompletedTitle", + IDS_REWARDS_NOTIFICATION_AUTO_CONTRIBUTE_COMPLETED_TITLE}, {"notificationBackupWalletAction", IDS_REWARDS_NOTIFICATION_BACKUP_WALLET_ACTION}, - {"notificationWalletDisconnectedTitle", - IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_TITLE}, - {"notificationWalletDisconnectedText", - IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_TEXT}, - {"notificationWalletDisconnectedAction", - IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_ACTION}, - {"notificationWalletVerifiedTitle", - IDS_REWARDS_NOTIFICATION_WALLET_VERIFIED_TITLE}, - {"notificationWalletVerifiedText", - IDS_REWARDS_NOTIFICATION_WALLET_VERIFIED_TEXT}, - {"notificationTokenGrantTitle", IDS_REWARDS_NOTIFICATION_TOKEN_GRANT_TITLE}, - {"notificationAdGrantAmount", IDS_REWARDS_NOTIFICATION_AD_GRANT_AMOUNT}, - {"notificationAdGrantTitle", IDS_REWARDS_NOTIFICATION_AD_GRANT_TITLE}, + {"notificationBackupWalletText", + IDS_REWARDS_NOTIFICATION_BACKUP_WALLET_TEXT}, + {"notificationBackupWalletTitle", + IDS_REWARDS_NOTIFICATION_BACKUP_WALLET_TITLE}, + {"notificationClaimRewards", IDS_REWARDS_NOTIFICATION_CLAIM_REWARDS}, + {"notificationClaimTokens", IDS_REWARDS_NOTIFICATION_CLAIM_TOKENS}, {"notificationGrantDaysRemaining", IDS_REWARDS_NOTIFICATION_GRANT_DAYS_REMAINING}, {"notificationInsufficientFundsText", @@ -154,20 +126,73 @@ static constexpr webui::LocalizedString kStrings[] = { IDS_REWARDS_NOTIFICATION_MONTHLY_CONTRIBUTION_FAILED_TEXT}, {"notificationMonthlyContributionFailedTitle", IDS_REWARDS_NOTIFICATION_MONTHLY_CONTRIBUTION_FAILED_TITLE}, - {"notificationMonthlyTipCompletedTitle", - IDS_REWARDS_NOTIFICATION_MONTHLY_TIP_COMPLETED_TITLE}, {"notificationMonthlyTipCompletedText", IDS_REWARDS_NOTIFICATION_MONTHLY_TIP_COMPLETED_TEXT}, - {"notificationPublisherVerifiedTitle", - IDS_REWARDS_NOTIFICATION_PUBLISHER_VERIFIED_TITLE}, - {"notificationPublisherVerifiedText", - IDS_REWARDS_NOTIFICATION_PUBLISHER_VERIFIED_TEXT}, - {"notificationPendingTipFailedTitle", - IDS_REWARDS_NOTIFICATION_PENDING_TIP_FAILED_TITLE}, + {"notificationMonthlyTipCompletedTitle", + IDS_REWARDS_NOTIFICATION_MONTHLY_TIP_COMPLETED_TITLE}, {"notificationPendingTipFailedText", IDS_REWARDS_NOTIFICATION_PENDING_TIP_FAILED_TEXT}, + {"notificationPendingTipFailedTitle", + IDS_REWARDS_NOTIFICATION_PENDING_TIP_FAILED_TITLE}, + {"notificationPublisherVerifiedText", + IDS_REWARDS_NOTIFICATION_PUBLISHER_VERIFIED_TEXT}, + {"notificationPublisherVerifiedTitle", + IDS_REWARDS_NOTIFICATION_PUBLISHER_VERIFIED_TITLE}, + {"notificationReconnect", IDS_REWARDS_NOTIFICATION_RECONNECT}, + {"notificationWalletDisconnectedAction", + IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_ACTION}, + {"notificationWalletDisconnectedText", + IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_TEXT}, + {"notificationWalletDisconnectedTitle", + IDS_REWARDS_NOTIFICATION_WALLET_DISCONNECTED_TITLE}, + {"notificationTokenGrantTitle", IDS_REWARDS_NOTIFICATION_TOKEN_GRANT_TITLE}, + {"notificationWalletVerifiedText", + IDS_REWARDS_NOTIFICATION_WALLET_VERIFIED_TEXT}, + {"notificationWalletVerifiedTitle", + IDS_REWARDS_NOTIFICATION_WALLET_VERIFIED_TITLE}, + {"ok", IDS_REWARDS_PANEL_OK}, {"onboardingEarnHeader", IDS_BRAVE_REWARDS_ONBOARDING_EARN_HEADER}, {"onboardingEarnText", IDS_BRAVE_REWARDS_ONBOARDING_EARN_TEXT}, + {"onboardingPanelAcHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_HEADER}, + {"onboardingPanelAcText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_TEXT}, + {"onboardingPanelAdsHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_ADS_HEADER}, + {"onboardingPanelAdsText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_ADS_TEXT}, + {"onboardingPanelBitflyerLearnMore", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_LEARN_MORE}, + {"onboardingPanelBitflyerNote", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_NOTE}, + {"onboardingPanelBitflyerText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_TEXT}, + {"onboardingPanelCompleteHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_COMPLETE_HEADER}, + {"onboardingPanelCompleteText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_COMPLETE_TEXT}, + {"onboardingPanelRedeemHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_REDEEM_HEADER}, + {"onboardingPanelRedeemText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_REDEEM_TEXT}, + {"onboardingPanelScheduleHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SCHEDULE_HEADER}, + {"onboardingPanelScheduleText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SCHEDULE_TEXT}, + {"onboardingPanelSetupHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SETUP_HEADER}, + {"onboardingPanelSetupText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SETUP_TEXT}, + {"onboardingPanelTippingHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_TIPPING_HEADER}, + {"onboardingPanelTippingText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_TIPPING_TEXT}, + {"onboardingPanelVerifyHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_HEADER}, + {"onboardingPanelVerifyLater", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_LATER}, + {"onboardingPanelVerifyNow", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_NOW}, + {"onboardingPanelVerifySubtext", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_SUBTEXT}, + {"onboardingPanelWelcomeHeader", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_HEADER}, + {"onboardingPanelWelcomeText", + IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_TEXT}, {"onboardingSetupAdsHeader", IDS_BRAVE_REWARDS_ONBOARDING_SETUP_ADS_HEADER}, {"onboardingSetupAdsSubheader", IDS_BRAVE_REWARDS_ONBOARDING_SETUP_ADS_SUBHEADER}, @@ -186,192 +211,124 @@ static constexpr webui::LocalizedString kStrings[] = { {"onboardingTourSkip", IDS_BRAVE_REWARDS_ONBOARDING_TOUR_SKIP}, {"onboardingTourSkipForNow", IDS_BRAVE_REWARDS_ONBOARDING_TOUR_SKIP_FOR_NOW}, - {"onboardingPanelWelcomeHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_HEADER}, - {"onboardingPanelWelcomeText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_WELCOME_TEXT}, - {"onboardingPanelAdsHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_ADS_HEADER}, - {"onboardingPanelAdsText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_ADS_TEXT}, - {"onboardingPanelScheduleHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SCHEDULE_HEADER}, - {"onboardingPanelScheduleText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SCHEDULE_TEXT}, - {"onboardingPanelAcHeader", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_HEADER}, - {"onboardingPanelAcText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_AC_TEXT}, - {"onboardingPanelTippingHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_TIPPING_HEADER}, - {"onboardingPanelTippingText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_TIPPING_TEXT}, - {"onboardingPanelRedeemHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_REDEEM_HEADER}, - {"onboardingPanelRedeemText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_REDEEM_TEXT}, - {"onboardingPanelSetupHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SETUP_HEADER}, - {"onboardingPanelSetupText", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_SETUP_TEXT}, - {"onboardingPanelCompleteHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_COMPLETE_HEADER}, - {"onboardingPanelCompleteText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_COMPLETE_TEXT}, - {"onboardingPanelVerifyHeader", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_HEADER}, - {"onboardingPanelVerifySubtext", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_SUBTEXT}, - {"onboardingPanelVerifyLater", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_LATER}, - {"onboardingPanelVerifyNow", IDS_BRAVE_REWARDS_ONBOARDING_PANEL_VERIFY_NOW}, - {"onboardingPanelBitflyerNote", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_NOTE}, - {"onboardingPanelBitflyerText", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_TEXT}, - {"onboardingPanelBitflyerLearnMore", - IDS_BRAVE_REWARDS_ONBOARDING_PANEL_BITFLYER_LEARN_MORE}, - {"captchaMaxAttemptsExceededText", - IDS_REWARDS_CAPTCHA_MAX_ATTEMPTS_EXCEEDED_TEXT}, - {"captchaMaxAttemptsExceededTitle", - IDS_REWARDS_CAPTCHA_MAX_ATTEMPTS_EXCEEDED_TITLE}, - {"captchaSolvedTitle", IDS_REWARDS_CAPTCHA_SOLVED_TITLE}, - {"captchaSolvedText", IDS_REWARDS_CAPTCHA_SOLVED_TEXT}, - {"captchaContactSupport", IDS_REWARDS_CAPTCHA_CONTACT_SUPPORT}, - {"captchaDismiss", IDS_REWARDS_CAPTCHA_DISMISS}, - {"braveTalkTurnOnRewardsToStartCall", - IDS_REWARDS_BRAVE_TALK_TURN_ON_REWARDS_TO_START_CALL}, - {"braveTalkBraveRewardsDescription", - IDS_REWARDS_BRAVE_TALK_BRAVE_REWARDS_DESCRIPTION}, - {"braveTalkTurnOnRewards", IDS_REWARDS_BRAVE_TALK_TURN_ON_REWARDS}, - {"braveTalkOptInRewardsTerms", IDS_REWARDS_BRAVE_TALK_OPT_IN_REWARDS_TERMS}, - {"braveTalkTurnOnPrivateAdsToStartCall", - IDS_REWARDS_BRAVE_TALK_TURN_ON_PRIVATE_ADS_TO_START_CALL}, - {"braveTalkPrivateAdsDescription", - IDS_REWARDS_BRAVE_TALK_PRIVATE_ADS_DESCRIPTION}, - {"braveTalkTurnOnPrivateAds", IDS_REWARDS_BRAVE_TALK_TURN_ON_PRIVATE_ADS}, - {"braveTalkCanStartFreeCall", IDS_REWARDS_BRAVE_TALK_CAN_START_FREE_CALL}, - {"braveTalkClickAnywhereToBraveTalk", - IDS_REWARDS_BRAVE_TALK_CLICK_ANYWHERE_TO_BRAVE_TALK}, - {"braveTalkWantLearnMore", IDS_REWARDS_BRAVE_TALK_WANT_LEARN_MORE}}; + {"pendingTipText", IDS_REWARDS_PANEL_PENDING_TIP_TEXT}, + {"pendingTipTitle", IDS_REWARDS_PANEL_PENDING_TIP_TITLE}, + {"pendingTipTitleRegistered", + IDS_REWARDS_PANEL_PENDING_TIP_TITLE_REGISTERED}, + {"platformPublisherTitle", IDS_REWARDS_PANEL_PLATFORM_PUBLISHER_TITLE}, + {"refreshStatus", IDS_REWARDS_PANEL_REFRESH_STATUS}, + {"rewardsLogInToSeeBalance", IDS_REWARDS_LOG_IN_TO_SEE_BALANCE}, + {"rewardsPaymentCheckStatus", IDS_REWARDS_PAYMENT_CHECK_STATUS}, + {"rewardsPaymentCompleted", IDS_REWARDS_PAYMENT_COMPLETED}, + {"rewardsPaymentPending", IDS_REWARDS_PAYMENT_PENDING}, + {"rewardsPaymentProcessing", IDS_REWARDS_PAYMENT_PROCESSING}, + {"sendTip", IDS_REWARDS_PANEL_SEND_TIP}, + {"set", IDS_REWARDS_PANEL_SET}, + {"summary", IDS_REWARDS_PANEL_SUMMARY}, + {"tip", IDS_REWARDS_PANEL_TIP}, + {"unverifiedCreator", IDS_REWARDS_PANEL_UNVERIFIED_CREATOR}, + {"verifiedCreator", IDS_REWARDS_PANEL_VERIFIED_CREATOR}, + {"walletAccountLink", IDS_REWARDS_WALLET_ACCOUNT_LINK}, + {"walletAddFunds", IDS_REWARDS_WALLET_ADD_FUNDS}, + {"walletAutoContribute", IDS_REWARDS_WALLET_AUTO_CONTRIBUTE}, + {"walletDisconnected", IDS_REWARDS_WALLET_DISCONNECTED}, + {"walletDisconnectLink", IDS_REWARDS_WALLET_DISCONNECT_LINK}, + {"walletEstimatedEarnings", IDS_REWARDS_WALLET_ESTIMATED_EARNINGS}, + {"walletLogIntoYourAccount", IDS_REWARDS_WALLET_LOG_INTO_YOUR_ACCOUNT}, + {"walletMonthlyTips", IDS_REWARDS_WALLET_MONTHLY_TIPS}, + {"walletOneTimeTips", IDS_REWARDS_WALLET_ONE_TIME_TIPS}, + {"walletPending", IDS_REWARDS_WALLET_PENDING}, + {"walletPendingText", IDS_REWARDS_WALLET_PENDING_TEXT}, + {"walletRewardsFromAds", IDS_REWARDS_WALLET_REWARDS_FROM_ADS}, + {"walletRewardsSummary", IDS_REWARDS_WALLET_REWARDS_SUMMARY}, + {"walletUnverified", IDS_REWARDS_WALLET_UNVERIFIED}, + {"walletVerified", IDS_REWARDS_WALLET_VERIFIED}, + {"walletYourBalance", IDS_REWARDS_WALLET_YOUR_BALANCE}}; -std::string TakeRewardsPanelArgs(content::WebUI* web_ui) { - DCHECK(web_ui); - auto* panel_service = - RewardsPanelServiceFactory::GetForProfile(Profile::FromWebUI(web_ui)); +using brave_rewards::RewardsPanelService; +using brave_rewards::RewardsPanelServiceFactory; +using brave_rewards::RewardsServiceFactory; +using brave_rewards::RewardsTabHelper; +std::string TakeRewardsPanelArgs(Profile* profile) { + DCHECK(profile); + auto* panel_service = RewardsPanelServiceFactory::GetForProfile(profile); return panel_service ? panel_service->TakePanelArguments() : ""; } -class MessageHandler : public content::WebUIMessageHandler, - public RewardsPanelService::Observer { +class PanelHandlerImpl : public brave_rewards::mojom::PanelHandler, + public RewardsPanelService::Observer { public: - MessageHandler() = default; - ~MessageHandler() override = default; - - MessageHandler(const MessageHandler&) = delete; - MessageHandler& operator=(const MessageHandler&) = delete; - - // content::WebUIMessageHandler - - void OnJavascriptAllowed() override { - auto* panel_service = - RewardsPanelServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); - - if (panel_service) { - panel_service_observation_.Observe(panel_service); - } - } - - void OnJavascriptDisallowed() override { panel_service_observation_.Reset(); } - - void RegisterMessages() override { - RegisterMessage("pageReady", &MessageHandler::HandlePageReady); - RegisterMessage("showUI", &MessageHandler::HandleShowUI); - RegisterMessage("hideUI", &MessageHandler::HandleHideUI); - } - - // brave_rewards::RewardsPanelService::Observer - void OnRewardsPanelRequested(Browser* browser) override { - if (!IsJavascriptAllowed()) { - NOTREACHED(); - return; - } - - auto* panel_service = - RewardsPanelServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); - - if (panel_service) { - std::string args = TakeRewardsPanelArgs(web_ui()); - FireWebUIListener("rewardsPanelRequested", base::Value(std::move(args))); + PanelHandlerImpl( + mojo::PendingReceiver receiver, + mojo::PendingRemote ui_handler, + base::WeakPtr embedder, + Profile* profile) + : receiver_(this, std::move(receiver)), + ui_handler_(std::move(ui_handler)), + embedder_(embedder), + profile_(profile) { + DCHECK(embedder_); + DCHECK(profile_); + + if (auto* service = RewardsPanelServiceFactory::GetForProfile(profile_)) { + panel_service_observation_.Observe(service); } } - private: - template - void RegisterMessage(const std::string& name, F&& fn) { - web_ui()->RegisterMessageCallback( - name, base::BindRepeating(fn, base::Unretained(this))); - } + PanelHandlerImpl(const PanelHandlerImpl&) = delete; + PanelHandlerImpl& operator=(const PanelHandlerImpl&) = delete; - void HandlePageReady(const base::Value::List& args) { - AllowJavascript(); - StartRewards(); - } + ~PanelHandlerImpl() override = default; - void HandleShowUI(const base::Value::List& args) { - if (auto* panel_ui = GetRewardsPanelUI()) { - if (auto embedder = panel_ui->embedder()) { - embedder->ShowUI(); - } + // brave_rewards::mojom::PanelHandler: + void ShowUI() override { + if (embedder_) { + embedder_->ShowUI(); } } - void HandleHideUI(const base::Value::List& args) { - if (auto* panel_ui = GetRewardsPanelUI()) { - if (auto embedder = panel_ui->embedder()) { - embedder->CloseUI(); - } + void CloseUI() override { + if (embedder_) { + embedder_->CloseUI(); } } - void StartRewards() { - auto* rewards_service = - RewardsServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); - + void StartRewards(StartRewardsCallback callback) override { + auto* rewards_service = RewardsServiceFactory::GetForProfile(profile_); if (!rewards_service) { - return NotifyError("rewards-service-not-available"); - } - - rewards_service->StartProcess(base::BindOnce( - &MessageHandler::OnRewardsProcessStarted, weak_factory_.GetWeakPtr())); - } - - void NotifyError(const std::string& type) { - if (IsJavascriptAllowed()) { - FireWebUIListener("error", base::Value(type)); + NOTREACHED(); + std::move(callback).Run(); + return; } - } - void OnRewardsProcessStarted() { - if (IsJavascriptAllowed()) { - FireWebUIListener("rewardsStarted"); - } + rewards_service->StartProcess(std::move(callback)); } - RewardsPanelUI* GetRewardsPanelUI() { - if (auto* controller = web_ui()->GetController()) { - return controller->GetAs(); + // brave_rewards::RewardsPanelService::Observer: + void OnRewardsPanelRequested(Browser* browser) override { + if (ui_handler_) { + ui_handler_->OnRewardsPanelRequested(TakeRewardsPanelArgs(profile_)); } - return nullptr; } + private: + mojo::Receiver receiver_; + mojo::Remote ui_handler_; + base::WeakPtr embedder_; + raw_ptr profile_ = nullptr; RewardsPanelService::Observation panel_service_observation_{this}; - base::WeakPtrFactory weak_factory_{this}; }; } // namespace RewardsPanelUI::RewardsPanelUI(content::WebUI* web_ui) : MojoBubbleWebUIController(web_ui, true) { + auto* profile = Profile::FromWebUI(web_ui); + auto* source = content::WebUIDataSource::Create(kBraveRewardsPanelHost); source->AddLocalizedStrings(kStrings); - source->AddString("rewardsPanelArgs", TakeRewardsPanelArgs(web_ui)); + source->AddString("rewardsPanelArgs", TakeRewardsPanelArgs(profile)); webui::SetupWebUIDataSource(source, base::make_span(kBraveRewardsPanelGenerated, @@ -388,14 +345,25 @@ RewardsPanelUI::RewardsPanelUI(content::WebUI* web_ui) content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), source); - auto* profile = Profile::FromWebUI(web_ui); - auto favicon_source = std::make_unique( - profile, chrome::FaviconUrlFormat::kFavicon2); - content::URLDataSource::Add(profile, std::move(favicon_source)); - - web_ui->AddMessageHandler(std::make_unique()); + content::URLDataSource::Add( + profile, std::make_unique( + profile, chrome::FaviconUrlFormat::kFavicon2)); } RewardsPanelUI::~RewardsPanelUI() = default; WEB_UI_CONTROLLER_TYPE_IMPL(RewardsPanelUI) + +void RewardsPanelUI::BindInterface( + mojo::PendingReceiver receiver) { + panel_factory_receiver_.reset(); + panel_factory_receiver_.Bind(std::move(receiver)); +} + +void RewardsPanelUI::CreatePanelHandler( + mojo::PendingReceiver panel_handler, + mojo::PendingRemote ui_handler) { + panel_handler_ = std::make_unique( + std::move(panel_handler), std::move(ui_handler), embedder(), + Profile::FromWebUI(web_ui())); +} diff --git a/browser/ui/webui/brave_rewards/rewards_panel_ui.h b/browser/ui/webui/brave_rewards/rewards_panel_ui.h index 486e546d9c70..7b4aad9a97c2 100644 --- a/browser/ui/webui/brave_rewards/rewards_panel_ui.h +++ b/browser/ui/webui/brave_rewards/rewards_panel_ui.h @@ -1,4 +1,4 @@ -/* 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/. */ @@ -6,9 +6,15 @@ #ifndef BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_REWARDS_PANEL_UI_H_ #define BRAVE_BROWSER_UI_WEBUI_BRAVE_REWARDS_REWARDS_PANEL_UI_H_ +#include + +#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; @@ -16,7 +22,17 @@ class RewardsPanelUI : public ui::MojoBubbleWebUIController { RewardsPanelUI(const RewardsPanelUI&) = delete; RewardsPanelUI& operator=(const RewardsPanelUI&) = delete; + void BindInterface(mojo::PendingReceiver receiver); + private: + // brave_rewards::mojom::PanelHandlerFactory: + void CreatePanelHandler( + mojo::PendingReceiver panel_handler, + mojo::PendingRemote ui_handler) override; + + std::unique_ptr panel_handler_; + mojo::Receiver panel_factory_receiver_{this}; + WEB_UI_CONTROLLER_TYPE_DECL(); }; diff --git a/components/brave_rewards/common/BUILD.gn b/components/brave_rewards/common/BUILD.gn index 2853dfb96b57..42bfd4ff6b00 100644 --- a/components/brave_rewards/common/BUILD.gn +++ b/components/brave_rewards/common/BUILD.gn @@ -1,3 +1,5 @@ +import("//mojo/public/tools/bindings/mojom.gni") + source_set("features") { sources = [ "features.cc", @@ -20,3 +22,9 @@ static_library("common") { deps = [ ":features" ] } + +mojom("mojom") { + sources = [ "brave_rewards_panel.mojom" ] + + public_deps = [ "//mojo/public/mojom/base" ] +} diff --git a/components/brave_rewards/common/brave_rewards_panel.mojom b/components/brave_rewards/common/brave_rewards_panel.mojom new file mode 100644 index 000000000000..1de9dfbd8c88 --- /dev/null +++ b/components/brave_rewards/common/brave_rewards_panel.mojom @@ -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 panel_handler, + pending_remote 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); +}; diff --git a/components/brave_rewards/resources/rewards_panel/BUILD.gn b/components/brave_rewards/resources/rewards_panel/BUILD.gn index afbc1bde17d0..dcd68118f802 100644 --- a/components/brave_rewards/resources/rewards_panel/BUILD.gn +++ b/components/brave_rewards/resources/rewards_panel/BUILD.gn @@ -1,4 +1,5 @@ import("//brave/components/common/typescript.gni") +import("//mojo/public/tools/bindings/mojom.gni") transpile_web_ui("brave_rewards_panel") { entry_points = [ [ @@ -6,6 +7,7 @@ transpile_web_ui("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") { diff --git a/components/brave_rewards/resources/rewards_panel/lib/bubble_events.ts b/components/brave_rewards/resources/rewards_panel/lib/bubble_events.ts deleted file mode 100644 index bcbbd3a74953..000000000000 --- a/components/brave_rewards/resources/rewards_panel/lib/bubble_events.ts +++ /dev/null @@ -1,33 +0,0 @@ -/* 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/. */ - -interface Listener { - onBubbleOpening: () => void - onBubbleClosing: () => void -} - -type State = 'open' | 'closed' - -let state: State = document.visibilityState === 'hidden' ? 'closed' : 'open' -const listeners: Array = [] - -document.addEventListener('visibilitychange', () => { - if (document.visibilityState === 'hidden') { - if (state === 'open') { - state = 'closed' - for (const listener of listeners) { - listener.onBubbleClosing() - } - } else { - state = 'open' - for (const listener of listeners) { - listener.onBubbleOpening() - } - } - } -}) - -export function addBubbleListener (listener: Listener) { - listeners.push(listener) -} diff --git a/components/brave_rewards/resources/rewards_panel/lib/extension_host.ts b/components/brave_rewards/resources/rewards_panel/lib/extension_host.ts index 886b58b71883..1587f873bd1f 100644 --- a/components/brave_rewards/resources/rewards_panel/lib/extension_host.ts +++ b/components/brave_rewards/resources/rewards_panel/lib/extension_host.ts @@ -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' @@ -49,12 +48,8 @@ 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 { @@ -62,6 +57,28 @@ export function createHost (): Host { const storage = createLocalStorageScope('rewards-panel') const grants = new Map() + 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) { @@ -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[]) { @@ -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 }) @@ -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((resolve) => { - addWebUIListener('rewardsStarted', resolve) - }) + await panelHandler.startRewards() await loadPanelData() @@ -597,7 +601,7 @@ export function createHost (): Host { openTab, onAppRendered () { - chrome.send('showUI') + panelHandler.showUI() } } }