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 &params)
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], &params.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, &params.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         return Common::JSParaError(env, params.callback);
80     }
81 
82     napi_value promise = nullptr;
83     AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo =
84         new (std::nothrow) AsyncCallbackInfoGetWorkStatus(env);
85     if (!asyncCallbackInfo) {
86         return Common::JSParaError(env, params.callback);
87     }
88     std::unique_ptr<AsyncCallbackInfoGetWorkStatus> callbackPtr {asyncCallbackInfo};
89     asyncCallbackInfo->workId = params.workId;
90     WS_HILOGD("asyncCallbackInfo->workId: %{public}d", asyncCallbackInfo->workId);
91     Common::PaddingAsyncWorkData(env, params.callback, *asyncCallbackInfo, promise);
92 
93     napi_value resourceName = nullptr;
94     NAPI_CALL(env, napi_create_string_latin1(env, "GetWorkStatus", NAPI_AUTO_LENGTH, &resourceName));
95 
96     NAPI_CALL(env, napi_create_async_work(env,
97         nullptr,
98         resourceName,
99         [](napi_env env, void *data) {
100             AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo = static_cast<AsyncCallbackInfoGetWorkStatus *>(data);
101             asyncCallbackInfo->errorCode =
102                 WorkSchedulerSrvClient::GetInstance().GetWorkStatus(asyncCallbackInfo->workId,
103                 asyncCallbackInfo->workInfo);
104             asyncCallbackInfo->errorMsg = Common::FindErrMsg(env, asyncCallbackInfo->errorCode);
105         },
106         [](napi_env env, napi_status status, void *data) {
107             AsyncCallbackInfoGetWorkStatus *asyncCallbackInfo = static_cast<AsyncCallbackInfoGetWorkStatus *>(data);
108             std::unique_ptr<AsyncCallbackInfoGetWorkStatus> callbackPtr {asyncCallbackInfo};
109             if (asyncCallbackInfo != nullptr) {
110                 napi_value result = Common::GetNapiWorkInfo(env, asyncCallbackInfo->workInfo);
111                 WS_HILOGD("asyncCallbackInfo->errorCode = %{public}d", asyncCallbackInfo->errorCode);
112                 Common::ReturnCallbackPromise(env, *asyncCallbackInfo, result);
113             }
114         },
115         static_cast<AsyncCallbackInfoGetWorkStatus *>(asyncCallbackInfo),
116         &asyncCallbackInfo->asyncWork));
117 
118     NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
119     callbackPtr.release();
120 
121     WS_HILOGD("Get work status napi end.");
122     if (asyncCallbackInfo->isCallback) {
123         return Common::NapiGetNull(env);
124     } else {
125         return promise;
126     }
127 }
128 } // namespace WorkScheduler
129 } // namespace OHOS