From 886c185d23eab38c4db5c01e9d9e0168df9c721b Mon Sep 17 00:00:00 2001 From: xiekaiming Date: Fri, 17 May 2024 14:32:47 +0800 Subject: [PATCH] add getchannelid Signed-off-by: xiekaiming --- BUILD.gn | 1 + bundle.json | 1 + .../config_policy/etc/customization.para.dac | 4 +- interfaces/kits/js/BUILD.gn | 27 +++- .../kits/js/include/custom_config_napi.h | 44 ++++++ interfaces/kits/js/src/custom_config_napi.cpp | 127 ++++++++++++++++++ 6 files changed, 201 insertions(+), 3 deletions(-) create mode 100644 interfaces/kits/js/include/custom_config_napi.h create mode 100644 interfaces/kits/js/src/custom_config_napi.cpp diff --git a/BUILD.gn b/BUILD.gn index 85822f7..d1fc028 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,6 +19,7 @@ group("config_policy_components") { "./frameworks/config_policy:configpolicy_util", "./interfaces/kits/cj:cj_config_policy_ffi", "./interfaces/kits/js:configpolicy", + "./interfaces/kits/js:customconfig", ] } else { deps = [ "./frameworks/config_policy:configpolicy_util" ] diff --git a/bundle.json b/bundle.json index 0de426b..3efca3f 100644 --- a/bundle.json +++ b/bundle.json @@ -22,6 +22,7 @@ "ram": "50KB", "deps": { "components": [ + "ability_runtime", "hisysevent", "hilog", "napi", diff --git a/frameworks/config_policy/etc/customization.para.dac b/frameworks/config_policy/etc/customization.para.dac index 823c6fb..4bc2985 100644 --- a/frameworks/config_policy/etc/customization.para.dac +++ b/frameworks/config_policy/etc/customization.para.dac @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -12,4 +12,4 @@ # limitations under the License. const.cust.="root:root:774" - +const.channelid.="root:root:775" diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index 01b1b76..a89952a 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -35,3 +35,28 @@ ohos_shared_library("configpolicy") { subsystem_name = "customization" part_name = "config_policy" } + +ohos_shared_library("customconfig") { + include_dirs = [ + "include", + "../../../interfaces/inner_api/include", + "../../../frameworks/dfx/hisysevent_adapter", + ] + + sources = [ + "../../../frameworks/dfx/hisysevent_adapter/hisysevent_adapter.cpp", + "src/custom_config_napi.cpp", + ] + + external_deps = [ + "ability_runtime:abilitykit_native", + "ability_runtime:app_context", + "hilog:libhilog", + "hisysevent:libhisysevent", + "init:libbegetutil", + "napi:ace_napi", + ] + relative_install_dir = "module/customization" + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/interfaces/kits/js/include/custom_config_napi.h b/interfaces/kits/js/include/custom_config_napi.h new file mode 100644 index 0000000..481bb42 --- /dev/null +++ b/interfaces/kits/js/include/custom_config_napi.h @@ -0,0 +1,44 @@ +/* + * 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 CUSTOM_CONFIG_NAPI_H +#define CUSTOM_CONFIG_NAPI_H + +#include + +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "napi/native_api.h" + +namespace OHOS { +namespace Customization { +namespace ConfigPolicy { +class CustomConfigNapi { +public: + CustomConfigNapi(); + ~CustomConfigNapi() = default; + static napi_value Init(napi_env env, napi_value exports); + +private: + static int GetBundleName(std::string &bundleName); + static napi_value NAPIGetChannelId(napi_env env, napi_callback_info info); + static napi_value NativeGetChannelId(napi_env env, std::string channelKey); + static napi_value CreateNapiStringValue(napi_env env, const char *str); + static char *CustGetSystemParam(const char *name); +}; +} // namespace ConfigPolicy +} // namespace Customization +} // namespace OHOS +#endif diff --git a/interfaces/kits/js/src/custom_config_napi.cpp b/interfaces/kits/js/src/custom_config_napi.cpp new file mode 100644 index 0000000..7599649 --- /dev/null +++ b/interfaces/kits/js/src/custom_config_napi.cpp @@ -0,0 +1,127 @@ +/* + * 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 "custom_config_napi.h" + +#include "application_context.h" +#include "hilog/log.h" +#include "hisysevent_adapter.h" +#include "init_param.h" + +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD001E00 + +#undef LOG_TAG +#define LOG_TAG "CustomConfigJs" + +namespace OHOS { +namespace Customization { +namespace ConfigPolicy { +using namespace OHOS::HiviewDFX; + +static const std::string CHANNEL_ID_PREFIX = "const.channelid."; + +napi_value CustomConfigNapi::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor property[] = { + DECLARE_NAPI_FUNCTION("getChannelId", CustomConfigNapi::NAPIGetChannelId), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); + return exports; +} + +int CustomConfigNapi::GetBundleName(std::string &bundleName) +{ + std::shared_ptr abilityContext = + AbilityRuntime::Context::GetApplicationContext(); + if (abilityContext == nullptr) { + HILOG_ERROR(LOG_CORE, "get abilityContext failed."); + return -1; + } + bundleName = abilityContext->GetBundleName(); + return 0; +} + +napi_value CustomConfigNapi::NAPIGetChannelId(napi_env env, napi_callback_info info) +{ + std::string bundleName; + if (GetBundleName(bundleName) != 0 || bundleName.empty()) { + return CreateNapiStringValue(env, ""); + } + std::string channelKey = CHANNEL_ID_PREFIX + bundleName; + return NativeGetChannelId(env, channelKey); +} + +char *CustomConfigNapi::CustGetSystemParam(const char *name) +{ + char *value = nullptr; + unsigned int len = 0; + + if (SystemGetParameter(name, nullptr, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) { + return nullptr; + } + value = (char *)calloc(len, sizeof(char)); + if (value != nullptr && SystemGetParameter(name, value, &len) == 0 && value[0]) { + return value; + } + if (value != nullptr) { + free(value); + } + return nullptr; +} + +napi_value CustomConfigNapi::NativeGetChannelId(napi_env env, std::string channelKey) +{ + char *channelId = CustGetSystemParam(channelKey.c_str()); + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getChannelId", ""); + napi_value result = nullptr; + if (channelId == nullptr) { + HILOG_WARN(LOG_CORE, "get channelId failed."); + return CreateNapiStringValue(env, ""); + } + result = CreateNapiStringValue(env, channelId); + free(channelId); + return result; +} + +napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, const char *str) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &result)); + return result; +} + +static napi_value CustomConfigInit(napi_env env, napi_value exports) +{ + return CustomConfigNapi::Init(env, exports); +} + +static napi_module g_customConfigModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = CustomConfigInit, + .nm_modname = "customConfig", + .nm_priv = ((void *)0), + .reserved = { 0 } +}; + +extern "C" __attribute__((constructor)) void CustomConfigRegister() +{ + napi_module_register(&g_customConfigModule); +} +} // namespace ConfigPolicy +} // namespace Customization +} // namespace OHOS -- Gitee