Skip to content

Commit

Permalink
Remove region support for Ads as CDN now controls catalog for each re…
Browse files Browse the repository at this point in the history
  • Loading branch information
tmancey authored Apr 2, 2019
1 parent 995a9cd commit 3aaa92c
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 94 deletions.
18 changes: 1 addition & 17 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ std::string LoadOnFileTaskRunner(
}

std::vector<ads::AdInfo> GetAdsForCategoryOnFileTaskRunner(
const std::string region,
const std::string category,
BundleStateDatabase* backend) {
std::vector<ads::AdInfo> ads;
if (!backend)
return ads;

backend->GetAdsForCategory(region, category, ads);
backend->GetAdsForCategory(category, &ads);

return ads;
}
Expand Down Expand Up @@ -374,12 +373,6 @@ void AdsServiceImpl::Start() {
if (command_line.HasSwitch(switches::kTesting)) {
is_testing = true;
}
if (command_line.HasSwitch(switches::kLocale)) {
std::string locale = command_line.GetSwitchValueASCII(switches::kLocale);
if (!locale.empty()) {
command_line_switch_ads_locale_ = locale;
}
}

bat_ads_service_->SetProduction(is_production, base::NullCallback());
bat_ads_service_->SetDebug(is_debug, base::NullCallback());
Expand Down Expand Up @@ -709,31 +702,26 @@ void AdsServiceImpl::OnReset(const ads::OnResetCallback& callback,
}

void AdsServiceImpl::GetAds(
const std::string& region,
const std::string& category,
ads::OnGetAdsCallback callback) {
base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
base::BindOnce(&GetAdsForCategoryOnFileTaskRunner,
region,
category,
bundle_state_backend_.get()),
base::BindOnce(&AdsServiceImpl::OnGetAdsForCategory,
AsWeakPtr(),
std::move(callback),
region,
category));
}

void AdsServiceImpl::OnGetAdsForCategory(
const ads::OnGetAdsCallback& callback,
const std::string& region,
const std::string& category,
const std::vector<ads::AdInfo>& ads) {
if (!connected())
return;

callback(ads.empty() ? ads::Result::FAILED : ads::Result::SUCCESS,
region,
category,
ads);
}
Expand Down Expand Up @@ -868,10 +856,6 @@ const std::vector<std::string> AdsServiceImpl::GetLocales() const {
}

const std::string AdsServiceImpl::GetAdsLocale() const {
if (!command_line_switch_ads_locale_.empty()) {
return command_line_switch_ads_locale_;
}

return g_browser_process->GetApplicationLocale();
}

Expand Down
4 changes: 0 additions & 4 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class AdsServiceImpl : public AdsService,
void Reset(const std::string& name,
ads::OnResetCallback callback) override;
void GetAds(
const std::string& region,
const std::string& category,
ads::OnGetAdsCallback callback) override;
void LoadSampleBundle(ads::OnLoadSampleBundleCallback callback) override;
Expand Down Expand Up @@ -161,7 +160,6 @@ class AdsServiceImpl : public AdsService,
void OnForeground() override;

void OnGetAdsForCategory(const ads::OnGetAdsCallback& callback,
const std::string& region,
const std::string& category,
const std::vector<ads::AdInfo>& ads);
void OnSaveBundleState(const ads::OnSaveCallback& callback, bool success);
Expand Down Expand Up @@ -206,8 +204,6 @@ class AdsServiceImpl : public AdsService,
NotificationInfoMap notification_ids_;
std::map<const net::URLFetcher*, ads::URLRequestCallback> fetchers_;

std::string command_line_switch_ads_locale_;

DISALLOW_COPY_AND_ASSIGN(AdsServiceImpl);
};

Expand Down
10 changes: 4 additions & 6 deletions components/brave_ads/browser/bundle_state_database.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ bool BundleStateDatabase::InsertOrUpdateAdInfoCategory(
return ad_info_statement.Run();
}

bool BundleStateDatabase::GetAdsForCategory(const std::string& region,
const std::string& category,
std::vector<ads::AdInfo>& ads) {
bool BundleStateDatabase::GetAdsForCategory(
const std::string& category,
std::vector<ads::AdInfo>* ads) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

bool initialized = Init();
Expand All @@ -348,13 +348,11 @@ bool BundleStateDatabase::GetAdsForCategory(const std::string& region,
"INNER JOIN ad_info_category AS aic "
"ON aic.ad_info_uuid = ai.uuid "
"WHERE aic.category_name = ? and "
"ai.region = ? and "
"ai.start_timestamp <= strftime('%Y-%m-%d %H:%M', "
"datetime('now','localtime')) and "
"ai.end_timestamp >= strftime('%Y-%m-%d %H:%M', "
"datetime('now','localtime'));"));
info_sql.BindString(0, category);
info_sql.BindString(1, region);

while (info_sql.Step()) {
ads::AdInfo info;
Expand All @@ -369,7 +367,7 @@ bool BundleStateDatabase::GetAdsForCategory(const std::string& region,
info.daily_cap = info_sql.ColumnInt(9);
info.per_day = info_sql.ColumnInt(10);
info.total_max = info_sql.ColumnInt(11);
ads.emplace_back(info);
ads->emplace_back(info);
}

return true;
Expand Down
28 changes: 16 additions & 12 deletions components/brave_ads/browser/bundle_state_database.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */

#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_DATA_DATABASE_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_DATA_DATABASE_H_
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_STATE_DATABASE_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_STATE_DATABASE_H_

#include <memory>
#include <stddef.h>
#include <string>
#include <vector>
#include <memory>

#include "bat/ads/ad_info.h"
#include "bat/ads/bundle_state.h"
Expand All @@ -24,7 +27,7 @@ namespace brave_ads {

class BundleStateDatabase {
public:
BundleStateDatabase(const base::FilePath& db_path);
explicit BundleStateDatabase(const base::FilePath& db_path);
~BundleStateDatabase();

// Call before Init() to set the error callback to be used for the
Expand All @@ -34,9 +37,9 @@ class BundleStateDatabase {
}

bool SaveBundleState(const ads::BundleState& bundle_state);
bool GetAdsForCategory(const std::string& region,
const std::string& category,
std::vector<ads::AdInfo>& ads);
bool GetAdsForCategory(
const std::string& category,
std::vector<ads::AdInfo>* ads);

// Returns the current version of the publisher info database
static int GetCurrentVersion();
Expand All @@ -50,7 +53,7 @@ class BundleStateDatabase {
private:
bool Init();
void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);

bool CreateCategoryTable();
bool CreateAdInfoTable();
Expand All @@ -63,8 +66,9 @@ class BundleStateDatabase {

bool InsertOrUpdateCategory(const std::string& category);
bool InsertOrUpdateAdInfo(const ads::AdInfo& info);
bool InsertOrUpdateAdInfoCategory(const ads::AdInfo& ad_info,
const std::string& category);
bool InsertOrUpdateAdInfoCategory(
const ads::AdInfo& ad_info,
const std::string& category);

sql::Database& GetDB();
sql::MetaTable& GetMetaTable();
Expand All @@ -85,4 +89,4 @@ class BundleStateDatabase {

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_DATA_DATABASE_H_
#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_BUNDLE_STATE_DATABASE_H_
4 changes: 2 additions & 2 deletions components/brave_ads/common/switches.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */

Expand All @@ -10,6 +11,5 @@ const char kStaging[] = "brave-ads-staging";
const char kProduction[] = "brave-ads-production";
const char kDebug[] = "brave-ads-debug";
const char kTesting[] = "brave-ads-testing";
const char kLocale[] = "brave-ads-locale";
} // namespace switches
} // namespace brave_ads
10 changes: 7 additions & 3 deletions components/brave_ads/common/switches.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */

#ifndef BRAVE_COMPONENTS_BRAVE_ADS_COMMON_SWITCHES_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_COMMON_SWITCHES_H_

namespace brave_ads {

namespace switches {

extern const char kStaging[];
extern const char kProduction[];
extern const char kDebug[];
extern const char kTesting[];
extern const char kLocale[];

} // namespace switches
} // namespace apps

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_COMMON_SWITCHES_H_
13 changes: 5 additions & 8 deletions components/services/bat_ads/bat_ads_client_mojo_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,6 @@ void BatAdsClientMojoBridge::Reset(const std::string& name,

void OnGetAds(const ads::OnGetAdsCallback& callback,
int32_t result,
const std::string& region,
const std::string& category,
const std::vector<std::string>& ad_info_json_list) {
std::vector<ads::AdInfo> ad_info_list;
Expand All @@ -301,26 +300,24 @@ void OnGetAds(const ads::OnGetAdsCallback& callback,
if (ad_info.FromJson(it) == ads::Result::SUCCESS) {
ad_info_list.push_back(ad_info);
} else {
callback(
ads::Result::FAILED, region, category, std::vector<ads::AdInfo>());
callback(ads::Result::FAILED, category, std::vector<ads::AdInfo>());
return;
}
}

callback(ToAdsResult(result), region, category, ad_info_list);
callback(ToAdsResult(result), category, ad_info_list);
}

void BatAdsClientMojoBridge::GetAds(
const std::string& region,
const std::string& category,
ads::OnGetAdsCallback callback) {
if (!connected()) {
callback(ads::Result::FAILED, region, category, std::vector<ads::AdInfo>());
callback(ads::Result::FAILED, category, std::vector<ads::AdInfo>());
return;
}

bat_ads_client_->GetAds(region, category,
base::BindOnce(&OnGetAds, std::move(callback)));
bat_ads_client_->GetAds(category, base::BindOnce(&OnGetAds,
std::move(callback)));
}

void OnLoadSampleBundle(const ads::OnLoadSampleBundleCallback& callback,
Expand Down
1 change: 0 additions & 1 deletion components/services/bat_ads/bat_ads_client_mojo_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class BatAdsClientMojoBridge : public ads::AdsClient {
void Reset(const std::string& name,
ads::OnResetCallback callback) override;
void GetAds(
const std::string& region,
const std::string& category,
ads::OnGetAdsCallback callback) override;
void LoadSampleBundle(ads::OnLoadSampleBundleCallback callback) override;
Expand Down
18 changes: 9 additions & 9 deletions components/services/bat_ads/public/cpp/ads_client_mojo_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
#include "base/containers/flat_map.h"
#include "bat/ads/ads.h"

using namespace std::placeholders;
using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

namespace bat_ads {

Expand Down Expand Up @@ -351,29 +353,27 @@ void AdsClientMojoBridge::SaveBundleState(const std::string& bundle_state_json,
void AdsClientMojoBridge::OnGetAds(
CallbackHolder<GetAdsCallback>* holder,
ads::Result result,
const std::string& region,
const std::string& category,
const std::vector<ads::AdInfo>& ad_info) {
if (holder->is_valid()) {
std::vector<std::string> ad_info_json;
for (const auto it : ad_info) {
ad_info_json.push_back(it.ToJson());
}
std::move(holder->get()).Run(
ToMojomResult(result), region, category, ad_info_json);
std::move(holder->get()).Run(ToMojomResult(result), category, ad_info_json);
}
delete holder;
}

void AdsClientMojoBridge::GetAds(const std::string& region,
const std::string& category,
GetAdsCallback callback) {
void AdsClientMojoBridge::GetAds(
const std::string& category,
GetAdsCallback callback) {
// this gets deleted in OnSaveBundleState
auto* holder = new CallbackHolder<GetAdsCallback>(
AsWeakPtr(), std::move(callback));

ads_client_->GetAds(region, category,
std::bind(AdsClientMojoBridge::OnGetAds, holder, _1, _2, _3, _4));
ads_client_->GetAds(category, std::bind(AdsClientMojoBridge::OnGetAds,
holder, _1, _2, _3));
}

} // namespace bat_ads
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class AdsClientMojoBridge : public mojom::BatAdsClient,
void ConfirmAd(const std::string& notification_info) override;
void SaveBundleState(const std::string& bundle_state,
SaveBundleStateCallback callback) override;
void GetAds(const std::string& region,
const std::string& category,
void GetAds(const std::string& category,
GetAdsCallback callback) override;

private:
Expand Down Expand Up @@ -123,7 +122,6 @@ class AdsClientMojoBridge : public mojom::BatAdsClient,
static void OnGetAds(
CallbackHolder<GetAdsCallback>* holder,
ads::Result result,
const std::string& region,
const std::string& category,
const std::vector<ads::AdInfo>& ad_info);

Expand Down
4 changes: 2 additions & 2 deletions components/services/bat_ads/public/interfaces/bat_ads.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ interface BatAdsClient {
SetCatalogIssuers(string issuers_info);
ConfirmAd(string notification_info);
SaveBundleState(string bundle_state) => (int32 result);
GetAds(string region, string category) =>
(int32 result, string region, string category, array<string> ad_info);
GetAds(string category) =>
(int32 result, string category, array<string> ad_info);
};

interface BatAds {
Expand Down
9 changes: 1 addition & 8 deletions vendor/bat-native-ads/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,9 @@ void LoadSampleBundle(OnLoadSampleBundleCallback callback)
void Reset(const std::string& name, OnResetCallback callback)
```

`GetAds` should get Ads for the specified region and category from the previously persisted bundle state
`GetAds` should get Ads for the specified category from the previously persisted bundle state
```
void GetAds(
const std::string& region,
const std::string& category,
OnGetAdsCallback callback)
```
Expand Down Expand Up @@ -302,12 +301,6 @@ Use staging Ads Serve as defined by `STAGING_SERVER` in `static_values.h`. Defau
--brave-ads-staging
```

Change locale. Default is locale of operating system. Where `{locale}`, i.e. `en_US`

```
--brave-ads-locale={locale}
```

Collect initial activity after 25 seconds instead of 1 hour as defined by `kDebugOneHourInSeconds` in `static_values.h`

```
Expand Down
Loading

0 comments on commit 3aaa92c

Please sign in to comment.