Skip to content

Commit

Permalink
[ads] Add SmartNTT virtual skus pref
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Oct 29, 2024
1 parent 5cce3da commit 2c363fd
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 10 deletions.
1 change: 1 addition & 0 deletions browser/brave_ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ source_set("impl") {
"//brave/components/brave_ads/browser/application_state",
"//brave/components/l10n/common",
"//brave/components/p3a_utils",
"//brave/components/skus/browser",
"//chrome/browser:browser_process",
"//chrome/browser:browser_public_dependencies",
"//chrome/browser:primitives",
Expand Down
86 changes: 76 additions & 10 deletions browser/brave_ads/ads_service_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <utility>

#include "base/json/json_reader.h"
#include "base/strings/utf_string_conversions.h"
#include "base/version_info/channel.h"
#include "base/version_info/version_info.h"
Expand All @@ -15,6 +16,7 @@
#include "brave/browser/ui/brave_ads/notification_ad.h"
#include "brave/components/brave_adaptive_captcha/brave_adaptive_captcha_service.h"
#include "brave/components/l10n/common/locale_util.h"
#include "brave/components/skus/browser/pref_names.h"
#include "build/build_config.h"
#include "chrome/browser/notifications/notification_display_service.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -65,6 +67,67 @@ std::string AdsServiceDelegate::GetDefaultSearchEngineName() {
return base::UTF16ToUTF8(default_search_engine_name);
}

base::Value::Dict AdsServiceDelegate::GetSkus() const {
base::Value::Dict dict;

const base::Value::Dict& skus_state_dict =
local_state_->GetDict(skus::prefs::kSkusState);

for (const auto sku_state_dict : skus_state_dict) {
if (!sku_state_dict.first.starts_with("skus:")) {
continue;
}

// Convert to Value as it's stored as string in local state.
const std::optional<base::Value::Dict> skus_dict =
base::JSONReader::ReadDict(sku_state_dict.second.GetString());
if (!skus_dict) {
continue;
}

const base::Value::Dict* const orders_dict = skus_dict->FindDict("orders");
if (!orders_dict) {
continue;
}

base::Value::Dict order_dict_output;

for (const auto [_, order] : *orders_dict) {
const auto* order_dict = order.GetIfDict();
if (!order_dict) {
continue;
}

const std::string* const order_location =
order_dict->FindString("location");
if (!order_location) {
continue;
}

base::Value::Dict order_details;

if (const auto* const created_at = order_dict->FindString("created_at")) {
order_details.Set("created_at", *created_at);
}

if (const auto* const expires_at = order_dict->FindString("expires_at")) {
order_details.Set("expires_at", *expires_at);
}

if (const auto* const status = order_dict->FindString("status")) {
order_details.Set("status", *status);
}

order_dict_output.Set(*order_location, std::move(order_details));
}

// Set output with env like {skus:production: {...}}.
dict.Set(sku_state_dict.first, std::move(order_dict_output));
}

return dict;
}

void AdsServiceDelegate::OpenNewTabWithUrl(const GURL& url) {
#if BUILDFLAG(IS_ANDROID)
// ServiceTabLauncher can currently only launch new tabs
Expand Down Expand Up @@ -157,16 +220,19 @@ bool AdsServiceDelegate::IsFullScreenMode() {
#endif

base::Value::Dict AdsServiceDelegate::GetVirtualPrefs() {
return base::Value::Dict()
.Set("[virtual]:operating_system.name", version_info::GetOSType())
.Set("[virtual]:operating_system.locale",
brave_l10n::GetDefaultLocaleString())
.Set("[virtual]:application_locale", application_locale_)
.Set("[virtual]:build_channel.name",
version_info::GetChannelString(chrome::GetChannel()))
.Set("[virtual]:browser_version", version_info::GetVersionNumber())
.Set("[virtual]:default_search_engine.name",
GetDefaultSearchEngineName());
auto foo =
base::Value::Dict()
.Set("[virtual]:operating_system.name", version_info::GetOSType())
.Set("[virtual]:operating_system.locale",
brave_l10n::GetDefaultLocaleString())
.Set("[virtual]:build_channel.name",
version_info::GetChannelString(chrome::GetChannel()))
.Set("[virtual]:browser.version", version_info::GetVersionNumber())
.Set("[virtual]:default_search_engine.name",
GetDefaultSearchEngineName())
.Set("[virtual]:skus", GetSkus());
VLOG(1) << "FOOBAR.Virtual_prefs: " << foo;
return foo;
}

} // namespace brave_ads
2 changes: 2 additions & 0 deletions browser/brave_ads/ads_service_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class AdsServiceDelegate : public AdsService::Delegate {

std::string GetDefaultSearchEngineName();

base::Value::Dict GetSkus() const;

// AdsService::Delegate implementation
void InitNotificationHelper() override;
bool CanShowSystemNotificationsWhileBrowserIsBackgrounded() override;
Expand Down

0 comments on commit 2c363fd

Please sign in to comment.