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_H 17 #define UNDERAGE_MODEL_NAPI_H 18 19 #include "underage_model_napi_event.h" 20 21 #include <array> 22 23 #include "iunderage_model_listener.h" 24 25 namespace OHOS { 26 namespace Msdp { 27 namespace DeviceStatus { 28 typedef int32_t (*RegisterListenerFunc)(uint32_t feature, 29 std::shared_ptr<UserStatusAwareness::IUnderageModelListener> listener); 30 typedef int32_t (*SubscribeFunc)(uint32_t feature); 31 typedef int32_t (*UnsubscribeFunc)(uint32_t feature); 32 33 class UnderageModelListener : public UserStatusAwareness::IUnderageModelListener { 34 public: UnderageModelListener(napi_env env)35 explicit UnderageModelListener(napi_env env) : env_(env) {} ~UnderageModelListener()36 ~UnderageModelListener() {}; 37 void OnUnderageModelListener(uint32_t eventType, int32_t result, float confidence) const override; 38 39 private: 40 napi_env env_; 41 }; 42 43 class UnderageModelNapi : public UnderageModelNapiEvent { 44 public: 45 UnderageModelNapi(napi_env env, napi_value thisVar); 46 ~UnderageModelNapi() override; 47 48 static napi_value Init(napi_env env, napi_value exports); 49 static napi_value SubscribeUnderageModel(napi_env env, napi_callback_info info); 50 static napi_value UnsubscribeUnderageModel(napi_env env, napi_callback_info info); 51 52 protected: 53 std::map<uint32_t, std::shared_ptr<UserStatusAwareness::IUnderageModelListener>> callbacks_; 54 55 private: 56 static bool LoadLibrary(); 57 static uint32_t GetUnderageModelType(const std::string &type); 58 static bool SubscribeCallback(napi_env env, uint32_t type); 59 static bool UnsubscribeCallback(napi_env env, uint32_t type); 60 static bool Subscribe(napi_env env, uint32_t type); 61 static bool RemoveCallbackArgs(uint32_t type, size_t argc, napi_value args[]); 62 static bool ConstructUnderageModel(napi_env env, napi_value jsThis); 63 static bool CreateUserAgeGroup(napi_env env, napi_value exports); 64 template <std::size_t N> 65 static bool ValidateArgsType(napi_env env, napi_value *args, size_t argc, 66 const std::array<napi_valuetype, N> &expectedTypes); 67 static bool TransJsToStr(napi_env env, napi_value value, std::string &str); 68 69 private: 70 napi_env env_ { nullptr }; 71 void* g_userStatusHandle { nullptr }; 72 RegisterListenerFunc g_registerListenerFunc { nullptr }; 73 SubscribeFunc g_subscribeFunc { nullptr }; 74 UnsubscribeFunc g_unsubscribeFunc { nullptr }; 75 }; 76 } // namespace DeviceStatus 77 } // namespace Msdp 78 } // namespace OHOS 79 #endif // UNDERAGE_MODEL_NAPI_H