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 <cstdint>
19 #include <functional>
20 #include <iomanip>
21 #include <sstream>
22 
23 #include "access_token_helper.h"
24 #include "ans_inner_errors.h"
25 #include "ans_log_wrapper.h"
26 #include "ans_permission_def.h"
27 #include "common_event_manager.h"
28 #include "common_event_support.h"
29 #include "event_report.h"
30 #include "errors.h"
31 #include "common_event_manager.h"
32 #include "common_event_support.h"
33 #include "hitrace_meter_adapter.h"
34 #include "ipc_skeleton.h"
35 
36 #include "advanced_notification_inline.cpp"
37 
38 namespace OHOS {
39 namespace Notification {
SendSubscribeHiSysEvent(int32_t pid,int32_t uid,const sptr<NotificationSubscribeInfo> & info,ErrCode errCode)40 void AdvancedNotificationService::SendSubscribeHiSysEvent(int32_t pid, int32_t uid,
41     const sptr<NotificationSubscribeInfo> &info, ErrCode errCode)
42 {
43     EventInfo eventInfo;
44     eventInfo.pid = pid;
45     eventInfo.uid = uid;
46     if (info != nullptr) {
47         ANS_LOGD("info is not nullptr.");
48         eventInfo.userId = info->GetAppUserId();
49         std::vector<std::string> appNames = info->GetAppNames();
50         eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
51             [appNames](const std::string &bundleName, const std::string &str) {
52                 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
53             });
54     }
55 
56     if (errCode != ERR_OK) {
57         eventInfo.errCode = errCode;
58         EventReport::SendHiSysEvent(SUBSCRIBE_ERROR, eventInfo);
59     } else {
60         EventReport::SendHiSysEvent(SUBSCRIBE, eventInfo);
61     }
62 }
63 
SendUnSubscribeHiSysEvent(int32_t pid,int32_t uid,const sptr<NotificationSubscribeInfo> & info)64 void AdvancedNotificationService::SendUnSubscribeHiSysEvent(int32_t pid, int32_t uid,
65     const sptr<NotificationSubscribeInfo> &info)
66 {
67     EventInfo eventInfo;
68     eventInfo.pid = pid;
69     eventInfo.uid = uid;
70     if (info != nullptr) {
71         eventInfo.userId = info->GetAppUserId();
72         std::vector<std::string> appNames = info->GetAppNames();
73         eventInfo.bundleName = std::accumulate(appNames.begin(), appNames.end(), std::string(""),
74             [appNames](const std::string &bundleName, const std::string &str) {
75                 return (str == appNames.front()) ? (bundleName + str) : (bundleName + "," + str);
76             });
77     }
78 
79     EventReport::SendHiSysEvent(UNSUBSCRIBE, eventInfo);
80 }
81 
SendPublishHiSysEvent(const sptr<NotificationRequest> & request,ErrCode errCode)82 void AdvancedNotificationService::SendPublishHiSysEvent(const sptr<NotificationRequest> &request, ErrCode errCode)
83 {
84     if (request == nullptr) {
85         return;
86     }
87 
88     EventInfo eventInfo;
89     eventInfo.notificationId = request->GetNotificationId();
90     eventInfo.contentType = static_cast<int32_t>(request->GetNotificationType());
91     eventInfo.bundleName = request->GetCreatorBundleName();
92     eventInfo.userId = request->GetCreatorUserId();
93     if (errCode != ERR_OK) {
94         eventInfo.errCode = errCode;
95         EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo);
96     } else {
97         EventReport::SendHiSysEvent(PUBLISH, eventInfo);
98     }
99 }
100 
SendCancelHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)101 void AdvancedNotificationService::SendCancelHiSysEvent(int32_t notificationId, const std::string &label,
102     const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
103 {
104     if (bundleOption == nullptr || errCode != ERR_OK) {
105         ANS_LOGD("bundleOption is nullptr or not ok %{public}d.", errCode);
106         return;
107     }
108 
109     EventInfo eventInfo;
110     eventInfo.notificationId = notificationId;
111     eventInfo.notificationLabel = label;
112     eventInfo.bundleName = bundleOption->GetBundleName();
113     eventInfo.uid = bundleOption->GetUid();
114     EventReport::SendHiSysEvent(CANCEL, eventInfo);
115 }
116 
SendRemoveHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)117 void AdvancedNotificationService::SendRemoveHiSysEvent(int32_t notificationId, const std::string &label,
118     const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
119 {
120     if (bundleOption == nullptr || errCode != ERR_OK) {
121         return;
122     }
123 
124     EventInfo eventInfo;
125     eventInfo.notificationId = notificationId;
126     eventInfo.notificationLabel = label;
127     eventInfo.bundleName = bundleOption->GetBundleName();
128     eventInfo.uid = bundleOption->GetUid();
129     EventReport::SendHiSysEvent(REMOVE, eventInfo);
130 }
131 
SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,bool enabled,ErrCode errCode)132 void AdvancedNotificationService::SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption,
133     bool enabled, ErrCode errCode)
134 {
135     if (bundleOption == nullptr) {
136         return;
137     }
138 
139     EventInfo eventInfo;
140     eventInfo.bundleName = bundleOption->GetBundleName();
141     eventInfo.uid = bundleOption->GetUid();
142     eventInfo.enable = enabled;
143     if (errCode != ERR_OK) {
144         eventInfo.errCode = errCode;
145         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo);
146     } else {
147         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo);
148     }
149 }
150 
SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,ErrCode errCode)151 void AdvancedNotificationService::SendEnableNotificationSlotHiSysEvent(
152     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType,
153     bool enabled, ErrCode errCode)
154 {
155     if (bundleOption == nullptr) {
156         return;
157     }
158 
159     EventInfo eventInfo;
160     eventInfo.bundleName = bundleOption->GetBundleName();
161     eventInfo.uid = bundleOption->GetUid();
162     eventInfo.slotType = slotType;
163     eventInfo.enable = enabled;
164     if (errCode != ERR_OK) {
165         eventInfo.errCode = errCode;
166         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo);
167     } else {
168         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo);
169     }
170 }
171 
SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> & record)172 void AdvancedNotificationService::SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record)
173 {
174     if (record == nullptr || record->request == nullptr || record->bundleOption == nullptr) {
175         return;
176     }
177 
178     EventInfo eventInfo;
179     eventInfo.notificationId = record->request->GetNotificationId();
180     eventInfo.bundleName = record->bundleOption->GetBundleName();
181     eventInfo.uid = record->bundleOption->GetUid();
182     EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo);
183 }
184 
SendLiveViewUploadHiSysEvent(const std::shared_ptr<NotificationRecord> & record,int32_t uploadStatus)185 void AdvancedNotificationService::SendLiveViewUploadHiSysEvent(
186     const std::shared_ptr<NotificationRecord> &record, int32_t uploadStatus)
187 {
188     if (record == nullptr || record->request == nullptr ||
189         uploadStatus < UploadStatus::CREATE || uploadStatus > UploadStatus::END) {
190         return;
191     }
192 
193     EventInfo eventInfo;
194     eventInfo.notificationId = record->request->GetNotificationId();
195     eventInfo.bundleName = record->request->GetCreatorBundleName();
196     eventInfo.contentType = static_cast<int32_t>(record->request->GetNotificationType());
197     eventInfo.operateFlag = uploadStatus;
198     EventReport::SendHiSysEvent(STATIC_LIVE_VIEW_UPLOAD, eventInfo);
199 }
200 }  // namespace Notification
201 }  // namespace OHOS
202