Skip to content

Commit 37b3c18

Browse files
authored
[ads] Add browser|version|* and operating_system|is_mobile_platform condition matcher virtual prefs (#29106)
1 parent ef0f2a4 commit 37b3c18

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

browser/brave_ads/ads_service_delegate.cc

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
#include <cstddef>
99
#include <utility>
10+
#include <vector>
1011

1112
#include "base/check_deref.h"
1213
#include "base/json/json_reader.h"
1314
#include "base/strings/utf_string_conversions.h"
15+
#include "base/version.h"
1416
#include "base/version_info/channel.h"
1517
#include "base/version_info/version_info.h"
1618
#include "brave/browser/brave_ads/ad_units/notification_ad/notification_ad_platform_bridge.h"
@@ -56,32 +58,65 @@ constexpr char kSkuOrderLastPaidAtKey[] = "last_paid_at";
5658
constexpr char kSkuOrderStatusKey[] = "status";
5759
constexpr char kNotificationAdUrlPrefix[] = "https://www.brave.com/ads/?";
5860

61+
int GetVersionComponent(size_t index) {
62+
const base::Version& version = version_info::GetVersion();
63+
const std::vector<uint32_t>& version_components = version.components();
64+
return index < version_components.size()
65+
? static_cast<int>(version_components[index])
66+
: 0;
67+
}
68+
69+
int GetMajorVersion() {
70+
return GetVersionComponent(0);
71+
}
72+
73+
int GetMinorVersion() {
74+
return GetVersionComponent(1);
75+
}
76+
77+
int GetBuildVersion() {
78+
return GetVersionComponent(2);
79+
}
80+
81+
int GetPatchVersion() {
82+
return GetVersionComponent(3);
83+
}
84+
85+
bool IsMobilePlatform() {
86+
std::string_view operating_system_name = version_info::GetOSType();
87+
return operating_system_name == "Android" || operating_system_name == "iOS";
88+
}
89+
5990
std::string StripSkuEnvironmentPrefix(const std::string& environment) {
6091
const size_t pos = environment.find(':');
6192
return environment.substr(pos + 1);
6293
}
6394

6495
std::string NormalizeSkuStatus(const std::string& status) {
96+
// Normalize the status field to use consistent (US) spelling, as the JSON
97+
// source localizes it (e.g., "cancelled" vs "canceled").
6598
return status == "cancelled" ? "canceled" : status;
6699
}
67100

68101
base::Value::Dict ParseSkuOrder(const base::Value::Dict& dict) {
69102
base::Value::Dict order;
70103

71-
if (const auto* const created_at = dict.FindString(kSkuOrderCreatedAtKey)) {
104+
if (const std::string* const created_at =
105+
dict.FindString(kSkuOrderCreatedAtKey)) {
72106
order.Set(kSkuOrderCreatedAtKey, *created_at);
73107
}
74108

75-
if (const auto* const expires_at = dict.FindString(kSkuOrderExpiresAtKey)) {
109+
if (const std::string* const expires_at =
110+
dict.FindString(kSkuOrderExpiresAtKey)) {
76111
order.Set(kSkuOrderExpiresAtKey, *expires_at);
77112
}
78113

79-
if (const auto* const last_paid_at =
114+
if (const std::string* const last_paid_at =
80115
dict.FindString(kSkuOrderLastPaidAtKey)) {
81116
order.Set(kSkuOrderLastPaidAtKey, *last_paid_at);
82117
}
83118

84-
if (const auto* const status = dict.FindString(kSkuOrderStatusKey)) {
119+
if (const std::string* const status = dict.FindString(kSkuOrderStatusKey)) {
85120
const std::string normalized_status = NormalizeSkuStatus(*status);
86121
order.Set(kSkuOrderStatusKey, normalized_status);
87122
}
@@ -99,7 +134,7 @@ base::Value::Dict ParseSkuOrders(const base::Value::Dict& dict) {
99134
}
100135

101136
const std::string* const location = order->FindString(kSkuOrderLocationKey);
102-
if (!location) {
137+
if (!location || location->empty()) {
103138
continue;
104139
}
105140

@@ -150,7 +185,8 @@ base::Value::Dict AdsServiceDelegate::GetSkus() const {
150185
continue;
151186
}
152187

153-
// Parse the SKUs JSON because it is stored as a string in local state.
188+
// Deserialize the SKUs data from a JSON string stored in local state into a
189+
// dictionary object for further processing.
154190
std::optional<base::Value::Dict> sku_state =
155191
base::JSONReader::ReadDict(value.GetString());
156192
if (!sku_state) {
@@ -297,12 +333,17 @@ base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() {
297333
base::Value::Dict()
298334
.Set("build_channel",
299335
version_info::GetChannelString(chrome::GetChannel()))
300-
.Set("version", version_info::GetVersionNumber()))
336+
.Set("version", version_info::GetVersionNumber())
337+
.Set("major_version", GetMajorVersion())
338+
.Set("minor_version", GetMinorVersion())
339+
.Set("build_version", GetBuildVersion())
340+
.Set("patch_version", GetPatchVersion()))
301341
.Set("[virtual]:operating_system",
302342
base::Value::Dict()
303343
.Set("locale", base::Value::Dict()
304344
.Set("language", CurrentLanguageCode())
305345
.Set("region", CurrentCountryCode()))
346+
.Set("is_mobile_platform", IsMobilePlatform())
306347
.Set("name", version_info::GetOSType()))
307348
.Set(
308349
"[virtual]:search_engine",

components/brave_ads/core/internal/common/locale/locale_util.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ namespace brave_ads {
1515
namespace {
1616

1717
std::string& MutableCurrentLanguageCode() {
18+
// ISO 639-1 language code (e.g. "en", "fr", "de").
1819
static base::NoDestructor<std::string> language_code(base::ToLowerASCII(
1920
MaybeGetLanguageCodeString().value_or(kDefaultLanguageCode)));
2021
return *language_code;
2122
}
2223

2324
std::string& MutableCurrentCountryCode() {
25+
// ISO 3166-1 alpha-2 country code (e.g. "US", "FR", "DE").
2426
static base::NoDestructor<std::string> country_code([]() {
2527
const country_codes::CountryId country_id =
2628
country_codes::GetCurrentCountryID();

0 commit comments

Comments
 (0)