Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brave ads mobile delivery policy change #3080

Merged
merged 3 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions vendor/bat-native-ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ source_set("ads") {
"src/bat/ads/internal/search_providers.cc",
"src/bat/ads/internal/search_providers.h",
"src/bat/ads/internal/static_values.h",
"src/bat/ads/internal/time_helper.cc",
"src/bat/ads/internal/time_helper.h",
"src/bat/ads/internal/time.cc",
"src/bat/ads/internal/time.h",
"src/bat/ads/internal/uri_helper.cc",
"src/bat/ads/internal/uri_helper.h",
]
Expand Down
64 changes: 42 additions & 22 deletions vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "bat/ads/internal/search_providers.h"
#include "bat/ads/internal/locale_helper.h"
#include "bat/ads/internal/uri_helper.h"
#include "bat/ads/internal/time_helper.h"
#include "bat/ads/internal/time.h"
#include "bat/ads/internal/static_values.h"

#include "rapidjson/document.h"
Expand Down Expand Up @@ -128,10 +128,6 @@ void AdsImpl::InitializeStep4(const Result result) {

client_->UpdateAdUUID();

if (IsMobile()) {
StartDeliveringNotifications();
}

if (_is_debug) {
StartCollectingActivity(kDebugOneHourInSeconds);
} else {
Expand Down Expand Up @@ -221,11 +217,19 @@ bool AdsImpl::GetNotificationForId(
void AdsImpl::OnForeground() {
is_foreground_ = true;
GenerateAdReportingForegroundEvent();

if (IsMobile()) {
StartDeliveringNotifications();
}
}

void AdsImpl::OnBackground() {
is_foreground_ = false;
GenerateAdReportingBackgroundEvent();

if (IsMobile()) {
StopDeliveringNotifications();
}
}

bool AdsImpl::IsForeground() const {
Expand Down Expand Up @@ -656,7 +660,7 @@ void AdsImpl::CheckEasterEgg(const std::string& url) {
return;
}

auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();

if (UrlHostsMatch(url, kEasterEggUrl) &&
next_easter_egg_timestamp_in_seconds_ < now_in_seconds) {
Expand Down Expand Up @@ -943,7 +947,7 @@ bool AdsImpl::HistoryRespectsRollingTimeConstraint(
const uint64_t allowable_ad_count) const {
uint64_t recent_count = 0;

auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();

for (const auto& timestamp_in_seconds : history) {
if (now_in_seconds - timestamp_in_seconds < seconds_window) {
Expand Down Expand Up @@ -1064,8 +1068,21 @@ bool AdsImpl::IsCollectingActivity() const {
void AdsImpl::StartDeliveringNotifications() {
StopDeliveringNotifications();

const uint64_t start_timer_in =
base::Time::kSecondsPerHour / ads_client_->GetAdsPerHour();
if (client_->GetNextCheckServeAdTimestampInSeconds() == 0) {
client_->UpdateNextCheckServeAdTimestampInSeconds();
}

auto now_in_seconds = Time::NowInSeconds();
auto next_check_serve_ad_timestamp_in_seconds =
client_->GetNextCheckServeAdTimestampInSeconds();

uint64_t start_timer_in;
if (now_in_seconds >= next_check_serve_ad_timestamp_in_seconds) {
// Browser was launched after the next check to serve an ad
start_timer_in = 1 * base::Time::kSecondsPerMinute;
} else {
start_timer_in = next_check_serve_ad_timestamp_in_seconds - now_in_seconds;
}

delivering_notifications_timer_id_ = ads_client_->SetTimer(start_timer_in);
if (delivering_notifications_timer_id_ == 0) {
Expand All @@ -1082,6 +1099,8 @@ void AdsImpl::StartDeliveringNotifications() {
void AdsImpl::DeliverNotification() {
NotificationAllowedCheck(true);

client_->UpdateNextCheckServeAdTimestampInSeconds();

StartDeliveringNotifications();
}

Expand All @@ -1108,7 +1127,7 @@ bool AdsImpl::IsCatalogOlderThanOneDay() {
auto catalog_last_updated_timestamp_in_seconds =
bundle_->GetCatalogLastUpdatedTimestampInSeconds();

auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();

if (catalog_last_updated_timestamp_in_seconds != 0 &&
now_in_seconds > catalog_last_updated_timestamp_in_seconds
Expand Down Expand Up @@ -1145,7 +1164,8 @@ void AdsImpl::NotificationAllowedCheck(const bool serve) {

if (!ok) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Notification not made', { reason: 'notifications not presently allowed' }
// 'Notification not made', { reason: 'notifications not presently allowed'
// }

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

Expand Down Expand Up @@ -1288,7 +1308,7 @@ void AdsImpl::GenerateAdReportingNotificationShownEvent(
writer.String("notify");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("notificationType");
Expand Down Expand Up @@ -1342,7 +1362,7 @@ void AdsImpl::GenerateAdReportingNotificationResultEvent(
writer.String("notify");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("notificationType");
Expand Down Expand Up @@ -1411,7 +1431,7 @@ void AdsImpl::GenerateAdReportingConfirmationEvent(
writer.String("confirmation");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("notificationId");
Expand Down Expand Up @@ -1447,7 +1467,7 @@ void AdsImpl::GenerateAdReportingLoadEvent(
writer.String("load");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("tabId");
Expand Down Expand Up @@ -1504,7 +1524,7 @@ void AdsImpl::GenerateAdReportingBackgroundEvent() {
writer.String("background");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.EndObject();
Expand All @@ -1528,7 +1548,7 @@ void AdsImpl::GenerateAdReportingForegroundEvent() {
writer.String("foreground");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.EndObject();
Expand All @@ -1553,7 +1573,7 @@ void AdsImpl::GenerateAdReportingBlurEvent(
writer.String("blur");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("tabId");
Expand Down Expand Up @@ -1581,7 +1601,7 @@ void AdsImpl::GenerateAdReportingDestroyEvent(
writer.String("destroy");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("tabId");
Expand Down Expand Up @@ -1609,7 +1629,7 @@ void AdsImpl::GenerateAdReportingFocusEvent(
writer.String("focus");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("tabId");
Expand All @@ -1636,7 +1656,7 @@ void AdsImpl::GenerateAdReportingRestartEvent() {
writer.String("restart");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.EndObject();
Expand All @@ -1660,7 +1680,7 @@ void AdsImpl::GenerateAdReportingSettingsEvent() {
writer.String("settings");

writer.String("stamp");
auto time_stamp = helper::Time::TimeStamp();
auto time_stamp = Time::Timestamp();
writer.String(time_stamp.c_str());

writer.String("settings");
Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ads/src/bat/ads/internal/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "bat/ads/internal/bundle.h"
#include "bat/ads/internal/catalog.h"
#include "bat/ads/internal/json_helper.h"
#include "bat/ads/internal/time_helper.h"
#include "bat/ads/internal/time.h"
#include "bat/ads/internal/logging.h"
#include "bat/ads/internal/static_values.h"

Expand Down Expand Up @@ -182,7 +182,7 @@ std::unique_ptr<BundleState> Bundle::GenerateFromCatalog(
state->catalog_version = catalog.GetVersion();
state->catalog_ping = catalog.GetPing();
state->catalog_last_updated_timestamp_in_seconds =
helper::Time::NowInSeconds();
Time::NowInSeconds();
state->categories = categories;

return state;
Expand Down
33 changes: 24 additions & 9 deletions vendor/bat-native-ads/src/bat/ads/internal/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "bat/ads/internal/client.h"
#include "bat/ads/internal/json_helper.h"
#include "bat/ads/internal/time_helper.h"
#include "bat/ads/internal/time.h"
#include "bat/ads/internal/static_values.h"
#include "bat/ads/internal/logging.h"

Expand All @@ -32,7 +32,7 @@ void Client::Initialize(InitializeCallback callback) {
}

void Client::AppendCurrentTimeToAdsShownHistory() {
auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();
client_state_->ads_shown_history.push_front(now_in_seconds);

if (client_state_->ads_shown_history.size() >
Expand Down Expand Up @@ -83,6 +83,21 @@ void Client::ResetAdsUUIDSeen(
SaveState();
}

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

client_state_->next_check_serve_ad_timestamp_in_seconds
= timestamp_in_seconds;

SaveState();
}

uint64_t Client::GetNextCheckServeAdTimestampInSeconds() {
return client_state_->next_check_serve_ad_timestamp_in_seconds;
}

void Client::SetAvailable(const bool available) {
client_state_->available = available;

Expand All @@ -99,7 +114,7 @@ void Client::FlagShoppingState(
client_state_->shop_activity = true;
client_state_->shop_url = url;
client_state_->score = score;
client_state_->last_shop_time = helper::Time::NowInSeconds();
client_state_->last_shop_time = Time::NowInSeconds();

SaveState();
}
Expand All @@ -120,7 +135,7 @@ void Client::FlagSearchState(
client_state_->search_activity = true;
client_state_->search_url = url;
client_state_->score = score;
client_state_->last_search_time = helper::Time::NowInSeconds();
client_state_->last_search_time = Time::NowInSeconds();

SaveState();
}
Expand All @@ -131,7 +146,7 @@ void Client::UnflagSearchState(const std::string& url) {
}

client_state_->search_activity = false;
client_state_->last_search_time = helper::Time::NowInSeconds();
client_state_->last_search_time = Time::NowInSeconds();

SaveState();
}
Expand All @@ -141,7 +156,7 @@ bool Client::GetSearchState() {
}

void Client::UpdateLastUserActivity() {
client_state_->last_user_activity = helper::Time::NowInSeconds();
client_state_->last_user_activity = Time::NowInSeconds();

SaveState();
}
Expand All @@ -151,7 +166,7 @@ uint64_t Client::GetLastUserActivity() {
}

void Client::UpdateLastUserIdleStopTime() {
client_state_->last_user_idle_stop_time = helper::Time::NowInSeconds();
client_state_->last_user_idle_stop_time = Time::NowInSeconds();

SaveState();
}
Expand Down Expand Up @@ -209,7 +224,7 @@ void Client::AppendCurrentTimeToCreativeSetHistory(
client_state_->creative_set_history.insert({creative_set_id, {}});
}

auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();
client_state_->creative_set_history.at(
creative_set_id).push_back(now_in_seconds);

Expand All @@ -228,7 +243,7 @@ void Client::AppendCurrentTimeToCampaignHistory(
client_state_->campaign_history.insert({campaign_id, {}});
}

auto now_in_seconds = helper::Time::NowInSeconds();
auto now_in_seconds = Time::NowInSeconds();
client_state_->campaign_history.at(campaign_id).push_back(now_in_seconds);

SaveState();
Expand Down
2 changes: 2 additions & 0 deletions vendor/bat-native-ads/src/bat/ads/internal/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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();
uint64_t GetNextCheckServeAdTimestampInSeconds();
void SetAvailable(const bool available);
bool GetAvailable() const;
void FlagShoppingState(const std::string& url, const uint64_t score);
Expand Down
Loading