• 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 #undef LOG_TAG
23 #define LOG_TAG "JsUtilCooperate"
24 
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28 namespace {
29 inline constexpr std::string_view GET_BOOLEAN { "napi_get_boolean" };
30 inline constexpr std::string_view COERCE_TO_BOOL { "napi_coerce_to_bool" };
31 inline constexpr std::string_view CREATE_ERROR { "napi_create_error" };
32 } // namespace
33 
GetEnableInfo(sptr<CallbackInfo> cb)34 napi_value JsUtilCooperate::GetEnableInfo(sptr<CallbackInfo> cb)
35 {
36     CHKPP(cb);
37     CHKPP(cb->env);
38     return GetResult(cb->env, cb->data.enableResult, cb->data.msgInfo);
39 }
40 
GetStartInfo(sptr<CallbackInfo> cb)41 napi_value JsUtilCooperate::GetStartInfo(sptr<CallbackInfo> cb)
42 {
43     CHKPP(cb);
44     CHKPP(cb->env);
45     return GetResult(cb->env, cb->data.startResult, cb->data.msgInfo);
46 }
47 
GetStopInfo(sptr<CallbackInfo> cb)48 napi_value JsUtilCooperate::GetStopInfo(sptr<CallbackInfo> cb)
49 {
50     CHKPP(cb);
51     CHKPP(cb->env);
52     return GetResult(cb->env, cb->data.stopResult, cb->data.msgInfo);
53 }
54 
GetStateInfo(sptr<CallbackInfo> cb)55 napi_value JsUtilCooperate::GetStateInfo(sptr<CallbackInfo> cb)
56 {
57     CHKPP(cb);
58     CHKPP(cb->env);
59     napi_value ret = nullptr;
60     napi_value state = nullptr;
61     CHKRP(napi_create_int32(cb->env, cb->data.coordinationOpened ? 1 : 0, &ret),
62         CREATE_INT32);
63     CHKRP(napi_coerce_to_bool(cb->env, ret, &state), COERCE_TO_BOOL);
64     return state;
65 }
66 
GetResult(napi_env env,bool result,const CoordinationMsgInfo & msgInfo)67 napi_value JsUtilCooperate::GetResult(napi_env env, bool result, const CoordinationMsgInfo &msgInfo)
68 {
69     CHKPP(env);
70     napi_value object = nullptr;
71     if (result) {
72         napi_get_undefined(env, &object);
73         return object;
74     }
75     std::string errMsg;
76     if (!GetErrMsg(msgInfo, errMsg)) {
77         FI_HILOGE("GetErrMsg failed");
78         return nullptr;
79     }
80     napi_value resultCode = nullptr;
81     int32_t errorCode = GetErrCode(msgInfo);
82     FI_HILOGI("errorCode:%{public}d, msg:%{public}s", errorCode, errMsg.c_str());
83     CHKRP(napi_create_int32(env, errorCode, &resultCode), CREATE_INT32);
84     napi_value resultMessage = nullptr;
85     CHKRP(napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &resultMessage),
86         CREATE_STRING_UTF8);
87     CHKRP(napi_create_error(env, nullptr, resultMessage, &object), CREATE_ERROR);
88     CHKRP(napi_set_named_property(env, object, ERR_CODE.c_str(), resultCode), SET_NAMED_PROPERTY);
89     return object;
90 }
91 
GetErrCode(const CoordinationMsgInfo & msgInfo)92 int32_t JsUtilCooperate::GetErrCode(const CoordinationMsgInfo &msgInfo)
93 {
94     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
95         case CoordinationErrCode::OPEN_SESSION_FAILED: {
96             return CustomErrCode::OPEN_SESSION_FAILED;
97         }
98         case CoordinationErrCode::SEND_PACKET_FAILED: {
99             return CustomErrCode::SEND_PACKET_FAILED;
100         }
101         case CoordinationErrCode::UNEXPECTED_START_CALL: {
102             return CustomErrCode::UNEXPECTED_START_CALL;
103         }
104         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
105             return CustomErrCode::WORKER_THREAD_TIMEOUT;
106         }
107         case CoordinationErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING: {
108             return CustomErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING;
109         }
110         default:
111             return msgInfo.errCode;
112     }
113 }
114 
GetErrMsg(const CoordinationMsgInfo & msgInfo,std::string & msg)115 bool JsUtilCooperate::GetErrMsg(const CoordinationMsgInfo &msgInfo, std::string &msg)
116 {
117     auto iter = COOPERATE_MSG_MAP.find(msgInfo.msg);
118     if (iter == COOPERATE_MSG_MAP.end()) {
119         FI_HILOGE("Error code:%{public}d is not founded in COOPERATE_MSG_MAP", static_cast<int32_t> (msgInfo.msg));
120         return false;
121     }
122     msg = iter->second;
123     switch (static_cast<CoordinationErrCode>(msgInfo.errCode)) {
124         case CoordinationErrCode::COORDINATION_OK: {
125             msg += "Everything is fine";
126             break;
127         }
128         case CoordinationErrCode::OPEN_SESSION_FAILED: {
129             msg += "Open session failed";
130             break;
131         }
132         case CoordinationErrCode::SEND_PACKET_FAILED: {
133             msg += "Send packet failed";
134             break;
135         }
136         case CoordinationErrCode::UNEXPECTED_START_CALL: {
137             msg += "Unexpected start call";
138             break;
139         }
140         case CoordinationErrCode::WORKER_THREAD_TIMEOUT: {
141             msg += "Worker thread timeout";
142             break;
143         }
144         case CoordinationErrCode::NOT_AOLLOW_COOPERATE_WHEN_MOTION_DRAGGING: {
145             msg += "Not allow cooperate when motion dragging";
146             break;
147         }
148         default:
149             msg +="Softbus bind failed";
150     }
151     return true;
152 }
153 
GetStateResult(napi_env env,bool result)154 napi_value JsUtilCooperate::GetStateResult(napi_env env, bool result)
155 {
156     CHKPP(env);
157     napi_value state = nullptr;
158     CHKRP(napi_get_boolean(env, result, &state), GET_BOOLEAN);
159     napi_value object = nullptr;
160     CHKRP(napi_create_object(env, &object), CREATE_OBJECT);
161     CHKRP(napi_set_named_property(env, object, "state", state), SET_NAMED_PROPERTY);
162     return object;
163 }
164 
IsSameHandle(napi_env env,napi_value handle,napi_ref ref)165 bool JsUtilCooperate::IsSameHandle(napi_env env, napi_value handle, napi_ref ref)
166 {
167     napi_handle_scope scope = nullptr;
168     napi_open_handle_scope(env, &scope);
169     CHKPF(scope);
170     napi_value handlerTemp = nullptr;
171     CHKRF_SCOPE(env, napi_get_reference_value(env, ref, &handlerTemp), GET_REFERENCE_VALUE, scope);
172     bool isSame = false;
173     CHKRF_SCOPE(env, napi_strict_equals(env, handle, handlerTemp, &isSame), STRICT_EQUALS, scope);
174     napi_close_handle_scope(env, scope);
175     return isSame;
176 }
177 } // namespace DeviceStatus
178 } // namespace Msdp
179 } // namespace OHOS
180