From f8f3a023366399be44ac9be09d6326311a8ea13b Mon Sep 17 00:00:00 2001 From: "zhangyafei.echo" Date: Fri, 17 Jun 2022 10:21:26 +0800 Subject: [PATCH] IssueNo:#I582Y4 Description:1. Add hisysevent to notification. 2. Add hitrace to notification. Sig:SIG_ApplicationFramework Feature or BugFix: Feature Binary Source: No Signed-off-by: zhangyafei.echo Change-Id: I0591f0bb1cdfa31a4aa169232e79603a3ce555ad --- frameworks/ans/BUILD.gn | 1 + .../ans/src/notification_subscriber.cpp | 8 + frameworks/core/BUILD.gn | 1 + .../core/common/include/ans_const_define.h | 1 - .../core/src/ans_manager_death_recipient.cpp | 11 - frameworks/core/src/ans_manager_proxy.cpp | 19 - frameworks/core/src/ans_notification.cpp | 11 + hisysevent.yaml | 141 ++++++-- notification.gni | 9 + services/ans/BUILD.gn | 11 +- .../include/advanced_notification_service.h | 14 + services/ans/include/event_report.h | 99 ++++++ .../ans/src/advanced_notification_service.cpp | 245 +++++++++++-- services/ans/src/event_report.cpp | 234 +++++++++++++ services/ans/src/notification_preferences.cpp | 4 + .../src/notification_preferences_database.cpp | 3 + .../src/notification_subscriber_manager.cpp | 10 + services/ans/src/reminder_timer_info.cpp | 4 + services/ans/test/unittest/BUILD.gn | 3 + .../unittest/notification_hisysevent_test.cpp | 325 ++++++++++++++++++ services/distributed/BUILD.gn | 1 + .../src/distributed_notification_manager.cpp | 4 + services/distributed/test/unittest/BUILD.gn | 1 + services/test/moduletest/BUILD.gn | 2 + 24 files changed, 1073 insertions(+), 89 deletions(-) create mode 100644 services/ans/include/event_report.h create mode 100644 services/ans/src/event_report.cpp create mode 100644 services/ans/test/unittest/notification_hisysevent_test.cpp diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 2b0223144..fc19daad6 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -90,6 +90,7 @@ ohos_shared_library("ans_innerkits") { "ability_base:want", "ability_base:zuri", "ability_runtime:wantagent_innerkits", + "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", diff --git a/frameworks/ans/src/notification_subscriber.cpp b/frameworks/ans/src/notification_subscriber.cpp index 2f5214397..b665d1acc 100644 --- a/frameworks/ans/src/notification_subscriber.cpp +++ b/frameworks/ans/src/notification_subscriber.cpp @@ -15,6 +15,7 @@ #include "notification_subscriber.h" +#include "hitrace_meter.h" #include "iservice_registry.h" #include "system_ability_definition.h" @@ -40,6 +41,7 @@ NotificationSubscriber::SubscriberImpl::SubscriberImpl(NotificationSubscriber &s void NotificationSubscriber::SubscriberImpl::OnConnected() { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (GetAnsManagerProxy()) { proxy_->AsObject()->AddDeathRecipient(recipient_); ANS_LOGD("%s, Add death recipient.", __func__); @@ -49,6 +51,7 @@ void NotificationSubscriber::SubscriberImpl::OnConnected() void NotificationSubscriber::SubscriberImpl::OnDisconnected() { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (GetAnsManagerProxy()) { proxy_->AsObject()->RemoveDeathRecipient(recipient_); ANS_LOGD("%s, Remove death recipient.", __func__); @@ -58,24 +61,28 @@ void NotificationSubscriber::SubscriberImpl::OnDisconnected() void NotificationSubscriber::SubscriberImpl::OnConsumed(const sptr ¬ification) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); subscriber_.OnConsumed(std::make_shared(*notification)); } void NotificationSubscriber::SubscriberImpl::OnConsumed( const sptr ¬ification, const sptr ¬ificationMap) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); subscriber_.OnConsumed( std::make_shared(*notification), std::make_shared(*notificationMap)); } void NotificationSubscriber::SubscriberImpl::OnCanceled(const sptr ¬ification) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); subscriber_.OnCanceled(std::make_shared(*notification)); } void NotificationSubscriber::SubscriberImpl::OnCanceled( const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); subscriber_.OnCanceled(std::make_shared(*notification), std::make_shared(*notificationMap), deleteReason); @@ -94,6 +101,7 @@ void NotificationSubscriber::SubscriberImpl::OnDoNotDisturbDateChange(const sptr void NotificationSubscriber::SubscriberImpl::OnEnabledNotificationChanged( const sptr &callbackData) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); subscriber_.OnEnabledNotificationChanged(std::make_shared(*callbackData)); } diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index a6364c4d5..26f6baaab 100644 --- a/frameworks/core/BUILD.gn +++ b/frameworks/core/BUILD.gn @@ -94,6 +94,7 @@ ohos_shared_library("ans_core") { "eventhandler:libeventhandler", "hicollie_native:libhicollie", "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", diff --git a/frameworks/core/common/include/ans_const_define.h b/frameworks/core/common/include/ans_const_define.h index 97156580a..eb286069d 100644 --- a/frameworks/core/common/include/ans_const_define.h +++ b/frameworks/core/common/include/ans_const_define.h @@ -25,7 +25,6 @@ namespace OHOS { namespace Notification { // Max active notification number -constexpr int32_t DH_ANS_SUCCESS = 0; constexpr size_t MAX_ACTIVE_NUM = 1000; constexpr uint32_t MAX_ACTIVE_NUM_PERAPP = 100; constexpr uint32_t MAX_ACTIVE_NUM_PERSECOND = 10; diff --git a/frameworks/core/src/ans_manager_death_recipient.cpp b/frameworks/core/src/ans_manager_death_recipient.cpp index 37e9069ae..3e71b4acd 100644 --- a/frameworks/core/src/ans_manager_death_recipient.cpp +++ b/frameworks/core/src/ans_manager_death_recipient.cpp @@ -15,10 +15,8 @@ #include -#include "ans_const_define.h" #include "ans_log_wrapper.h" #include "ans_notification.h" -#include "hisysevent.h" #include "singleton.h" #include "ans_manager_death_recipient.h" @@ -26,15 +24,6 @@ namespace OHOS { namespace Notification { void AnsManagerDeathRecipient::OnRemoteDied(const wptr &remote) { - std::string eventType = "ANS_SERVICE_DIED"; - int32_t res = OHOS::HiviewDFX::HiSysEvent::Write( - HiviewDFX::HiSysEvent::Domain::NOTIFICATION, eventType, - HiviewDFX::HiSysEvent::EventType::FAULT, - "UID", getuid(), - "PID", getpid()); - if (res != DH_ANS_SUCCESS) { - ANS_LOGE("Write HiSysEvent error, res:%d", res); - } ANS_LOGE("Ans service died"); DelayedSingleton::GetInstance()->ResetAnsManagerProxy(); } diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 48df44cee..7c00a7b9d 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -18,7 +18,6 @@ #include "ans_const_define.h" #include "ans_inner_errors.h" #include "ans_log_wrapper.h" -#include "hisysevent.h" #include "message_option.h" #include "message_parcel.h" #include "parcel.h" @@ -71,15 +70,6 @@ ErrCode AnsManagerProxy::Publish(const std::string &label, const sptr &args) override; void GetDumpInfo(const std::vector &args, std::string &result); + void SendSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr &info, + ErrCode errCode); + void SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid, const sptr &info); + void SendPublishHiSysEvent(const sptr &request, ErrCode errCode); + void SendCancelHiSysEvent(int32_t notificationId, const std::string &label, + const sptr &bundleOption, ErrCode errCode); + void SendRemoveHiSysEvent(int32_t notificationId, const std::string &label, + const sptr &bundleOption, ErrCode errCode); + void SendEnableNotificationHiSysEvent(const sptr &bundleOption, bool enabled, + ErrCode errCode); + void SendEnableNotificationSlotHiSysEvent(const sptr &bundleOption, + const NotificationConstant::SlotType &slotType, bool enabled, ErrCode errCode); + void SendFlowControlOccurHiSysEvent(const std::shared_ptr &record); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/event_report.h b/services/ans/include/event_report.h new file mode 100644 index 000000000..119381680 --- /dev/null +++ b/services/ans/include/event_report.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2022 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_EVENT_REPORT_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_EVENT_REPORT_H + +#include + +#ifdef HAS_HISYSEVENT_PART +#include "hisysevent.h" +#endif + +namespace OHOS { +namespace Notification { +namespace { +// event name +const std::string SUBSCRIBE_ERROR = "SUBSCRIBE_ERROR"; +const std::string ENABLE_NOTIFICATION_ERROR = "ENABLE_NOTIFICATION_ERROR"; +const std::string ENABLE_NOTIFICATION_SLOT_ERROR = "ENABLE_NOTIFICATION_SLOT_ERROR"; +const std::string PUBLISH_ERROR = "PUBLISH_ERROR"; +const std::string FLOW_CONTROL_OCCUR = "FLOW_CONTROL_OCCUR"; + +const std::string SUBSCRIBE = "SUBSCRIBE"; +const std::string UNSUBSCRIBE = "UNSUBSCRIBE"; +const std::string ENABLE_NOTIFICATION = "ENABLE_NOTIFICATION"; +const std::string ENABLE_NOTIFICATION_SLOT = "ENABLE_NOTIFICATION_SLOT"; + +const std::string PUBLISH = "PUBLISH"; +const std::string CANCEL = "CANCEL"; +const std::string REMOVE = "REMOVE"; +} // namespace + +struct EventInfo { + int32_t notificationId; + int32_t contentType; + int32_t userId; + int32_t pid; + int32_t uid; + int32_t slotType; + int32_t errCode; + bool enable; + std::string bundleName; + std::string notificationLabel; + + EventInfo() : userId(-1), pid(0), uid(0), errCode(0), enable(false) {} +}; + +class EventReport { +public: + /** + * @brief send hisysevent + * + * @param eventName event name, corresponding to the document 'hisysevent.yaml' + * @param eventInfo event info + */ + static void SendHiSysEvent(const std::string &eventName, const EventInfo &eventInfo); + +private: +#ifdef HAS_HISYSEVENT_PART + // fault event + static void InnerSendSubscribeErrorEvent(const EventInfo &eventInfo); + static void InnerSendEnableNotificationErrorEvent(const EventInfo &eventInfo); + static void InnerSendEnableNotificationSlotErrorEvent(const EventInfo &eventInfo); + static void InnerSendPublishErrorEvent(const EventInfo &eventInfo); + static void InnerSendFlowControlOccurEvent(const EventInfo &eventInfo); + + // behavior event + static void InnerSendSubscribeEvent(const EventInfo &eventInfo); + static void InnerSendUnSubscribeEvent(const EventInfo &eventInfo); + static void InnerSendEnableNotificationEvent(const EventInfo &eventInfo); + static void InnerSendEnableNotificationSlotEvent(const EventInfo &eventInfo); + + // statistic event + static void InnerSendPublishEvent(const EventInfo &eventInfo); + static void InnerSendCancelEvent(const EventInfo &eventInfo); + static void InnerSendRemoveEvent(const EventInfo &eventInfo); + + template + static void InnerEventWrite(const std::string &eventName, + HiviewDFX::HiSysEvent::EventType type, Types... keyValues); + + static std::unordered_map ansSysEventFuncMap_; +#endif +}; +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_EVENT_REPORT_H \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 537f95d30..a532e307b 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -35,6 +35,8 @@ #include "common_event_manager.h" #include "common_event_support.h" #include "display_manager.h" +#include "event_report.h" +#include "hitrace_meter.h" #include "ipc_skeleton.h" #include "notification_constant.h" #include "notification_filter.h" @@ -391,6 +393,7 @@ ErrCode AdvancedNotificationService::AssignToNotificationList(const std::shared_ ErrCode AdvancedNotificationService::CancelPreparedNotification( int32_t notificationId, const std::string &label, const sptr &bundleOption) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (bundleOption == nullptr) { return ERR_ANS_INVALID_BUNDLE; } @@ -412,12 +415,15 @@ ErrCode AdvancedNotificationService::CancelPreparedNotification( #endif } })); + + SendCancelHiSysEvent(notificationId, label, bundleOption, result); return result; } ErrCode AdvancedNotificationService::PrepareNotificationInfo( const sptr &request, sptr &bundleOption) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if ((request->GetSlotType() == NotificationConstant::SlotType::CUSTOM) && !IsSystemApp()) { return ERR_ANS_NON_SYSTEM_APP; } @@ -444,6 +450,7 @@ ErrCode AdvancedNotificationService::PrepareNotificationInfo( ErrCode AdvancedNotificationService::PublishPreparedNotification( const sptr &request, const sptr &bundleOption) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGI("PublishPreparedNotification"); auto record = std::make_shared(); record->request = request; @@ -486,18 +493,30 @@ ErrCode AdvancedNotificationService::PublishPreparedNotification( ErrCode AdvancedNotificationService::Publish(const std::string &label, const sptr &request) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); - if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT && !IsSystemApp()) { - return ERR_ANS_NON_SYSTEM_APP; - } + ErrCode result = ERR_OK; + do { + if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT && !IsSystemApp()) { + result = ERR_ANS_NON_SYSTEM_APP; + break; + } - sptr bundleOption; - ErrCode result = PrepareNotificationInfo(request, bundleOption); - if (result != ERR_OK) { - return result; - } - return PublishPreparedNotification(request, bundleOption); + sptr bundleOption; + result = PrepareNotificationInfo(request, bundleOption); + if (result != ERR_OK) { + break; + } + + result = PublishPreparedNotification(request, bundleOption); + if (result != ERR_OK) { + break; + } + } while (0); + + SendPublishHiSysEvent(request, result); + return result; } void AdvancedNotificationService::ReportInfoToResourceSchedule(const int32_t userId, const std::string &bundleName) @@ -1327,28 +1346,42 @@ ErrCode AdvancedNotificationService::RemoveFromNotificationListForDeleteAll( ErrCode AdvancedNotificationService::Subscribe( const sptr &subscriber, const sptr &info) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); - bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); - if (!IsSystemApp() && !isSubsystem) { - ANS_LOGE("Client is not a system app or subsystem"); - return ERR_ANS_NON_SYSTEM_APP; - } + ErrCode errCode = ERR_OK; + do { + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!IsSystemApp() && !isSubsystem) { + ANS_LOGE("Client is not a system app or subsystem"); + errCode = ERR_ANS_NON_SYSTEM_APP; + break; + } - if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { - return ERR_ANS_PERMISSION_DENIED; - } + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + errCode = ERR_ANS_PERMISSION_DENIED; + break; + } - if (subscriber == nullptr) { - return ERR_ANS_INVALID_PARAM; - } + if (subscriber == nullptr) { + errCode = ERR_ANS_INVALID_PARAM; + break; + } + + errCode = NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, info); + if (errCode != ERR_OK) { + break; + } + } while (0); - return NotificationSubscriberManager::GetInstance()->AddSubscriber(subscriber, info); + SendSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info, errCode); + return errCode; } ErrCode AdvancedNotificationService::Unsubscribe( const sptr &subscriber, const sptr &info) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); @@ -1365,7 +1398,13 @@ ErrCode AdvancedNotificationService::Unsubscribe( return ERR_ANS_INVALID_PARAM; } - return NotificationSubscriberManager::GetInstance()->RemoveSubscriber(subscriber, info); + ErrCode errCode = NotificationSubscriberManager::GetInstance()->RemoveSubscriber(subscriber, info); + if (errCode != ERR_OK) { + return errCode; + } + + SendUnSubscribeHiSysEvent(IPCSkeleton::GetCallingPid(), IPCSkeleton::GetCallingUid(), info); + return ERR_OK; } ErrCode AdvancedNotificationService::GetSlotByType( @@ -1573,6 +1612,7 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForAllBundles(const ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( const std::string &deviceId, const sptr &bundleOption, bool enabled) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (!IsSystemApp()) { @@ -1606,6 +1646,8 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( // Remote revice } })); + + SendEnableNotificationHiSysEvent(bundleOption, enabled, result); return result; } @@ -2139,18 +2181,25 @@ ErrCode AdvancedNotificationService::FlowControl(const std::shared_ptr recordToRemove; if (bundleList.size() >= MAX_ACTIVE_NUM_PERAPP) { bundleList.sort(SortNotificationsByLevelAndTime); + recordToRemove = bundleList.front(); + SendFlowControlOccurHiSysEvent(recordToRemove); notificationList_.remove(bundleList.front()); } if (notificationList_.size() >= MAX_ACTIVE_NUM) { if (bundleList.size() > 0) { bundleList.sort(SortNotificationsByLevelAndTime); + recordToRemove = bundleList.front(); + SendFlowControlOccurHiSysEvent(recordToRemove); notificationList_.remove(bundleList.front()); } else { std::list> sorted = notificationList_; sorted.sort(SortNotificationsByLevelAndTime); + recordToRemove = bundleList.front(); + SendFlowControlOccurHiSysEvent(recordToRemove); notificationList_.remove(sorted.front()); } } @@ -2269,6 +2318,7 @@ ErrCode AdvancedNotificationService::AddSlotByType(NotificationConstant::SlotTyp ErrCode AdvancedNotificationService::RemoveNotification( const sptr &bundleOption, int32_t notificationId, const std::string &label) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (!IsSystemApp()) { @@ -2328,11 +2378,13 @@ ErrCode AdvancedNotificationService::RemoveNotification( TriggerRemoveWantAgent(notificationRequest); })); + SendRemoveHiSysEvent(notificationId, label, bundleOption, result); return result; } ErrCode AdvancedNotificationService::RemoveAllNotifications(const sptr &bundleOption) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (!IsSystemApp()) { @@ -2917,6 +2969,7 @@ ErrCode AdvancedNotificationService::DoDistributedPublish( ErrCode AdvancedNotificationService::DoDistributedDelete( const std::string deviceId, const sptr notification) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (!notification->GetNotificationRequest().GetNotificationDistributedOptions().IsDistributed()) { return ERR_OK; } @@ -3155,6 +3208,7 @@ bool AdvancedNotificationService::GetActiveUserId(int& userId) void AdvancedNotificationService::TriggerRemoveWantAgent(const sptr &request) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if ((request == nullptr) || (request->GetRemovalWantAgent() == nullptr)) { @@ -3483,6 +3537,7 @@ void AdvancedNotificationService::GetDisplayPosition( ErrCode AdvancedNotificationService::SetEnabledForBundleSlot( const sptr &bundleOption, const NotificationConstant::SlotType &slotType, bool enabled) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("slotType: %{public}d, enabled: %{public}d", slotType, enabled); if (!IsSystemApp()) { @@ -3531,6 +3586,8 @@ ErrCode AdvancedNotificationService::SetEnabledForBundleSlot( PublishSlotChangeCommonEvent(bundle); })); + + SendEnableNotificationSlotHiSysEvent(bundleOption, slotType, enabled, result); return result; } @@ -3573,6 +3630,7 @@ ErrCode AdvancedNotificationService::GetEnabledForBundleSlot( bool AdvancedNotificationService::PublishSlotChangeCommonEvent(const sptr &bundleOption) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("bundle [%{public}s : %{public}d]", bundleOption->GetBundleName().c_str(), bundleOption->GetUid()); EventFwk::Want want; @@ -3655,5 +3713,148 @@ void AdvancedNotificationService::GetDumpInfo(const std::vector result.append(info); } } + +void AdvancedNotificationService::SendSubscribeHiSysEvent(int32_t pid, int32_t uid, + const sptr &info, ErrCode errCode) +{ + EventInfo eventInfo; + eventInfo.pid = pid; + eventInfo.uid = uid; + if (info != nullptr) { + eventInfo.userId = info->GetAppUserId(); + std::vector appNames = info->GetAppNames(); + eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""), + [appNames](std::string bundleName, const std::string &str) { + return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + } + + if (errCode != ERR_OK) { + eventInfo.errCode = errCode; + EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo); + } else { + EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo); + } +} + +void AdvancedNotificationService::SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid, + const sptr &info) +{ + EventInfo eventInfo; + eventInfo.pid = pid; + eventInfo.uid = uid; + if (info != nullptr) { + eventInfo.userId = info->GetAppUserId(); + std::vector appNames = info->GetAppNames(); + eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""), + [appNames](std::string bundleName, const std::string &str) { + return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + } + + EventReport::SendHiSysEvent(UNSUBSCRIBE, eventInfo); +} + +void AdvancedNotificationService::SendPublishHiSysEvent(const sptr &request, ErrCode errCode) +{ + if (request == nullptr) { + return; + } + + EventInfo eventInfo; + eventInfo.notificationId = request->GetNotificationId(); + eventInfo.contentType = static_cast(request->GetNotificationType()); + eventInfo.bundleName = request->GetCreatorBundleName(); + eventInfo.userId = request->GetCreatorUserId(); + if (errCode != ERR_OK) { + eventInfo.errCode = errCode; + EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo); + } else { + EventReport::SendHiSysEvent(PUBLISH, eventInfo); + } +} + +void AdvancedNotificationService::SendCancelHiSysEvent(int32_t notificationId, const std::string &label, + const sptr &bundleOption, ErrCode errCode) +{ + if (bundleOption == nullptr || errCode != ERR_OK) { + return; + } + + EventInfo eventInfo; + eventInfo.notificationId = notificationId; + eventInfo.notificationLabel = label; + eventInfo.bundleName = bundleOption->GetBundleName(); + eventInfo.uid = bundleOption->GetUid(); + EventReport::SendHiSysEvent(CANCEL, eventInfo); +} + +void AdvancedNotificationService::SendRemoveHiSysEvent(int32_t notificationId, const std::string &label, + const sptr &bundleOption, ErrCode errCode) +{ + if (bundleOption == nullptr || errCode != ERR_OK) { + return; + } + + EventInfo eventInfo; + eventInfo.notificationId = notificationId; + eventInfo.notificationLabel = label; + eventInfo.bundleName = bundleOption->GetBundleName(); + eventInfo.uid = bundleOption->GetUid(); + EventReport::SendHiSysEvent(REMOVE, eventInfo); +} + +void AdvancedNotificationService::SendEnableNotificationHiSysEvent(const sptr &bundleOption, + bool enabled, ErrCode errCode) +{ + if (bundleOption == nullptr) { + return; + } + + EventInfo eventInfo; + eventInfo.bundleName = bundleOption->GetBundleName(); + eventInfo.uid = bundleOption->GetUid(); + eventInfo.enable = enabled; + if (errCode != ERR_OK) { + eventInfo.errCode = errCode; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo); + } else { + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo); + } +} + +void AdvancedNotificationService::SendEnableNotificationSlotHiSysEvent( + const sptr &bundleOption, const NotificationConstant::SlotType &slotType, + bool enabled, ErrCode errCode) +{ + if (bundleOption == nullptr) { + return; + } + + EventInfo eventInfo; + eventInfo.bundleName = bundleOption->GetBundleName(); + eventInfo.uid = bundleOption->GetUid(); + eventInfo.slotType = slotType; + eventInfo.enable = enabled; + if (errCode != ERR_OK) { + eventInfo.errCode = errCode; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo); + } else { + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo); + } +} + +void AdvancedNotificationService::SendFlowControlOccurHiSysEvent(const std::shared_ptr &record) +{ + if (record == nullptr || record->request == nullptr || record->bundleOption == nullptr) { + return; + } + + EventInfo eventInfo; + eventInfo.notificationId = record->request->GetNotificationId(); + eventInfo.bundleName = record->bundleOption->GetBundleName(); + eventInfo.uid = record->bundleOption->GetUid(); + EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo); +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/event_report.cpp b/services/ans/src/event_report.cpp new file mode 100644 index 000000000..dc1fc2857 --- /dev/null +++ b/services/ans/src/event_report.cpp @@ -0,0 +1,234 @@ +/* + * Copyright (c) 2022 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_log_wrapper.h" +#include "event_report.h" + +namespace OHOS { +namespace Notification { +namespace { +// event params +const std::string EVENT_PARAM_USER_ID = "USER_ID"; +const std::string EVENT_PARAM_BUNDLE_NAME = "BUNDLE_NAME"; +const std::string EVENT_PARAM_ERROR_CODE = "ERROR_CODE"; +const std::string EVENT_PARAM_PID = "PID"; +const std::string EVENT_PARAM_UID = "UID"; +const std::string EVENT_PARAM_ENABLE = "ENABLE"; +const std::string EVENT_PARAM_SLOT_TYPE = "SLOT_TYPE"; +const std::string EVENT_PARAM_NOTIFICATION_ID = "NOTIFICATION_ID"; +const std::string EVENT_PARAM_NOTIFICATION_LABEL = "NOTIFICATION_LABEL"; +const std::string EVENT_PARAM_CONTENT_TYPE = "CONTENT_TYPE"; +} // namespace + +void EventReport::SendHiSysEvent(const std::string &eventName, const EventInfo &eventInfo) +{ +#ifndef HAS_HISYSEVENT_PART + ANS_LOGD("Hisysevent is disabled"); +#else + auto iter = ansSysEventFuncMap_.find(eventName); + if (iter == ansSysEventFuncMap_.end()) { + return; + } + + iter->second(eventInfo); +#endif +} + +#ifdef HAS_HISYSEVENT_PART +std::unordered_map EventReport::ansSysEventFuncMap_ = { + {SUBSCRIBE_ERROR, [](const EventInfo& eventInfo) { + InnerSendSubscribeErrorEvent(eventInfo); + }}, + {ENABLE_NOTIFICATION_ERROR, [](const EventInfo& eventInfo) { + InnerSendEnableNotificationErrorEvent(eventInfo); + }}, + {ENABLE_NOTIFICATION_SLOT_ERROR, [](const EventInfo& eventInfo) { + InnerSendEnableNotificationSlotErrorEvent(eventInfo); + }}, + {PUBLISH_ERROR, [](const EventInfo& eventInfo) { + InnerSendPublishErrorEvent(eventInfo); + }}, + {FLOW_CONTROL_OCCUR, [](const EventInfo& eventInfo) { + InnerSendFlowControlOccurEvent(eventInfo); + }}, + {SUBSCRIBE, [](const EventInfo& eventInfo) { + InnerSendSubscribeEvent(eventInfo); + }}, + {UNSUBSCRIBE, [](const EventInfo& eventInfo) { + InnerSendUnSubscribeEvent(eventInfo); + }}, + {ENABLE_NOTIFICATION, [](const EventInfo& eventInfo) { + InnerSendEnableNotificationEvent(eventInfo); + }}, + {ENABLE_NOTIFICATION_SLOT, [](const EventInfo& eventInfo) { + InnerSendEnableNotificationSlotEvent(eventInfo); + }}, + {PUBLISH, [](const EventInfo& eventInfo) { + InnerSendPublishEvent(eventInfo); + }}, + {CANCEL, [](const EventInfo& eventInfo) { + InnerSendCancelEvent(eventInfo); + }}, + {REMOVE, [](const EventInfo& eventInfo) { + InnerSendRemoveEvent(eventInfo); + }}, +}; + +void EventReport::InnerSendSubscribeErrorEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + SUBSCRIBE_ERROR, + HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_PARAM_PID, eventInfo.pid, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_USER_ID, eventInfo.userId, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_ERROR_CODE, eventInfo.errCode); +} + +void EventReport::InnerSendEnableNotificationErrorEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + ENABLE_NOTIFICATION_ERROR, + HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_ENABLE, eventInfo.enable, + EVENT_PARAM_ERROR_CODE, eventInfo.errCode); +} + +void EventReport::InnerSendEnableNotificationSlotErrorEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + ENABLE_NOTIFICATION_SLOT_ERROR, + HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_SLOT_TYPE, eventInfo.slotType, + EVENT_PARAM_ENABLE, eventInfo.enable, + EVENT_PARAM_ERROR_CODE, eventInfo.errCode); +} + +void EventReport::InnerSendPublishErrorEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + PUBLISH_ERROR, + HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId, + EVENT_PARAM_CONTENT_TYPE, eventInfo.contentType, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_USER_ID, eventInfo.userId, + EVENT_PARAM_ERROR_CODE, eventInfo.errCode); +} + +void EventReport::InnerSendFlowControlOccurEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + FLOW_CONTROL_OCCUR, + HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid); +} + +void EventReport::InnerSendSubscribeEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + SUBSCRIBE, + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + EVENT_PARAM_PID, eventInfo.pid, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_USER_ID, eventInfo.userId, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName); +} + +void EventReport::InnerSendUnSubscribeEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + UNSUBSCRIBE, + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + EVENT_PARAM_PID, eventInfo.pid, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_USER_ID, eventInfo.userId, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName); +} + +void EventReport::InnerSendEnableNotificationEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + ENABLE_NOTIFICATION, + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_ENABLE, eventInfo.enable); +} + +void EventReport::InnerSendEnableNotificationSlotEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + ENABLE_NOTIFICATION_SLOT, + HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid, + EVENT_PARAM_SLOT_TYPE, eventInfo.slotType, + EVENT_PARAM_ENABLE, eventInfo.enable); +} + +void EventReport::InnerSendPublishEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + PUBLISH, + HiviewDFX::HiSysEvent::EventType::STATISTIC, + EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId, + EVENT_PARAM_CONTENT_TYPE, eventInfo.contentType, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_USER_ID, eventInfo.userId); +} + +void EventReport::InnerSendCancelEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + CANCEL, + HiviewDFX::HiSysEvent::EventType::STATISTIC, + EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId, + EVENT_PARAM_NOTIFICATION_LABEL, eventInfo.notificationLabel, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid); +} + +void EventReport::InnerSendRemoveEvent(const EventInfo &eventInfo) +{ + InnerEventWrite( + REMOVE, + HiviewDFX::HiSysEvent::EventType::STATISTIC, + EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId, + EVENT_PARAM_NOTIFICATION_LABEL, eventInfo.notificationLabel, + EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName, + EVENT_PARAM_UID, eventInfo.uid); +} + +template +void EventReport::InnerEventWrite(const std::string &eventName, + HiviewDFX::HiSysEvent::EventType type, Types... keyValues) +{ + HiviewDFX::HiSysEvent::Write( + HiviewDFX::HiSysEvent::Domain::NOTIFICATION, + eventName, + static_cast(type), + keyValues...); +} +#endif +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 981ba23b8..30bf6e192 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -21,6 +21,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "bundle_manager_helper.h" +#include "hitrace_meter.h" #include "nlohmann/json.hpp" #include "os_account_manager.h" @@ -43,6 +44,7 @@ NotificationPreferences &NotificationPreferences::GetInstance() ErrCode NotificationPreferences::AddNotificationSlots( const sptr &bundleOption, const std::vector> &slots) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (bundleOption == nullptr || bundleOption->GetBundleName().empty() || slots.empty()) { return ERR_ANS_INVALID_PARAM; @@ -117,6 +119,7 @@ ErrCode NotificationPreferences::AddNotificationBundleProperty(const sptr &bundleOption, const NotificationConstant::SlotType &slotType) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { return ERR_ANS_INVALID_PARAM; @@ -483,6 +486,7 @@ ErrCode NotificationPreferences::GetNotificationsEnabledForBundle( ErrCode NotificationPreferences::SetNotificationsEnabledForBundle( const sptr &bundleOption, const bool enabled) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { return ERR_ANS_INVALID_PARAM; } diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index a0e6e93a4..f98f9d5df 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -17,6 +17,7 @@ #include "ans_const_define.h" #include "ans_log_wrapper.h" +#include "hitrace_meter.h" #include "os_account_manager.h" #include "uri.h" @@ -185,6 +186,7 @@ bool NotificationPreferencesDatabase::CheckKvStore() bool NotificationPreferencesDatabase::PutSlotsToDisturbeDB( const std::string &bundleName, const int32_t &bundleUid, const std::vector> &slots) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (bundleName.empty()) { ANS_LOGE("Bundle name is null."); @@ -618,6 +620,7 @@ bool NotificationPreferencesDatabase::RemoveBundleFromDisturbeDB(const std::stri bool NotificationPreferencesDatabase::RemoveSlotFromDisturbeDB( const std::string &bundleKey, const NotificationConstant::SlotType &type) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s", __FUNCTION__); if (bundleKey.empty()) { ANS_LOGE("Bundle name is null."); diff --git a/services/ans/src/notification_subscriber_manager.cpp b/services/ans/src/notification_subscriber_manager.cpp index ff144a621..cf35d5600 100644 --- a/services/ans/src/notification_subscriber_manager.cpp +++ b/services/ans/src/notification_subscriber_manager.cpp @@ -23,6 +23,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "ans_watchdog.h" +#include "hitrace_meter.h" #include "ipc_skeleton.h" #include "os_account_manager.h" #include "remote_death_recipient.h" @@ -53,6 +54,7 @@ NotificationSubscriberManager::~NotificationSubscriberManager() ErrCode NotificationSubscriberManager::AddSubscriber( const sptr &subscriber, const sptr &subscribeInfo) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (subscriber == nullptr) { ANS_LOGE("subscriber is null."); return ERR_ANS_INVALID_PARAM; @@ -91,6 +93,7 @@ ErrCode NotificationSubscriberManager::AddSubscriber( ErrCode NotificationSubscriberManager::RemoveSubscriber( const sptr &subscriber, const sptr &subscribeInfo) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (subscriber == nullptr) { ANS_LOGE("subscriber is null."); return ERR_ANS_INVALID_PARAM; @@ -107,6 +110,7 @@ ErrCode NotificationSubscriberManager::RemoveSubscriber( void NotificationSubscriberManager::NotifyConsumed( const sptr ¬ification, const sptr ¬ificationMap) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (handler_ == nullptr) { ANS_LOGE("handler is nullptr"); return; @@ -120,6 +124,7 @@ void NotificationSubscriberManager::NotifyConsumed( void NotificationSubscriberManager::NotifyCanceled( const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (handler_ == nullptr) { ANS_LOGE("handler is nullptr"); return; @@ -160,6 +165,7 @@ void NotificationSubscriberManager::NotifyDoNotDisturbDateChanged(const sptr &callbackData) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); if (handler_ == nullptr) { ANS_LOGE("handler is nullptr"); return; @@ -257,6 +263,7 @@ void NotificationSubscriberManager::RemoveRecordInfo( ErrCode NotificationSubscriberManager::AddSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); std::shared_ptr record = FindSubscriberRecord(subscriber); if (record == nullptr) { record = CreateSubscriberRecord(subscriber); @@ -280,6 +287,7 @@ ErrCode NotificationSubscriberManager::AddSubscriberInner( ErrCode NotificationSubscriberManager::RemoveSubscriberInner( const sptr &subscriber, const sptr &subscribeInfo) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); std::shared_ptr record = FindSubscriberRecord(subscriber); if (record == nullptr) { @@ -304,6 +312,7 @@ ErrCode NotificationSubscriberManager::RemoveSubscriberInner( void NotificationSubscriberManager::NotifyConsumedInner( const sptr ¬ification, const sptr ¬ificationMap) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId()); int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); int32_t sendUserId = notification->GetUserId(); @@ -327,6 +336,7 @@ void NotificationSubscriberManager::NotifyConsumedInner( void NotificationSubscriberManager::NotifyCanceledInner( const sptr ¬ification, const sptr ¬ificationMap, int32_t deleteReason) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGD("%{public}s notification->GetUserId <%{public}d>", __FUNCTION__, notification->GetUserId()); int32_t recvUserId = notification->GetNotificationRequest().GetReceiverUserId(); int32_t sendUserId = notification->GetUserId(); diff --git a/services/ans/src/reminder_timer_info.cpp b/services/ans/src/reminder_timer_info.cpp index 5a1993584..57ca75750 100644 --- a/services/ans/src/reminder_timer_info.cpp +++ b/services/ans/src/reminder_timer_info.cpp @@ -17,7 +17,9 @@ #include "ans_log_wrapper.h" #include "common_event_manager.h" +#ifdef HAS_HISYSEVENT_PART #include "hisysevent.h" +#endif #include "reminder_request.h" using namespace OHOS::EventFwk; @@ -48,9 +50,11 @@ void ReminderTimerInfo::OnTrigger() { ANSR_LOGI("Timing is arrivelled."); if (action_ == ReminderRequest::REMINDER_EVENT_ALARM_ALERT) { +#ifdef HAS_HISYSEVENT_PART std::string eventType = "ALARM_TRIGGER"; HiSysEvent::Write(HiSysEvent::Domain::NOTIFICATION, eventType, HiSysEvent::EventType::STATISTIC, "UID", uid_, "PID", pid_, "NAME", bundleName_); +#endif } } } diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index da643bad8..e2c5fbb79 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -33,6 +33,7 @@ ohos_unittest("ans_unit_test") { sources = [ "${services_path}/ans/src/advanced_notification_service.cpp", "${services_path}/ans/src/advanced_notification_service_ability.cpp", + "${services_path}/ans/src/event_report.cpp", "${services_path}/ans/src/notification_preferences.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", @@ -52,6 +53,7 @@ ohos_unittest("ans_unit_test") { "mock/mock_event_handler.cpp", "mock/mock_ipc.cpp", "mock/mock_single_kv_store.cpp", + "notification_hisysevent_test.cpp", "notification_preferences_database_test.cpp", "notification_preferences_test.cpp", "notification_slot_filter_test.cpp", @@ -88,6 +90,7 @@ ohos_unittest("ans_unit_test") { "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/ans/test/unittest/notification_hisysevent_test.cpp b/services/ans/test/unittest/notification_hisysevent_test.cpp new file mode 100644 index 000000000..829e0e6a4 --- /dev/null +++ b/services/ans/test/unittest/notification_hisysevent_test.cpp @@ -0,0 +1,325 @@ +/* + * Copyright (c) 2022 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 +#include +#include "event_report.h" +#include "notification_constant.h" +#include "notification_content.h" + +namespace OHOS { +namespace Notification { +using namespace testing::ext; + +namespace { +const std::string TEST_CREATER_BUNDLE_NAME = "creater"; +const std::string TEST_BUNDLE_OPTION_BUNDLE_NAME = "bundleName"; +const std::string TEST_NOTIFICATION_LABEL = "notificationLabel"; +constexpr int32_t TEST_NOTIFICATION_ID = 1; +constexpr int32_t TEST_BUNDLE_OPTION_UID = 100; +constexpr int32_t TEST_USER_ID = 1000; +constexpr int32_t TEST_ERROR_CODE = 22; +} // namespace + +class NotificationHisyseventTest : public testing::Test { +public: + NotificationHisyseventTest() + {} + ~NotificationHisyseventTest() + {} + + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void NotificationHisyseventTest::SetUpTestCase(void) +{} + +void NotificationHisyseventTest::TearDownTestCase(void) +{} + +void NotificationHisyseventTest::SetUp(void) +{} + +void NotificationHisyseventTest::TearDown(void) +{} + +/** + * @tc.name: SendSubscriberErrorSysEvent_0100 + * @tc.desc: Send "SUBSCRIBE_ERROR" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendSubscriberErrorSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendSubscriberErrorSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.pid = getpid(); + eventInfo.uid = getuid(); + eventInfo.errCode = TEST_ERROR_CODE; + EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo); + + eventInfo.userId = TEST_USER_ID; + std::vector appNames = {"app1_1", "app1_2", "app1_3"}; + eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""), + [appNames](std::string bundleName, const std::string &str) { + return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo); + + GTEST_LOG_(INFO) << "SendSubscriberErrorSysEvent_0100 end"; +} + +/** + * @tc.name: SendEnableNotificationErrorSysEvent_0100 + * @tc.desc: Send "ENABLE_NOTIFICATION_ERROR" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendEnableNotificationErrorSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendEnableNotificationErrorSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = getuid(); + eventInfo.enable = true; + eventInfo.errCode = TEST_ERROR_CODE; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo); + + GTEST_LOG_(INFO) << "SendEnableNotificationErrorSysEvent_0100 end"; +} + +/** + * @tc.name: SendEnableNotificationSlotErrorSysEvent_0100 + * @tc.desc: Send "ENABLE_NOTIFICATION_SLOT_ERROR" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSlotErrorSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendEnableNotificationSlotErrorSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = getuid(); + eventInfo.enable = false; + eventInfo.slotType = NotificationConstant::SERVICE_REMINDER; + eventInfo.errCode = TEST_ERROR_CODE; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo); + + GTEST_LOG_(INFO) << "SendEnableNotificationSlotErrorSysEvent_0100 end"; +} + +/** + * @tc.name: SendPublishErrorSysEvent_0100 + * @tc.desc: Send "SUBSCRIBE_ERROR" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendPublishErrorSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendPublishErrorSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.notificationId = TEST_NOTIFICATION_ID; + eventInfo.contentType = static_cast(NotificationContent::Type::LONG_TEXT); + eventInfo.bundleName = TEST_CREATER_BUNDLE_NAME; + eventInfo.userId = TEST_USER_ID; + eventInfo.errCode = TEST_ERROR_CODE; + EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo); + + GTEST_LOG_(INFO) << "SendPublishErrorSysEvent_0100 end"; +} + +/** + * @tc.name: SendFlowControlOccurSysEvent_0100 + * @tc.desc: Send "FLOW_CONTROL_OCCUR" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendFlowControlOccurSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendFlowControlOccurSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.notificationId = TEST_NOTIFICATION_ID; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = TEST_BUNDLE_OPTION_UID; + EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo); + + GTEST_LOG_(INFO) << "SendFlowControlOccurSysEvent_0100 end"; +} + +/** + * @tc.name: SendSubscribeSysEvent_0100 + * @tc.desc: Send "SUBSCRIBE" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendSubscribeSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.pid = getpid(); + eventInfo.uid = getuid(); + EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo); + + eventInfo.userId = TEST_USER_ID; + std::vector appNames = {"app1_1", "app1_2", "app1_3"}; + eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""), + [appNames](std::string bundleName, const std::string &str) { + return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo); + + std::vector anotherBundle = {"app"}; + eventInfo.bundleName = std::accumulate(anotherBundle.begin(), anotherBundle.end(), std::string(""), + [anotherBundle](std::string bundleName, const std::string &str) { + return (str == anotherBundle.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo); + + GTEST_LOG_(INFO) << "SendSubscribeSysEvent_0100 end"; +} + +/** + * @tc.name: SendUnSubscribeSysEvent_0100 + * @tc.desc: Send "UNSUBSCRIBE" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendUnSubscribeSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.pid = getpid(); + eventInfo.uid = getuid(); + eventInfo.userId = TEST_USER_ID; + std::vector appNames = {"app1_1", "app1_2", "app1_3"}; + eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""), + [appNames](std::string bundleName, const std::string &str) { + return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str); + }); + EventReport::SendHiSysEvent(UNSUBSCRIBE, eventInfo); + + GTEST_LOG_(INFO) << "SendUnSubscribeSysEvent_0100 end"; +} + +/** + * @tc.name: SendEnableNotificationSysEvent_0100 + * @tc.desc: Send "ENABLE_NOTIFICATION" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendEnableNotificationSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = getuid(); + eventInfo.enable = true; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo); + + GTEST_LOG_(INFO) << "SendEnableNotificationSysEvent_0100 end"; +} + +/** + * @tc.name: SendEnableNotificationSlotSysEvent_0100 + * @tc.desc: Send "ENABLE_NOTIFICATION_SLOT" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendEnableNotificationSlotSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendEnableNotificationSlotSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = getuid(); + eventInfo.enable = true; + eventInfo.slotType = NotificationConstant::CONTENT_INFORMATION; + EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo); + + GTEST_LOG_(INFO) << "SendEnableNotificationSlotSysEvent_0100 end"; +} + +/** + * @tc.name: SendPublishSysEvent_0100 + * @tc.desc: Send "PUBLISH" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendPublishSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.notificationId = TEST_NOTIFICATION_ID; + eventInfo.contentType = static_cast(NotificationContent::Type::LONG_TEXT); + eventInfo.bundleName = TEST_CREATER_BUNDLE_NAME; + eventInfo.userId = TEST_USER_ID; + EventReport::SendHiSysEvent(PUBLISH, eventInfo); + + GTEST_LOG_(INFO) << "SendPublishSysEvent_0100 end"; +} + +/** + * @tc.name: SendCancelSysEvent_0100 + * @tc.desc: Send "CANCEL" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendCancelSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendCancelSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.notificationId = TEST_NOTIFICATION_ID; + eventInfo.notificationLabel = TEST_NOTIFICATION_LABEL; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = TEST_BUNDLE_OPTION_UID; + EventReport::SendHiSysEvent(CANCEL, eventInfo); + + GTEST_LOG_(INFO) << "SendCancelSysEvent_0100 end"; +} + +/** + * @tc.name: SendRemoveSysEvent_0100 + * @tc.desc: Send "REMOVE" hisysevent. + * @tc.type: FUNC + * @tc.require: I582Y4 + */ +HWTEST_F(NotificationHisyseventTest, SendRemoveSysEvent_0100, Level1) +{ + GTEST_LOG_(INFO) << "SendRemoveSysEvent_0100 start"; + + EventInfo eventInfo; + eventInfo.notificationId = TEST_NOTIFICATION_ID; + eventInfo.notificationLabel = TEST_NOTIFICATION_LABEL; + eventInfo.bundleName = TEST_BUNDLE_OPTION_BUNDLE_NAME; + eventInfo.uid = TEST_BUNDLE_OPTION_UID; + EventReport::SendHiSysEvent(REMOVE, eventInfo); + + GTEST_LOG_(INFO) << "SendRemoveSysEvent_0100 end"; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/BUILD.gn b/services/distributed/BUILD.gn index 10b198771..cceac28cc 100644 --- a/services/distributed/BUILD.gn +++ b/services/distributed/BUILD.gn @@ -49,6 +49,7 @@ ohos_shared_library("libans_distributed") { "ability_base:zuri", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "multimedia_image_standard:image_native", diff --git a/services/distributed/src/distributed_notification_manager.cpp b/services/distributed/src/distributed_notification_manager.cpp index 26fb43a8d..6b6590c01 100644 --- a/services/distributed/src/distributed_notification_manager.cpp +++ b/services/distributed/src/distributed_notification_manager.cpp @@ -20,6 +20,7 @@ #include "ans_inner_errors.h" #include "ans_log_wrapper.h" #include "ans_watchdog.h" +#include "hitrace_meter.h" namespace OHOS { namespace Notification { @@ -276,6 +277,7 @@ bool DistributedNotificationManager::DeleteCallback( ErrCode DistributedNotificationManager::Publish( const std::string &bundleName, const std::string &label, int32_t id, const sptr &request) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGI("%{public}s start", __FUNCTION__); std::string key; if (!GenerateLocalDistributedKey(bundleName, label, id, key)) { @@ -322,6 +324,7 @@ ErrCode DistributedNotificationManager::Update( ErrCode DistributedNotificationManager::Delete(const std::string &bundleName, const std::string &label, int32_t id) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGI("%{public}s start", __FUNCTION__); std::string key; if (!GenerateLocalDistributedKey(bundleName, label, id, key)) { @@ -339,6 +342,7 @@ ErrCode DistributedNotificationManager::Delete(const std::string &bundleName, co ErrCode DistributedNotificationManager::DeleteRemoteNotification( const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id) { + HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); ANS_LOGI("%{public}s start", __FUNCTION__); std::string key; diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index 531594720..b8157b5ef 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -71,6 +71,7 @@ ohos_unittest("ans_distributed_unit_test") { "bundle_framework:appexecfwk_base", "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index a2f6fa252..4e9e45c7e 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -30,6 +30,7 @@ ohos_moduletest("ans_module_test") { sources = [ "${services_path}/ans/src/advanced_notification_service.cpp", "${services_path}/ans/src/advanced_notification_service_ability.cpp", + "${services_path}/ans/src/event_report.cpp", "${services_path}/ans/src/notification_preferences.cpp", "${services_path}/ans/src/notification_preferences_database.cpp", "${services_path}/ans/src/notification_preferences_info.cpp", @@ -74,6 +75,7 @@ ohos_moduletest("ans_module_test") { "distributeddatamgr:distributeddata_inner", "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", + "hitrace_native:hitrace_meter", "hitrace_native:libhitrace", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", -- Gitee