diff --git a/BUILD.gn b/BUILD.gn index 94fad4e..81cb495 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -78,6 +78,8 @@ source_set("ads") { "src/json_helper.cc", "src/uri_helper.cc", "src/bat/ads/ad_info.cc", + "src/bat/ads/issuer_info.cc", + "src/bat/ads/issuers_info.cc", "src/bat/ads/notification_info.cc", "src/bat/ads/client_info.cc", "src/bat/ads/url_components.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c9343e..6420682 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,8 @@ message(STATUS "BAT Native Ads") set(SOURCES "${PROJECT_SOURCE_DIR}/src/bat/ads/ad_info.cc" "${PROJECT_SOURCE_DIR}/src/bat/ads/client_info.cc" + "${PROJECT_SOURCE_DIR}/src/bat/ads/issuer_info.cc" + "${PROJECT_SOURCE_DIR}/src/bat/ads/issuers_info.cc" "${PROJECT_SOURCE_DIR}/src/bat/ads/notification_info.cc" "${PROJECT_SOURCE_DIR}/src/bat/ads/url_components.cc" "${PROJECT_SOURCE_DIR}/src/ads.cc" diff --git a/README.md b/README.md index 06f8d15..4afa107 100644 --- a/README.md +++ b/README.md @@ -175,11 +175,6 @@ void LoadUserModelForLocale( const std::string GenerateUUID() const ``` -`GetSSID` should return the network SSID or an empty string if not available -``` -const std::string GetSSID() const -``` - `IsForeground` should return `true` if the browser is in the foreground otherwise returns `false` ``` bool IsForeground() const @@ -195,14 +190,19 @@ bool IsNotificationsAvailable() const void ShowNotification(std::unique_ptr info) ``` -`CanShowAd` should return `true` if Confirmations is ready to show the ad otherwise returns `false` +`SetCatalogIssuers` should notify that the catalog issuers have changed ``` -bool CanShowAd(const AdInfo& ad_info) + void SetCatalogIssuers(std::unique_ptr info); +``` + +`IsConfirmationsReadyToShowAds` should return `true` if Confirmations is ready to show ads otherwise returns `false` +``` +bool IsConfirmationsReadyToShowAds() ``` `AdSustained` should be called to inform Confirmations that an ad was sustained ``` -void AdSustained(const AdInfo& ad_info) +void AdSustained(std::unique_ptr info) ``` `SetTimer` should create a timer to trigger after the time offset specified in seconds. If the timer was created successfully a unique identifier should be returned, otherwise returns `0` @@ -215,12 +215,6 @@ uint32_t SetTimer(const uint64_t time_offset) void KillTimer(uint32_t timer_id) ``` -`OnCatalogIssuersChanged` should notify that the catalog issuers have changed -``` - void OnCatalogIssuersChanged( - const std::vector& issuers); -``` - `URLRequest` should start a URL request ``` void URLRequest( diff --git a/include/bat/ads/ads_client.h b/include/bat/ads/ads_client.h index a57c0bd..4463303 100644 --- a/include/bat/ads/ads_client.h +++ b/include/bat/ads/ads_client.h @@ -14,7 +14,7 @@ #include #include "bat/ads/ad_info.h" -#include "bat/ads/issuer_info.h" +#include "bat/ads/issuers_info.h" #include "bat/ads/bundle_state.h" #include "bat/ads/client_info.h" #include "bat/ads/export.h" @@ -110,12 +110,15 @@ class ADS_EXPORT AdsClient { // Should show a notification virtual void ShowNotification(std::unique_ptr info) = 0; + // Should notify that the catalog issuers have changed + virtual void SetCatalogIssuers(std::unique_ptr info) = 0; + // Should return true if Confirmations is ready to show ad otherwise returns // false - virtual bool CanShowAd(const AdInfo& ad_info) = 0; + virtual bool IsConfirmationsReadyToShowAds() = 0; // Should be called to inform Confirmations that an ad was sustained - virtual void AdSustained(const NotificationInfo& info) = 0; + virtual void AdSustained(std::unique_ptr info) = 0; // Should create a timer to trigger after the time offset specified in // seconds. If the timer was created successfully a unique identifier should @@ -125,10 +128,6 @@ class ADS_EXPORT AdsClient { // Should destroy the timer associated with the specified timer identifier virtual void KillTimer(uint32_t timer_id) = 0; - // Should notify that the catalog issuers have changed - virtual void OnCatalogIssuersChanged( - const std::vector& issuers) = 0; - // Should start a URL request virtual void URLRequest( const std::string& url, diff --git a/include/bat/ads/issuer_info.h b/include/bat/ads/issuer_info.h index 7b65fe7..89514c4 100644 --- a/include/bat/ads/issuer_info.h +++ b/include/bat/ads/issuer_info.h @@ -7,22 +7,14 @@ #include -namespace ads { - -struct IssuerInfo { - IssuerInfo() : - name(""), - public_key("") {} +#include "bat/ads/export.h" - explicit IssuerInfo(const std::string& public_key) : - name(""), - public_key(public_key) {} - - IssuerInfo(const IssuerInfo& info) : - name(info.name), - public_key(info.public_key) {} +namespace ads { - ~IssuerInfo() {} +struct ADS_EXPORT IssuerInfo { + IssuerInfo(); + IssuerInfo(const IssuerInfo& info); + ~IssuerInfo(); std::string name; std::string public_key; diff --git a/include/bat/ads/issuers_info.h b/include/bat/ads/issuers_info.h new file mode 100644 index 0000000..edadaaf --- /dev/null +++ b/include/bat/ads/issuers_info.h @@ -0,0 +1,29 @@ +/* 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/. */ + +#ifndef BAT_ADS_ISSUERS_INFO_H_ +#define BAT_ADS_ISSUERS_INFO_H_ + +#include +#include + +#include "bat/ads/export.h" +#include "bat/ads/issuer_info.h" + +namespace ads { + +struct ADS_EXPORT IssuersInfo { + IssuersInfo(); + IssuersInfo(const IssuersInfo& info); + ~IssuersInfo(); + + const std::string ToJson() const; + bool FromJson(const std::string& json); + + std::vector issuers; +}; + +} // namespace ads + +#endif // BAT_ADS_ISSUERS_INFO_H_ diff --git a/src/ads_impl.cc b/src/ads_impl.cc index 334c05f..292d265 100644 --- a/src/ads_impl.cc +++ b/src/ads_impl.cc @@ -556,6 +556,12 @@ void AdsImpl::CheckReadyAdServe(const bool forced) { } if (!forced) { + if (!ads_client_->IsConfirmationsReadyToShowAds()) { + LOG(INFO) << "Notification not made: Confirmations not ready"; + + return; + } + if (!IsMobile() && !IsForeground()) { // TODO(Terry Mancey): Implement Log (#44) // 'Notification not made', { reason: 'not in foreground' } @@ -735,17 +741,6 @@ bool AdsImpl::ShowAd( return false; } - if (!ads_client_->CanShowAd(ad_info)) { - LOG(INFO) << "Notification not made: Confirmations not ready" - << std::endl << " advertiser: " << ad_info.advertiser - << std::endl << " notificationText: " << ad_info.notification_text - << std::endl << " notificationUrl: " << ad_info.notification_url - << std::endl << " creativeSetId: " << ad_info.notification_url - << std::endl << " uuid: " << ad_info.notification_url; - - return false; - } - auto notification_info = std::make_unique(); notification_info->advertiser = ad_info.advertiser; notification_info->category = category; @@ -992,6 +987,10 @@ void AdsImpl::SustainAdInteraction() { LOG(INFO) << "Sustained ad interaction"; GenerateAdReportingSustainEvent(last_shown_notification_info_); + + auto notification_info = + std::make_unique(last_shown_notification_info_); + ads_client_->AdSustained(std::move(notification_info)); } void AdsImpl::StopSustainingAdInteraction() { @@ -1193,8 +1192,6 @@ void AdsImpl::GenerateAdReportingSustainEvent( auto json = buffer.GetString(); ads_client_->EventLog(json); - - ads_client_->AdSustained(info); } void AdsImpl::GenerateAdReportingLoadEvent( diff --git a/src/ads_serve.cc b/src/ads_serve.cc index 53eaca0..72f7faa 100644 --- a/src/ads_serve.cc +++ b/src/ads_serve.cc @@ -156,11 +156,12 @@ bool AdsServe::ProcessCatalog(const std::string& json) { return false; } - ads_client_->OnCatalogIssuersChanged(catalog.GetIssuers()); - auto callback = std::bind(&AdsServe::OnCatalogSaved, this, _1); catalog.Save(json, callback); + auto issuers_info = std::make_unique(catalog.GetIssuers()); + ads_client_->SetCatalogIssuers(std::move(issuers_info)); + return true; } diff --git a/src/bat/ads/issuer_info.cc b/src/bat/ads/issuer_info.cc new file mode 100644 index 0000000..367d873 --- /dev/null +++ b/src/bat/ads/issuer_info.cc @@ -0,0 +1,19 @@ +/* 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 "bat/ads/issuer_info.h" + +namespace ads { + +IssuerInfo::IssuerInfo() : + name(""), + public_key("") {} + +IssuerInfo::IssuerInfo(const IssuerInfo& info) : + name(info.name), + public_key(info.public_key) {} + +IssuerInfo::~IssuerInfo() = default; + +} // namespace ads diff --git a/src/bat/ads/issuers_info.cc b/src/bat/ads/issuers_info.cc new file mode 100644 index 0000000..c1a1e39 --- /dev/null +++ b/src/bat/ads/issuers_info.cc @@ -0,0 +1,68 @@ +/* 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 "bat/ads/issuers_info.h" + +#include "json_helper.h" + +namespace ads { + +IssuersInfo::IssuersInfo() : + issuers({}) {} + +IssuersInfo::IssuersInfo(const IssuersInfo& info) : + issuers(info.issuers) {} + +IssuersInfo::~IssuersInfo() = default; + +const std::string IssuersInfo::ToJson() const { + std::string json; + SaveToJson(*this, &json); + return json; +} + +bool IssuersInfo::FromJson(const std::string& json) { + rapidjson::Document document; + document.Parse(json.c_str()); + + if (document.HasParseError()) { + return false; + } + + std::vector issuers = {}; + if (document.HasMember("issuers")) { + for (const auto& issuer : document["issuers"].GetArray()) { + IssuerInfo info; + info.name = issuer["name"].GetString(); + info.public_key = issuer["public_key"].GetString(); + + issuers.push_back(info); + } + } + + return true; +} + +void SaveToJson(JsonWriter* writer, const IssuersInfo& info) { + writer->StartObject(); + + writer->String("issuers"); + writer->StartArray(); + for (const auto& issuer : info.issuers) { + writer->StartObject(); + + writer->String("name"); + writer->String(issuer.name.c_str()); + + writer->String("public_key"); + writer->String(issuer.public_key.c_str()); + + writer->EndObject(); + } + writer->EndArray(); + + writer->EndObject(); +} + +} // namespace ads diff --git a/src/catalog.cc b/src/catalog.cc index ca1aef5..cbad147 100755 --- a/src/catalog.cc +++ b/src/catalog.cc @@ -60,7 +60,7 @@ const std::vector& Catalog::GetCampaigns() const { return catalog_state_->campaigns; } -const std::vector& Catalog::GetIssuers() const { +const IssuersInfo& Catalog::GetIssuers() const { return catalog_state_->issuers; } diff --git a/src/catalog.h b/src/catalog.h index fa7374a..38999e5 100755 --- a/src/catalog.h +++ b/src/catalog.h @@ -30,7 +30,7 @@ class Catalog { const std::vector& GetCampaigns() const; - const std::vector& GetIssuers() const; + const IssuersInfo& GetIssuers() const; void Save(const std::string& json, OnSaveCallback callback); void Reset(OnSaveCallback callback); diff --git a/src/catalog_state.cc b/src/catalog_state.cc index 7444c45..dad488e 100644 --- a/src/catalog_state.cc +++ b/src/catalog_state.cc @@ -14,7 +14,7 @@ CatalogState::CatalogState() : version(0), ping(0), campaigns({}), - issuers({}) {} + issuers(IssuersInfo()) {} CatalogState::CatalogState(const CatalogState& state) : catalog_id(state.catalog_id), @@ -39,7 +39,7 @@ bool CatalogState::FromJson( uint64_t new_version = 0; uint64_t new_ping = kDefaultCatalogPing * kMillisecondsInASecond; std::vector new_campaigns = {}; - std::vector new_issuers = {}; + IssuersInfo new_issuers = IssuersInfo(); new_catalog_id = catalog["catalogId"].GetString(); @@ -159,7 +159,7 @@ bool CatalogState::FromJson( issuer_info.name = issuer["name"].GetString(); issuer_info.public_key = issuer["publicKey"].GetString(); - new_issuers.push_back(issuer_info); + new_issuers.issuers.push_back(issuer_info); } catalog_id = new_catalog_id; diff --git a/src/catalog_state.h b/src/catalog_state.h index cd74d75..f9f33f7 100644 --- a/src/catalog_state.h +++ b/src/catalog_state.h @@ -10,7 +10,7 @@ #include #include "campaign_info.h" -#include "bat/ads/issuer_info.h" +#include "bat/ads/issuers_info.h" #include "json_helper.h" namespace ads { @@ -26,7 +26,7 @@ struct CatalogState { uint64_t version; uint64_t ping; std::vector campaigns; - std::vector issuers; + IssuersInfo issuers; }; } // namespace ads diff --git a/src/json_helper.h b/src/json_helper.h index 6343d6e..ada1a7a 100644 --- a/src/json_helper.h +++ b/src/json_helper.h @@ -13,8 +13,9 @@ namespace ads { -struct AdInfo; struct ClientInfo; +struct IssuersInfo; +struct AdInfo; struct NotificationInfo; struct UrlComponents; struct ClientState; @@ -22,8 +23,9 @@ struct BundleState; using JsonWriter = rapidjson::Writer; -void SaveToJson(JsonWriter* writer, const AdInfo& info); void SaveToJson(JsonWriter* writer, const ClientInfo& info); +void SaveToJson(JsonWriter* writer, const IssuersInfo& info); +void SaveToJson(JsonWriter* writer, const AdInfo& info); void SaveToJson(JsonWriter* writer, const NotificationInfo& info); void SaveToJson(JsonWriter* writer, const UrlComponents& components); void SaveToJson(JsonWriter* writer, const ClientState& state); diff --git a/src/mock_ads_client.cc b/src/mock_ads_client.cc index 9d3d0f6..c7536ce 100644 --- a/src/mock_ads_client.cc +++ b/src/mock_ads_client.cc @@ -11,6 +11,7 @@ #include "mock_ads_client.h" #include "bat/ads/bundle_state.h" #include "bat/ads/ad_info.h" +#include "bat/ads/issuers_info.h" #include "math_helper.h" #include "string_helper.h" #include "time_helper.h" @@ -140,8 +141,7 @@ bool MockAdsClient::IsNotificationsAvailable() const { return true; } -void MockAdsClient::ShowNotification( - std::unique_ptr info) { +void MockAdsClient::ShowNotification(std::unique_ptr info) { std::cout << std::endl << "------------------------------------------------"; std::cout << std::endl << "Notification shown:"; std::cout << std::endl << " advertiser: " << info->advertiser; @@ -151,12 +151,15 @@ void MockAdsClient::ShowNotification( std::cout << std::endl << " uuid: " << info->uuid; } -bool MockAdsClient::CanShowAd(const AdInfo& ad_info) { - (void)ad_info; +void MockAdsClient::SetCatalogIssuers(std::unique_ptr info) { + (void)info; +} + +bool MockAdsClient::IsConfirmationsReadyToShowAds() { return true; } -void MockAdsClient::AdSustained(const NotificationInfo& info) { +void MockAdsClient::AdSustained(std::unique_ptr info) { (void)info; } @@ -173,11 +176,6 @@ void MockAdsClient::KillTimer(uint32_t timer_id) { (void)timer_id; } -void MockAdsClient::OnCatalogIssuersChanged( - const std::vector& issuers) { - (void)issuers; -} - void MockAdsClient::URLRequest( const std::string& url, const std::vector& headers, diff --git a/src/mock_ads_client.h b/src/mock_ads_client.h index 5465581..b4cde90 100644 --- a/src/mock_ads_client.h +++ b/src/mock_ads_client.h @@ -53,16 +53,13 @@ class MockAdsClient : public AdsClient { bool IsNotificationsAvailable() const override; void ShowNotification(std::unique_ptr info) override; - bool CanShowAd(const AdInfo& ad_info) override; - - void AdSustained(const NotificationInfo& info) override; + void SetCatalogIssuers(std::unique_ptr info) override; + bool IsConfirmationsReadyToShowAds() override; + void AdSustained(std::unique_ptr info) override; uint32_t SetTimer(const uint64_t time_offset) override; void KillTimer(const uint32_t timer_id) override; - void OnCatalogIssuersChanged( - const std::vector& issuers) override; - void URLRequest( const std::string& url, const std::vector& headers, diff --git a/test/mock_ads_client.h b/test/mock_ads_client.h index 2f849d7..4bc445d 100644 --- a/test/mock_ads_client.h +++ b/test/mock_ads_client.h @@ -89,18 +89,17 @@ class MockAdsClient : public AdsClient { MOCK_METHOD1(ShowNotification, void( std::unique_ptr info)); - MOCK_METHOD1(CanShowAd, bool( - const AdInfo& ad_info)); + MOCK_METHOD1(SetCatalogIssuers, void( + std::unique_ptr info)); + + MOCK_METHOD0(IsConfirmationsReadyToShowAds, bool()); MOCK_METHOD1(AdSustained, void( - const NotificationInfo& info)); + std::unique_ptr info)); MOCK_METHOD1(SetTimer, uint32_t( const uint64_t time_offset)); - MOCK_METHOD1(OnCatalogIssuersChanged, void( - const std::vector& issuers)); - MOCK_METHOD1(KillTimer, void( uint32_t timer_id));