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 < NEED_CANCEL_INDEX || 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 
45     // get params
46     bool needCancel = false;
47     napi_get_value_bool(env, argv[NEED_CANCEL_INDEX], &needCancel);
48 
49     // Check workInfo and call service.
50     WorkInfo workInfo = WorkInfo();
51     ErrCode errCode = E_WORK_INFO_TYPE_ERR;
52     if (Common::GetWorkInfo(env, argv[WORK_INFO_INDEX], workInfo)) {
53         if (needCancel) {
54             errCode = WorkSchedulerSrvClient::GetInstance().StopAndCancelWork(workInfo);
55         } else {
56             errCode = WorkSchedulerSrvClient::GetInstance().StopWork(workInfo);
57         }
58     }
59     Common::HandleErrCode(env, errCode);
60     WS_HILOGD("Stop Work napi end.");
61     return Common::NapiGetNull(env);
62 }
63 } // namespace WorkScheduler
64 } // namespace OHOS