From b5c78843553cd4e083bf35d563fe62f6fba731ba Mon Sep 17 00:00:00 2001 From: z00454238 Date: Thu, 3 Jul 2025 15:33:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ElineWantAgents=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BC=98=E5=8C=96=20Signed-off-by:=20z00454238=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../js/napi/src/common_convert_content.cpp | 14 +++-- .../include/advanced_notification_service.h | 2 + .../advanced_notification_publish.cpp | 34 ++++++++++ ...nced_notification_service_publish_test.cpp | 62 +++++++++++++++++++ 4 files changed, 108 insertions(+), 4 deletions(-) diff --git a/frameworks/js/napi/src/common_convert_content.cpp b/frameworks/js/napi/src/common_convert_content.cpp index 8f2a02008..ad8419f7f 100644 --- a/frameworks/js/napi/src/common_convert_content.cpp +++ b/frameworks/js/napi/src/common_convert_content.cpp @@ -330,7 +330,7 @@ napi_value Common::SetNotificationMultiLineContent( } napi_set_named_property(env, result, "lines", arr); - //lineWantAgents: Array + //lineWantAgents?: Array auto lineWantAgents = multiLineContent->GetLineWantAgents(); if (lineWantAgents.size() > 0) { napi_value lineWantAgentsArr = nullptr; @@ -1441,7 +1441,7 @@ napi_value Common::GetNotificationMultiLineContent( return nullptr; } - // lineWantAgents: Array + // lineWantAgents?: Array NAPI_CALL(env, napi_has_named_property(env, contentResult, "lineWantAgents", &hasProperty)); if (hasProperty) { if (GetNotificationContentLineWantAgents(env, contentResult, multiLineContent) == nullptr) { @@ -1515,7 +1515,7 @@ napi_value Common::GetNotificationContentLineWantAgents(const napi_env &env, con if (!isArray) { ANS_LOGE("lineWantAgents is expected to be an array."); std::string msg = "Incorrect parameter types. The type of lineWantAgents must be array."; - Common::NapiThrow(env, ERROR_PARAM_INVALID); + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); return nullptr; } napi_get_array_length(env, value, &length); @@ -1526,7 +1526,7 @@ napi_value Common::GetNotificationContentLineWantAgents(const napi_env &env, con if (valuetype != napi_object) { ANS_LOGE("Wrong agrument type. Object expected."); std::string msg = "Incorrect parameter types. The type of lineWantAgents item must be object."; - Common::NapiThrow(env, ERROR_PARAM_INVALID); + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); return nullptr; } AbilityRuntime::WantAgent::WantAgent *wantAgent = nullptr; @@ -1535,6 +1535,12 @@ napi_value Common::GetNotificationContentLineWantAgents(const napi_env &env, con ANS_LOGE("Invalid object lineWantAgents"); return nullptr; } + if (lineWantAgents.size() >= multiLineContent->GetAllLines().size()) { + ANS_LOGE("lineWantAgents size is larger than lines size."); + std::string msg = "Incorrect parameter types. LineWantAgents size is larger than lines size."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } lineWantAgents.push_back(std::make_shared(*wantAgent)); } multiLineContent->SetLineWantAgents(lineWantAgents); diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index dcd725510..9e94d2fc1 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1672,6 +1672,8 @@ private: void SetChainIdToExtraInfo(const sptr &request, OHOS::HiviewDFX::HiTraceId traceId); ErrCode CheckNotificationRequest(const sptr &request); + ErrCode CheckNotificationRequestLineWantAgents(const std::shared_ptr &content, + bool isAgentController, bool isSystemComp); private: static sptr instance_; diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index aa04b8736..c3acd3d21 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -391,6 +391,16 @@ ErrCode AdvancedNotificationService::CheckNotificationRequest(const sptrGetWantAgent(); const auto removalWantAgent = request->GetRemovalWantAgent(); + + auto content = request->GetContent(); + if (content != nullptr && content->GetContentType() == NotificationContent::Type::MULTILINE) { + ErrCode checkCode = CheckNotificationRequestLineWantAgents(content, isAgentController, + isSystemApp || isSubsystem); + if (checkCode != ERR_OK) { + return checkCode; + } + } + const auto isLocalWantAgent = (wantAgent != nullptr && wantAgent->IsLocal()) || (removalWantAgent != nullptr && removalWantAgent->IsLocal()); bool isSpecifiedAccess = (isSystemApp || isSubsystem) && isAgentController; @@ -401,5 +411,29 @@ ErrCode AdvancedNotificationService::CheckNotificationRequest(const sptr &content, bool isAgentController, bool isSystemComp) +{ + auto multiLineContent = + std::static_pointer_cast(content->GetNotificationContent()); + if (multiLineContent != nullptr) { + auto lineWantAgents = multiLineContent->GetLineWantAgents(); + if (lineWantAgents.size() > 0) { + if (!isAgentController) { + ANS_LOGE("LineWantAgents does not support permission denied"); + return ERR_ANS_PERMISSION_DENIED; + } + auto lineWantAgentsIter = std::find_if(lineWantAgents.begin(), lineWantAgents.end(), + [&](auto& lineWantAgent) { + return (lineWantAgent != nullptr && lineWantAgent->IsLocal()); + }); + if (lineWantAgentsIter != lineWantAgents.end() && !isSystemComp) { + ANS_LOGE("Local wantAgent does not support non system app"); + return ERR_ANS_NON_SYSTEM_APP; + } + } + } + return ERR_OK; +} } // Notification } // OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp index e8268fc7f..bada0de6a 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_service_publish_test.cpp @@ -1851,5 +1851,67 @@ HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0500, Level1) ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); } + +/** + * @tc.number : CheckNotificationRequest_0600 + * @tc.name : CheckNotificationRequest_0600 + * @tc.desc : Test CheckNotificationRequest method when request has linesWantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequest_0600, Level1) +{ + AdvancedNotificationService advancedNotificationService; + auto request = new NotificationRequest(); + const std::shared_ptr multiLineContent = + std::make_shared(); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + std::vector> lineWantAgents; + lineWantAgents.emplace_back(wantAgent); + lineWantAgents.emplace_back(wantAgent); + lineWantAgents.emplace_back(wantAgent); + multiLineContent->AddSingleLine("test1"); + multiLineContent->AddSingleLine("test2"); + multiLineContent->AddSingleLine("test3"); + multiLineContent->SetLineWantAgents(lineWantAgents); + const std::shared_ptr content = std::make_shared(multiLineContent); + request->SetContent(content); + MockSystemApp(); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequest(request), ERR_OK); +} + +/** + * @tc.number : CheckNotificationRequestLineWantAgents_0100 + * @tc.name : CheckNotificationRequestLineWantAgents_0100 + * @tc.desc : Test CheckNotificationRequestLineWantAgents method when request has local wantAgent + * @tc.require : I5TIQR + */ +HWTEST_F(AdvancedNotificationServiceTest, CheckNotificationRequestLineWantAgents_0100, Level1) +{ + AdvancedNotificationService advancedNotificationService; + const std::shared_ptr multiLineContent = + std::make_shared(); + std::shared_ptr want = std::make_shared(); + std::shared_ptr localPendingWant = + std::make_shared("TestBundleName", want, 0); + std::shared_ptr wantAgent = + std::make_shared(localPendingWant); + std::vector> lineWantAgents; + lineWantAgents.emplace_back(wantAgent); + lineWantAgents.emplace_back(wantAgent); + lineWantAgents.emplace_back(wantAgent); + multiLineContent->AddSingleLine("test1"); + multiLineContent->AddSingleLine("test2"); + multiLineContent->AddSingleLine("test3"); + multiLineContent->SetLineWantAgents(lineWantAgents); + const std::shared_ptr content = std::make_shared(multiLineContent); + MockIsSystemApp(false); + + ASSERT_EQ(advancedNotificationService.CheckNotificationRequestLineWantAgents(content, true, true), ERR_OK); +} } // namespace Notification } // namespace OHOS -- Gitee