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 #include "custom_config_napi.h"
17
18 #include "application_context.h"
19 #include "hilog/log.h"
20 #include "hisysevent_adapter.h"
21 #include "init_param.h"
22
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD001E00
25
26 #undef LOG_TAG
27 #define LOG_TAG "CustomConfigJs"
28
29 namespace OHOS {
30 namespace Customization {
31 namespace ConfigPolicy {
32 using namespace OHOS::HiviewDFX;
33
34 static const std::string CHANNEL_ID_PREFIX = "const.channelid.";
35
Init(napi_env env,napi_value exports)36 napi_value CustomConfigNapi::Init(napi_env env, napi_value exports)
37 {
38 napi_property_descriptor property[] = {
39 DECLARE_NAPI_FUNCTION("getChannelId", CustomConfigNapi::NAPIGetChannelId),
40 };
41 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
42 return exports;
43 }
44
GetBundleName(std::string & bundleName)45 int CustomConfigNapi::GetBundleName(std::string &bundleName)
46 {
47 std::shared_ptr<AbilityRuntime::ApplicationContext> abilityContext =
48 AbilityRuntime::Context::GetApplicationContext();
49 if (abilityContext == nullptr) {
50 HILOG_ERROR(LOG_CORE, "get abilityContext failed.");
51 return -1;
52 }
53 bundleName = abilityContext->GetBundleName();
54 return 0;
55 }
56
NAPIGetChannelId(napi_env env,napi_callback_info info)57 napi_value CustomConfigNapi::NAPIGetChannelId(napi_env env, napi_callback_info info)
58 {
59 std::string bundleName;
60 if (GetBundleName(bundleName) != 0 || bundleName.empty()) {
61 return CreateNapiStringValue(env, "");
62 }
63 std::string channelKey = CHANNEL_ID_PREFIX + bundleName;
64 return NativeGetChannelId(env, channelKey);
65 }
66
CustGetSystemParam(const char * name)67 char *CustomConfigNapi::CustGetSystemParam(const char *name)
68 {
69 char *value = nullptr;
70 unsigned int len = 0;
71
72 if (SystemGetParameter(name, nullptr, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) {
73 return nullptr;
74 }
75 value = (char *)calloc(len, sizeof(char));
76 if (value != nullptr && SystemGetParameter(name, value, &len) == 0 && value[0]) {
77 return value;
78 }
79 if (value != nullptr) {
80 free(value);
81 }
82 return nullptr;
83 }
84
NativeGetChannelId(napi_env env,std::string channelKey)85 napi_value CustomConfigNapi::NativeGetChannelId(napi_env env, std::string channelKey)
86 {
87 char *channelId = CustGetSystemParam(channelKey.c_str());
88 ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getChannelId", "");
89 napi_value result = nullptr;
90 if (channelId == nullptr) {
91 HILOG_WARN(LOG_CORE, "get channelId failed.");
92 return CreateNapiStringValue(env, "");
93 }
94 result = CreateNapiStringValue(env, channelId);
95 free(channelId);
96 return result;
97 }
98
CreateNapiStringValue(napi_env env,const char * str)99 napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, const char *str)
100 {
101 napi_value result = nullptr;
102 NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &result));
103 return result;
104 }
105
CustomConfigInit(napi_env env,napi_value exports)106 static napi_value CustomConfigInit(napi_env env, napi_value exports)
107 {
108 return CustomConfigNapi::Init(env, exports);
109 }
110
111 static napi_module g_customConfigModule = {
112 .nm_version = 1,
113 .nm_flags = 0,
114 .nm_filename = nullptr,
115 .nm_register_func = CustomConfigInit,
116 .nm_modname = "customConfig",
117 .nm_priv = ((void *)0),
118 .reserved = { 0 }
119 };
120
CustomConfigRegister()121 extern "C" __attribute__((constructor)) void CustomConfigRegister()
122 {
123 napi_module_register(&g_customConfigModule);
124 }
125 } // namespace ConfigPolicy
126 } // namespace Customization
127 } // namespace OHOS
128