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