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
16 #include "js_util.h"
17
18 #include "mmi_log.h"
19 #include "napi_constants.h"
20 #include "util_napi_error.h"
21 #include "util_napi.h"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "JsUtil" };
27 } // namespace
28
~CallbackInfo()29 JsUtil::CallbackInfo::~CallbackInfo()
30 {
31 CALL_DEBUG_ENTER;
32 if (ref != nullptr && env != nullptr) {
33 CHKRV(env, napi_delete_reference(env, ref), DELETE_REFERENCE);
34 env = nullptr;
35 }
36 }
37
GetEnableInfo(const std::unique_ptr<CallbackInfo> & cb)38 napi_value JsUtil::GetEnableInfo(const std::unique_ptr<CallbackInfo> &cb)
39 {
40 CHKPP(cb);
41 CHKPP(cb->env);
42 return GetResult(cb->env, cb->data.enableResult, cb->data.errCode);
43 }
44
GetStartInfo(const std::unique_ptr<CallbackInfo> & cb)45 napi_value JsUtil::GetStartInfo(const std::unique_ptr<CallbackInfo> &cb)
46 {
47 CHKPP(cb);
48 CHKPP(cb->env);
49 return GetResult(cb->env, cb->data.startResult, cb->data.errCode);
50 }
51
GetStopInfo(const std::unique_ptr<CallbackInfo> & cb)52 napi_value JsUtil::GetStopInfo(const std::unique_ptr<CallbackInfo> &cb)
53 {
54 CHKPP(cb);
55 CHKPP(cb->env);
56 return GetResult(cb->env, cb->data.stopResult, cb->data.errCode);
57 }
58
GetStateInfo(const std::unique_ptr<CallbackInfo> & cb)59 napi_value JsUtil::GetStateInfo(const std::unique_ptr<CallbackInfo> &cb)
60 {
61 CHKPP(cb);
62 CHKPP(cb->env);
63 napi_value ret = nullptr;
64 napi_value state = nullptr;
65 CHKRP(cb->env, napi_create_int32(cb->env, cb->data.cooperateOpened ? 1 : 0, &ret),
66 CREATE_INT32);
67 CHKRP(cb->env, napi_coerce_to_bool(cb->env, ret, &state), COERCE_TO_BOOL);
68 return state;
69 }
70
GetResult(napi_env env,bool result,int32_t errCode)71 napi_value JsUtil::GetResult(napi_env env, bool result, int32_t errCode)
72 {
73 CHKPP(env);
74 napi_value object = nullptr;
75 if (result) {
76 napi_get_undefined(env, &object);
77 } else {
78 NapiError napiError;
79 if (!UtilNapiError::GetApiError(errCode, napiError)) {
80 MMI_HILOGE("This error code could not be found");
81 return nullptr;
82 }
83 napi_value resultCode = nullptr;
84 CHKRP(env, napi_create_int32(env, errCode, &resultCode), CREATE_INT32);
85 napi_value resultMessage = nullptr;
86 CHKRP(env, napi_create_string_utf8(env, napiError.msg.data(), NAPI_AUTO_LENGTH, &resultMessage), CREATE_INT32);
87 CHKRP(env, napi_create_error(env, nullptr, resultMessage, &object), CREATE_ERROR);
88 CHKRP(env, napi_set_named_property(env, object, ERR_CODE.c_str(), resultCode), SET_NAMED_PROPERTY);
89 }
90 return object;
91 }
92
GetStateResult(napi_env env,bool result)93 napi_value JsUtil::GetStateResult(napi_env env, bool result)
94 {
95 CHKPP(env);
96 napi_value state = nullptr;
97 CHKRP(env, napi_get_boolean(env, result, &state), GET_BOOLEAN);
98 napi_value object = nullptr;
99 CHKRP(env, napi_create_object(env, &object), CREATE_OBJECT);
100 CHKRP(env, napi_set_named_property(env, object, "state", state), SET_NAMED_PROPERTY);
101 return object;
102 }
103
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)104 bool JsUtil::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
105 {
106 napi_value handlerTemp = nullptr;
107 CHKRF(env, napi_get_reference_value(env, ref, &handlerTemp), GET_REFERENCE_VALUE);
108 bool isEqual = false;
109 CHKRF(env, napi_strict_equals(env, handle, handlerTemp, &isEqual), STRICT_EQUALS);
110 return isEqual;
111 }
112 } // namespace MMI
113 } // namespace OHOS
114