• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "obtain_all_works.h"
16 
17 #include <list>
18 #include <new>
19 
20 #include "common.h"
21 #include "workscheduler_srv_client.h"
22 #include "work_sched_hilog.h"
23 #include "work_sched_errors.h"
24 
25 namespace OHOS {
26 namespace WorkScheduler {
27 const uint32_t CALLBACK_INDEX = 0;
28 const uint32_t OBTAIN_ALL_WORKS_MIN_PARAMS = 0;
29 const uint32_t OBTAIN_ALL_WORKS_MAX_PARAMS = 1;
30 
31 struct AsyncCallbackInfoObtainAllWorks : public AsyncWorkData {
AsyncCallbackInfoObtainAllWorksOHOS::WorkScheduler::AsyncCallbackInfoObtainAllWorks32     explicit AsyncCallbackInfoObtainAllWorks(napi_env env) : AsyncWorkData(env) {}
33     std::list<std::shared_ptr<WorkInfo>> workInfoList;
34 };
35 
ParseParameters(const napi_env & env,const napi_callback_info & info,napi_ref & callback)36 napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_ref &callback)
37 {
38     size_t argc = OBTAIN_ALL_WORKS_MAX_PARAMS;
39     napi_value argv[OBTAIN_ALL_WORKS_MAX_PARAMS] = {nullptr};
40     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
41     if (argc != OBTAIN_ALL_WORKS_MIN_PARAMS && argc != OBTAIN_ALL_WORKS_MAX_PARAMS) {
42         Common::HandleParamErr(env, E_PARAM_NUMBER_ERR);
43         return nullptr;
44     }
45 
46     // argv[0]: callback
47     if (argc == OBTAIN_ALL_WORKS_MAX_PARAMS) {
48         napi_valuetype valuetype = napi_undefined;
49         NAPI_CALL(env, napi_typeof(env, argv[CALLBACK_INDEX], &valuetype));
50         if (valuetype != napi_function) {
51             Common::HandleParamErr(env, E_CALLBACK_TYPE_ERR);
52             return nullptr;
53         }
54         napi_create_reference(env, argv[CALLBACK_INDEX], 1, &callback);
55     }
56     return Common::NapiGetNull(env);
57 }
58 
ObtainAllWorks(napi_env env,napi_callback_info info)59 napi_value ObtainAllWorks(napi_env env, napi_callback_info info)
60 {
61     WS_HILOGD("Obtain All Works napi begin.");
62 
63     // Get params.
64     napi_ref callback = nullptr;
65     if (ParseParameters(env, info, callback) == nullptr) {
66         napi_value ret = Common::JSParaError(env, callback);
67         napi_delete_reference(env, callback);
68         return ret;
69     }
70 
71     napi_value promise = nullptr;
72     AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo = new (std::nothrow) AsyncCallbackInfoObtainAllWorks(env);
73     if (!asyncCallbackInfo) {
74         napi_value ret = Common::JSParaError(env, callback);
75         napi_delete_reference(env, callback);
76         return ret;
77     }
78     std::unique_ptr<AsyncCallbackInfoObtainAllWorks> callbackPtr {asyncCallbackInfo};
79     Common::PaddingAsyncWorkData(env, callback, *asyncCallbackInfo, promise);
80 
81     napi_value resourceName = nullptr;
82     NAPI_CALL(env, napi_create_string_latin1(env, "ObtainAllWorks", NAPI_AUTO_LENGTH, &resourceName));
83 
84     NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName,
85         [](napi_env env, void *data) {
86             AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo = static_cast<AsyncCallbackInfoObtainAllWorks *>(data);
87             asyncCallbackInfo->errorCode =
88                 WorkSchedulerSrvClient::GetInstance().ObtainAllWorks(asyncCallbackInfo->workInfoList);
89             asyncCallbackInfo->errorMsg = Common::FindErrMsg(env, asyncCallbackInfo->errorCode);
90         },
91         [](napi_env env, napi_status status, void *data) {
92             AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo = static_cast<AsyncCallbackInfoObtainAllWorks *>(data);
93             std::unique_ptr<AsyncCallbackInfoObtainAllWorks> callbackPtr {asyncCallbackInfo};
94             if (asyncCallbackInfo != nullptr) {
95                 napi_value result = nullptr;
96                 if (asyncCallbackInfo->errorCode != ERR_OK) {
97                     result = Common::NapiGetNull(env);
98                 } else {
99                     napi_create_array(env, &result);
100                     uint32_t count = 0;
101                     for (auto workInfo : asyncCallbackInfo->workInfoList) {
102                         napi_value napiWork = Common::GetNapiWorkInfo(env, workInfo);
103                         napi_set_element(env, result, count, napiWork);
104                         count++;
105                     }
106                 }
107                 Common::ReturnCallbackPromise(env, *asyncCallbackInfo, result);
108             }
109         }, static_cast<AsyncCallbackInfoObtainAllWorks *>(asyncCallbackInfo), &asyncCallbackInfo->asyncWork));
110 
111     NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
112     callbackPtr.release();
113     WS_HILOGD("Obtain All Works napi end.");
114     if (asyncCallbackInfo->isCallback) {
115         return Common::NapiGetNull(env);
116     } else {
117         return promise;
118     }
119 }
120 } // namespace WorkScheduler
121 } // namespace OHOS