1 /*
2  * Copyright (c) 2024 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 
16 #include "napi_async_work_callback.h"
17 
18 #include "app_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace LIBZIP {
23 using namespace std;
24 
NapiAsyncWorkCallback(napi_env env,NapiValue thisPtr,NapiValue cb)25 NapiAsyncWorkCallback::NapiAsyncWorkCallback(napi_env env, NapiValue thisPtr, NapiValue cb) : NapiAsyncWorkFactory(env)
26 {
27     ctx_ = new NAsyncContextCallback(thisPtr, cb);
28 }
29 
~NapiAsyncWorkCallback()30 NapiAsyncWorkCallback::~NapiAsyncWorkCallback()
31 {
32     if (ctx_ != nullptr) {
33         delete ctx_;
34         ctx_ = nullptr;
35     }
36 }
37 
CallbackExecute(napi_env env,void * data)38 static void CallbackExecute(napi_env env, void *data)
39 {
40     auto ctx = static_cast<NAsyncContextCallback *>(data);
41     if (ctx != nullptr && ctx->cbExec_ != nullptr) {
42         ctx->err_ = ctx->cbExec_(env);
43     }
44 }
45 
CallbackComplete(napi_env env,napi_status status,void * data)46 static void CallbackComplete(napi_env env, napi_status status, void *data)
47 {
48     napi_handle_scope scope = nullptr;
49     napi_open_handle_scope(env, &scope);
50     auto ctx = static_cast<NAsyncContextCallback *>(data);
51     if (ctx == nullptr) {
52         napi_close_handle_scope(env, scope);
53         return;
54     }
55 
56     if (ctx->cbComplete_ != nullptr) {
57         ctx->res_ = ctx->cbComplete_(env, ctx->err_);
58         ctx->cbComplete_ = nullptr;
59     }
60 
61     vector<napi_value> argv;
62     if (!ctx->res_.TypeIsError(true)) {
63         argv = {NapiBusinessError(ERRNO_NOERR).GetNapiErr(env), ctx->res_.val_};
64     } else {
65         argv = {ctx->res_.val_};
66     }
67 
68     napi_value global = nullptr;
69     napi_value callback = ctx->cb_.Deref(env).val_;
70     napi_value tmp = nullptr;
71     napi_get_global(env, &global);
72     napi_status stat = napi_call_function(env, global, callback, argv.size(), argv.data(), &tmp);
73     if (stat != napi_ok) {
74         APP_LOGE("Failed to call function for %{public}d", stat);
75     }
76 
77     napi_close_handle_scope(env, scope);
78     napi_delete_async_work(env, ctx->awork_);
79     delete ctx;
80 }
81 
Schedule(string procedureName,NContextCBExec cbExec,NContextCBComplete cbComplete)82 NapiValue NapiAsyncWorkCallback::Schedule(string procedureName, NContextCBExec cbExec, NContextCBComplete cbComplete)
83 {
84     if (!ctx_->cb_ || !ctx_->cb_.Deref(env_).TypeIs(napi_function)) {
85         APP_LOGE("The callback shall be a function");
86         NapiBusinessError(EINVAL, true).ThrowErr(env_);
87         return NapiValue();
88     }
89 
90     ctx_->cbExec_ = move(cbExec);
91     ctx_->cbComplete_ = move(cbComplete);
92     napi_value resource = NapiValue::CreateUTF8String(env_, procedureName).val_;
93     napi_status status =
94         napi_create_async_work(env_, nullptr, resource, CallbackExecute, CallbackComplete, ctx_, &ctx_->awork_);
95     if (status != napi_ok) {
96         APP_LOGE("Failed to create async work for %{public}d", status);
97         return NapiValue();
98     }
99 
100     status = napi_queue_async_work(env_, ctx_->awork_);
101     if (status != napi_ok) {
102         APP_LOGE("Failed to queue async work for %{public}d", status);
103         return NapiValue();
104     }
105 
106     ctx_ = nullptr;  // The ownership of ctx_ has been transferred
107     return NapiValue::CreateUndefined(env_);
108 }
109 }  // namespace LIBZIP
110 }  // namespace AppExecFwk
111 }  // namespace OHOS