diff --git a/frameworks/js/napi/src/common_convert_content.cpp b/frameworks/js/napi/src/common_convert_content.cpp index 8f2a02008fb70fb48233df7be23e6a7b2081c1e4..ad8419f7f444c1ceba6fce85e1d4696594578331 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 dcd72551009d1133e9938937aa813640219517cd..9e94d2fc139846637b2b02a783bd5a8614093ddd 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 aa04b87366a07945b89c1d55b9b2ee48fab5ae2c..c3acd3d21bc04b7db2a8b612517a9bb94ae6edf0 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 e8268fc7f2160cb774ca7c103ebb42a9427f3f5b..bada0de6a70eb3bf4468afd43d44de790ac23790 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