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