2
2
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
3
3
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
+ #include < algorithm>
6
+
5
7
#include " brave/components/brave_rewards/browser/rewards_notification_service_impl.h"
6
8
7
9
#include " base/json/json_reader.h"
@@ -33,7 +35,7 @@ RewardsNotificationServiceImpl::RewardsNotificationServiceImpl(Profile* profile)
33
35
profile);
34
36
AddObserver (extension_rewards_notification_service_observer_.get ());
35
37
#endif
36
- ReadRewardsNotifications ();
38
+ ReadRewardsNotificationsJSON ();
37
39
}
38
40
39
41
RewardsNotificationServiceImpl::~RewardsNotificationServiceImpl () {
@@ -46,14 +48,28 @@ RewardsNotificationServiceImpl::~RewardsNotificationServiceImpl() {
46
48
void RewardsNotificationServiceImpl::AddNotification (
47
49
RewardsNotificationType type,
48
50
RewardsNotificationArgs args,
49
- RewardsNotificationID id) {
51
+ RewardsNotificationID id,
52
+ bool only_once) {
50
53
DCHECK (type != REWARDS_NOTIFICATION_INVALID);
51
- if (id.empty ())
54
+ if (id.empty ()) {
52
55
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
+
53
65
RewardsNotification rewards_notification (
54
66
id, type, GenerateRewardsNotificationTimestamp (), std::move (args));
55
67
rewards_notifications_[id] = rewards_notification;
56
68
OnNotificationAdded (rewards_notification);
69
+
70
+ if (only_once) {
71
+ rewards_notifications_displayed_.push_back (id);
72
+ }
57
73
}
58
74
59
75
void RewardsNotificationServiceImpl::DeleteNotification (RewardsNotificationID id) {
@@ -96,16 +112,47 @@ RewardsNotificationServiceImpl::GenerateRewardsNotificationTimestamp() const {
96
112
return base::Time::NowFromSystemTime ().ToTimeT ();
97
113
}
98
114
99
- void RewardsNotificationServiceImpl::ReadRewardsNotifications () {
115
+ void RewardsNotificationServiceImpl::ReadRewardsNotificationsJSON () {
100
116
std::string json = profile_->GetPrefs ()->GetString (kRewardsNotifications );
101
117
if (json.empty ())
102
118
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 =
104
125
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" , ¬ifications);
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) {
105
152
if (!root) {
106
- LOG (ERROR) << " Failed to deserialize rewards notifications on startup" ;
107
153
return ;
108
154
}
155
+
109
156
for (auto it = root->begin (); it != root->end (); ++it) {
110
157
if (!it->is_dict ())
111
158
continue ;
@@ -150,8 +197,9 @@ void RewardsNotificationServiceImpl::ReadRewardsNotifications() {
150
197
}
151
198
152
199
void RewardsNotificationServiceImpl::StoreRewardsNotifications () {
153
- base::ListValue root;
200
+ base::DictionaryValue root;
154
201
202
+ auto notifications = std::make_unique<base::ListValue>();
155
203
for (auto & item : rewards_notifications_) {
156
204
auto dict = std::make_unique<base::DictionaryValue>();
157
205
dict->SetString (" id" , item.second .id_ );
@@ -162,9 +210,17 @@ void RewardsNotificationServiceImpl::StoreRewardsNotifications() {
162
210
args->AppendString (arg);
163
211
}
164
212
dict->SetList (" args" , std::move (args));
165
- root. Append (std::move (dict));
213
+ notifications-> Append (std::move (dict));
166
214
}
167
215
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
+
168
224
std::string result;
169
225
if (!base::JSONWriter::Write (root, &result)) {
170
226
LOG (ERROR) << " Failed to serialize rewards notifications on shutdown" ;
@@ -234,14 +290,17 @@ if (error_code == ledger::Result::LEDGER_OK) {
234
290
RewardsNotificationService::RewardsNotificationArgs args;
235
291
AddNotification (RewardsNotificationService::REWARDS_NOTIFICATION_GRANT,
236
292
args,
237
- " rewards_notification_grant" );
293
+ " rewards_notification_grant_" + properties.promotionId ,
294
+ true );
238
295
}
239
296
}
240
297
241
298
void RewardsNotificationServiceImpl::OnGrantFinish (
242
299
RewardsService* rewards_service,
243
300
unsigned int result,
244
301
Grant grant) {
302
+ DeleteNotification (" rewards_notification_grant_" + grant.promotionId );
303
+ // We keep it for back compatibility
245
304
DeleteNotification (" rewards_notification_grant" );
246
305
}
247
306
0 commit comments