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 "get_all_transient_tasks.h"
17
18 #include "singleton.h"
19
20 #include "background_task_manager.h"
21 #include "hitrace_meter.h"
22 #include "transient_task_log.h"
23
24 namespace OHOS {
25 namespace BackgroundTaskMgr {
26 struct AsyncCallbackInfoGetAllTransientTasks : public AsyncWorkData {
AsyncCallbackInfoGetAllTransientTasksOHOS::BackgroundTaskMgr::AsyncCallbackInfoGetAllTransientTasks27 explicit AsyncCallbackInfoGetAllTransientTasks(napi_env env) : AsyncWorkData(env) {}
28 int32_t remainingQuota = 0; // out
29 std::vector<std::shared_ptr<DelaySuspendInfo>> list; // out
30 };
31
GetAllTransientTasksExecuteCB(napi_env env,void * data)32 void GetAllTransientTasksExecuteCB(napi_env env, void *data)
33 {
34 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
35 "BackgroundTaskManager::ContinuousTask::Napi::GetAllContinuousTasksExecuteCB");
36 AsyncCallbackInfoGetAllTransientTasks *asyncCallbackInfo =
37 static_cast<AsyncCallbackInfoGetAllTransientTasks *>(data);
38 if (asyncCallbackInfo == nullptr || asyncCallbackInfo->errCode != ERR_OK) {
39 BGTASK_LOGE("input params error");
40 return;
41 }
42 asyncCallbackInfo->errCode = DelayedSingleton<BackgroundTaskManager>::GetInstance()->GetAllTransientTasks(
43 asyncCallbackInfo->remainingQuota, asyncCallbackInfo->list);
44 }
45
GetAllTransientTasksPromiseCompletedCB(napi_env env,napi_status status,void * data)46 void GetAllTransientTasksPromiseCompletedCB(napi_env env, napi_status status, void *data)
47 {
48 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
49 "BackgroundTaskManager::ContinuousTask::Napi::GetAllTransientTasksPromiseCompletedCB");
50 AsyncCallbackInfoGetAllTransientTasks *asyncCallbackInfo =
51 static_cast<AsyncCallbackInfoGetAllTransientTasks *>(data);
52 std::unique_ptr<AsyncCallbackInfoGetAllTransientTasks> callbackPtr {asyncCallbackInfo};
53 napi_value result {nullptr};
54 if (asyncCallbackInfo->errCode == ERR_OK) {
55 NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &result));
56 napi_value napiRemainingQuota = nullptr;
57 NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, asyncCallbackInfo->remainingQuota, &napiRemainingQuota));
58 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "remainingQuota", napiRemainingQuota));
59
60 napi_value info {nullptr};
61 NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &info));
62 if (asyncCallbackInfo->list.size() > 0) {
63 uint32_t count = 0;
64 for (const auto &delaySuspendInfo : asyncCallbackInfo->list) {
65 napi_value napiWork = Common::GetNapiDelaySuspendInfo(env, delaySuspendInfo);
66 NAPI_CALL_RETURN_VOID(env, napi_set_element(env, info, count, napiWork));
67 count++;
68 }
69 }
70 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, result, "transientTasks", info));
71 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result));
72 } else {
73 std::string errMsg = Common::FindErrMsg(env, asyncCallbackInfo->errCode);
74 int32_t errCodeInfo = Common::FindErrCode(env, asyncCallbackInfo->errCode);
75 result = Common::GetCallbackErrorValue(env, errCodeInfo, errMsg);
76 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result));
77 }
78 callbackPtr.release();
79 }
80
GetAllTransientTasksPromise(napi_env env,AsyncCallbackInfoGetAllTransientTasks * asyncCallbackInfo,bool isThrow)81 napi_value GetAllTransientTasksPromise(napi_env env, AsyncCallbackInfoGetAllTransientTasks *asyncCallbackInfo,
82 bool isThrow)
83 {
84 if (asyncCallbackInfo == nullptr) {
85 BGTASK_LOGE("param is nullptr");
86 return nullptr;
87 }
88 napi_value resourceName;
89 NAPI_CALL(env, napi_create_string_latin1(env, __func__, NAPI_AUTO_LENGTH, &resourceName));
90 napi_deferred deferred;
91 napi_value promise {nullptr};
92 NAPI_CALL(env, napi_create_promise(env, &deferred, &promise));
93 asyncCallbackInfo->deferred = deferred;
94 NAPI_CALL(env, napi_create_async_work(env,
95 nullptr,
96 resourceName,
97 GetAllTransientTasksExecuteCB,
98 GetAllTransientTasksPromiseCompletedCB,
99 static_cast<void *>(asyncCallbackInfo),
100 &asyncCallbackInfo->asyncWork));
101 NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
102 return promise;
103 }
104
GetAllTransientTasks(napi_env env,napi_callback_info info,bool isThrow)105 napi_value GetAllTransientTasks(napi_env env, napi_callback_info info, bool isThrow)
106 {
107 HitraceScoped traceScoped(HITRACE_TAG_OHOS,
108 "BackgroundTaskManager::ContinuousTask::Napi::GetAllTransientTasks");
109 if (env == nullptr) {
110 BGTASK_LOGE("env param invaild.");
111 return Common::NapiGetNull(env);
112 }
113 AsyncCallbackInfoGetAllTransientTasks *asyncCallbackInfo =
114 new (std::nothrow) AsyncCallbackInfoGetAllTransientTasks(env);
115 if (asyncCallbackInfo == nullptr) {
116 BGTASK_LOGE("asyncCallbackInfo == nullpter");
117 return Common::NapiGetNull(env);
118 }
119 std::unique_ptr<AsyncCallbackInfoGetAllTransientTasks> callbackPtr {asyncCallbackInfo};
120
121 napi_value ret {nullptr};
122 ret = GetAllTransientTasksPromise(env, asyncCallbackInfo, isThrow);
123 callbackPtr.release();
124 if (ret == nullptr) {
125 BGTASK_LOGE("ret is nullpter");
126 if (asyncCallbackInfo != nullptr) {
127 delete asyncCallbackInfo;
128 asyncCallbackInfo = nullptr;
129 }
130 return Common::NapiGetNull(env);
131 }
132 return ret;
133 }
134
GetAllTransientTasksThrow(napi_env env,napi_callback_info info)135 napi_value GetAllTransientTasksThrow(napi_env env, napi_callback_info info)
136 {
137 return GetAllTransientTasks(env, info, true);
138 }
139 } // namespace BackgroundTaskMgr
140 } // namespace OHOS