• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "common.h"
17 
18 #include "background_mode.h"
19 #include "background_sub_mode.h"
20 #include "cancel_suspend_delay.h"
21 #include "transient_task_log.h"
22 
23 namespace OHOS {
24 namespace BackgroundTaskMgr {
25 const uint32_t STR_MAX_SIZE = 64;
26 const uint32_t EXPIRE_CALLBACK_PARAM_NUM = 1;
27 const uint32_t ASYNC_CALLBACK_PARAM_NUM = 2;
28 const std::map<int32_t, std::string> SA_ERRCODE_MSG_MAP = {
29     {ERR_BGTASK_PERMISSION_DENIED, "Permission denied."},
30     {ERR_BGTASK_NOT_SYSTEM_APP,
31         "System API verification failed. Only system application can apply."},
32     {ERR_BGTASK_NO_MEMORY, "Memory operation failed. Failed to allocate the memory."},
33     {ERR_BGTASK_SYS_NOT_READY, "System service operation failed. The system service is not ready."},
34     {ERR_BGTASK_SERVICE_NOT_CONNECTED, "System service operation failed. The system service is not connected."},
35     {ERR_BGTASK_PARCELABLE_FAILED,
36         "Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 2. Failed to apply for memory."},
37     {ERR_BGTASK_TRANSACT_FAILED, "Internal transaction failed."},
38     {ERR_BGTASK_OBJECT_EXISTS,
39         "Continuous Task verification failed. The application has applied for a continuous task."},
40     {ERR_BGTASK_OBJECT_NOT_EXIST,
41         "Continuous Task verification failed. The application has not applied for a continuous task."},
42     {ERR_BGTASK_KEEPING_TASK_VERIFY_ERR,
43         "Continuous Task verification failed. TASK_KEEPING background mode only supported in particular device."},
44     {ERR_BGTASK_INVALID_BGMODE, "Continuous Task verification failed. The bgMode is invalid."},
45     {ERR_BGTASK_INVALID_UID, "Continuous Task verification failed. The uid is invalid."},
46     {ERR_BGTASK_NOTIFICATION_VERIFY_FAILED, "Notification verification failed for a continuous task."
47         " The title or text of the notification cannot be empty."},
48     {ERR_BGTASK_NOTIFICATION_ERR, "Notification verification failed. Failed to send or cancel the notification."},
49     {ERR_BGTASK_CREATE_FILE_ERR, "Continuous task storage failed. Failed to create the storage task file."},
50     {ERR_BGTASK_GET_ACTUAL_FILE_ERR, "Task storage failed. Failed to get the actual storage task file."},
51     {ERR_BGTASK_OPEN_FILE_ERR, "Task storage failed. Failed to open the file."},
52     {ERR_BGTASK_INVALID_PID_OR_UID,
53         "Caller information verification failed for a transient task. Invalid pid or uid."},
54     {ERR_BGTASK_INVALID_BUNDLE_NAME,
55         "Caller information verification failed for a transient task. The bundleName cannot be found."},
56     {ERR_BGTASK_INVALID_REQUEST_ID,
57         "Caller information verification failed for a transient task. Invalid requestId."},
58     {ERR_BGTASK_INVALID_CALLBACK,
59         "Transient task verification failed. The callback cannot be empty."},
60     {ERR_BGTASK_CALLBACK_EXISTS, "Transient task verification failed. The callback already exists."},
61     {ERR_BGTASK_CALLBACK_NOT_EXIST, "Transient task verification failed. The callback does not exist."},
62     {ERR_BGTASK_NOT_IN_PRESET_TIME,
63         "Transient task verification failed. Request is not allow after the preset time of entering background."},
64     {ERR_BGTASK_EXCEEDS_THRESHOLD, "Transient task verification failed. The number of request exceeds the threshold."},
65     {ERR_BGTASK_TIME_INSUFFICIENT,
66         "Transient task verification failed. The remaining time to run transient task is insufficient."},
67     {ERR_BGTASK_RESOURCES_EXCEEDS_MAX, "Caller information verification failed for an energy"
68         " resource request. The number of resources applied exceeds maximun."},
69     {ERR_BGTASK_RESOURCES_INVALID_PID_OR_UID,
70         "Caller information verification failed for an energy resource. Invalid pid or uid."},
71     {ERR_BGTASK_RESOURCES_PARCELABLE_FAILED,
72         "Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 2. Failed to apply for memory."},
73     {ERR_BGTASK_RESOURCES_SYS_NOT_READY, "System service operation failed. The system service is not ready."},
74     {ERR_BGTASK_RESOURCES_SERVICE_NOT_CONNECTED,
75         "System service operation failed. The system service is not connected."},
76     {ERR_BGTASK_SERVICE_INNER_ERROR, "Service inner error."},
77     {ERR_BGTASK_NOREQUEST_TASK, "Transient task verification failed. application no request transient task."},
78     {ERR_BGTASK_FOREGROUND, "Transient task verification failed. application is foreground."},
79     {ERR_BGTASK_TRANSIENT_PARCELABLE_FAILED,
80         "Failed to write data into parcel. Possible reasons: 1. Invalid parameters; 2. Failed to apply for memory."},
81     {ERR_BGTASK_TRANSIENT_SYS_NOT_READY, "System service operation failed. The system service is not ready."},
82     {ERR_BGTASK_TRANSIENT_SERVICE_NOT_CONNECTED,
83         "System service operation failed. The system service is not connected."},
84     {ERR_BGTASK_INVALID_PROCESS_NAME, "Transient task verification failed. caller process name invaild."},
85 };
86 
87 const std::map<int32_t, std::string> PARAM_ERRCODE_MSG_MAP = {
88     {ERR_PARAM_NUMBER_ERR, "The number of arguments is wrong."},
89     {ERR_REASON_NULL_OR_TYPE_ERR, "The reason cannot be null and its type must be string."},
90     {ERR_CALLBACK_NULL_OR_TYPE_ERR, "The callback cannot be null and its type must be function."},
91     {ERR_REQUESTID_NULL_OR_ID_TYPE_ERR, "The requestId cannot be null and its type must be integer."},
92     {ERR_REQUESTID_ILLEGAL, "The requestId must be greater than 0."},
93     {ERR_CONTEXT_NULL_OR_TYPE_ERR, "The context cannot be null and its type must be Context."},
94     {ERR_BGMODE_NULL_OR_TYPE_ERR, "The bgMode cannot be null and its type must be BackgroundMode object."},
95     {ERR_WANTAGENT_NULL_OR_TYPE_ERR, "The wantAgent cannot be null and its type must be WantAgent object."},
96     {ERR_ABILITY_INFO_EMPTY, "The abilityInfo of context cannot be null."},
97     {ERR_GET_TOKEN_ERR, "The token of context cannot be null."},
98     {ERR_BGMODE_RANGE_ERR, "The value of bgMode ranges from BG_MODE_ID_BEGIN to BG_MODE_ID_END."},
99     {ERR_APP_NAME_EMPTY, "The app name of abilityInfo in context cannot be null."},
100     {ERR_RESOURCE_TYPES_INVALID, "The resourcesType cannot be null and must be integer greater than 0."},
101     {ERR_ISAPPLY_NULL_OR_TYPE_ERR, "The isApply cannot be null and its type must be boolean."},
102     {ERR_TIMEOUT_INVALID, "The timeOut cannot be null and must be integer greater than 0."},
103     {ERR_ISPERSIST_NULL_OR_TYPE_ERR, "The isPersist cannot be null and must be boolean."},
104     {ERR_ISPROCESS_NULL_OR_TYPE_ERR, "The isProcess cannot be null and must be boolean."},
105     {ERR_BGTASK_INVALID_PARAM, "The input param is invalid."}
106 };
107 
AsyncWorkData(napi_env napiEnv)108 AsyncWorkData::AsyncWorkData(napi_env napiEnv)
109 {
110     env = napiEnv;
111 }
112 
~AsyncWorkData()113 AsyncWorkData::~AsyncWorkData()
114 {
115     if (callback) {
116         BGTASK_LOGD("AsyncWorkData::~AsyncWorkData delete callback");
117         napi_delete_reference(env, callback);
118         callback = nullptr;
119     }
120     if (asyncWork) {
121         BGTASK_LOGD("AsyncWorkData::~AsyncWorkData delete asyncWork");
122         napi_delete_async_work(env, asyncWork);
123         asyncWork = nullptr;
124     }
125 }
126 
NapiGetboolean(const napi_env & env,const bool isValue)127 napi_value Common::NapiGetboolean(const napi_env &env, const bool isValue)
128 {
129     napi_value result = nullptr;
130     NAPI_CALL(env, napi_get_boolean(env, isValue, &result));
131     return result;
132 }
133 
NapiGetNull(napi_env env)134 napi_value Common::NapiGetNull(napi_env env)
135 {
136     napi_value result = nullptr;
137     napi_get_null(env, &result);
138     return result;
139 }
140 
GetCallbackErrorValue(napi_env env,const int32_t errCode,const std::string errMsg)141 napi_value Common::GetCallbackErrorValue(napi_env env, const int32_t errCode, const std::string errMsg)
142 {
143     if (errCode == ERR_OK) {
144         return NapiGetNull(env);
145     }
146     napi_value error = nullptr;
147     napi_value eCode = nullptr;
148     napi_value eMsg = nullptr;
149     NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
150     NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
151         errMsg.length(), &eMsg));
152     NAPI_CALL(env, napi_create_object(env, &error));
153     NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode));
154     NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg));
155     return error;
156 }
157 
GetExpireCallbackValue(napi_env env,int32_t errCode,const napi_value & value)158 napi_value Common::GetExpireCallbackValue(napi_env env, int32_t errCode, const napi_value &value)
159 {
160     napi_value result = nullptr;
161     napi_value eCode = nullptr;
162     NAPI_CALL(env, napi_create_int32(env, errCode, &eCode));
163     NAPI_CALL(env, napi_create_object(env, &result));
164     NAPI_CALL(env, napi_set_named_property(env, result, "code", eCode));
165     NAPI_CALL(env, napi_set_named_property(env, result, "data", value));
166     return result;
167 }
168 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const napi_value & result)169 void Common::SetCallback(const napi_env &env, const napi_ref &callbackIn, const napi_value &result)
170 {
171     napi_value undefined = nullptr;
172     napi_get_undefined(env, &undefined);
173 
174     napi_value callback = nullptr;
175     napi_value resultout = nullptr;
176     napi_value res = nullptr;
177     res = GetExpireCallbackValue(env, 0, result);
178     napi_get_reference_value(env, callbackIn, &callback);
179     NAPI_CALL_RETURN_VOID(env,
180         napi_call_function(env, undefined, callback, EXPIRE_CALLBACK_PARAM_NUM, &res, &resultout));
181 }
182 
SetCallback(const napi_env & env,const napi_ref & callbackIn,const int32_t & errCode,const napi_value & result)183 void Common::SetCallback(
184     const napi_env &env, const napi_ref &callbackIn, const int32_t &errCode, const napi_value &result)
185 {
186     napi_value undefined = nullptr;
187     napi_get_undefined(env, &undefined);
188 
189     napi_value callback = nullptr;
190     napi_value resultout = nullptr;
191     napi_get_reference_value(env, callbackIn, &callback);
192     napi_value results[ASYNC_CALLBACK_PARAM_NUM] = {nullptr};
193     if (errCode == ERR_OK) {
194         results[0] = NapiGetNull(env);
195     } else {
196         int32_t errCodeInfo = FindErrCode(env, errCode);
197         std::string errMsg = FindErrMsg(env, errCode);
198         results[0] = GetCallbackErrorValue(env, errCodeInfo, errMsg);
199     }
200     results[1] = result;
201     NAPI_CALL_RETURN_VOID(env,
202         napi_call_function(env, undefined, callback, ASYNC_CALLBACK_PARAM_NUM, &results[0], &resultout));
203 }
204 
SetPromise(const napi_env & env,const AsyncWorkData & info,const napi_value & result)205 napi_value Common::SetPromise(
206     const napi_env &env, const AsyncWorkData &info, const napi_value &result)
207 {
208     if (info.errCode == ERR_OK) {
209         napi_resolve_deferred(env, info.deferred, result);
210     } else {
211         int32_t errCodeInfo = FindErrCode(env, info.errCode);
212         std::string errMsg = FindErrMsg(env, info.errCode);
213         napi_value error = nullptr;
214         napi_value eCode = nullptr;
215         napi_value eMsg = nullptr;
216         NAPI_CALL(env, napi_create_int32(env, errCodeInfo, &eCode));
217         NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
218             errMsg.length(), &eMsg));
219         NAPI_CALL(env, napi_create_object(env, &error));
220         NAPI_CALL(env, napi_set_named_property(env, error, "data", eCode));
221         NAPI_CALL(env, napi_set_named_property(env, error, "code", eCode));
222         NAPI_CALL(env, napi_set_named_property(env, error, "message", eMsg));
223         napi_reject_deferred(env, info.deferred, error);
224     }
225     return result;
226 }
227 
ReturnCallbackPromise(const napi_env & env,const AsyncWorkData & info,const napi_value & result)228 void Common::ReturnCallbackPromise(const napi_env &env, const AsyncWorkData &info, const napi_value &result)
229 {
230     if (info.isCallback) {
231         SetCallback(env, info.callback, info.errCode, result);
232     } else {
233         SetPromise(env, info, result);
234     }
235 }
236 
JSParaError(const napi_env & env,const napi_ref & callback)237 napi_value Common::JSParaError(const napi_env &env, const napi_ref &callback)
238 {
239     if (callback) {
240         SetCallback(env, callback, ERR_BGTASK_INVALID_PARAM, nullptr);
241         return Common::NapiGetNull(env);
242     } else {
243         napi_value promise = nullptr;
244         napi_deferred deferred = nullptr;
245         napi_create_promise(env, &deferred, &promise);
246 
247         napi_value res = nullptr;
248         napi_value eCode = nullptr;
249         napi_value eMsg = nullptr;
250         std::string errMsg = FindErrMsg(env, ERR_BGTASK_INVALID_PARAM);
251         NAPI_CALL(env, napi_create_int32(env, ERR_BGTASK_INVALID_PARAM, &eCode));
252         NAPI_CALL(env, napi_create_string_utf8(env, errMsg.c_str(),
253             errMsg.length(), &eMsg));
254         NAPI_CALL(env, napi_create_object(env, &res));
255         NAPI_CALL(env, napi_set_named_property(env, res, "data", eCode));
256         NAPI_CALL(env, napi_set_named_property(env, res, "code", eCode));
257         NAPI_CALL(env, napi_set_named_property(env, res, "message", eMsg));
258         napi_reject_deferred(env, deferred, res);
259         return promise;
260     }
261 }
262 
GetU16StringValue(const napi_env & env,const napi_value & value,std::u16string & result)263 napi_value Common::GetU16StringValue(const napi_env &env, const napi_value &value, std::u16string &result)
264 {
265     napi_valuetype valuetype = napi_undefined;
266 
267     NAPI_CALL(env, napi_typeof(env, value, &valuetype));
268     if (valuetype == napi_string) {
269         char str[STR_MAX_SIZE] = {0};
270         size_t strLen = 0;
271         NAPI_CALL(env, napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen));
272 
273         result = Str8ToStr16((std::string)str);
274         BGTASK_LOGD("GetU16StringValue result: %{public}s", Str16ToStr8(result).c_str());
275     } else {
276         BGTASK_LOGE("valuetype is not stringU16");
277         return nullptr;
278     }
279 
280     return Common::NapiGetNull(env);
281 }
282 
GetInt32NumberValue(const napi_env & env,const napi_value & value,int32_t & result)283 napi_value Common::GetInt32NumberValue(const napi_env &env, const napi_value &value, int32_t &result)
284 {
285     napi_valuetype valuetype = napi_undefined;
286     BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype));
287     if (valuetype != napi_number) {
288         BGTASK_LOGE("valuetype is not number");
289         return nullptr;
290     }
291     BGTASK_NAPI_CALL(env, napi_get_value_int32(env, value, &result));
292     BGTASK_LOGD("GetInt32NumberValue result: %{public}d", result);
293     return Common::NapiGetNull(env);
294 }
295 
PaddingAsyncWorkData(const napi_env & env,const napi_ref & callback,AsyncWorkData & info,napi_value & promise)296 void Common::PaddingAsyncWorkData(
297     const napi_env &env, const napi_ref &callback, AsyncWorkData &info, napi_value &promise)
298 {
299     if (callback) {
300         info.callback = callback;
301         info.isCallback = true;
302     } else {
303         napi_deferred deferred = nullptr;
304         NAPI_CALL_RETURN_VOID(env, napi_create_promise(env, &deferred, &promise));
305         info.deferred = deferred;
306         info.isCallback = false;
307     }
308 }
309 
SetDelaySuspendInfo(const napi_env & env,std::shared_ptr<DelaySuspendInfo> & delaySuspendInfo,napi_value & result)310 napi_value Common::SetDelaySuspendInfo(
311     const napi_env &env, std::shared_ptr<DelaySuspendInfo>& delaySuspendInfo, napi_value &result)
312 {
313     if (delaySuspendInfo == nullptr) {
314         BGTASK_LOGI("delaySuspendInfo is nullptr");
315         return NapiGetboolean(env, false);
316     }
317     napi_value value = nullptr;
318 
319     // readonly requestId?: number
320     napi_create_int32(env, delaySuspendInfo->GetRequestId(), &value);
321     napi_set_named_property(env, result, "requestId", value);
322 
323     // readonly actualDelayTime?: number
324     napi_create_int32(env, delaySuspendInfo->GetActualDelayTime(), &value);
325     napi_set_named_property(env, result, "actualDelayTime", value);
326 
327     return NapiGetboolean(env, true);
328 }
329 
GetStringValue(const napi_env & env,const napi_value & value,std::string & result)330 napi_value Common::GetStringValue(const napi_env &env, const napi_value &value, std::string &result)
331 {
332     napi_valuetype valuetype = napi_undefined;
333     BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype));
334     if (valuetype != napi_string) {
335         BGTASK_LOGE("valuetype is not string");
336         return nullptr;
337     }
338 
339     char str[STR_MAX_SIZE] = {0};
340     size_t strLen = 0;
341     napi_status status = napi_get_value_string_utf8(env, value, str, STR_MAX_SIZE - 1, &strLen);
342     if (status != napi_ok) {
343         BGTASK_LOGE("get value string utf8 failed");
344         return nullptr;
345     }
346     result = std::string(str);
347     BGTASK_LOGD("GetStringValue result: %{public}s", result.c_str());
348     return Common::NapiGetNull(env);
349 }
350 
HandleErrCode(const napi_env & env,int32_t errCode,bool isThrow)351 void Common::HandleErrCode(const napi_env &env, int32_t errCode, bool isThrow)
352 {
353     BGTASK_LOGD("HandleErrCode errCode = %{public}d, isThrow = %{public}d", errCode, isThrow);
354     if (!isThrow || errCode == ERR_OK) {
355         return;
356     }
357     std::string errMsg = FindErrMsg(env, errCode);
358     int32_t errCodeInfo = FindErrCode(env, errCode);
359     if (errMsg != "") {
360         napi_throw_error(env, std::to_string(errCodeInfo).c_str(), errMsg.c_str());
361     }
362 }
363 
HandleParamErr(const napi_env & env,int32_t errCode,bool isThrow)364 bool Common::HandleParamErr(const napi_env &env, int32_t errCode, bool isThrow)
365 {
366     BGTASK_LOGD("HandleParamErr errCode = %{public}d, isThrow = %{public}d", errCode, isThrow);
367     if (!isThrow || errCode == ERR_OK) {
368         return false;
369     }
370     auto iter = PARAM_ERRCODE_MSG_MAP.find(errCode);
371     if (iter != PARAM_ERRCODE_MSG_MAP.end()) {
372         std::string errMessage = "BussinessError 401: Parameter error. ";
373         errMessage.append(iter->second);
374         napi_throw_error(env, std::to_string(ERR_BGTASK_INVALID_PARAM).c_str(), errMessage.c_str());
375         return true;
376     }
377     return false;
378 }
379 
FindErrMsg(const napi_env & env,const int32_t errCode)380 std::string Common::FindErrMsg(const napi_env &env, const int32_t errCode)
381 {
382     if (errCode == ERR_OK) {
383         return "";
384     }
385     auto iter = SA_ERRCODE_MSG_MAP.find(errCode);
386     if (iter != SA_ERRCODE_MSG_MAP.end()) {
387         std::string errMessage = "BussinessError ";
388         int32_t errCodeInfo = FindErrCode(env, errCode);
389         errMessage.append(std::to_string(errCodeInfo)).append(": ").append(iter->second);
390         return errMessage;
391     }
392     iter = PARAM_ERRCODE_MSG_MAP.find(errCode);
393     if (iter != PARAM_ERRCODE_MSG_MAP.end()) {
394         std::string errMessage = "BussinessError 401: Parameter error. ";
395         errMessage.append(iter->second);
396         return errMessage;
397     }
398     return "Inner error.";
399 }
400 
FindErrCode(const napi_env & env,const int32_t errCodeIn)401 int32_t Common::FindErrCode(const napi_env &env, const int32_t errCodeIn)
402 {
403     auto iter = PARAM_ERRCODE_MSG_MAP.find(errCodeIn);
404     if (iter != PARAM_ERRCODE_MSG_MAP.end()) {
405         return ERR_BGTASK_INVALID_PARAM;
406     }
407     return errCodeIn > THRESHOLD ? errCodeIn / OFFSET : errCodeIn;
408 }
409 
GetBooleanValue(const napi_env & env,const napi_value & value,bool & result)410 napi_value Common::GetBooleanValue(const napi_env &env, const napi_value &value, bool &result)
411 {
412     napi_valuetype valuetype = napi_undefined;
413     BGTASK_NAPI_CALL(env, napi_typeof(env, value, &valuetype));
414     if (valuetype != napi_boolean) {
415         BGTASK_LOGE("valuetype is not boolean");
416         return nullptr;
417     }
418     BGTASK_NAPI_CALL(env, napi_get_value_bool(env, value, &result));
419     BGTASK_LOGD("GetBooleanValue result: %{public}d", result);
420 
421     return Common::NapiGetNull(env);
422 }
423 
NapiSetBgTaskMode(napi_env env,napi_value napiInfo,const std::shared_ptr<ContinuousTaskInfo> & continuousTaskInfo)424 napi_value Common::NapiSetBgTaskMode(napi_env env, napi_value napiInfo,
425     const std::shared_ptr<ContinuousTaskInfo> &continuousTaskInfo)
426 {
427     // backgroundmode
428     napi_value napiBackgroundModes = nullptr;
429     NAPI_CALL(env, napi_create_array(env, &napiBackgroundModes));
430     uint32_t count = 0;
431     for (auto mode : continuousTaskInfo->GetBackgroundModes()) {
432         if (mode < BackgroundMode::END) {
433             napi_value napiModeText = nullptr;
434             std::string modeStr = BackgroundMode::GetBackgroundModeStr(mode);
435             NAPI_CALL(env, napi_create_string_utf8(env, modeStr.c_str(), modeStr.length(), &napiModeText));
436             NAPI_CALL(env, napi_set_element(env, napiBackgroundModes, count, napiModeText));
437             count++;
438         }
439     }
440     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "backgroundModes", napiBackgroundModes));
441 
442     // backgroundsubmode
443     napi_value napiBackgroundSubModes = nullptr;
444     NAPI_CALL(env, napi_create_array(env, &napiBackgroundSubModes));
445     count = 0;
446     for (auto subMode : continuousTaskInfo->GetBackgroundSubModes()) {
447         if (subMode < BackgroundSubMode::END) {
448             napi_value napiSubModeText = nullptr;
449             std::string subModeStr = BackgroundSubMode::GetBackgroundSubModeStr(subMode);
450             NAPI_CALL(env, napi_create_string_utf8(env, subModeStr.c_str(), subModeStr.length(),
451                 &napiSubModeText));
452                 NAPI_CALL(env, napi_set_element(env, napiBackgroundSubModes, count, napiSubModeText));
453             count++;
454         }
455     }
456     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "backgroundSubModes", napiBackgroundSubModes));
457     return napiInfo;
458 }
459 
GetNapiContinuousTaskInfo(napi_env env,const std::shared_ptr<ContinuousTaskInfo> & continuousTaskInfo)460 napi_value Common::GetNapiContinuousTaskInfo(napi_env env,
461     const std::shared_ptr<ContinuousTaskInfo> &continuousTaskInfo)
462 {
463     if (continuousTaskInfo == nullptr) {
464         BGTASK_LOGE("continuousTaskInfo is null");
465         return NapiGetNull(env);
466     }
467     napi_value napiInfo = nullptr;
468     NAPI_CALL(env, napi_create_object(env, &napiInfo));
469 
470     // ability name
471     napi_value napiAbilityName = nullptr;
472     NAPI_CALL(env, napi_create_string_utf8(env, continuousTaskInfo->GetAbilityName().c_str(),
473         continuousTaskInfo->GetAbilityName().length(), &napiAbilityName));
474     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "abilityName", napiAbilityName));
475 
476     // uid
477     napi_value napiUid = nullptr;
478     NAPI_CALL(env, napi_create_int32(env, continuousTaskInfo->GetUid(), &napiUid));
479     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "uid", napiUid));
480 
481     // pid
482     napi_value napiPid = nullptr;
483     NAPI_CALL(env, napi_create_int32(env, continuousTaskInfo->GetPid(), &napiPid));
484     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "pid", napiPid));
485 
486     // Set isFromWebView.
487     napi_value napiIsFromWebView = nullptr;
488     NAPI_CALL(env, napi_get_boolean(env, continuousTaskInfo->IsFromWebView(), &napiIsFromWebView));
489     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "isFromWebView", napiIsFromWebView));
490 
491     if (NapiSetBgTaskMode(env, napiInfo, continuousTaskInfo) == nullptr) {
492         BGTASK_LOGE("set bavktask mode fail.");
493         return NapiGetNull(env);
494     }
495 
496     // notificationId
497     napi_value napiNotificationId = nullptr;
498     NAPI_CALL(env, napi_create_int32(env, continuousTaskInfo->GetNotificationId(), &napiNotificationId));
499     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "notificationId", napiNotificationId));
500 
501     // continuousTaskId
502     napi_value napiContinuousTaskId = nullptr;
503     NAPI_CALL(env, napi_create_int32(env, continuousTaskInfo->GetContinuousTaskId(), &napiContinuousTaskId));
504     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "continuousTaskId", napiContinuousTaskId));
505 
506     // abilityId
507     napi_value napiAbilityId = nullptr;
508     NAPI_CALL(env, napi_create_int32(env, continuousTaskInfo->GetAbilityId(), &napiAbilityId));
509     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "abilityId", napiAbilityId));
510 
511     // want agent bundle name
512     napi_value napiWantAgentBundleName = nullptr;
513     NAPI_CALL(env, napi_create_string_utf8(env, continuousTaskInfo->GetWantAgentBundleName().c_str(),
514         continuousTaskInfo->GetWantAgentBundleName().length(), &napiWantAgentBundleName));
515     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "wantAgentBundleName", napiWantAgentBundleName));
516 
517     // want agent ability name
518     napi_value napiWantAgentAbilityName = nullptr;
519     NAPI_CALL(env, napi_create_string_utf8(env, continuousTaskInfo->GetWantAgentAbilityName().c_str(),
520         continuousTaskInfo->GetWantAgentAbilityName().length(), &napiWantAgentAbilityName));
521     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "wantAgentAbilityName", napiWantAgentAbilityName));
522 
523     // suspend state.
524     napi_value napiSuspendState = nullptr;
525     NAPI_CALL(env, napi_get_boolean(env, continuousTaskInfo->GetSuspendState(), &napiSuspendState));
526     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "suspendState", napiSuspendState));
527     return napiInfo;
528 }
529 
GetNapiDelaySuspendInfo(napi_env env,const std::shared_ptr<DelaySuspendInfo> & delaySuspendInfo)530 napi_value Common::GetNapiDelaySuspendInfo(napi_env env,
531     const std::shared_ptr<DelaySuspendInfo> &delaySuspendInfo)
532 {
533     if (delaySuspendInfo == nullptr) {
534         BGTASK_LOGE("delaySuspendInfo is null");
535         return NapiGetNull(env);
536     }
537     napi_value napiInfo = nullptr;
538     NAPI_CALL(env, napi_create_object(env, &napiInfo));
539 
540     // requestId
541     napi_value napiRequestId = nullptr;
542     NAPI_CALL(env, napi_create_int32(env, delaySuspendInfo->GetRequestId(), &napiRequestId));
543     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "requestId", napiRequestId));
544 
545     // actualDelayTime
546     napi_value napiActualDelayTime = nullptr;
547     NAPI_CALL(env, napi_create_int32(env, delaySuspendInfo->GetActualDelayTime(), &napiActualDelayTime));
548     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "actualDelayTime", napiActualDelayTime));
549     return napiInfo;
550 }
551 
GetNapiEfficiencyResourcesInfo(const napi_env & env,std::shared_ptr<EfficiencyResourceInfo> efficiencyResourceInfo)552 napi_value Common::GetNapiEfficiencyResourcesInfo(const napi_env &env,
553     std::shared_ptr<EfficiencyResourceInfo> efficiencyResourceInfo)
554 {
555     if (efficiencyResourceInfo == nullptr) {
556         BGTASK_LOGE("efficiencyResourceInfo is null");
557         return NapiGetNull(env);
558     }
559     napi_value napiInfo = nullptr;
560     NAPI_CALL(env, napi_create_object(env, &napiInfo));
561 
562     napi_value napiResourceType = nullptr;
563     napi_value napiTimeOut = nullptr;
564     napi_value napiIsPersist = nullptr;
565     napi_value napiIsProcess = nullptr;
566     napi_value napiReason = nullptr;
567     napi_value napiUid = nullptr;
568     napi_value napiPid = nullptr;
569 
570     NAPI_CALL(env, napi_create_int32(env, efficiencyResourceInfo->GetResourceNumber(), &napiResourceType));
571     NAPI_CALL(env, napi_create_int32(env, efficiencyResourceInfo->GetTimeOut(), &napiTimeOut));
572     NAPI_CALL(env, napi_get_boolean(env, efficiencyResourceInfo->IsPersist(), &napiIsPersist));
573     NAPI_CALL(env, napi_get_boolean(env, efficiencyResourceInfo->IsProcess(), &napiIsProcess));
574     NAPI_CALL(env, napi_create_string_utf8(env, efficiencyResourceInfo->GetReason().c_str(),
575         efficiencyResourceInfo->GetReason().length(), &napiReason));
576     NAPI_CALL(env, napi_create_int32(env, efficiencyResourceInfo->GetUid(), &napiUid));
577     NAPI_CALL(env, napi_create_int32(env, efficiencyResourceInfo->GetPid(), &napiPid));
578 
579     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "resourceTypes", napiResourceType));
580     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "timeout", napiTimeOut));
581     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "isPersistent", napiIsPersist));
582     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "isForProcess", napiIsProcess));
583     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "reason", napiReason));
584     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "uid", napiUid));
585     NAPI_CALL(env, napi_set_named_property(env, napiInfo, "pid", napiPid));
586 
587     return napiInfo;
588 }
589 }  // namespace BackgroundTaskMgr
590 }  // namespace OHOS
591