Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey committed Jan 14, 2019
1 parent e44c3e3 commit 6ebc8c1
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 81 deletions.
2 changes: 2 additions & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 8 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -195,14 +190,19 @@ bool IsNotificationsAvailable() const
void ShowNotification(std::unique_ptr<NotificationInfo> 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<IssuersInfo> 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<NotificationInfo> 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`
Expand All @@ -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<IssuerInfo>& issuers);
```

`URLRequest` should start a URL request
```
void URLRequest(
Expand Down
13 changes: 6 additions & 7 deletions include/bat/ads/ads_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <functional>

#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"
Expand Down Expand Up @@ -110,12 +110,15 @@ class ADS_EXPORT AdsClient {
// Should show a notification
virtual void ShowNotification(std::unique_ptr<NotificationInfo> info) = 0;

// Should notify that the catalog issuers have changed
virtual void SetCatalogIssuers(std::unique_ptr<IssuersInfo> 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<NotificationInfo> 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
Expand All @@ -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<IssuerInfo>& issuers) = 0;

// Should start a URL request
virtual void URLRequest(
const std::string& url,
Expand Down
20 changes: 6 additions & 14 deletions include/bat/ads/issuer_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,14 @@

#include <string>

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;
Expand Down
29 changes: 29 additions & 0 deletions include/bat/ads/issuers_info.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <vector>

#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<IssuerInfo> issuers;
};

} // namespace ads

#endif // BAT_ADS_ISSUERS_INFO_H_
23 changes: 10 additions & 13 deletions src/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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<NotificationInfo>();
notification_info->advertiser = ad_info.advertiser;
notification_info->category = category;
Expand Down Expand Up @@ -992,6 +987,10 @@ void AdsImpl::SustainAdInteraction() {
LOG(INFO) << "Sustained ad interaction";

GenerateAdReportingSustainEvent(last_shown_notification_info_);

auto notification_info =
std::make_unique<NotificationInfo>(last_shown_notification_info_);
ads_client_->AdSustained(std::move(notification_info));
}

void AdsImpl::StopSustainingAdInteraction() {
Expand Down Expand Up @@ -1193,8 +1192,6 @@ void AdsImpl::GenerateAdReportingSustainEvent(

auto json = buffer.GetString();
ads_client_->EventLog(json);

ads_client_->AdSustained(info);
}

void AdsImpl::GenerateAdReportingLoadEvent(
Expand Down
5 changes: 3 additions & 2 deletions src/ads_serve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<IssuersInfo>(catalog.GetIssuers());
ads_client_->SetCatalogIssuers(std::move(issuers_info));

return true;
}

Expand Down
19 changes: 19 additions & 0 deletions src/bat/ads/issuer_info.cc
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions src/bat/ads/issuers_info.cc
Original file line number Diff line number Diff line change
@@ -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<IssuerInfo> 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
2 changes: 1 addition & 1 deletion src/catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const std::vector<CampaignInfo>& Catalog::GetCampaigns() const {
return catalog_state_->campaigns;
}

const std::vector<IssuerInfo>& Catalog::GetIssuers() const {
const IssuersInfo& Catalog::GetIssuers() const {
return catalog_state_->issuers;
}

Expand Down
2 changes: 1 addition & 1 deletion src/catalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Catalog {

const std::vector<CampaignInfo>& GetCampaigns() const;

const std::vector<IssuerInfo>& GetIssuers() const;
const IssuersInfo& GetIssuers() const;

void Save(const std::string& json, OnSaveCallback callback);
void Reset(OnSaveCallback callback);
Expand Down
6 changes: 3 additions & 3 deletions src/catalog_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CatalogState::CatalogState() :
version(0),
ping(0),
campaigns({}),
issuers({}) {}
issuers(IssuersInfo()) {}

CatalogState::CatalogState(const CatalogState& state) :
catalog_id(state.catalog_id),
Expand All @@ -39,7 +39,7 @@ bool CatalogState::FromJson(
uint64_t new_version = 0;
uint64_t new_ping = kDefaultCatalogPing * kMillisecondsInASecond;
std::vector<CampaignInfo> new_campaigns = {};
std::vector<IssuerInfo> new_issuers = {};
IssuersInfo new_issuers = IssuersInfo();

new_catalog_id = catalog["catalogId"].GetString();

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/catalog_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <map>

#include "campaign_info.h"
#include "bat/ads/issuer_info.h"
#include "bat/ads/issuers_info.h"
#include "json_helper.h"

namespace ads {
Expand All @@ -26,7 +26,7 @@ struct CatalogState {
uint64_t version;
uint64_t ping;
std::vector<CampaignInfo> campaigns;
std::vector<IssuerInfo> issuers;
IssuersInfo issuers;
};

} // namespace ads
Expand Down
Loading

0 comments on commit 6ebc8c1

Please sign in to comment.