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 DEVICESTATUS_EVENT_H 17 #define DEVICESTATUS_EVENT_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <string> 23 24 #include "napi/native_api.h" 25 26 namespace OHOS { 27 namespace Msdp { 28 namespace DeviceStatus { 29 struct DeviceStatusEventListener { 30 napi_ref onHandlerRef { nullptr }; 31 }; 32 33 class DeviceStatusEvent { 34 public: 35 DeviceStatusEvent(napi_env env); 36 DeviceStatusEvent() = default; 37 virtual ~DeviceStatusEvent(); 38 39 virtual bool On(int32_t eventType, napi_value handler, bool isOnce); 40 virtual bool Off(int32_t eventType, napi_value handler); 41 virtual bool OffOnce(int32_t eventType, napi_value handler); 42 virtual void OnEvent(int32_t eventType, size_t argc, int32_t value, bool isOnce); 43 void CheckRet(int32_t eventType, size_t argc, int32_t value, 44 std::shared_ptr<DeviceStatusEventListener> &typeHandler); 45 void SendRet(int32_t eventType, int32_t value, napi_value &result); 46 void ClearEventMap(); 47 bool RemoveAllCallback(int32_t eventType); 48 protected: 49 napi_env env_ { nullptr }; 50 napi_ref thisVarRef_ { nullptr }; 51 std::map<int32_t, std::list<std::shared_ptr<DeviceStatusEventListener>>> eventMap_; 52 std::map<int32_t, std::list<std::shared_ptr<DeviceStatusEventListener>>> eventOnceMap_; 53 }; 54 } // namespace DeviceStatus 55 } // namespace Msdp 56 } // namespace OHOS 57 #endif // DEVICESTATUS_EVENT_H 58