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 "start_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 START_WORK_PARAMS = 1;
26 
StartWork(napi_env env,napi_callback_info info)27 napi_value StartWork(napi_env env, napi_callback_info info)
28 {
29     WS_HILOGD("Start work napi begin.");
30 
31     // Check params.
32     size_t argc = START_WORK_PARAMS;
33     napi_value argv[START_WORK_PARAMS] = {nullptr};
34     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
35     if (argc != START_WORK_PARAMS) {
36         Common::HandleParamErr(env, E_PARAM_NUMBER_ERR);
37         return Common::NapiGetNull(env);
38     }
39     if (!Common::MatchValueType(env, argv[WORK_INFO_INDEX], napi_object)) {
40         Common::HandleParamErr(env, E_WORK_INFO_TYPE_ERR);
41         return Common::NapiGetNull(env);
42     }
43 
44     // Get workInfo and call service.
45     WorkInfo workInfo = WorkInfo();
46     if (Common::GetWorkInfo(env, argv[WORK_INFO_INDEX], workInfo)) {
47         ErrCode errCode = WorkSchedulerSrvClient::GetInstance().StartWork(workInfo);
48         Common::HandleErrCode(env, errCode);
49     }
50     WS_HILOGD("Start work napi end.");
51     return Common::NapiGetNull(env);
52 }
53 } // namespace WorkScheduler
54 } // namespace OHOS
55