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

Unify AdsService with iOS #26639

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions browser/brave_ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ source_set("impl") {
"//chrome/common:constants",
"//components/keyed_service/content",
"//ui/gfx",
"//ui/message_center/public/cpp",
]

if (toolkit_views) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/ui/brave_ads/notification_ad_delegate.h"
#include "brave/browser/ui/brave_ads/notification_ad_popup_handler.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
Expand Down
88 changes: 58 additions & 30 deletions browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/channel_info.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"
#include "ui/message_center/public/cpp/notifier_id.h"

#if BUILDFLAG(IS_ANDROID)
#include "brave/browser/notifications/brave_notification_platform_bridge_helper_android.h"
Expand All @@ -47,6 +50,7 @@ constexpr char kSkuOrderCreatedAtKey[] = "created_at";
constexpr char kSkuOrderExpiresAtKey[] = "expires_at";
constexpr char kSkuOrderLastPaidAtKey[] = "last_paid_at";
constexpr char kSkuOrderStatusKey[] = "status";
constexpr char kNotificationAdUrlPrefix[] = "https://www.brave.com/ads/?";

std::string StripSkuEnvironmentPrefix(const std::string& environment) {
const size_t pos = environment.find(':');
Expand Down Expand Up @@ -221,46 +225,70 @@ void AdsServiceDelegate::SnoozeScheduledCaptcha() {
adaptive_captcha_service_->SnoozeScheduledCaptcha();
}

void AdsServiceDelegate::Display(
const message_center::Notification& notification) {
// We cannot store a raw_ptr to NotificationDisplayService due to upstream
// browser tests changes NotificationDisplayService instance during test run
// which leads to dangling pointer errors.
GetNotificationDisplayService()->Display(NotificationHandler::Type::BRAVE_ADS,
notification, nullptr);
}

void AdsServiceDelegate::Close(const std::string& notification_id) {
// We cannot store a raw_ptr to NotificationDisplayService due to upstream
// browser tests changes NotificationDisplayService instance during test run
// which leads to dangling pointer errors.
GetNotificationDisplayService()->Close(NotificationHandler::Type::BRAVE_ADS,
notification_id);
}

void AdsServiceDelegate::ShowNotificationAd(const std::string& id,
const std::u16string& title,
const std::u16string& body) {
notification_ad_platform_bridge_->ShowNotificationAd(
NotificationAd(id, title, body, nullptr));
}
const std::u16string& body,
const bool is_custom) {
if (is_custom) {
notification_ad_platform_bridge_->ShowNotificationAd(
NotificationAd(id, title, body, nullptr));
} else {
message_center::RichNotificationData notification_data;
notification_data.context_message = u" ";

const GURL url = GURL(kNotificationAdUrlPrefix + id);

const std::unique_ptr<message_center::Notification> notification =
std::make_unique<message_center::Notification>(
message_center::NOTIFICATION_TYPE_SIMPLE, id, title, body,
ui::ImageModel(), std::u16string(), url,
message_center::NotifierId(
message_center::NotifierType::SYSTEM_COMPONENT,
"service.ads_service"),
notification_data, nullptr);

#if !BUILDFLAG(IS_MAC) || defined(OFFICIAL_BUILD)
// `set_never_timeout` uses an XPC service which requires signing so for now
// we don't set this for macos dev builds
notification->set_never_timeout(true);
#endif

void AdsServiceDelegate::CloseNotificationAd(const std::string& id) {
notification_ad_platform_bridge_->CloseNotificationAd(id);
// We cannot store a raw_ptr to NotificationDisplayService due to upstream
// browser tests changes NotificationDisplayService instance during test run
// which leads to dangling pointer errors.
GetNotificationDisplayService()->Display(
NotificationHandler::Type::BRAVE_ADS, *notification, nullptr);
}
}

void AdsServiceDelegate::CloseNotificationAd(const std::string& id,
const bool is_custom) {
if (is_custom) {
notification_ad_platform_bridge_->CloseNotificationAd(id);
} else {
#if BUILDFLAG(IS_ANDROID)
void AdsServiceDelegate::MaybeRegenerateNotification(
const std::string& placement_id,
const GURL& url) {
BraveNotificationPlatformBridgeHelperAndroid::MaybeRegenerateNotification(
placement_id, url);
const std::string brave_ads_url_prefix = kNotificationAdUrlPrefix;
const GURL url =
GURL(brave_ads_url_prefix.substr(0, brave_ads_url_prefix.size() - 1));
BraveNotificationPlatformBridgeHelperAndroid::MaybeRegenerateNotification(
id, url);
#endif

// We cannot store a raw_ptr to NotificationDisplayService due to upstream
// browser tests changes NotificationDisplayService instance during test run
// which leads to dangling pointer errors.
GetNotificationDisplayService()->Close(NotificationHandler::Type::BRAVE_ADS,
id);
}
}
#else

bool AdsServiceDelegate::IsFullScreenMode() {
#if !BUILDFLAG(IS_ANDROID)
return ::IsFullScreenMode();
}
#else
return true;
#endif
}

base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() {
return base::Value::Dict()
Expand Down
14 changes: 4 additions & 10 deletions browser/brave_ads/ads_service_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/values.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/search_engine_choice/search_engine_choice_service.h"

Expand Down Expand Up @@ -61,19 +61,13 @@ class AdsServiceDelegate : public AdsService::Delegate {
const std::string& captcha_id) override;
void ClearScheduledCaptcha() override;
void SnoozeScheduledCaptcha() override;
void Display(const message_center::Notification& notification) override;
void Close(const std::string& notification_id) override;
void ShowNotificationAd(const std::string& id,
const std::u16string& title,
const std::u16string& body) override;
void CloseNotificationAd(const std::string& id) override;
const std::u16string& body,
bool is_custom) override;
void CloseNotificationAd(const std::string& id, bool is_custom) override;
void OpenNewTabWithUrl(const GURL& url) override;
#if BUILDFLAG(IS_ANDROID)
void MaybeRegenerateNotification(const std::string& notification_id,
const GURL& service_worker_scope) override;
#else
bool IsFullScreenMode() override;
#endif

base::Value::Dict GetVirtualPrefs() override;

Expand Down
2 changes: 1 addition & 1 deletion browser/brave_ads/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/brave_rewards/rewards_util.h"
#include "brave/common/brave_channel_info.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_impl.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/notifications/notification_display_service.h"
Expand Down
5 changes: 3 additions & 2 deletions browser/brave_ads/android/brave_ads_native_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
#include <string>

#include "base/android/jni_string.h"
#include "base/functional/callback_helpers.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_ads/android/jni_headers/BraveAdsNativeHelper_jni.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/ads_util.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ void JNI_BraveAdsNativeHelper_ClearData(
return;
}

ads_service->ClearData();
ads_service->ClearData(/*intentional*/ base::DoNothing());
}

// static
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_handler.h"
#include "brave/components/brave_ads/content/browser/creatives/search_result_ad/creative_search_result_ad_url_placement_id_extractor.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/mojom/brave_ads.mojom.h"
#include "brave/components/brave_ads/core/public/ads_feature.h"
#include "brave/components/brave_rewards/common/pref_names.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/ads_service_mock.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_feature.h"
#include "brave/components/brave_rewards/common/pref_names.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_ads/tabs/ads_tab_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "base/containers/contains.h"
#include "base/strings/stringprintf.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/brave_ads/test_ads_service_waiter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "brave/browser/brave_ads/test_ads_service_waiter.h"

#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"

namespace brave_ads {

Expand Down
2 changes: 1 addition & 1 deletion browser/brave_ads/test_ads_service_waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "brave/components/brave_ads/browser/ads_service_observer.h"
#include "brave/components/brave_ads/core/browser/service/ads_service_observer.h"

namespace brave_ads {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "brave/build/android/jni_headers/BraveRewardsNativeWorker_jni.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
#include "brave/components/brave_adaptive_captcha/server_util.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"
#include "brave/components/brave_rewards/browser/rewards_p3a.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "brave/browser/ui/brave_rewards/tip_panel_coordinator.h"
#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_rewards/browser/rewards_p3a.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/common/pref_names.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/notifications/ads_notification_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <optional>

#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "build/build_config.h"
#include "url/gurl.h"

Expand Down
2 changes: 1 addition & 1 deletion browser/ntp_background/view_counter_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/ntp_background/ntp_p3a_helper_impl.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/ads_util.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/brave_rewards/rewards_page_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "base/json/json_writer.h"
#include "base/scoped_observation.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/ads_util.h"
#include "brave/components/brave_ads/core/public/history/ad_history_feature.h"
#include "brave/components/brave_ads/core/public/history/ad_history_item_value_util.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/brave_rewards_internals_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_ads/core/public/prefs/pref_names.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/common/mojom/rewards.mojom.h"
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/browser/brave_rewards/rewards_service_factory.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/brave_ads/browser/ads_service.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/ad_units/ad_type.h"
#include "brave/components/brave_ads/core/public/ads_util.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "mojo/public/cpp/bindings/receiver.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include "brave/browser/ui/webui/settings/brave_clear_browsing_data_handler.h"

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "brave/browser/brave_ads/ads_service_factory.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/core/browser/service/ads_service.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
Expand Down Expand Up @@ -60,7 +61,7 @@ void BraveClearBrowsingDataHandler::HandleClearBraveAdsData(
const base::Value::List& /*args*/) {
if (auto* ads_service =
brave_ads::AdsServiceFactory::GetForProfile(profile_)) {
ads_service->ClearData();
ads_service->ClearData(/*intentional*/ base::DoNothing());
}
}

Expand Down
4 changes: 0 additions & 4 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ static_library("browser") {
"ad_units/notification_ad/custom_notification_ad_constants.h",
"ad_units/notification_ad/custom_notification_ad_feature.cc",
"ad_units/notification_ad/custom_notification_ad_feature.h",
"ads_service.cc",
"ads_service.h",
"ads_service_observer.h",
"analytics/p2a/p2a.cc",
"analytics/p2a/p2a.h",
"analytics/p2a/p2a_constants.h",
Expand Down Expand Up @@ -84,7 +81,6 @@ static_library("browser") {
"//services/network/public/cpp",
"//services/network/public/mojom",
"//ui/base",
"//ui/message_center/public/cpp",
]

public_deps += [
Expand Down
Loading
Loading