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