Skip to content

Commit b5c2c39

Browse files
committedDec 20, 2018
Shows grant notification only once
Resolves brave/brave-browser#2454
1 parent 85422d5 commit b5c2c39

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed
 

‎components/brave_rewards/browser/rewards_notification_service.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ class RewardsNotificationService {
5151

5252
virtual void AddNotification(RewardsNotificationType type,
5353
RewardsNotificationArgs args,
54-
RewardsNotificationID id = "") = 0;
54+
RewardsNotificationID id = "",
55+
bool only_once = false) = 0;
5556
virtual void DeleteNotification(RewardsNotificationID id) = 0;
5657
virtual void DeleteAllNotifications() = 0;
5758
virtual void GetNotification(RewardsNotificationID id) = 0;
5859
virtual void GetAllNotifications() = 0;
5960

60-
virtual void ReadRewardsNotifications() = 0;
61+
virtual void ReadRewardsNotificationsJSON() = 0;
6162
virtual void StoreRewardsNotifications() = 0;
6263

6364
void AddObserver(RewardsNotificationServiceObserver* observer);

‎components/brave_rewards/browser/rewards_notification_service_impl.cc

+68-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
33
* You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
#include <algorithm>
6+
57
#include "brave/components/brave_rewards/browser/rewards_notification_service_impl.h"
68

79
#include "base/json/json_reader.h"
@@ -33,7 +35,7 @@ RewardsNotificationServiceImpl::RewardsNotificationServiceImpl(Profile* profile)
3335
profile);
3436
AddObserver(extension_rewards_notification_service_observer_.get());
3537
#endif
36-
ReadRewardsNotifications();
38+
ReadRewardsNotificationsJSON();
3739
}
3840

3941
RewardsNotificationServiceImpl::~RewardsNotificationServiceImpl() {
@@ -46,14 +48,28 @@ RewardsNotificationServiceImpl::~RewardsNotificationServiceImpl() {
4648
void RewardsNotificationServiceImpl::AddNotification(
4749
RewardsNotificationType type,
4850
RewardsNotificationArgs args,
49-
RewardsNotificationID id) {
51+
RewardsNotificationID id,
52+
bool only_once) {
5053
DCHECK(type != REWARDS_NOTIFICATION_INVALID);
51-
if (id.empty())
54+
if (id.empty()) {
5255
id = GenerateRewardsNotificationID();
56+
} else if (only_once) {
57+
if (std::find(
58+
rewards_notifications_displayed_.begin(),
59+
rewards_notifications_displayed_.end(),
60+
id) != rewards_notifications_displayed_.end()) {
61+
return;
62+
}
63+
}
64+
5365
RewardsNotification rewards_notification(
5466
id, type, GenerateRewardsNotificationTimestamp(), std::move(args));
5567
rewards_notifications_[id] = rewards_notification;
5668
OnNotificationAdded(rewards_notification);
69+
70+
if (only_once) {
71+
rewards_notifications_displayed_.push_back(id);
72+
}
5773
}
5874

5975
void RewardsNotificationServiceImpl::DeleteNotification(RewardsNotificationID id) {
@@ -96,16 +112,47 @@ RewardsNotificationServiceImpl::GenerateRewardsNotificationTimestamp() const {
96112
return base::Time::NowFromSystemTime().ToTimeT();
97113
}
98114

99-
void RewardsNotificationServiceImpl::ReadRewardsNotifications() {
115+
void RewardsNotificationServiceImpl::ReadRewardsNotificationsJSON() {
100116
std::string json = profile_->GetPrefs()->GetString(kRewardsNotifications);
101117
if (json.empty())
102118
return;
103-
std::unique_ptr<base::ListValue> root =
119+
std::unique_ptr<base::DictionaryValue> dictionary =
120+
base::DictionaryValue::From(base::JSONReader::Read(json));
121+
122+
// legacy read
123+
if (!dictionary || !dictionary->is_dict()) {
124+
std::unique_ptr<base::ListValue> list =
104125
base::ListValue::From(base::JSONReader::Read(json));
126+
if (!list) {
127+
LOG(ERROR) << "Failed to deserialize rewards notifications on startup";
128+
return;
129+
}
130+
131+
ReadRewardsNotifications(std::move(list));
132+
return;
133+
}
134+
135+
base::ListValue* notifications;
136+
dictionary->GetList("notifications", &notifications);
137+
auto unique = std::make_unique<base::ListValue>(notifications->GetList());
138+
ReadRewardsNotifications(std::move(unique));
139+
140+
base::ListValue* displayed;
141+
dictionary->GetList("displayed", &displayed);
142+
if (displayed && displayed->is_list()) {
143+
for (auto& it : *displayed) {
144+
rewards_notifications_displayed_.push_back(it.GetString());
145+
}
146+
}
147+
148+
}
149+
150+
void RewardsNotificationServiceImpl::ReadRewardsNotifications(
151+
std::unique_ptr<base::ListValue> root) {
105152
if (!root) {
106-
LOG(ERROR) << "Failed to deserialize rewards notifications on startup";
107153
return;
108154
}
155+
109156
for (auto it = root->begin(); it != root->end(); ++it) {
110157
if (!it->is_dict())
111158
continue;
@@ -150,8 +197,9 @@ void RewardsNotificationServiceImpl::ReadRewardsNotifications() {
150197
}
151198

152199
void RewardsNotificationServiceImpl::StoreRewardsNotifications() {
153-
base::ListValue root;
200+
base::DictionaryValue root;
154201

202+
auto notifications = std::make_unique<base::ListValue>();
155203
for (auto& item : rewards_notifications_) {
156204
auto dict = std::make_unique<base::DictionaryValue>();
157205
dict->SetString("id", item.second.id_);
@@ -162,9 +210,17 @@ void RewardsNotificationServiceImpl::StoreRewardsNotifications() {
162210
args->AppendString(arg);
163211
}
164212
dict->SetList("args", std::move(args));
165-
root.Append(std::move(dict));
213+
notifications->Append(std::move(dict));
166214
}
167215

216+
auto displayed = std::make_unique<base::ListValue>();
217+
for (auto& item : rewards_notifications_displayed_) {
218+
displayed->AppendString(item);
219+
}
220+
221+
root.SetList("notifications", std::move(notifications));
222+
root.SetList("displayed", std::move(displayed));
223+
168224
std::string result;
169225
if (!base::JSONWriter::Write(root, &result)) {
170226
LOG(ERROR) << "Failed to serialize rewards notifications on shutdown";
@@ -234,14 +290,17 @@ if (error_code == ledger::Result::LEDGER_OK) {
234290
RewardsNotificationService::RewardsNotificationArgs args;
235291
AddNotification(RewardsNotificationService::REWARDS_NOTIFICATION_GRANT,
236292
args,
237-
"rewards_notification_grant");
293+
"rewards_notification_grant_" + properties.promotionId,
294+
true);
238295
}
239296
}
240297

241298
void RewardsNotificationServiceImpl::OnGrantFinish(
242299
RewardsService* rewards_service,
243300
unsigned int result,
244301
Grant grant) {
302+
DeleteNotification("rewards_notification_grant_" + grant.promotionId);
303+
// We keep it for back compatibility
245304
DeleteNotification("rewards_notification_grant");
246305
}
247306

‎components/brave_rewards/browser/rewards_notification_service_impl.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <memory>
99
#include <string>
1010

11+
#include "base/values.h"
1112
#include "brave/components/brave_rewards/browser/rewards_notification_service.h"
1213
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
1314
#include "extensions/buildflags/buildflags.h"
@@ -28,13 +29,15 @@ class RewardsNotificationServiceImpl
2829

2930
void AddNotification(RewardsNotificationType type,
3031
RewardsNotificationArgs args,
31-
RewardsNotificationID id = "") override;
32+
RewardsNotificationID id = "",
33+
bool only_once = false) override;
3234
void DeleteNotification(RewardsNotificationID id) override;
3335
void DeleteAllNotifications() override;
3436
void GetNotification(RewardsNotificationID id) override;
3537
void GetAllNotifications() override;
3638

37-
void ReadRewardsNotifications() override;
39+
void ReadRewardsNotificationsJSON() override;
40+
void ReadRewardsNotifications(std::unique_ptr<base::ListValue>);
3841
void StoreRewardsNotifications() override;
3942

4043
private:
@@ -73,6 +76,7 @@ class RewardsNotificationServiceImpl
7376

7477
Profile* profile_;
7578
std::map<RewardsNotificationID, RewardsNotification> rewards_notifications_;
79+
std::vector<RewardsNotificationID> rewards_notifications_displayed_;
7680
#if BUILDFLAG(ENABLE_EXTENSIONS)
7781
std::unique_ptr<ExtensionRewardsNotificationServiceObserver>
7882
extension_rewards_notification_service_observer_;

0 commit comments

Comments
 (0)
Please sign in to comment.