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

[ads] Fix intermittent BraveNewsController::GetDisplayAd DCHECK during browser shutdown #25480

Merged
merged 1 commit into from
Sep 9, 2024
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
100 changes: 69 additions & 31 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "mojo/public/cpp/bindings/callback_helpers.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
Expand Down Expand Up @@ -1207,7 +1208,9 @@ void AdsServiceImpl::GetStatementOfAccounts(
return std::move(callback).Run(/*statement*/ nullptr);
}

bat_ads_associated_remote_->GetStatementOfAccounts(std::move(callback));
bat_ads_associated_remote_->GetStatementOfAccounts(
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*statement*/ nullptr));
}

void AdsServiceImpl::MaybeServeInlineContentAd(
Expand All @@ -1218,8 +1221,10 @@ void AdsServiceImpl::MaybeServeInlineContentAd(
/*inline_content_ad*/ std::nullopt);
}

bat_ads_associated_remote_->MaybeServeInlineContentAd(dimensions,
std::move(callback));
bat_ads_associated_remote_->MaybeServeInlineContentAd(
dimensions,
mojo::WrapCallbackWithDefaultInvokeIfNotRun(
Copy link
Collaborator Author

@aseren aseren Sep 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use mojo::WrapCallbackWithDefaultInvokeIfNotRun to wrap mojo callback (or a wrapper around mojo callback). This allows us call the callback when ads mojom connection is down which allows us to avoid DCHECK:

    DCHECK(!connected)
        << "BraveNewsController::GetDisplayAd was destroyed without "
        << "first either being run or its corresponding binding being closed. "
        << "It is an error to drop response callbacks which still correspond "
        << "to an open interface pipe.";

std::move(callback), dimensions, /*inline_content_ad*/ std::nullopt));
}

void AdsServiceImpl::TriggerInlineContentAdEvent(
Expand All @@ -1235,7 +1240,8 @@ void AdsServiceImpl::TriggerInlineContentAdEvent(

bat_ads_associated_remote_->TriggerInlineContentAdEvent(
placement_id, creative_instance_id, mojom_ad_event_type,
std::move(callback));
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::PrefetchNewTabPageAd() {
Expand Down Expand Up @@ -1314,79 +1320,111 @@ void AdsServiceImpl::TriggerSearchResultAdEvent(
TriggerAdEventCallback callback) {
CHECK(mojom::IsKnownEnumValue(mojom_ad_event_type));

if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->TriggerSearchResultAdEvent(
std::move(mojom_creative_ad), mojom_ad_event_type, std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->TriggerSearchResultAdEvent(
std::move(mojom_creative_ad), mojom_ad_event_type, std::move(callback));
}

void AdsServiceImpl::PurgeOrphanedAdEventsForType(
const mojom::AdType mojom_ad_type,
PurgeOrphanedAdEventsForTypeCallback callback) {
CHECK(mojom::IsKnownEnumValue(mojom_ad_type));

if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->PurgeOrphanedAdEventsForType(
mojom_ad_type, std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->PurgeOrphanedAdEventsForType(mojom_ad_type,
std::move(callback));
}

void AdsServiceImpl::GetAdHistory(const base::Time from_time,
const base::Time to_time,
GetAdHistoryForUICallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->GetAdHistory(from_time, to_time,
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*ad_history*/ std::nullopt);
}

bat_ads_associated_remote_->GetAdHistory(
from_time, to_time,
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*ad_history*/ std::nullopt));
}

void AdsServiceImpl::ToggleLikeAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleLikeAd(std::move(mojom_reaction),
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleLikeAd(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::ToggleDislikeAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleDislikeAd(std::move(mojom_reaction),
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleDislikeAd(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::ToggleLikeSegment(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleLikeSegment(std::move(mojom_reaction),
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleLikeSegment(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::ToggleDislikeSegment(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleDislikeSegment(std::move(mojom_reaction),
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleDislikeSegment(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::ToggleSaveAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleSaveAd(std::move(mojom_reaction),
std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleSaveAd(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::ToggleMarkAdAsInappropriate(
mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) {
if (bat_ads_associated_remote_.is_bound()) {
bat_ads_associated_remote_->ToggleMarkAdAsInappropriate(
std::move(mojom_reaction), std::move(callback));
if (!bat_ads_associated_remote_.is_bound()) {
return std::move(callback).Run(/*success*/ false);
}

bat_ads_associated_remote_->ToggleMarkAdAsInappropriate(
std::move(mojom_reaction),
mojo::WrapCallbackWithDefaultInvokeIfNotRun(std::move(callback),
/*success*/ false));
}

void AdsServiceImpl::NotifyTabTextContentDidChange(
Expand Down
Loading