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

Fixes clicking tipping button before opting into rewards. #15541

Merged
merged 1 commit into from
Oct 19, 2022
Merged
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
53 changes: 43 additions & 10 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,30 @@ RewardsTabHelper* GetRewardsTabHelperForTabId(
return RewardsTabHelper::FromWebContents(web_contents);
}

content::WebContents* WebContentsFromBrowserContext(
int tab_id,
content::BrowserContext* browser_context) {
content::WebContents* contents = nullptr;
extensions::ExtensionTabUtil::GetTabById(
tab_id, Profile::FromBrowserContext(browser_context), false, nullptr,
nullptr, &contents, nullptr);
return contents;
}

RewardsPanelCoordinator* GetPanelCoordinator(
content::WebContents* web_contents) {
DCHECK(web_contents);
auto* browser = chrome::FindBrowserWithWebContents(web_contents);
return browser ? RewardsPanelCoordinator::FromBrowser(browser) : nullptr;
}

RewardsPanelCoordinator* GetPanelCoordinator(ExtensionFunction* function) {
DCHECK(function);
auto* web_contents = function->GetSenderWebContents();
if (!web_contents) {
return nullptr;
}
auto* browser = chrome::FindBrowserWithWebContents(web_contents);
return browser ? RewardsPanelCoordinator::FromBrowser(browser) : nullptr;
return GetPanelCoordinator(web_contents);
}

std::string StringifyResult(ledger::mojom::CreateRewardsWalletResult result) {
Expand Down Expand Up @@ -375,10 +391,9 @@ ExtensionFunction::ResponseAction BraveRewardsTipSiteFunction::Run() {
}

// Get web contents for this tab
content::WebContents* contents = nullptr;
if (!ExtensionTabUtil::GetTabById(
params->tab_id, Profile::FromBrowserContext(browser_context()), false,
nullptr, nullptr, &contents, nullptr)) {
content::WebContents* contents =
WebContentsFromBrowserContext(params->tab_id, browser_context());
if (!contents) {
return RespondNow(Error(tabs_constants::kTabNotFoundError,
base::NumberToString(params->tab_id)));
}
Expand Down Expand Up @@ -412,6 +427,25 @@ ExtensionFunction::ResponseAction BraveRewardsTipUserFunction::Run() {
return RespondNow(Error("Rewards service is not initialized"));
}

// If the user clicks the tipping button before having opted into the Rewards,
// then the Rewards service would not have started the ledger process yet. We
// need to open the Rewards panel for the user to offer opting in.
if (!profile->GetPrefs()->GetBoolean(::brave_rewards::prefs::kEnabled)) {
// Get web contents for this tab
content::WebContents* contents =
WebContentsFromBrowserContext(params->tab_id, browser_context());
if (!contents) {
return RespondNow(Error(tabs_constants::kTabNotFoundError,
base::NumberToString(params->tab_id)));
}
auto* coordinator = GetPanelCoordinator(contents);
if (!coordinator) {
return RespondNow(Error("Unable to open Rewards panel"));
}
coordinator->OpenRewardsPanel();
return RespondNow(NoArguments());
}

AddRef();

rewards_service->GetPublisherInfo(
Expand Down Expand Up @@ -480,10 +514,9 @@ void BraveRewardsTipUserFunction::ShowTipDialog() {
}

// Get web contents for this tab
content::WebContents* contents = nullptr;
if (!ExtensionTabUtil::GetTabById(
params->tab_id, Profile::FromBrowserContext(browser_context()), false,
nullptr, nullptr, &contents, nullptr)) {
content::WebContents* contents =
WebContentsFromBrowserContext(params->tab_id, browser_context());
if (!contents) {
Release();
return;
}
Expand Down