diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 9bf597c9321e3200a69e48d3460b81af91098f2f..64c3df66bd74fde31edb2e52338142bce50c56aa 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -92,6 +92,7 @@ ohos_source_set("ans_service_sources") { "src/common/aes_gcm_helper.cpp", "src/common/file_utils.cpp", "src/common/notification_analytics_util.cpp", + "src/common/ans_status.cpp", "src/common/notification_app_privileges.cpp", "src/common/notification_config_parse.cpp", "src/distributed_device_status.cpp", diff --git a/services/ans/include/ans_status.h b/services/ans/include/ans_status.h new file mode 100644 index 0000000000000000000000000000000000000000..ba63233d497d7786c7e7055804da61600e47f0c1 --- /dev/null +++ b/services/ans/include/ans_status.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2025-2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ANS_STATUS_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ANS_STATUS_H + +#include +#include +#include "notification_analytics_util.h" + +namespace OHOS { +namespace Notification { +class AnsStatus { +public: + AnsStatus() = default; + AnsStatus(int32_t errCode, const std::string& msg); + AnsStatus(int32_t errCode, const std::string& msg, int32_t sceneId, int32_t branchId); + bool Ok(); + AnsStatus& AppendSceneBranch(int32_t sceneId, int32_t branchId); + AnsStatus& AppendSceneBranch(int32_t sceneId, int32_t branchId, const std::string& msg); + int32_t GetErrCode(); + static AnsStatus InvalidParam(int32_t sceneId, int32_t branchId); + static AnsStatus InvalidParam(const std::string& msg, int32_t sceneId, int32_t branchId); + HaMetaMessage BuildMessage(bool isPrint); +private: + std::string FormatSceneBranchStr(int32_t sceneId, int32_t branchId); + int32_t errCode_{0}; + std::string msg_{}; + int32_t branchId_{-1}; + int32_t sceneId_{-1}; + std::string path_{}; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_ANS_STATUS_H diff --git a/services/ans/include/base_publish_process.h b/services/ans/include/base_publish_process.h index 618ec1fedcdc3acebdb28bd5510e0a468fe2bc49..a8796f6c33927de0de430cc0a3f9eaf4690b340b 100644 --- a/services/ans/include/base_publish_process.h +++ b/services/ans/include/base_publish_process.h @@ -17,6 +17,7 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_BASE_PUBLISH_PROCESS_H #include "notification_request.h" +#include "ans_status.h" namespace OHOS { namespace Notification { @@ -24,7 +25,7 @@ class BasePublishProcess { public: BasePublishProcess() = default; ~BasePublishProcess() = default; - virtual ErrCode PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed); + virtual AnsStatus PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed); virtual ErrCode PublishNotificationByApp(const sptr &request) = 0; ErrCode CommonPublishCheck(const sptr &request); ErrCode CommonPublishProcess(const sptr &request); diff --git a/services/ans/include/live_publish_process.h b/services/ans/include/live_publish_process.h index 227af2daa7c179e8c6ecbd5f76049997f02a92d8..b5f0d7379d89d22d217b449885073ce363d626ca 100644 --- a/services/ans/include/live_publish_process.h +++ b/services/ans/include/live_publish_process.h @@ -17,13 +17,14 @@ #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_LIVE_PUBLISH_PROCESS_H #include "base_publish_process.h" +#include "ans_status.h" namespace OHOS { namespace Notification { class LivePublishProcess final : public BasePublishProcess { public: static std::shared_ptr GetInstance(); - ErrCode PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) override; + AnsStatus PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) override; ErrCode PublishNotificationByApp(const sptr &request) override; void EraseLiveViewSubsciber(int32_t uid); void AddLiveViewSubscriber(int32_t uid); diff --git a/services/ans/include/notification_analytics_util.h b/services/ans/include/notification_analytics_util.h index 292ec786db4a0c75c87fa3663ca400eae3146ad1..fcc1f38b1cc1c36a85930e256ea1d1ef25951cc9 100644 --- a/services/ans/include/notification_analytics_util.h +++ b/services/ans/include/notification_analytics_util.h @@ -85,6 +85,7 @@ public: HaMetaMessage& BranchId(uint32_t branchId); HaMetaMessage& ErrorCode(uint32_t errorCode); HaMetaMessage& Message(const std::string& message, bool print = false); + HaMetaMessage& Path(const std::string path); HaMetaMessage& Append(const std::string& message); HaMetaMessage& BundleName(const std::string& bundleName_); HaMetaMessage& AgentBundleName(const std::string& agentBundleName); @@ -114,6 +115,7 @@ public: uint32_t branchId_; uint32_t errorCode_ = ERR_OK; std::string message_; + std::string path_; bool checkfailed_ = true; int32_t deleteReason_ = -1; static int32_t syncWatch_; diff --git a/services/ans/src/advanced_notification_publish/base_publish_process.cpp b/services/ans/src/advanced_notification_publish/base_publish_process.cpp index d3674edeee194f1167587f3eca5be7940ec1d073..ace0df57041a660a65d0e670df7e3af4d78e18ed 100644 --- a/services/ans/src/advanced_notification_publish/base_publish_process.cpp +++ b/services/ans/src/advanced_notification_publish/base_publish_process.cpp @@ -24,18 +24,19 @@ #include "ipc_skeleton.h" #include "parameters.h" #include "notification_analytics_util.h" +#include "ans_status.h" namespace OHOS { namespace Notification { -ErrCode BasePublishProcess::PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) +AnsStatus BasePublishProcess::PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) { if (!request->IsRemoveAllowed()) { if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) { request->SetRemoveAllowed(true); } } - return ERR_OK; + return AnsStatus(); } ErrCode BasePublishProcess::CommonPublishCheck(const sptr &request) diff --git a/services/ans/src/advanced_notification_publish/live_publish_process.cpp b/services/ans/src/advanced_notification_publish/live_publish_process.cpp index 35487d3040259de8be18522292f15ce3a374a9ef..d4d5ce8df142eefeec0628d0305abbd835c7d7f3 100644 --- a/services/ans/src/advanced_notification_publish/live_publish_process.cpp +++ b/services/ans/src/advanced_notification_publish/live_publish_process.cpp @@ -23,6 +23,7 @@ #include "notification_content.h" #include "notification_live_view_content.h" #include "os_account_manager_helper.h" +#include "ans_status.h" #include "advanced_notification_inline.cpp" @@ -45,14 +46,11 @@ std::shared_ptr LivePublishProcess::GetInstance() return instance_; } -ErrCode LivePublishProcess::PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) +AnsStatus LivePublishProcess::PublishPreWork(const sptr &request, bool isUpdateByOwnerAllowed) { HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1); if (!CheckLocalLiveViewAllowed(request, isUpdateByOwnerAllowed)) { - message.BranchId(EventBranchId::BRANCH_3).ErrorCode(ERR_ANS_NON_SYSTEM_APP) - .Message("CheckLocalLiveViewAllowed is false", true); - NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); - return ERR_ANS_NON_SYSTEM_APP; + return AnsStatus(ERR_ANS_NON_SYSTEM_APP, "CheckLocalLiveViewAllowed is false", EventSceneId::SCENE_1, EventBranchId::BRANCH_1); } if (!request->IsRemoveAllowed()) { @@ -65,13 +63,10 @@ ErrCode LivePublishProcess::PublishPreWork(const sptr &requ !AccessTokenHelper::IsSystemApp(); if (isUpdateByOwnerAllowed && isHap) { if (request->GetTemplate() == nullptr) { - message.BranchId(EventBranchId::BRANCH_4).ErrorCode(ERR_ANS_INVALID_PARAM) - .Message("Owner must has template to update", true); - NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); - return ERR_ANS_INVALID_PARAM; + return AnsStatus::InvalidParam("Owner must has template to update", EventSceneId::SCENE_1, EventBranchId::BRANCH_4); } } - return ERR_OK; + return AnsStatus(); } ErrCode LivePublishProcess::PublishNotificationByApp(const sptr &request) diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index fa2cb42aeeb5beb7950f9d2aeb6ef3f99f3abbdf..4169be73283abd7fec622f3b7b7864b1485f83d3 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -133,15 +133,17 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt } request->SetCreateTime(GetCurrentTime()); - HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1); + bool isUpdateByOwnerAllowed = IsUpdateSystemLiveviewByOwner(request); - ErrCode result = publishProcess_[request->GetSlotType()]->PublishPreWork(request, isUpdateByOwnerAllowed); - if (result != ERR_OK) { - message.BranchId(EventBranchId::BRANCH_0).ErrorCode(result).Message("publish prework failed", true); - NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); - return result; + AnsStatus ansStatus = publishProcess_[request->GetSlotType()]->PublishPreWork(request, isUpdateByOwnerAllowed); + if (!ansStatus.Ok()) { + ansStatus.AppendSceneBranch(EventSceneId::SCENE_1, EventBranchId::BRANCH_0, "publish prework failed"); + NotificationAnalyticsUtil::ReportPublishFailedEvent(request, ansStatus.BuildMessage(true)); + return ansStatus.GetErrCode(); } - result = CheckUserIdParams(request->GetReceiverUserId()); + + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1); + ErrCode result = CheckUserIdParams(request->GetReceiverUserId()); if (result != ERR_OK) { message.SceneId(EventSceneId::SCENE_3).ErrorCode(result).Message("User is invalid", true); NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 9105ed040ad9defbe7902281dc9267317c16e353..6546d803e69498835586d21646a07b16d7cd1e2a 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -1792,7 +1792,8 @@ ErrCode AdvancedNotificationService::PrePublishRequest(const sptrGetSlotType()]->PublishPreWork(request, false); + AnsStatus ansStatus = publishProcess_[request->GetSlotType()]->PublishPreWork(request, false); + ErrCode result = ansStatus.GetErrCode(); if (result != ERR_OK) { message.BranchId(EventBranchId::BRANCH_0).ErrorCode(result).Message("publish prework failed", true); NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message); diff --git a/services/ans/src/common/ans_status.cpp b/services/ans/src/common/ans_status.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a6d1963754579e5d1e75960b82b6dbd264adba92 --- /dev/null +++ b/services/ans/src/common/ans_status.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2025-2025 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ans_status.h" +#include +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace Notification { + +AnsStatus::AnsStatus(int32_t errCode, const std::string& msg) +{ + errCode_ = errCode; + msg_ = msg; +} + +AnsStatus::AnsStatus(int32_t errCode, const std::string& msg, int32_t sceneId, int32_t branchId) +{ + errCode_ = errCode; + msg_ = msg; + sceneId_ = sceneId; + branchId_ = branchId; + path_ = FormatSceneBranchStr(sceneId, branchId); +} + +bool AnsStatus::Ok() +{ + return errCode_ == ERR_OK; +} + +std::string AnsStatus::FormatSceneBranchStr(int32_t sceneId, int32_t branchId) +{ + return std::to_string(sceneId) + "_" + std::to_string(branchId); +} + +AnsStatus& AnsStatus::AppendSceneBranch(int32_t sceneId, int32_t branchId) +{ + return AppendSceneBranch(sceneId, branchId, ""); +} + +AnsStatus& AnsStatus::AppendSceneBranch(int32_t sceneId, int32_t branchId, const std::string& msg) +{ + std::string path = FormatSceneBranchStr(sceneId, branchId); + path_.append("#" + path); + msg_.append("#" + msg); + return *this; +} + +int32_t AnsStatus::GetErrCode() +{ + return errCode_; +} + +AnsStatus AnsStatus::InvalidParam(int32_t sceneId, int32_t branchId) +{ + return AnsStatus::InvalidParam("Invalid param", sceneId, branchId); +} + +AnsStatus AnsStatus::InvalidParam(const std::string& msg, int32_t sceneId, int32_t branchId) +{ + return AnsStatus(ERR_ANS_INVALID_PARAM, msg, sceneId, branchId); +} + +HaMetaMessage AnsStatus::BuildMessage(bool isPrint) +{ + ANS_LOGD("Build message(branchId: %{public}d, sceneId: %{public}d, path: %{public}s, msg: %{public}s)", branchId_, sceneId_, path_.c_str(), msg_.c_str()); + return HaMetaMessage(branchId_, sceneId_).Path(path_).Message(msg_, isPrint); +} + +} // namespace Notification +} // namespace OHOS diff --git a/services/ans/src/common/notification_analytics_util.cpp b/services/ans/src/common/notification_analytics_util.cpp index 66005871a0a960f7922bb8ec1ec1726d7fd2bc44..9083184ee0960caffe857f255e5e29c2547abec3 100644 --- a/services/ans/src/common/notification_analytics_util.cpp +++ b/services/ans/src/common/notification_analytics_util.cpp @@ -48,7 +48,7 @@ constexpr const int32_t REPORT_CACHE_MAX_SIZE = 50; constexpr const int32_t SUCCESS_REPORT_CACHE_MAX_SIZE = 60; constexpr const int32_t REPORT_CACHE_INTERVAL_TIME = 30; constexpr const int32_t SUCCESS_REPORT_CACHE_INTERVAL_TIME = 1800; -constexpr const int32_t REASON_MAX_LENGTH = 127; +constexpr const int32_t REASON_MAX_LENGTH = 255; constexpr const int32_t SUB_CODE = 100; constexpr const int32_t MAX_TIME = 43200000; constexpr const int32_t NOTIFICATION_MAX_DATA = 100; @@ -131,6 +131,12 @@ HaMetaMessage& HaMetaMessage::Message(const std::string& message, bool print) return *this; } +HaMetaMessage& HaMetaMessage::Path(const std::string path) +{ + path_ = path; + return *this; +} + HaMetaMessage& HaMetaMessage::Append(const std::string& message) { message_+=message; diff --git a/services/ans/test/unittest/publish_process_test.cpp b/services/ans/test/unittest/publish_process_test.cpp index fc5f86dc644ce601942ea81f90ed2db0059d13f6..e0d1b84a096a83aa8c8d9b812ac43cb376b078ad 100644 --- a/services/ans/test/unittest/publish_process_test.cpp +++ b/services/ans/test/unittest/publish_process_test.cpp @@ -162,7 +162,7 @@ HWTEST_F(PublishProcessTest, LivePublishPreWork_00001, Function | SmallTest | Le LivePublishProcess progress; auto res = progress.PublishPreWork(request, true); ASSERT_EQ(request->IsRemoveAllowed(), true); - ASSERT_EQ(res, ERR_OK); + ASSERT_EQ(res.GetErrCode(), ERR_OK); } /** @@ -178,7 +178,7 @@ HWTEST_F(PublishProcessTest, LivePublishPreWork_00002, Function | SmallTest | Le LivePublishProcess progress; auto res = progress.PublishPreWork(request, true); - ASSERT_EQ(res, ERR_ANS_INVALID_PARAM); + ASSERT_EQ(res.GetErrCode(), ERR_ANS_INVALID_PARAM); } /** @@ -200,7 +200,7 @@ HWTEST_F(PublishProcessTest, LivePublishNotificationByApp_00001, Function | Smal LivePublishProcess progress; auto res = progress.PublishPreWork(request, false); - ASSERT_EQ(res, ERR_ANS_NON_SYSTEM_APP); + ASSERT_EQ(res.GetErrCode(), ERR_ANS_NON_SYSTEM_APP); } /**