• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     eventInfo.slotType = request->GetSlotType();
94     eventInfo.classification = request->GetClassification();
95     if (request->GetFlags() != nullptr) {
96         eventInfo.reminderFlags = request->GetFlags()->GetReminderFlags();
97     }
98     eventInfo.notificationControlFlags = request->GetNotificationControlFlags();
99     if (errCode != ERR_OK) {
100         eventInfo.errCode = errCode;
101         EventReport::SendHiSysEvent(PUBLISH_ERROR, eventInfo);
102     } else {
103         EventReport::SendHiSysEvent(PUBLISH, eventInfo);
104     }
105 }
106 
SendCancelHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)107 void AdvancedNotificationService::SendCancelHiSysEvent(int32_t notificationId, const std::string &label,
108     const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
109 {
110     if (bundleOption == nullptr || errCode != ERR_OK) {
111         ANS_LOGD("bundleOption is nullptr or not ok %{public}d.", errCode);
112         return;
113     }
114 
115     EventInfo eventInfo;
116     eventInfo.notificationId = notificationId;
117     eventInfo.notificationLabel = label;
118     eventInfo.bundleName = bundleOption->GetBundleName();
119     eventInfo.uid = bundleOption->GetUid();
120     EventReport::SendHiSysEvent(CANCEL, eventInfo);
121 }
122 
SendRemoveHiSysEvent(int32_t notificationId,const std::string & label,const sptr<NotificationBundleOption> & bundleOption,ErrCode errCode)123 void AdvancedNotificationService::SendRemoveHiSysEvent(int32_t notificationId, const std::string &label,
124     const sptr<NotificationBundleOption> &bundleOption, ErrCode errCode)
125 {
126     if (bundleOption == nullptr || errCode != ERR_OK) {
127         return;
128     }
129 
130     EventInfo eventInfo;
131     eventInfo.notificationId = notificationId;
132     eventInfo.notificationLabel = label;
133     eventInfo.bundleName = bundleOption->GetBundleName();
134     eventInfo.uid = bundleOption->GetUid();
135     EventReport::SendHiSysEvent(REMOVE, eventInfo);
136 }
137 
SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,bool enabled,ErrCode errCode)138 void AdvancedNotificationService::SendEnableNotificationHiSysEvent(const sptr<NotificationBundleOption> &bundleOption,
139     bool enabled, ErrCode errCode)
140 {
141     if (bundleOption == nullptr) {
142         return;
143     }
144 
145     EventInfo eventInfo;
146     eventInfo.bundleName = bundleOption->GetBundleName();
147     eventInfo.uid = bundleOption->GetUid();
148     eventInfo.enable = enabled;
149     if (errCode != ERR_OK) {
150         eventInfo.errCode = errCode;
151         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_ERROR, eventInfo);
152     } else {
153         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION, eventInfo);
154     }
155 }
156 
SendEnableNotificationSlotHiSysEvent(const sptr<NotificationBundleOption> & bundleOption,const NotificationConstant::SlotType & slotType,bool enabled,ErrCode errCode)157 void AdvancedNotificationService::SendEnableNotificationSlotHiSysEvent(
158     const sptr<NotificationBundleOption> &bundleOption, const NotificationConstant::SlotType &slotType,
159     bool enabled, ErrCode errCode)
160 {
161     if (bundleOption == nullptr) {
162         return;
163     }
164 
165     EventInfo eventInfo;
166     eventInfo.bundleName = bundleOption->GetBundleName();
167     eventInfo.uid = bundleOption->GetUid();
168     eventInfo.slotType = slotType;
169     eventInfo.enable = enabled;
170     if (errCode != ERR_OK) {
171         eventInfo.errCode = errCode;
172         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT_ERROR, eventInfo);
173     } else {
174         EventReport::SendHiSysEvent(ENABLE_NOTIFICATION_SLOT, eventInfo);
175     }
176 }
177 
SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> & record)178 void AdvancedNotificationService::SendFlowControlOccurHiSysEvent(const std::shared_ptr<NotificationRecord> &record)
179 {
180     if (record == nullptr || record->request == nullptr || record->bundleOption == nullptr) {
181         return;
182     }
183 
184     EventInfo eventInfo;
185     eventInfo.notificationId = record->request->GetNotificationId();
186     eventInfo.bundleName = record->bundleOption->GetBundleName();
187     eventInfo.uid = record->bundleOption->GetUid();
188     EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo);
189 }
190 
SendLiveViewUploadHiSysEvent(const std::shared_ptr<NotificationRecord> & record,int32_t uploadStatus)191 void AdvancedNotificationService::SendLiveViewUploadHiSysEvent(
192     const std::shared_ptr<NotificationRecord> &record, int32_t uploadStatus)
193 {
194     if (record == nullptr || record->request == nullptr ||
195         uploadStatus < UploadStatus::CREATE || uploadStatus > UploadStatus::END) {
196         return;
197     }
198 
199     EventInfo eventInfo;
200     eventInfo.notificationId = record->request->GetNotificationId();
201     eventInfo.bundleName = record->request->GetCreatorBundleName();
202     eventInfo.contentType = static_cast<int32_t>(record->request->GetNotificationType());
203     eventInfo.operateFlag = uploadStatus;
204     EventReport::SendHiSysEvent(STATIC_LIVE_VIEW_UPLOAD, eventInfo);
205 }
206 }  // namespace Notification
207 }  // namespace OHOS
208