1 /*
2 * Copyright (c) 2022 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 "common.h"
17
18 #include "cancel_suspend_delay.h"
19 #include "transient_task_log.h"
20
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
23 const std::int32_t STR_MAX_SIZE = 64;
24 const std::int32_t EXPIRE_CALLBACK_PARAM_NUM = 1;
25 const std::int32_t ASYNC_CALLBACK_PARAM_NUM = 2;
26
NapiGetboolean(napi_env env,const bool & isValue)27 napi_value Common::NapiGetboolean(napi_env env, const bool &isValue)
28 {
29 napi_value result = nullptr;
30 napi_get_boolean(env, isValue, &result);
31 return result;
32 }
33
NapiGetNull(napi_env env)34 napi_value Common::NapiGetNull(napi_env env)
35 {
36 napi_value result = nullptr;
37 napi_get_null(env, &result);
38 return result;
39 }
40
ReturnCallbackPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)41 void Common::ReturnCallbackPromise(const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
42 {
43 if (info.isCallback) {
44 SetCallback(env, info.callback, info.errorCode, result);
45 } else {
46 SetPromise(env, info, result);
47 }
48 }
49
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int & errorCode,const napi_value & result)50 void Common::SetCallback(
51 const napi_env &env, const napi_ref &callbackIn, const int &errorCode, const napi_value &result)
52 {
53 napi_value undefined = nullptr;
54 napi_get_undefined(env, &undefined);
55
56 napi_value callback = nullptr;
57 napi_value resultout = nullptr;
58 napi_get_reference_value(env, callbackIn, &callback);
59 napi_value results[ASYNC_CALLBACK_PARAM_NUM] = {nullptr};
60 results[0] = GetCallbackErrorValue(env, errorCode);
61 results[1] = result;
62 NAPI_CALL_RETURN_VOID(env,
63 napi_call_function(env, undefined, callback, ASYNC_CALLBACK_PARAM_NUM, &results[0], &resultout));
64 }
65
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)66 void Common::SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
67 {
68 napi_value undefined = nullptr;
69 napi_get_undefined(env, &undefined);
70
71 napi_value callback = nullptr;
72 napi_value resultout = nullptr;
73 napi_value res = nullptr;
74 res = GetExpireCallbackValue(env, 0, result);
75 napi_get_reference_value(env, callbackIn, &callback);
76 NAPI_CALL_RETURN_VOID(env,
77 napi_call_function(env, undefined, callback, EXPIRE_CALLBACK_PARAM_NUM, &res, &resultout));
78 }
79
GetExpireCallbackValue(napi_env env,int errCode,const napi_value & value)80 napi_value Common::GetExpireCallbackValue(napi_env env, int errCode, const napi_value &value)
81 {
82 napi_value result = nullptr;
83 napi_value eCode = nullptr;
84 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
85 NAPI_CALL(env, napi_create_object(env, &result));
86 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
87 NAPI_CALL(env, napi_set_named_property(env, result, "data", value));
88 return result;
89 }
90
SetPromise(const napi_env & env,const CallbackPromiseInfo & info,const napi_value & result)91 napi_value Common::SetPromise(
92 const napi_env &env, const CallbackPromiseInfo &info, const napi_value &result)
93 {
94 if (info.errorCode == ERR_OK) {
95 napi_resolve_deferred(env, info.deferred, result);
96 } else {
97 napi_value res = nullptr;
98 napi_value eCode = nullptr;
99 NAPI_CALL(env, napi_create_int32(env, info.errorCode, &eCode));
100 NAPI_CALL(env, napi_create_object(env, &res));
101 NAPI_CALL(env, napi_set_named_property(env, res, "data", eCode));
102 NAPI_CALL(env, napi_set_named_property(env, res, "code", eCode));
103 napi_reject_deferred(env, info.deferred, res);
104 }
105 return result;
106 }
107
GetCallbackErrorValue(napi_env env,int errCode)108 napi_value Common::GetCallbackErrorValue(napi_env env, int errCode)
109 {
110 if (errCode == ERR_OK) {
111 return NapiGetNull(env);
112 }
113 napi_value result = nullptr;
114 napi_value eCode = nullptr;
115 NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
116 NAPI_CALL(env, napi_create_object(env, &result));
117 NAPI_CALL(env, napi_set_named_property(env, result, "data", eCode));
118 NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
119 return result;
120 }
121
JSParaError(const napi_env & env,const napi_ref & callback)122 napi_value Common::JSParaError(const napi_env &env, const napi_ref &callback)
123 {
124 if (callback) {
125 SetCallback(env, callback, ERR_BGTASK_INVALID_PARAM, nullptr);
126 return Common::NapiGetNull(env);
127 } else {
128 napi_value promise = nullptr;
129 napi_deferred deferred = nullptr;
130 napi_create_promise(env, &deferred, &promise);
131
132 napi_value res = nullptr;
133 napi_value eCode = nullptr;
134 NAPI_CALL(env, napi_create_int32(env, ERR_BGTASK_INVALID_PARAM, &eCode));
135 NAPI_CALL(env, napi_create_object(env, &res));
136 NAPI_CALL(env, napi_set_named_property(env, res, "data", eCode));
137 NAPI_CALL(env, napi_set_named_property(env, res, "code", eCode));
138 napi_reject_deferred(env, deferred, res);
139 return promise;
140 }
141 }
142
GetU16StringValue(const napi_env & env,const napi_value & value,std::u16string & result)143 napi_value Common::GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result)
144 {
145 BGTASK_LOGI("GetU16StringValue start");
146 napi_valuetype valuetype = napi_undefined;
147
148 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
149 NAPI_ASSERT(env, valuetype == napi_string, "Wrong argument type. String or function expected.");
150 if (valuetype == napi_string) {
151 char str[STR_MAX_SIZE] = {0};
152 size_t strLen = 0;
153 NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
154
155 result = Str8ToStr16((std::string)str);
156 BGTASK_LOGI("string result: %{public}s", Str16ToStr8(result).c_str());
157 } else {
158 return nullptr;
159 }
160
161 return Common::NapiGetNull(env);
162 }
163
GetInt32NumberValue(const napi_env & env,const napi_value & value,int32_t & result)164 napi_value Common::GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result)
165 {
166 BGTASK_LOGI("GetInt32NumberValue start ");
167 napi_valuetype valuetype = napi_undefined;
168
169 NAPI_CALL(env, napi_typeof(env, value, &valuetype));
170 NAPI_ASSERT(env, valuetype == napi_number, "Wrong argument type. Number or function expected.");
171 napi_get_value_int32(env, value, &result);
172 BGTASK_LOGI("number result: %{public}d", result);
173
174 return Common::NapiGetNull(env);
175 }
176
PaddingCallbackPromiseInfo(const napi_env & env,const napi_ref & callback,CallbackPromiseInfo & info,napi_value & promise)177 void Common::PaddingCallbackPromiseInfo(
178 const napi_env &env, const napi_ref &callback, CallbackPromiseInfo &info, napi_value &promise)
179 {
180 if (callback) {
181 info.callback = callback;
182 info.isCallback = true;
183 } else {
184 napi_deferred deferred = nullptr;
185 NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
186 info.deferred = deferred;
187 info.isCallback = false;
188 }
189 }
190
SetDelaySuspendInfo(const napi_env & env,std::shared_ptr<DelaySuspendInfo> & delaySuspendInfo,napi_value & result)191 napi_value Common::SetDelaySuspendInfo(
192 const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result)
193 {
194 if (delaySuspendInfo == nullptr) {
195 BGTASK_LOGI("delaySuspendInfo is nullptr");
196 return NapiGetboolean(env, false);
197 }
198 napi_value value = nullptr;
199
200 // readonly requestId?: number
201 napi_create_int32(env, delaySuspendInfo->GetRequestId(), &value);
202 napi_set_named_property(env, result, "requestId", value);
203
204 // readonly actualDelayTime?: number
205 napi_create_int32(env, delaySuspendInfo->GetActualDelayTime(), &value);
206 napi_set_named_property(env, result, "actualDelayTime", value);
207
208 return NapiGetboolean(env, true);
209 }
210 } // namespace BackgroundTaskMgr
211 } // namespace OHOS