Skip to content

Commit

Permalink
[ads] Followup to "SmartNTT" expose default search engine
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Oct 17, 2024
1 parent f0c6454 commit 6fcf34c
Show file tree
Hide file tree
Showing 25 changed files with 205 additions and 19 deletions.
1 change: 1 addition & 0 deletions browser/brave_ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ source_set("impl") {
"//chrome/browser:browser_public_dependencies",
"//chrome/browser:primitives",
"//chrome/browser/profiles:profile",
"//chrome/browser/search_engines",
"//chrome/browser/ui",
"//chrome/common:chrome_features",
"//chrome/common:chrome_features",
Expand Down
29 changes: 29 additions & 0 deletions browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@

#include <utility>

#include "base/strings/utf_string_conversions.h"
#include "brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h"
#include "brave/browser/brave_ads/application_state/notification_helper/notification_helper.h"
#include "brave/browser/ui/brave_ads/notification_ad.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
#include "build/build_config.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"

#if BUILDFLAG(IS_ANDROID)
#include "brave/browser/notifications/brave_notification_platform_bridge_helper_android.h"
#include "chrome/browser/android/service_tab_launcher.h"
Expand Down Expand Up @@ -132,4 +137,28 @@ bool AdsServiceDelegate::IsFullScreenMode() {
return ::IsFullScreenMode();
}
#endif

base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() const {
const TemplateURLService* const template_url_service =
TemplateURLServiceFactory::GetForProfile(profile_);
if (!template_url_service) {
return {};
}

const TemplateURL* const default_search_provider =
template_url_service->GetDefaultSearchProvider();
if (!template_url_service) {
return {};
}

// TODO(aseren): Is there a way we can return the synced GUID rather than a
// short name?
auto virtual_prefs = base::Value::Dict().Set(
"default_search_provider.synced_guid",
base::UTF16ToUTF8(default_search_provider->short_name()));
VLOG(0) << "FOOBAR.VIRTUAL_PREFS: " << virtual_prefs;

return virtual_prefs;
}

} // namespace brave_ads
4 changes: 4 additions & 0 deletions browser/brave_ads/ads_service_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/values.h"
#include "brave/components/brave_ads/browser/ads_service.h"

class Profile;
Expand Down Expand Up @@ -64,6 +65,9 @@ class AdsServiceDelegate : public AdsService::Delegate {
#else
bool IsFullScreenMode() override;
#endif

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

private:
raw_ptr<Profile> profile_;
raw_ptr<brave_adaptive_captcha::BraveAdaptiveCaptchaService>
Expand Down
6 changes: 6 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class AdsService : public KeyedService {
#else
virtual bool IsFullScreenMode() = 0;
#endif

virtual base::Value::Dict GetVirtualPrefs() const = 0;

protected:
virtual ~Delegate() = default;
};
Expand All @@ -71,6 +74,9 @@ class AdsService : public KeyedService {
virtual void AddObserver(AdsServiceObserver* observer) = 0;
virtual void RemoveObserver(AdsServiceObserver* observer) = 0;

// Called to get the delegate.
virtual Delegate* GetDelegate() = 0;

// Returns true if a browser upgrade is required to serve ads.
virtual bool IsBrowserUpgradeRequiredToServeAds() const = 0;

Expand Down
8 changes: 8 additions & 0 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@ void AdsServiceImpl::AddBatAdsObserver(
}
}

AdsService::Delegate* AdsServiceImpl::GetDelegate() {
return delegate_;
}

bool AdsServiceImpl::IsBrowserUpgradeRequiredToServeAds() const {
return browser_upgrade_required_to_serve_ads_;
}
Expand Down Expand Up @@ -1824,6 +1828,10 @@ void AdsServiceImpl::HasLocalStatePrefPath(
std::move(callback).Run(local_state_->HasPrefPath(path));
}

void AdsServiceImpl::GetVirtualPrefs(GetVirtualPrefsCallback callback) {
std::move(callback).Run(delegate_->GetVirtualPrefs());
}

void AdsServiceImpl::Log(const std::string& file,
const int32_t line,
const int32_t verbose_level,
Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class AdsServiceImpl final : public AdsService,
void AddBatAdsObserver(mojo::PendingRemote<bat_ads::mojom::BatAdsObserver>
bat_ads_observer_pending_remote) override;

Delegate* GetDelegate() override;

bool IsBrowserUpgradeRequiredToServeAds() const override;

int64_t GetMaximumNotificationAdsPerHour() const override;
Expand Down Expand Up @@ -386,6 +388,8 @@ class AdsServiceImpl final : public AdsService,
void HasLocalStatePrefPath(const std::string& path,
HasLocalStatePrefPathCallback callback) override;

void GetVirtualPrefs(GetVirtualPrefsCallback callback) override;

void Log(const std::string& file,
int32_t line,
int32_t verbose_level,
Expand Down
2 changes: 2 additions & 0 deletions components/brave_ads/browser/ads_service_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class AdsServiceMock : public AdsService {
MOCK_METHOD(void, AddObserver, (AdsServiceObserver * observer));
MOCK_METHOD(void, RemoveObserver, (AdsServiceObserver * observer));

MOCK_METHOD(AdsService::Delegate*, GetDelegate, ());

MOCK_METHOD(void,
AddBatAdsObserver,
(mojo::PendingRemote<bat_ads::mojom::BatAdsObserver>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class AdsClientMock : public AdsClient {
MOCK_METHOD(void, ClearLocalStatePref, (const std::string& path));
MOCK_METHOD(bool, HasLocalStatePrefPath, (const std::string& path), (const));

MOCK_METHOD(base::Value::Dict, GetVirtualPrefs, (), (const));

MOCK_METHOD(void,
Log,
(const char* file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

#include "brave/components/brave_ads/core/internal/ads_client/ads_client_pref_provider.h"

#include <optional>

#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/public/ads_client/ads_client.h"

namespace brave_ads {

AdsClientPrefProvider::AdsClientPrefProvider() = default;
AdsClientPrefProvider::AdsClientPrefProvider() {
virtual_prefs_ = GetAdsClient()->GetVirtualPrefs();
}

AdsClientPrefProvider::~AdsClientPrefProvider() = default;

Expand Down Expand Up @@ -54,4 +58,36 @@ bool AdsClientPrefProvider::HasLocalStatePrefPath(
return GetAdsClient()->HasLocalStatePrefPath(pref_path);
}

std::optional<base::Value> AdsClientPrefProvider::GetVirtualPref(
const std::string& virtual_pref_path) const {
if (virtual_pref_path.starts_with("[virtual]:")) {
const std::string pref_path =
virtual_pref_path.substr(/*pos=*/std::size("[virtual]:"));

if (const base::Value* const value = virtual_prefs_.Find(pref_path)) {
return value->Clone();
}
}

if (virtual_pref_path.starts_with("[virtual_default]:")) {
const std::string pref_path =
virtual_pref_path.substr(/*pos=*/std::size("[virtual_default]:"));

if (HasProfilePrefPath(pref_path)) {
return GetProfilePref(pref_path);
}

if (HasLocalStatePrefPath(pref_path)) {
return GetLocalStatePref(pref_path);
}

if (const base::Value* const value = virtual_prefs_.Find(pref_path)) {
return value->Clone();
}
}

// Unknown virtual pref path.
return std::nullopt;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class AdsClientPrefProvider : public PrefProviderInterface {
std::optional<base::Value> GetLocalStatePref(
const std::string& pref_path) const override;
bool HasLocalStatePrefPath(const std::string& pref_path) const override;

std::optional<base::Value> GetVirtualPref(
const std::string& virtual_pref_path) const override;

private:
base::Value::Dict virtual_prefs_;
};

} // namespace brave_ads
Expand Down
41 changes: 39 additions & 2 deletions components/brave_ads/core/internal/prefs/pref_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@

#include "brave/components/brave_ads/core/public/prefs/pref_provider.h"

#include <utility>

#include "components/prefs/pref_service.h"

namespace brave_ads {

PrefProvider::PrefProvider(PrefService* const profile_prefs,
PrefService* const local_state_prefs)
: profile_prefs_(profile_prefs), local_state_prefs_(local_state_prefs) {
PrefService* const local_state_prefs,
base::Value::Dict virtual_prefs)
: profile_prefs_(profile_prefs),
local_state_prefs_(local_state_prefs),
virtual_prefs_(std::move(virtual_prefs)) {
CHECK(profile_prefs_);
CHECK(local_state_prefs_);
}
Expand Down Expand Up @@ -46,4 +51,36 @@ bool PrefProvider::HasLocalStatePrefPath(const std::string& pref_path) const {
return local_state_prefs_->HasPrefPath(pref_path);
}

std::optional<base::Value> PrefProvider::GetVirtualPref(
const std::string& virtual_pref_path) const {
if (virtual_pref_path.starts_with("[virtual]:")) {
const std::string pref_path =
virtual_pref_path.substr(/*pos=*/std::size("[virtual]:"));

if (const base::Value* const value = virtual_prefs_.Find(pref_path)) {
return value->Clone();
}
}

if (virtual_pref_path.starts_with("[virtual_default]:")) {
const std::string pref_path =
virtual_pref_path.substr(/*pos=*/std::size("[virtual_default]:"));

if (HasProfilePrefPath(pref_path)) {
return GetProfilePref(pref_path);
}

if (HasLocalStatePrefPath(pref_path)) {
return GetLocalStatePref(pref_path);
}

if (const base::Value* const value = virtual_prefs_.Find(pref_path)) {
return value->Clone();
}
}

// Unknown virtual pref path.
return std::nullopt;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <vector>

#include "base/functional/bind.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_pref_provider.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_util.h"
#include "brave/components/brave_ads/core/internal/common/logging_util.h"
#include "brave/components/brave_ads/core/internal/serving/eligible_ads/eligible_ads_feature.h"
Expand All @@ -26,19 +25,6 @@

namespace brave_ads {

namespace {

void ApplyConditionMatcher(CreativeNewTabPageAdList& creative_ads) {
const AdsClientPrefProvider pref_provider;
std::erase_if(creative_ads, [&pref_provider](const auto& creative_ad) {
return creative_ad.wallpapers.size() != 1 ||
!MatchConditions(&pref_provider,
creative_ad.wallpapers[0].condition_matchers);
});
}

} // namespace

EligibleNewTabPageAdsV2::EligibleNewTabPageAdsV2(
const SubdivisionTargeting& subdivision_targeting,
const AntiTargetingResource& anti_targeting_resource)
Expand Down Expand Up @@ -152,6 +138,15 @@ void EligibleNewTabPageAdsV2::FilterAndMaybePredictCreativeAd(
std::move(callback).Run(/*eligible_ads=*/{});
}

void EligibleNewTabPageAdsV2::ApplyConditionMatcher(
CreativeNewTabPageAdList& creative_ads) {
std::erase_if(creative_ads, [this](const auto& creative_ad) {
return creative_ad.wallpapers.size() != 1 ||
!MatchConditions(&pref_provider_,
creative_ad.wallpapers[0].condition_matchers);
});
}

void EligibleNewTabPageAdsV2::FilterIneligibleCreativeAds(
CreativeNewTabPageAdList& creative_ads,
const AdEventList& ad_events,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_SERVING_ELIGIBLE_ADS_PIPELINES_NEW_TAB_PAGE_ADS_ELIGIBLE_NEW_TAB_PAGE_ADS_V2_H_

#include "base/memory/weak_ptr.h"
#include "brave/components/brave_ads/core/internal/ads_client/ads_client_pref_provider.h"
#include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ad_info.h"
#include "brave/components/brave_ads/core/internal/creatives/new_tab_page_ads/creative_new_tab_page_ads_database_table.h"
#include "brave/components/brave_ads/core/internal/segments/segment_alias.h"
Expand Down Expand Up @@ -51,6 +52,8 @@ class EligibleNewTabPageAdsV2 final : public EligibleNewTabPageAdsBase {
const SegmentList& segments,
const CreativeNewTabPageAdList& creative_ads);

void ApplyConditionMatcher(CreativeNewTabPageAdList& creative_ads);

void FilterAndMaybePredictCreativeAd(
const UserModelInfo& user_model,
const CreativeNewTabPageAdList& creative_ads,
Expand All @@ -65,6 +68,8 @@ class EligibleNewTabPageAdsV2 final : public EligibleNewTabPageAdsBase {

const database::table::AdEvents ad_events_database_table_;

const AdsClientPrefProvider pref_provider_;

base::WeakPtrFactory<EligibleNewTabPageAdsV2> weak_factory_{this};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ std::optional<base::Value> MaybeGetRootPrefValue(
const std::string& pref_path) {
CHECK(pref_provider);

if (std::optional<base::Value> pref_value =
pref_provider->GetVirtualPref(pref_path)) {
return pref_value;
}

if (std::optional<base::Value> pref_value =
pref_provider->GetProfilePref(pref_path)) {
return pref_value;
Expand Down
3 changes: 3 additions & 0 deletions components/brave_ads/core/public/ads_client/ads_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class ADS_EXPORT AdsClient {
// Close the notification ad for the specified `placement_id`.
virtual void CloseNotificationAd(const std::string& placement_id) = 0;

// Returns a default search engine.
virtual base::Value::Dict GetVirtualPrefs() const = 0;

// Cache an ad event for the specified instance `id`, `mojom_ad_type`,
// `mojom_confirmation_type` and `time`.
virtual void CacheAdEventForInstanceId(
Expand Down
7 changes: 6 additions & 1 deletion components/brave_ads/core/public/prefs/pref_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ namespace brave_ads {
class PrefProvider : public PrefProviderInterface {
public:
explicit PrefProvider(PrefService* profile_prefs,
PrefService* local_state_prefs);
PrefService* local_state_prefs,
base::Value::Dict virtual_prefs);

PrefProvider(const PrefProvider& other) = delete;
PrefProvider& operator=(const PrefProvider& other) = delete;
Expand All @@ -37,9 +38,13 @@ class PrefProvider : public PrefProviderInterface {
const std::string& pref_path) const override;
bool HasLocalStatePrefPath(const std::string& pref_path) const override;

std::optional<base::Value> GetVirtualPref(
const std::string& virtual_pref_path) const override;

private:
const raw_ptr<PrefService> profile_prefs_ = nullptr;
const raw_ptr<PrefService> local_state_prefs_ = nullptr;
const base::Value::Dict virtual_prefs_;
};

} // namespace brave_ads
Expand Down
Loading

0 comments on commit 6fcf34c

Please sign in to comment.