1 /* 2 * Copyright (c) 2021-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_EVENT_TARGET_H 17 #define JS_EVENT_TARGET_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "util_napi.h" 22 #include "utils/log.h" 23 24 #include "define_multimodal.h" 25 #include "error_multimodal.h" 26 27 #include "input_manager.h" 28 #include "js_util.h" 29 30 namespace OHOS { 31 namespace MMI { 32 class JsEventTarget : public IInputDeviceListener, public std::enable_shared_from_this<JsEventTarget> { 33 public: 34 JsEventTarget(); 35 ~JsEventTarget(); 36 DISALLOW_COPY_AND_MOVE(JsEventTarget); 37 static void EmitJsIds(int32_t userData, std::vector<int32_t> &ids); 38 static void EmitJsDev(int32_t userData, std::shared_ptr<InputDevice> device); 39 static void EmitSupportKeys(int32_t userData, std::vector<bool> &keystrokeAbility); 40 static void EmitJsKeyboardType(int32_t userData, int32_t keyboardType); 41 void AddListener(napi_env env, const std::string &type, napi_value handle); 42 void RemoveListener(napi_env env, const std::string &type, napi_value handle); 43 void RemoveCallbackInfo(napi_env env, napi_value handle, int32_t userData); 44 napi_value CreateCallbackInfo(napi_env env, napi_value handle, const int32_t userData, bool isApi9 = false); 45 void ResetEnv(); 46 virtual void OnDeviceAdded(int32_t deviceId, const std::string &type) override; 47 virtual void OnDeviceRemoved(int32_t deviceId, const std::string &type) override; 48 49 private: 50 static void CallIdsPromiseWork(uv_work_t *work, int32_t status); 51 static void CallIdsAsyncWork(uv_work_t *work, int32_t status); 52 static void CallDevAsyncWork(uv_work_t *work, int32_t status); 53 static void CallDevPromiseWork(uv_work_t *work, int32_t status); 54 static void CallKeystrokeAbilityPromise(uv_work_t *work, int32_t status); 55 static void CallKeystrokeAbilityAsync(uv_work_t *work, int32_t status); 56 static void CallKeyboardTypeAsync(uv_work_t *work, int32_t status); 57 static void CallKeyboardTypePromise(uv_work_t *work, int32_t status); 58 static void CallDevListPromiseWork(uv_work_t *work, int32_t status); 59 static void CallDevListAsyncWork(uv_work_t *work, int32_t status); 60 static void CallDevInfoAsyncWork(uv_work_t *work, int32_t status); 61 static void CallDevInfoPromiseWork(uv_work_t *work, int32_t status); 62 static void EmitAddedDeviceEvent(uv_work_t *work, int32_t status); 63 static void EmitRemoveDeviceEvent(uv_work_t *work, int32_t status); 64 static std::unique_ptr<JsUtil::CallbackInfo> GetCallbackInfo(uv_work_t *work); 65 static napi_value GreateBusinessError(napi_env env, int32_t errCode, std::string errMessage); 66 private: 67 inline static std::map<int32_t, std::unique_ptr<JsUtil::CallbackInfo>> callback_ {}; 68 inline static std::map<std::string, std::vector<std::unique_ptr<JsUtil::CallbackInfo>>> devListener_ {}; 69 bool isListeningProcess_ { false }; 70 }; 71 } // namespace MMI 72 } // namespace OHOS 73 74 #endif // JS_EVENT_TARGET_H