1 /*
2  * Copyright (c) 2024 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 "napi_security_event_querier.h"
17 
18 #include <unistd.h>
19 
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "napi_request_data_manager.h"
23 
24 namespace OHOS::Security::SecurityGuard {
NapiSecurityEventQuerier(QuerySecurityEventContext *context, ON_COMPLETE_FUNC handler)25 NapiSecurityEventQuerier::NapiSecurityEventQuerier(QuerySecurityEventContext *context, ON_COMPLETE_FUNC handler)
26     : callbackContext_(context), onCompleteHandler_(handler) {};
27 
~NapiSecurityEventQuerier()28 NapiSecurityEventQuerier::~NapiSecurityEventQuerier()
29 {
30     if (callbackContext_ != nullptr) {
31         if (callbackContext_->threadId == getproctid()) {
32             napi_delete_reference(callbackContext_->env, callbackContext_->ref);
33         }
34         delete callbackContext_;
35     }
36 };
37 
NapiGetNamedProperty(const napi_env env, const napi_value &object, const std::string &name)38 napi_value NapiSecurityEventQuerier::NapiGetNamedProperty(const napi_env env, const napi_value &object,
39     const std::string &name)
40 {
41     napi_value result = nullptr;
42     napi_status status = napi_get_named_property(env, object, name.c_str(), &result);
43     if (status != napi_ok || result == nullptr) {
44         SGLOGE("failed to parse property named %{public}s from JS object.", name.c_str());
45     }
46     return result;
47 }
48 
NapiCreateString(const napi_env env, const std::string &value)49 napi_value NapiSecurityEventQuerier::NapiCreateString(const napi_env env, const std::string &value)
50 {
51     napi_value result = nullptr;
52     napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
53     SGLOGD("create napi value of string type, value is %{public}s.", value.c_str());
54     if (status != napi_ok || result == nullptr) {
55         SGLOGE("failed to create napi value of string type.");
56     }
57     return result;
58 }
59 
NapiCreateInt64(const napi_env env, int64_t value)60 napi_value NapiSecurityEventQuerier::NapiCreateInt64(const napi_env env, int64_t value)
61 {
62     napi_value result = nullptr;
63     napi_status status = napi_create_int64(env, value, &result);
64     SGLOGI("create napi value of int64 type, value is %{public}" PRId64, value);
65     if (status != napi_ok || result == nullptr) {
66         SGLOGE("failed to create napi value of int64 type.");
67     }
68     return result;
69 }
70 
DestoryWork(uv_work_t *work)71 void NapiSecurityEventQuerier::DestoryWork(uv_work_t *work)
72 {
73     if (work == nullptr) {
74         return;
75     }
76     if (work->data != nullptr) {
77         delete (reinterpret_cast<QuerySecurityEventContext *>(work->data));
78     }
79     delete work;
80 }
81 
OnExecute(uv_work_t *work)82 static void OnExecute(uv_work_t *work)
83 {
84     SGLOGD("begin executr work error");
85 }
86 
RunCallback(QuerySecurityEventContext *context, CALLBACK_FUNC callback, RELEASE_FUNC release)87 void NapiSecurityEventQuerier::RunCallback(QuerySecurityEventContext *context, CALLBACK_FUNC callback,
88     RELEASE_FUNC release)
89 {
90     uv_loop_t* loop = nullptr;
91     napi_get_uv_event_loop(context->env, &loop);
92     if (loop == nullptr) {
93         SGLOGE("failed to get uv_loop.");
94         return;
95     }
96 
97     QuerySecurityEventContext *tmpContext = new (std::nothrow) QuerySecurityEventContext(context);
98     if (tmpContext == nullptr) {
99         SGLOGE("tmpContext new failed, no memory left.");
100         return;
101     }
102     tmpContext->callback = callback;
103     tmpContext->release = release;
104     uv_work_t* work = new (std::nothrow) uv_work_t();
105     if (work == nullptr) {
106         SGLOGE("uv_work new failed, no memory left.");
107         delete tmpContext;
108         return;
109     }
110     work->data = reinterpret_cast<void *>(tmpContext);
111     uv_queue_work_with_qos(loop, work, OnExecute,
112         [] (uv_work_t *work, int status) {
113             SGLOGD("Begin uv work.");
114             if (work == nullptr || work->data == nullptr) {
115                 DestoryWork(work);
116                 return;
117             }
118             QuerySecurityEventContext *context = reinterpret_cast<QuerySecurityEventContext*>(work->data);
119             if (context == nullptr || context->env == nullptr) {
120                 DestoryWork(work);
121                 return;
122             }
123             napi_handle_scope scope = nullptr;
124             napi_open_handle_scope(context->env, &scope);
125             if (scope == nullptr) {
126                 DestoryWork(work);
127                 return;
128             }
129             if (context->callback != nullptr) {
130                 SGLOGD("Begin execute callback.");
131                 context->callback(context->env, context->ref, context->threadId, context->events);
132             }
133             napi_close_handle_scope(context->env, scope);
134             if (context->release != nullptr) {
135                 context->release(context->threadId);
136             }
137             DestoryWork(work);
138         }, uv_qos_default);
139 }
140 
OnQuery(const std::vector<SecurityCollector::SecurityEvent> &events)141 void NapiSecurityEventQuerier::OnQuery(const std::vector<SecurityCollector::SecurityEvent> &events)
142 {
143     SGLOGD("NAPI OnQuery.");
144     callbackContext_->events = events;
145 
146     RunCallback(callbackContext_,
147         [this] (const napi_env env, const napi_ref ref, pid_t threadId,
148             const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
149             SGLOGD("NAPI OnQuery Callback.");
150             if (threadId != getproctid() || !NapiRequestDataManager::GetInstance().GetDataCallback(env)) {
151                 return;
152             }
153             napi_value eventJsArray = nullptr;
154             napi_create_array_with_length(env, napiEvents.size(), &eventJsArray);
155             auto len = napiEvents.size();
156             for (size_t i = 0; i < len; i++) {
157                 napi_value item = nullptr;
158                 napi_status status = napi_create_object(env, &item);
159                 if (status != napi_ok) {
160                     SGLOGE("napi_create_object failed, %{public}d", status);
161                     return;
162                 }
163                 napi_value eventId = NapiCreateInt64(env, napiEvents[i].GetEventId());
164                 napi_value version = NapiCreateString(env, napiEvents[i].GetVersion().c_str());
165                 napi_value content = NapiCreateString(env, napiEvents[i].GetContent().c_str());
166                 napi_value timestamp = NapiCreateString(env, napiEvents[i].GetTimestamp().c_str());
167                 napi_set_named_property(env, item, "eventId", eventId);
168                 napi_set_named_property(env, item, "version", version);
169                 napi_set_named_property(env, item, "content", content);
170                 napi_set_named_property(env, item, "timestamp", timestamp);
171                 status = napi_set_element(env, eventJsArray, i, item);
172                 if (status != napi_ok) {
173                     SGLOGE("napi_set_element failed, %{public}d", status);
174                     return;
175                 }
176             }
177             napi_value argv[1] = {eventJsArray};
178             napi_value querier = nullptr;
179             napi_get_reference_value(env, ref, &querier);
180             napi_value onQuery = NapiGetNamedProperty(env, querier, ON_QUERY_ATTR);
181             napi_value ret = nullptr;
182             SGLOGD("NAPI begin call OnQuery.");
183             napi_status res = napi_call_function(env, querier, onQuery, 1, argv, &ret);
184             if (res != napi_ok) {
185                 SGLOGE("failed to call OnQuery JS function. %{public}d", res);
186             }
187             SGLOGD("NAPI OnQuery Callback END.");
188         }, nullptr);
189 };
190 
OnComplete()191 void NapiSecurityEventQuerier::OnComplete()
192 {
193     RunCallback(callbackContext_, [] (const napi_env env, const napi_ref ref, pid_t threadId,
194             const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
195         SGLOGD("NAPI OnComplete Callback.");
196         napi_value querier = nullptr;
197         napi_get_reference_value(env, ref, &querier);
198         napi_value onComplete = NapiGetNamedProperty(env, querier, ON_COMPLETE_ATTR);
199         napi_value ret = nullptr;
200         napi_status status = napi_call_function(env, querier, onComplete, 0, nullptr, &ret);
201         if (status != napi_ok) {
202             SGLOGE("failed to call onComplete JS function.");
203         }
204         SGLOGD("NAPI OnComplete Callback END.");
205     }, [this] (pid_t threadId) {
206         SGLOGD("NAPI OnComplete Release.");
207         if (threadId != getproctid()) {
208             return;
209         }
210         if (onCompleteHandler_ != nullptr && callbackContext_ != nullptr) {
211             onCompleteHandler_(callbackContext_->env, callbackContext_->ref);
212         }
213         SGLOGD("NAPI OnComplete Release END.");
214     });
215 };
216 
OnError(const std::string &message)217 void NapiSecurityEventQuerier::OnError(const std::string &message)
218 {
219     RunCallback(callbackContext_, [message] (const napi_env env, const napi_ref ref, pid_t threadId,
220             const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
221         SGLOGD("NAPI OnError.");
222         napi_value jsMessage = NapiCreateString(env, message);
223         napi_value argv[1] = {jsMessage};
224         napi_value querier = nullptr;
225         napi_get_reference_value(env, ref, &querier);
226         napi_value onQuery = NapiGetNamedProperty(env, querier, ON_ERROR_ATTR);
227         napi_value ret = nullptr;
228         napi_status status = napi_call_function(env, querier, onQuery, 1, argv, &ret);
229         if (status != napi_ok) {
230             SGLOGE("failed to call OnQuery JS function.");
231         }
232         SGLOGD("NAPI OnError END.");
233     },  [this] (pid_t threadId) {
234         if (threadId != getproctid()) {
235             return;
236         }
237         if (onCompleteHandler_ != nullptr && callbackContext_ != nullptr) {
238             onCompleteHandler_(callbackContext_->env, callbackContext_->ref);
239         }
240     });
241 };
242 } // OHOS::Security::SecurityGuard