Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Oct 18, 2024
1 parent 6fcf34c commit 12fb10b
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 89 deletions.
10 changes: 3 additions & 7 deletions browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#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/search_engines_pref_names.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"

Expand Down Expand Up @@ -151,14 +152,9 @@ base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() const {
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",
return base::Value::Dict().Set(
prefs::kDefaultSearchProviderGUID,
base::UTF16ToUTF8(default_search_provider->short_name()));
VLOG(0) << "FOOBAR.VIRTUAL_PREFS: " << virtual_prefs;

return virtual_prefs;
}

} // namespace brave_ads
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@

#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() {
virtual_prefs_ = GetAdsClient()->GetVirtualPrefs();
}
AdsClientPrefProvider::AdsClientPrefProvider() = default;

AdsClientPrefProvider::~AdsClientPrefProvider() = default;

Expand Down Expand Up @@ -60,33 +56,17 @@ bool AdsClientPrefProvider::HasLocalStatePrefPath(

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]:")) {
if (virtual_pref_path.starts_with(kVirtualPrefPathPrefix)) {
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);
}
virtual_pref_path.substr(/*pos=*/std::strlen(kVirtualPrefPathPrefix));

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

// Unknown virtual pref path.
// The virtual preference does not exist.
return std::nullopt;
}

Expand Down
23 changes: 3 additions & 20 deletions components/brave_ads/core/internal/prefs/pref_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,16 @@ bool PrefProvider::HasLocalStatePrefPath(const std::string& pref_path) const {

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

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.
// The virtual preference does not exist.
return std::nullopt;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ 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 (pref_path.starts_with(kVirtualPrefPathPrefix)) {
return pref_provider->GetVirtualPref(pref_path);
}

if (std::optional<base::Value> pref_value =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,33 @@ TEST_F(BraveAdsNewTabPageAdServingConditionMatcherUtilInternalTest,
EXPECT_FALSE(MatchRegex("foo.baz.bar", "bar.*.foo"));
}

TEST_F(BraveAdsNewTabPageAdServingConditionMatcherUtilInternalTest,
GetVirtualPrefValue) {
// Arrange
test::RegisterProfileIntegerPref("matrix", 303);

ON_CALL(ads_client_mock_, GetVirtualPrefs).WillByDefault([]() {
return base::Value::Dict().Set("matrix", 101);
});

// Act & Assert
EXPECT_EQ(base::Value(101),
MaybeGetPrefValue(&pref_provider_, "[virtual]:matrix"));
}

TEST_F(BraveAdsNewTabPageAdServingConditionMatcherUtilInternalTest,
DoNotGetUnknownVirtualPrefValue) {
// Arrange
test::RegisterProfileIntegerPref("matrix", 303);

ON_CALL(ads_client_mock_, GetVirtualPrefs).WillByDefault([]() {
return base::Value::Dict().Set("inverse.matrices", 101);
});

// Act & Assert
EXPECT_FALSE(MaybeGetPrefValue(&pref_provider_, "[virtual]:matrix"));
}

TEST_F(BraveAdsNewTabPageAdServingConditionMatcherUtilInternalTest,
GetBooleanProfilePrefValue) {
// Arrange
Expand Down
6 changes: 3 additions & 3 deletions components/brave_ads/core/public/ads_client/ads_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ 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 Expand Up @@ -164,6 +161,9 @@ class ADS_EXPORT AdsClient {
// preference `path`.
virtual bool HasLocalStatePrefPath(const std::string& path) const = 0;

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

// Log a `message` to `file` and the console log with `line` and
// `verbose_level`.
virtual void Log(const char* file,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace brave_ads {

inline constexpr char kVirtualPrefPathPrefix[] = "[virtual]:";

class PrefProviderInterface {
public:
virtual ~PrefProviderInterface() = default;
Expand Down
19 changes: 10 additions & 9 deletions components/ntp_background_images/browser/view_counter_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ ViewCounterService::GetNextBrandedWallpaperWhichMatchesConditions() {
const auto initial_branded_wallpaper_index =
model_.GetCurrentBrandedImageIndex();

base::Value::Dict virtual_prefs;
if (ads_service_) {
const brave_ads::AdsService::Delegate* const delegate =
ads_service_->GetDelegate();
CHECK(delegate);
virtual_prefs = delegate->GetVirtualPrefs();
}
const brave_ads::PrefProvider pref_provider(prefs_, local_state_prefs_,
std::move(virtual_prefs));

do {
std::optional<base::Value::Dict> branded_wallpaper =
GetCurrentBrandedWallpaperFromModel();
Expand All @@ -289,15 +299,6 @@ ViewCounterService::GetNextBrandedWallpaperWhichMatchesConditions() {
return branded_wallpaper;
}

base::Value::Dict virtual_prefs;
if (ads_service_) {
brave_ads::AdsService::Delegate* delegate = ads_service_->GetDelegate();
CHECK(delegate);
virtual_prefs = delegate->GetVirtualPrefs();
}

const brave_ads::PrefProvider pref_provider(prefs_, local_state_prefs_,
std::move(virtual_prefs));
if (brave_ads::MatchConditions(&pref_provider, *condition_matchers)) {
// The branded wallpaper matches the conditions, so we can return it.
return branded_wallpaper;
Expand Down
22 changes: 12 additions & 10 deletions components/services/bat_ads/bat_ads_client_mojo_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ void BatAdsClientMojoBridge::CloseNotificationAd(
}
}

base::Value::Dict BatAdsClientMojoBridge::GetVirtualPrefs() const {
if (!bat_ads_client_associated_remote_.is_bound()) {
return {};
}

base::Value::Dict virtual_prefs;
bat_ads_client_associated_remote_->GetVirtualPrefs(&virtual_prefs);
return virtual_prefs;
}

void BatAdsClientMojoBridge::CacheAdEventForInstanceId(
const std::string& id,
const brave_ads::mojom::AdType mojom_ad_type,
Expand Down Expand Up @@ -405,6 +395,18 @@ bool BatAdsClientMojoBridge::HasLocalStatePrefPath(
return value;
}

base::Value::Dict BatAdsClientMojoBridge::GetVirtualPrefs() const {
if (!bat_ads_client_associated_remote_.is_bound()) {
return {};
}

base::Value::Dict virtual_prefs;
bat_ads_client_associated_remote_->GetVirtualPrefs(&virtual_prefs);
return virtual_prefs;
}

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

std::optional<base::Value> BatAdsClientMojoBridge::CachedProfilePrefValue(
const std::string& path) const {
if (!cached_profile_prefs_.contains(path)) {
Expand Down
4 changes: 2 additions & 2 deletions components/services/bat_ads/bat_ads_client_mojo_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ class BatAdsClientMojoBridge : public brave_ads::AdsClient {
void ShowNotificationAd(const brave_ads::NotificationAdInfo& ad) override;
void CloseNotificationAd(const std::string& placement_id) override;

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

void CacheAdEventForInstanceId(
const std::string& id,
brave_ads::mojom::AdType mojom_ad_type,
Expand Down Expand Up @@ -115,6 +113,8 @@ class BatAdsClientMojoBridge : public brave_ads::AdsClient {
void ClearLocalStatePref(const std::string& path) override;
bool HasLocalStatePrefPath(const std::string& path) const override;

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

void Log(const char* file,
int line,
int verbose_level,
Expand Down
2 changes: 1 addition & 1 deletion ios/browser/api/ads/ads_client_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
- (void)notifyPendingObservers;
- (bool)isNetworkConnectionAvailable;
- (bool)canShowNotificationAds;
- (base::Value::Dict)getVirtualPrefs;
- (void)loadResourceComponent:(const std::string&)id
version:(const int)version
callback:(brave_ads::LoadFileCallback)callback;
Expand Down Expand Up @@ -73,6 +72,7 @@
- (std::optional<base::Value>)getLocalStatePref:(const std::string&)path;
- (void)clearLocalStatePref:(const std::string&)path;
- (bool)hasLocalStatePrefPath:(const std::string&)path;
- (base::Value::Dict)getVirtualPrefs;
- (void)recordP2AEvents:(const std::vector<std::string>&)events;

@end
Expand Down
8 changes: 4 additions & 4 deletions ios/browser/api/ads/ads_client_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@
time:time];
}

base::Value::Dict AdsClientIOS::GetVirtualPrefs() const {
return [bridge_ getVirtualPrefs];
}

std::vector<base::Time> AdsClientIOS::GetCachedAdEvents(
const brave_ads::mojom::AdType mojom_ad_type,
const brave_ads::mojom::ConfirmationType mojom_confirmation_type) const {
Expand Down Expand Up @@ -189,6 +185,10 @@
return [bridge_ hasLocalStatePrefPath:path];
}

base::Value::Dict AdsClientIOS::GetVirtualPrefs() const {
return [bridge_ getVirtualPrefs];
}

void AdsClientIOS::RecordP2AEvents(const std::vector<std::string>& events) {
[bridge_ recordP2AEvents:events];
}
9 changes: 5 additions & 4 deletions ios/browser/api/ads/brave_ads.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1288,10 +1288,6 @@ - (void)closeNotificationAd:(const std::string&)placement_id {
closeNotificationAd:base::SysUTF8ToNSString(placement_id)];
}

- (base::Value::Dict)getVirtualPrefs {
return {};
}

- (void)cacheAdEventForInstanceId:(const std::string&)id
adType:(const brave_ads::mojom::AdType)mojom_ad_type
confirmationType:(const brave_ads::mojom::ConfirmationType)
Expand Down Expand Up @@ -1499,6 +1495,11 @@ - (bool)hasLocalStatePrefPath:(const std::string&)path {
return self.localStatePrefService->HasPrefPath(path);
}

- (base::Value::Dict)getVirtualPrefs {
// Intentionally empty.
return {};
}

- (void)log:(const char*)file
line:(const int)line
verboseLevel:(const int)verbose_level
Expand Down

0 comments on commit 12fb10b

Please sign in to comment.