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 #include "get_work_status.h"
16
17 #include <new>
18
19 #include "common.h"
20 #include "workscheduler_srv_client.h"
21 #include "work_sched_hilog.h"
22 #include "work_sched_errors.h"
23
24 namespace OHOS {
25 namespace WorkScheduler {
26 const uint32_t WORK_ID_INDEX = 0;
27 const uint32_t CALLBACK_INDEX = 1;
28 const uint32_t GET_WORK_STATUS_MIN_PARAMS = 1;
29 const uint32_t GET_WORK_STATUS_MAX_PARAMS = 2;
30
31 struct GetWorkStatusParamsInfo {
32 int32_t workId = -1;
33 napi_ref callback = nullptr;
34 };
35
36 struct AsyncCallbackInfoGetWorkStatus : public AsyncWorkData {
AsyncCallbackInfoGetWorkStatusOHOS::WorkScheduler::AsyncCallbackInfoGetWorkStatus37 explicit AsyncCallbackInfoGetWorkStatus(napi_env env) : AsyncWorkData(env) {}
38 int32_t workId {-1};
39 std::shared_ptr<WorkInfo> workInfo {nullptr};
40 };
41
ParseParameters(const napi_env & env,const napi_callback_info & info,GetWorkStatusParamsInfo & params)42 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetWorkStatusParamsInfo ¶ms)
43 {
44 size_t argc = GET_WORK_STATUS_MAX_PARAMS;
45 napi_value argv[GET_WORK_STATUS_MAX_PARAMS] = {nullptr};
46 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
47 if (argc != GET_WORK_STATUS_MIN_PARAMS && argc != GET_WORK_STATUS_MAX_PARAMS) {
48 Common::HandleParamErr(env, E_PARAM_NUMBER_ERR);
49 return nullptr;
50 }
51
52 // argv[0] : workId
53 if (!Common::MatchValueType(env, argv[WORK_ID_INDEX], napi_number)) {
54 Common::HandleParamErr(env, E_WORKID_ERR);
55 return nullptr;
56 }
57 napi_get_value_int32(env, argv[WORK_ID_INDEX], ¶ms.workId);
58
59 // argv[1]: callback
60 if (argc == GET_WORK_STATUS_MAX_PARAMS) {
61 napi_valuetype valuetype = napi_undefined;
62 NAPI_CALL(env, napi_typeof(env, argv[CALLBACK_INDEX], &valuetype));
63 if (valuetype != napi_function) {
64 Common::HandleParamErr(env, E_CALLBACK_TYPE_ERR);
65 return nullptr;
66 }
67 napi_create_reference(env, argv[CALLBACK_INDEX], 1, ¶ms.callback);
68 }
69 return Common::NapiGetNull(env);
70 }
71
GetWorkStatus(napi_env env,napi_callback_info info)72 napi_value GetWorkStatus(napi_env env, napi_callback_info info)
73 {
74 WS_HILOGD("Get work status napi begin.");
75
76 // Get params.
77 GetWorkStatusParamsInfo params;
78 if (ParseParameters(env, info, params) == nullptr) {
79 napi_value ret = Common::JSParaError(env, params.callback);
80 napi_delete_reference(env, params.callback);
81 return ret;
82 }
83
84 napi_value promise = nullptr;
85 AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo =
86 new (std::nothrow) AsyncCallbackInfoGetWorkStatus(env);
87 if (!asyncCallbackInfo) {
88 napi_value ret = Common::JSParaError(env, params.callback);
89 napi_delete_reference(env, params.callback);
90 return ret;
91 }
92 std::unique_ptr<AsyncCallbackInfoGetWorkStatus> callbackPtr {asyncCallbackInfo};
93 asyncCallbackInfo->workId = params.workId;
94 WS_HILOGD("asyncCallbackInfo->workId: %{public}d", asyncCallbackInfo->workId);
95 Common::PaddingAsyncWorkData(env, params.callback, *asyncCallbackInfo, promise);
96
97 napi_value resourceName = nullptr;
98 NAPI_CALL(env, napi_create_string_latin1(env, "GetWorkStatus", NAPI_AUTO_LENGTH, &resourceName));
99
100 NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
101 [](napi_env env, void *data) {
102 AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo = static_cast<AsyncCallbackInfoGetWorkStatus *>(data);
103 asyncCallbackInfo->errorCode =
104 WorkSchedulerSrvClient::GetInstance().GetWorkStatus(asyncCallbackInfo->workId,
105 asyncCallbackInfo->workInfo);
106 asyncCallbackInfo->errorMsg = Common::FindErrMsg(env, asyncCallbackInfo->errorCode);
107 },
108 [](napi_env env, napi_status status, void *data) {
109 AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo = static_cast<AsyncCallbackInfoGetWorkStatus *>(data);
110 std::unique_ptr<AsyncCallbackInfoGetWorkStatus> callbackPtr {asyncCallbackInfo};
111 if (asyncCallbackInfo != nullptr) {
112 napi_value result = Common::GetNapiWorkInfo(env, asyncCallbackInfo->workInfo);
113 WS_HILOGD("asyncCallbackInfo->errorCode = %{public}d", asyncCallbackInfo->errorCode);
114 Common::ReturnCallbackPromise(env, *asyncCallbackInfo, result);
115 }
116 },
117 static_cast<AsyncCallbackInfoGetWorkStatus *>(asyncCallbackInfo),
118 &asyncCallbackInfo->asyncWork));
119
120 NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
121 callbackPtr.release();
122
123 WS_HILOGD("Get work status napi end.");
124 if (asyncCallbackInfo->isCallback) {
125 return Common::NapiGetNull(env);
126 } else {
127 return promise;
128 }
129 }
130 } // namespace WorkScheduler
131 } // namespace OHOS