• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "advanced_notification_service.h"
17 
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21 
22 #include "accesstoken_kit.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "errors.h"
26 
27 #include "ipc_skeleton.h"
28 #include "notification_constant.h"
29 #include "hitrace_meter_adapter.h"
30 #include "os_account_manager.h"
31 #include "distributed_screen_status_manager.h"
32 #include "notification_local_live_view_subscriber_manager.h"
33 
34 #include "advanced_notification_inline.cpp"
35 
36 namespace OHOS {
37 namespace Notification {
38 
39 constexpr char FOUNDATION_BUNDLE_NAME[] = "ohos.global.systemres";
40 
SetDefaultNotificationEnabled(const sptr<NotificationBundleOption> & bundleOption,bool enabled)41 ErrCode AdvancedNotificationService::SetDefaultNotificationEnabled(
42     const sptr<NotificationBundleOption> &bundleOption, bool enabled)
43 {
44     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
45     if (bundle == nullptr) {
46         return ERR_ANS_INVALID_BUNDLE;
47     }
48     sptr<EnabledNotificationCallbackData> bundleData =
49         new (std::nothrow) EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled);
50     if (bundleData == nullptr) {
51         ANS_LOGE("Failed to create EnabledNotificationCallbackData instance");
52         return ERR_NO_MEMORY;
53     }
54 
55     ErrCode result = ERR_OK;
56     result = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle(bundle, enabled);
57 
58     if (!enabled) {
59         ANS_LOGI("result = %{public}d", result);
60         result = RemoveAllNotifications(bundle);
61     }
62     if (result == ERR_OK) {
63         NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData);
64         PublishSlotChangeCommonEvent(bundle);
65     }
66 
67     SendEnableNotificationHiSysEvent(bundleOption, enabled, result);
68     return result;
69 }
70 
Publish(const std::string & label,const sptr<NotificationRequest> & request)71 ErrCode AdvancedNotificationService::Publish(const std::string &label, const sptr<NotificationRequest> &request)
72 {
73     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
74     ANS_LOGD("%{public}s", __FUNCTION__);
75 
76     if (!request) {
77         ANSR_LOGE("ReminderRequest object is nullptr");
78         return ERR_ANS_INVALID_PARAM;
79     }
80 
81     if (!CheckLocalLiveViewAllowed(request)) {
82         return ERR_ANS_NON_SYSTEM_APP;
83     }
84 
85     if (!CheckLocalLiveViewSubscribed(request)) {
86         return ERR_ANS_INVALID_PARAM;
87     }
88 
89     if (!request->IsRemoveAllowed()) {
90         if (!CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
91             request->SetRemoveAllowed(true);
92         }
93     }
94 
95     ErrCode result = ERR_OK;
96     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
97     if (isSubsystem) {
98         return PublishNotificationBySa(request);
99     }
100 
101     do {
102         if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT) {
103             if (!AccessTokenHelper::IsSystemApp()) {
104                 result = ERR_ANS_NON_SYSTEM_APP;
105                 break;
106             }
107             if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
108                 result = ERR_ANS_PERMISSION_DENIED;
109                 break;
110             }
111         }
112 
113         // The third-party app needn't support in progress except for live view
114         if (request->IsInProgress() &&
115             !AccessTokenHelper::IsSystemApp() &&
116             !request->IsCommonLiveView()) {
117             request->SetInProgress(false);
118         }
119 
120         Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
121         if (AccessTokenHelper::IsDlpHap(callerToken)) {
122             result = ERR_ANS_DLP_HAP;
123             ANS_LOGE("DLP hap not allowed to send notifications");
124             break;
125         }
126 
127         sptr<NotificationBundleOption> bundleOption;
128         result = PrepareNotificationInfo(request, bundleOption);
129         if (result != ERR_OK) {
130             break;
131         }
132 
133         if (IsNeedPushCheck(request)) {
134             result = PushCheck(request);
135         }
136         if (result != ERR_OK) {
137             break;
138         }
139         result = PublishPreparedNotification(request, bundleOption);
140         if (result != ERR_OK) {
141             break;
142         }
143     } while (0);
144 
145     SendPublishHiSysEvent(request, result);
146     return result;
147 }
148 
Cancel(int32_t notificationId,const std::string & label)149 ErrCode AdvancedNotificationService::Cancel(int32_t notificationId, const std::string &label)
150 {
151     ANS_LOGD("%{public}s", __FUNCTION__);
152 
153     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
154     return CancelPreparedNotification(notificationId, label, bundleOption);
155 }
156 
CancelAll()157 ErrCode AdvancedNotificationService::CancelAll()
158 {
159     ANS_LOGD("%{public}s", __FUNCTION__);
160 
161     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
162     if (bundleOption == nullptr) {
163         return ERR_ANS_INVALID_BUNDLE;
164     }
165 
166     if (notificationSvrQueue_ == nullptr) {
167         ANS_LOGE("Serial queue is invalidated.");
168         return ERR_ANS_INVALID_PARAM;
169     }
170     ErrCode result = ERR_OK;
171     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
172         ANS_LOGD("ffrt enter!");
173         sptr<Notification> notification = nullptr;
174 
175         std::vector<std::string> keys = GetNotificationKeys(bundleOption);
176         std::vector<sptr<Notification>> notifications;
177         for (auto key : keys) {
178 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
179             std::string deviceId;
180             std::string bundleName;
181             GetDistributedInfo(key, deviceId, bundleName);
182 #endif
183             result = RemoveFromNotificationList(key, notification, true,
184                 NotificationConstant::APP_CANCEL_ALL_REASON_DELETE);
185             if (result != ERR_OK) {
186                 continue;
187             }
188 
189             if (notification != nullptr) {
190                 int32_t reason = NotificationConstant::APP_CANCEL_ALL_REASON_DELETE;
191                 UpdateRecentNotification(notification, true, reason);
192                 notifications.emplace_back(notification);
193 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
194                 DoDistributedDelete(deviceId, bundleName, notification);
195 #endif
196             }
197             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
198                 std::vector<sptr<Notification>> currNotificationList = notifications;
199                 NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
200                     currNotificationList, nullptr, NotificationConstant::APP_CANCEL_ALL_REASON_DELETE);
201                 notifications.clear();
202             }
203         }
204 
205         if (!notifications.empty()) {
206             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
207                 notifications, nullptr, NotificationConstant::APP_CANCEL_ALL_REASON_DELETE);
208         }
209         result = ERR_OK;
210     }));
211     notificationSvrQueue_->wait(handler);
212     return result;
213 }
214 
CancelAsBundle(int32_t notificationId,const std::string & representativeBundle,int32_t userId)215 ErrCode AdvancedNotificationService::CancelAsBundle(
216     int32_t notificationId, const std::string &representativeBundle, int32_t userId)
217 {
218     ANS_LOGD("%{public}s", __FUNCTION__);
219 
220     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
221     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
222         return ERR_ANS_NON_SYSTEM_APP;
223     }
224 
225     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER) ||
226         !CheckPermission(OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER)) {
227         return ERR_ANS_PERMISSION_DENIED;
228     }
229 
230     int32_t uid = -1;
231     std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
232     if (bundleManager != nullptr) {
233         uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(representativeBundle, userId);
234     }
235     if (uid < 0) {
236         return ERR_ANS_INVALID_UID;
237     }
238     sptr<NotificationBundleOption> bundleOption = new (std::nothrow) NotificationBundleOption(
239         representativeBundle, uid);
240     return CancelPreparedNotification(notificationId, "", bundleOption);
241 }
242 
PublishAsBundle(const sptr<NotificationRequest> notification,const std::string & representativeBundle)243 ErrCode AdvancedNotificationService::PublishAsBundle(
244     const sptr<NotificationRequest> notification, const std::string &representativeBundle)
245 {
246     return ERR_INVALID_OPERATION;
247 }
248 
SetNotificationBadgeNum(int32_t num)249 ErrCode AdvancedNotificationService::SetNotificationBadgeNum(int32_t num)
250 {
251     ANS_LOGD("%{public}s", __FUNCTION__);
252 
253     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
254     if (bundleOption == nullptr) {
255         ANS_LOGD("BundleOption is null.");
256         return ERR_ANS_INVALID_BUNDLE;
257     }
258 
259     if (notificationSvrQueue_ == nullptr) {
260         ANS_LOGE("Serial queue is invalidity.");
261         return ERR_ANS_INVALID_PARAM;
262     }
263     ErrCode result = ERR_OK;
264     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
265         std::bind([&]() {
266             ANS_LOGD("ffrt enter!");
267             result = NotificationPreferences::GetInstance().SetTotalBadgeNums(bundleOption, num);
268         }));
269     notificationSvrQueue_->wait(handler);
270     return result;
271 }
272 
Delete(const std::string & key,int32_t removeReason)273 ErrCode AdvancedNotificationService::Delete(const std::string &key, int32_t removeReason)
274 {
275     ANS_LOGD("%{public}s", __FUNCTION__);
276 
277     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
278     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
279         ANS_LOGD("VerifyNativeToken and IsSystemApp is false.");
280         return ERR_ANS_NON_SYSTEM_APP;
281     }
282 
283     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
284         return ERR_ANS_PERMISSION_DENIED;
285     }
286 
287     if (notificationSvrQueue_ == nullptr) {
288         ANS_LOGE("Serial queue is invalidated.");
289         return ERR_ANS_INVALID_PARAM;
290     }
291     ErrCode result = ERR_OK;
292     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
293         ANS_LOGD("ffrt enter!");
294         sptr<Notification> notification = nullptr;
295 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
296         std::string deviceId;
297         std::string bundleName;
298         GetDistributedInfo(key, deviceId, bundleName);
299 #endif
300         result = RemoveFromNotificationList(key, notification, false, removeReason);
301         if (result != ERR_OK) {
302             return;
303         }
304 
305         if (notification != nullptr) {
306             UpdateRecentNotification(notification, true, removeReason);
307             NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason);
308 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
309             DoDistributedDelete(deviceId, bundleName, notification);
310 #endif
311         }
312     }));
313     notificationSvrQueue_->wait(handler);
314 
315     return result;
316 }
317 
DeleteByBundle(const sptr<NotificationBundleOption> & bundleOption)318 ErrCode AdvancedNotificationService::DeleteByBundle(const sptr<NotificationBundleOption> &bundleOption)
319 {
320     ANS_LOGD("%{public}s", __FUNCTION__);
321 
322     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
323     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
324         ANS_LOGD("VerifyNativeToken is false.");
325         return ERR_ANS_NON_SYSTEM_APP;
326     }
327 
328     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
329         return ERR_ANS_PERMISSION_DENIED;
330     }
331 
332     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
333     if (bundle == nullptr) {
334         ANS_LOGD("bundle is false.");
335         return ERR_ANS_INVALID_BUNDLE;
336     }
337 
338     if (notificationSvrQueue_ == nullptr) {
339         ANS_LOGE("Serial queue is invalid.");
340         return ERR_ANS_INVALID_PARAM;
341     }
342     ErrCode result = ERR_OK;
343     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
344         ANS_LOGD("ffrt enter!");
345         std::vector<std::string> keys = GetNotificationKeys(bundle);
346         for (auto key : keys) {
347 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
348             std::string deviceId;
349             std::string bundleName;
350             GetDistributedInfo(key, deviceId, bundleName);
351 #endif
352             sptr<Notification> notification = nullptr;
353 
354             result = RemoveFromNotificationList(key, notification, false, NotificationConstant::CANCEL_REASON_DELETE);
355             if (result != ERR_OK) {
356                 continue;
357             }
358 
359             if (notification != nullptr) {
360                 int32_t reason = NotificationConstant::CANCEL_REASON_DELETE;
361                 UpdateRecentNotification(notification, true, reason);
362                 NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason);
363 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
364                 DoDistributedDelete(deviceId, bundleName, notification);
365 #endif
366             }
367         }
368 
369         result = ERR_OK;
370     }));
371     notificationSvrQueue_->wait(handler);
372 
373     return result;
374 }
375 
DeleteAll()376 ErrCode AdvancedNotificationService::DeleteAll()
377 {
378     ANS_LOGD("%{public}s", __FUNCTION__);
379 
380     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
381     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
382         return ERR_ANS_NON_SYSTEM_APP;
383     }
384 
385     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
386         ANS_LOGD("CheckPermission is false.");
387         return ERR_ANS_PERMISSION_DENIED;
388     }
389 
390     if (notificationSvrQueue_ == nullptr) {
391         ANS_LOGE("Serial queue is invalidity.");
392         return ERR_ANS_INVALID_PARAM;
393     }
394     ErrCode result = ERR_OK;
395     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
396         ANS_LOGD("ffrt enter!");
397         int32_t activeUserId = SUBSCRIBE_USER_INIT;
398         if (!GetActiveUserId(activeUserId)) {
399             return;
400         }
401         std::vector<std::string> keys = GetNotificationKeys(nullptr);
402         std::vector<sptr<Notification>> notifications;
403         for (auto key : keys) {
404 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
405             std::string deviceId;
406             std::string bundleName;
407             GetDistributedInfo(key, deviceId, bundleName);
408 #endif
409             sptr<Notification> notification = nullptr;
410 
411             result = RemoveFromNotificationListForDeleteAll(key, activeUserId, notification);
412             if ((result != ERR_OK) || (notification == nullptr)) {
413                 continue;
414             }
415 
416             if (notification->GetUserId() == activeUserId) {
417                 int32_t reason = NotificationConstant::CANCEL_ALL_REASON_DELETE;
418                 UpdateRecentNotification(notification, true, reason);
419                 notifications.emplace_back(notification);
420 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
421                 DoDistributedDelete(deviceId, bundleName, notification);
422 #endif
423             }
424             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
425                 ANS_LOGD("Notifications size greater than or equal to MAX_CANCELED_PARCELABLE_VECTOR_NUM.");
426                 SendNotificationsOnCanceled(notifications, nullptr, NotificationConstant::CANCEL_ALL_REASON_DELETE);
427             }
428         }
429         if (!notifications.empty()) {
430             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
431                 notifications, nullptr, NotificationConstant::CANCEL_REASON_DELETE);
432         }
433 
434         result = ERR_OK;
435     }));
436     notificationSvrQueue_->wait(handler);
437 
438     return result;
439 }
440 
SetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool enabled)441 ErrCode AdvancedNotificationService::SetShowBadgeEnabledForBundle(
442     const sptr<NotificationBundleOption> &bundleOption, bool enabled)
443 {
444     ANS_LOGD("%{public}s", __FUNCTION__);
445 
446     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
447     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
448         return ERR_ANS_NON_SYSTEM_APP;
449     }
450 
451     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
452         ANS_LOGD("Check permission is false.");
453         return ERR_ANS_PERMISSION_DENIED;
454     }
455 
456     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
457     if (bundle == nullptr) {
458         return ERR_ANS_INVALID_BUNDLE;
459     }
460 
461     if (notificationSvrQueue_ == nullptr) {
462         ANS_LOGE("NotificationSvrQueue_ is invalid.");
463         return ERR_ANS_INVALID_PARAM;
464     }
465     ErrCode result = ERR_OK;
466     ffrt::task_handle handler = notificationSvrQueue_->submit_h(
467         std::bind([&]() {
468             result = NotificationPreferences::GetInstance().SetShowBadge(bundle, enabled);
469             ANS_LOGD("ffrt enter!");
470         }));
471     notificationSvrQueue_->wait(handler);
472     return result;
473 }
474 
GetShowBadgeEnabledForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & enabled)475 ErrCode AdvancedNotificationService::GetShowBadgeEnabledForBundle(
476     const sptr<NotificationBundleOption> &bundleOption, bool &enabled)
477 {
478     ANS_LOGD("%{public}s", __FUNCTION__);
479 
480     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
481     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
482         ANS_LOGD("VerifyNativeToken is bogus.");
483         return ERR_ANS_NON_SYSTEM_APP;
484     }
485 
486     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
487         return ERR_ANS_PERMISSION_DENIED;
488     }
489 
490     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
491     if (bundle == nullptr) {
492         ANS_LOGD("Failed to generateValidBundleOption.");
493         return ERR_ANS_INVALID_BUNDLE;
494     }
495 
496     if (notificationSvrQueue_ == nullptr) {
497         ANS_LOGE("Serial queue is invalid.");
498         return ERR_ANS_INVALID_PARAM;
499     }
500     ErrCode result = ERR_OK;
501     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
502         ANS_LOGD("ffrt enter!");
503         result = NotificationPreferences::GetInstance().IsShowBadge(bundle, enabled);
504         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
505             result = ERR_OK;
506             enabled = false;
507         }
508     }));
509     notificationSvrQueue_->wait(handler);
510     return result;
511 }
512 
GetShowBadgeEnabled(bool & enabled)513 ErrCode AdvancedNotificationService::GetShowBadgeEnabled(bool &enabled)
514 {
515     ANS_LOGD("%{public}s", __FUNCTION__);
516 
517     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
518     if (bundleOption == nullptr) {
519         return ERR_ANS_INVALID_BUNDLE;
520     }
521 
522     if (notificationSvrQueue_ == nullptr) {
523         ANS_LOGE("Serial queue is ineffective.");
524         return ERR_ANS_INVALID_PARAM;
525     }
526     ErrCode result = ERR_OK;
527     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
528         ANS_LOGD("ffrt enter!");
529         result = NotificationPreferences::GetInstance().IsShowBadge(bundleOption, enabled);
530         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
531             result = ERR_OK;
532             enabled = false;
533         }
534     }));
535     notificationSvrQueue_->wait(handler);
536     return result;
537 }
538 
RequestEnableNotification(const std::string & deviceId,const sptr<AnsDialogCallback> & callback,const sptr<IRemoteObject> & callerToken)539 ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId,
540     const sptr<AnsDialogCallback> &callback,
541     const sptr<IRemoteObject> &callerToken)
542 {
543     ANS_LOGD("%{public}s", __FUNCTION__);
544     if (callback == nullptr) {
545         ANS_LOGE("callback == nullptr");
546         return ERR_ANS_INVALID_PARAM;
547     }
548 
549     ErrCode result = ERR_OK;
550     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
551     if (bundleOption == nullptr) {
552         ANS_LOGD("bundleOption == nullptr");
553         return ERR_ANS_INVALID_BUNDLE;
554     }
555     // To get the permission
556     bool allowedNotify = false;
557     result = IsAllowedNotifySelf(bundleOption, allowedNotify);
558     if (result != ERR_OK) {
559         return ERROR_INTERNAL_ERROR;
560     }
561     ANS_LOGI("allowedNotify = %{public}d", allowedNotify);
562     if (allowedNotify) {
563         return ERR_OK;
564     }
565     // Check to see if it has been popover before
566     bool hasPopped = false;
567     result = GetHasPoppedDialog(bundleOption, hasPopped);
568     if (result != ERR_OK) {
569         return ERROR_INTERNAL_ERROR;
570     }
571     if (hasPopped) {
572         return ERR_ANS_NOT_ALLOWED;
573     }
574 
575     if (!CreateDialogManager()) {
576         return ERROR_INTERNAL_ERROR;
577     }
578     result = dialogManager_->RequestEnableNotificationDailog(bundleOption, callback, callerToken);
579     if (result == ERR_OK) {
580         result = ERR_ANS_DIALOG_POP_SUCCEEDED;
581     }
582     return result;
583 }
584 
SetNotificationsEnabledForBundle(const std::string & deviceId,bool enabled)585 ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled)
586 {
587     return ERR_INVALID_OPERATION;
588 }
589 
SetNotificationsEnabledForAllBundles(const std::string & deviceId,bool enabled)590 ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const std::string &deviceId, bool enabled)
591 {
592     ANS_LOGD("%{public}s", __FUNCTION__);
593 
594     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
595     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
596         ANS_LOGD("VerifyNativeToken and IsSystemApp is false.");
597         return ERR_ANS_NON_SYSTEM_APP;
598     }
599 
600     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
601         return ERR_ANS_PERMISSION_DENIED;
602     }
603 
604     int32_t userId = SUBSCRIBE_USER_INIT;
605     if (!GetActiveUserId(userId)) {
606         return ERR_ANS_GET_ACTIVE_USER_FAILED;
607     }
608 
609     if (notificationSvrQueue_ == nullptr) {
610         ANS_LOGE("Serial queue is invalidity.");
611         return ERR_ANS_INVALID_PARAM;
612     }
613     ErrCode result = ERR_OK;
614     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
615         ANS_LOGD("ffrt enter!");
616         if (deviceId.empty()) {
617             // Local device
618             result = NotificationPreferences::GetInstance().SetNotificationsEnabled(userId, enabled);
619         } else {
620             // Remote device
621         }
622     }));
623     notificationSvrQueue_->wait(handler);
624     return result;
625 }
626 
SetNotificationsEnabledForSpecialBundle(const std::string & deviceId,const sptr<NotificationBundleOption> & bundleOption,bool enabled)627 ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle(
628     const std::string &deviceId, const sptr<NotificationBundleOption> &bundleOption, bool enabled)
629 {
630     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
631     ANS_LOGD("%{public}s", __FUNCTION__);
632 
633     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
634     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
635         ANS_LOGD("IsSystemApp is bogus.");
636         return ERR_ANS_NON_SYSTEM_APP;
637     }
638 
639     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
640         return ERR_ANS_PERMISSION_DENIED;
641     }
642 
643     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
644     if (bundle == nullptr) {
645         return ERR_ANS_INVALID_BUNDLE;
646     }
647 
648     sptr<EnabledNotificationCallbackData> bundleData = new (std::nothrow)
649         EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled);
650     if (bundleData == nullptr) {
651         ANS_LOGE("Failed to create EnabledNotificationCallbackData instance");
652         return ERR_NO_MEMORY;
653     }
654 
655     ErrCode result = ERR_OK;
656     if (deviceId.empty()) {
657         // Local device
658         result = NotificationPreferences::GetInstance().SetNotificationsEnabledForBundle(bundle, enabled);
659         if (!enabled) {
660             result = RemoveAllNotifications(bundle);
661         }
662         if (result == ERR_OK) {
663             NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData);
664             PublishSlotChangeCommonEvent(bundle);
665         }
666     } else {
667         // Remote device
668     }
669 
670     SendEnableNotificationHiSysEvent(bundleOption, enabled, result);
671     return result;
672 }
673 
IsAllowedNotify(bool & allowed)674 ErrCode AdvancedNotificationService::IsAllowedNotify(bool &allowed)
675 {
676     ANS_LOGD("%{public}s", __FUNCTION__);
677 
678     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
679     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
680         return ERR_ANS_NON_SYSTEM_APP;
681     }
682 
683     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
684         ANS_LOGD("CheckPermission is false");
685         return ERR_ANS_PERMISSION_DENIED;
686     }
687 
688     int32_t userId = SUBSCRIBE_USER_INIT;
689     if (!GetActiveUserId(userId)) {
690         return ERR_ANS_GET_ACTIVE_USER_FAILED;
691     }
692 
693     if (notificationSvrQueue_ == nullptr) {
694         ANS_LOGE("Serial queue is invalid.");
695         return ERR_ANS_INVALID_PARAM;
696     }
697     ErrCode result = ERR_OK;
698     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
699         ANS_LOGD("ffrt enter!");
700         allowed = false;
701         result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed);
702     }));
703     notificationSvrQueue_->wait(handler);
704     return result;
705 }
706 
IsAllowedNotifySelf(bool & allowed)707 ErrCode AdvancedNotificationService::IsAllowedNotifySelf(bool &allowed)
708 {
709     ANS_LOGD("%{public}s", __FUNCTION__);
710 
711     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
712     if (bundleOption == nullptr) {
713         return ERR_ANS_INVALID_BUNDLE;
714     }
715     return IsAllowedNotifySelf(bundleOption, allowed);
716 }
717 
IsAllowedNotifySelf(const sptr<NotificationBundleOption> & bundleOption,bool & allowed)718 ErrCode AdvancedNotificationService::IsAllowedNotifySelf(const sptr<NotificationBundleOption> &bundleOption,
719     bool &allowed)
720 {
721     ANS_LOGD("%{public}s", __FUNCTION__);
722     if (bundleOption == nullptr) {
723         return ERR_ANS_INVALID_BUNDLE;
724     }
725 
726     int32_t userId = SUBSCRIBE_USER_INIT;
727     if (!GetActiveUserId(userId)) {
728         ANS_LOGD("GetActiveUserId is false");
729         return ERR_ANS_GET_ACTIVE_USER_FAILED;
730     }
731 
732     ErrCode result = ERR_OK;
733     allowed = false;
734     result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed);
735     if (result == ERR_OK && allowed) {
736         result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(bundleOption, allowed);
737         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
738             result = ERR_OK;
739             // FA model app can publish notification without user confirm
740             allowed = CheckApiCompatibility(bundleOption);
741             SetDefaultNotificationEnabled(bundleOption, allowed);
742         }
743     }
744     return result;
745 }
746 
IsAllowedNotifyForBundle(const sptr<NotificationBundleOption> & bundleOption,bool & allowed)747 ErrCode AdvancedNotificationService::IsAllowedNotifyForBundle(const sptr<NotificationBundleOption>
748     &bundleOption, bool &allowed)
749 {
750     ANS_LOGD("%{public}s", __FUNCTION__);
751     if (bundleOption == nullptr) {
752         return ERR_ANS_INVALID_BUNDLE;
753     }
754 
755     int32_t userId = SUBSCRIBE_USER_INIT;
756     if (!GetActiveUserId(userId)) {
757         ANS_LOGD("GetActiveUserId is false");
758         return ERR_ANS_GET_ACTIVE_USER_FAILED;
759     }
760 
761     ErrCode result = ERR_OK;
762     allowed = false;
763     result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed);
764     if (result == ERR_OK && allowed) {
765         result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(bundleOption, allowed);
766         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
767             result = ERR_OK;
768             // FA model app can publish notification without user confirm
769             allowed = CheckApiCompatibility(bundleOption);
770         }
771     }
772     return result;
773 }
774 
IsSpecialBundleAllowedNotify(const sptr<NotificationBundleOption> & bundleOption,bool & allowed)775 ErrCode AdvancedNotificationService::IsSpecialBundleAllowedNotify(
776     const sptr<NotificationBundleOption> &bundleOption, bool &allowed)
777 {
778     ANS_LOGD("%{public}s", __FUNCTION__);
779 
780     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
781     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
782         ANS_LOGE("Not system application");
783         return ERR_ANS_NON_SYSTEM_APP;
784     }
785 
786     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
787         return ERR_ANS_PERMISSION_DENIED;
788     }
789 
790     sptr<NotificationBundleOption> targetBundle = nullptr;
791     if (isSubsystem) {
792         if (bundleOption != nullptr) {
793             targetBundle = GenerateValidBundleOption(bundleOption);
794         }
795     } else {
796         ErrCode result = GetAppTargetBundle(bundleOption, targetBundle);
797         if (result != ERR_OK) {
798             return result;
799         }
800     }
801 
802     if (targetBundle == nullptr) {
803         return ERR_ANS_INVALID_BUNDLE;
804     }
805 
806     int32_t userId = SUBSCRIBE_USER_INIT;
807     if (!GetActiveUserId(userId)) {
808         return ERR_ANS_GET_ACTIVE_USER_FAILED;
809     }
810 
811     ErrCode result = ERR_OK;
812         allowed = false;
813         result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed);
814         if (result == ERR_OK && allowed) {
815             result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(targetBundle, allowed);
816             if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
817                 result = ERR_OK;
818                 allowed = CheckApiCompatibility(targetBundle);
819                 SetNotificationsEnabledForSpecialBundle("", bundleOption, allowed);
820             }
821         }
822     return result;
823 }
824 
PublishContinuousTaskNotification(const sptr<NotificationRequest> & request)825 ErrCode AdvancedNotificationService::PublishContinuousTaskNotification(const sptr<NotificationRequest> &request)
826 {
827     ANS_LOGD("%{public}s", __FUNCTION__);
828 
829     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
830     if (!isSubsystem) {
831         return ERR_ANS_NOT_SYSTEM_SERVICE;
832     }
833 
834     int32_t uid = IPCSkeleton::GetCallingUid();
835     int32_t userId = SUBSCRIBE_USER_INIT;
836     OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
837     request->SetCreatorUserId(userId);
838     ANS_LOGD("%{public}s, uid=%{public}d userId=%{public}d", __FUNCTION__, uid, userId);
839 
840     if (request->GetCreatorBundleName().empty()) {
841         request->SetCreatorBundleName(FOUNDATION_BUNDLE_NAME);
842     }
843 
844     if (request->GetOwnerBundleName().empty()) {
845         request->SetOwnerBundleName(FOUNDATION_BUNDLE_NAME);
846     }
847 
848     sptr<NotificationBundleOption> bundleOption = nullptr;
849     bundleOption = new (std::nothrow) NotificationBundleOption(std::string(), uid);
850     if (bundleOption == nullptr) {
851         ANS_LOGE("Failed to create NotificationBundleOption instance");
852         return ERR_NO_MEMORY;
853     }
854 
855     ErrCode result = PrepareContinuousTaskNotificationRequest(request, uid);
856     if (result != ERR_OK) {
857         return result;
858     }
859     request->SetUnremovable(true);
860     std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
861     record->request = request;
862     record->bundleOption = bundleOption;
863     record->notification = new (std::nothrow) Notification(request);
864     if (record->notification == nullptr) {
865         ANS_LOGE("Failed to create Notification instance");
866         return ERR_NO_MEMORY;
867     }
868     record->notification->SetSourceType(NotificationConstant::SourceType::TYPE_CONTINUOUS);
869 
870     if (notificationSvrQueue_ == nullptr) {
871         ANS_LOGE("Serial queue is invalid.");
872         return ERR_ANS_INVALID_PARAM;
873     }
874     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
875         ANS_LOGD("ffrt enter!");
876         if (!IsNotificationExists(record->notification->GetKey())) {
877             AddToNotificationList(record);
878         } else {
879             if (record->request->IsAlertOneTime()) {
880                 record->notification->SetEnableLight(false);
881                 record->notification->SetEnableSound(false);
882                 record->notification->SetEnableVibration(false);
883             }
884             UpdateInNotificationList(record);
885         }
886 
887         UpdateRecentNotification(record->notification, false, 0);
888         sptr<NotificationSortingMap> sortingMap = GenerateSortingMap();
889         NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap);
890     }));
891     notificationSvrQueue_->wait(handler);
892 
893     return result;
894 }
895 
CancelContinuousTaskNotification(const std::string & label,int32_t notificationId)896 ErrCode AdvancedNotificationService::CancelContinuousTaskNotification(const std::string &label, int32_t notificationId)
897 {
898     ANS_LOGD("%{public}s", __FUNCTION__);
899 
900     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
901     if (!isSubsystem) {
902         return ERR_ANS_NOT_SYSTEM_SERVICE;
903     }
904 
905     if (notificationSvrQueue_ == nullptr) {
906         ANS_LOGE("Serial queue is invalid.");
907         return ERR_ANS_INVALID_PARAM;
908     }
909     int32_t uid = IPCSkeleton::GetCallingUid();
910     ErrCode result = ERR_OK;
911     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
912         ANS_LOGD("ffrt enter!");
913         sptr<Notification> notification = nullptr;
914         for (auto record : notificationList_) {
915             if ((record->bundleOption->GetBundleName().empty()) && (record->bundleOption->GetUid() == uid) &&
916                 (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) {
917                 notification = record->notification;
918                 notificationList_.remove(record);
919                 result = ERR_OK;
920                 break;
921             }
922         }
923         if (notification != nullptr) {
924             int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE;
925             UpdateRecentNotification(notification, true, reason);
926             NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, reason);
927         }
928     }));
929     notificationSvrQueue_->wait(handler);
930     return result;
931 }
932 
RemoveSystemLiveViewNotifications(const std::string & bundleName)933 ErrCode AdvancedNotificationService::RemoveSystemLiveViewNotifications(const std::string& bundleName)
934 {
935     std::vector<std::shared_ptr<NotificationRecord>> recordList;
936     EraseLiveViewSubsciber(bundleName);
937     if (notificationSvrQueue_ == nullptr) {
938         ANS_LOGE("NotificationSvrQueue is nullptr");
939         return ERR_ANS_INVALID_PARAM;
940     }
941     ErrCode result = ERR_OK;
942     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
943         if (GetTargetRecordList(bundleName,  NotificationConstant::SlotType::LIVE_VIEW,
944             NotificationContent::Type::LOCAL_LIVE_VIEW, recordList) != ERR_OK) {
945             ANS_LOGE("Get Target record list fail.");
946             result = ERR_ANS_NOTIFICATION_NOT_EXISTS;
947             return;
948         }
949         result = RemoveNotificationFromRecordList(recordList);
950     }));
951     notificationSvrQueue_->wait(handler);
952     return result;
953 }
954 
TriggerLocalLiveView(const sptr<NotificationBundleOption> & bundleOption,const int32_t notificationId,const sptr<NotificationButtonOption> & buttonOption)955 ErrCode AdvancedNotificationService::TriggerLocalLiveView(const sptr<NotificationBundleOption> &bundleOption,
956     const int32_t notificationId, const sptr<NotificationButtonOption> &buttonOption)
957 {
958     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
959     ANS_LOGD("%{public}s", __FUNCTION__);
960 
961     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
962     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
963         return ERR_ANS_NON_SYSTEM_APP;
964     }
965 
966     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
967         ANS_LOGD("CheckPermission is bogus.");
968         return ERR_ANS_PERMISSION_DENIED;
969     }
970 
971     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
972     if (bundle == nullptr) {
973         return ERR_ANS_INVALID_BUNDLE;
974     }
975 
976     ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS;
977     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
978         ANS_LOGD("ffrt enter!");
979         sptr<Notification> notification = nullptr;
980 
981         for (auto record : notificationList_) {
982             if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) &&
983                 (record->bundleOption->GetUid() == bundle->GetUid()) &&
984                 (record->notification->GetId() == notificationId)) {
985                 notification = record->notification;
986                 result = ERR_OK;
987                 break;
988             }
989         }
990 
991         if (notification != nullptr) {
992             NotificationLocalLiveViewSubscriberManager::GetInstance()->NotifyTriggerResponse(notification,
993                 buttonOption);
994         }
995     }));
996     notificationSvrQueue_->wait(handler);
997     return result;
998 }
999 
RemoveNotification(const sptr<NotificationBundleOption> & bundleOption,int32_t notificationId,const std::string & label,int32_t removeReason)1000 ErrCode AdvancedNotificationService::RemoveNotification(const sptr<NotificationBundleOption> &bundleOption,
1001     int32_t notificationId, const std::string &label, int32_t removeReason)
1002 {
1003     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1004     ANS_LOGD("%{public}s", __FUNCTION__);
1005 
1006     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1007     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1008         return ERR_ANS_NON_SYSTEM_APP;
1009     }
1010 
1011     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1012         ANS_LOGD("CheckPermission is bogus.");
1013         return ERR_ANS_PERMISSION_DENIED;
1014     }
1015 
1016     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1017     if (bundle == nullptr) {
1018         return ERR_ANS_INVALID_BUNDLE;
1019     }
1020 
1021     if (notificationSvrQueue_ == nullptr) {
1022         ANS_LOGE("NotificationSvrQueue_ is null.");
1023         return ERR_ANS_INVALID_PARAM;
1024     }
1025     ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS;
1026     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1027         ANS_LOGD("ffrt enter!");
1028         sptr<Notification> notification = nullptr;
1029         sptr<NotificationRequest> notificationRequest = nullptr;
1030 
1031 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1032         std::string deviceId;
1033         std::string bundleName;
1034 #endif
1035         for (auto record : notificationList_) {
1036             if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) &&
1037                 (record->bundleOption->GetUid() == bundle->GetUid()) &&
1038 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1039                 (record->deviceId.empty()) &&
1040 #endif
1041                 (record->notification->GetId() == notificationId) && (record->notification->GetLabel() == label)) {
1042                 if (!record->notification->IsRemoveAllowed()) {
1043                     result = ERR_ANS_NOTIFICATION_IS_UNALLOWED_REMOVEALLOWED;
1044                     break;
1045                 }
1046 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1047                 deviceId = record->deviceId;
1048                 bundleName = record->bundleName;
1049 #endif
1050                 notification = record->notification;
1051                 notificationRequest = record->request;
1052 
1053                 if (removeReason != NotificationConstant::CLICK_REASON_DELETE) {
1054                     ProcForDeleteLiveView(record);
1055                 }
1056 
1057                 notificationList_.remove(record);
1058                 result = ERR_OK;
1059                 break;
1060             }
1061         }
1062 
1063         if (notification != nullptr) {
1064             UpdateRecentNotification(notification, true, removeReason);
1065             NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr, removeReason);
1066 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1067             DoDistributedDelete(deviceId, bundleName, notification);
1068 #endif
1069         }
1070         if (removeReason != NotificationConstant::CLICK_REASON_DELETE) {
1071             TriggerRemoveWantAgent(notificationRequest);
1072         }
1073     }));
1074     notificationSvrQueue_->wait(handler);
1075 
1076     SendRemoveHiSysEvent(notificationId, label, bundleOption, result);
1077     return result;
1078 }
1079 
RemoveAllNotifications(const sptr<NotificationBundleOption> & bundleOption)1080 ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptr<NotificationBundleOption> &bundleOption)
1081 {
1082     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1083     ANS_LOGD("%{public}s", __FUNCTION__);
1084 
1085     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1086     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1087         return ERR_ANS_NON_SYSTEM_APP;
1088     }
1089 
1090     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1091         ANS_LOGD("CheckPermission is fake.");
1092         return ERR_ANS_PERMISSION_DENIED;
1093     }
1094 
1095     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1096     if (bundle == nullptr) {
1097         return ERR_ANS_INVALID_BUNDLE;
1098     }
1099 
1100     if (notificationSvrQueue_ == nullptr) {
1101         ANS_LOGE("Serial queue is nullptr.");
1102         return ERR_ANS_INVALID_PARAM;
1103     }
1104     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1105         std::vector<std::shared_ptr<NotificationRecord>> removeList;
1106         int32_t reason = NotificationConstant::CANCEL_REASON_DELETE;
1107         ANS_LOGD("ffrt enter!");
1108         for (auto record : notificationList_) {
1109             bool isAllowedNotification = true;
1110             if (IsAllowedNotifyForBundle(bundleOption, isAllowedNotification) != ERR_OK) {
1111                 ANSR_LOGW("The application does not request enable notification.");
1112             }
1113             if (!record->notification->IsRemoveAllowed() && isAllowedNotification) {
1114                 continue;
1115             }
1116             if (record->slot != nullptr) {
1117                 if (record->slot->GetForceControl() && record->slot->GetEnable()) {
1118                     continue;
1119                 }
1120             }
1121             if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) &&
1122                 (record->bundleOption->GetUid() == bundle->GetUid())
1123 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1124                 && record->deviceId.empty()
1125 #endif
1126                 ) {
1127                 ProcForDeleteLiveView(record);
1128                 removeList.push_back(record);
1129             }
1130         }
1131 
1132         std::vector<sptr<Notification>> notifications;
1133         for (auto record : removeList) {
1134             notificationList_.remove(record);
1135             if (record->notification != nullptr) {
1136                 ANS_LOGD("record->notification is not nullptr.");
1137                 UpdateRecentNotification(record->notification, true, reason);
1138                 notifications.emplace_back(record->notification);
1139 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1140                 DoDistributedDelete(record->deviceId, record->bundleName, record->notification);
1141 #endif
1142             }
1143             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
1144                 SendNotificationsOnCanceled(notifications, nullptr, reason);
1145             }
1146 
1147             TriggerRemoveWantAgent(record->request);
1148         }
1149 
1150         if (!notifications.empty()) {
1151             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason);
1152         }
1153     }));
1154     notificationSvrQueue_->wait(handler);
1155 
1156     return ERR_OK;
1157 }
1158 
RemoveNotifications(const std::vector<std::string> & keys,int32_t removeReason)1159 ErrCode AdvancedNotificationService::RemoveNotifications(
1160     const std::vector<std::string> &keys, int32_t removeReason)
1161 {
1162     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1163     ANS_LOGD("enter");
1164 
1165     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1166     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1167         return ERR_ANS_NON_SYSTEM_APP;
1168     }
1169 
1170     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1171         return ERR_ANS_PERMISSION_DENIED;
1172     }
1173 
1174     if (notificationSvrQueue_ == nullptr) {
1175         ANS_LOGE("NotificationSvrQueue is nullptr.");
1176         return ERR_ANS_INVALID_PARAM;
1177     }
1178     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1179         std::vector<sptr<Notification>> notifications;
1180         for (auto key : keys) {
1181             sptr<Notification> notification = nullptr;
1182 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1183             std::string deviceId;
1184             std::string bundleName;
1185             GetDistributedInfo(key, deviceId, bundleName);
1186 #endif
1187             ErrCode result = RemoveFromNotificationList(key, notification, false, removeReason);
1188             if (result != ERR_OK) {
1189                 continue;
1190             }
1191             if (notification != nullptr) {
1192                 UpdateRecentNotification(notification, true, removeReason);
1193                 notifications.emplace_back(notification);
1194 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1195                 DoDistributedDelete(deviceId, bundleName, notification);
1196 #endif
1197             }
1198             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
1199                 std::vector<sptr<Notification>> currNotificationList = notifications;
1200                 NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
1201                     currNotificationList, nullptr, removeReason);
1202                 notifications.clear();
1203             }
1204         }
1205 
1206         if (!notifications.empty()) {
1207             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, removeReason);
1208         }
1209     }));
1210     notificationSvrQueue_->wait(handler);
1211 
1212     return ERR_OK;
1213 }
1214 
RemoveNotificationBySlot(const sptr<NotificationBundleOption> & bundleOption,const sptr<NotificationSlot> & slot)1215 ErrCode AdvancedNotificationService::RemoveNotificationBySlot(const sptr<NotificationBundleOption> &bundleOption,
1216     const sptr<NotificationSlot> &slot)
1217 {
1218     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1219     ANS_LOGD("%{public}s", __FUNCTION__);
1220 
1221     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1222     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1223         return ERR_ANS_NON_SYSTEM_APP;
1224     }
1225 
1226     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1227         ANS_LOGD("CheckPermission is bogus.");
1228         return ERR_ANS_PERMISSION_DENIED;
1229     }
1230 
1231     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1232     if (bundle == nullptr) {
1233         return ERR_ANS_INVALID_BUNDLE;
1234     }
1235 
1236     ErrCode result = ERR_ANS_NOTIFICATION_NOT_EXISTS;
1237     sptr<Notification> notification = nullptr;
1238     sptr<NotificationRequest> notificationRequest = nullptr;
1239 
1240     for (auto record : notificationList_) {
1241         if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) &&
1242             (record->bundleOption->GetUid() == bundle->GetUid()) &&
1243             (record->request->GetSlotType() == slot->GetType())) {
1244             if (!record->notification->IsRemoveAllowed() || !record->request->IsCommonLiveView()) {
1245                 continue;
1246             }
1247 
1248             notification = record->notification;
1249             notificationRequest = record->request;
1250 
1251             ProcForDeleteLiveView(record);
1252             notificationList_.remove(record);
1253 
1254             if (notification != nullptr) {
1255                 UpdateRecentNotification(notification, true, NotificationConstant::CANCEL_REASON_DELETE);
1256                 NotificationSubscriberManager::GetInstance()->NotifyCanceled(notification, nullptr,
1257                     NotificationConstant::CANCEL_REASON_DELETE);
1258             }
1259 
1260             TriggerRemoveWantAgent(notificationRequest);
1261             result = ERR_OK;
1262         }
1263     }
1264 
1265     return result;
1266 }
1267 
CancelGroup(const std::string & groupName)1268 ErrCode AdvancedNotificationService::CancelGroup(const std::string &groupName)
1269 {
1270     ANS_LOGD("%{public}s", __FUNCTION__);
1271 
1272     if (groupName.empty()) {
1273         return ERR_ANS_INVALID_PARAM;
1274     }
1275 
1276     sptr<NotificationBundleOption> bundleOption = GenerateBundleOption();
1277     if (bundleOption == nullptr) {
1278         return ERR_ANS_INVALID_BUNDLE;
1279     }
1280 
1281     if (notificationSvrQueue_ == nullptr) {
1282         ANS_LOGE("Serial queue is invalid.");
1283         return ERR_ANS_INVALID_PARAM;
1284     }
1285     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1286         ANS_LOGD("ffrt enter!");
1287         std::vector<std::shared_ptr<NotificationRecord>> removeList;
1288         for (auto record : notificationList_) {
1289             if ((record->bundleOption->GetBundleName() == bundleOption->GetBundleName()) &&
1290                 (record->bundleOption->GetUid() == bundleOption->GetUid()) &&
1291 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1292                 record->deviceId.empty() &&
1293 #endif
1294                 (record->request->GetGroupName() == groupName)) {
1295                 removeList.push_back(record);
1296             }
1297         }
1298 
1299         std::vector<sptr<Notification>> notifications;
1300         for (auto record : removeList) {
1301             notificationList_.remove(record);
1302 
1303             if (record->notification != nullptr) {
1304                 int32_t reason = NotificationConstant::APP_CANCEL_REASON_DELETE;
1305                 UpdateRecentNotification(record->notification, true, reason);
1306                 notifications.emplace_back(record->notification);
1307 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1308                 DoDistributedDelete(record->deviceId, record->bundleName, record->notification);
1309 #endif
1310             }
1311             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
1312                 std::vector<sptr<Notification>> currNotificationList = notifications;
1313                 NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
1314                     currNotificationList, nullptr, NotificationConstant::APP_CANCEL_REASON_DELETE);
1315                 notifications.clear();
1316             }
1317         }
1318 
1319         if (!notifications.empty()) {
1320             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
1321                 notifications, nullptr, NotificationConstant::APP_CANCEL_REASON_DELETE);
1322         }
1323     }));
1324     notificationSvrQueue_->wait(handler);
1325 
1326     return ERR_OK;
1327 }
1328 
RemoveGroupByBundle(const sptr<NotificationBundleOption> & bundleOption,const std::string & groupName)1329 ErrCode AdvancedNotificationService::RemoveGroupByBundle(
1330     const sptr<NotificationBundleOption> &bundleOption, const std::string &groupName)
1331 {
1332     ANS_LOGD("%{public}s", __FUNCTION__);
1333 
1334     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1335     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1336         return ERR_ANS_NON_SYSTEM_APP;
1337     }
1338 
1339     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1340         return ERR_ANS_PERMISSION_DENIED;
1341     }
1342 
1343     if (bundleOption == nullptr || groupName.empty()) {
1344         return ERR_ANS_INVALID_PARAM;
1345     }
1346 
1347     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1348     if (bundle == nullptr) {
1349         return ERR_ANS_INVALID_BUNDLE;
1350     }
1351 
1352     if (notificationSvrQueue_ == nullptr) {
1353         ANS_LOGE("Serial queue is invalid.");
1354         return ERR_ANS_INVALID_PARAM;
1355     }
1356     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1357         ANS_LOGD("ffrt enter!");
1358         std::vector<std::shared_ptr<NotificationRecord>> removeList;
1359         int32_t reason = NotificationConstant::CANCEL_REASON_DELETE;
1360         for (auto record : notificationList_) {
1361             if (!record->notification->IsRemoveAllowed()) {
1362                 continue;
1363             }
1364             if ((record->bundleOption->GetBundleName() == bundle->GetBundleName()) &&
1365                 (record->bundleOption->GetUid() == bundle->GetUid()) && !record->request->IsUnremovable() &&
1366 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1367                 record->deviceId.empty() &&
1368 #endif
1369                 (record->request->GetGroupName() == groupName)) {
1370                 ANS_LOGD("RemoveList push enter.");
1371                 removeList.push_back(record);
1372             }
1373         }
1374 
1375         std::vector<sptr<Notification>> notifications;
1376         for (auto record : removeList) {
1377             notificationList_.remove(record);
1378             ProcForDeleteLiveView(record);
1379 
1380             if (record->notification != nullptr) {
1381                 UpdateRecentNotification(record->notification, true, reason);
1382                 notifications.emplace_back(record->notification);
1383 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1384                 DoDistributedDelete(record->deviceId, record->bundleName, record->notification);
1385 #endif
1386             }
1387             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
1388                 SendNotificationsOnCanceled(notifications, nullptr, reason);
1389             }
1390         }
1391 
1392         if (!notifications.empty()) {
1393             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(notifications, nullptr, reason);
1394         }
1395     }));
1396     notificationSvrQueue_->wait(handler);
1397 
1398     return ERR_OK;
1399 }
1400 
RemoveNotificationFromRecordList(const std::vector<std::shared_ptr<NotificationRecord>> & recordList)1401 ErrCode AdvancedNotificationService::RemoveNotificationFromRecordList(
1402     const std::vector<std::shared_ptr<NotificationRecord>>& recordList)
1403 {
1404     ErrCode result = ERR_OK;
1405         std::vector<sptr<Notification>> notifications;
1406         for (auto& record : recordList) {
1407             std::string key = record->notification->GetKey();
1408             sptr<Notification> notification = nullptr;
1409 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1410             std::string deviceId;
1411             std::string bundleName;
1412             GetDistributedInfo(key, deviceId, bundleName);
1413 #endif
1414             result = RemoveFromNotificationList(key, notification, true,
1415                 NotificationConstant::USER_STOPPED_REASON_DELETE);
1416             if (result != ERR_OK) {
1417                 continue;
1418             }
1419             if (notification != nullptr) {
1420                 int32_t reason = NotificationConstant::USER_STOPPED_REASON_DELETE;
1421                 UpdateRecentNotification(notification, true, reason);
1422                 notifications.emplace_back(notification);
1423 #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED
1424             DoDistributedDelete(deviceId, bundleName, notification);
1425 #endif
1426             }
1427             if (notifications.size() >= MAX_CANCELED_PARCELABLE_VECTOR_NUM) {
1428                 std::vector<sptr<Notification>> currNotificationList = notifications;
1429                 NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
1430                     currNotificationList, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE);
1431                 notifications.clear();
1432             }
1433         }
1434         if (!notifications.empty()) {
1435             NotificationSubscriberManager::GetInstance()->BatchNotifyCanceled(
1436                 notifications, nullptr, NotificationConstant::USER_STOPPED_REASON_DELETE);
1437         }
1438         return result;
1439 }
1440 
IsSpecialUserAllowedNotify(const int32_t & userId,bool & allowed)1441 ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(const int32_t &userId, bool &allowed)
1442 {
1443     ANS_LOGD("%{public}s", __FUNCTION__);
1444 
1445     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1446     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1447         return ERR_ANS_NON_SYSTEM_APP;
1448     }
1449 
1450     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1451         ANS_LOGD("Failed to checkPermission");
1452         return ERR_ANS_PERMISSION_DENIED;
1453     }
1454 
1455     if (notificationSvrQueue_ == nullptr) {
1456         ANS_LOGE("NotificationSvrQueue_ is nullptr.");
1457         return ERR_ANS_INVALID_PARAM;
1458     }
1459     ErrCode result = ERR_OK;
1460     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1461         ANS_LOGD("ffrt enter!");
1462         allowed = false;
1463         result = NotificationPreferences::GetInstance().GetNotificationsEnabled(userId, allowed);
1464     }));
1465     notificationSvrQueue_->wait(handler);
1466     return result;
1467 }
1468 
SetNotificationsEnabledByUser(const int32_t & userId,bool enabled)1469 ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(const int32_t &userId, bool enabled)
1470 {
1471     ANS_LOGD("%{public}s", __FUNCTION__);
1472 
1473     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1474     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1475         return ERR_ANS_NON_SYSTEM_APP;
1476     }
1477 
1478     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1479         return ERR_ANS_PERMISSION_DENIED;
1480     }
1481 
1482     if (notificationSvrQueue_ == nullptr) {
1483         ANS_LOGE("Serial queue is ineffectiveness.");
1484         return ERR_ANS_INVALID_PARAM;
1485     }
1486     ErrCode result = ERR_OK;
1487     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1488         ANS_LOGD("ffrt enter!");
1489         result = NotificationPreferences::GetInstance().SetNotificationsEnabled(userId, enabled);
1490     }));
1491     notificationSvrQueue_->wait(handler);
1492     return result;
1493 }
1494 
SetEnabledForBundleSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,bool isForceControl)1495 ErrCode AdvancedNotificationService::SetEnabledForBundleSlot(const sptr<NotificationBundleOption> &bundleOption,
1496     const NotificationConstant::SlotType &slotType, bool enabled, bool isForceControl)
1497 {
1498     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1499     ANS_LOGD("slotType: %{public}d, enabled: %{public}d, isForceControl: %{public}d",
1500         slotType, enabled, isForceControl);
1501 
1502     ErrCode result = CheckCommonParams();
1503     if (result != ERR_OK) {
1504         return result;
1505     }
1506 
1507     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1508     if (bundle == nullptr) {
1509         return ERR_ANS_INVALID_BUNDLE;
1510     }
1511 
1512     result = ERR_OK;
1513     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1514         sptr<NotificationSlot> slot;
1515         result = NotificationPreferences::GetInstance().GetNotificationSlot(bundle, slotType, slot);
1516         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_SLOT_TYPE_NOT_EXIST ||
1517             result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
1518             slot = new (std::nothrow) NotificationSlot(slotType);
1519             if (slot == nullptr) {
1520                 ANS_LOGE("Failed to create NotificationSlot ptr.");
1521                 result = ERR_ANS_NO_MEMORY;
1522                 return;
1523             }
1524         } else if ((result == ERR_OK) && (slot != nullptr)) {
1525             if (slot->GetEnable() == enabled && slot->GetForceControl() == isForceControl) {
1526                 return;
1527             }
1528             NotificationPreferences::GetInstance().RemoveNotificationSlot(bundle, slotType);
1529         } else {
1530             ANS_LOGE("Set enable slot: GetNotificationSlot failed");
1531             return;
1532         }
1533         bool allowed = false;
1534         result = NotificationPreferences::GetInstance().GetNotificationsEnabledForBundle(bundle, allowed);
1535         if (result == ERR_ANS_PREFERENCES_NOTIFICATION_BUNDLE_NOT_EXIST) {
1536             result = ERR_OK;
1537             allowed = CheckApiCompatibility(bundle);
1538             SetDefaultNotificationEnabled(bundle, allowed);
1539         }
1540         if (!slot->GetEnable()) {
1541             RemoveNotificationBySlot(bundle, slot);
1542         } else {
1543             if (!slot->GetForceControl() && !allowed) {
1544                 RemoveNotificationBySlot(bundle, slot);
1545             }
1546         }
1547         slot->SetEnable(enabled);
1548         slot->SetForceControl(isForceControl);
1549         std::vector<sptr<NotificationSlot>> slots;
1550         slots.push_back(slot);
1551         result = NotificationPreferences::GetInstance().AddNotificationSlots(bundle, slots);
1552         if (result != ERR_OK) {
1553             ANS_LOGE("Set enable slot: AddNotificationSlot failed");
1554             return;
1555         }
1556 
1557         PublishSlotChangeCommonEvent(bundle);
1558     }));
1559     notificationSvrQueue_->wait(handler);
1560 
1561     SendEnableNotificationSlotHiSysEvent(bundleOption, slotType, enabled, result);
1562     return result;
1563 }
1564 
GetEnabledForBundleSlot(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool & enabled)1565 ErrCode AdvancedNotificationService::GetEnabledForBundleSlot(
1566     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled)
1567 {
1568     ANS_LOGD("slotType: %{public}d", slotType);
1569 
1570     bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1571     if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1572         ANS_LOGD("VerifyNativeToken and isSystemApp failed.");
1573         return ERR_ANS_NON_SYSTEM_APP;
1574     }
1575 
1576     if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
1577         return ERR_ANS_PERMISSION_DENIED;
1578     }
1579 
1580     sptr<NotificationBundleOption> bundle = GenerateValidBundleOption(bundleOption);
1581     if (bundle == nullptr) {
1582         return ERR_ANS_INVALID_BUNDLE;
1583     }
1584 
1585     if (notificationSvrQueue_ == nullptr) {
1586         ANS_LOGE("Serial queue is invalid.");
1587         return ERR_ANS_INVALID_PARAM;
1588     }
1589     ErrCode result = ERR_OK;
1590     ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() {
1591         ANS_LOGD("ffrt enter!");
1592         sptr<NotificationSlot> slot;
1593         result = NotificationPreferences::GetInstance().GetNotificationSlot(bundle, slotType, slot);
1594         if (result != ERR_OK) {
1595             ANS_LOGE("Get enable slot: GetNotificationSlot failed");
1596             return;
1597         }
1598         if (slot == nullptr) {
1599             ANS_LOGW("Get enable slot: object is null, enabled default true");
1600             enabled = true;
1601             result = ERR_OK;
1602             return;
1603         }
1604         enabled = slot->GetEnable();
1605     }));
1606     notificationSvrQueue_->wait(handler);
1607 
1608     return result;
1609 }
1610 
PublishNotificationBySa(const sptr<NotificationRequest> & request)1611 ErrCode AdvancedNotificationService::PublishNotificationBySa(const sptr<NotificationRequest> &request)
1612 {
1613     ANS_LOGD("%{public}s", __FUNCTION__);
1614 
1615     int32_t uid = request->GetCreatorUid();
1616     if (uid <= 0) {
1617         ANS_LOGE("CreatorUid[%{public}d] error", uid);
1618         return ERR_ANS_INVALID_UID;
1619     }
1620     std::string bundle = "";
1621     ErrCode result = PrePublishNotificationBySa(request, uid, bundle);
1622     if (result != ERR_OK) {
1623         return result;
1624     }
1625 
1626     std::shared_ptr<NotificationRecord> record = std::make_shared<NotificationRecord>();
1627     record->request = request;
1628     record->bundleOption = new (std::nothrow) NotificationBundleOption(bundle, uid);
1629     if (record->bundleOption == nullptr) {
1630         ANS_LOGE("Failed to create bundleOption");
1631         return ERR_ANS_NO_MEMORY;
1632     }
1633     record->notification = new (std::nothrow) Notification(request);
1634     if (record->notification == nullptr) {
1635         ANS_LOGE("Failed to create notification");
1636         return ERR_ANS_NO_MEMORY;
1637     }
1638 
1639     if (notificationSvrQueue_ == nullptr) {
1640         ANS_LOGE("Serial queue is invalid.");
1641         return ERR_ANS_INVALID_PARAM;
1642     }
1643     ffrt::task_handle handler = notificationSvrQueue_->submit_h([this, &record]() {
1644         if (!record->bundleOption->GetBundleName().empty()) {
1645             ErrCode ret = AssignValidNotificationSlot(record);
1646             if (ret != ERR_OK) {
1647                 ANS_LOGE("Can not assign valid slot!");
1648             }
1649         }
1650         if (AssignToNotificationList(record) != ERR_OK) {
1651             ANS_LOGE("Failed to assign notification list");
1652             return;
1653         }
1654 
1655         UpdateRecentNotification(record->notification, false, 0);
1656         sptr<NotificationSortingMap> sortingMap = GenerateSortingMap();
1657         NotificationSubscriberManager::GetInstance()->NotifyConsumed(record->notification, sortingMap);
1658     });
1659     notificationSvrQueue_->wait(handler);
1660 
1661     return result;
1662 }
1663 
SetBadgeNumber(int32_t badgeNumber)1664 ErrCode AdvancedNotificationService::SetBadgeNumber(int32_t badgeNumber)
1665 {
1666     ANS_LOGD("%{public}s", __FUNCTION__);
1667     if (notificationSvrQueue_ == nullptr) {
1668         ANS_LOGE("Serial queue is invalid.");
1669         return ERR_ANS_INVALID_PARAM;
1670     }
1671     int32_t callingUid = IPCSkeleton::GetCallingUid();
1672     std::string bundleName = GetClientBundleName();
1673     sptr<BadgeNumberCallbackData> badgeData = new (std::nothrow) BadgeNumberCallbackData(
1674         bundleName, callingUid, badgeNumber);
1675     if (badgeData == nullptr) {
1676         ANS_LOGE("Failed to create BadgeNumberCallbackData.");
1677         return ERR_ANS_NO_MEMORY;
1678     }
1679 
1680     ffrt::task_handle handler = notificationSvrQueue_->submit_h([&]() {
1681         ANS_LOGD("ffrt enter!");
1682         NotificationSubscriberManager::GetInstance()->SetBadgeNumber(badgeData);
1683     });
1684     notificationSvrQueue_->wait(handler);
1685     return ERR_OK;
1686 }
1687 
AddLiveViewSubscriber()1688 void AdvancedNotificationService::AddLiveViewSubscriber()
1689 {
1690     std::string bundleName = GetClientBundleName();
1691     std::lock_guard<std::mutex> lock(liveViewMutext_);
1692     localLiveViewSubscribedList_.emplace(bundleName);
1693 }
1694 
EraseLiveViewSubsciber(const std::string & bundleName)1695 void AdvancedNotificationService::EraseLiveViewSubsciber(const std::string &bundleName)
1696 {
1697     std::lock_guard<std::mutex> lock(liveViewMutext_);
1698     localLiveViewSubscribedList_.erase(bundleName);
1699 }
1700 
GetLiveViewSubscribeState(const std::string & bundleName)1701 bool AdvancedNotificationService::GetLiveViewSubscribeState(const std::string &bundleName)
1702 {
1703     std::lock_guard<std::mutex> lock(liveViewMutext_);
1704     if (localLiveViewSubscribedList_.find(bundleName) == localLiveViewSubscribedList_.end()) {
1705         return false;
1706     }
1707     return true;
1708 }
1709 
SubscribeLocalLiveView(const sptr<AnsSubscriberLocalLiveViewInterface> & subscriber,const sptr<NotificationSubscribeInfo> & info)1710 ErrCode AdvancedNotificationService::SubscribeLocalLiveView(
1711     const sptr<AnsSubscriberLocalLiveViewInterface> &subscriber, const sptr<NotificationSubscribeInfo> &info)
1712 {
1713     HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__);
1714     ANS_LOGD("%{public}s", __FUNCTION__);
1715 
1716     ErrCode errCode = ERR_OK;
1717     do {
1718         if (subscriber == nullptr) {
1719             errCode = ERR_ANS_INVALID_PARAM;
1720             break;
1721         }
1722 
1723         bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID());
1724         if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) {
1725             ANS_LOGE("Client is not a system app or subsystem");
1726             errCode = ERR_ANS_NON_SYSTEM_APP;
1727             break;
1728         }
1729 
1730         errCode = NotificationLocalLiveViewSubscriberManager::GetInstance()->AddLocalLiveViewSubscriber(
1731             subscriber, info);
1732         if (errCode != ERR_OK) {
1733             break;
1734         }
1735     } while (0);
1736     if (errCode == ERR_OK) {
1737         AddLiveViewSubscriber();
1738     }
1739     SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode);
1740     return errCode;
1741 }
1742 }  // namespace Notification
1743 }  // namespace OHOS
1744