1 /* 2 * Copyright (c) 2022 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 CONFIG_POLICY_NAPI_H 17 #define CONFIG_POLICY_NAPI_H 18 19 #include <string> 20 21 #include "napi/native_common.h" 22 #include "napi/native_node_api.h" 23 #include "napi/native_api.h" 24 25 namespace OHOS { 26 namespace Customization { 27 namespace ConfigPolicy { 28 struct ConfigAsyncContext { 29 napi_async_work work_; 30 napi_deferred deferred_; 31 napi_ref callbackRef_; 32 typedef napi_value (*CreateNapiValue)(napi_env env, ConfigAsyncContext &context); 33 CreateNapiValue createValueFunc_; 34 35 std::string relPath_; 36 std::string pathValue_; 37 std::vector<std::string> paths_; 38 }; 39 40 class ConfigPolicyNapi { 41 public: 42 ConfigPolicyNapi(); 43 ~ConfigPolicyNapi() = default; 44 static napi_value Init(napi_env env, napi_value exports); 45 46 private: 47 static napi_value NAPIGetOneCfgFile(napi_env env, napi_callback_info info); 48 static napi_value NAPIGetCfgFiles(napi_env env, napi_callback_info info); 49 static napi_value NAPIGetCfgDirList(napi_env env, napi_callback_info info); 50 static napi_value CreateUndefined(napi_env env); 51 static std::string GetStringFromNAPI(napi_env env, napi_value value); 52 static napi_value HandleAsyncWork(napi_env env, ConfigAsyncContext *context, std::string workName, 53 napi_async_execute_callback execute, napi_async_complete_callback complete); 54 static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); 55 static void NativeGetOneCfgFile(napi_env env, void *data); 56 static void NativeGetCfgFiles(napi_env env, void *data); 57 static void NativeGetCfgDirList(napi_env env, void *data); 58 static void NativeCallbackComplete(napi_env env, napi_status status, void *data); 59 static napi_value ParseRelPath(napi_env env, std::string ¶m, napi_value args); 60 static void CreateArraysValueFunc(ConfigAsyncContext &context); 61 static napi_value ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage); 62 }; 63 } // namespace ConfigPolicy 64 } // namespace Customization 65 } // namespace OHOS 66 #endif 67