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 <functional>
17 #include <iomanip>
18 #include <sstream>
19
20 #include "ans_const_define.h"
21 #include "ans_inner_errors.h"
22 #include "ans_log_wrapper.h"
23 #include "access_token_helper.h"
24 #include "ans_permission_def.h"
25 #include "bundle_manager_helper.h"
26 #include "errors.h"
27 #include "ipc_skeleton.h"
28 #include "notification_constant.h"
29 #include "os_account_manager_helper.h"
30 #include "notification_preferences.h"
31 #include "notification_analytics_util.h"
32
33
34 namespace OHOS {
35 namespace Notification {
GetClientBundleNameByUid(int32_t callingUid)36 inline std::string GetClientBundleNameByUid(int32_t callingUid)
37 {
38 std::string bundle;
39
40 std::shared_ptr<BundleManagerHelper> bundleManager = BundleManagerHelper::GetInstance();
41 if (bundleManager != nullptr) {
42 bundle = bundleManager->GetBundleNameByUid(callingUid);
43 }
44
45 return bundle;
46 }
47
GetClientBundleName()48 inline std::string GetClientBundleName()
49 {
50 int32_t callingUid = IPCSkeleton::GetCallingUid();
51 return GetClientBundleNameByUid(callingUid);
52 }
53
CheckUserIdParams(const int userId)54 inline int32_t CheckUserIdParams(const int userId)
55 {
56 if (userId != SUBSCRIBE_USER_INIT && !OsAccountManagerHelper::GetInstance().CheckUserExists(userId)) {
57 return ERROR_USER_NOT_EXIST;
58 }
59 return ERR_OK;
60 }
61
ResetSeconds(int64_t date)62 inline int64_t ResetSeconds(int64_t date)
63 {
64 auto milliseconds = std::chrono::milliseconds(date);
65 auto tp = std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds>(milliseconds);
66 auto tp_minutes = std::chrono::time_point_cast<std::chrono::minutes>(tp);
67 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(tp_minutes.time_since_epoch());
68 return duration.count();
69 }
70
GetCurrentTime()71 inline int64_t GetCurrentTime()
72 {
73 auto now = std::chrono::system_clock::now();
74 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
75 return duration.count();
76 }
77
GetLocalTime(time_t time)78 inline tm GetLocalTime(time_t time)
79 {
80 struct tm ret = {0};
81 localtime_r(&time, &ret);
82 return ret;
83 }
84
CheckPictureSize(const sptr<NotificationRequest> & request)85 inline ErrCode CheckPictureSize(const sptr<NotificationRequest> &request)
86 {
87 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_1, EventBranchId::BRANCH_1);
88 auto result = request->CheckImageSizeForContent();
89 if (result != ERR_OK) {
90 ANS_LOGE("Check image size failed.");
91 message.ErrorCode(result).Message("Check image size failed.");
92 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
93 return result;
94 }
95
96 if (request->CheckImageOverSizeForPixelMap(request->GetLittleIcon(), MAX_ICON_SIZE)) {
97 message.ErrorCode(ERR_ANS_ICON_OVER_SIZE).Message("Check little image size failed.");
98 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
99 return ERR_ANS_ICON_OVER_SIZE;
100 }
101
102 if (request->CheckImageOverSizeForPixelMap(request->GetOverlayIcon(), MAX_ICON_SIZE)) {
103 message.ErrorCode(ERR_ANS_ICON_OVER_SIZE).Message("Check overlay size failed.");
104 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
105 return ERR_ANS_ICON_OVER_SIZE;
106 }
107
108 if (request->CheckImageOverSizeForPixelMap(request->GetBigIcon(), MAX_ICON_SIZE)) {
109 request->ResetBigIcon();
110 ANS_LOGI("Check big image size over limit");
111 }
112
113 return ERR_OK;
114 }
115
AddInformationInMessage(OHOS::Notification::HaMetaMessage haMetaMessage,const int32_t reason,std::string message)116 inline OHOS::Notification::HaMetaMessage AddInformationInMessage(
117 OHOS::Notification::HaMetaMessage haMetaMessage, const int32_t reason,
118 std::string message)
119 {
120 message += "reason:" + std::to_string(reason) + ".";
121
122 std::string bundleName;
123 int32_t callingUid = IPCSkeleton::GetCallingUid();
124 message += "uid:" + std::to_string(callingUid) + ".";
125 bundleName = GetClientBundleNameByUid(callingUid);
126
127 haMetaMessage = haMetaMessage.AgentBundleName(bundleName);
128 haMetaMessage = haMetaMessage.Message(message);
129 return haMetaMessage;
130 }
131
132
ReportDeleteFailedEventPush(OHOS::Notification::HaMetaMessage haMetaMessage,const int32_t reason,std::string message)133 inline void ReportDeleteFailedEventPush(OHOS::Notification::HaMetaMessage haMetaMessage,
134 const int32_t reason, std::string message)
135 {
136 haMetaMessage = AddInformationInMessage(haMetaMessage, reason, message);
137 NotificationAnalyticsUtil::ReportDeleteFailedEvent(haMetaMessage);
138 }
139
ReportDeleteFailedEventPushByNotification(const sptr<Notification> & notification,OHOS::Notification::HaMetaMessage haMetaMessage,const int32_t reason,std::string message)140 inline void ReportDeleteFailedEventPushByNotification(const sptr<Notification> ¬ification,
141 OHOS::Notification::HaMetaMessage haMetaMessage, const int32_t reason,
142 std::string message)
143 {
144 if (notification == nullptr) {
145 ANS_LOGW("report notificaiton is null");
146 return;
147 }
148 haMetaMessage = AddInformationInMessage(haMetaMessage, reason, message);
149 NotificationAnalyticsUtil::ReportDeleteFailedEvent(
150 notification->GetNotificationRequestPoint(), haMetaMessage);
151 }
152 } // namespace Notification
153 } // namespace OHOS
154