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 "stop_work.h"
16
17 #include "common.h"
18 #include "workscheduler_srv_client.h"
19 #include "work_sched_hilog.h"
20 #include "work_sched_errors.h"
21
22 namespace OHOS {
23 namespace WorkScheduler {
24 const uint32_t WORK_INFO_INDEX = 0;
25 const uint32_t NEED_CANCEL_INDEX = 1;
26 const uint32_t STOP_WORK_PARAMS = 2;
27
StopWork(napi_env env,napi_callback_info info)28 napi_value StopWork(napi_env env, napi_callback_info info)
29 {
30 WS_HILOGD("Stop Work napi begin.");
31
32 // Check params.
33 size_t argc = STOP_WORK_PARAMS;
34 napi_value argv[STOP_WORK_PARAMS] = {nullptr};
35 napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
36 if (argc != STOP_WORK_PARAMS) {
37 Common::HandleParamErr(env, E_PARAM_NUMBER_ERR);
38 return Common::NapiGetNull(env);
39 }
40 if (!Common::MatchValueType(env, argv[WORK_INFO_INDEX], napi_object)) {
41 Common::HandleParamErr(env, E_WORK_INFO_TYPE_ERR);
42 return Common::NapiGetNull(env);
43 }
44 if (!Common::MatchValueType(env, argv[NEED_CANCEL_INDEX], napi_boolean)) {
45 Common::HandleParamErr(env, E_NEED_CANCLE_TYPE_ERR);
46 return Common::NapiGetNull(env);
47 }
48
49 // get params
50 bool needCancel = false;
51 napi_get_value_bool(env, argv[NEED_CANCEL_INDEX], &needCancel);
52
53 // Check workInfo and call service.
54 WorkInfo workInfo = WorkInfo();
55 ErrCode errCode = E_WORK_INFO_TYPE_ERR;
56 if (Common::GetWorkInfo(env, argv[WORK_INFO_INDEX], workInfo)) {
57 if (needCancel) {
58 errCode = WorkSchedulerSrvClient::GetInstance().StopAndCancelWork(workInfo);
59 } else {
60 errCode = WorkSchedulerSrvClient::GetInstance().StopWork(workInfo);
61 }
62 }
63 Common::HandleErrCode(env, errCode);
64 WS_HILOGD("Stop Work napi end.");
65 return Common::NapiGetNull(env);
66 }
67 } // namespace WorkScheduler
68 } // namespace OHOS