• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "notification_subscriber_manager.h"
17 
18 #include <algorithm>
19 #include <memory>
20 #include <set>
21 
22 #include "ans_const_define.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "ans_watchdog.h"
26 #include "hitrace_meter_adapter.h"
27 #include "ipc_skeleton.h"
28 #include "os_account_manager.h"
29 #include "remote_death_recipient.h"
30 
31 namespace OHOS {
32 namespace Notification {
33 struct NotificationSubscriberManager::SubscriberRecord {
34     sptr<AnsSubscriberInterface> subscriber {nullptr};
35     std::set<std::string> bundleList_ {};
36     bool subscribedAll {false};
37     int32_t userId {SUBSCRIBE_USER_INIT};
38 };
39 
NotificationSubscriberManager()40 NotificationSubscriberManager::NotificationSubscriberManager()
41 {
42     ANS_LOGI("constructor");
43     notificationSubQueue_ = std::make_shared<ffrt::queue>("NotificationSubscriberMgr");
44     recipient_ = new (std::nothrow)
45         RemoteDeathRecipient(std::bind(&NotificationSubscriberManager::OnRemoteDied, this, std::placeholders::_1));
46     if (recipient_ == nullptr) {
47         ANS_LOGE("Failed to create RemoteDeathRecipient instance");
48     }
49 }
50 
~NotificationSubscriberManager()51 NotificationSubscriberManager::~NotificationSubscriberManager()
52 {
53     ANS_LOGI("deconstructor");
54     subscriberRecordList_.clear();
55 }
56 
ResetFfrtQueue()57 void NotificationSubscriberManager::ResetFfrtQueue()
58 {
59     if (notificationSubQueue_ != nullptr) {
60         notificationSubQueue_.reset();
61     }
62 }
63 
AddSubscriber(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)64 ErrCode NotificationSubscriberManager::AddSubscriber(
65     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo)
66 {
67     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
68     if (subscriber == nullptr) {
69         ANS_LOGE("subscriber is null.");
70         return ERR_ANS_INVALID_PARAM;
71     }
72 
73     sptr<NotificationSubscribeInfo> subInfo = subscribeInfo;
74     if (subInfo == nullptr) {
75         subInfo = new (std::nothrow) NotificationSubscribeInfo();
76         if (subInfo == nullptr) {
77             ANS_LOGE("Failed to create NotificationSubscribeInfo ptr.");
78             return ERR_ANS_NO_MEMORY;
79         }
80     }
81 
82     if (subInfo->GetAppUserId() == SUBSCRIBE_USER_INIT) {
83         int32_t userId = SUBSCRIBE_USER_INIT;
84         int32_t callingUid = IPCSkeleton::GetCallingUid();
85         ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId);
86         if (ret != ERR_OK) {
87             ANS_LOGD("Get userId failed, callingUid = <%{public}d>", callingUid);
88             return ERR_ANS_INVALID_PARAM;
89         }
90 
91         ANS_LOGD("Get userId succeeded, callingUid = <%{public}d> userId = <%{public}d>", callingUid, userId);
92         subInfo->AddAppUserId(userId);
93     }
94 
95     ErrCode result = ERR_ANS_TASK_ERR;
96     if (notificationSubQueue_ == nullptr) {
97         ANS_LOGE("queue is nullptr");
98         return result;
99     }
100     ffrt::task_handle handler = notificationSubQueue_->submit_h(std::bind([this, &subscriber, &subInfo, &result]() {
101         result = this->AddSubscriberInner(subscriber, subInfo);
102     }));
103     notificationSubQueue_->wait(handler);
104     return result;
105 }
106 
RemoveSubscriber(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)107 ErrCode NotificationSubscriberManager::RemoveSubscriber(
108     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo)
109 {
110     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
111     if (subscriber == nullptr) {
112         ANS_LOGE("subscriber is null.");
113         return ERR_ANS_INVALID_PARAM;
114     }
115 
116     ErrCode result = ERR_ANS_TASK_ERR;
117     if (notificationSubQueue_ == nullptr) {
118         ANS_LOGE("queue is nullptr");
119         return result;
120     }
121     ffrt::task_handle handler = notificationSubQueue_->submit_h(std::bind([this, &subscriber,
122         &subscribeInfo, &result]() {
123         ANS_LOGE("ffrt enter!");
124         result = this->RemoveSubscriberInner(subscriber, subscribeInfo);
125     }));
126     notificationSubQueue_->wait(handler);
127     return result;
128 }
129 
NotifyConsumed(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)130 void NotificationSubscriberManager::NotifyConsumed(
131     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
132 {
133     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
134     if (notificationSubQueue_ == nullptr) {
135         ANS_LOGE("queue is nullptr");
136         return;
137     }
138     AppExecFwk::EventHandler::Callback NotifyConsumedFunc =
139         std::bind(&NotificationSubscriberManager::NotifyConsumedInner, this, notification, notificationMap);
140 
141     notificationSubQueue_->submit(NotifyConsumedFunc);
142 }
143 
BatchNotifyConsumed(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,const std::shared_ptr<SubscriberRecord> & record)144 void NotificationSubscriberManager::BatchNotifyConsumed(const std::vector<sptr<Notification>> &notifications,
145     const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record)
146 {
147     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
148     ANS_LOGI("Start batch notifyConsumed.");
149     if (notifications.empty() || notificationMap == nullptr || record == nullptr) {
150         ANS_LOGE("Invalid input.");
151         return;
152     }
153 
154     if (notificationSubQueue_ == nullptr) {
155         ANS_LOGE("Queue is nullptr");
156         return;
157     }
158 
159     AppExecFwk::EventHandler::Callback batchNotifyConsumedFunc = std::bind(
160         &NotificationSubscriberManager::BatchNotifyConsumedInner, this, notifications, notificationMap, record);
161 
162     notificationSubQueue_->submit(batchNotifyConsumedFunc);
163 }
164 
NotifyCanceled(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)165 void NotificationSubscriberManager::NotifyCanceled(
166     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
167 {
168     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
169     if (notificationSubQueue_ == nullptr) {
170         ANS_LOGE("queue is nullptr");
171         return;
172     }
173     AppExecFwk::EventHandler::Callback NotifyCanceledFunc = std::bind(
174         &NotificationSubscriberManager::NotifyCanceledInner, this, notification, notificationMap, deleteReason);
175 
176     notificationSubQueue_->submit(NotifyCanceledFunc);
177 }
178 
BatchNotifyCanceled(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)179 void NotificationSubscriberManager::BatchNotifyCanceled(const std::vector<sptr<Notification>> &notifications,
180     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
181 {
182     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
183     if (notificationSubQueue_ == nullptr) {
184         ANS_LOGD("queue is nullptr");
185         return;
186     }
187 
188     AppExecFwk::EventHandler::Callback NotifyCanceledFunc = std::bind(
189         &NotificationSubscriberManager::BatchNotifyCanceledInner, this, notifications, notificationMap, deleteReason);
190 
191     notificationSubQueue_->submit(NotifyCanceledFunc);
192 }
193 
NotifyUpdated(const sptr<NotificationSortingMap> & notificationMap)194 void NotificationSubscriberManager::NotifyUpdated(const sptr<NotificationSortingMap> &notificationMap)
195 {
196     if (notificationSubQueue_ == nullptr) {
197         ANS_LOGE("queue is nullptr");
198         return;
199     }
200     AppExecFwk::EventHandler::Callback NotifyUpdatedFunc =
201         std::bind(&NotificationSubscriberManager::NotifyUpdatedInner, this, notificationMap);
202 
203     notificationSubQueue_->submit(NotifyUpdatedFunc);
204 }
205 
NotifyDoNotDisturbDateChanged(const sptr<NotificationDoNotDisturbDate> & date)206 void NotificationSubscriberManager::NotifyDoNotDisturbDateChanged(const sptr<NotificationDoNotDisturbDate> &date)
207 {
208     if (notificationSubQueue_ == nullptr) {
209         ANS_LOGE("queue is nullptr");
210         return;
211     }
212     AppExecFwk::EventHandler::Callback func =
213         std::bind(&NotificationSubscriberManager::NotifyDoNotDisturbDateChangedInner, this, date);
214 
215     notificationSubQueue_->submit(func);
216 }
217 
NotifyEnabledNotificationChanged(const sptr<EnabledNotificationCallbackData> & callbackData)218 void NotificationSubscriberManager::NotifyEnabledNotificationChanged(
219     const sptr<EnabledNotificationCallbackData> &callbackData)
220 {
221     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
222     if (notificationSubQueue_ == nullptr) {
223         ANS_LOGE("queue is nullptr");
224         return;
225     }
226     AppExecFwk::EventHandler::Callback func =
227         std::bind(&NotificationSubscriberManager::NotifyEnabledNotificationChangedInner, this, callbackData);
228 
229     notificationSubQueue_->submit(func);
230 }
231 
OnRemoteDied(const wptr<IRemoteObject> & object)232 void NotificationSubscriberManager::OnRemoteDied(const wptr<IRemoteObject> &object)
233 {
234     ANS_LOGI("OnRemoteDied");
235     if (notificationSubQueue_ == nullptr) {
236         ANS_LOGE("queue is nullptr");
237         return;
238     }
239     ffrt::task_handle handler = notificationSubQueue_->submit_h(std::bind([this, object]() {
240         ANS_LOGE("ffrt enter!");
241         std::shared_ptr<SubscriberRecord> record = FindSubscriberRecord(object);
242         if (record != nullptr) {
243             ANS_LOGW("subscriber removed.");
244             subscriberRecordList_.remove(record);
245         }
246     }));
247     notificationSubQueue_->wait(handler);
248 }
249 
FindSubscriberRecord(const wptr<IRemoteObject> & object)250 std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> NotificationSubscriberManager::FindSubscriberRecord(
251     const wptr<IRemoteObject> &object)
252 {
253     auto iter = subscriberRecordList_.begin();
254 
255     for (; iter != subscriberRecordList_.end(); iter++) {
256         if ((*iter)->subscriber->AsObject() == object) {
257             return (*iter);
258         }
259     }
260     return nullptr;
261 }
262 
FindSubscriberRecord(const sptr<AnsSubscriberInterface> & subscriber)263 std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> NotificationSubscriberManager::FindSubscriberRecord(
264     const sptr<AnsSubscriberInterface> &subscriber)
265 {
266     auto iter = subscriberRecordList_.begin();
267 
268     for (; iter != subscriberRecordList_.end(); iter++) {
269         if ((*iter)->subscriber->AsObject() == subscriber->AsObject()) {
270             return (*iter);
271         }
272     }
273     return nullptr;
274 }
275 
CreateSubscriberRecord(const sptr<AnsSubscriberInterface> & subscriber)276 std::shared_ptr<NotificationSubscriberManager::SubscriberRecord> NotificationSubscriberManager::CreateSubscriberRecord(
277     const sptr<AnsSubscriberInterface> &subscriber)
278 {
279     std::shared_ptr<SubscriberRecord> record = std::make_shared<SubscriberRecord>();
280     if (record != nullptr) {
281         record->subscriber = subscriber;
282     }
283     return record;
284 }
285 
AddRecordInfo(std::shared_ptr<SubscriberRecord> & record,const sptr<NotificationSubscribeInfo> & subscribeInfo)286 void NotificationSubscriberManager::AddRecordInfo(
287     std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo)
288 {
289     if (subscribeInfo != nullptr) {
290         record->bundleList_.clear();
291         record->subscribedAll = true;
292         for (auto bundle : subscribeInfo->GetAppNames()) {
293             record->bundleList_.insert(bundle);
294             record->subscribedAll = false;
295         }
296         record->userId = subscribeInfo->GetAppUserId();
297     } else {
298         record->bundleList_.clear();
299         record->subscribedAll = true;
300     }
301 }
302 
RemoveRecordInfo(std::shared_ptr<SubscriberRecord> & record,const sptr<NotificationSubscribeInfo> & subscribeInfo)303 void NotificationSubscriberManager::RemoveRecordInfo(
304     std::shared_ptr<SubscriberRecord> &record, const sptr<NotificationSubscribeInfo> &subscribeInfo)
305 {
306     if (subscribeInfo != nullptr) {
307         for (auto bundle : subscribeInfo->GetAppNames()) {
308             if (record->subscribedAll) {
309                 record->bundleList_.insert(bundle);
310             } else {
311                 record->bundleList_.erase(bundle);
312             }
313         }
314     } else {
315         record->bundleList_.clear();
316         record->subscribedAll = false;
317     }
318 }
319 
AddSubscriberInner(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)320 ErrCode NotificationSubscriberManager::AddSubscriberInner(
321     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo)
322 {
323     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
324     std::shared_ptr<SubscriberRecord> record = FindSubscriberRecord(subscriber);
325     if (record == nullptr) {
326         record = CreateSubscriberRecord(subscriber);
327         if (record == nullptr) {
328             ANS_LOGE("CreateSubscriberRecord failed.");
329             return ERR_ANS_NO_MEMORY;
330         }
331         subscriberRecordList_.push_back(record);
332 
333         record->subscriber->AsObject()->AddDeathRecipient(recipient_);
334 
335         record->subscriber->OnConnected();
336         ANS_LOGI("subscriber is connected.");
337     }
338 
339     AddRecordInfo(record, subscribeInfo);
340     if (onSubscriberAddCallback_ != nullptr) {
341         onSubscriberAddCallback_(record);
342     }
343 
344     return ERR_OK;
345 }
346 
RemoveSubscriberInner(const sptr<AnsSubscriberInterface> & subscriber,const sptr<NotificationSubscribeInfo> & subscribeInfo)347 ErrCode NotificationSubscriberManager::RemoveSubscriberInner(
348     const sptr<AnsSubscriberInterface> &subscriber, const sptr<NotificationSubscribeInfo> &subscribeInfo)
349 {
350     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
351     std::shared_ptr<SubscriberRecord> record = FindSubscriberRecord(subscriber);
352 
353     if (record == nullptr) {
354         ANS_LOGE("subscriber not found.");
355         return ERR_ANS_INVALID_PARAM;
356     }
357 
358     RemoveRecordInfo(record, subscribeInfo);
359 
360     if (!record->subscribedAll && record->bundleList_.empty()) {
361         record->subscriber->AsObject()->RemoveDeathRecipient(recipient_);
362 
363         subscriberRecordList_.remove(record);
364 
365         record->subscriber->OnDisconnected();
366         ANS_LOGI("subscriber is disconnected.");
367     }
368 
369     return ERR_OK;
370 }
371 
NotifyConsumedInner(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap)372 void NotificationSubscriberManager::NotifyConsumedInner(
373     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap)
374 {
375     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
376     ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId());
377     int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId();
378     int32_t sendUserId = notification->GetUserId();
379     for (auto record : subscriberRecordList_) {
380         auto BundleNames = notification->GetBundleName();
381         ANS_LOGD("%{public}s record->userId = <%{public}d> BundleName  = <%{public}s",
382             __FUNCTION__, record->userId, BundleNames.c_str());
383         auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames);
384         if (!record->subscribedAll == (iter != record->bundleList_.end()) &&
385             ((record->userId == sendUserId) ||
386             (record->userId == SUBSCRIBE_USER_ALL) ||
387             (record->userId == recvUserId) ||
388             IsSystemUser(record->userId) ||  // Delete this, When the systemui subscribe carry the user ID.
389             IsSystemUser(sendUserId))) {
390             record->subscriber->OnConsumed(notification, notificationMap);
391         }
392     }
393 }
394 
BatchNotifyConsumedInner(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,const std::shared_ptr<SubscriberRecord> & record)395 void NotificationSubscriberManager::BatchNotifyConsumedInner(const std::vector<sptr<Notification>> &notifications,
396     const sptr<NotificationSortingMap> &notificationMap, const std::shared_ptr<SubscriberRecord> &record)
397 {
398     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
399     if (notifications.empty() || notificationMap == nullptr || record == nullptr) {
400         ANS_LOGE("Invalid input.");
401         return;
402     }
403 
404     ANS_LOGD("Record->userId = <%{public}d>", record->userId);
405     std::vector<sptr<Notification>> currNotifications;
406     for (size_t i = 0; i < notifications.size(); i ++) {
407         sptr<Notification> notification = notifications[i];
408         if (notification == nullptr) {
409             continue;
410         }
411         auto bundleName = notification->GetBundleName();
412         auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), bundleName);
413         int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId();
414         int32_t sendUserId = notification->GetUserId();
415         if (!record->subscribedAll == (iter != record->bundleList_.end()) &&
416             ((record->userId == sendUserId) ||
417             (record->userId == SUBSCRIBE_USER_ALL) ||
418             (record->userId == recvUserId) ||
419             IsSystemUser(record->userId) ||   // Delete this, When the systemui subscribe carry the user ID.
420             IsSystemUser(sendUserId))) {
421             currNotifications.emplace_back(notification);
422         }
423     }
424     if (!currNotifications.empty()) {
425         ANS_LOGD("OnConsumedList currNotifications size = <%{public}zu>", currNotifications.size());
426         if (record->subscriber != nullptr) {
427             record->subscriber->OnConsumedList(currNotifications, notificationMap);
428         }
429     }
430 }
431 
NotifyCanceledInner(const sptr<Notification> & notification,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)432 void NotificationSubscriberManager::NotifyCanceledInner(
433     const sptr<Notification> &notification, const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
434 {
435     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
436     ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId());
437     bool isCommonLiveView = notification->GetNotificationRequest().IsCommonLiveView();
438     std::shared_ptr<NotificationLiveViewContent> liveViewContent = nullptr;
439     if (isCommonLiveView) {
440         liveViewContent = std::static_pointer_cast<NotificationLiveViewContent>(
441             notification->GetNotificationRequest().GetContent()->GetNotificationContent());
442         liveViewContent->FillPictureMarshallingMap();
443     }
444 
445     int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId();
446     int32_t sendUserId = notification->GetUserId();
447     for (auto record : subscriberRecordList_) {
448         ANS_LOGD("%{public}s record->userId = <%{public}d>", __FUNCTION__, record->userId);
449         auto BundleNames = notification->GetBundleName();
450         auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), BundleNames);
451         if (!record->subscribedAll == (iter != record->bundleList_.end()) &&
452             ((record->userId == sendUserId) ||
453             (record->userId == SUBSCRIBE_USER_ALL) ||
454             (record->userId == recvUserId) ||
455             IsSystemUser(record->userId) ||   // Delete this, When the systemui subscribe carry the user ID.
456             IsSystemUser(sendUserId))) {
457             record->subscriber->OnCanceled(notification, notificationMap, deleteReason);
458         }
459     }
460 
461     if (isCommonLiveView && liveViewContent != nullptr) {
462         liveViewContent->ClearPictureMarshallingMap();
463     }
464 }
465 
BatchNotifyCanceledInner(const std::vector<sptr<Notification>> & notifications,const sptr<NotificationSortingMap> & notificationMap,int32_t deleteReason)466 void NotificationSubscriberManager::BatchNotifyCanceledInner(const std::vector<sptr<Notification>> &notifications,
467     const sptr<NotificationSortingMap> &notificationMap, int32_t deleteReason)
468 {
469     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
470 
471     ANS_LOGD("notifications size = <%{public}zu>", notifications.size());
472     for (auto record : subscriberRecordList_) {
473         if (record == nullptr) {
474             continue;
475         }
476         ANS_LOGD("record->userId = <%{public}d>", record->userId);
477         std::vector<sptr<Notification>> currNotifications;
478         for (size_t i = 0; i < notifications.size(); i ++) {
479             sptr<Notification> notification = notifications[i];
480             if (notification == nullptr) {
481                 continue;
482             }
483             auto bundleName = notification->GetBundleName();
484             auto iter = std::find(record->bundleList_.begin(), record->bundleList_.end(), bundleName);
485             int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId();
486             int32_t sendUserId = notification->GetUserId();
487             if (!record->subscribedAll == (iter != record->bundleList_.end()) &&
488                 ((record->userId == sendUserId) ||
489                 (record->userId == SUBSCRIBE_USER_ALL) ||
490                 (record->userId == recvUserId) ||
491                 IsSystemUser(record->userId) ||   // Delete this, When the systemui subscribe carry the user ID.
492                 IsSystemUser(sendUserId))) {
493                 currNotifications.emplace_back(notification);
494             }
495         }
496         if (!currNotifications.empty()) {
497             ANS_LOGD("onCanceledList currNotifications size = <%{public}zu>", currNotifications.size());
498             if (record->subscriber != nullptr) {
499                 record->subscriber->OnCanceledList(currNotifications, notificationMap, deleteReason);
500             }
501         }
502     }
503 }
504 
NotifyUpdatedInner(const sptr<NotificationSortingMap> & notificationMap)505 void NotificationSubscriberManager::NotifyUpdatedInner(const sptr<NotificationSortingMap> &notificationMap)
506 {
507     for (auto record : subscriberRecordList_) {
508         record->subscriber->OnUpdated(notificationMap);
509     }
510 }
511 
NotifyDoNotDisturbDateChangedInner(const sptr<NotificationDoNotDisturbDate> & date)512 void NotificationSubscriberManager::NotifyDoNotDisturbDateChangedInner(const sptr<NotificationDoNotDisturbDate> &date)
513 {
514     for (auto record : subscriberRecordList_) {
515         record->subscriber->OnDoNotDisturbDateChange(date);
516     }
517 }
518 
IsSystemUser(int32_t userId)519 bool NotificationSubscriberManager::IsSystemUser(int32_t userId)
520 {
521     return ((userId >= SUBSCRIBE_USER_SYSTEM_BEGIN) && (userId <= SUBSCRIBE_USER_SYSTEM_END));
522 }
523 
NotifyEnabledNotificationChangedInner(const sptr<EnabledNotificationCallbackData> & callbackData)524 void NotificationSubscriberManager::NotifyEnabledNotificationChangedInner(
525     const sptr<EnabledNotificationCallbackData> &callbackData)
526 {
527     for (auto record : subscriberRecordList_) {
528         record->subscriber->OnEnabledNotificationChanged(callbackData);
529     }
530 }
531 
SetBadgeNumber(const sptr<BadgeNumberCallbackData> & badgeData)532 void NotificationSubscriberManager::SetBadgeNumber(const sptr<BadgeNumberCallbackData> &badgeData)
533 {
534     if (notificationSubQueue_ == nullptr) {
535         ANS_LOGE("queue is nullptr");
536         return;
537     }
538     std::function<void()> setBadgeNumberFunc = [badgeData] () {
539         for (auto record : NotificationSubscriberManager::GetInstance()->subscriberRecordList_) {
540             record->subscriber->OnBadgeChanged(badgeData);
541         }
542     };
543     notificationSubQueue_->submit(setBadgeNumberFunc);
544 }
545 
RegisterOnSubscriberAddCallback(std::function<void (const std::shared_ptr<SubscriberRecord> &)> callback)546 void NotificationSubscriberManager::RegisterOnSubscriberAddCallback(
547     std::function<void(const std::shared_ptr<SubscriberRecord> &)> callback)
548 {
549     if (callback == nullptr) {
550         ANS_LOGE("Callback is nullptr");
551         return;
552     }
553 
554     onSubscriberAddCallback_ = callback;
555 }
556 
UnRegisterOnSubscriberAddCallback()557 void NotificationSubscriberManager::UnRegisterOnSubscriberAddCallback()
558 {
559     onSubscriberAddCallback_ = nullptr;
560 }
561 
562 using SubscriberRecordPtr = std::shared_ptr<NotificationSubscriberManager::SubscriberRecord>;
GetSubscriberRecords()563 std::list<SubscriberRecordPtr> NotificationSubscriberManager::GetSubscriberRecords()
564 {
565     return subscriberRecordList_;
566 }
567 
568 }  // namespace Notification
569 }  // namespace OHOS
570