Skip to content

Commit

Permalink
Adds Brave Ads launch notification for existing Rewards users
Browse files Browse the repository at this point in the history
Fixes #1365
  • Loading branch information
NejcZdovc authored and tmancey committed Apr 21, 2019
1 parent 19f551b commit a0b18f7
Show file tree
Hide file tree
Showing 36 changed files with 871 additions and 94 deletions.
36 changes: 36 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -455,5 +455,41 @@ void BraveRewardsRefreshPublisherFunction::OnRefreshPublisher(
std::make_unique<base::Value>(publisher_key)));
}

BraveRewardsGetAllNotificationsFunction::
~BraveRewardsGetAllNotificationsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetAllNotificationsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

auto list = std::make_unique<base::ListValue>();

if (!rewards_service) {
return RespondNow(OneArgument(std::move(list)));
}

auto notifications = rewards_service->GetAllNotifications();

for (auto const& notification : notifications) {
auto item = std::make_unique<base::DictionaryValue>();
item->SetString("id", notification.second.id_);
item->SetInteger("type", notification.second.type_);
item->SetInteger("timestamp", notification.second.timestamp_);

auto args = std::make_unique<base::ListValue>();
for (auto const& arg : notification.second.args_) {
args->AppendString(arg);
}

item->SetList("args", std::move(args));
list->Append(std::move(item));
}

return RespondNow(OneArgument(std::move(list)));
}

} // namespace api
} // namespace extensions
11 changes: 11 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ class BraveRewardsRefreshPublisherFunction : public UIThreadExtensionFunction {
void OnRefreshPublisher(bool verified, const std::string& publisher_key);
};

class BraveRewardsGetAllNotificationsFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getAllNotifications", UNKNOWN)

protected:
~BraveRewardsGetAllNotificationsFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
3 changes: 3 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "amount", IDS_BRAVE_UI_AMOUNT },
{ "backup", IDS_BRAVE_UI_BACKUP },
{ "braveAdsDesc", IDS_BRAVE_UI_BRAVE_ADS_DESC },
{ "braveAdsLaunchMsg", IDS_BRAVE_UI_BRAVE_ADS_LAUNCH_MSG },
{ "braveAdsLaunchTitle", IDS_BRAVE_UI_BRAVE_ADS_LAUNCH_TITLE },
{ "braveAdsTitle", IDS_BRAVE_UI_BRAVE_ADS_TITLE },
{ "braveContributeDesc", IDS_BRAVE_UI_BRAVE_CONTRIBUTE_DESC },
{ "braveContributeTitle", IDS_BRAVE_UI_BRAVE_CONTRIBUTE_TITLE },
Expand Down Expand Up @@ -426,6 +428,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "tokens", IDS_BRAVE_UI_TOKENS },
{ "total", IDS_BRAVE_UI_TOTAL },
{ "transactions", IDS_BRAVE_UI_TRANSACTIONS },
{ "turnOnAds", IDS_BRAVE_UI_TURN_ON_ADS },
{ "turnOnRewardsDesc", IDS_BRAVE_UI_TURN_ON_REWARDS_DESC },
{ "turnOnRewardsTitle", IDS_BRAVE_UI_TURN_ON_REWARDS_TITLE },
{ "type", IDS_BRAVE_UI_TYPE },
Expand Down
41 changes: 41 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,47 @@
]
}
]
},
{
"name": "getAllNotifications",
"type": "function",
"description": "Gets all notifications",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "list",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "notification id"
},
"type": {
"type": "integer",
"description": "notification type"
},
"timestamp": {
"type": "integer",
"description": "timestamp when notification was created"
},
"args": {
"type": "array",
"description": "notification arguments, additional data",
"items": {
"type": "string"
}
}
}
}
}
]
}
]
}
]
}
Expand Down
39 changes: 39 additions & 0 deletions components/brave_ads/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import("//brave/build/config.gni")
import("//brave/components/brave_ads/browser/buildflags/buildflags.gni")
import("//brave/components/brave_rewards/browser/buildflags/buildflags.gni")

source_set("browser") {
public_deps = [
Expand Down Expand Up @@ -80,3 +82,40 @@ source_set("browser") {
}
}
}

source_set("testutil") {
testonly = true

deps = [
":browser",
"//base",
"//content/public/browser",
"//chrome/test:test_support",
"//testing/gtest",
]

if (brave_rewards_enabled) {
deps += [
"//brave/vendor/bat-native-ledger",
]

configs += [
"//brave/vendor/bat-native-ledger:internal_config"
]
}

if (brave_ads_enabled) {
deps += [
"//brave/vendor/bat-native-ads",
]

configs += [
"//brave/vendor/bat-native-ads:internal_config"
]
}

sources = [
"test_util.cc",
"test_util.h",
]
}
13 changes: 12 additions & 1 deletion components/brave_ads/browser/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
* 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 <memory>

#include "brave/components/brave_ads/browser/ads_service_factory.h"

#include "base/time/time.h"
#include "brave/components/brave_ads/browser/buildflags/buildflags.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/common/pref_names.h"
Expand Down Expand Up @@ -75,7 +78,7 @@ content::BrowserContext* AdsServiceFactory::GetBrowserContextToUse(
}

bool AdsServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
return false;
}

void AdsServiceFactory::RegisterProfilePrefs(
Expand Down Expand Up @@ -103,6 +106,14 @@ void AdsServiceFactory::RegisterProfilePrefs(
#endif

registry->RegisterIntegerPref(prefs::kBraveAdsIdleThreshold, 15);
registry->RegisterBooleanPref(
prefs::kBraveAdShouldShowFirstLaunchNotification,
true);

auto now = static_cast<uint64_t>(
(base::Time::Now() - base::Time()).InSeconds());
registry->RegisterUint64Pref(
prefs::kBraveAdsLaunchNotificationTimestamp, now);

if (should_migrate_prefs_from_62) {
registry->RegisterBooleanPref(prefs::kBraveAdsPrefsMigratedFrom62, true);
Expand Down
11 changes: 6 additions & 5 deletions components/brave_ads/browser/ads_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* 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_ADS_SERVICE_FACTORY_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_ADS_SERVICE_FACTORY_H_
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_ADS_SERVICE_FACTORY_H_
#define BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_ADS_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
Expand All @@ -21,6 +21,9 @@ class AdsServiceFactory : public BrowserContextKeyedServiceFactory {

static AdsServiceFactory* GetInstance();

void RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) override;

private:
friend struct base::DefaultSingletonTraits<AdsServiceFactory>;

Expand All @@ -33,8 +36,6 @@ class AdsServiceFactory : public BrowserContextKeyedServiceFactory {
content::BrowserContext* GetBrowserContextToUse(
content::BrowserContext* context) const override;
bool ServiceIsNULLWhileTesting() const override;
void RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) override;
bool ShouldMigratePrefs(user_prefs::PrefRegistrySyncable* registry) const;
bool ShouldMigratePrefsFrom62(
user_prefs::PrefRegistrySyncable* registry) const;
Expand All @@ -44,4 +45,4 @@ class AdsServiceFactory : public BrowserContextKeyedServiceFactory {

} // namespace brave_ads

#endif // BRAVE_COMPONENTS_BRAVE_ADS_ADS_SERVICE_FACTORY_H_
#endif // BRAVE_COMPONENTS_BRAVE_ADS_BROWSER_ADS_SERVICE_FACTORY_H_
Loading

0 comments on commit a0b18f7

Please sign in to comment.