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 #include "underage_model_napi_event.h"
17
18 #include <iostream>
19 #include <iomanip>
20
21 #include "devicestatus_define.h"
22 #include "fi_log.h"
23 #include "napi_constants.h"
24 #include "util_napi.h"
25
26 #undef LOG_TAG
27 #define LOG_TAG "DeviceUnderageModelNapiEvent"
28
29 namespace OHOS {
30 namespace Msdp {
31 namespace DeviceStatus {
32 namespace {
33 constexpr int16_t ONE_PARAMETER = 1;
34 constexpr int16_t TWO_PARAMETER = 2;
35 constexpr int16_t FLOAT_PRECSION = 3;
36 } // namespace
37
UnderageModelNapiEvent(napi_env env,napi_value thisVar)38 UnderageModelNapiEvent::UnderageModelNapiEvent(napi_env env, napi_value thisVar)
39 {
40 env_ = env;
41 thisVarRef_ = nullptr;
42 napi_create_reference(env, thisVar, ONE_PARAMETER, &thisVarRef_);
43 }
44
~UnderageModelNapiEvent()45 UnderageModelNapiEvent::~UnderageModelNapiEvent()
46 {
47 if (env_ == nullptr) {
48 FI_HILOGW("env_ is nullptr");
49 return;
50 }
51 {
52 std::lock_guard<std::mutex> lock(eventsMutex_);
53 for (auto& iter : events_) {
54 if (iter.second == nullptr) {
55 continue;
56 }
57 for (auto it = iter.second->onRefSets.begin(); it != iter.second->onRefSets.end();) {
58 if (*it == nullptr) {
59 ++it;
60 continue;
61 }
62 napi_status status = napi_delete_reference(env_, *it);
63 if (status != napi_ok) {
64 FI_HILOGE("napi_delete_reference failed");
65 }
66 it = iter.second->onRefSets.erase(it);
67 }
68 iter.second = nullptr;
69 }
70 events_.clear();
71 }
72 if (env_ != nullptr && thisVarRef_ != nullptr) {
73 napi_delete_reference(env_, thisVarRef_);
74 }
75 }
76
AddCallback(uint32_t eventType,napi_value handler)77 bool UnderageModelNapiEvent::AddCallback(uint32_t eventType, napi_value handler)
78 {
79 FI_HILOGD("Enter");
80 std::lock_guard<std::mutex> lock(eventsMutex_);
81 auto iter = events_.find(eventType);
82 if (iter == events_.end()) {
83 FI_HILOGD("found event:%{public}d", eventType);
84 napi_ref onHandlerRef = nullptr;
85 napi_status status = napi_create_reference(env_, handler, ONE_PARAMETER, &onHandlerRef);
86 if (status != napi_ok) {
87 FI_HILOGE("napi_create_reference failed");
88 return false;
89 }
90 auto listener = std::make_shared<UnderageModelEventListener>();
91 std::set<napi_ref> onRefSets;
92 listener->onRefSets = onRefSets;
93 auto ret = listener->onRefSets.insert(onHandlerRef);
94 if (!ret.second) {
95 FI_HILOGE("Failed to insert refs");
96 return false;
97 }
98 events_.insert(std::make_pair(eventType, listener));
99 FI_HILOGD("Insert finish");
100 return true;
101 }
102 FI_HILOGD("found event: %{public}d", eventType);
103 if (iter->second == nullptr || iter->second->onRefSets.empty()) {
104 FI_HILOGE("listener or onRefSets is nullptr");
105 events_.erase(iter);
106 return false;
107 }
108 FI_HILOGD("Check type: %{public}d same handle", eventType);
109 if (!InsertRef(iter->second, handler)) {
110 FI_HILOGE("Failed to insert ref");
111 events_.erase(iter);
112 return false;
113 }
114 return true;
115 }
116
RemoveAllCallback(uint32_t eventType)117 bool UnderageModelNapiEvent::RemoveAllCallback(uint32_t eventType)
118 {
119 FI_HILOGD("RemoveAllCallback in, event:%{public}d", eventType);
120 std::lock_guard<std::mutex> lock(eventsMutex_);
121 auto iter = events_.find(eventType);
122 if (iter == events_.end()) {
123 FI_HILOGE("EventType %{public}d not found", eventType);
124 return false;
125 }
126 if (iter->second == nullptr) {
127 FI_HILOGE("listener is nullptr");
128 return false;
129 }
130 for (auto it = iter->second->onRefSets.begin(); it != iter->second->onRefSets.end();) {
131 if (*it == nullptr) {
132 ++it;
133 continue;
134 }
135 napi_status status = napi_delete_reference(env_, *it);
136 if (status != napi_ok) {
137 FI_HILOGE("napi_delete_reference failed");
138 }
139 it = iter->second->onRefSets.erase(it);
140 }
141 if (iter->second->onRefSets.empty()) {
142 FI_HILOGE("onRefSets is empty");
143 events_.erase(iter);
144 }
145 return true;
146 }
147
RemoveCallback(uint32_t eventType,napi_value handler)148 bool UnderageModelNapiEvent::RemoveCallback(uint32_t eventType, napi_value handler)
149 {
150 FI_HILOGD("Enter, event:%{public}d", eventType);
151 std::lock_guard<std::mutex> lock(eventsMutex_);
152 auto iter = events_.find(eventType);
153 if (iter == events_.end()) {
154 FI_HILOGE("EventType %{public}d not found", eventType);
155 return false;
156 }
157 if (iter->second == nullptr) {
158 FI_HILOGE("listener is nullptr");
159 events_.erase(iter);
160 return false;
161 }
162 for (auto it = iter->second->onRefSets.begin(); it != iter->second->onRefSets.end();) {
163 if (*it == nullptr) {
164 ++it;
165 continue;
166 }
167 napi_value deleteHandler;
168 napi_status status = napi_get_reference_value(env_, *it, &deleteHandler);
169 if (status != napi_ok) {
170 FI_HILOGE("napi_get_reference_value failed");
171 ++it;
172 continue;
173 }
174 if (IsSameValue(env_, handler, deleteHandler)) {
175 status = napi_delete_reference(env_, *it);
176 if (status != napi_ok) {
177 FI_HILOGE("napi_delete_reference failed");
178 }
179 it = iter->second->onRefSets.erase(it);
180 break;
181 }
182 ++it;
183 }
184 if (iter->second->onRefSets.empty()) {
185 events_.erase(eventType);
186 }
187 return true;
188 }
189
InsertRef(std::shared_ptr<UnderageModelEventListener> listener,const napi_value & handler)190 bool UnderageModelNapiEvent::InsertRef(std::shared_ptr<UnderageModelEventListener> listener, const napi_value &handler)
191 {
192 if (listener == nullptr) {
193 FI_HILOGE("listener is nullptr");
194 return false;
195 }
196 bool hasHandler = false;
197 for (auto item : listener->onRefSets) {
198 napi_value onHandler = nullptr;
199 napi_status status = napi_get_reference_value(env_, item, &onHandler);
200 if (status != napi_ok) {
201 FI_HILOGE("napi_get_reference_value failed");
202 status = napi_delete_reference(env_, item);
203 if (status != napi_ok) {
204 FI_HILOGE("napi_delete_reference failed");
205 continue;
206 }
207 continue;
208 }
209 if (IsSameValue(env_, handler, onHandler)) {
210 hasHandler = true;
211 break;
212 }
213 }
214 if (hasHandler) {
215 FI_HILOGD("napi repeat subscribe");
216 return true;
217 }
218 napi_ref onHandlerRef = nullptr;
219 napi_status status = napi_create_reference(env_, handler, 1, &(onHandlerRef));
220 if (status != napi_ok) {
221 FI_HILOGE("napi_create_reference failed");
222 return false;
223 }
224
225 FI_HILOGD("Insert new ref");
226 auto ret = listener->onRefSets.insert(onHandlerRef);
227 if (!ret.second) {
228 FI_HILOGE("Failed to insert");
229 return false;
230 }
231 FI_HILOGD("ref size %{public}zu", listener->onRefSets.size());
232 return true;
233 }
234
OnEventChanged(uint32_t eventType,int32_t result,float confidence)235 void UnderageModelNapiEvent::OnEventChanged(uint32_t eventType, int32_t result, float confidence)
236 {
237 std::lock_guard<std::mutex> lock(eventsMutex_);
238 auto typeIter = events_.find(eventType);
239 if (typeIter == events_.end()) {
240 FI_HILOGE("eventType: %{public}d not found", eventType);
241 return;
242 }
243 if (typeIter->second == nullptr) {
244 FI_HILOGE("listener is nullptr.");
245 return;
246 }
247 for (auto item : typeIter->second->onRefSets) {
248 napi_value handler = nullptr;
249 napi_status ret = napi_get_reference_value(env_, item, &handler);
250 if (ret != napi_ok) {
251 FI_HILOGE("napi_get_reference_value for %{public}d failed, status: %{public}d", eventType, ret);
252 continue;
253 }
254 ConvertUserAgeGroup(handler, result, confidence);
255 }
256 }
257
ConvertUserAgeGroup(napi_value handler,int32_t result,float confidence)258 void UnderageModelNapiEvent::ConvertUserAgeGroup(napi_value handler, int32_t result, float confidence)
259 {
260 napi_handle_scope scope = nullptr;
261 napi_open_handle_scope(env_, &scope);
262 CHKPV(scope);
263
264 napi_value napiResult = nullptr;
265 CHKRV_SCOPE(env_, napi_create_object(env_, &napiResult), CREATE_OBJECT, scope);
266 napi_value tmpValue = nullptr;
267 CHKRV_SCOPE(env_, napi_create_int32(env_, result, &tmpValue), CREATE_INT32, scope);
268 CHKRV_SCOPE(env_, napi_set_named_property(env_, napiResult, "ageGroup", tmpValue), SET_NAMED_PROPERTY, scope);
269
270 double temp = confidence;
271 std::stringstream ss;
272 ss << std::fixed << std::setprecision(FLOAT_PRECSION) << temp;
273 ss >> temp;
274 CHKRV_SCOPE(env_, napi_create_double(env_, temp, &tmpValue), "napi_create_double", scope);
275 CHKRV_SCOPE(env_, napi_set_named_property(env_, napiResult, "confidence", tmpValue), SET_NAMED_PROPERTY, scope);
276 napi_value callResult = nullptr;
277 CHKRV_SCOPE(env_, napi_call_function(env_, nullptr, handler, TWO_PARAMETER, &napiResult, &callResult),
278 CALL_FUNCTION, scope);
279 napi_close_handle_scope(env_, scope);
280 }
281
IsSameValue(const napi_env & env,const napi_value & lhs,const napi_value & rhs)282 bool UnderageModelNapiEvent::IsSameValue(const napi_env &env, const napi_value &lhs, const napi_value &rhs)
283 {
284 FI_HILOGD("Enter");
285 bool result = false;
286 napi_status status = napi_strict_equals(env, lhs, rhs, &result);
287 if (status != napi_ok) {
288 FI_HILOGE("napi_strict_equals failed");
289 }
290 return result;
291 }
292
CheckEvents(uint32_t eventType)293 bool UnderageModelNapiEvent::CheckEvents(uint32_t eventType)
294 {
295 FI_HILOGD("Enter");
296 std::lock_guard<std::mutex> lock(eventsMutex_);
297 auto typeIter = events_.find(eventType);
298 if (typeIter == events_.end()) {
299 FI_HILOGD("eventType %{public}d not find", eventType);
300 return false;
301 }
302 return !typeIter->second->onRefSets.empty();
303 }
304 } // namespace DeviceStatus
305 } // namespace Msdp
306 } // namespace OHOS
307