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_EVENT_COOPERATE_TARGET_H 17 #define JS_EVENT_COOPERATE_TARGET_H 18 19 #include <cstdint> 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <string_view> 24 #include <vector> 25 26 #include "nocopyable.h" 27 #include "uv.h" 28 29 #include "i_coordination_listener.h" 30 #include "js_util_cooperate.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class JsEventCooperateTarget : public ICoordinationListener, 36 public std::enable_shared_from_this<JsEventCooperateTarget> { 37 public: 38 enum class CooperateMessage { 39 INFO_START = 0, 40 INFO_SUCCESS = 1, 41 INFO_FAIL = 2, 42 STATE_ON = 3, 43 STATE_OFF = 4 44 }; 45 46 JsEventCooperateTarget(); 47 DISALLOW_COPY_AND_MOVE(JsEventCooperateTarget); 48 virtual ~JsEventCooperateTarget() = default; 49 50 static void EmitJsEnable(sptr<JsUtilCooperate::CallbackInfo> cb, 51 const std::string &networkId, const CoordinationMsgInfo &msgInfo); 52 static void EmitJsStart(sptr<JsUtilCooperate::CallbackInfo> cb, 53 const std::string &remoteNetworkId, const CoordinationMsgInfo &msgInfo); 54 static void EmitJsStop(sptr<JsUtilCooperate::CallbackInfo> cb, 55 const std::string &networkId, const CoordinationMsgInfo &msgInfo); 56 static void EmitJsGetState(sptr<JsUtilCooperate::CallbackInfo> cb, bool state); 57 void AddListener(napi_env env, const std::string &type, napi_value handle); 58 void RemoveListener(napi_env env, const std::string &type, napi_value handle); 59 napi_value CreateCallbackInfo(napi_env, napi_value handle, sptr<JsUtilCooperate::CallbackInfo> callback); 60 void ResetEnv(); 61 void OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg) override; 62 63 private: 64 static void CallEnablePromiseWork(uv_work_t *work, int32_t status); 65 static void CallEnableAsyncWork(uv_work_t *work, int32_t status); 66 static void CallStartPromiseWork(uv_work_t *work, int32_t status); 67 static void CallStartAsyncWork(uv_work_t *work, int32_t status); 68 static void CallStopPromiseWork(uv_work_t *work, int32_t status); 69 static void CallStopAsyncWork(uv_work_t *work, int32_t status); 70 static void CallGetStatePromiseWork(uv_work_t *work, int32_t status); 71 static void CallGetStateAsyncWork(uv_work_t *work, int32_t status); 72 static void EmitCoordinationMessageEvent(uv_work_t *work, int32_t status); 73 74 inline static std::map<CoordinationMessage, CooperateMessage> messageTransform = { 75 { CoordinationMessage::PREPARE, CooperateMessage::STATE_ON }, 76 { CoordinationMessage::UNPREPARE, CooperateMessage::STATE_OFF }, 77 { CoordinationMessage::ACTIVATE, CooperateMessage::INFO_START }, 78 { CoordinationMessage::ACTIVATE_SUCCESS, CooperateMessage::INFO_SUCCESS }, 79 { CoordinationMessage::ACTIVATE_FAIL, CooperateMessage::INFO_FAIL } 80 }; 81 inline static std::map<std::string_view, std::vector<sptr<JsUtilCooperate::CallbackInfo>>> 82 coordinationListeners_ {}; 83 std::atomic_bool isListeningProcess_ { false }; 84 }; 85 } // namespace DeviceStatus 86 } // namespace Msdp 87 } // namespace OHOS 88 #endif // JS_EVENT_COOPERATE_TARGET_H 89