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 #ifndef JS_UTIL_COOPERATE_H 17 #define JS_UTIL_COOPERATE_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "napi/native_api.h" 26 27 #include "coordination_message.h" 28 29 #include "refbase.h" 30 31 namespace OHOS { 32 namespace Msdp { 33 namespace DeviceStatus { 34 namespace { 35 const std::unordered_map<CoordinationMessage, std::string> COOPERATE_MSG_MAP { 36 { CoordinationMessage::PREPARE, "PREPARE" }, 37 { CoordinationMessage::UNPREPARE, "UNPREPARE" }, 38 { CoordinationMessage::ACTIVATE, "ACTIVATE" }, 39 { CoordinationMessage::ACTIVATE_SUCCESS, "ACTIVATE_SUCCESS" }, 40 { CoordinationMessage::ACTIVATE_FAIL, "ACTIVATE_FAIL" }, 41 { CoordinationMessage::DEACTIVATE_SUCCESS, "DEACTIVATE_SUCCESS" }, 42 { CoordinationMessage::DEACTIVATE_FAIL, "DEACTIVATE_FAIL" }, 43 { CoordinationMessage::SESSION_CLOSED, "SESSION_CLOSED" } 44 }; 45 } // namespace 46 47 class JsUtilCooperate { 48 public: 49 struct UserData { 50 int32_t userData { 0 }; 51 int32_t deviceId { 0 }; 52 napi_value handle { nullptr }; 53 std::vector<int32_t> keys; 54 }; 55 struct CallbackData { 56 bool enableResult { false }; 57 bool startResult { false }; 58 bool stopResult { false }; 59 bool coordinationOpened { false }; 60 std::string deviceDescriptor; 61 CoordinationMsgInfo msgInfo; 62 }; 63 struct CallbackInfo : RefBase { 64 CallbackInfo() = default; 65 ~CallbackInfo() = default; 66 int32_t errCode { 0 }; 67 napi_ref ref { nullptr }; 68 napi_env env { nullptr }; 69 napi_deferred deferred { nullptr }; 70 CallbackData data; 71 }; 72 template <typename T> DeletePtr(T & ptr)73 static void DeletePtr(T &ptr) 74 { 75 if (ptr != nullptr) { 76 delete ptr; 77 ptr = nullptr; 78 } 79 } 80 81 static napi_value GetEnableInfo(sptr<CallbackInfo> cb); 82 static napi_value GetStartInfo(sptr<CallbackInfo> cb); 83 static napi_value GetStopInfo(sptr<CallbackInfo> cb); 84 static napi_value GetStateInfo(sptr<CallbackInfo> cb); 85 static napi_value GetStateResult(napi_env env, bool result); 86 static napi_value GetResult(napi_env env, bool result, const CoordinationMsgInfo &msgInfo); 87 static int32_t GetErrCode(const CoordinationMsgInfo &msgInfo); 88 static bool GetErrMsg(const CoordinationMsgInfo &msgInfo, std::string &msg); 89 static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 90 }; 91 } // namespace DeviceStatus 92 } // namespace Msdp 93 } // namespace OHOS 94 #endif // JS_UTIL_COOPERATE_H 95