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 #define MLOG_TAG "PickerNapiUtils"
16
17 #include "picker_napi_utils.h"
18 #include "ipc_skeleton.h"
19 #include "picker_n_exporter.h"
20
21 using namespace std;
22
23 namespace OHOS {
24 namespace Picker {
25 static const string EMPTY_STRING = "";
26
CreateNapiErrorObject(napi_env env,napi_value & errorObj,const int32_t errCode,const string errMsg)27 void PickerNapiUtils::CreateNapiErrorObject(napi_env env, napi_value &errorObj, const int32_t errCode,
28 const string errMsg)
29 {
30 napi_status statusError;
31 napi_value napiErrorCode = nullptr;
32 napi_value napiErrorMsg = nullptr;
33 statusError = napi_create_string_utf8(env, to_string(errCode).c_str(), NAPI_AUTO_LENGTH, &napiErrorCode);
34 if (statusError == napi_ok) {
35 statusError = napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &napiErrorMsg);
36 if (statusError == napi_ok) {
37 statusError = napi_create_error(env, napiErrorCode, napiErrorMsg, &errorObj);
38 if (statusError == napi_ok) {
39 HILOG_DEBUG("napi_create_error success");
40 }
41 }
42 }
43 }
44
InvokeJSAsyncMethod(napi_env env,napi_deferred deferred,napi_ref callbackRef,napi_async_work work,const JSAsyncContextOutput & asyncContext)45 void PickerNapiUtils::InvokeJSAsyncMethod(napi_env env, napi_deferred deferred, napi_ref callbackRef,
46 napi_async_work work, const JSAsyncContextOutput &asyncContext)
47 {
48 HILOG_INFO("modal picker: InvokeJSAsyncMethod begin.");
49 if (asyncContext.status) {
50 napi_resolve_deferred(env, deferred, asyncContext.data);
51 } else {
52 napi_reject_deferred(env, deferred, asyncContext.error);
53 }
54 napi_delete_async_work(env, work);
55 }
56
57 template <class AsyncContext>
NapiCreateAsyncWork(napi_env env,unique_ptr<AsyncContext> & asyncContext,const string & resourceName,void (* execute)(napi_env,void *),void (* complete)(napi_env,napi_status,void *))58 napi_value PickerNapiUtils::NapiCreateAsyncWork(napi_env env, unique_ptr<AsyncContext> &asyncContext,
59 const string &resourceName, void (*execute)(napi_env, void *), void (*complete)(napi_env, napi_status, void *))
60 {
61 napi_value result = nullptr;
62 napi_value resource = nullptr;
63 napi_create_promise(env, &(asyncContext->deferred), &(result));
64 napi_create_string_utf8(env, resourceName.c_str(), NAPI_AUTO_LENGTH, &(resource));
65 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, execute, complete,
66 static_cast<void *>(asyncContext.get()), &asyncContext->work));
67 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
68 asyncContext.release();
69
70 return result;
71 }
72
73 template napi_value PickerNapiUtils::NapiCreateAsyncWork<PickerAsyncContext>(napi_env env,
74 unique_ptr<PickerAsyncContext> &asyncContext, const string &resourceName,
75 void (*execute)(napi_env, void *), void (*complete)(napi_env, napi_status, void *));
76 } // namespace Picker
77 } // namespace OHOS
78