From 100d978ecfca780b7dff9ee1cc346e3ade851638 Mon Sep 17 00:00:00 2001 From: XKK Date: Fri, 14 Jul 2023 14:46:51 +0800 Subject: [PATCH] =?UTF-8?q?fixed=207d7c2f3=20from=20https://gitee.com/hu-y?= =?UTF-8?q?ingsong/notification=5Fdistributed=5Fnotification=5Fservice/pul?= =?UTF-8?q?ls/1114=20=E6=A8=A1=E6=80=81=E5=BC=B9=E7=AA=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: XKK --- frameworks/ans/src/notification_helper.cpp | 4 +- .../unittest/notification_helper_test.cpp | 3 +- .../core/include/ans_manager_interface.h | 2 +- frameworks/core/include/ans_manager_proxy.h | 2 +- frameworks/core/include/ans_manager_stub.h | 2 +- frameworks/core/include/ans_notification.h | 2 +- frameworks/core/src/ans_manager_proxy.cpp | 13 ++++- frameworks/core/src/ans_manager_stub.cpp | 16 +++++- frameworks/core/src/ans_notification.cpp | 4 +- .../ans_manager_proxy_unit_test.cpp | 12 +++-- .../ans_manager_stub_test.cpp | 4 +- .../ans_notification_unit_test.cpp | 3 +- .../js/napi/include/enable_notification.h | 2 + .../manager/napi_enable_notification.h | 2 + .../js/napi/src/enable_notification.cpp | 2 +- frameworks/js/napi/src/manager/BUILD.gn | 1 + .../src/manager/napi_enable_notification.cpp | 54 ++++++++++++++++++- interfaces/inner_api/notification_helper.h | 2 +- .../include/advanced_notification_service.h | 2 +- services/ans/include/notification_dialog.h | 2 +- .../ans/src/advanced_notification_service.cpp | 4 +- services/ans/src/notification_dialog.cpp | 5 +- .../advanced_notification_service_test.cpp | 6 ++- .../notification_dialog_test.cpp | 6 ++- .../NotificationServiceExtAbility.ts | 45 ++++++++++++---- .../advancednotificationservice_fuzzer.cpp | 3 +- .../ansmanagerstub_fuzzer.cpp | 3 +- .../setnotificationbadgenum_fuzzer.cpp | 3 +- 28 files changed, 166 insertions(+), 43 deletions(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 0fc8750b9..2eac92284 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -144,9 +144,9 @@ ErrCode NotificationHelper::IsAllowedNotifySelf(bool &allowed) return DelayedSingleton::GetInstance()->IsAllowedNotifySelf(allowed); } -ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId) +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, sptr &callerToken) { - return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId); + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, callerToken); } ErrCode NotificationHelper::HasNotificationPolicyAccessPermission(bool &hasPermission) diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index 8dcaa32ee..af385f789 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -409,7 +409,8 @@ HWTEST_F(NotificationHelperTest, RequestEnableNotification_00001, Function | Sma { std::string deviceId = "DeviceId"; NotificationHelper notificationHelper; - ErrCode ret = notificationHelper.RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + ErrCode ret = notificationHelper.RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE); } diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index ccccdcd2f..6159fd145 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -318,7 +318,7 @@ public: * @param deviceId Indicates the device Id. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RequestEnableNotification(const std::string &deviceId) = 0; + virtual ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callerToken) = 0; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index a73751a9e..c31fc4330 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -307,7 +307,7 @@ public: * @param deviceId Indicates the device Id. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId) override; + ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callerToken) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index b91aeef02..78fcb7305 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -323,7 +323,7 @@ public: * @return Returns ERR_OK on success, others on failure. */ ErrCode RequestEnableNotification( - const std::string &deviceId) override; + const std::string &deviceId, const sptr &callerToken) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 0794a5c4c..b91ea0dec 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -262,7 +262,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - ErrCode RequestEnableNotification(std::string &deviceId); + ErrCode RequestEnableNotification(std::string &deviceId, sptr &callerToken); /** * @brief Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy. diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 7afd2eb3d..494504b3f 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1050,7 +1050,7 @@ ErrCode AnsManagerProxy::UpdateSlots( return result; } -ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) +ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, const sptr &callerToken) { ANS_LOGI("enter"); MessageParcel data; @@ -1064,6 +1064,17 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) return ERR_ANS_PARCELABLE_FAILED; } + if (!data.WriteBool(callerToken != nullptr)) { + ANS_LOGE("fail: write callerToken failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + if (callerToken != nullptr) { + if (!data.WriteRemoteObject(callerToken)) { + ANS_LOGE("fail: write callerToken failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + } + MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(NotificationInterfaceCode::REQUEST_ENABLE_NOTIFICATION, option, data, reply); diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 7fd70d812..f2f435117 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -919,7 +919,19 @@ ErrCode AnsManagerStub::HandleRequestEnableNotification(MessageParcel &data, Mes ANS_LOGE("[HandleRequestEnableNotification] fail: read deviceId failed."); return ERR_ANS_PARCELABLE_FAILED; } - ErrCode result = RequestEnableNotification(deviceId); + + bool hasCallerToken = false; + if (!data.ReadBool(hasCallerToken)) { + ANS_LOGE("fail: read hasCallerToken failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + sptr callerToken = nullptr; + if (hasCallerToken) { + callerToken = data.ReadRemoteObject(); + } + + ErrCode result = RequestEnableNotification(deviceId, callerToken); if (!reply.WriteInt32(result)) { ANS_LOGE("[HandleRequestEnableNotification] fail: write result failed, ErrCode=%{public}d", result); return ERR_ANS_PARCELABLE_FAILED; @@ -1969,7 +1981,7 @@ ErrCode AnsManagerStub::UpdateSlots( return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId) +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, const sptr &callerToken) { ANS_LOGE("AnsManagerStub::RequestEnableNotification called!"); return ERR_INVALID_OPERATION; diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index feea0bbbc..9bcbc1b6b 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -326,14 +326,14 @@ ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed) return ansManagerProxy_->IsAllowedNotifySelf(allowed); } -ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId) +ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, sptr &callerToken) { ANS_LOGD("enter"); if (!GetAnsManagerProxy()) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - return ansManagerProxy_->RequestEnableNotification(deviceId); + return ansManagerProxy_->RequestEnableNotification(deviceId, callerToken); } ErrCode AnsNotification::HasNotificationPolicyAccessPermission(bool &hasPermission) diff --git a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp index 193e37a6a..07d9cbe19 100644 --- a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp @@ -893,7 +893,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0100, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - int32_t result = proxy->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + int32_t result = proxy->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); } @@ -913,7 +914,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0200, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = ""; - int32_t result = proxy->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + int32_t result = proxy->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); } @@ -935,7 +937,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0300, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - int32_t result = proxy->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + int32_t result = proxy->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ERR_OK, result); } @@ -956,7 +959,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0400, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - int32_t result = proxy->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + int32_t result = proxy->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ERR_ANS_TRANSACT_FAILED, result); } diff --git a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp index 710b7f0f0..154e10543 100644 --- a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp @@ -1368,6 +1368,7 @@ HWTEST_F(AnsManagerStubTest, HandleRequestEnableNotification01, Function | Small data.WriteInterfaceToken(AnsManagerStub::GetDescriptor()); data.WriteString(deviceId); + data.WriteBool(false); ErrCode ret = ansManagerStub_->OnRemoteRequest(code, data, reply, option); EXPECT_EQ(ret, (int)ERR_OK); @@ -3616,7 +3617,8 @@ HWTEST_F(AnsManagerStubTest, UpdateSlots01, Function | SmallTest | Level1) HWTEST_F(AnsManagerStubTest, RequestEnableNotification01, Function | SmallTest | Level1) { std::string deviceId = "this is deviceId"; - ErrCode result = ansManagerStub_->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + ErrCode result = ansManagerStub_->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(result, (int)ERR_INVALID_OPERATION); } diff --git a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp index ed2b11678..4723a0cfd 100644 --- a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp @@ -355,7 +355,8 @@ HWTEST_F(AnsNotificationUnitTest, RequestEnableNotification_0100, Function | Med ASSERT_NE(nullptr, proxy); ans_->GetAnsManagerProxy(); std::string deviceId = "this is deviceId"; - ErrCode ret1 = ans_->RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + ErrCode ret1 = ans_->RequestEnableNotification(deviceId, callerToken); EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); bool hasPermission = true; ErrCode ret3 = ans_->HasNotificationPolicyAccessPermission(hasPermission); diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index 9c76362c5..182f495e0 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -31,6 +31,8 @@ struct IsEnableParams { int32_t userId = SUBSCRIBE_USER_INIT; bool hasUserId = false; bool allowToPop = false; + sptr callerToken = nullptr; + bool hasCallerToken = false; }; struct AsyncCallbackInfoIsEnable { diff --git a/frameworks/js/napi/include/manager/napi_enable_notification.h b/frameworks/js/napi/include/manager/napi_enable_notification.h index a75cce5d5..8430c7e14 100644 --- a/frameworks/js/napi/include/manager/napi_enable_notification.h +++ b/frameworks/js/napi/include/manager/napi_enable_notification.h @@ -16,6 +16,7 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H #include "common.h" +#include "enable_notification.h" namespace OHOS { namespace NotificationNapi { @@ -25,6 +26,7 @@ napi_value NapiEnableNotification(napi_env env, napi_callback_info info); napi_value NapiIsNotificationEnabled(napi_env env, napi_callback_info info); napi_value NapiIsNotificationEnabledSelf(napi_env env, napi_callback_info info); napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info); +napi_value ParseRequestEnableParameters(const napi_env &env, const napi_callback_info &info, IsEnableParams ¶ms); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/src/enable_notification.cpp b/frameworks/js/napi/src/enable_notification.cpp index a7a7e7e46..9117d5105 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -374,7 +374,7 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) if (asynccallbackinfo) { std::string deviceId {""}; asynccallbackinfo->info.errorCode = - NotificationHelper::RequestEnableNotification(deviceId); + NotificationHelper::RequestEnableNotification(deviceId, asynccallbackinfo->params.callerToken); } }, [](napi_env env, napi_status status, void *data) { diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index cf5a0f870..9f19a8385 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("notificationmanager") { "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", "ability_runtime:app_context", + "ability_runtime:napi_base_context", "ability_runtime:napi_common", "ability_runtime:runtime", "ability_runtime:wantagent_innerkits", diff --git a/frameworks/js/napi/src/manager/napi_enable_notification.cpp b/frameworks/js/napi/src/manager/napi_enable_notification.cpp index d5358fd0d..31dd4d253 100644 --- a/frameworks/js/napi/src/manager/napi_enable_notification.cpp +++ b/frameworks/js/napi/src/manager/napi_enable_notification.cpp @@ -17,9 +17,11 @@ #include "ans_inner_errors.h" #include "enable_notification.h" +#include "napi_base_context.h" namespace OHOS { namespace NotificationNapi { +const int IS_NOTIFICATION_ENABLE_MAX_PARA = 2; void AsyncCompleteCallbackNapiEnableNotification(napi_env env, napi_status status, void *data) { ANS_LOGI("enter"); @@ -254,7 +256,7 @@ napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info) { ANS_LOGI("enter"); IsEnableParams params {}; - if (ParseParameters(env, info, params) == nullptr) { + if (ParseRequestEnableParameters(env, info, params) == nullptr) { Common::NapiThrow(env, ERROR_PARAM_INVALID); return Common::NapiGetUndefined(env); } @@ -277,7 +279,7 @@ napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info) if (asynccallbackinfo) { std::string deviceId {""}; asynccallbackinfo->info.errorCode = - NotificationHelper::RequestEnableNotification(deviceId); + NotificationHelper::RequestEnableNotification(deviceId, asynccallbackinfo->params.callerToken); } }, [](napi_env env, napi_status status, void *data) { @@ -309,5 +311,53 @@ napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info) return promise; } } + +napi_value ParseRequestEnableParameters(const napi_env &env, const napi_callback_info &info, IsEnableParams ¶ms) +{ + ANS_LOGD("enter"); + + size_t argc = IS_NOTIFICATION_ENABLE_MAX_PARA; + napi_value argv[IS_NOTIFICATION_ENABLE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + + if (argc == 0) { + return Common::NapiGetNull(env); + } + + // argv[0]: context / callback + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if ((valuetype != napi_object) && (valuetype != napi_function)) { + ANS_LOGW("Wrong argument type. Function or object expected. Excute promise."); + return Common::NapiGetNull(env); + } + if (valuetype == napi_object) { + bool stageMode = false; + napi_status status = OHOS::AbilityRuntime::IsStageContext(env, argv[PARAM0], stageMode); + if (status == napi_ok && stageMode) { + auto context = OHOS::AbilityRuntime::GetStageModeContext(env, argv[PARAM0]); + sptr callerToken = context->GetToken(); + params.callerToken = callerToken; + params.hasCallerToken = true; + } else { + ANS_LOGE("Only support stage mode"); + return nullptr; + } + } else { + napi_create_reference(env, argv[PARAM0], 1, ¶ms.callback); + } + // argv[1]:context + if (argc >= IS_NOTIFICATION_ENABLE_MAX_PARA && valuetype == napi_object) { + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGW("Callback is not function excute promise."); + return Common::NapiGetNull(env); + } + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 6956d1b8d..fdff33476 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -264,7 +264,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - static ErrCode RequestEnableNotification(std::string &deviceId); + static ErrCode RequestEnableNotification(std::string &deviceId, sptr &callerToken); /** * @brief Checks whether this application has permission to modify the Do Not Disturb (DND) notification policy. diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index c5e3f0aaf..f0477e1a0 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -333,7 +333,7 @@ public: * @param deviceId Indicates the device Id. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId) override; + ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callerToken) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/services/ans/include/notification_dialog.h b/services/ans/include/notification_dialog.h index a47a54b8b..5f1725735 100644 --- a/services/ans/include/notification_dialog.h +++ b/services/ans/include/notification_dialog.h @@ -30,7 +30,7 @@ public: * @param uid The uid of application that want launch notification dialog. * @return ERR_OK if success, else not. */ - ErrCode StartEnableNotificationDialogAbility(int32_t uid); + ErrCode StartEnableNotificationDialogAbility(int32_t uid, const sptr &callerToken); private: int32_t GetActiveUserId(); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 6640c7cf0..8bb739115 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1487,7 +1487,7 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( } ErrCode AdvancedNotificationService::RequestEnableNotification( - const std::string &deviceId) + const std::string &deviceId, const sptr &callerToken) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1518,7 +1518,7 @@ ErrCode AdvancedNotificationService::RequestEnableNotification( ANS_LOGI("hasPopped = %{public}d, allowedNotify = %{public}d", hasPopped, allowedNotify); if (!hasPopped && !allowedNotify) { auto notificationDialog = std::make_shared(); - result = notificationDialog->StartEnableNotificationDialogAbility(bundleOption->GetUid()); + result = notificationDialog->StartEnableNotificationDialogAbility(bundleOption->GetUid(), callerToken); if (result != ERR_OK) { ANS_LOGD("StartEnableNotificationDialogAbility failed, result = %{public}d", result); return result; diff --git a/services/ans/src/notification_dialog.cpp b/services/ans/src/notification_dialog.cpp index 1fb3b2884..546f58b83 100644 --- a/services/ans/src/notification_dialog.cpp +++ b/services/ans/src/notification_dialog.cpp @@ -47,7 +47,7 @@ int32_t NotificationDialog::GetUidByBundleName(const std::string &bundleName) return IN_PROCESS_CALL(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, userId)); } -ErrCode NotificationDialog::StartEnableNotificationDialogAbility(int32_t uid) +ErrCode NotificationDialog::StartEnableNotificationDialogAbility(int32_t uid, const sptr &callerToken) { ANS_LOGD("%{public}s, Enter.", __func__); auto bundleName = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName()); @@ -60,6 +60,9 @@ ErrCode NotificationDialog::StartEnableNotificationDialogAbility(int32_t uid) AAFwk::Want want; want.SetElementName("com.ohos.notificationdialog", "EnableNotificationDialog"); want.SetParam("from", bundleName); + if (callerToken != nullptr) { + want.SetParam("callerToken", callerToken); + } auto result = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want)); ANS_LOGD("End, result = %{public}d", result); return result; diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 093c5714d..a911deba5 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -1744,7 +1744,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13100, sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); std::string deviceId = "DeviceId"; - EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId), (int)ERR_ANS_INVALID_PARAM); + sptr callerToken = nullptr; + EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, callerToken), (int)ERR_ANS_INVALID_PARAM); } /** @@ -2321,7 +2322,8 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16900, std::string deviceId = "DeviceId"; bool needPop = false; - EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId), ERR_ANS_INVALID_BUNDLE); + sptr callerToken = nullptr; + EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, callerToken), ERR_ANS_INVALID_BUNDLE); EXPECT_EQ(advancedNotificationService_->IsAllowedNotifySelf(needPop), ERR_ANS_INVALID_BUNDLE); sptr bundleOption; EXPECT_EQ(advancedNotificationService_->IsAllowedNotifySelf(bundleOption, needPop), ERR_ANS_INVALID_BUNDLE); diff --git a/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp b/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp index ab85b9ac7..3d9614b36 100644 --- a/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp +++ b/services/ans/test/unittest/notification_dialog_test/notification_dialog_test.cpp @@ -89,7 +89,8 @@ HWTEST_F(NotificationDialogTest, NotificationDialog_00300, Function | SmallTest EXPECT_EQ(result2, code); int32_t uid = 2; - ErrCode result3 = notificationDialog.StartEnableNotificationDialogAbility(uid); + sptr callerToken = nullptr; + ErrCode result3 = notificationDialog.StartEnableNotificationDialogAbility(uid, callerToken); EXPECT_EQ(result3, ERR_ANS_INVALID_BUNDLE); } @@ -112,7 +113,8 @@ HWTEST_F(NotificationDialogTest, NotificationDialog_00400, Function | SmallTest EXPECT_EQ(result2, code); int32_t uid = -1; - ErrCode result3 = notificationDialog.StartEnableNotificationDialogAbility(uid); + sptr callerToken = nullptr; + ErrCode result3 = notificationDialog.StartEnableNotificationDialogAbility(uid, callerToken); EXPECT_EQ(result3, ERR_ANS_INVALID_BUNDLE); } } // namespace Notification diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts index 711ce1f95..ef8e4e42e 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts @@ -19,21 +19,37 @@ import display from '@ohos.display'; import deviceInfo from '@ohos.deviceInfo'; const TAG = 'NotificationDialog_Service'; +let winNum = 1; +let win; export default class NotificationDialogServiceExtensionAbility extends extension { onCreate(want): void { - console.debug(TAG, "onCreate, want: " + JSON.stringify(want)); - globalThis.notificationExtensionContext = this.context; - globalThis.closeDialog = (): void => { - console.info(TAG, 'click waiting for a response'); - globalThis.notificationExtensionContext.terminateSelf(); - } + console.debug(TAG, "onCreate, want: " + JSON.stringify(want)); + globalThis.notificationExtensionContext = this.context; + globalThis.closeDialog = (): void => { + console.info(TAG, 'click waiting for a response'); + globalThis.notificationExtensionContext.terminateSelf(); + } }; onRequest(want, startId): void { globalThis.abilityWant = want; - console.log(TAG, 'globalThis.resolution' + JSON.stringify(globalThis.resolution)); + + if (want["parameters"]["callerToken"] !== undefined && want["parameters"]["callerToken"] != null) { + globalThis.callerToken = want["parameters"]["callerToken"]; + } display.getDefaultDisplay().then(() => { - this.createWindow('EnableNotificationDialog' + startId, window.WindowType.TYPE_SYSTEM_ALERT); + + if (globalThis.callerToken != null) { + if (winNum > 1) { + win.destroy(); + winNum--; + } + this.createWindow('EnableNotificationDialog' + startId, window.WindowType.TYPE_DIALOG); + winNum++; + } else { + this.createWindow('EnableNotificationDialog' + startId, window.WindowType.TYPE_SYSTEM_ALERT); + } + }); } @@ -44,7 +60,18 @@ export default class NotificationDialogServiceExtensionAbility extends extension private async createWindow(name: string, windowType: number) { console.info(TAG, 'create window'); try { - const win = await window.create(globalThis.notificationExtensionContext, name, windowType); + win = await window.create(globalThis.notificationExtensionContext, name, windowType); + + if (globalThis.callerToken != null) { + await win.bindDialogTarget(globalThis.callerToken.value, () => { + win.destroyWindow(); + winNum--; + if (winNum === 0) { + globalThis.selectExtensionContext.terminateSelf(); + } + }); + } + await win.show(); if (deviceInfo.deviceType === 'default' || deviceInfo.deviceType === 'phone') { await win.setWindowLayoutFullScreen(true); diff --git a/test/fuzztest/advancednotificationservice_fuzzer/advancednotificationservice_fuzzer.cpp b/test/fuzztest/advancednotificationservice_fuzzer/advancednotificationservice_fuzzer.cpp index ad39f6eb5..ee5efeace 100644 --- a/test/fuzztest/advancednotificationservice_fuzzer/advancednotificationservice_fuzzer.cpp +++ b/test/fuzztest/advancednotificationservice_fuzzer/advancednotificationservice_fuzzer.cpp @@ -77,7 +77,8 @@ namespace OHOS { advancedNotificationService.DeleteAll(); advancedNotificationService.GetSlotsByBundle(bundleOption, slots); advancedNotificationService.UpdateSlots(bundleOption, slots); - advancedNotificationService.RequestEnableNotification(stringData); + sptr callerToken = nullptr; + advancedNotificationService.RequestEnableNotification(stringData, callerToken); bool enabled = *data % ENABLE; advancedNotificationService.SetNotificationsEnabledForBundle(stringData, enabled); advancedNotificationService.SetNotificationsEnabledForAllBundles(stringData, enabled); diff --git a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp index 96d00f115..8d3f24f0a 100644 --- a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp +++ b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp @@ -148,7 +148,8 @@ namespace OHOS { ansManagerStub.DeleteAll(); ansManagerStub.GetSlotsByBundle(bundleOption, slots); ansManagerStub.UpdateSlots(bundleOption, slots); - ansManagerStub.RequestEnableNotification(stringData); + sptr callerToken = nullptr; + ansManagerStub.RequestEnableNotification(stringData, callerToken); bool enabled = *data % ENABLE; ansManagerStub.SetNotificationsEnabledForBundle(stringData, enabled); ansManagerStub.SetNotificationsEnabledForSpecialBundle(stringData, bundleOption, enabled); diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp index 08dafc8cb..2c2ede61f 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp @@ -21,7 +21,8 @@ namespace OHOS { { // test RequestEnableNotification function std::string deviceId(data); - Notification::NotificationHelper::RequestEnableNotification(deviceId); + sptr callerToken = nullptr; + Notification::NotificationHelper::RequestEnableNotification(deviceId, callerToken); // test HasNotificationPolicyAccessPermission function bool hasPermission = true; Notification::NotificationHelper::HasNotificationPolicyAccessPermission(hasPermission); -- Gitee