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 #ifndef JS_UTIL_H 17 #define JS_UTIL_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include <napi/native_api.h> 25 26 #include "cooperation_message.h" 27 28 namespace OHOS { 29 namespace MMI { 30 class JsUtil { 31 public: 32 struct UserData { 33 int32_t userData { 0 }; 34 int32_t deviceId { 0 }; 35 napi_value handle { nullptr }; 36 std::vector<int32_t> keys; 37 }; 38 39 struct CallbackData { 40 bool enableResult { false }; 41 bool startResult { false }; 42 bool stopResult { false }; 43 bool cooperateOpened { false }; 44 std::string deviceDescriptor; 45 int32_t errCode { 0 }; 46 CooperationMessage msg = CooperationMessage::OPEN_SUCCESS; 47 }; 48 49 struct CallbackInfo { 50 CallbackInfo() = default; 51 ~CallbackInfo(); 52 napi_env env { nullptr }; 53 napi_ref ref { nullptr }; 54 napi_deferred deferred { nullptr }; 55 int32_t errCode { 0 }; 56 CallbackData data; 57 UserData uData; 58 }; 59 60 static napi_value GetEnableInfo(const std::unique_ptr<CallbackInfo> &cb); 61 static napi_value GetStartInfo(const std::unique_ptr<CallbackInfo> &cb); 62 static napi_value GetStopInfo(const std::unique_ptr<CallbackInfo> &cb); 63 static napi_value GetStateInfo(const std::unique_ptr<CallbackInfo> &cb); 64 static napi_value GetStateResult(napi_env env, bool result); 65 static napi_value GetResult(napi_env env, bool result, int32_t errCode); 66 static bool IsSameHandle(napi_env env, napi_value handle, napi_ref ref); 67 68 template <typename T> DeletePtr(T & ptr)69 static void DeletePtr(T &ptr) 70 { 71 if (ptr != nullptr) { 72 delete ptr; 73 ptr = nullptr; 74 } 75 } 76 }; 77 } // namespace MMI 78 } // namespace OHOS 79 #endif // JS_UTIL_H 80