• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "js_util_cooperate.h"
17 
18 #include "devicestatus_define.h"
19 #include "napi_constants.h"
20 #include "util_napi_error.h"
21 
22 namespace OHOS {
23 namespace Msdp {
24 namespace DeviceStatus {
25 namespace {
26 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "JsUtilCooperate" };
27 inline constexpr std::string_view GET_BOOLEAN { "napi_get_boolean" };
28 inline constexpr std::string_view COERCE_TO_BOOL { "napi_coerce_to_bool" };
29 inline constexpr std::string_view CREATE_ERROR { "napi_create_error" };
30 } // namespace
31 
GetEnableInfo(sptr<CallbackInfo> cb)32 napi_value JsUtilCooperate::GetEnableInfo(sptr<CallbackInfo> cb)
33 {
34     CHKPP(cb);
35     CHKPP(cb->env);
36     return GetResult(cb->env, cb->data.enableResult, cb->data.errCode);
37 }
38 
GetStartInfo(sptr<CallbackInfo> cb)39 napi_value JsUtilCooperate::GetStartInfo(sptr<CallbackInfo> cb)
40 {
41     CHKPP(cb);
42     CHKPP(cb->env);
43     return GetResult(cb->env, cb->data.startResult, cb->data.errCode);
44 }
45 
GetStopInfo(sptr<CallbackInfo> cb)46 napi_value JsUtilCooperate::GetStopInfo(sptr<CallbackInfo> cb)
47 {
48     CHKPP(cb);
49     CHKPP(cb->env);
50     return GetResult(cb->env, cb->data.stopResult, cb->data.errCode);
51 }
52 
GetStateInfo(sptr<CallbackInfo> cb)53 napi_value JsUtilCooperate::GetStateInfo(sptr<CallbackInfo> cb)
54 {
55     CHKPP(cb);
56     CHKPP(cb->env);
57     napi_value ret = nullptr;
58     napi_value state = nullptr;
59     CHKRP(napi_create_int32(cb->env, cb->data.coordinationOpened ? 1 : 0, &ret),
60         CREATE_INT32);
61     CHKRP(napi_coerce_to_bool(cb->env, ret, &state), COERCE_TO_BOOL);
62     return state;
63 }
64 
GetResult(napi_env env,bool result,int32_t errorCode)65 napi_value JsUtilCooperate::GetResult(napi_env env, bool result, int32_t errorCode)
66 {
67     CHKPP(env);
68     napi_value object = nullptr;
69     if (result) {
70         napi_get_undefined(env, &object);
71         return object;
72     }
73     std::string errMsg;
74     if (!UtilNapiError::GetErrorMsg(errorCode, errMsg)) {
75         FI_HILOGE("This errCode could not be found");
76         return nullptr;
77     }
78     napi_value resultCode = nullptr;
79     CHKRP(napi_create_int32(env, errorCode, &resultCode), CREATE_INT32);
80     napi_value resultMessage = nullptr;
81     CHKRP(napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &resultMessage),
82         CREATE_STRING_UTF8);
83     CHKRP(napi_create_error(env, nullptr, resultMessage, &object), CREATE_ERROR);
84     CHKRP(napi_set_named_property(env, object, ERR_CODE.c_str(), resultCode), SET_NAMED_PROPERTY);
85     return object;
86 }
87 
GetStateResult(napi_env env,bool result)88 napi_value JsUtilCooperate::GetStateResult(napi_env env, bool result)
89 {
90     CHKPP(env);
91     napi_value state = nullptr;
92     CHKRP(napi_get_boolean(env, result, &state), GET_BOOLEAN);
93     napi_value object = nullptr;
94     CHKRP(napi_create_object(env, &object), CREATE_OBJECT);
95     CHKRP(napi_set_named_property(env, object, "state", state), SET_NAMED_PROPERTY);
96     return object;
97 }
98 
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)99 bool JsUtilCooperate::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
100 {
101     napi_handle_scope scope = nullptr;
102     napi_open_handle_scope(env, &scope);
103     CHKPF(scope);
104     napi_value handlerTemp = nullptr;
105     CHKRF_SCOPE(env, napi_get_reference_value(env, ref, &handlerTemp), GET_REFERENCE_VALUE, scope);
106     bool isSame = false;
107     CHKRF_SCOPE(env, napi_strict_equals(env, handle, handlerTemp, &isSame), STRICT_EQUALS, scope);
108     napi_close_handle_scope(env, scope);
109     return isSame;
110 }
111 } // namespace DeviceStatus
112 } // namespace Msdp
113 } // namespace OHOS
114