Skip to content

Commit

Permalink
Handle SI by bat-native-ads library.
Browse files Browse the repository at this point in the history
  • Loading branch information
aseren committed Feb 21, 2022
1 parent ed58688 commit f7f5058
Show file tree
Hide file tree
Showing 22 changed files with 345 additions and 57 deletions.
62 changes: 56 additions & 6 deletions browser/brave_ads/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -682,13 +682,23 @@ void AdsServiceImpl::OnInitialize(const bool success) {

StartCheckIdleStateTimer();

if (!deprecated_data_files_removed_) {
deprecated_data_files_removed_ = true;
file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
if (!is_setup_on_first_initialize_done_) {
SetupOnFirstInitialize();
is_setup_on_first_initialize_done_ = true;
}
}

void AdsServiceImpl::SetupOnFirstInitialize() {
DCHECK(!is_setup_on_first_initialize_done_);

PurgeOrphanedAdEventsForType(
ads::mojom::AdType::kNewTabPageAd,
base::BindOnce(&AdsServiceImpl::PrefetchNewTabPageAd, AsWeakPtr()));

file_task_runner_->PostTask(
FROM_HERE, base::BindOnce(&RemoveDeprecatedAdsDataFiles, base_path_));
}

void AdsServiceImpl::ShutdownBatAds() {
if (!connected()) {
return;
Expand Down Expand Up @@ -1106,6 +1116,23 @@ void AdsServiceImpl::OnOpenNewTabWithAd(const std::string& json) {
OpenNewTabWithUrl(notification.target_url);
}

absl::optional<ads::NewTabPageAdInfo>
AdsServiceImpl::GetPrefetchedNewTabPageAd() {
if (!connected()) {
return absl::nullopt;
}

absl::optional<ads::NewTabPageAdInfo> ad_info;
if (prefetched_new_tab_page_ad_info_) {
ad_info = prefetched_new_tab_page_ad_info_;
prefetched_new_tab_page_ad_info_.reset();
}

PrefetchNewTabPageAd();

return ad_info;
}

void AdsServiceImpl::OnNewTabPageAdEvent(
const std::string& uuid,
const std::string& creative_instance_id,
Expand Down Expand Up @@ -1152,12 +1179,13 @@ void AdsServiceImpl::OnInlineContentAdEvent(
}

void AdsServiceImpl::PurgeOrphanedAdEventsForType(
const ads::mojom::AdType ad_type) {
const ads::mojom::AdType ad_type,
base::OnceClosure callback) {
if (!connected()) {
return;
}

bat_ads_->PurgeOrphanedAdEventsForType(ad_type);
bat_ads_->PurgeOrphanedAdEventsForType(ad_type, std::move(callback));
}

void AdsServiceImpl::RetryOpeningNewTabWithAd(const std::string& uuid) {
Expand Down Expand Up @@ -1211,6 +1239,28 @@ void AdsServiceImpl::RegisterResourceComponentsForLocale(
locale);
}

void AdsServiceImpl::PrefetchNewTabPageAd() {
if (!connected()) {
prefetched_new_tab_page_ad_info_.reset();
return;
}

bat_ads_->GetNewTabPageAd(
base::BindOnce(&AdsServiceImpl::OnPrefetchNewTabPageAd, AsWeakPtr()));
}

void AdsServiceImpl::OnPrefetchNewTabPageAd(bool success,
const std::string& json) {
if (!success) {
prefetched_new_tab_page_ad_info_.reset();
return;
}

ads::NewTabPageAdInfo ad_info;
ad_info.FromJson(json);
prefetched_new_tab_page_ad_info_ = ad_info;
}

void AdsServiceImpl::OnURLRequestStarted(
const GURL& final_url,
const network::mojom::URLResponseHead& response_head) {
Expand Down
14 changes: 12 additions & 2 deletions browser/brave_ads/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ class AdsServiceImpl : public AdsService,
const std::string& creative_instance_id,
const ads::mojom::InlineContentAdEventType event_type) override;

void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type) override;
absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() override;

void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type,
base::OnceClosure callback) override;

void GetAdsHistory(const double from_timestamp,
const double to_timestamp,
Expand Down Expand Up @@ -208,6 +211,7 @@ class AdsServiceImpl : public AdsService,
void OnCreate();

void OnInitialize(const bool success);
void SetupOnFirstInitialize();

void ShutdownBatAds();
void OnShutdownBatAds(const bool success);
Expand Down Expand Up @@ -255,6 +259,10 @@ class AdsServiceImpl : public AdsService,

void RegisterResourceComponentsForLocale(const std::string& locale);

void PrefetchNewTabPageAd();

void OnPrefetchNewTabPageAd(bool success, const std::string& json);

void OnURLRequestStarted(
const GURL& final_url,
const network::mojom::URLResponseHead& response_head);
Expand Down Expand Up @@ -464,7 +472,7 @@ class AdsServiceImpl : public AdsService,

bool is_initialized_ = false;

bool deprecated_data_files_removed_ = false;
bool is_setup_on_first_initialize_done_ = false;

bool is_upgrading_from_pre_brave_ads_build_;

Expand All @@ -485,6 +493,8 @@ class AdsServiceImpl : public AdsService,

std::unique_ptr<ads::Database> database_;

absl::optional<ads::NewTabPageAdInfo> prefetched_new_tab_page_ad_info_;

ui::IdleState last_idle_state_;
int last_idle_time_;

Expand Down
1 change: 1 addition & 0 deletions browser/notifications/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ source_set("notifications") {
deps = [
"//base",
"//brave/components/brave_adaptive_captcha/buildflags",
"//brave/vendor/bat-native-ads",
"//chrome/browser/notifications",
"//third_party/abseil-cpp:absl",
"//url",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ base::android::ScopedJavaLocalRef<jobject>
NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
JNIEnv* env = AttachCurrentThread();

const std::string wallpaper_id = base::GenerateGUID();
view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id);

auto* image_path =
data->FindStringKey(ntp_background_images::kWallpaperImagePathKey);
auto* logo_image_path =
Expand All @@ -171,6 +168,11 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
data->FindBoolKey(ntp_background_images::kIsSponsoredKey).value_or(false);
auto* creative_instance_id =
data->FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
const std::string* wallpaper_id =
data->FindStringKey(ntp_background_images::kWallpaperIDKey);

view_counter_service_->BrandedWallpaperWillBeDisplayed(wallpaper_id,
creative_instance_id);

return Java_NTPBackgroundImagesBridge_createBrandedWallpaper(
env, ConvertUTF8ToJavaString(env, *image_path), focal_point_x,
Expand All @@ -180,7 +182,7 @@ NTPBackgroundImagesBridge::CreateBrandedWallpaper(base::Value* data) {
ConvertUTF8ToJavaString(env, *theme_name), is_sponsored,
ConvertUTF8ToJavaString(
env, creative_instance_id ? *creative_instance_id : ""),
ConvertUTF8ToJavaString(env, wallpaper_id));
ConvertUTF8ToJavaString(env, wallpaper_id ? *wallpaper_id : ""));
}

void NTPBackgroundImagesBridge::GetTopSites(
Expand Down
12 changes: 7 additions & 5 deletions browser/ui/webui/new_tab_page/brave_new_tab_message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <utility>

#include "base/cxx17_backports.h"
#include "base/guid.h"
#include "base/json/json_writer.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
Expand Down Expand Up @@ -567,7 +566,7 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
return;
}

auto data = service->GetCurrentWallpaperForDisplay();
base::Value data = service->GetCurrentWallpaperForDisplay();

if (!data.is_dict()) {
ResolveJavascriptCallback(args[0], std::move(wallpaper));
Expand All @@ -585,11 +584,14 @@ void BraveNewTabMessageHandler::HandleGetWallpaperData(
return;
}

const std::string* creative_instance_id =
data.FindStringKey(ntp_background_images::kCreativeInstanceIDKey);
const std::string* wallpaper_id =
data.FindStringKey(ntp_background_images::kWallpaperIDKey);
service->BrandedWallpaperWillBeDisplayed(wallpaper_id, creative_instance_id);

constexpr char kBrandedWallpaperKey[] = "brandedWallpaper";
const std::string wallpaper_id = base::GenerateGUID();
data.SetStringKey(ntp_background_images::kWallpaperIDKey, wallpaper_id);
wallpaper.SetKey(kBrandedWallpaperKey, std::move(data));
service->BrandedWallpaperWillBeDisplayed(wallpaper_id);
ResolveJavascriptCallback(args[0], std::move(wallpaper));
}

Expand Down
11 changes: 5 additions & 6 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
#include "base/observer_list.h"
#include "brave/components/brave_adaptive_captcha/buildflags/buildflags.h"
#include "brave/components/brave_ads/browser/ads_service_observer.h"
#include "brave/vendor/bat-native-ads/include/bat/ads/new_tab_page_ad_info.h"
#include "brave/vendor/bat-native-ads/include/bat/ads/public/interfaces/ads.mojom.h"
#include "build/build_config.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/sessions/core/session_id.h"
#include "url/gurl.h"

namespace ads {
struct AdsHistoryInfo;
}

namespace base {
class DictionaryValue;
class ListValue;
Expand Down Expand Up @@ -141,8 +138,10 @@ class AdsService : public KeyedService {
const std::string& creative_instance_id,
const ads::mojom::InlineContentAdEventType event_type) = 0;

virtual void PurgeOrphanedAdEventsForType(
const ads::mojom::AdType ad_type) = 0;
virtual absl::optional<ads::NewTabPageAdInfo> GetPrefetchedNewTabPageAd() = 0;

virtual void PurgeOrphanedAdEventsForType(const ads::mojom::AdType ad_type,
base::OnceClosure callback) = 0;

virtual void GetAdsHistory(const double from_timestamp,
const double to_timestamp,
Expand Down
3 changes: 1 addition & 2 deletions components/ntp_background_images/browser/DEPS
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include_rules = [
"+bat/ads/pref_names.h",
"+bat/ads/public",
"+bat/ads",
"+content/public/browser",
"+content/public/common",
"+third_party/skia",
Expand Down
Loading

0 comments on commit f7f5058

Please sign in to comment.