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