From 234ff41bbd4243582de37a70ca38e8f674d49467 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Fri, 2 Aug 2019 11:33:57 +0100 Subject: [PATCH 1/3] Refactor ads timestamps to use To/FromDoubleT --- vendor/bat-native-ads/BUILD.gn | 4 +- .../src/bat/ads/internal/ads_impl.cc | 30 +++++------ .../src/bat/ads/internal/bundle.cc | 4 +- .../src/bat/ads/internal/client.cc | 18 +++---- .../src/bat/ads/internal/client_state.cc | 46 +++++++++++----- .../src/bat/ads/internal/time.cc | 53 +++++++++++++++++++ .../ads/internal/{time_helper.h => time.h} | 18 ++++--- .../src/bat/ads/internal/time_helper.cc | 29 ---------- 8 files changed, 125 insertions(+), 77 deletions(-) create mode 100644 vendor/bat-native-ads/src/bat/ads/internal/time.cc rename vendor/bat-native-ads/src/bat/ads/internal/{time_helper.h => time.h} (50%) delete mode 100644 vendor/bat-native-ads/src/bat/ads/internal/time_helper.cc diff --git a/vendor/bat-native-ads/BUILD.gn b/vendor/bat-native-ads/BUILD.gn index 3ac5963a220a..8510025f2325 100644 --- a/vendor/bat-native-ads/BUILD.gn +++ b/vendor/bat-native-ads/BUILD.gn @@ -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", ] diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index 6047054bdcf1..0c32fa4c718c 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -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" @@ -656,7 +656,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) { @@ -943,7 +943,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) { @@ -1108,7 +1108,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 @@ -1288,7 +1288,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"); @@ -1342,7 +1342,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"); @@ -1411,7 +1411,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"); @@ -1447,7 +1447,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"); @@ -1504,7 +1504,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(); @@ -1528,7 +1528,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(); @@ -1553,7 +1553,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"); @@ -1581,7 +1581,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"); @@ -1609,7 +1609,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"); @@ -1636,7 +1636,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(); @@ -1660,7 +1660,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"); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/bundle.cc b/vendor/bat-native-ads/src/bat/ads/internal/bundle.cc index 0f10eff1cee7..15f8c59049fa 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/bundle.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/bundle.cc @@ -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" @@ -182,7 +182,7 @@ std::unique_ptr 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; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client.cc b/vendor/bat-native-ads/src/bat/ads/internal/client.cc index 2c29390a3a9b..ab786dd15604 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/client.cc @@ -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" @@ -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() > @@ -99,7 +99,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(); } @@ -120,7 +120,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(); } @@ -131,7 +131,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(); } @@ -141,7 +141,7 @@ bool Client::GetSearchState() { } void Client::UpdateLastUserActivity() { - client_state_->last_user_activity = helper::Time::NowInSeconds(); + client_state_->last_user_activity = Time::NowInSeconds(); SaveState(); } @@ -151,7 +151,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(); } @@ -209,7 +209,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); @@ -228,7 +228,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(); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc b/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc index c3f10e99ec44..206d7585f8bb 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc @@ -6,6 +6,7 @@ #include "bat/ads/internal/client_state.h" #include "bat/ads/internal/json_helper.h" #include "bat/ads/internal/static_values.h" +#include "bat/ads/internal/time.h" namespace ads { @@ -74,8 +75,11 @@ Result ClientState::FromJson( } if (client.HasMember("adsShownHistory")) { - for (const auto& ad_shown : client["adsShownHistory"].GetArray()) { - ads_shown_history.push_back(ad_shown.GetUint64()); + for (const auto& timestamp_in_seconds : + client["adsShownHistory"].GetArray()) { + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + timestamp_in_seconds.GetUint64()); + ads_shown_history.push_back(migrated_timestamp_in_seconds); } } @@ -95,19 +99,27 @@ Result ClientState::FromJson( } if (client.HasMember("lastSearchTime")) { - last_search_time = client["lastSearchTime"].GetUint64(); + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + client["lastSearchTime"].GetUint64()); + last_search_time = migrated_timestamp_in_seconds; } if (client.HasMember("lastShopTime")) { - last_shop_time = client["lastShopTime"].GetUint64(); + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + client["lastShopTime"].GetUint64()); + last_shop_time = migrated_timestamp_in_seconds; } if (client.HasMember("lastUserActivity")) { - last_user_activity = client["lastUserActivity"].GetUint64(); + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + client["lastUserActivity"].GetUint64()); + last_user_activity = migrated_timestamp_in_seconds; } if (client.HasMember("lastUserIdleStopTime")) { - last_user_idle_stop_time = client["lastUserIdleStopTime"].GetUint64(); + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + client["lastUserIdleStopTime"].GetUint64()); + last_user_idle_stop_time = migrated_timestamp_in_seconds; } if (client.HasMember("locale")) { @@ -137,25 +149,31 @@ Result ClientState::FromJson( } if (client.HasMember("creativeSetHistory")) { - for (const auto& history : client["creativeSetHistory"].GetObject()) { + for (const auto& creative_set : client["creativeSetHistory"].GetObject()) { std::deque timestamps_in_seconds = {}; - for (const auto& timestamp_in_seconds : history.value.GetArray()) { - timestamps_in_seconds.push_back(timestamp_in_seconds.GetUint64()); + + for (const auto& timestamp_in_seconds : creative_set.value.GetArray()) { + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + timestamp_in_seconds.GetUint64()); + timestamps_in_seconds.push_back(migrated_timestamp_in_seconds); } - std::string creative_set_id = history.name.GetString(); + std::string creative_set_id = creative_set.name.GetString(); creative_set_history.insert({creative_set_id, timestamps_in_seconds}); } } if (client.HasMember("campaignHistory")) { - for (const auto& history : client["campaignHistory"].GetObject()) { + for (const auto& campaign : client["campaignHistory"].GetObject()) { std::deque timestamps_in_seconds = {}; - for (const auto& timestamp_in_seconds : history.value.GetArray()) { - timestamps_in_seconds.push_back(timestamp_in_seconds.GetUint64()); + + for (const auto& timestamp_in_seconds : campaign.value.GetArray()) { + auto migrated_timestamp_in_seconds = Time::MigrateTimestampToDoubleT( + timestamp_in_seconds.GetUint64()); + timestamps_in_seconds.push_back(migrated_timestamp_in_seconds); } - std::string campaign_id = history.name.GetString(); + std::string campaign_id = campaign.name.GetString(); campaign_history.insert({campaign_id, timestamps_in_seconds}); } } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/time.cc b/vendor/bat-native-ads/src/bat/ads/internal/time.cc new file mode 100644 index 000000000000..0021de1a13a0 --- /dev/null +++ b/vendor/bat-native-ads/src/bat/ads/internal/time.cc @@ -0,0 +1,53 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include + +#include "bat/ads/internal/time.h" + +namespace ads { + +std::string Time::Timestamp() { + time_t rawtime; + std::time(&rawtime); + + char buffer[24]; + struct tm* timeinfo = std::localtime(&rawtime); + strftime(buffer, 24, "%FT%TZ", timeinfo); + return std::string(buffer); +} + +uint64_t Time::NowInSeconds() { + auto now = base::Time::Now(); + return now.ToDoubleT(); +} + +uint64_t Time::MigrateTimestampToDoubleT(const uint64_t timestamp_in_seconds) { + if (timestamp_in_seconds < 10000000000) { + // Already migrated as DoubleT will never reach 10000000000 in our lifetime + // and legacy timestamps are above 10000000000 + return timestamp_in_seconds; + } + + // Migrate date to DoubleT + auto now = base::Time::Now(); + auto now_in_seconds = static_cast((now - base::Time()).InSeconds()); + + auto delta = timestamp_in_seconds - now_in_seconds; + + auto date = now + base::TimeDelta::FromSeconds(delta); + return date.ToDoubleT(); +} + +base::Time Time::FromDoubleT(const uint64_t timestamp_in_seconds) { + if (timestamp_in_seconds == 0) { + // Workaround for Windows crash when passing 0 to LocalExplode + return base::Time::Now(); + } + + return base::Time::FromDoubleT(timestamp_in_seconds); +} + +} // namespace ads diff --git a/vendor/bat-native-ads/src/bat/ads/internal/time_helper.h b/vendor/bat-native-ads/src/bat/ads/internal/time.h similarity index 50% rename from vendor/bat-native-ads/src/bat/ads/internal/time_helper.h rename to vendor/bat-native-ads/src/bat/ads/internal/time.h index 28d8a147656c..2c9ce85f23af 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/time_helper.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/time.h @@ -3,21 +3,27 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BAT_ADS_INTERNAL_TIME_HELPER_H_ -#define BAT_ADS_INTERNAL_TIME_HELPER_H_ +#ifndef BAT_ADS_INTERNAL_TIME_H_ +#define BAT_ADS_INTERNAL_TIME_H_ #include #include -namespace helper { +#include "base/time/time.h" + +namespace ads { class Time { public: - static std::string TimeStamp(); + static std::string Timestamp(); static uint64_t NowInSeconds(); + static uint64_t MigrateTimestampToDoubleT( + const uint64_t timestamp_in_seconds); + static base::Time FromDoubleT( + const uint64_t timestamp_in_seconds); }; -} // namespace helper +} // namespace ads -#endif // BAT_ADS_INTERNAL_TIME_HELPER_H_ +#endif // BAT_ADS_INTERNAL_TIME_H_ diff --git a/vendor/bat-native-ads/src/bat/ads/internal/time_helper.cc b/vendor/bat-native-ads/src/bat/ads/internal/time_helper.cc deleted file mode 100644 index 6d3c53f5547e..000000000000 --- a/vendor/bat-native-ads/src/bat/ads/internal/time_helper.cc +++ /dev/null @@ -1,29 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include - -#include "bat/ads/internal/time_helper.h" - -#include "base/time/time.h" - -namespace helper { - -std::string Time::TimeStamp() { - time_t rawtime; - std::time(&rawtime); - - char buffer[24]; - struct tm* timeinfo = std::localtime(&rawtime); - strftime(buffer, 24, "%FT%TZ", timeinfo); - return std::string(buffer); -} - -uint64_t Time::NowInSeconds() { - auto now = base::Time::Now(); - return static_cast((now - base::Time()).InSeconds()); -} - -} // namespace helper From 4d52ceb64f4a720b1fe421789dc71aac8be89971 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Fri, 2 Aug 2019 11:35:43 +0100 Subject: [PATCH 2/3] Update mobile ads policy for delivery of ads --- .../src/bat/ads/internal/ads_impl.cc | 31 +++++++++++++++---- .../src/bat/ads/internal/client.cc | 15 +++++++++ .../src/bat/ads/internal/client.h | 2 ++ .../src/bat/ads/internal/client_state.cc | 11 +++++++ .../src/bat/ads/internal/client_state.h | 1 + vendor/bat-native-ads/test/data/client.json | 1 + 6 files changed, 55 insertions(+), 6 deletions(-) diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index 0c32fa4c718c..5b6643693b97 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -128,10 +128,6 @@ void AdsImpl::InitializeStep4(const Result result) { client_->UpdateAdUUID(); - if (IsMobile()) { - StartDeliveringNotifications(); - } - if (_is_debug) { StartCollectingActivity(kDebugOneHourInSeconds); } else { @@ -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 { @@ -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) { @@ -1082,6 +1099,8 @@ void AdsImpl::StartDeliveringNotifications() { void AdsImpl::DeliverNotification() { NotificationAllowedCheck(true); + client_->UpdateNextCheckServeAdTimestampInSeconds(); + StartDeliveringNotifications(); } diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client.cc b/vendor/bat-native-ads/src/bat/ads/internal/client.cc index ab786dd15604..fa4c592e39e4 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/client.cc @@ -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; diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client.h b/vendor/bat-native-ads/src/bat/ads/internal/client.h index 40f0abe9f1a4..c29902f62856 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/client.h @@ -35,6 +35,8 @@ class Client { void UpdateAdsUUIDSeen(const std::string& uuid, uint64_t value); const std::map GetAdsUUIDSeen(); void ResetAdsUUIDSeen(const std::vector& ads); + void UpdateNextCheckServeAdTimestampInSeconds(); + uint64_t GetNextCheckServeAdTimestampInSeconds(); void SetAvailable(const bool available); bool GetAvailable() const; void FlagShoppingState(const std::string& url, const uint64_t score); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc b/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc index 206d7585f8bb..4cf4d7bf499a 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/client_state.cc @@ -14,6 +14,7 @@ ClientState::ClientState() : ads_shown_history({}), ad_uuid(""), ads_uuid_seen({}), + next_check_serve_ad_timestamp_in_seconds(0), available(false), last_search_time(0), last_shop_time(0), @@ -35,6 +36,8 @@ ClientState::ClientState(const ClientState& state) : ads_shown_history(state.ads_shown_history), ad_uuid(state.ad_uuid), ads_uuid_seen(state.ads_uuid_seen), + next_check_serve_ad_timestamp_in_seconds( + state.next_check_serve_ad_timestamp_in_seconds), available(state.available), last_search_time(state.last_search_time), last_shop_time(state.last_shop_time), @@ -94,6 +97,11 @@ Result ClientState::FromJson( } } + if (client.HasMember("nextCheckServeAd")) { + next_check_serve_ad_timestamp_in_seconds = + client["nextCheckServeAd"].GetUint64(); + } + if (client.HasMember("available")) { available = client["available"].GetBool(); } @@ -222,6 +230,9 @@ void SaveToJson(JsonWriter* writer, const ClientState& state) { } writer->EndObject(); + writer->String("nextCheckServeAd"); + writer->Uint64(state.next_check_serve_ad_timestamp_in_seconds); + writer->String("available"); writer->Bool(state.available); diff --git a/vendor/bat-native-ads/src/bat/ads/internal/client_state.h b/vendor/bat-native-ads/src/bat/ads/internal/client_state.h index 1320009aadad..a32c2a47e976 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/client_state.h +++ b/vendor/bat-native-ads/src/bat/ads/internal/client_state.h @@ -29,6 +29,7 @@ struct ClientState { std::deque ads_shown_history; std::string ad_uuid; std::map ads_uuid_seen; + uint64_t next_check_serve_ad_timestamp_in_seconds; bool available; uint64_t last_search_time; uint64_t last_shop_time; diff --git a/vendor/bat-native-ads/test/data/client.json b/vendor/bat-native-ads/test/data/client.json index 817d25b74da5..f77d32596af7 100644 --- a/vendor/bat-native-ads/test/data/client.json +++ b/vendor/bat-native-ads/test/data/client.json @@ -2,6 +2,7 @@ "lastUserIdleStopTime": 0, "creativeSetHistory": { }, + "nextCheckServeAd": 0, "lastShopTime": 0, "campaignHistory": { }, From b82c89e48fbca92a371d1097741a8e88281db014 Mon Sep 17 00:00:00 2001 From: Terry Mancey Date: Fri, 2 Aug 2019 16:36:59 +0100 Subject: [PATCH 3/3] Fix unrelated Linter error --- vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc index 5b6643693b97..86343bf96c05 100644 --- a/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc +++ b/vendor/bat-native-ads/src/bat/ads/internal/ads_impl.cc @@ -1164,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";