1 /*
2  * Copyright (c) 2022 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 "ans_log_wrapper.h"
17 #include "event_report.h"
18 
19 namespace OHOS {
20 namespace Notification {
21 namespace {
22 // event params
23 const std::string EVENT_PARAM_USER_ID = "USER_ID";
24 const std::string EVENT_PARAM_BUNDLE_NAME = "BUNDLE_NAME";
25 const std::string EVENT_PARAM_ERROR_CODE = "ERROR_CODE";
26 const std::string EVENT_PARAM_PID = "PID";
27 const std::string EVENT_PARAM_UID = "UID";
28 const std::string EVENT_PARAM_ENABLE = "ENABLE";
29 const std::string EVENT_PARAM_SLOT_TYPE = "SLOT_TYPE";
30 const std::string EVENT_PARAM_NOTIFICATION_ID = "NOTIFICATION_ID";
31 const std::string EVENT_PARAM_NOTIFICATION_LABEL = "NOTIFICATION_LABEL";
32 const std::string EVENT_PARAM_CONTENT_TYPE = "CONTENT_TYPE";
33 const std::string EVENT_PARAM_OPERATE_FLAG = "OPERATE_FLAG";
34 } // namespace
35 
SendHiSysEvent(const std::string & eventName,const EventInfo & eventInfo)36 void EventReport::SendHiSysEvent(const std::string &eventName, const EventInfo &eventInfo)
37 {
38 #ifndef HAS_HISYSEVENT_PART
39     ANS_LOGD("Hisysevent is disabled");
40 #else
41     auto iter = ansSysEventFuncMap_.find(eventName);
42     if (iter == ansSysEventFuncMap_.end()) {
43         return;
44     }
45 
46     iter->second(eventInfo);
47 #endif
48 }
49 
50 #ifdef HAS_HISYSEVENT_PART
51 std::unordered_map<std::string, void (*)(const EventInfo& eventInfo)> EventReport::ansSysEventFuncMap_ = {
__anonf0fe24120202() 52     {SUBSCRIBE_ERROR, [](const EventInfo& eventInfo) {
53         InnerSendSubscribeErrorEvent(eventInfo);
54     }},
__anonf0fe24120302() 55     {ENABLE_NOTIFICATION_ERROR, [](const EventInfo& eventInfo) {
56         InnerSendEnableNotificationErrorEvent(eventInfo);
57     }},
__anonf0fe24120402() 58     {ENABLE_NOTIFICATION_SLOT_ERROR, [](const EventInfo& eventInfo) {
59         InnerSendEnableNotificationSlotErrorEvent(eventInfo);
60     }},
__anonf0fe24120502() 61     {PUBLISH_ERROR, [](const EventInfo& eventInfo) {
62         InnerSendPublishErrorEvent(eventInfo);
63     }},
__anonf0fe24120602() 64     {FLOW_CONTROL_OCCUR, [](const EventInfo& eventInfo) {
65         InnerSendFlowControlOccurEvent(eventInfo);
66     }},
__anonf0fe24120702() 67     {SUBSCRIBE, [](const EventInfo& eventInfo) {
68         InnerSendSubscribeEvent(eventInfo);
69     }},
__anonf0fe24120802() 70     {UNSUBSCRIBE, [](const EventInfo& eventInfo) {
71         InnerSendUnSubscribeEvent(eventInfo);
72     }},
__anonf0fe24120902() 73     {ENABLE_NOTIFICATION, [](const EventInfo& eventInfo) {
74         InnerSendEnableNotificationEvent(eventInfo);
75     }},
__anonf0fe24120a02() 76     {ENABLE_NOTIFICATION_SLOT, [](const EventInfo& eventInfo) {
77         InnerSendEnableNotificationSlotEvent(eventInfo);
78     }},
__anonf0fe24120b02() 79     {PUBLISH, [](const EventInfo& eventInfo) {
80         InnerSendPublishEvent(eventInfo);
81     }},
__anonf0fe24120c02() 82     {CANCEL, [](const EventInfo& eventInfo) {
83         InnerSendCancelEvent(eventInfo);
84     }},
__anonf0fe24120d02() 85     {REMOVE, [](const EventInfo& eventInfo) {
86         InnerSendRemoveEvent(eventInfo);
87     }},
__anonf0fe24120e02() 88     {STATIC_LIVE_VIEW_UPLOAD, [](const EventInfo& eventInfo) {
89         InnerSendLiveviewUploadEvent(eventInfo);
90     }},
91 };
92 
InnerSendSubscribeErrorEvent(const EventInfo & eventInfo)93 void EventReport::InnerSendSubscribeErrorEvent(const EventInfo &eventInfo)
94 {
95     InnerEventWrite(
96         SUBSCRIBE_ERROR,
97         HiviewDFX::HiSysEvent::EventType::FAULT,
98         EVENT_PARAM_PID, eventInfo.pid,
99         EVENT_PARAM_UID, eventInfo.uid,
100         EVENT_PARAM_USER_ID, eventInfo.userId,
101         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
102         EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
103 }
104 
InnerSendEnableNotificationErrorEvent(const EventInfo & eventInfo)105 void EventReport::InnerSendEnableNotificationErrorEvent(const EventInfo &eventInfo)
106 {
107     InnerEventWrite(
108         ENABLE_NOTIFICATION_ERROR,
109         HiviewDFX::HiSysEvent::EventType::FAULT,
110         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
111         EVENT_PARAM_UID, eventInfo.uid,
112         EVENT_PARAM_ENABLE, eventInfo.enable,
113         EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
114 }
115 
InnerSendEnableNotificationSlotErrorEvent(const EventInfo & eventInfo)116 void EventReport::InnerSendEnableNotificationSlotErrorEvent(const EventInfo &eventInfo)
117 {
118     InnerEventWrite(
119         ENABLE_NOTIFICATION_SLOT_ERROR,
120         HiviewDFX::HiSysEvent::EventType::FAULT,
121         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
122         EVENT_PARAM_UID, eventInfo.uid,
123         EVENT_PARAM_SLOT_TYPE, eventInfo.slotType,
124         EVENT_PARAM_ENABLE, eventInfo.enable,
125         EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
126 }
127 
InnerSendPublishErrorEvent(const EventInfo & eventInfo)128 void EventReport::InnerSendPublishErrorEvent(const EventInfo &eventInfo)
129 {
130     InnerEventWrite(
131         PUBLISH_ERROR,
132         HiviewDFX::HiSysEvent::EventType::FAULT,
133         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
134         EVENT_PARAM_CONTENT_TYPE, eventInfo.contentType,
135         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
136         EVENT_PARAM_USER_ID, eventInfo.userId,
137         EVENT_PARAM_ERROR_CODE, eventInfo.errCode);
138 }
139 
InnerSendFlowControlOccurEvent(const EventInfo & eventInfo)140 void EventReport::InnerSendFlowControlOccurEvent(const EventInfo &eventInfo)
141 {
142     InnerEventWrite(
143         FLOW_CONTROL_OCCUR,
144         HiviewDFX::HiSysEvent::EventType::FAULT,
145         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
146         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
147         EVENT_PARAM_UID, eventInfo.uid);
148 }
149 
InnerSendSubscribeEvent(const EventInfo & eventInfo)150 void EventReport::InnerSendSubscribeEvent(const EventInfo &eventInfo)
151 {
152     InnerEventWrite(
153         SUBSCRIBE,
154         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
155         EVENT_PARAM_PID, eventInfo.pid,
156         EVENT_PARAM_UID, eventInfo.uid,
157         EVENT_PARAM_USER_ID, eventInfo.userId,
158         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName);
159 }
160 
InnerSendUnSubscribeEvent(const EventInfo & eventInfo)161 void EventReport::InnerSendUnSubscribeEvent(const EventInfo &eventInfo)
162 {
163     InnerEventWrite(
164         UNSUBSCRIBE,
165         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
166         EVENT_PARAM_PID, eventInfo.pid,
167         EVENT_PARAM_UID, eventInfo.uid,
168         EVENT_PARAM_USER_ID, eventInfo.userId,
169         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName);
170 }
171 
InnerSendEnableNotificationEvent(const EventInfo & eventInfo)172 void EventReport::InnerSendEnableNotificationEvent(const EventInfo &eventInfo)
173 {
174     InnerEventWrite(
175         ENABLE_NOTIFICATION,
176         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
177         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
178         EVENT_PARAM_UID, eventInfo.uid,
179         EVENT_PARAM_ENABLE, eventInfo.enable);
180 }
181 
InnerSendEnableNotificationSlotEvent(const EventInfo & eventInfo)182 void EventReport::InnerSendEnableNotificationSlotEvent(const EventInfo &eventInfo)
183 {
184     InnerEventWrite(
185         ENABLE_NOTIFICATION_SLOT,
186         HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
187         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
188         EVENT_PARAM_UID, eventInfo.uid,
189         EVENT_PARAM_SLOT_TYPE, eventInfo.slotType,
190         EVENT_PARAM_ENABLE, eventInfo.enable);
191 }
192 
InnerSendLiveviewUploadEvent(const EventInfo & eventInfo)193 void EventReport::InnerSendLiveviewUploadEvent(const EventInfo &eventInfo)
194 {
195     InnerEventWrite(
196         STATIC_LIVE_VIEW_UPLOAD,
197         HiviewDFX::HiSysEvent::EventType::STATISTIC,
198         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
199         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
200         EVENT_PARAM_CONTENT_TYPE, eventInfo.contentType,
201         EVENT_PARAM_OPERATE_FLAG, eventInfo.operateFlag);
202 }
203 
InnerSendPublishEvent(const EventInfo & eventInfo)204 void EventReport::InnerSendPublishEvent(const EventInfo &eventInfo)
205 {
206     InnerEventWrite(
207         PUBLISH,
208         HiviewDFX::HiSysEvent::EventType::STATISTIC,
209         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
210         EVENT_PARAM_CONTENT_TYPE, eventInfo.contentType,
211         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
212         EVENT_PARAM_USER_ID, eventInfo.userId);
213 }
214 
InnerSendCancelEvent(const EventInfo & eventInfo)215 void EventReport::InnerSendCancelEvent(const EventInfo &eventInfo)
216 {
217     InnerEventWrite(
218         CANCEL,
219         HiviewDFX::HiSysEvent::EventType::STATISTIC,
220         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
221         EVENT_PARAM_NOTIFICATION_LABEL, eventInfo.notificationLabel,
222         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
223         EVENT_PARAM_UID, eventInfo.uid);
224 }
225 
InnerSendRemoveEvent(const EventInfo & eventInfo)226 void EventReport::InnerSendRemoveEvent(const EventInfo &eventInfo)
227 {
228     InnerEventWrite(
229         REMOVE,
230         HiviewDFX::HiSysEvent::EventType::STATISTIC,
231         EVENT_PARAM_NOTIFICATION_ID, eventInfo.notificationId,
232         EVENT_PARAM_NOTIFICATION_LABEL, eventInfo.notificationLabel,
233         EVENT_PARAM_BUNDLE_NAME, eventInfo.bundleName,
234         EVENT_PARAM_UID, eventInfo.uid);
235 }
236 
237 template<typename... Types>
InnerEventWrite(const std::string & eventName,HiviewDFX::HiSysEvent::EventType type,Types...keyValues)238 void EventReport::InnerEventWrite(const std::string &eventName,
239     HiviewDFX::HiSysEvent::EventType type, Types... keyValues)
240 {
241     HiSysEventWrite(
242         HiviewDFX::HiSysEvent::Domain::NOTIFICATION,
243         eventName,
244         static_cast<HiviewDFX::HiSysEvent::EventType>(type),
245         keyValues...);
246 }
247 #endif
248 } // namespace Notification
249 } // namespace OHOS