diff --git a/browser/brave_rewards/android/brave_rewards_native_worker.cc b/browser/brave_rewards/android/brave_rewards_native_worker.cc index ece3e9bb9e56..1a5b60e5f928 100644 --- a/browser/brave_rewards/android/brave_rewards_native_worker.cc +++ b/browser/brave_rewards/android/brave_rewards_native_worker.cc @@ -42,7 +42,6 @@ namespace android { BraveRewardsNativeWorker::BraveRewardsNativeWorker(JNIEnv* env, const base::android::JavaRef& obj): weak_java_brave_rewards_native_worker_(env, obj), - brave_rewards_service_(nullptr), weak_factory_(this) { Java_BraveRewardsNativeWorker_setNativePtr(env, obj, reinterpret_cast(this)); @@ -50,11 +49,11 @@ BraveRewardsNativeWorker::BraveRewardsNativeWorker(JNIEnv* env, brave_rewards_service_ = brave_rewards::RewardsServiceFactory::GetForProfile( ProfileManager::GetActiveUserProfile()->GetOriginalProfile()); if (brave_rewards_service_) { - brave_rewards_service_->AddObserver(this); - brave_rewards::RewardsNotificationService* notification_service = - brave_rewards_service_->GetNotificationService(); - if (notification_service) { - notification_service->AddObserver(this); + rewards_service_observation_.Observe(brave_rewards_service_); + + if (auto* notification_service = + brave_rewards_service_->GetNotificationService()) { + rewards_notification_service_observation_.Observe(notification_service); } } } @@ -63,14 +62,6 @@ BraveRewardsNativeWorker::~BraveRewardsNativeWorker() { } void BraveRewardsNativeWorker::Destroy(JNIEnv* env) { - if (brave_rewards_service_) { - brave_rewards_service_->RemoveObserver(this); - brave_rewards::RewardsNotificationService* notification_service = - brave_rewards_service_->GetNotificationService(); - if (notification_service) { - notification_service->RemoveObserver(this); - } - } delete this; } @@ -251,8 +242,8 @@ void BraveRewardsNativeWorker::GetPublisherInfo( int tabId, const base::android::JavaParamRef& host) { if (brave_rewards_service_) { - brave_rewards_service_->GetPublisherActivityFromUrl(tabId, - base::android::ConvertJavaStringToUTF8(env, host), "", ""); + brave_rewards_service_->GetPublisherActivityFromUrl( + tabId, base::android::ConvertJavaStringToUTF8(env, host), "", ""); } } @@ -578,26 +569,16 @@ void BraveRewardsNativeWorker::OnSendContribution(bool result) { } void BraveRewardsNativeWorker::GetAllNotifications(JNIEnv* env) { - if (!brave_rewards_service_) { - return; - } - brave_rewards::RewardsNotificationService* notification_service = - brave_rewards_service_->GetNotificationService(); - if (notification_service) { - notification_service->GetNotifications(); + if (rewards_notification_service_observation_.IsObserving()) { + rewards_notification_service_observation_.GetSource()->GetNotifications(); } } void BraveRewardsNativeWorker::DeleteNotification(JNIEnv* env, const base::android::JavaParamRef& notification_id) { - if (!brave_rewards_service_) { - return; - } - brave_rewards::RewardsNotificationService* notification_service = - brave_rewards_service_->GetNotificationService(); - if (notification_service) { - notification_service->DeleteNotification( - base::android::ConvertJavaStringToUTF8(env, notification_id)); + if (rewards_notification_service_observation_.IsObserving()) { + rewards_notification_service_observation_.GetSource()->DeleteNotification( + base::android::ConvertJavaStringToUTF8(env, notification_id)); } } @@ -704,14 +685,14 @@ double BraveRewardsNativeWorker::GetPublisherRecurrentDonationAmount( void BraveRewardsNativeWorker::RemoveRecurring(JNIEnv* env, const base::android::JavaParamRef& publisher) { if (brave_rewards_service_) { - brave_rewards_service_->RemoveRecurringTip( + brave_rewards_service_->RemoveRecurringTip( + base::android::ConvertJavaStringToUTF8(env, publisher)); + auto it = map_recurrent_publishers_.find( base::android::ConvertJavaStringToUTF8(env, publisher)); - auto it = map_recurrent_publishers_.find( - base::android::ConvertJavaStringToUTF8(env, publisher)); - if (it != map_recurrent_publishers_.end()) { - map_recurrent_publishers_.erase(it); - } + if (it != map_recurrent_publishers_.end()) { + map_recurrent_publishers_.erase(it); + } } } diff --git a/browser/brave_rewards/android/brave_rewards_native_worker.h b/browser/brave_rewards/android/brave_rewards_native_worker.h index 650d71f5bdab..7907f9458a32 100644 --- a/browser/brave_rewards/android/brave_rewards_native_worker.h +++ b/browser/brave_rewards/android/brave_rewards_native_worker.h @@ -7,6 +7,7 @@ #define BRAVE_BROWSER_BRAVE_REWARDS_ANDROID_BRAVE_REWARDS_NATIVE_WORKER_H_ #include + #include #include #include @@ -16,6 +17,7 @@ #include "base/containers/flat_map.h" #include "base/memory/raw_ptr.h" #include "base/memory/weak_ptr.h" +#include "base/scoped_observation.h" #include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h" #include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h" #include "brave/components/brave_rewards/browser/rewards_service_observer.h" @@ -273,6 +275,12 @@ class BraveRewardsNativeWorker std::map map_recurrent_publishers_; std::map addresses_; + base::ScopedObservation + rewards_service_observation_{this}; + base::ScopedObservation + rewards_notification_service_observation_{this}; base::WeakPtrFactory weak_factory_; }; diff --git a/browser/brave_rewards/rewards_tab_helper.cc b/browser/brave_rewards/rewards_tab_helper.cc index fc34cd14836f..e21f4acce14d 100644 --- a/browser/brave_rewards/rewards_tab_helper.cc +++ b/browser/brave_rewards/rewards_tab_helper.cc @@ -55,10 +55,9 @@ RewardsTabHelper::RewardsTabHelper(content::WebContents* web_contents) if (tab_id_.is_valid()) { rewards_service_ = RewardsServiceFactory::GetForProfile( Profile::FromBrowserContext(GetWebContents().GetBrowserContext())); - } - - if (rewards_service_) { - rewards_service_->AddObserver(this); + if (rewards_service_) { + rewards_service_observation_.Observe(rewards_service_); + } } #if !BUILDFLAG(IS_ANDROID) @@ -67,9 +66,6 @@ RewardsTabHelper::RewardsTabHelper(content::WebContents* web_contents) } RewardsTabHelper::~RewardsTabHelper() { - if (rewards_service_) { - rewards_service_->RemoveObserver(this); - } #if !BUILDFLAG(IS_ANDROID) BrowserList::RemoveObserver(browser_list_observer_.get()); #endif @@ -186,10 +182,8 @@ void RewardsTabHelper::OnRewardsInitialized(RewardsService* rewards_service) { // When Rewards is initialized for the current profile, we need to inform the // utility process about the currently active tab so that it can start // measuring auto-contribute correctly. - if (rewards_service_) { - rewards_service_->OnShow(tab_id_); - rewards_service_->OnLoad(tab_id_, GetWebContents().GetLastCommittedURL()); - } + rewards_service->OnShow(tab_id_); + rewards_service->OnLoad(tab_id_, GetWebContents().GetLastCommittedURL()); } void RewardsTabHelper::MaybeSavePublisherInfo() { diff --git a/browser/brave_rewards/rewards_tab_helper.h b/browser/brave_rewards/rewards_tab_helper.h index e5ed86df44dc..685d70c1c8aa 100644 --- a/browser/brave_rewards/rewards_tab_helper.h +++ b/browser/brave_rewards/rewards_tab_helper.h @@ -89,7 +89,10 @@ class RewardsTabHelper : public content::WebContentsUserData, std::unique_ptr browser_list_observer_; #endif SessionID tab_id_; - raw_ptr rewards_service_ = nullptr; // NOT OWNED + base::ScopedObservation + rewards_service_observation_{this}; + raw_ptr rewards_service_ = nullptr; base::ObserverList observer_list_; std::string publisher_id_;