1 /*
2  * Copyright (c) 2022-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_TARGET_H
17 #define JS_EVENT_TARGET_H
18 
19 #include <cstdint>
20 #include <map>
21 #include <memory>
22 #include <queue>
23 #include <string>
24 #include <string_view>
25 #include <vector>
26 
27 #include "nocopyable.h"
28 #include "uv.h"
29 
30 #include "coordination_message.h"
31 #include "i_coordination_listener.h"
32 #include "i_event_listener.h"
33 #include "js_util.h"
34 
35 namespace OHOS {
36 namespace Msdp {
37 namespace DeviceStatus {
38 class JsEventTarget : public ICoordinationListener,
39                       public IEventListener,
40                       public std::enable_shared_from_this<JsEventTarget> {
41 public:
42     JsEventTarget();
43     DISALLOW_COPY_AND_MOVE(JsEventTarget);
44     virtual ~JsEventTarget() = default;
45 
46     static void EmitJsPrepare(sptr<JsUtil::CallbackInfo> cb, const std::string &networkId,
47         const CoordinationMsgInfo &msgInfo);
48     static void EmitJsActivate(sptr<JsUtil::CallbackInfo> cb, const std::string &remoteNetworkId,
49         const CoordinationMsgInfo &msgInfo);
50     static void EmitJsDeactivate(sptr<JsUtil::CallbackInfo> cb, const std::string &networkId,
51         const CoordinationMsgInfo &msgInfo);
52     static void EmitJsGetCrossingSwitchState(sptr<JsUtil::CallbackInfo> cb, bool state);
53     void AddListener(napi_env env, const std::string &type, napi_value handle);
54     void RemoveListener(napi_env env, const std::string &type, napi_value handle);
55     void AddListener(napi_env env, const std::string &type, const std::string &networkId, napi_value handle);
56     void RemoveListener(napi_env env, const std::string &type, const std::string &networkId, napi_value handle);
57     napi_value CreateCallbackInfo(napi_env env, napi_value handle, sptr<JsUtil::CallbackInfo> callback);
58     napi_value CreateMouseCallbackInfo(napi_env env, napi_value handle, sptr<JsUtil::MouseCallbackInfo> callback);
59     void ResetEnv();
60     void OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg) override;
61     void OnMouseLocationEvent(const std::string &networkId, const Event &event) override;
62 
63 private:
64     static void CallPreparePromiseWork(uv_work_t *work, int32_t status);
65     static void CallPrepareAsyncWork(uv_work_t *work, int32_t status);
66     static void CallActivatePromiseWork(uv_work_t *work, int32_t status);
67     static void CallActivateAsyncWork(uv_work_t *work, int32_t status);
68     static void CallDeactivatePromiseWork(uv_work_t *work, int32_t status);
69     static void CallDeactivateAsyncWork(uv_work_t *work, int32_t status);
70     static void CallGetCrossingSwitchStatePromiseWork(uv_work_t *work, int32_t status);
71     static void CallGetCrossingSwitchStateAsyncWork(uv_work_t *work, int32_t status);
72     static void EmitCoordinationMessageEvent(uv_work_t *work, int32_t status);
73     static void EmitMouseLocationEvent(uv_work_t *work, int32_t status);
74     bool IsHandleExist(napi_env env, const std::string &networkId, napi_value handle);
75 
76 private:
77     std::atomic_bool isListeningProcess_ { false };
78     struct CoordinationEvent {
79         std::string networkId;
80         CoordinationMessage msg { CoordinationMessage::UNKNOW };
81     };
82     inline static std::queue<CoordinationEvent> eventQueue_;
83     inline static std::map<std::string_view, std::vector<sptr<JsUtil::CallbackInfo>>> coordinationListeners_;
84     inline static std::map<std::string, std::vector<sptr<JsUtil::MouseCallbackInfo>>> mouseLocationListeners_;
85 };
86 } // namespace DeviceStatus
87 } // namespace Msdp
88 } // namespace OHOS
89 #endif // JS_EVENT_TARGET_H
90