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 #include "ani_on.h"
17 #include "ans_log_wrapper.h"
18 #include "sts_common.h"
19 #include "sts_throw_erro.h"
20 #include "sts_request.h"
21 #include "ani_push_callback.h"
22 #include "ipc_skeleton.h"
23 #include "tokenid_kit.h"
24 #include "notification_helper.h"
25
26 constexpr const char* TYPE_STRING = "checkNotification";
27 namespace OHOS {
28 namespace NotificationManagerSts {
29 using namespace OHOS::Notification;
30
CheckCallerIsSystemApp()31 bool CheckCallerIsSystemApp()
32 {
33 auto selfToken = IPCSkeleton::GetSelfTokenID();
34 if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
35 ANS_LOGE("current app is not system app, not allow.");
36 return false;
37 }
38 return true;
39 }
40
AniOn(ani_env * env,ani_string type,ani_fn_object fn,ani_object checkRequestObj)41 ani_int AniOn(ani_env *env, ani_string type, ani_fn_object fn, ani_object checkRequestObj)
42 {
43 ANS_LOGD("enter");
44 #ifdef ANS_FEATURE_LIVEVIEW_LOCAL_LIVEVIEW
45 std::string typeStr = "";
46 ani_status status = OHOS::NotificationSts::GetStringByAniString(env, type, typeStr);
47 if (status != ANI_OK || typeStr.compare(TYPE_STRING)) {
48 ANS_LOGE("InvalidParam 'type'");
49 int32_t errCode = OHOS::Notification::ERROR_PARAM_INVALID;
50 OHOS::NotificationSts::ThrowErrorWithInvalidParam(env);
51 return errCode;
52 }
53 if (OHOS::NotificationSts::IsUndefine(env, checkRequestObj)) {
54 ANS_LOGI("Old function param, don't need register.");
55 return ERR_OK;
56 }
57 sptr<NotificationCheckRequest> checkRequest = new NotificationCheckRequest();
58 if (!OHOS::NotificationSts::UnWarpNotificationCheckRequest(env, checkRequestObj, checkRequest)) {
59 ANS_LOGE("InvalidParam 'checkRequest'");
60 int32_t errCode = OHOS::Notification::ERROR_PARAM_INVALID;
61 OHOS::NotificationSts::ThrowErrorWithInvalidParam(env);
62 return errCode;
63 }
64 if (!CheckCallerIsSystemApp()) {
65 OHOS::NotificationSts::ThrowErrorWithCode(env, ERROR_NOT_SYSTEM_APP);
66 return ERROR_NOT_SYSTEM_APP;
67 }
68
69 sptr<StsPushCallBack> stsPushCallBack_ = new (std::nothrow) StsPushCallBack(env);
70 if (stsPushCallBack_ == nullptr) {
71 ANS_LOGE("new stsPushCallBack_ failed");
72 OHOS::NotificationSts::ThrowErrorWithCode(env, ERROR_INTERNAL_ERROR);
73 return ERROR_INTERNAL_ERROR;
74 }
75 NotificationConstant::SlotType outSlotType = checkRequest->GetSlotType();
76 stsPushCallBack_->SetJsPushCallBackObject(env, outSlotType, fn);
77 auto result = NotificationHelper::RegisterPushCallback(stsPushCallBack_->AsObject(), checkRequest);
78 if (result != ERR_OK) {
79 int32_t externalCode = ERR_OK ? ERR_OK : NotificationSts::GetExternalCode(result);
80 ANS_LOGE("Register failed, result is %{public}d", externalCode);
81 OHOS::NotificationSts::ThrowErrorWithCode(env, externalCode);
82 return externalCode;
83 }
84 ANS_LOGD("done");
85 return result;
86 #else
87 int32_t errCode = OHOS::Notification::ERROR_SYSTEM_CAP_ERROR;
88 OHOS::NotificationSts::ThrowErrorWithCode(env, errCode);
89 return errCode;
90 #endif
91 }
92
AniOff(ani_env * env,ani_string type,ani_fn_object fn)93 ani_int AniOff(ani_env *env, ani_string type, ani_fn_object fn)
94 {
95 ANS_LOGD("enter");
96 #ifdef ANS_FEATURE_LIVEVIEW_LOCAL_LIVEVIEW
97 std::string typeStr = "";
98 ani_status status = OHOS::NotificationSts::GetStringByAniString(env, type, typeStr);
99 if (status != ANI_OK || typeStr.compare(TYPE_STRING)) {
100 ANS_LOGE("InvalidParam 'type'");
101 int32_t errCode = OHOS::Notification::ERROR_PARAM_INVALID;
102 OHOS::NotificationSts::ThrowErrorWithInvalidParam(env);
103 return errCode;
104 }
105 if (!CheckCallerIsSystemApp()) {
106 OHOS::NotificationSts::ThrowErrorWithCode(env, ERROR_NOT_SYSTEM_APP);
107 return ERROR_NOT_SYSTEM_APP;
108 }
109 if (!OHOS::NotificationSts::IsUndefine(env, fn)) {
110 int32_t errCode = OHOS::Notification::ERROR_PARAM_INVALID;
111 OHOS::NotificationSts::ThrowErrorWithInvalidParam(env);
112 return errCode;
113 }
114 int32_t ret = NotificationHelper::UnregisterPushCallback();
115 ANS_LOGD("done. ret %{public}d", ret);
116 return ERR_OK;
117 #else
118 int32_t errCode = OHOS::Notification::ERROR_SYSTEM_CAP_ERROR;
119 OHOS::NotificationSts::ThrowErrorWithCode(env, errCode);
120 return errCode;
121 #endif
122 }
123 }
124 }