Skip to content

Commit

Permalink
[ads] Utilize AdsService for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Nov 19, 2024
1 parent 3a80e3b commit 73482e6
Show file tree
Hide file tree
Showing 9 changed files with 391 additions and 85 deletions.
2 changes: 1 addition & 1 deletion browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ void AdsServiceDelegate::CloseNotificationAd(const std::string& id,
const GURL url =
GURL(brave_ads_url_prefix.substr(0, brave_ads_url_prefix.size() - 1));
BraveNotificationPlatformBridgeHelperAndroid::MaybeRegenerateNotification(
placement_id, url);
id, url);
#endif

// We cannot store a raw_ptr to NotificationDisplayService due to upstream
Expand Down
12 changes: 0 additions & 12 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,6 @@ AdsServiceImpl::AdsServiceImpl(

AdsServiceImpl::~AdsServiceImpl() = default;

void AdsServiceImpl::AddObserver(AdsServiceObserver* const observer) {
CHECK(observer);

observers_.AddObserver(observer);
}

void AdsServiceImpl::RemoveObserver(AdsServiceObserver* const observer) {
CHECK(observer);

observers_.RemoveObserver(observer);
}

///////////////////////////////////////////////////////////////////////////////

bool AdsServiceImpl::IsBatAdsServiceBound() const {
Expand Down
3 changes: 0 additions & 3 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ class AdsServiceImpl final : public AdsService,

~AdsServiceImpl() override;

void AddObserver(AdsServiceObserver* observer) override;
void RemoveObserver(AdsServiceObserver* observer) override;

private:
using SimpleURLLoaderList =
std::list<std::unique_ptr<network::SimpleURLLoader>>;
Expand Down
3 changes: 0 additions & 3 deletions components/brave_ads/browser/ads_service_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ class AdsServiceMock : public AdsService {

~AdsServiceMock() override;

MOCK_METHOD(void, AddObserver, (AdsServiceObserver * observer));
MOCK_METHOD(void, RemoveObserver, (AdsServiceObserver * observer));

MOCK_METHOD(void,
AddBatAdsObserver,
(mojo::PendingRemote<bat_ads::mojom::BatAdsObserver>
Expand Down
14 changes: 14 additions & 0 deletions components/brave_ads/core/browser/service/ads_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,25 @@

#include <utility>

#include "base/check.h"

namespace brave_ads {

AdsService::AdsService(std::unique_ptr<Delegate> delegate)
: delegate_(std::move(delegate)) {}

AdsService::~AdsService() = default;

void AdsService::AddObserver(AdsServiceObserver* const observer) {
CHECK(observer);

observers_.AddObserver(observer);
}

void AdsService::RemoveObserver(AdsServiceObserver* const observer) {
CHECK(observer);

observers_.RemoveObserver(observer);
}

} // namespace brave_ads
7 changes: 5 additions & 2 deletions components/brave_ads/core/browser/service/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <vector>

#include "base/observer_list.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/core/browser/service/ads_service_observer.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h"
Expand Down Expand Up @@ -64,8 +65,8 @@ class AdsService : public KeyedService {

AdsService::Delegate* delegate() { return delegate_.get(); }

virtual void AddObserver(AdsServiceObserver* observer) = 0;
virtual void RemoveObserver(AdsServiceObserver* observer) = 0;
void AddObserver(AdsServiceObserver* observer);
void RemoveObserver(AdsServiceObserver* observer);

// Returns true if a browser upgrade is required to serve ads.
virtual bool IsBrowserUpgradeRequiredToServeAds() const = 0;
Expand Down Expand Up @@ -297,6 +298,8 @@ class AdsService : public KeyedService {

protected:
std::unique_ptr<Delegate> delegate_;

base::ObserverList<AdsServiceObserver> observers_;
};

} // namespace brave_ads
Expand Down
2 changes: 1 addition & 1 deletion ios/browser/api/ads/brave_ads.mm
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ + (BOOL)isSupportedRegion {

- (BOOL)isServiceRunning {
return adsClientNotifier != nil && adsService != nil &&
adsService->IsRunning();
adsService->IsInitialized();
}

+ (BOOL)shouldAlwaysRunService {
Expand Down
104 changes: 83 additions & 21 deletions ios/browser/brave_ads/ads_service_impl_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

#include <memory>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/sequence_bound.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom-forward.h"
#include "brave/components/brave_ads/core/public/ads_callback.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client_callback.h"
#include "components/keyed_service/core/keyed_service.h"

class PrefService;

Expand All @@ -29,7 +30,7 @@ class Ads;
class AdsClient;
class Database;

class AdsServiceImplIOS : public KeyedService {
class AdsServiceImplIOS : public AdsService {
public:
explicit AdsServiceImplIOS(PrefService* prefs);

Expand All @@ -41,7 +42,7 @@ class AdsServiceImplIOS : public KeyedService {

~AdsServiceImplIOS() override;

bool IsRunning() const;
bool IsInitialized() const;

void InitializeAds(const std::string& storage_path,
std::unique_ptr<AdsClient> ads_client,
Expand All @@ -51,50 +52,111 @@ class AdsServiceImplIOS : public KeyedService {
InitializeCallback callback);
void ShutdownAds(ShutdownCallback callback);

void ClearData(base::OnceClosure callback);

void RunDBTransaction(mojom::DBTransactionInfoPtr mojom_db_transaction,
RunDBTransactionCallback callback);
void MaybeGetNotificationAd(const std::string& placement_id,
MaybeGetNotificationAdCallback callback);
void TriggerNotificationAdEvent(
const std::string& placement_id,
mojom::NotificationAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);

// AdsService:
bool IsBrowserUpgradeRequiredToServeAds() const override;

int64_t GetMaximumNotificationAdsPerHour() const override;

void OnNotificationAdShown(const std::string& placement_id) override;
void OnNotificationAdClosed(const std::string& placement_id,
bool by_user) override;
void OnNotificationAdClicked(const std::string& placement_id) override;

void ClearData(base::OnceClosure callback) override;

void AddBatAdsObserver(mojo::PendingRemote<bat_ads::mojom::BatAdsObserver>
bat_ads_observer_pending_remote) override;

void GetDiagnostics(GetDiagnosticsCallback callback) override;

void GetStatementOfAccounts(GetStatementOfAccountsCallback callback);
void GetStatementOfAccounts(GetStatementOfAccountsCallback callback) override;

void MaybeServeInlineContentAd(const std::string& dimensions,
MaybeServeInlineContentAdCallback callback);
void MaybeServeInlineContentAd(
const std::string& dimensions,
MaybeServeInlineContentAdCallback callback) override;
void TriggerInlineContentAdEvent(
const std::string& placement_id,
const std::string& creative_instance_id,
mojom::InlineContentAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);
TriggerAdEventCallback callback) override;

std::optional<NewTabPageAdInfo> MaybeGetPrefetchedNewTabPageAdForDisplay()
override;
void PrefetchNewTabPageAd() override;
void OnFailedToPrefetchNewTabPageAd(
const std::string& placement_id,
const std::string& creative_instance_id) override;
void TriggerNewTabPageAdEvent(
const std::string& placement_id,
const std::string& creative_instance_id,
mojom::NewTabPageAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);

void MaybeGetNotificationAd(const std::string& placement_id,
MaybeGetNotificationAdCallback callback);
void TriggerNotificationAdEvent(
const std::string& placement_id,
mojom::NotificationAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);
TriggerAdEventCallback callback) override;

void TriggerPromotedContentAdEvent(
const std::string& placement_id,
const std::string& creative_instance_id,
mojom::PromotedContentAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);
TriggerAdEventCallback callback) override;

void MaybeGetSearchResultAd(const std::string& placement_id,
MaybeGetSearchResultAdCallback callback);
MaybeGetSearchResultAdCallback callback) override;
void TriggerSearchResultAdEvent(
mojom::CreativeSearchResultAdInfoPtr mojom_creative_ad,
mojom::SearchResultAdEventType mojom_ad_event_type,
TriggerAdEventCallback callback);
TriggerAdEventCallback callback) override;

void PurgeOrphanedAdEventsForType(
mojom::AdType mojom_ad_type,
PurgeOrphanedAdEventsForTypeCallback callback);
PurgeOrphanedAdEventsForTypeCallback callback) override;

void GetAdHistory(base::Time from_time,
base::Time to_time,
GetAdHistoryForUICallback callback) override;

void ToggleLikeAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;
void ToggleDislikeAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;
void ToggleLikeSegment(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;
void ToggleDislikeSegment(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;
void ToggleSaveAd(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;
void ToggleMarkAdAsInappropriate(mojom::ReactionInfoPtr mojom_reaction,
ToggleReactionCallback callback) override;

void NotifyTabTextContentDidChange(int32_t tab_id,
const std::vector<GURL>& redirect_chain,
const std::string& text) override;
void NotifyTabHtmlContentDidChange(int32_t tab_id,
const std::vector<GURL>& redirect_chain,
const std::string& html) override;
void NotifyTabDidStartPlayingMedia(int32_t tab_id) override;
void NotifyTabDidStopPlayingMedia(int32_t tab_id) override;
void NotifyTabDidChange(int32_t tab_id,
const std::vector<GURL>& redirect_chain,
bool is_new_navigation,
bool is_restoring,
bool is_visible) override;
void NotifyTabDidLoad(int32_t tab_id, int http_status_code) override;
void NotifyDidCloseTab(int32_t tab_id) override;

void NotifyUserGestureEventTriggered(int32_t page_transition_type) override;

void NotifyBrowserDidBecomeActive() override;
void NotifyBrowserDidResignActive() override;

void NotifyDidSolveAdaptiveCaptcha() override;

private:
// KeyedService:
Expand Down
Loading

0 comments on commit 73482e6

Please sign in to comment.