Skip to content

Commit

Permalink
[DanglingPtr] Fix dangling pointers in AdsServiceDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Oct 31, 2024
1 parent 188ee6e commit 499ad60
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 10 additions & 6 deletions browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "brave/components/skus/browser/pref_names.h"
#include "build/build_config.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/notifications/notification_display_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/channel_info.h"
#include "components/search_engines/template_url_prepopulate_data.h"
Expand Down Expand Up @@ -107,7 +108,6 @@ AdsServiceDelegate::AdsServiceDelegate(
PrefService* local_state,
brave_adaptive_captcha::BraveAdaptiveCaptchaService*
adaptive_captcha_service,
NotificationDisplayService* notification_display_service,
std::unique_ptr<NotificationAdPlatformBridge>
notification_ad_platform_bridge)
: profile_(profile),
Expand All @@ -117,7 +117,6 @@ AdsServiceDelegate::AdsServiceDelegate(
local_state_,
/*is_profile_eligible_for_dse_guest_propagation=*/false),
adaptive_captcha_service_(adaptive_captcha_service),
notification_display_service_(notification_display_service),
notification_ad_platform_bridge_(
std::move(notification_ad_platform_bridge)) {}

Expand Down Expand Up @@ -224,13 +223,13 @@ void AdsServiceDelegate::SnoozeScheduledCaptcha() {

void AdsServiceDelegate::Display(
const message_center::Notification& notification) {
notification_display_service_->Display(NotificationHandler::Type::BRAVE_ADS,
notification, nullptr);
GetNotificationDisplayService()->Display(NotificationHandler::Type::BRAVE_ADS,
notification, nullptr);
}

void AdsServiceDelegate::Close(const std::string& notification_id) {
notification_display_service_->Close(NotificationHandler::Type::BRAVE_ADS,
notification_id);
GetNotificationDisplayService()->Close(NotificationHandler::Type::BRAVE_ADS,
notification_id);
}

void AdsServiceDelegate::ShowNotificationAd(const std::string& id,
Expand Down Expand Up @@ -279,4 +278,9 @@ base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() {
.Set("[virtual]:skus", GetSkus());
}

NotificationDisplayService*
AdsServiceDelegate::GetNotificationDisplayService() {
return NotificationDisplayServiceFactory::GetForProfile(profile_);
}

} // namespace brave_ads
5 changes: 2 additions & 3 deletions browser/brave_ads/ads_service_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class AdsServiceDelegate : public AdsService::Delegate {
PrefService* local_state,
brave_adaptive_captcha::BraveAdaptiveCaptchaService*
adaptive_captcha_service,
NotificationDisplayService* notification_display_service,
std::unique_ptr<NotificationAdPlatformBridge>
notification_ad_platform_bridge);

Expand Down Expand Up @@ -78,13 +77,13 @@ class AdsServiceDelegate : public AdsService::Delegate {
base::Value::Dict GetVirtualPrefs() override;

private:
NotificationDisplayService* GetNotificationDisplayService();

raw_ptr<Profile> profile_ = nullptr;
raw_ptr<PrefService> local_state_ = nullptr;
search_engines::SearchEngineChoiceService search_engine_choice_service_;
raw_ptr<brave_adaptive_captcha::BraveAdaptiveCaptchaService>
adaptive_captcha_service_ = nullptr;
raw_ptr<NotificationDisplayService, DanglingUntriaged>
notification_display_service_ = nullptr;
std::unique_ptr<NotificationAdPlatformBridge>
notification_ad_platform_bridge_;
};
Expand Down
3 changes: 0 additions & 3 deletions browser/brave_ads/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,8 @@ AdsServiceFactory::BuildServiceInstanceForBrowserContext(
auto* brave_adaptive_captcha_service =
brave_adaptive_captcha::BraveAdaptiveCaptchaServiceFactory::GetInstance()
->GetForProfile(profile);
auto* display_service =
NotificationDisplayServiceFactory::GetForProfile(profile);
auto delegate = std::make_unique<AdsServiceDelegate>(
profile, g_browser_process->local_state(), brave_adaptive_captcha_service,
display_service,
std::make_unique<NotificationAdPlatformBridge>(*profile));

auto* history_service = HistoryServiceFactory::GetInstance()->GetForProfile(
Expand Down

0 comments on commit 499ad60

Please sign in to comment.