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