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 "unsubscribe.h"
17
18 #include "ans_inner_errors.h"
19
20 namespace OHOS {
21 namespace NotificationNapi {
22 const int UNSUBSCRIBE_MAX_PARA = 2;
23
ParseParameters(const napi_env & env,const napi_callback_info & info,ParametersInfoUnsubscribe & paras)24 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, ParametersInfoUnsubscribe ¶s)
25 {
26 ANS_LOGI("enter");
27
28 size_t argc = UNSUBSCRIBE_MAX_PARA;
29 napi_value argv[UNSUBSCRIBE_MAX_PARA] = {nullptr};
30 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
31 if (argc < 1) {
32 ANS_LOGE("Wrong number of arguments");
33 return nullptr;
34 }
35
36 napi_valuetype valuetype = napi_undefined;
37 // argv[0]:subscriber
38 NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype));
39 if (valuetype != napi_object) {
40 ANS_LOGE("Wrong argument type. Object expected.");
41 return nullptr;
42 }
43
44 SubscriberInstancesInfo subscriberInstancesInfo;
45 if (!HasNotificationSubscriber(env, argv[PARAM0], subscriberInstancesInfo)) {
46 ANS_LOGW("Subscriber not found");
47 }
48 paras.objectInfo = subscriberInstancesInfo.subscriber;
49
50 // argv[1]:callback
51 if (argc >= UNSUBSCRIBE_MAX_PARA) {
52 NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype));
53 if (valuetype != napi_function) {
54 ANS_LOGE("Wrong argument type. Function expected.");
55 return nullptr;
56 }
57 napi_create_reference(env, argv[PARAM1], 1, ¶s.callback);
58 }
59
60 return Common::NapiGetNull(env);
61 }
62
Unsubscribe(napi_env env,napi_callback_info info)63 napi_value Unsubscribe(napi_env env, napi_callback_info info)
64 {
65 ANS_LOGI("Unsubscribe start");
66
67 ParametersInfoUnsubscribe paras;
68 if (ParseParameters(env, info, paras) == nullptr) {
69 return Common::NapiGetUndefined(env);
70 }
71
72 AsyncCallbackInfoUnsubscribe *asynccallbackinfo = new (std::nothrow)
73 AsyncCallbackInfoUnsubscribe {.env = env, .asyncWork = nullptr, .objectInfo = paras.objectInfo};
74 if (!asynccallbackinfo) {
75 return Common::JSParaError(env, paras.callback);
76 }
77 napi_value promise = nullptr;
78 Common::PaddingCallbackPromiseInfo(env, paras.callback, asynccallbackinfo->info, promise);
79
80 napi_value resourceName = nullptr;
81 napi_create_string_latin1(env, "unsubscribe", NAPI_AUTO_LENGTH, &resourceName);
82
83 // Asynchronous function call
84 napi_create_async_work(env,
85 nullptr,
86 resourceName,
87 [](napi_env env, void *data) {
88 ANS_LOGI("Unsubscribe napi_create_async_work start");
89 auto asynccallbackinfo = reinterpret_cast<AsyncCallbackInfoUnsubscribe *>(data);
90
91 if (asynccallbackinfo->objectInfo == nullptr) {
92 ANS_LOGE("invalid object info");
93 asynccallbackinfo->info.errorCode = ERR_ANS_INVALID_PARAM;
94 return;
95 }
96
97 bool ret = AddDeletingSubscriber(asynccallbackinfo->objectInfo);
98 if (ret) {
99 asynccallbackinfo->info.errorCode =
100 NotificationHelper::UnSubscribeNotification(*(asynccallbackinfo->objectInfo));
101 if (asynccallbackinfo->info.errorCode != ERR_OK) {
102 DelDeletingSubscriber(asynccallbackinfo->objectInfo);
103 }
104 } else {
105 asynccallbackinfo->info.errorCode = ERR_ANS_SUBSCRIBER_IS_DELETING;
106 }
107 },
108 [](napi_env env, napi_status status, void *data) {
109 ANS_LOGI("Unsubscribe napi_create_async_work end");
110 AsyncCallbackInfoUnsubscribe *asynccallbackinfo = static_cast<AsyncCallbackInfoUnsubscribe *>(data);
111 if (asynccallbackinfo) {
112 Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env));
113 if (asynccallbackinfo->info.callback != nullptr) {
114 napi_delete_reference(env, asynccallbackinfo->info.callback);
115 }
116 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
117 delete asynccallbackinfo;
118 asynccallbackinfo = nullptr;
119 }
120 },
121 (void *)asynccallbackinfo,
122 &asynccallbackinfo->asyncWork);
123
124 napi_status status = napi_queue_async_work(env, asynccallbackinfo->asyncWork);
125 if (status != napi_ok) {
126 ANS_LOGE("napi_queue_async_work failed return: %{public}d", status);
127 if (asynccallbackinfo->info.callback != nullptr) {
128 napi_delete_reference(env, asynccallbackinfo->info.callback);
129 }
130 napi_delete_async_work(env, asynccallbackinfo->asyncWork);
131 delete asynccallbackinfo;
132 asynccallbackinfo = nullptr;
133 return Common::JSParaError(env, paras.callback);
134 }
135
136 if (asynccallbackinfo->info.isCallback) {
137 return Common::NapiGetNull(env);
138 } else {
139 return promise;
140 }
141 }
142 } // namespace NotificationNapi
143 } // namespace OHOS