Skip to content

Commit

Permalink
Merge pull request #1365 from brave/fix-2914-2
Browse files Browse the repository at this point in the history
Adds Brave Ads launch notification for existing Rewards users
  • Loading branch information
ryanml authored Apr 20, 2019
2 parents e71bcdd + e14c8ea commit ed63eb6
Show file tree
Hide file tree
Showing 25 changed files with 557 additions and 18 deletions.
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
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",
]
}
9 changes: 8 additions & 1 deletion components/brave_ads/browser/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#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 +76,7 @@ content::BrowserContext* AdsServiceFactory::GetBrowserContextToUse(
}

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

void AdsServiceFactory::RegisterProfilePrefs(
Expand Down Expand Up @@ -103,6 +104,12 @@ 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
7 changes: 4 additions & 3 deletions components/brave_ads/browser/ads_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
144 changes: 144 additions & 0 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
#include "brave/components/brave_ads/common/switches.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/services/bat_ads/public/cpp/ads_client_mojo_bridge.h"
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -60,8 +63,17 @@
#include "net/android/network_library.h"
#endif

using brave_rewards::RewardsNotificationService;

namespace brave_ads {

namespace {

const char kRewardsNotificationAdsLaunch[] =
"rewards_notification_ads_launch";

}

class LogStreamImpl : public ads::LogStream {
public:
LogStreamImpl(const char* file,
Expand Down Expand Up @@ -258,6 +270,7 @@ AdsServiceImpl::AdsServiceImpl(Profile* profile)
base::TaskShutdownBehavior::BLOCK_SHUTDOWN})),
base_path_(profile_->GetPath().AppendASCII("ads_service")),
next_timer_id_(0),
ads_launch_id_(0),
is_supported_region_(false),
bundle_state_backend_(
new BundleStateDatabase(base_path_.AppendASCII("bundle_state"))),
Expand All @@ -273,6 +286,7 @@ AdsServiceImpl::AdsServiceImpl(Profile* profile)

file_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&EnsureBaseDirectoryExists, base_path_));

profile_pref_change_registrar_.Init(profile_->GetPrefs());
profile_pref_change_registrar_.Add(
prefs::kBraveAdsEnabled,
Expand Down Expand Up @@ -340,6 +354,8 @@ void AdsServiceImpl::OnMaybeStartForRegion(
return;
}

MaybeShowFirstLaunchNotification();

if (IsAdsEnabled()) {
if (should_restart) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE,
Expand Down Expand Up @@ -412,6 +428,131 @@ void AdsServiceImpl::Start() {
base::BindOnce(&AdsServiceImpl::OnCreate, AsWeakPtr()));
}

void AdsServiceImpl::MaybeShowFirstLaunchNotification() {
auto ads_enabled = profile_->GetPrefs()->GetBoolean(
prefs::kBraveAdsEnabled);
auto prefs_migrated_from_62 = profile_->GetPrefs()->GetBoolean(
prefs::kBraveAdsPrefsMigratedFrom62);

if (!ads_enabled ||
!prefs_migrated_from_62 ||
!ShouldShowFirstLaunchNotification() ||
!profile_->GetPrefs()->GetBoolean(
brave_rewards::prefs::kBraveRewardsEnabled)) {
StartFirstLaunchNotificationTimer();
return;
}

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

ShowFirstLaunchNotification();

StartFirstLaunchNotificationTimer();
}

bool AdsServiceImpl::ShouldShowFirstLaunchNotification() {
return profile_->GetPrefs()->GetBoolean(
prefs::kBraveAdShouldShowFirstLaunchNotification);
}

void AdsServiceImpl::RemoveFirstLaunchNotification() {
KillTimer(ads_launch_id_);

auto* rewards_service =
brave_rewards::RewardsServiceFactory::GetForProfile(profile_);
auto* rewards_notification_service =
rewards_service->GetNotificationService();
rewards_notification_service->DeleteNotification(
kRewardsNotificationAdsLaunch);
}

void AdsServiceImpl::ShowFirstLaunchNotification() {
auto* rewards_service =
brave_rewards::RewardsServiceFactory::GetForProfile(profile_);

auto* rewards_notification_service =
rewards_service->GetNotificationService();

RewardsNotificationService::RewardsNotificationArgs args;
rewards_notification_service->AddNotification(
RewardsNotificationService::REWARDS_NOTIFICATION_ADS_LAUNCH,
args, kRewardsNotificationAdsLaunch);

profile_->GetPrefs()->SetBoolean(
prefs::kBraveAdShouldShowFirstLaunchNotification, false);
}

void AdsServiceImpl::StartFirstLaunchNotificationTimer() {
uint64_t timer_offset_in_seconds;

if (HasFirstLaunchNotificationExpired()) {
timer_offset_in_seconds = base::Time::kSecondsPerMinute;
} else {
timer_offset_in_seconds = GetFirstLaunchNotificationTimerOffset();
}

auto timer_offset = base::TimeDelta::FromSeconds(timer_offset_in_seconds);

uint32_t timer_id = next_timer_id();
ads_launch_id_ = timer_id;
timers_[timer_id] = std::make_unique<base::OneShotTimer>();
timers_[timer_id]->Start(FROM_HERE,
timer_offset,
base::BindOnce(&AdsServiceImpl::OnFirstLaunchNotificationTimedOut,
AsWeakPtr(),
timer_id));
}

uint64_t AdsServiceImpl::GetFirstLaunchNotificationTimeout() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
auto timeout_in_seconds =
command_line.HasSwitch(switches::kDebug)
? (5 * base::Time::kSecondsPerMinute)
: (base::Time::kMicrosecondsPerWeek /
base::Time::kMicrosecondsPerSecond);
return timeout_in_seconds;
}

uint64_t AdsServiceImpl::GetFirstLaunchNotificationTimerOffset() {
auto timeout_in_seconds = GetFirstLaunchNotificationTimeout();

auto timestamp_in_seconds = profile_->GetPrefs()->GetUint64(
prefs::kBraveAdsLaunchNotificationTimestamp);

auto now_in_seconds = static_cast<uint64_t>(
(base::Time::Now() - base::Time()).InSeconds());

auto timer_offset = (timestamp_in_seconds +
timeout_in_seconds) - now_in_seconds;

return timer_offset;
}

bool AdsServiceImpl::HasFirstLaunchNotificationExpired() {
auto timeout_in_seconds = GetFirstLaunchNotificationTimeout();

auto timestamp_in_seconds = profile_->GetPrefs()->GetUint64(
prefs::kBraveAdsLaunchNotificationTimestamp);

auto now_in_seconds = static_cast<uint64_t>(
(base::Time::Now() - base::Time()).InSeconds());

if (now_in_seconds < (timestamp_in_seconds + timeout_in_seconds)) {
return false;
}

return true;
}

void AdsServiceImpl::OnFirstLaunchNotificationTimedOut(uint32_t timer_id) {
timers_.erase(timer_id);
RemoveFirstLaunchNotification();
}

void AdsServiceImpl::Stop() {
if (connected()) {
// this is kind of weird, but we need to call Initialize on disable too
Expand Down Expand Up @@ -610,7 +751,10 @@ void AdsServiceImpl::SetAdsEnabled(const bool is_enabled) {
if (is_enabled) {
profile_->GetPrefs()->SetBoolean(
prefs::kBraveAdsPrefsMigratedFrom62, false);

RemoveFirstLaunchNotification();
}

profile_->GetPrefs()->SetBoolean(prefs::kBraveAdsEnabled, is_enabled);
}

Expand Down
14 changes: 14 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/brave_ads/browser/background_helper.h"
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#include "brave/components/brave_rewards/browser/rewards_notification_service_observer.h"
#include "chrome/browser/notifications/notification_handler.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/prefs/pref_change_registrar.h"
Expand All @@ -29,6 +30,8 @@
#include "ui/base/idle/idle.h"
#endif

using brave_rewards::RewardsNotificationService;

class NotificationDisplayService;
class Profile;

Expand Down Expand Up @@ -171,6 +174,7 @@ class AdsServiceImpl : public AdsService,
const ads::OnGetAdsCallback& callback,
const std::string& category,
const std::vector<ads::AdInfo>& ads);

void OnSaveBundleState(const ads::OnSaveCallback& callback, bool success);
void OnLoaded(
const ads::OnLoadCallback& callback,
Expand All @@ -195,6 +199,15 @@ class AdsServiceImpl : public AdsService,
void NotificationTimedOut(
uint32_t timer_id,
const std::string& notification_id);
void MaybeShowFirstLaunchNotification();
bool ShouldShowFirstLaunchNotification();
void RemoveFirstLaunchNotification();
void ShowFirstLaunchNotification();
void StartFirstLaunchNotificationTimer();
uint64_t GetFirstLaunchNotificationTimeout();
uint64_t GetFirstLaunchNotificationTimerOffset();
bool HasFirstLaunchNotificationExpired();
void OnFirstLaunchNotificationTimedOut(uint32_t timer_id);

uint32_t next_timer_id();

Expand All @@ -206,6 +219,7 @@ class AdsServiceImpl : public AdsService,
const base::FilePath base_path_;
std::map<uint32_t, std::unique_ptr<base::OneShotTimer>> timers_;
uint32_t next_timer_id_;
uint32_t ads_launch_id_;
bool is_supported_region_;
std::unique_ptr<BundleStateDatabase> bundle_state_backend_;
NotificationDisplayService* display_service_; // NOT OWNED
Expand Down
Loading

0 comments on commit ed63eb6

Please sign in to comment.