1 /* 2 * Copyright (c) 2025 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 UNDERAGE_MODEL_NAPI_EVENT_H 17 #define UNDERAGE_MODEL_NAPI_EVENT_H 18 19 #include <list> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <set> 24 #include <string> 25 26 #include "napi/native_api.h" 27 28 namespace OHOS { 29 namespace Msdp { 30 namespace DeviceStatus { 31 struct UnderageModelEventListener { 32 std::set<napi_ref> onRefSets; 33 }; 34 35 class UnderageModelNapiEvent { 36 public: 37 UnderageModelNapiEvent(napi_env env, napi_value thisVar); 38 virtual ~UnderageModelNapiEvent(); 39 bool AddCallback(uint32_t eventType, napi_value handler); 40 bool CheckEvents(uint32_t eventType); 41 bool RemoveAllCallback(uint32_t eventType); 42 bool RemoveCallback(uint32_t eventType, napi_value handler); 43 virtual void OnEventChanged(uint32_t eventType, int32_t result, float confidence); 44 void ConvertUserAgeGroup(napi_value handler, int32_t result, float confidence); 45 46 private: 47 bool InsertRef(std::shared_ptr<UnderageModelEventListener> listener, const napi_value &handler); 48 bool IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs); 49 50 private: 51 napi_env env_ { nullptr }; 52 napi_ref thisVarRef_ { nullptr }; 53 std::map<uint32_t, std::shared_ptr<UnderageModelEventListener>> events_; 54 std::mutex eventsMutex_; 55 }; 56 } // namespace DeviceStatus 57 } // namespace Msdp 58 } // namespace OHOS 59 #endif // UNDERAGE_MODEL_NAPI_EVENT_H 60