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 #ifndef NAPI_SECURITY_EVENT_QUERIER_H 17 #define NAPI_SECURITY_EVENT_QUERIER_H 18 19 #include <functional> 20 #include <string> 21 #include <vector> 22 23 #include "uv.h" 24 25 #include "security_guard_napi.h" 26 #include "security_event_query_callback.h" 27 28 namespace OHOS::Security::SecurityGuard { 29 constexpr char ON_QUERY_ATTR[] = "onQuery"; 30 constexpr char ON_COMPLETE_ATTR[] = "onComplete"; 31 constexpr char ON_ERROR_ATTR[] = "onError"; 32 using ON_COMPLETE_FUNC = std::function<void(const napi_env, const napi_ref)>; 33 class NapiSecurityEventQuerier : public SecurityEventQueryCallback { 34 public: 35 NapiSecurityEventQuerier(QuerySecurityEventContext *context, ON_COMPLETE_FUNC handler); 36 ~NapiSecurityEventQuerier() override; 37 static napi_value NapiGetNamedProperty(const napi_env env, const napi_value &object, const std::string &name); 38 static napi_value NapiCreateString(const napi_env env, const std::string &value); 39 static napi_value NapiCreateInt64(const napi_env env, int64_t value); 40 static void DestoryWork(uv_work_t *work); 41 void RunCallback(QuerySecurityEventContext *context, CALLBACK_FUNC callback, RELEASE_FUNC release); 42 void OnQuery(const std::vector<SecurityCollector::SecurityEvent> &events) override; 43 void OnComplete() override; 44 void OnError(const std::string &message) override; 45 46 private: 47 QuerySecurityEventContext* callbackContext_; 48 ON_COMPLETE_FUNC onCompleteHandler_; 49 }; 50 } // namespace OHOS::Security::SecurityGuard 51 52 #endif // NAPI_SECURITY_EVENT_QUERIER_H 53