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 // LCOV_EXCL_START
~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 callbackContext_ = nullptr;
36 }
37 };
38
NapiGetNamedProperty(const napi_env env,const napi_value & object,const std::string & name)39 napi_value NapiSecurityEventQuerier::NapiGetNamedProperty(const napi_env env, const napi_value &object,
40 const std::string &name)
41 {
42 napi_value result = nullptr;
43 napi_status status = napi_get_named_property(env, object, name.c_str(), &result);
44 if (status != napi_ok || result == nullptr) {
45 SGLOGE("failed to parse property named %{public}s from JS object.", name.c_str());
46 }
47 return result;
48 }
49
NapiCreateString(const napi_env env,const std::string & value)50 napi_value NapiSecurityEventQuerier::NapiCreateString(const napi_env env, const std::string &value)
51 {
52 napi_value result = nullptr;
53 napi_status status = napi_create_string_utf8(env, value.c_str(), NAPI_AUTO_LENGTH, &result);
54 SGLOGD("create napi value of string type, value is %{public}s.", value.c_str());
55 if (status != napi_ok || result == nullptr) {
56 SGLOGE("failed to create napi value of string type.");
57 }
58 return result;
59 }
60
NapiCreateInt64(const napi_env env,int64_t value)61 napi_value NapiSecurityEventQuerier::NapiCreateInt64(const napi_env env, int64_t value)
62 {
63 napi_value result = nullptr;
64 napi_status status = napi_create_int64(env, value, &result);
65 SGLOGI("create napi value of int64 type, value is %{public}" PRId64, value);
66 if (status != napi_ok || result == nullptr) {
67 SGLOGE("failed to create napi value of int64 type.");
68 }
69 return result;
70 }
71
RunCallback(QuerySecurityEventContext * context,CALLBACK_FUNC callback,RELEASE_FUNC release)72 void NapiSecurityEventQuerier::RunCallback(QuerySecurityEventContext *context, CALLBACK_FUNC callback,
73 RELEASE_FUNC release)
74 {
75 if (context == nullptr) {
76 SGLOGE("context is nullptr");
77 return;
78 }
79 auto tmpContext = std::make_shared<QuerySecurityEventContext>(context);
80 auto task = [tmpContext, callback, release]() {
81 napi_handle_scope scope = nullptr;
82 napi_open_handle_scope(tmpContext->env, &scope);
83 if (scope == nullptr) {
84 return;
85 }
86 if (callback != nullptr) {
87 SGLOGD("Begin execute callback.");
88 callback(tmpContext->env, tmpContext->ref, tmpContext->threadId, tmpContext->events);
89 }
90 napi_close_handle_scope(tmpContext->env, scope);
91 if (release != nullptr) {
92 release(tmpContext->threadId);
93 }
94 };
95 napi_send_event(tmpContext->env, task, napi_eprio_high);
96 }
97
OnQuery(const std::vector<SecurityCollector::SecurityEvent> & events)98 void NapiSecurityEventQuerier::OnQuery(const std::vector<SecurityCollector::SecurityEvent> &events)
99 {
100 SGLOGD("NAPI OnQuery.");
101 callbackContext_->events = events;
102
103 RunCallback(callbackContext_,
104 [this] (const napi_env env, const napi_ref ref, pid_t threadId,
105 const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
106 SGLOGD("NAPI OnQuery Callback.");
107 if (threadId != getproctid() || !NapiRequestDataManager::GetInstance().GetDataCallback(env)) {
108 return;
109 }
110 napi_value eventJsArray = nullptr;
111 napi_create_array_with_length(env, napiEvents.size(), &eventJsArray);
112 auto len = napiEvents.size();
113 for (size_t i = 0; i < len; i++) {
114 napi_value item = nullptr;
115 napi_status status = napi_create_object(env, &item);
116 if (status != napi_ok) {
117 SGLOGE("napi_create_object failed, %{public}d", status);
118 return;
119 }
120 napi_value eventId = NapiCreateInt64(env, napiEvents[i].GetEventId());
121 napi_value version = NapiCreateString(env, napiEvents[i].GetVersion().c_str());
122 napi_value content = NapiCreateString(env, napiEvents[i].GetContent().c_str());
123 napi_value timestamp = NapiCreateString(env, napiEvents[i].GetTimestamp().c_str());
124 napi_set_named_property(env, item, "eventId", eventId);
125 napi_set_named_property(env, item, "version", version);
126 napi_set_named_property(env, item, "content", content);
127 napi_set_named_property(env, item, "timestamp", timestamp);
128 status = napi_set_element(env, eventJsArray, i, item);
129 if (status != napi_ok) {
130 SGLOGE("napi_set_element failed, %{public}d", status);
131 return;
132 }
133 }
134 napi_value argv[1] = {eventJsArray};
135 napi_value querier = nullptr;
136 napi_get_reference_value(env, ref, &querier);
137 napi_value onQuery = NapiGetNamedProperty(env, querier, ON_QUERY_ATTR);
138 napi_value ret = nullptr;
139 SGLOGD("NAPI begin call OnQuery.");
140 napi_status res = napi_call_function(env, querier, onQuery, 1, argv, &ret);
141 if (res != napi_ok) {
142 SGLOGE("failed to call OnQuery JS function. %{public}d", res);
143 }
144 SGLOGD("NAPI OnQuery Callback END.");
145 }, nullptr);
146 };
147
OnComplete()148 void NapiSecurityEventQuerier::OnComplete()
149 {
150 RunCallback(callbackContext_, [] (const napi_env env, const napi_ref ref, pid_t threadId,
151 const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
152 SGLOGD("NAPI OnComplete Callback.");
153 napi_value querier = nullptr;
154 napi_get_reference_value(env, ref, &querier);
155 napi_value onComplete = NapiGetNamedProperty(env, querier, ON_COMPLETE_ATTR);
156 napi_value ret = nullptr;
157 napi_status status = napi_call_function(env, querier, onComplete, 0, nullptr, &ret);
158 if (status != napi_ok) {
159 SGLOGE("failed to call onComplete JS function.");
160 }
161 SGLOGD("NAPI OnComplete Callback END.");
162 }, [this] (pid_t threadId) {
163 SGLOGD("NAPI OnComplete Release.");
164 if (threadId != getproctid()) {
165 return;
166 }
167 if (onCompleteHandler_ != nullptr && callbackContext_ != nullptr) {
168 onCompleteHandler_(callbackContext_->env, callbackContext_->ref);
169 }
170 SGLOGD("NAPI OnComplete Release END.");
171 });
172 };
173
OnError(const std::string & message)174 void NapiSecurityEventQuerier::OnError(const std::string &message)
175 {
176 RunCallback(callbackContext_, [message] (const napi_env env, const napi_ref ref, pid_t threadId,
177 const std::vector<SecurityCollector::SecurityEvent> &napiEvents) {
178 SGLOGD("NAPI OnError.");
179 napi_value jsMessage = NapiCreateString(env, message);
180 napi_value argv[1] = {jsMessage};
181 napi_value querier = nullptr;
182 napi_get_reference_value(env, ref, &querier);
183 napi_value onQuery = NapiGetNamedProperty(env, querier, ON_ERROR_ATTR);
184 napi_value ret = nullptr;
185 napi_status status = napi_call_function(env, querier, onQuery, 1, argv, &ret);
186 if (status != napi_ok) {
187 SGLOGE("failed to call OnQuery JS function.");
188 }
189 SGLOGD("NAPI OnError END.");
190 }, [this] (pid_t threadId) {
191 if (threadId != getproctid()) {
192 return;
193 }
194 if (onCompleteHandler_ != nullptr && callbackContext_ != nullptr) {
195 onCompleteHandler_(callbackContext_->env, callbackContext_->ref);
196 }
197 });
198 };
199 // LCOV_EXCL_STOP
200 } // OHOS::Security::SecurityGuard