From 44264200b84790e4f6fc8437b42442addad0844d Mon Sep 17 00:00:00 2001 From: zengsiyu Date: Thu, 5 May 2022 22:18:47 +0800 Subject: [PATCH] feat: agent notification Signed-off-by: zengsiyu Change-Id: I547d9cbff68c122a5d401aabf80ee078aaf2fa8b Signed-off-by: zengsiyu --- .../core/common/include/ans_permission_def.h | 4 +- .../ans/core/include/ans_manager_interface.h | 14 ++ .../ans/core/include/ans_manager_proxy.h | 14 +- .../ans/core/include/ans_manager_stub.h | 16 +- .../ans/core/include/ans_notification.h | 12 ++ frameworks/ans/core/src/ans_manager_proxy.cpp | 40 ++++ frameworks/ans/core/src/ans_manager_stub.cpp | 39 +++- frameworks/ans/core/src/ans_notification.cpp | 12 +- frameworks/ans/native/src/notification.cpp | 2 +- .../ans/native/src/notification_helper.cpp | 7 + .../ans/native/src/notification_request.cpp | 63 +++++- .../ans/native/include/notification.h | 2 +- .../ans/native/include/notification_helper.h | 12 ++ .../ans/native/include/notification_request.h | 41 +++- interfaces/kits/napi/ans/include/cancel.h | 1 + interfaces/kits/napi/ans/include/common.h | 84 ++++---- interfaces/kits/napi/ans/include/publish.h | 1 + interfaces/kits/napi/ans/src/cancel.cpp | 180 ++++++++++++++++-- interfaces/kits/napi/ans/src/common.cpp | 36 +++- interfaces/kits/napi/ans/src/init.cpp | 2 + interfaces/kits/napi/ans/src/publish.cpp | 128 ++++++++++++- sa_profile/3203.xml | 0 .../include/advanced_notification_service.h | 18 +- services/ans/resource/external.json | 0 .../ans/src/advanced_notification_service.cpp | 138 ++++++++++---- services/ans/src/bundle_manager_helper.cpp | 6 +- 26 files changed, 741 insertions(+), 131 deletions(-) mode change 100644 => 100755 sa_profile/3203.xml mode change 100644 => 100755 services/ans/resource/external.json diff --git a/frameworks/ans/core/common/include/ans_permission_def.h b/frameworks/ans/core/common/include/ans_permission_def.h index a236a61ed..427900b87 100644 --- a/frameworks/ans/core/common/include/ans_permission_def.h +++ b/frameworks/ans/core/common/include/ans_permission_def.h @@ -21,8 +21,8 @@ namespace OHOS { namespace Notification { // Permission -const std::string OHOS_PERMISSION_NOTIFICATION_CONTROLLER = "ohos.permission.NOTIFICATION_CONTROLLER"; -const std::string OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER = "ohos.permission.NOTIFICATION_AGENT_CONTROLLER"; +constexpr char OHOS_PERMISSION_NOTIFICATION_CONTROLLER[] = "ohos.permission.NOTIFICATION_CONTROLLER"; +constexpr char OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER[] = "ohos.permission.NOTIFICATION_AGENT_CONTROLLER"; } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/core/include/ans_manager_interface.h b/frameworks/ans/core/include/ans_manager_interface.h index f88e61e28..960f1e29b 100644 --- a/frameworks/ans/core/include/ans_manager_interface.h +++ b/frameworks/ans/core/include/ans_manager_interface.h @@ -81,6 +81,19 @@ public: */ virtual ErrCode CancelAll() = 0; + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + virtual ErrCode CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) = 0; + /** * @brief Adds a notification slot by type. * @@ -709,6 +722,7 @@ protected: PUBLISH_NOTIFICATION_TO_DEVICE, CANCEL_NOTIFICATION, CANCEL_ALL_NOTIFICATIONS, + CANCEL_AS_BUNDLE, ADD_SLOT_BY_TYPE, ADD_SLOTS, REMOVE_SLOT_BY_TYPE, diff --git a/frameworks/ans/core/include/ans_manager_proxy.h b/frameworks/ans/core/include/ans_manager_proxy.h index 61c95a8cd..c8ccab397 100644 --- a/frameworks/ans/core/include/ans_manager_proxy.h +++ b/frameworks/ans/core/include/ans_manager_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -69,6 +69,18 @@ public: */ ErrCode CancelAll() override; + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + ErrCode CancelAsBundle(int32_t notificationId, const std::string &representativeBundle, int32_t userId) override; + /** * @brief Adds a notification slot by type. * diff --git a/frameworks/ans/core/include/ans_manager_stub.h b/frameworks/ans/core/include/ans_manager_stub.h index 7b7e9e7e8..09a09586a 100644 --- a/frameworks/ans/core/include/ans_manager_stub.h +++ b/frameworks/ans/core/include/ans_manager_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -83,6 +83,19 @@ public: */ virtual ErrCode CancelAll() override; + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + virtual ErrCode CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) override; + /** * @brief Adds a notification slot by type. * @@ -720,6 +733,7 @@ private: ErrCode HandlePublishToDevice(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancel(MessageParcel &data, MessageParcel &reply); ErrCode HandleCancelAll(MessageParcel &data, MessageParcel &reply); + ErrCode HandleCancelAsBundle(MessageParcel &data, MessageParcel &reply); ErrCode HandleAddSlotByType(MessageParcel &data, MessageParcel &reply); ErrCode HandleAddSlots(MessageParcel &data, MessageParcel &reply); ErrCode HandleRemoveSlotByType(MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/ans/core/include/ans_notification.h b/frameworks/ans/core/include/ans_notification.h index 3a773d3cf..57d1f8994 100644 --- a/frameworks/ans/core/include/ans_notification.h +++ b/frameworks/ans/core/include/ans_notification.h @@ -213,6 +213,18 @@ public: */ ErrCode CancelAllNotifications(); + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + ErrCode CancelAsBundle(int32_t notificationId, const std::string &representativeBundle, int32_t userId); + /** * @brief Obtains the number of active notifications of the current application in the system. * diff --git a/frameworks/ans/core/src/ans_manager_proxy.cpp b/frameworks/ans/core/src/ans_manager_proxy.cpp index 79fc53976..70293f09e 100644 --- a/frameworks/ans/core/src/ans_manager_proxy.cpp +++ b/frameworks/ans/core/src/ans_manager_proxy.cpp @@ -168,6 +168,46 @@ ErrCode AnsManagerProxy::CancelAll() return result; } +ErrCode AnsManagerProxy::CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGW("[CancelAsBundle] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(notificationId)) { + ANS_LOGW("[CancelAsBundle] fail: write notificationId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteString(representativeBundle)) { + ANS_LOGW("[CancelAsBundle] fail: write representativeBundle failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGW("[CancelAsBundle] fail: write userId failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(CANCEL_AS_BUNDLE, option, data, reply); + if (result != ERR_OK) { + ANS_LOGW("[CancelAsBundle] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGW("[CancelAsBundle] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + ErrCode AnsManagerProxy::AddSlotByType(NotificationConstant::SlotType slotType) { MessageParcel data; diff --git a/frameworks/ans/core/src/ans_manager_stub.cpp b/frameworks/ans/core/src/ans_manager_stub.cpp index 9e0acab19..3f1489e8a 100644 --- a/frameworks/ans/core/src/ans_manager_stub.cpp +++ b/frameworks/ans/core/src/ans_manager_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -40,6 +40,9 @@ const std::map(data.ReadInt32()); @@ -1803,6 +1834,12 @@ ErrCode AnsManagerStub::CancelAll() return ERR_INVALID_OPERATION; } +ErrCode AnsManagerStub::CancelAsBundle(int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + ANS_LOGW("AnsManagerStub::CancelAsBundle called!"); + return ERR_INVALID_OPERATION; +} + ErrCode AnsManagerStub::AddSlotByType(NotificationConstant::SlotType slotType) { ANS_LOGW("AnsManagerStub::AddSlotByType called!"); diff --git a/frameworks/ans/core/src/ans_notification.cpp b/frameworks/ans/core/src/ans_notification.cpp index 7090f50ba..81625449e 100644 --- a/frameworks/ans/core/src/ans_notification.cpp +++ b/frameworks/ans/core/src/ans_notification.cpp @@ -278,6 +278,16 @@ ErrCode AnsNotification::CancelAllNotifications() return ansManagerProxy_->CancelAll(); } +ErrCode AnsNotification::CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return ansManagerProxy_->CancelAsBundle(notificationId, representativeBundle, userId); +} + ErrCode AnsNotification::GetActiveNotificationNums(uint64_t &num) { if (!GetAnsManagerProxy()) { @@ -1312,7 +1322,7 @@ ErrCode AnsNotification::GetDoNotDisturbDate(const int32_t &userId, Notification ANS_LOGE("Input userId is invalid."); return ERR_ANS_INVALID_PARAM; } - + if (!GetAnsManagerProxy()) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; diff --git a/frameworks/ans/native/src/notification.cpp b/frameworks/ans/native/src/notification.cpp index 0773681bd..d84d422d2 100644 --- a/frameworks/ans/native/src/notification.cpp +++ b/frameworks/ans/native/src/notification.cpp @@ -145,7 +145,7 @@ Uri Notification::GetSound() const return Uri(""); } -pid_t Notification::GetUid() const +uid_t Notification::GetUid() const { if (request_ == nullptr) { return 0; diff --git a/frameworks/ans/native/src/notification_helper.cpp b/frameworks/ans/native/src/notification_helper.cpp index 94b50eb3c..294d93a01 100644 --- a/frameworks/ans/native/src/notification_helper.cpp +++ b/frameworks/ans/native/src/notification_helper.cpp @@ -115,6 +115,13 @@ ErrCode NotificationHelper::CancelAllNotifications() return DelayedSingleton::GetInstance()->CancelAllNotifications(); } +ErrCode NotificationHelper::CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + return DelayedSingleton::GetInstance()->CancelAsBundle( + notificationId, representativeBundle, userId); +} + ErrCode NotificationHelper::GetActiveNotificationNums(uint64_t &num) { return DelayedSingleton::GetInstance()->GetActiveNotificationNums(num); diff --git a/frameworks/ans/native/src/notification_request.cpp b/frameworks/ans/native/src/notification_request.cpp index 88a2beb7f..fd0ba7426 100644 --- a/frameworks/ans/native/src/notification_request.cpp +++ b/frameworks/ans/native/src/notification_request.cpp @@ -203,6 +203,16 @@ void NotificationRequest::SetPermitSystemGeneratedContextualActionButtons(bool p permitted_ = permitted; } +bool NotificationRequest::IsAgentNotification() const +{ + return isAgent_; +} + +void NotificationRequest::SetIsAgentNotification(bool isAgent) +{ + isAgent_ = isAgent; +} + void NotificationRequest::AddMessageUser(const std::shared_ptr &messageUser) { if (!messageUser) { @@ -585,16 +595,26 @@ pid_t NotificationRequest::GetCreatorPid() const return creatorPid_; } -void NotificationRequest::SetCreatorUid(pid_t uid) +void NotificationRequest::SetCreatorUid(uid_t uid) { creatorUid_ = uid; } -pid_t NotificationRequest::GetCreatorUid() const +uid_t NotificationRequest::GetCreatorUid() const { return creatorUid_; } +void NotificationRequest::SetOwnerUid(uid_t uid) +{ + ownerUid_ = uid; +} + +uid_t NotificationRequest::GetOwnerUid() const +{ + return ownerUid_; +} + void NotificationRequest::SetLabel(const std::string &label) { label_ = label; @@ -635,6 +655,16 @@ int32_t NotificationRequest::GetCreatorUserId() const return creatorUserId_; } +void NotificationRequest::SetOwnerUserId(int32_t userId) +{ + ownerUserId_ = userId; +} + +int32_t NotificationRequest::GetOwnerUserId() const +{ + return ownerUserId_; +} + std::string NotificationRequest::Dump() { return "NotificationRequest{ " @@ -645,7 +675,9 @@ std::string NotificationRequest::Dump() ", creatorBundleName = " + creatorBundleName_ + ", creatorPid = " + std::to_string(static_cast(creatorPid_)) + ", creatorUid = " + std::to_string(static_cast(creatorUid_)) + - ", ownerBundleName = " + ownerBundleName_ + ", groupName = " + groupName_ + + ", ownerBundleName = " + ownerBundleName_ + + ", ownerUid = " + std::to_string(static_cast(ownerUid_)) + + ", groupName = " + groupName_ + ", statusBarText = " + statusBarText_ + ", label = " + label_ + ", shortcutId = " + shortcutId_ + ", sortingKey = " + sortingKey_ + ", groupAlertType = " + std::to_string(static_cast(groupAlertType_)) + @@ -667,6 +699,7 @@ std::string NotificationRequest::Dump() ", unremovable = " + (unremovable_ ? "true" : "false") + ", floatingIcon = " + (floatingIcon_ ? "true" : "false") + ", onlyLocal = " + (onlyLocal_ ? "true" : "false") + ", permitted = " + (permitted_ ? "true" : "false") + + ", isAgent = " + (isAgent_ ? "true" : "false") + ", removalWantAgent = " + (removalWantAgent_ ? "not null" : "null") + ", maxScreenWantAgent = " + (maxScreenWantAgent_ ? "not null" : "null") + ", additionalParams = " + (additionalParams_ ? "not null" : "null") + @@ -681,6 +714,7 @@ std::string NotificationRequest::Dump() ", distributedOptions = " + distributedOptions_.Dump() + ", notificationFlags = " + (notificationFlags_ ? "not null" : "null") + ", creatorUserId = " + std::to_string(creatorUserId_) + + ", ownerUserId = " + std::to_string(ownerUserId_) + ", receiverUserId = " + std::to_string(receiverUserId_) + " }"; } @@ -848,11 +882,21 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteInt32(static_cast(ownerUid_))) { + ANS_LOGE("Failed to write owner uid"); + return false; + } + if (!parcel.WriteInt32(static_cast(creatorUserId_))) { ANS_LOGE("Failed to write creator userId"); return false; } + if (!parcel.WriteInt32(static_cast(ownerUserId_))) { + ANS_LOGE("Failed to write owner userId"); + return false; + } + if (!parcel.WriteInt32(static_cast(receiverUserId_))) { ANS_LOGE("Failed to write receiver userId"); return false; @@ -997,6 +1041,11 @@ bool NotificationRequest::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteBool(isAgent_)) { + ANS_LOGE("Failed to write flag indicating whether an agent notification"); + return false; + } + // write objects which managed by std::shared_ptr bool valid {false}; @@ -1191,8 +1240,10 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) autoDeletedTime_ = parcel.ReadInt64(); creatorPid_ = static_cast(parcel.ReadInt32()); - creatorUid_ = static_cast(parcel.ReadInt32()); + creatorUid_ = static_cast(parcel.ReadInt32()); + ownerUid_ = static_cast(parcel.ReadInt32()); creatorUserId_ = parcel.ReadInt32(); + ownerUserId_ = parcel.ReadInt32(); receiverUserId_ = parcel.ReadInt32(); if (!parcel.ReadString(settingsText_)) { @@ -1259,6 +1310,7 @@ bool NotificationRequest::ReadFromParcel(Parcel &parcel) floatingIcon_ = parcel.ReadBool(); onlyLocal_ = parcel.ReadBool(); permitted_ = parcel.ReadBool(); + isAgent_ = parcel.ReadBool(); bool valid {false}; @@ -1444,8 +1496,11 @@ void NotificationRequest::CopyBase(const NotificationRequest &other) this->creatorPid_ = other.creatorPid_; this->creatorUid_ = other.creatorUid_; + this->ownerUid_ = other.ownerUid_; this->creatorUserId_ = other.creatorUserId_; + this->ownerUserId_ = other.ownerUserId_; this->receiverUserId_ = other.receiverUserId_; + this->isAgent_ = other.isAgent_; this->slotType_ = other.slotType_; this->settingsText_ = other.settingsText_; diff --git a/interfaces/innerkits/ans/native/include/notification.h b/interfaces/innerkits/ans/native/include/notification.h index 79e022c93..cec3185f2 100644 --- a/interfaces/innerkits/ans/native/include/notification.h +++ b/interfaces/innerkits/ans/native/include/notification.h @@ -153,7 +153,7 @@ public: * * @return Returns the UID of the notification creator. */ - pid_t GetUid() const; + uid_t GetUid() const; /** * @brief Obtains the PID of the notification creator. diff --git a/interfaces/innerkits/ans/native/include/notification_helper.h b/interfaces/innerkits/ans/native/include/notification_helper.h index 4f7eeb9b7..8ebfb8b8a 100644 --- a/interfaces/innerkits/ans/native/include/notification_helper.h +++ b/interfaces/innerkits/ans/native/include/notification_helper.h @@ -215,6 +215,18 @@ public: */ static ErrCode CancelAllNotifications(); + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + static ErrCode CancelAsBundle(int32_t notificationId, const std::string &representativeBundle, int32_t userId); + /** * @brief Obtains the number of active notifications of the current application in the system. * diff --git a/interfaces/innerkits/ans/native/include/notification_request.h b/interfaces/innerkits/ans/native/include/notification_request.h index b6869ac89..ea0e3c723 100644 --- a/interfaces/innerkits/ans/native/include/notification_request.h +++ b/interfaces/innerkits/ans/native/include/notification_request.h @@ -360,6 +360,10 @@ public: */ void SetPermitSystemGeneratedContextualActionButtons(bool permitted); + bool IsAgentNotification() const; + + void SetIsAgentNotification(bool isAgent); + /** * @brief Adds a MessageUser object and associates it with this notification. * @@ -905,14 +909,28 @@ public: * * @param uid Indicates the UID of the notification creator. */ - void SetCreatorUid(pid_t uid); + void SetCreatorUid(uid_t uid); /** * @brief Obtains the UID of the notification creator. * * @return Returns the UID of the notification creator. */ - pid_t GetCreatorUid() const; + uid_t GetCreatorUid() const; + + /** + * @brief Sets the UID of the notification owner. + * + * @param uid the UID of the notification owner. + */ + void SetOwnerUid(uid_t uid); + + /** + * @brief Obtains the UID of the notification owner. + * + * @return the UID of the notification owner. + */ + uid_t GetOwnerUid() const; /** * @brief Sets the label of this notification. @@ -972,6 +990,20 @@ public: */ int32_t GetCreatorUserId() const; + /** + * @brief Sets the UserId of the notification owner. + * + * @param userId the UserId of the notification owner. + */ + void SetOwnerUserId(int32_t userId); + + /** + * @brief Obtains the UserId of the notification owner. + * + * @return the UserId of the notification owner. + */ + int32_t GetOwnerUserId() const; + /** * @brief Returns a string representation of the object. * @@ -1112,8 +1144,10 @@ private: int64_t autoDeletedTime_ {0}; pid_t creatorPid_ {0}; - pid_t creatorUid_ {0}; + uid_t creatorUid_ {0}; + uid_t ownerUid_ {0}; int32_t creatorUserId_ {SUBSCRIBE_USER_INIT}; + int32_t ownerUserId_ {SUBSCRIBE_USER_INIT}; int32_t receiverUserId_ {SUBSCRIBE_USER_INIT}; std::string settingsText_ {}; @@ -1145,6 +1179,7 @@ private: bool floatingIcon_ {false}; bool onlyLocal_ {false}; bool permitted_ {true}; + bool isAgent_ {false}; std::shared_ptr wantAgent_ {}; std::shared_ptr removalWantAgent_ {}; diff --git a/interfaces/kits/napi/ans/include/cancel.h b/interfaces/kits/napi/ans/include/cancel.h index 4cdc87e79..49b306db5 100644 --- a/interfaces/kits/napi/ans/include/cancel.h +++ b/interfaces/kits/napi/ans/include/cancel.h @@ -25,6 +25,7 @@ using namespace OHOS::Notification; napi_value Cancel(napi_env env, napi_callback_info info); napi_value CancelAll(napi_env env, napi_callback_info info); napi_value CancelGroup(napi_env env, napi_callback_info info); +napi_value CancelAsBundle(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/interfaces/kits/napi/ans/include/common.h b/interfaces/kits/napi/ans/include/common.h index c81deced6..b79276d4a 100644 --- a/interfaces/kits/napi/ans/include/common.h +++ b/interfaces/kits/napi/ans/include/common.h @@ -24,14 +24,14 @@ namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; -const std::int32_t STR_MAX_SIZE = 200; -const std::int32_t LONG_STR_MAX_SIZE = 1024; -const int32_t NO_ERROR = 0; -const int32_t ERROR = -1; -const int32_t PARAM0 = 0; -const int32_t PARAM1 = 1; -const int32_t PARAM2 = 2; -const int32_t PARAM3 = 3; +constexpr int32_t STR_MAX_SIZE = 200; +constexpr int32_t LONG_STR_MAX_SIZE = 1024; +constexpr uint8_t NO_ERROR = 0; +constexpr uint8_t ERROR = -1; +constexpr uint8_t PARAM0 = 0; +constexpr uint8_t PARAM1 = 1; +constexpr uint8_t PARAM2 = 2; +constexpr uint8_t PARAM3 = 3; enum class ContentType { NOTIFICATION_CONTENT_BASIC_TEXT, @@ -277,7 +277,7 @@ public: */ static napi_value SetNotificationRequest( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the string obejcts of specified NotificationRequest object * @@ -288,7 +288,7 @@ public: */ static napi_value SetNotificationRequestByString( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the number obejcts of specified NotificationRequest object * @@ -299,7 +299,7 @@ public: */ static napi_value SetNotificationRequestByNumber( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the bool obejcts of specified NotificationRequest object * @@ -310,7 +310,7 @@ public: */ static napi_value SetNotificationRequestByBool( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the WantAgent obejct of specified NotificationRequest object * @@ -321,7 +321,7 @@ public: */ static napi_value SetNotificationRequestByWantAgent( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the PixelMap obejct of specified NotificationRequest object * @@ -332,7 +332,7 @@ public: */ static napi_value SetNotificationRequestByPixelMap( const napi_env &env, const OHOS::Notification::NotificationRequest *request, napi_value &result); - + /** * @brief Sets a js object by the custom obejcts of specified NotificationRequest object * @@ -397,7 +397,7 @@ public: */ static napi_value SetNotificationContent( const napi_env &env, const std::shared_ptr &content, napi_value &result); - + /** * @brief Sets a js object by the object of specified type in specified NotificationContent object * @@ -507,7 +507,7 @@ public: */ static napi_value SetNotificationActionButton( const napi_env &env, const std::shared_ptr &actionButton, napi_value &result); - + /** * @brief Sets a js object by the extra objects of specified NotificationActionButton object * @@ -518,7 +518,7 @@ public: */ static napi_value SetNotificationActionButtonByExtras( const napi_env &env, const std::shared_ptr &actionButton, napi_value &result); - + /** * @brief Sets a js object by specified NotificationUserInput object * @@ -627,7 +627,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetNotificationId(const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the slot type of NotificationRequest object from specified js object * @@ -649,7 +649,7 @@ public: */ static napi_value GetNotificationIsOngoing( const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the isUnremovable flag of NotificationRequest object from specified js object * @@ -715,7 +715,7 @@ public: */ static napi_value GetNotificationRemovalWantAgent( const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the max screen WantAgent object of NotificationRequest object from specified js object * @@ -758,7 +758,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetNotificationColor(const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the colorEnabled flag of NotificationRequest object from specified js object * @@ -813,7 +813,7 @@ public: */ static napi_value GetNotificationStatusBarText( const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the label of NotificationRequest object from specified js object * @@ -823,7 +823,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetNotificationLabel(const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the badge icon style of NotificationRequest object from specified js object * @@ -944,7 +944,7 @@ public: */ static napi_value GetNotificationActionButtonsDetailed( const napi_env &env, const napi_value &actionButton, std::shared_ptr &pActionButton); - + /** * @brief Gets the basic information of NotificationActionButton object from specified js object * @@ -955,7 +955,7 @@ public: */ static napi_value GetNotificationActionButtonsDetailedBasicInfo( const napi_env &env, const napi_value &actionButton, std::shared_ptr &pActionButton); - + /** * @brief Gets the extras of NotificationActionButton object from specified js object * @@ -966,7 +966,7 @@ public: */ static napi_value GetNotificationActionButtonsDetailedByExtras( const napi_env &env, const napi_value &actionButton, std::shared_ptr &pActionButton); - + /** * @brief Gets the user input of NotificationActionButton object from specified js object * @@ -977,7 +977,7 @@ public: */ static napi_value GetNotificationUserInput( const napi_env &env, const napi_value &actionButton, std::shared_ptr &pActionButton); - + /** * @brief Gets the input key of NotificationUserInput object from specified js object * @@ -988,7 +988,7 @@ public: */ static napi_value GetNotificationUserInputByInputKey( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the tag of NotificationUserInput object from specified js object * @@ -999,7 +999,7 @@ public: */ static napi_value GetNotificationUserInputByTag( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the options of NotificationUserInput object from specified js object * @@ -1010,7 +1010,7 @@ public: */ static napi_value GetNotificationUserInputByOptions( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the permit mime types of NotificationUserInput object from specified js object * @@ -1021,7 +1021,7 @@ public: */ static napi_value GetNotificationUserInputByPermitMimeTypes( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the permit free from input of NotificationUserInput object from specified js object * @@ -1032,7 +1032,7 @@ public: */ static napi_value GetNotificationUserInputByPermitFreeFormInput( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the edit type of NotificationUserInput object from specified js object * @@ -1043,7 +1043,7 @@ public: */ static napi_value GetNotificationUserInputByEditType( const napi_env &env, const napi_value &userInputResult, std::shared_ptr &userInput); - + /** * @brief Gets the additional data of NotificationUserInput object from specified js object * @@ -1065,7 +1065,7 @@ public: */ static napi_value GetNotificationSmallIcon( const napi_env &env, const napi_value &value, NotificationRequest &request); - + /** * @brief Gets the large icon of NotificationRequest object from specified js object * @@ -1130,7 +1130,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetNotificationContentType(const napi_env &env, const napi_value &result, int32_t &type); - + /** * @brief Gets a basic content of NotificationRequest object from specified js object * @@ -1232,7 +1232,7 @@ public: static napi_value GetNotificationConversationalContentTitle( const napi_env &env, const napi_value &contentResult, std::shared_ptr &conversationalContent); - + /** * @brief Gets the group of NotificationConversationalContent object from specified js object * @@ -1244,7 +1244,7 @@ public: static napi_value GetNotificationConversationalContentGroup( const napi_env &env, const napi_value &contentResult, std::shared_ptr &conversationalContent); - + /** * @brief Gets the messages of NotificationConversationalContent object from specified js object * @@ -1256,7 +1256,7 @@ public: static napi_value GetNotificationConversationalContentMessages( const napi_env &env, const napi_value &contentResult, std::shared_ptr &conversationalContent); - + /** * @brief Gets a NotificationConversationalMessage object from specified js object * @@ -1268,7 +1268,7 @@ public: static napi_value GetConversationalMessage( const napi_env &env, const napi_value &conversationalMessage, std::shared_ptr &message); - + /** * @brief Gets the basic information of NotificationConversationalMessage object from specified js object * @@ -1302,7 +1302,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetMessageUser(const napi_env &env, const napi_value &result, MessageUser &messageUser); - + /** * @brief Gets a MessageUser object from specified js object * @@ -1312,7 +1312,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetMessageUserByString(const napi_env &env, const napi_value &result, MessageUser &messageUser); - + /** * @brief Gets the bool objects of MessageUser object from specified js object * @@ -1322,7 +1322,7 @@ public: * @return Returns the null object if success, returns the null value otherwise */ static napi_value GetMessageUserByBool(const napi_env &env, const napi_value &result, MessageUser &messageUser); - + /** * @brief Gets the custom objects of MessageUser object from specified js object * diff --git a/interfaces/kits/napi/ans/include/publish.h b/interfaces/kits/napi/ans/include/publish.h index 684921abd..40db6ba7f 100644 --- a/interfaces/kits/napi/ans/include/publish.h +++ b/interfaces/kits/napi/ans/include/publish.h @@ -22,6 +22,7 @@ namespace NotificationNapi { using namespace OHOS::Notification; napi_value Publish(napi_env env, napi_callback_info info); napi_value ShowNotification(napi_env env, napi_callback_info info); +napi_value PublishAsBundle(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_ANS_STANDARD_KITS_NAPI_INCLUDE_PUBLISH_H \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/cancel.cpp b/interfaces/kits/napi/ans/src/cancel.cpp index 7524026d7..becba5129 100644 --- a/interfaces/kits/napi/ans/src/cancel.cpp +++ b/interfaces/kits/napi/ans/src/cancel.cpp @@ -17,12 +17,13 @@ namespace OHOS { namespace NotificationNapi { -const int CANCEL_MAX_PARA = 3; -const int CANCEL_GROUP_MAX_PARA = 2; -const int CANCEL_GROUP_MIN_PARA = 1; +constexpr int8_t CANCEL_MAX_PARA = 3; +constexpr int8_t CANCEL_GROUP_MAX_PARA = 2; +constexpr int8_t CANCEL_GROUP_MIN_PARA = 1; +constexpr int8_t CANCEL_AS_BUNDLE_MAX_PARA = 4; struct ParametersInfoCancel { - int id = 0; + int32_t id = 0; std::string label = ""; napi_ref callback = nullptr; }; @@ -30,7 +31,7 @@ struct ParametersInfoCancel { struct AsyncCallbackInfoCancel { napi_env env = nullptr; napi_async_work asyncWork = nullptr; - int id = 0; + int32_t id = 0; std::string label; CallbackPromiseInfo info; }; @@ -47,6 +48,22 @@ struct AsyncCallbackInfoCancelGroup { ParametersInfoCancelGroup params {}; }; +struct ParametersInfoCancelAsBundle { + int32_t id = 0; + std::string representativeBundle = ""; + int32_t userId = 0; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoCancelAsBundle { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + int32_t id = 0; + std::string representativeBundle = ""; + int32_t userId = 0; + CallbackPromiseInfo info; +}; + napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancel ¶s) { ANS_LOGI("enter"); @@ -55,20 +72,27 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[CANCEL_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= 1, "Wrong number of arguments"); + if (argc < 1) { + ANS_LOGW("Wrong number of arguments"); + return nullptr; + } napi_valuetype valuetype = napi_undefined; // argv[0]: id: number NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type. Number expected."); + if (valuetype != napi_number) { + ANS_LOGW("Wrong argument type. Number expected."); + return nullptr; + } NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶s.id)); // argv[1]: label: string / callback if (argc >= CANCEL_MAX_PARA - 1) { NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, - (valuetype == napi_string || valuetype == napi_function), - "Wrong argument type. String or function expected."); + if (valuetype != napi_string && valuetype != napi_function) { + ANS_LOGW("Wrong argument type. String or function expected."); + return nullptr; + } if (valuetype == napi_string) { char str[STR_MAX_SIZE] = {0}; size_t strLen = 0; @@ -82,7 +106,10 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, // argv[2]: callback if (argc >= CANCEL_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM2], 1, ¶s.callback); } @@ -97,12 +124,18 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[CANCEL_GROUP_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= CANCEL_GROUP_MIN_PARA, "Wrong number of arguments"); + if (argc < CANCEL_GROUP_MIN_PARA) { + ANS_LOGW("Wrong number of arguments"); + return nullptr; + } napi_valuetype valuetype = napi_undefined; // argv[0]: groupName: string NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String expected."); + if (valuetype != napi_string) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } char str[STR_MAX_SIZE] = {0}; size_t strLen = 0; NAPI_CALL(env, napi_get_value_string_utf8(env, argv[PARAM0], str, STR_MAX_SIZE - 1, &strLen)); @@ -111,7 +144,10 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, // argv[1]: callback if (argc >= CANCEL_GROUP_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM1], 1, ¶s.callback); } @@ -287,5 +323,121 @@ napi_value CancelGroup(napi_env env, napi_callback_info info) return promise; } } + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoCancelAsBundle ¶s) +{ + ANS_LOGI("enter"); + + size_t argc = CANCEL_AS_BUNDLE_MAX_PARA; + napi_value argv[CANCEL_AS_BUNDLE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < 1) { + ANS_LOGW("Wrong number of arguments"); + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + // argv[0]: id: number + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_number) { + ANS_LOGW("Wrong argument type. Number expected."); + return nullptr; + } + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶s.id)); + + // argv[1]: representativeBundle: string + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } + + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen); + paras.representativeBundle = str; + + // argv[2] : userId + NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); + if (valuetype != napi_number) { + ANS_LOGW("Wrong argument type. Number expected."); + return nullptr; + } + napi_get_value_int32(env, argv[PARAM2], ¶s.userId); + + // argv[3]: callback + if (argc >= CANCEL_AS_BUNDLE_MAX_PARA) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM3], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, argv[PARAM2], 1, ¶s.callback); + } + + return Common::NapiGetNull(env); +} + +napi_value CancelAsBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoCancelAsBundle paras; + if (ParseParameters(env, info, paras) == nullptr) { + return Common::NapiGetUndefined(env); + } + + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = new (std::nothrow) AsyncCallbackInfoCancelAsBundle { + .env = env, .asyncWork = nullptr, + .id = paras.id, + .representativeBundle = paras.representativeBundle, + .userId = paras.userId + }; + if (!asynccallbackinfo) { + return Common::JSParaError(env, paras.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "cancelasbundle", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("Cancel napi_create_async_work start"); + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = static_cast(data); + + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::CancelAsBundle( + asynccallbackinfo->id, asynccallbackinfo->representativeBundle, asynccallbackinfo->userId); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("Cancel napi_create_async_work end"); + AsyncCallbackInfoCancelAsBundle *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/napi/ans/src/common.cpp b/interfaces/kits/napi/ans/src/common.cpp index b484cd0b9..e03ebc096 100644 --- a/interfaces/kits/napi/ans/src/common.cpp +++ b/interfaces/kits/napi/ans/src/common.cpp @@ -242,22 +242,38 @@ napi_value Common::SetNotification( napi_get_boolean(env, notification->IsFloatingIcon(), &value); napi_set_named_property(env, result, "isFloatingIcon", value); - // readonly creatorBundleName?: string - napi_create_string_utf8(env, notification->GetCreateBundle().c_str(), NAPI_AUTO_LENGTH, &value); - napi_set_named_property(env, result, "creatorBundleName", value); + if (notification->GetNotificationRequest().IsAgentNotification()) { + // Agent notification, replace creator with owner + // readonly creatorBundleName?: string + napi_create_string_utf8( + env, notification->GetNotificationRequest().GetOwnerBundleName().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "creatorBundleName", value); + + // readonly creatorUid?: number + napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUid(), &value); + napi_set_named_property(env, result, "creatorUid", value); + + // readonly creatorUserId?: number + napi_create_int32(env, notification->GetNotificationRequest().GetOwnerUserId(), &value); + napi_set_named_property(env, result, "creatorUserId", value); + } else { + // readonly creatorBundleName?: string + napi_create_string_utf8(env, notification->GetCreateBundle().c_str(), NAPI_AUTO_LENGTH, &value); + napi_set_named_property(env, result, "creatorBundleName", value); - // readonly creatorUid?: number - napi_create_int32(env, notification->GetUid(), &value); - napi_set_named_property(env, result, "creatorUid", value); + // readonly creatorUid?: number + napi_create_int32(env, notification->GetUid(), &value); + napi_set_named_property(env, result, "creatorUid", value); + + // readonly creatorUserId?: number + napi_create_int32(env, notification->GetUserId(), &value); + napi_set_named_property(env, result, "creatorUserId", value); + } // readonly creatorPid?: number napi_create_int32(env, notification->GetPid(), &value); napi_set_named_property(env, result, "creatorPid", value); - // readonly creatorUserId?: number - napi_create_int32(env, notification->GetUserId(), &value); - napi_set_named_property(env, result, "creatorUserId", value); - // distributedOption?:DistributedOptions napi_value distributedResult = nullptr; napi_create_object(env, &distributedResult); diff --git a/interfaces/kits/napi/ans/src/init.cpp b/interfaces/kits/napi/ans/src/init.cpp index 9889ebc6e..e1136712b 100644 --- a/interfaces/kits/napi/ans/src/init.cpp +++ b/interfaces/kits/napi/ans/src/init.cpp @@ -43,6 +43,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("subscribe", Subscribe), DECLARE_NAPI_FUNCTION("unsubscribe", Unsubscribe), DECLARE_NAPI_FUNCTION("publish", Publish), + DECLARE_NAPI_FUNCTION("publishAsBundle", PublishAsBundle), DECLARE_NAPI_FUNCTION("remove", Remove), DECLARE_NAPI_FUNCTION("removeAll", RemoveAll), DECLARE_NAPI_FUNCTION("getAllActiveNotifications", GetAllActiveNotifications), @@ -51,6 +52,7 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("cancel", Cancel), DECLARE_NAPI_FUNCTION("cancelAll", CancelAll), DECLARE_NAPI_FUNCTION("cancelGroup", CancelGroup), + DECLARE_NAPI_FUNCTION("cancelAsBundle", CancelAsBundle), DECLARE_NAPI_FUNCTION("addSlot", AddSlot), DECLARE_NAPI_FUNCTION("addSlots", AddSlots), DECLARE_NAPI_FUNCTION("setSlotByBundle", SetSlotByBundle), diff --git a/interfaces/kits/napi/ans/src/publish.cpp b/interfaces/kits/napi/ans/src/publish.cpp index ad6a9b101..05fefbeb5 100644 --- a/interfaces/kits/napi/ans/src/publish.cpp +++ b/interfaces/kits/napi/ans/src/publish.cpp @@ -22,8 +22,9 @@ namespace NotificationNapi { using namespace AbilityRuntime::WantAgent; namespace { -static const int8_t PUBLISH_NOTIFICATION_MAX = 3; -static const int8_t SHOW_NOTIFICATION_MAX = 1; +constexpr int8_t PUBLISH_NOTIFICATION_MAX = 3; +constexpr int8_t SHOW_NOTIFICATION_MAX = 1; +constexpr int8_t PUBLISH_AS_BUNDLE_MAX = 4; } struct AsyncCallbackInfoPublish { @@ -61,7 +62,7 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[PUBLISH_NOTIFICATION_MAX] = {nullptr}; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); if (argc < 1) { - ANS_LOGW("Wrong argument type. Function expected."); + ANS_LOGW("Wrong number of arguments."); return nullptr; } @@ -357,5 +358,126 @@ napi_value ShowNotification(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); return nullptr; } + +napi_value ParsePublishAsBundleParameters( + const napi_env &env, const napi_callback_info &info, ParametersInfoPublish ¶ms) +{ + ANS_LOGI("enter"); + + size_t argc = PUBLISH_AS_BUNDLE_MAX; + napi_value argv[PUBLISH_AS_BUNDLE_MAX] = {nullptr}; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); + if (argc < 1) { + ANS_LOGW("Wrong number of arguments"); + return nullptr; + } + + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_object) { + ANS_LOGW("Wrong argument type. Object expected."); + return nullptr; + } + + // argv[0] : NotificationRequest + if (Common::GetNotificationRequest(env, argv[PARAM0], params.request) == nullptr) { + return nullptr; + } + + // argv[1] : bundleName + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_string) { + ANS_LOGW("Wrong argument type. String expected."); + return nullptr; + } + + char str[STR_MAX_SIZE] = {0}; + size_t strLen = 0; + napi_get_value_string_utf8(env, argv[PARAM1], str, STR_MAX_SIZE - 1, &strLen); + params.request.SetOwnerBundleName(str); + + // argv[2] : userId + NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); + if (valuetype != napi_number) { + ANS_LOGW("Wrong argument type. Number expected."); + return nullptr; + } + int32_t userId = 0; + napi_get_value_int32(env, argv[PARAM2], &userId); + params.request.SetOwnerUserId(userId); + params.request.SetIsAgentNotification(true); + + // argv[3] : callback + if (argc >= PUBLISH_AS_BUNDLE_MAX) { + if (GetCallback(env, argv[PARAM3], params) == nullptr) { + return nullptr; + } + } + + ANS_LOGI("end"); + return Common::NapiGetNull(env); +} + +napi_value PublishAsBundle(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + ParametersInfoPublish params; + if (ParsePublishAsBundleParameters(env, info, params) == nullptr) { + return Common::NapiGetUndefined(env); + } + + napi_value promise = nullptr; + AsyncCallbackInfoPublish *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoPublish {.env = env, .asyncWork = nullptr}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + + asynccallbackinfo->request = params.request; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "publishasbundle", NAPI_AUTO_LENGTH, &resourceName); + + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("PublishAsBundle napi_create_async_work start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + ANS_LOGI("PublishAsBundle napi_create_async_work start notificationId = %{public}d, contentType = " + "%{public}d", + asynccallbackinfo->request.GetNotificationId(), + asynccallbackinfo->request.GetContent()->GetContentType()); + + asynccallbackinfo->info.errorCode = + NotificationHelper::PublishNotification(asynccallbackinfo->request); + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("PublishAsBundle napi_create_async_work complete start"); + AsyncCallbackInfoPublish *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + ANS_LOGI("PublishAsBundle napi_create_async_work complete end"); + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + NAPI_CALL(env, napi_queue_async_work(env, asynccallbackinfo->asyncWork)); + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } else { + return promise; + } +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/sa_profile/3203.xml b/sa_profile/3203.xml old mode 100644 new mode 100755 diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 57b2084cc..bf6864d6f 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -80,6 +80,19 @@ public: */ ErrCode CancelAll() override; + /** + * @brief Cancels a published agent notification. + * + * @param notificationId Indicates the unique notification ID in the application. + * The value must be the ID of a published notification. + * Otherwise, this method does not take effect. + * @param representativeBundle Indicates the name of application bundle your application is representing. + * @param userId Indicates the specific user. + * @return Returns cancel notification result. + */ + ErrCode CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) override; + /** * @brief Adds a notification slot by type. * @@ -760,8 +773,9 @@ private: void UpdateRecentNotification(sptr ¬ification, bool isDelete, int32_t reason); void AdjustDateForDndTypeOnce(int64_t &beginDate, int64_t &endDate); - bool CheckPermission(); - ErrCode PrepereContinuousTaskNotificationRequest(const sptr &request, const int32_t &uid); + bool CheckPermission(const std::string &permission); + ErrCode PrepareNotificationRequest(const sptr &request); + ErrCode PrepareContinuousTaskNotificationRequest(const sptr &request, const int32_t &uid); bool GetActiveUserId(int& userId); void TriggerRemoveWantAgent(const sptr &request); bool CheckApiCompatibility(const sptr &bundleOption); diff --git a/services/ans/resource/external.json b/services/ans/resource/external.json old mode 100644 new mode 100755 diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index cedff58c5..662b448f5 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -200,14 +200,36 @@ inline ErrCode CheckPictureSize(const sptr &request) return result; } -ErrCode PrepereNotificationRequest(const sptr &request) +ErrCode AdvancedNotificationService::PrepareNotificationRequest(const sptr &request) { + ANS_LOGD("%{public}s", __FUNCTION__); + std::string bundle = GetClientBundleName(); if (bundle.empty()) { return ERR_ANS_INVALID_BUNDLE; } - request->SetOwnerBundleName(bundle); + if (request->IsAgentNotification()) { + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + uid_t uid = 0; + if (bundleManager != nullptr) { + uid = bundleManager->GetDefaultUidByBundleName(request->GetOwnerBundleName(), request->GetOwnerUserId()); + } + if (uid < 0) { + return ERR_ANS_INVALID_UID; + } + request->SetOwnerUid(uid); + } else { + request->SetOwnerBundleName(bundle); + } request->SetCreatorBundleName(bundle); int32_t uid = IPCSkeleton::GetCallingUid(); @@ -378,11 +400,17 @@ ErrCode AdvancedNotificationService::PrepareNotificationInfo( if ((request->GetSlotType() == NotificationConstant::SlotType::CUSTOM) && !IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; } - ErrCode result = PrepereNotificationRequest(request); + ErrCode result = PrepareNotificationRequest(request); if (result != ERR_OK) { return result; } - bundleOption = GenerateBundleOption(); + + if (request->IsAgentNotification()) { + bundleOption = new NotificationBundleOption(request->GetOwnerBundleName(), request->GetOwnerUid()); + } else { + bundleOption = GenerateBundleOption(); + } + if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } @@ -542,6 +570,7 @@ void AdvancedNotificationService::StopFilters() ErrCode AdvancedNotificationService::Cancel(int32_t notificationId, const std::string &label) { ANS_LOGD("%{public}s", __FUNCTION__); + sptr bundleOption = GenerateBundleOption(); return CancelPreparedNotification(notificationId, label, bundleOption); } @@ -586,6 +615,31 @@ ErrCode AdvancedNotificationService::CancelAll() return result; } +ErrCode AdvancedNotificationService::CancelAsBundle( + int32_t notificationId, const std::string &representativeBundle, int32_t userId) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + uid_t uid = -1; + std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); + if (bundleManager != nullptr) { + uid = BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(representativeBundle, userId); + } + if (uid < 0) { + return ERR_ANS_INVALID_UID; + } + sptr bundleOption = new NotificationBundleOption(representativeBundle, uid); + return CancelPreparedNotification(notificationId, "", bundleOption); +} + ErrCode AdvancedNotificationService::AddSlots(const std::vector> &slots) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -594,7 +648,7 @@ ErrCode AdvancedNotificationService::AddSlots(const std::vector &request, const int32_t &uid) { int32_t pid = IPCSkeleton::GetCallingPid(); @@ -3067,7 +3121,7 @@ ErrCode AdvancedNotificationService::IsSpecialUserAllowedNotify(const int32_t &u return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } @@ -3087,7 +3141,7 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledByUser(const int32_t return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } @@ -3156,7 +3210,7 @@ ErrCode AdvancedNotificationService::SetDoNotDisturbDate(const int32_t &userId, return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } @@ -3177,7 +3231,7 @@ ErrCode AdvancedNotificationService::GetDoNotDisturbDate(const int32_t &userId, return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } @@ -3383,7 +3437,7 @@ ErrCode AdvancedNotificationService::SetEnabledForBundleSlot( return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } @@ -3437,7 +3491,7 @@ ErrCode AdvancedNotificationService::GetEnabledForBundleSlot( return ERR_ANS_NON_SYSTEM_APP; } - if (!CheckPermission()) { + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { return ERR_ANS_PERMISSION_DENIED; } diff --git a/services/ans/src/bundle_manager_helper.cpp b/services/ans/src/bundle_manager_helper.cpp index c344003af..f5083925f 100644 --- a/services/ans/src/bundle_manager_helper.cpp +++ b/services/ans/src/bundle_manager_helper.cpp @@ -140,9 +140,9 @@ int32_t BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle Connect(); if (bundleMgr_ != nullptr) { - AppExecFwk::BundleInfo bundleInfo; - if (bundleMgr_->GetBundleInfo(bundle, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, userId)) { - uid = bundleInfo.uid; + uid = bundleMgr_->GetUidByBundleName(bundle, userId); + if (uid < 0) { + ANS_LOGW("get invalid uid of bundle %{public}s in userId %{public}d", bundle.c_str(), userId); } } -- Gitee