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 #include "ani_subscribe.h"
16
17 #include <thread>
18 #include <iostream>
19 #include "notification_helper.h"
20 #include "ani_remove.h"
21 #include "ans_log_wrapper.h"
22 #include "sts_subscribe.h"
23 #include "sts_throw_erro.h"
24
25 namespace OHOS {
26 namespace NotificationSubScribeSts {
27 static const char *REMOVE_FOR_BUNDLE_SIGNATURE =
28 "Lnotification/NotificationCommonDef/BundleOption;"
29 "L@ohos/notificationSubscribe/notificationSubscribe/NotificationKey;"
30 "L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V";
31 static const char *REMOVE_FOR_HASHCODE_SIGNATURE =
32 "Lstd/core/String;L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V";
33 static const char *REMOVE_FOR_HASHCODES_SIGNATURE =
34 "Lescompat/Array;L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V";
35 static const char *DISTRIBUTE_OPERATION_SIGNATURE =
36 "Lstd/core/String;L@ohos/notificationSubscribe/notificationSubscribe/OperationInfo;:Lstd/core/Promise;";
37 static const char *SUBSCRIBE_SIGNATURE =
38 "Lnotification/notificationSubscriber/NotificationSubscriber;"
39 "Lnotification/notificationSubscribeInfo/NotificationSubscribeInfo;:V";
40 static const char *UNSUBSCRIBE_SIGNATURE =
41 "Lnotification/notificationSubscriber/NotificationSubscriber;:V";
42 static const char *REMOVEALL_FOR_BUNDLEOPTION_SIGNATURE =
43 "Lnotification/NotificationCommonDef/BundleOption;:V";
44 static const char *REMOVEALL_FOR_USERID_STGNATURE = "D:V";
45 static const char *REMOVEALL_SIGNATURE = ":V";
46
AniDistributeOperation(ani_env * env,ani_string hashcode,ani_object operationInfo)47 ani_object AniDistributeOperation(ani_env *env, ani_string hashcode, ani_object operationInfo)
48 {
49 ANS_LOGD("StsDistributeOperation enter");
50 ani_object aniPromise {};
51 ani_resolver aniResolver {};
52 if (ANI_OK != env->Promise_New(&aniResolver, &aniPromise)) {
53 ANS_LOGE("Promise_New faild");
54 return nullptr;
55 }
56 bool noWithOperationInfo = false;
57 auto info = NotificationSts::GetOperationInfoForDistributeOperation(
58 env, hashcode, operationInfo, noWithOperationInfo);
59 if (info == nullptr) {
60 ANS_LOGE("get distributeOperation object fail");
61 return nullptr;
62 }
63 sptr<NotificationSts::StsDistributedOperationCallback> callback
64 = new (std::nothrow) NotificationSts::StsDistributedOperationCallback(aniPromise, aniResolver);
65 if (callback == nullptr) {
66 ANS_LOGE("create callback object fail");
67 return nullptr;
68 }
69
70 ani_vm *vm = nullptr;
71 if (ANI_OK != env->GetVM(&vm)) {
72 ANS_LOGD("env GetVM faild");
73 return nullptr;
74 }
75 callback->SetVm(vm);
76 int32_t result = Notification::NotificationHelper::DistributeOperation(info, callback);
77 ANS_LOGD("StsDistributeOperation ret %{public}d. ErrorToExternal %{public}d",
78 result, NotificationSts::GetExternalCode(result));
79 if (result != ERR_OK || noWithOperationInfo) {
80 callback->OnStsOperationCallback(env, result);
81 }
82 return aniPromise;
83 }
84
AniSubscribe(ani_env * env,ani_object obj,ani_object info)85 void AniSubscribe(ani_env *env, ani_object obj, ani_object info)
86 {
87 ANS_LOGD("StsSubscribe enter");
88 OHOS::NotificationSts::SubscriberInstanceManager::GetInstance()->Subscribe(env, obj, info);
89 }
90
AniUnSubscribe(ani_env * env,ani_object obj)91 void AniUnSubscribe(ani_env *env, ani_object obj)
92 {
93 ANS_LOGD("StsUnSubscribe enter");
94 OHOS::NotificationSts::SubscriberInstanceManager::GetInstance()->UnSubscribe(env, obj);
95 }
96
AniSubscribeSelf(ani_env * env,ani_object obj)97 void AniSubscribeSelf(ani_env *env, ani_object obj)
98 {
99 ANS_LOGD("StsSubscribeSelf enter");
100 OHOS::NotificationSts::SubscriberInstanceManager::GetInstance()->SubscribeSelf(env, obj);
101 }
102
AniSubScribeRegistryInit(ani_env * env)103 void AniSubScribeRegistryInit(ani_env *env)
104 {
105 ANS_LOGD("AniSubScribeRegistryInit call");
106 static const char *npName = "L@ohos/notificationSubscribe/notificationSubscribe;";
107 ani_namespace np;
108 if (ANI_OK != env->FindNamespace(npName, &np)) {
109 ANS_LOGD("Not found '%{public}s'", npName);
110 return;
111 }
112
113 std::array methods = {
114 ani_native_function {"nativeRemove",
115 REMOVE_FOR_BUNDLE_SIGNATURE, reinterpret_cast<void *>(AniRemoveForBundle)},
116 ani_native_function {"nativeRemove",
117 REMOVE_FOR_HASHCODE_SIGNATURE, reinterpret_cast<void *>(AniRemoveForHashCode)},
118 ani_native_function {"nativeRemove",
119 REMOVE_FOR_HASHCODES_SIGNATURE, reinterpret_cast<void *>(AniRemoveForHashCodes)},
120 ani_native_function {"nativeDistributeOperation",
121 DISTRIBUTE_OPERATION_SIGNATURE, reinterpret_cast<void *>(AniDistributeOperation)},
122 ani_native_function {"nativeSubscribe", SUBSCRIBE_SIGNATURE, reinterpret_cast<void *>(AniSubscribe)},
123 ani_native_function {"nativeUnSubscribe", UNSUBSCRIBE_SIGNATURE, reinterpret_cast<void *>(AniUnSubscribe)},
124 ani_native_function {"nativeSubscribeSelf", UNSUBSCRIBE_SIGNATURE, reinterpret_cast<void *>(AniSubscribeSelf)},
125 ani_native_function {"nativeRemoveAllForBundle",
126 REMOVEALL_FOR_BUNDLEOPTION_SIGNATURE, reinterpret_cast<void *>(AniRemoveAllForBundle)},
127 ani_native_function {"nativeRemoveAllForUserId",
128 REMOVEALL_FOR_USERID_STGNATURE, reinterpret_cast<void *>(AniRemoveAllForUserId)},
129 ani_native_function {"nativeRemoveAll", REMOVEALL_SIGNATURE, reinterpret_cast<void *>(AniRemoveAll)},
130 };
131
132 ANS_LOGD("Start bind native methods to '%{public}s'", npName);
133 ani_status status = env->Namespace_BindNativeFunctions(np, methods.data(), methods.size());
134 if (ANI_OK != status) {
135 ANS_LOGD("Cannot bind native methods to '%{public}s'. status %{public}d", npName, status);
136 return;
137 };
138 ANS_LOGD("Finish bind native methods to '%{public}s'", npName);
139
140 if (env->ResetError() != ANI_OK) {
141 ANS_LOGD("ResetError failed");
142 }
143 ANS_LOGD("AniSubScribeRegistryInit end");
144 }
145 } // namespace NotificationSubScribeSts
146 } // namespace OHOS
147
148 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)149 ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result)
150 {
151 ANS_LOGD("ANI_Constructor enter");
152 ani_env *env;
153 if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) {
154 ANS_LOGD("Unsupported ANI_VERSION_1");
155 return ANI_ERROR;
156 }
157
158 OHOS::NotificationSubScribeSts::AniSubScribeRegistryInit(env);
159 ANS_LOGD("ANI_Constructor OK");
160 *result = ANI_VERSION_1;
161 return ANI_OK;
162 }
163 }