Skip to content

Commit

Permalink
Merge pull request #3823 from /issues/6661
Browse files Browse the repository at this point in the history
Change first ad to appear after 2 minutes and retry after 2 minutes if no eligible ads for iOS/Android
  • Loading branch information
tmancey authored Nov 4, 2019
2 parents b4bbabb + 4f9fb0e commit 4de8e57
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
90 changes: 55 additions & 35 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ void AdsImpl::InitializeStep4(
client_->UpdateAdUUID();

if (IsMobile()) {
StartDeliveringNotifications();
if (client_->GetNextCheckServeAdTimestampInSeconds() == 0) {
StartDeliveringNotificationsAfterSeconds(
2 * base::Time::kSecondsPerMinute);
} else {
StartDeliveringNotifications();
}
}

if (_is_debug) {
Expand Down Expand Up @@ -445,11 +450,15 @@ void AdsImpl::NotificationEventTimedOut(
NotificationResultInfoResultType::TIMEOUT);
}

bool AdsImpl::IsDoNotDisturb() const {
bool AdsImpl::ShouldNotDisturb() const {
if (!IsAndroid()) {
return false;
}

if (IsForeground()) {
return false;
}

auto now = base::Time::Now();
base::Time::Exploded now_exploded;
now.LocalExplode(&now_exploded);
Expand Down Expand Up @@ -905,39 +914,37 @@ void AdsImpl::CheckEasterEgg(
void AdsImpl::CheckReadyAdServe(
const bool forced) {
if (!IsInitialized() || !bundle_->IsReady()) {
BLOG(INFO) << "Notification not made: Not initialized";
FailedToServeAd("Not initialized");
return;
}

if (!forced) {
if (!is_confirmations_ready_) {
BLOG(INFO) << "Notification not made: Confirmations not ready";
FailedToServeAd("Confirmations not ready");
return;
}

if (!IsAndroid() && !IsForeground()) {
BLOG(INFO) << "Notification not made: Not in foreground";
FailedToServeAd("Not in foreground");
return;
}

if (IsMediaPlaying()) {
BLOG(INFO) << "Notification not made: Media playing in browser";
FailedToServeAd("Media playing in browser");
return;
}

if (IsDoNotDisturb() && !IsForeground()) {
if (ShouldNotDisturb()) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Notification not made', { reason: 'do not disturb while not in
// foreground' }

BLOG(INFO)
<< "Notification not made: Do not disturb while not in foreground";

FailedToServeAd("Should not disturb");
return;
}

if (!IsAllowedToServeAds()) {
BLOG(INFO) << "Notification not made: Not allowed based on history";
FailedToServeAd("Not allowed based on history");
return;
}
}
Expand All @@ -948,11 +955,11 @@ void AdsImpl::CheckReadyAdServe(

void AdsImpl::ServeAdFromCategory(
const std::string& category) {
BLOG(INFO) << "Notification for category " << category;
BLOG(INFO) << "Serving ad for category: " << category;

std::string catalog_id = bundle_->GetCatalogId();
if (catalog_id.empty()) {
BLOG(INFO) << "Notification not made: No ad catalog";
FailedToServeAd("No ad catalog");
return;
}

Expand All @@ -963,8 +970,7 @@ void AdsImpl::ServeAdFromCategory(
return;
}

BLOG(INFO) << "Notification not made: Category is empty, trying "
<< "again with untargeted category";
BLOG(INFO) << "Category is empty, trying again with untargeted category";

ServeUntargetedAd();
}
Expand All @@ -983,7 +989,7 @@ void AdsImpl::OnServeAdFromCategory(
return;
}

BLOG(INFO) << "Notification not made: No ads found in \"" << category
BLOG(INFO) << "No ads found in \"" << category
<< "\" category, trying again with untargeted category";

ServeUntargetedAd();
Expand All @@ -999,12 +1005,11 @@ bool AdsImpl::ServeAdFromParentCategory(

std::string parent_category = category.substr(0, pos);

BLOG(INFO) << "Notification not made: No ads found in \"" << category
BLOG(INFO) << "No ads found in \"" << category
<< "\" category, trying again with \"" << parent_category
<< "\" category";

auto callback =
std::bind(&AdsImpl::OnServeAdFromCategory, this, _1, _2, _3);
auto callback = std::bind(&AdsImpl::OnServeAdFromCategory, this, _1, _2, _3);
ads_client_->GetAds(parent_category, callback);

return true;
Expand All @@ -1025,8 +1030,7 @@ void AdsImpl::OnServeUntargetedAd(
return;
}

BLOG(INFO) << "Notification not made: No ad (or eligible ad) for \""
<< category << "\" category";
FailedToServeAd("No eligible ads found");
}

void AdsImpl::ServeAd(
Expand All @@ -1038,6 +1042,25 @@ void AdsImpl::ServeAd(
auto rand = base::RandInt(0, ads.size() - 1);
auto ad = ads.at(rand);
ShowAd(ad, category);

SuccessfullyServedAd();
}

void AdsImpl::SuccessfullyServedAd() {
if (IsMobile()) {
StartDeliveringNotificationsAfterSeconds(
base::Time::kSecondsPerHour / ads_client_->GetAdsPerHour());
}
}

void AdsImpl::FailedToServeAd(
const std::string& reason) {
BLOG(INFO) << "Notification not made: " << reason;

if (IsMobile()) {
StartDeliveringNotificationsAfterSeconds(
2 * base::Time::kSecondsPerMinute);
}
}

std::vector<AdInfo> AdsImpl::GetEligibleAds(
Expand Down Expand Up @@ -1416,10 +1439,6 @@ bool AdsImpl::IsCollectingActivity() const {
void AdsImpl::StartDeliveringNotifications() {
StopDeliveringNotifications();

if (client_->GetNextCheckServeAdTimestampInSeconds() == 0) {
client_->UpdateNextCheckServeAdTimestampInSeconds();
}

auto now_in_seconds = Time::NowInSeconds();
auto next_check_serve_ad_timestamp_in_seconds =
client_->GetNextCheckServeAdTimestampInSeconds();
Expand All @@ -1444,14 +1463,18 @@ void AdsImpl::StartDeliveringNotifications() {
<< start_timer_in << " seconds";
}

void AdsImpl::DeliverNotification() {
NotificationAllowedCheck(true);

client_->UpdateNextCheckServeAdTimestampInSeconds();
void AdsImpl::StartDeliveringNotificationsAfterSeconds(
const uint64_t seconds) {
auto timestamp_in_seconds = Time::NowInSeconds() + seconds;
client_->SetNextCheckServeAdTimestampInSeconds(timestamp_in_seconds);

StartDeliveringNotifications();
}

void AdsImpl::DeliverNotification() {
NotificationAllowedCheck(true);
}

void AdsImpl::StopDeliveringNotifications() {
if (!IsDeliveringNotifications()) {
return;
Expand Down Expand Up @@ -1516,26 +1539,23 @@ void AdsImpl::NotificationAllowedCheck(
// 'Notification not made', { reason: 'notifications not presently allowed'
// }

BLOG(INFO) << "Notification not made: Notifications not presently allowed";

FailedToServeAd("Notifications not presently allowed");
return;
}

if (!ads_client_->IsNetworkConnectionAvailable()) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Notification not made', { reason: 'network connection not available' }

BLOG(INFO) << "Notification not made: Network connection not available";

FailedToServeAd("Network connection not available");
return;
}

if (IsCatalogOlderThanOneDay()) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Notification not made', { reason: 'catalog older than one day' }

BLOG(INFO) << "Notification not made: Catalog older than one day";

FailedToServeAd("Catalog older than one day");
return;
}

Expand Down
8 changes: 7 additions & 1 deletion vendor/bat-native-ads/src/bat/ads/internal/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class AdsImpl : public Ads {
const std::string& id,
const NotificationInfo& notification);

bool IsDoNotDisturb() const;
bool ShouldNotDisturb() const;

int32_t active_tab_id_;
std::string active_tab_url_;
Expand Down Expand Up @@ -205,6 +205,10 @@ class AdsImpl : public Ads {
const std::string& category,
const std::vector<AdInfo>& ads);

void SuccessfullyServedAd();
void FailedToServeAd(
const std::string& reason);

std::vector<AdInfo> GetEligibleAds(
const std::vector<AdInfo>& ads);
std::vector<AdInfo> GetUnseenAdsAndRoundRobinIfNeeded(
Expand Down Expand Up @@ -255,6 +259,8 @@ class AdsImpl : public Ads {

uint32_t delivering_notifications_timer_id_;
void StartDeliveringNotifications();
void StartDeliveringNotificationsAfterSeconds(
const uint64_t seconds);
void DeliverNotification();
void StopDeliveringNotifications();
bool IsDeliveringNotifications() const;
Expand Down
7 changes: 2 additions & 5 deletions vendor/bat-native-ads/src/bat/ads/internal/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,8 @@ void Client::ResetAdsUUIDSeen(
SaveState();
}

void Client::UpdateNextCheckServeAdTimestampInSeconds() {
auto timestamp_in_seconds = Time::NowInSeconds();
timestamp_in_seconds += base::Time::kSecondsPerHour /
ads_client_->GetAdsPerHour();

void Client::SetNextCheckServeAdTimestampInSeconds(
const uint64_t timestamp_in_seconds) {
client_state_->next_check_serve_ad_timestamp_in_seconds
= timestamp_in_seconds;

Expand Down
3 changes: 2 additions & 1 deletion vendor/bat-native-ads/src/bat/ads/internal/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ class Client {
void UpdateAdsUUIDSeen(const std::string& uuid, uint64_t value);
const std::map<std::string, uint64_t> GetAdsUUIDSeen();
void ResetAdsUUIDSeen(const std::vector<AdInfo>& ads);
void UpdateNextCheckServeAdTimestampInSeconds();
void SetNextCheckServeAdTimestampInSeconds(
const uint64_t timestamp_in_seconds);
uint64_t GetNextCheckServeAdTimestampInSeconds();
void SetAvailable(const bool available);
bool GetAvailable() const;
Expand Down

0 comments on commit 4de8e57

Please sign in to comment.