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_create_reference(env, argv[CALLBACK_INDEX], 1, &callback);
49 }
50 return Common::NapiGetNull(env);
51 }
52
ObtainAllWorks(napi_env env,napi_callback_info info)53 napi_value ObtainAllWorks(napi_env env, napi_callback_info info)
54 {
55 WS_HILOGD("Obtain All Works napi begin.");
56
57 // Get params.
58 napi_ref callback = nullptr;
59 if (ParseParameters(env, info, callback) == nullptr) {
60 return Common::JSParaError(env, callback);
61 }
62
63 napi_value promise = nullptr;
64 AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo =
65 new (std::nothrow) AsyncCallbackInfoObtainAllWorks(env);
66 if (!asyncCallbackInfo) {
67 return Common::JSParaError(env, callback);
68 }
69 std::unique_ptr<AsyncCallbackInfoObtainAllWorks> callbackPtr {asyncCallbackInfo};
70 Common::PaddingAsyncWorkData(env, callback, *asyncCallbackInfo, promise);
71
72 napi_value resourceName = nullptr;
73 NAPI_CALL(env, napi_create_string_latin1(env, "ObtainAllWorks", NAPI_AUTO_LENGTH, &resourceName));
74
75 NAPI_CALL(env, napi_create_async_work(env,
76 nullptr,
77 resourceName,
78 [](napi_env env, void *data) {
79 AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo = static_cast<AsyncCallbackInfoObtainAllWorks *>(data);
80 asyncCallbackInfo->errorCode =
81 WorkSchedulerSrvClient::GetInstance().ObtainAllWorks(asyncCallbackInfo->workInfoList);
82 asyncCallbackInfo->errorMsg = Common::FindErrMsg(env, asyncCallbackInfo->errorCode);
83 },
84 [](napi_env env, napi_status status, void *data) {
85 AsyncCallbackInfoObtainAllWorks *asyncCallbackInfo = static_cast<AsyncCallbackInfoObtainAllWorks *>(data);
86 std::unique_ptr<AsyncCallbackInfoObtainAllWorks> callbackPtr {asyncCallbackInfo};
87 if (asyncCallbackInfo != nullptr) {
88 napi_value result = nullptr;
89 if (asyncCallbackInfo->errorCode != ERR_OK) {
90 result = Common::NapiGetNull(env);
91 } else {
92 napi_create_array(env, &result);
93 uint32_t count = 0;
94 for (auto workInfo : asyncCallbackInfo->workInfoList) {
95 napi_value napiWork = Common::GetNapiWorkInfo(env, workInfo);
96 napi_set_element(env, result, count, napiWork);
97 count++;
98 }
99 }
100 Common::ReturnCallbackPromise(env, *asyncCallbackInfo, result);
101 }
102 },
103 static_cast<AsyncCallbackInfoObtainAllWorks *>(asyncCallbackInfo),
104 &asyncCallbackInfo->asyncWork));
105
106 NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
107 callbackPtr.release();
108 WS_HILOGD("Obtain All Works napi end.");
109 if (asyncCallbackInfo->isCallback) {
110 return Common::NapiGetNull(env);
111 } else {
112 return promise;
113 }
114 }
115 } // namespace WorkScheduler
116 } // namespace OHOS