diff --git a/BUILD.gn b/BUILD.gn index 0a8f478dc5e93c44f74d55e535ca7f9f6e77d0fd..a0c78ea312e7d7ab9a4a5d37579d46e5b5788696 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -17,6 +17,7 @@ group("config_policy_components") { if (os_level == "standard" && support_config_policy_napi && support_jsapi) { deps = [ "./frameworks/config_policy:configpolicy_util", + "./interfaces/kits/ffi:cj_config_policy_ffi", "./interfaces/kits/js:configpolicy", ] } else { diff --git a/interfaces/kits/ffi/BUILD.gn b/interfaces/kits/ffi/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e94c5e2044fcfbe1b1f7ce2b9a1fbf876eb069fb --- /dev/null +++ b/interfaces/kits/ffi/BUILD.gn @@ -0,0 +1,55 @@ +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_shared_library("cj_config_policy_ffi") { + include_dirs = [ + "include", + "../../../interfaces/inner_api/include", + "//third_party/bounds_checking_function/include", + "../../../frameworks/dfx/hisysevent_adapter", + ] + if (!defined(defines)) { + defines = [] + } + + if (product_name != "ohos-sdk") { + deps = [ + "../../../frameworks/config_policy:configpolicy_util", + "//third_party/bounds_checking_function:libsec_shared", + ] + external_deps = [ + "hilog:libhilog", + "hisysevent:libhisysevent", + ] + sources = [ + "../../../frameworks/dfx/hisysevent_adapter/hisysevent_adapter.cpp", + "src/config_policy_ffi.cpp", + ] + } else { + defines += [ "PREVIEWER" ] + sources = [ "src/config_policy_mock.cpp" ] + } + + if (current_os == "ohos") { + defines += [ "OHOS_PLATFORM" ] + } + + if (current_os == "mingw") { + defines += [ "WINDOWS_PLATFORM" ] + } + + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/interfaces/kits/ffi/src/config_policy_ffi.cpp b/interfaces/kits/ffi/src/config_policy_ffi.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3af9e30f524d2cb74e79ae3bcc3776dab0e32f09 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "config_policy_ffi.h" + +#include +#include +#include "config_policy_utils.h" +#include "hisysevent_adapter.h" +#include "config_policy_log.h" + +namespace OHOS::Customization::ConfigPolicy { + +static constexpr int MAX_MALLOC_LEN = 1024; + +char** MallocCStringArr(const std::vector& origin) +{ + if (origin.empty()) { + return nullptr; + } + auto size = origin.size(); + if (size == 0 || size > MAX_MALLOC_LEN) { + return nullptr; + } + auto arr = static_cast(malloc(sizeof(char*) * size)); + if (arr == nullptr) { + return nullptr; + } + for (size_t i = 0; i < size; i++) { + size_t len = strlen(origin[i]) + 1; + arr[i] = static_cast(malloc(sizeof(char) * len)); + if (arr[i] == nullptr) { + continue; + } + errno_t ret = strcpy_s(arr[i], len, origin[i]); + if (ret != 0) { + free(arr[i]); + arr[i] = nullptr; + } + } + return arr; +} + +extern "C" { + RetDataCArrString CJ_GetCfgDirList() + { + LOGI("CJ_GetCfgDirList start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + CfgDir *cfgDir = GetCfgDirList(); + + std::vector dirList; + if (cfgDir != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgDir->paths[i] != nullptr) { + dirList.push_back(cfgDir->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(dirList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : dirList.size()); + + FreeCfgDirList(cfgDir); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirList", ""); + LOGI("CJ_GetCfgDirList ok"); + return ret; + } + + RetDataCArrString CJ_GetCfgFiles(const char* relPath) + { + LOGI("CJ_GetCfgFiles start"); + RetDataCArrString ret = { .code = SUCCESS_CODE, .data = { .head = nullptr, .size = 0 } }; + LOGI("input path: [%{public}s]", relPath) + std::string extra(""); + CfgFiles *cfgFiles = GetCfgFilesEx(relPath, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + std::vector fileList; + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + fileList.push_back(cfgFiles->paths[i]); + } + } + } + + ret.data.head = MallocCStringArr(fileList); + ret.data.size = static_cast(ret.data.head == nullptr ? 0 : fileList.size()); + + FreeCfgFiles(cfgFiles); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFiles", ""); + return ret; + } + + RetDataCString CJ_GetOneCfgFile(const char* relPath) + { + LOGI("CJ_GetOneCfgFile start"); + RetDataCString ret = { .code = SUCCESS_CODE, .data = nullptr }; + char outBuf[MAX_PATH_LEN] = {0}; + std::string extra(""); + char* filePath = GetOneCfgFileEx(relPath, outBuf, MAX_PATH_LEN, FOLLOWX_MODE_DEFAULT, extra.c_str()); + + if (filePath == nullptr) { + LOGI("GetOneCfgFileEx result is nullptr."); + return ret; + } else { + LOGI("GetOneCfgFile return [%{public}s]", filePath); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFile", ""); + size_t pathLen = strlen(filePath) + 1; + ret.data = static_cast(malloc(sizeof(char) * pathLen)); + if (ret.data == nullptr) { + return ret; + } + errno_t err = strcpy_s(ret.data, pathLen, filePath); + if (err != 0) { + free(ret.data); + ret.data = nullptr; + } + return ret; + } +} +} // namespace OHOS::Customization::ConfigPolicy diff --git a/interfaces/kits/ffi/src/config_policy_ffi.h b/interfaces/kits/ffi/src/config_policy_ffi.h new file mode 100644 index 0000000000000000000000000000000000000000..415125665b72730ca37a325772df080241c934fb --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_ffi.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONFIG_POLICY_FFI_H +#define CONFIG_POLICY_FFI_H + +#include + +#define FFI_EXPORT __attribute__((visibility("default"))) + +extern "C" { + const int32_t SUCCESS_CODE = 0; + struct CArrString { + char** head; + int64_t size; + }; + + struct RetDataCArrString { + int32_t code; + CArrString data; + }; + + struct RetDataCString { + int32_t code; + char* data; + }; + + FFI_EXPORT RetDataCArrString CJ_GetCfgDirList(); + FFI_EXPORT RetDataCArrString CJ_GetCfgFiles(const char* relPath); + FFI_EXPORT RetDataCString CJ_GetOneCfgFile(const char* relPath); +} + +#endif \ No newline at end of file diff --git a/interfaces/kits/ffi/src/config_policy_log.h b/interfaces/kits/ffi/src/config_policy_log.h new file mode 100644 index 0000000000000000000000000000000000000000..afe700251bdb0b24270be7a7e2f361adc8df81c9 --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_log.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CONFIG_POLICY_LOG_H +#define CONFIG_POLICY_LOG_H + +#include "hilog/log.h" + +#ifdef LOG_DOMAIN +#undef LOG_DOMAIN +#endif +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#define LOG_DOMAIN 0xD001E00 +#define LOG_TAG "ConfigPolicyFFI" + +#define LOGI(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_INFO)) { \ + HILOG_INFO(LOG_CORE, ##__VA_ARGS__); \ +} + +#define LOGE(...) \ +if (HiLogIsLoggable(LOG_DOMAIN, LOG_TAG, LOG_ERROR)) { \ + HILOG_ERROR(LOG_CORE, ##__VA_ARGS__); \ +} + +#endif diff --git a/interfaces/kits/ffi/src/config_policy_mock.cpp b/interfaces/kits/ffi/src/config_policy_mock.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c53d335d1acfa0ceeeba6e794a9a1e2339a241af --- /dev/null +++ b/interfaces/kits/ffi/src/config_policy_mock.cpp @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +extern "C" { +FFI_EXPORT int CJ_GetCfgDirList = 0; +FFI_EXPORT int CJ_GetCfgFiles = 0; +FFI_EXPORT int CJ_GetOneCfgFile = 0; +} \ No newline at end of file