1 /* 2 * Copyright (c) 2023 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 SECURITY_GUARD_NAPI_H 17 #define SECURITY_GUARD_NAPI_H 18 19 #include <thread> 20 #include "napi/native_api.h" 21 #include "napi/native_node_api.h" 22 23 #include "security_event.h" 24 #include "security_guard_define.h" 25 #include "event_define.h" 26 27 constexpr int CONDITIONS_MAX_LEN = 100; 28 constexpr int VERSION_MAX_LEN = 50; 29 constexpr int CONTENT_MAX_LEN = 900; 30 constexpr int EXTRA_MAX_LEN = 2000; 31 constexpr int DEVICE_ID_MAX_LEN = 64; 32 constexpr int FILE_NAME_MAX_LEN = 64; 33 constexpr int MODEL_NAME_MAX_LEN = 64; 34 constexpr int PARAM_MAX_LEN = 900; 35 constexpr int NAPI_ON_RESULT_ARGS_CNT = 3; 36 constexpr char NAPI_ON_RESULT_ATTR[] = "onResult"; 37 constexpr char NAPI_SECURITY_MODEL_RESULT_DEVICE_ID_ATTR[] = "deviceId"; 38 constexpr char NAPI_SECURITY_MODEL_RESULT_MODEL_ID_ATTR[] = "modelId"; 39 constexpr char NAPI_SECURITY_MODEL_RESULT_RESULT_ATTR[] = "result"; 40 constexpr int32_t TIMEOUT_REPLY = 500; 41 42 struct RequestSecurityEventInfoContext { 43 napi_env env = nullptr; 44 napi_ref ref = nullptr; 45 uint32_t status = 0; 46 pid_t threadId; 47 std::string devId; 48 std::string info; 49 std::vector<int64_t> eventIds; 50 std::string beginTime; 51 std::string endTime; 52 std::string conditions; 53 std::string errMsg; 54 napi_ref dataCallback = nullptr; 55 napi_ref endCallback = nullptr; 56 napi_ref errorCallback = nullptr; 57 }; 58 59 struct RequestSecurityModelResultContext { 60 napi_env env = nullptr; 61 napi_ref ref = nullptr; 62 napi_deferred deferred; 63 napi_async_work asyncWork; 64 std::string deviceId; 65 uint32_t modelId; 66 OHOS::Security::SecurityGuard::SecurityModel result; 67 int32_t ret; 68 }; 69 70 struct ReportSecurityEventInfoContext { 71 int64_t eventId; 72 std::string version; 73 std::string content; 74 }; 75 76 77 struct NotifyCollectorContext { 78 OHOS::Security::SecurityCollector::Event event; 79 int64_t duration; 80 }; 81 82 struct NapiSecurityEventRuler { 83 int64_t eventId; 84 std::string beginTime; 85 std::string endTime; 86 std::string param; 87 }; 88 89 struct NapiSecurityEvent { 90 int64_t eventId; 91 std::string version; 92 std::string content; 93 std::string timestamp; 94 }; 95 struct SubscribeEventInfo { 96 int64_t eventId; 97 }; 98 99 struct ModelRule { 100 std::string modelName; 101 std::string param; 102 }; 103 104 struct NapiSecurityPolicyFileInfo { 105 napi_env env = nullptr; 106 napi_ref ref = nullptr; 107 napi_deferred deferred; 108 napi_async_work asyncWork; 109 std::string fileName; 110 int32_t fd; 111 int32_t ret; 112 }; 113 using CALLBACK_FUNC = std::function<void(const napi_env, const napi_ref, pid_t threadId, 114 const std::vector<OHOS::Security::SecurityCollector::SecurityEvent> &napiEvents)>; 115 using RELEASE_FUNC = std::function<void(pid_t threadId)>; 116 117 struct QuerySecurityEventContext { 118 QuerySecurityEventContext() = default; QuerySecurityEventContextQuerySecurityEventContext119 explicit QuerySecurityEventContext(QuerySecurityEventContext *context) 120 : env(context->env), ref(context->ref), 121 callback(context->callback), 122 release(context->release), 123 threadId(context->threadId), 124 events(context->events) {}; 125 126 napi_env env = nullptr; 127 napi_ref ref = nullptr; 128 CALLBACK_FUNC callback; 129 RELEASE_FUNC release; 130 pid_t threadId; 131 std::vector<OHOS::Security::SecurityCollector::SecurityEvent> events; 132 }; 133 134 enum EventIdType : int64_t { 135 PRINTER_EVENT_ID = 1011015004 136 }; 137 138 enum ModelIdType : uint32_t { 139 ROOT_SCAN_MODEL_ID = 3001000000, 140 DEVICE_COMPLETENESS_MODEL_ID = 3001000001, 141 PHYSICAL_MACHINE_DETECTION_MODEL_ID = 3001000002, 142 SECURITY_AUDIT_MODEL_ID = 3001000003, 143 }; 144 145 enum JsErrCode : int32_t { 146 JS_ERR_SUCCESS = 0, 147 JS_ERR_NO_PERMISSION = 201, 148 JS_ERR_NO_SYSTEMCALL = 202, 149 JS_ERR_BAD_PARAM = 401, 150 JS_ERR_SYS_ERR = 21200001, 151 }; 152 153 class SubscriberPtr; 154 struct CommonAsyncContext { CommonAsyncContextCommonAsyncContext155 CommonAsyncContext() {}; 156 explicit CommonAsyncContext(napi_env napiEnv, std::thread::id thId, 157 bool throwAble = false) : env(napiEnv), threadId(thId), throwErr(throwAble) {}; ~CommonAsyncContextCommonAsyncContext158 virtual ~CommonAsyncContext() 159 { 160 if (env == nullptr) { 161 return; 162 } 163 if (callbackRef != nullptr) { 164 napi_delete_reference(env, callbackRef); 165 callbackRef = nullptr; 166 } 167 if (work != nullptr) { 168 napi_delete_async_work(env, work); 169 work = nullptr; 170 } 171 }; 172 napi_env env = nullptr; 173 napi_async_work work = nullptr; 174 napi_deferred deferred = nullptr; 175 napi_ref callbackRef = nullptr; 176 napi_status status = napi_ok; 177 int32_t errCode = 0; 178 std::string errMsg; 179 std::thread::id threadId; 180 bool throwErr = false; 181 }; 182 struct SubscribeCBInfo : public CommonAsyncContext { SubscribeCBInfoSubscribeCBInfo183 explicit SubscribeCBInfo(napi_env napiEnv, 184 std::thread::id thId) : CommonAsyncContext(napiEnv, thId) {}; 185 OHOS::Security::SecurityCollector::Event events; 186 std::shared_ptr<SubscriberPtr> subscriber = nullptr; 187 }; 188 189 struct UnsubscribeCBInfo : public CommonAsyncContext { UnsubscribeCBInfoUnsubscribeCBInfo190 explicit UnsubscribeCBInfo(napi_env napiEnv, 191 std::thread::id thId) : CommonAsyncContext(napiEnv, thId){}; 192 std::vector<std::shared_ptr<SubscriberPtr>> subscribers; 193 }; 194 195 196 struct SubscriberOAWorker : public CommonAsyncContext { 197 NapiSecurityEvent event; 198 napi_ref ref = nullptr; 199 SubscriberPtr *subscriber = nullptr; 200 }; 201 #endif // SECURITY_GUARD_NAPI_H