1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "advanced_notification_service.h"
17
18 #include <functional>
19 #include <iomanip>
20 #include <sstream>
21
22 #include "access_token_helper.h"
23 #include "ans_inner_errors.h"
24 #include "ans_log_wrapper.h"
25 #include "ans_permission_def.h"
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28 #include "event_report.h"
29 #include "errors.h"
30 #include "common_event_manager.h"
31 #include "common_event_support.h"
32 #include "hitrace_meter_adapter.h"
33 #include "ipc_skeleton.h"
34
35 #include "advanced_notification_inline.cpp"
36
37 namespace OHOS {
38 namespace Notification {
SendSubscribeHiSysEvent(int32_t pid,int32_t uid,const sptr<NotificationSubscribeInfo> & info,ErrCode errCode)39 void AdvancedNotificationService::SendSubscribeHiSysEvent(int32_t pid, int32_t uid,
40 const sptr<NotificationSubscribeInfo> &info, ErrCode errCode)
41 {
42 EventInfo eventInfo;
43 eventInfo.pid = pid;
44 eventInfo.uid = uid;
45 if (info != nullptr) {
46 ANS_LOGD("info is not nullptr.");
47 eventInfo.userId = info->GetAppUserId();
48 std::vector<std::string> appNames = info->GetAppNames();
49 eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
50 [appNames](const std::string &bundleName, const std::string &str) {
51 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
52 });
53 }
54
55 if (errCode != ERR_OK) {
56 eventInfo.errCode = errCode;
57 EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo);
58 } else {
59 EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo);
60 }
61 }
62
SendUnSubscribeHiSysEvent(int32_t pid,int32_t uid,const sptr<NotificationSubscribeInfo> & info)63 void AdvancedNotificationService::SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid,
64 const sptr<NotificationSubscribeInfo> &info)
65 {
66 EventInfo eventInfo;
67 eventInfo.pid = pid;
68 eventInfo.uid = uid;
69 if (info != nullptr) {
70 eventInfo.userId = info->GetAppUserId();
71 std::vector<std::string> appNames = info->GetAppNames();
72 eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
73 [appNames](const std::string &bundleName, const std::string &str) {
74 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
75 });
76 }
77
78 EventReport::SendHiSysEvent(UNSUBSCRIBE, eventInfo);
79 }
80
SendPublishHiSysEvent(const sptr<NotificationRequest> & request,ErrCode errCode)81 void AdvancedNotificationService::SendPublishHiSysEvent(const sptr<NotificationRequest> &request, ErrCode errCode)
82 {
83 if (request == nullptr) {
84 return;
85 }
86
87 EventInfo eventInfo;
88 eventInfo.notificationId = request->GetNotificationId();
89 eventInfo.contentType = static_cast<int32_t>(request->GetNotificationType());
90 eventInfo.bundleName = request->GetCreatorBundleName();
91 eventInfo.userId = request->GetCreatorUserId();
92 if (errCode != ERR_OK) {
93 eventInfo.errCode = errCode;
94 EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo);
95 } else {
96 EventReport::SendHiSysEvent(PUBLISH, eventInfo);
97 }
98 }
99
SendCancelHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)100 void AdvancedNotificationService::SendCancelHiSysEvent(int32_t notificationId, const std::string &label,
101 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
102 {
103 if (bundleOption == nullptr || errCode != ERR_OK) {
104 ANS_LOGD("bundleOption is nullptr.");
105 return;
106 }
107
108 EventInfo eventInfo;
109 eventInfo.notificationId = notificationId;
110 eventInfo.notificationLabel = label;
111 eventInfo.bundleName = bundleOption->GetBundleName();
112 eventInfo.uid = bundleOption->GetUid();
113 EventReport::SendHiSysEvent(CANCEL, eventInfo);
114 }
115
SendRemoveHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)116 void AdvancedNotificationService::SendRemoveHiSysEvent(int32_t notificationId, const std::string &label,
117 const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
118 {
119 if (bundleOption == nullptr || errCode != ERR_OK) {
120 return;
121 }
122
123 EventInfo eventInfo;
124 eventInfo.notificationId = notificationId;
125 eventInfo.notificationLabel = label;
126 eventInfo.bundleName = bundleOption->GetBundleName();
127 eventInfo.uid = bundleOption->GetUid();
128 EventReport::SendHiSysEvent(REMOVE, eventInfo);
129 }
130
SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,bool enabled,ErrCode errCode)131 void AdvancedNotificationService::SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption,
132 bool enabled, ErrCode errCode)
133 {
134 if (bundleOption == nullptr) {
135 return;
136 }
137
138 EventInfo eventInfo;
139 eventInfo.bundleName = bundleOption->GetBundleName();
140 eventInfo.uid = bundleOption->GetUid();
141 eventInfo.enable = enabled;
142 if (errCode != ERR_OK) {
143 eventInfo.errCode = errCode;
144 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo);
145 } else {
146 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo);
147 }
148 }
149
SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,ErrCode errCode)150 void AdvancedNotificationService::SendEnableNotificationSlotHiSysEvent(
151 const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType,
152 bool enabled, ErrCode errCode)
153 {
154 if (bundleOption == nullptr) {
155 return;
156 }
157
158 EventInfo eventInfo;
159 eventInfo.bundleName = bundleOption->GetBundleName();
160 eventInfo.uid = bundleOption->GetUid();
161 eventInfo.slotType = slotType;
162 eventInfo.enable = enabled;
163 if (errCode != ERR_OK) {
164 eventInfo.errCode = errCode;
165 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo);
166 } else {
167 EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo);
168 }
169 }
170
SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> & record)171 void AdvancedNotificationService::SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record)
172 {
173 if (record == nullptr || record->request == nullptr || record->bundleOption == nullptr) {
174 return;
175 }
176
177 EventInfo eventInfo;
178 eventInfo.notificationId = record->request->GetNotificationId();
179 eventInfo.bundleName = record->bundleOption->GetBundleName();
180 eventInfo.uid = record->bundleOption->GetUid();
181 EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo);
182 }
183 } // namespace Notification
184 } // namespace OHOS
185