diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 0fc8750b92458c85c5304903416072212451d28c..2eac9228496e18e20a3a27c9e226c302b9f3c183 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 8dcaa32ee67061ada178443819c5ab28279aaff8..af385f78995b29c46211877385b587af287dbdfe 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 ccccdcd2fc88d613189b2ad55c99925cf285ad6c..6159fd14580599269ed2dd1c2d6bf690eebae328 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 a73751a9e52168c87f7beb403baf02ef1a2eda66..c31fc4330fe626cb8e212fbf9c78fca1d483f260 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 b91aeef02f98603a2879f7503670f44b4fdf4982..78fcb73054695d15309faef3b4eff73dd3b3e410 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 0794a5c4cd974e24faf77d792cc805da9750202a..b91ea0decdfce3d490b700696a08cd2d7f8c3aad 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 7afd2eb3d21fab9107239bd7cb5d154a47d72dde..494504b3fdec0188bffaf4311dc5392ad2dfa8e3 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 7fd70d81234c74c859d8a164e5f264cdaa722dbe..f2f4351177488cbdf007e37ce2ef4af7adeaaf28 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 feea0bbbc85b875c735827cbc7143ba7a517d0f5..9bcbc1b6bcb0468222721a2a878c3e0093b644ea 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 193e37a6a09f42fa058e86fbbb93b8eacc03875a..07d9cbe199834285bf5c9af2c9c8c8c86c3d6377 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 710b7f0f0824a111c5017eb95c9f94daa0bd57fc..154e105437f82b51607dc99626b5b110c250a6bd 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 ed2b116786cbc09218a9e34c2f993668133c966a..4723a0cfdf2c5e016cbbd00d47a9343cca26e4b7 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 9c76362c55414a2406758b5718bacdfd6a5b9016..182f495e0fe9e9e66876fa9cd59a8db0c00c725f 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 a75cce5d55263116962fd3c06df3722acbd060ef..8430c7e144d424428b02170a8e2f306870107f70 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 a7a7e7e46ee98c30df0295e2ed475f83ebacceec..9117d5105316848b6084c038c040bfbdddc45f82 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 cf5a0f870e9f9182a2bb3a3085e5760bf23dfaff..9f19a838552dac4c1fcd0d010c82f7371eae0a7d 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 d5358fd0d466a5ac7d9c86208d9169e505e6e78d..31dd4d253a3f8e386aab7b8f450cf81bd0fa5d11 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 6956d1b8d9a052f6e1de2a546f5f93f842dd7d11..fdff334760137a49e4ac74f6b846b3e671c55ca6 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 c5e3f0aafde0ffafe71938742fdaec8e45172e7e..f0477e1a04fdc72661918d58b9b0baeda3ce3bc6 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 a47a54b8bfb714ccfd9cfd9299b7aef72b4ac923..5f1725735c0ebc0d97e9670b18e6c7b5bf236873 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 6640c7cf0c114a880389d7f5ba842cc00724ea54..8bb739115ef5c17c00fcfa14c413a374173d8a5a 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 1fb3b28849baa7c4a72607cc548c0334c6cc268a..546f58b836801d2806bdcf2891f87bc4e1460614 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 093c5714d6e57b8f2e173b90e9d550fe3a38c608..a911deba53cafb6590600096513885bcb9d35b38 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 ab85b9ac7ea4964be932851f2ae19043b9ce7238..3d9614b36bf6fb8b9e65893c04c94682388a3c73 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 711ce1f9533057e5ed78073f8d4e1834b02e52d6..ef8e4e42ee99d956e8ff9624f292b0bc6b40054f 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 ad39f6eb5be5d50dd7061f93193d31a9755a79be..ee5efeacecec709f03d7ad38f106c0a441be403b 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 96d00f115ada4c9f7e7f8ebdd5cca991bf1997d3..8d3f24f0a88f0c2db15b34ad9632cf446fa9452f 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 08dafc8cb2bb8b84cc596c42a4626266bd04440c..2c2ede61f54684a65c7f4b9194336fd39ee2e53d 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);