• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 EmitJsActivateCooperate(sptr<JsUtil::CallbackInfo> cb, const std::string &remoteNetworkId,
53         const CoordinationMsgInfo &msgInfo);
54     static void EmitJsGetCrossingSwitchState(sptr<JsUtil::CallbackInfo> cb, bool state);
55     void AddListener(napi_env env, const std::string &type, napi_value handle);
56     void RemoveListener(napi_env env, const std::string &type, napi_value handle);
57     void AddListener(napi_env env, const std::string &type, const std::string &networkId, napi_value handle);
58     void RemoveListener(napi_env env, const std::string &type, const std::string &networkId, napi_value handle);
59     napi_value CreateCallbackInfo(napi_env env, napi_value handle, sptr<JsUtil::CallbackInfo> callback);
60     napi_value CreateMouseCallbackInfo(napi_env env, napi_value handle, sptr<JsUtil::MouseCallbackInfo> callback);
61     void ResetEnv();
62     void OnCoordinationMessage(const std::string &networkId, CoordinationMessage msg) override;
63     void OnMouseLocationEvent(const std::string &networkId, const Event &event) override;
64 
65 private:
66     static void CallPreparePromiseWork(sptr<JsUtil::CallbackInfo> cb);
67     static void CallPrepareAsyncWork(sptr<JsUtil::CallbackInfo> cb);
68     static void CallActivatePromiseWork(sptr<JsUtil::CallbackInfo> cb);
69     static void CallActivateAsyncWork(sptr<JsUtil::CallbackInfo> cb);
70     static void CallDeactivatePromiseWork(sptr<JsUtil::CallbackInfo> cb);
71     static void CallDeactivateAsyncWork(sptr<JsUtil::CallbackInfo> cb);
72     static void CallGetCrossingSwitchStatePromiseWork(sptr<JsUtil::CallbackInfo> cb);
73     static void CallGetCrossingSwitchStateAsyncWork(sptr<JsUtil::CallbackInfo> cb);
74     static void EmitCoordinationMessageEvent(sptr<JsUtil::CallbackInfo> cb);
75     static void EmitMouseLocationEvent(sptr<JsUtil::MouseCallbackInfo> cb);
76     bool IsHandleExist(napi_env env, const std::string &networkId, napi_value handle);
77 
78 private:
79     std::atomic_bool isListeningProcess_ { false };
80     struct CoordinationEvent {
81         std::string networkId;
82         CoordinationMessage msg { CoordinationMessage::UNKNOW };
83     };
84     inline static std::queue<CoordinationEvent> eventQueue_;
85     inline static std::map<std::string_view, std::vector<sptr<JsUtil::CallbackInfo>>> coordinationListeners_;
86     inline static std::map<std::string, std::vector<sptr<JsUtil::MouseCallbackInfo>>> mouseLocationListeners_;
87 };
88 } // namespace DeviceStatus
89 } // namespace Msdp
90 } // namespace OHOS
91 #endif // JS_EVENT_TARGET_H
92