Skip to content

Commit

Permalink
Merge pull request #49 from brave-intl/resources
Browse files Browse the repository at this point in the history
add support for getting user model and schema data from non-filesyste…
  • Loading branch information
bridiver authored Nov 21, 2018
2 parents 9e66743 + 4e6f686 commit fe2923d
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 20 deletions.
14 changes: 8 additions & 6 deletions include/bat/ads/ads_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ using OnResetCallback = std::function<void(Result)>;

using OnGetAdsForCategoryCallback = std::function<void(Result,
const std::string&, const std::vector<AdInfo>&)>;
using OnGetAdForSampleCategoryCallback = std::function<void(Result,
const std::string&, const AdInfo&)>;
using OnGetAdSampleBundleCallback = std::function<void(Result,
const std::string&)>;

using URLRequestCallback = std::function<void(const int, const std::string&,
const std::map<std::string, std::string>& headers)>;
Expand Down Expand Up @@ -75,6 +75,9 @@ class ADS_EXPORT AdsClient {
// Gets available locales
virtual const std::vector<std::string> GetLocales() const = 0;

virtual void GetUserModelForLocale(const std::string& locale,
OnLoadCallback callback) const = 0;

// Generate a v4 UUID
virtual const std::string GenerateUUID() const = 0;

Expand Down Expand Up @@ -119,7 +122,7 @@ class ADS_EXPORT AdsClient {

// Loads a value
virtual void Load(const std::string& name, OnLoadCallback callback) = 0;
virtual const std::string Load(const std::string& name) = 0;
virtual const std::string LoadSchema(const std::string& name) = 0;

// Reset a previously saved value
virtual void Reset(
Expand All @@ -131,9 +134,8 @@ class ADS_EXPORT AdsClient {
const std::string& category,
OnGetAdsForCategoryCallback callback) = 0;

// Gets a random sample ad
virtual void GetAdForSampleCategory(
OnGetAdForSampleCategoryCallback callback) = 0;
// Gets the sample ad bundle
virtual void GetAdSampleBundle(ads::OnGetAdSampleBundleCallback callback) = 0;

// Gets the components of a URL
virtual bool GetUrlComponents(
Expand Down
15 changes: 15 additions & 0 deletions resources/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import("//tools/grit/grit_rule.gni")
import("//tools/grit/repack.gni")

grit("resources") {
source = "bat_ads_resources.grd"

outputs = [
"grit/bat_ads_resources.h",
"bat_ads_resources.pak",
]

resource_ids = "//brave/browser/resources/resource_ids"

output_dir = "$root_gen_dir/bat/ads/resources"
}
19 changes: 19 additions & 0 deletions resources/bat_ads_resources.grd
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<grit latest_public_release="0" current_release="1" output_all_resource_defines="false">
<outputs>
<output filename="grit/bat_ads_resources.h" type="rc_header">
<emit emit_type='prepend'></emit>
</output>
<output filename="bat_ads_resources.pak" type="data_package" />
</outputs>
<release seq="1">
<includes>
<include name="IDR_ADS_CATALOG_SCHEMA" file="catalog-schema.json" type="BINDATA" />
<include name="IDR_ADS_BUNDLE_SCHEMA" file="bundle-schema.json" type="BINDATA" />
<include name="IDR_ADS_SAMPLE_BUNDLE" file="sample_bundle.json" type="BINDATA" />
<include name="IDR_ADS_USER_MODEL_DE" file="locales/de/user_model.json" type="BINDATA" />
<include name="IDR_ADS_USER_MODEL_FR" file="locales/fr/user_model.json" type="BINDATA" />
<include name="IDR_ADS_USER_MODEL_EN" file="locales/en/user_model.json" type="BINDATA" />
</includes>
</release>
</grit>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
46 changes: 37 additions & 9 deletions src/ads_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,9 @@ void AdsImpl::ServeSampleAd() {
return;
}

auto callback = std::bind(&AdsImpl::OnGetAdForSampleCategory,
this, _1, _2, _3);
ads_client_->GetAdForSampleCategory(callback);
auto callback = std::bind(&AdsImpl::OnGetAdSampleBundle,
this, _1, _2);
ads_client_->GetAdSampleBundle(callback);
}

void AdsImpl::StartCollectingActivity(const uint64_t start_timer_in) {
Expand Down Expand Up @@ -636,19 +636,47 @@ void AdsImpl::OnGetAdsForCategory(
ShowAd(ad, category);
}

void AdsImpl::OnGetAdForSampleCategory(
void AdsImpl::OnGetAdSampleBundle(
const Result result,
const std::string& category,
const AdInfo& ad) {
const std::string& sample_bundle_json) {
if (result == Result::FAILED) {
LOG(LogLevel::WARNING) << "Could not load sample bundle";
return;
}

BundleState sample_bundle_state;
if (!sample_bundle_state.LoadFromJson(sample_bundle_json,
ads_client_->LoadSchema("bundle"))) {
LOG(LogLevel::WARNING) << "Invalid data for sample bundle";
return;
}


auto categories = sample_bundle_state.categories.begin();
auto categories_count = sample_bundle_state.categories.size();
if (categories_count == 0) {
LOG(LogLevel::WARNING) << "Sample bundle does not contain any categories";
return;
}

auto category_rand = helper::Math::Random(categories_count - 1);
std::advance(categories, static_cast<long>(category_rand));

auto category = categories->first;
auto ads = categories->second;

auto ads_count = ads.size();
if (ads_count == 0) {
// TODO(Terry Mancey): Implement Log (#44)
// 'Notification not made', { reason: 'no ads for category', category }

LOG(LogLevel::WARNING) << "No ads found for \""
<< category << "\" sample category";

<< category << "\" sample category";
return;
}

auto ad_rand = helper::Math::Random(ads_count - 1);
auto ad = ads.at(ad_rand);

ShowAd(ad, category);
}

Expand Down
6 changes: 2 additions & 4 deletions src/ads_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,8 @@ class AdsImpl : public Ads {
const Result result,
const std::string& category,
const std::vector<AdInfo>& ads);
void OnGetAdForSampleCategory(
const Result result,
const std::string& category,
const AdInfo& ad);
void OnGetAdSampleBundle(const Result result,
const std::string& sample_bundle_json);

uint32_t collect_activity_timer_id_;
void CollectActivity();
Expand Down
2 changes: 1 addition & 1 deletion src/catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Catalog::~Catalog() {}
bool Catalog::FromJson(const std::string& json) {
auto catalog_state = std::make_unique<CatalogState>();

auto jsonSchema = ads_client_->Load("catalog-schema.json");
auto jsonSchema = ads_client_->LoadSchema("catalog");
if (!LoadFromJson(*catalog_state, json, jsonSchema)) {
LOG(LogLevel::ERROR) << "Failed to parse catalog: " << json;
return false;
Expand Down

0 comments on commit fe2923d

Please # to comment.