• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static const std::string CUSTOM_PRELOAD_LIST_PARA = "persist.custom.preload.list";
36 
Init(napi_env env,napi_value exports)37 napi_value CustomConfigNapi::Init(napi_env env, napi_value exports)
38 {
39     napi_property_descriptor property[] = {
40         DECLARE_NAPI_FUNCTION("getChannelId", CustomConfigNapi::NAPIGetChannelId),
41     };
42     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
43     return exports;
44 }
45 
GetBundleName(std::string & bundleName)46 int CustomConfigNapi::GetBundleName(std::string &bundleName)
47 {
48     std::shared_ptr<AbilityRuntime::ApplicationContext> abilityContext =
49         AbilityRuntime::Context::GetApplicationContext();
50     if (abilityContext == nullptr) {
51         HILOG_ERROR(LOG_CORE, "get abilityContext failed.");
52         return -1;
53     }
54     bundleName = abilityContext->GetBundleName();
55     return 0;
56 }
57 
IsInPreloadList(std::string bundleName)58  bool CustomConfigNapi::IsInPreloadList(std::string bundleName)
59  {
60     char *preloadList = CustGetSystemParam(CUSTOM_PRELOAD_LIST_PARA.c_str());
61     if (preloadList == nullptr) {
62         HILOG_WARN(LOG_CORE, "get preload list fail.");
63         return false;
64     }
65     if (preloadList[0] < '0' || preloadList[0] > '9' || preloadList[1] != ',') {
66         HILOG_ERROR(LOG_CORE, "preload list param format error.");
67         free(preloadList);
68         return false;
69     }
70     int listlen = preloadList[0] - '0';
71     std::string preloadListResult(preloadList + 1); // skip listlen
72     free(preloadList);
73 
74     for (int i = 1; i < listlen; i++) {
75         std::string tempPreloadListPara = CUSTOM_PRELOAD_LIST_PARA + std::to_string(i);
76         char *tempList = CustGetSystemParam(tempPreloadListPara.c_str());
77         if (tempList == nullptr) {
78             HILOG_ERROR(LOG_CORE, "preload list len error.");
79             return false;
80         }
81         preloadListResult.append(tempList + 1); // skip listlen
82         free(tempList);
83     }
84     preloadListResult.append(",");
85     return preloadListResult.find("," + bundleName + ",") != std::string::npos;
86 }
87 
NAPIGetChannelId(napi_env env,napi_callback_info info)88 napi_value CustomConfigNapi::NAPIGetChannelId(napi_env env, napi_callback_info info)
89 {
90     std::string bundleName;
91     if (GetBundleName(bundleName) != 0 || bundleName.empty() || IsInPreloadList(bundleName)) {
92         return CreateNapiStringValue(env, "");
93     }
94     std::string channelKey = CHANNEL_ID_PREFIX + bundleName;
95     return NativeGetChannelId(env, channelKey);
96 }
97 
CustGetSystemParam(const char * name)98 char *CustomConfigNapi::CustGetSystemParam(const char *name)
99 {
100     char *value = nullptr;
101     unsigned int len = 0;
102 
103     if (SystemGetParameter(name, nullptr, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) {
104         return nullptr;
105     }
106     value = (char *)calloc(len, sizeof(char));
107     if (value != nullptr && SystemGetParameter(name, value, &len) == 0 && value[0]) {
108         return value;
109     }
110     if (value != nullptr) {
111         free(value);
112     }
113     return nullptr;
114 }
115 
NativeGetChannelId(napi_env env,std::string channelKey)116 napi_value CustomConfigNapi::NativeGetChannelId(napi_env env, std::string channelKey)
117 {
118     char *channelId = CustGetSystemParam(channelKey.c_str());
119     napi_value result = nullptr;
120     if (channelId == nullptr) {
121         HILOG_WARN(LOG_CORE, "get channelId failed.");
122         ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_FAILED, "getChannelId", "ChannelId is nullptr.");
123         return CreateNapiStringValue(env, "");
124     }
125     result = CreateNapiStringValue(env, channelId);
126     free(channelId);
127     return result;
128 }
129 
CreateNapiStringValue(napi_env env,const char * str)130 napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, const char *str)
131 {
132     napi_value result = nullptr;
133     NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &result));
134     return result;
135 }
136 
CustomConfigInit(napi_env env,napi_value exports)137 static napi_value CustomConfigInit(napi_env env, napi_value exports)
138 {
139     return CustomConfigNapi::Init(env, exports);
140 }
141 
142 static napi_module g_customConfigModule = {
143     .nm_version = 1,
144     .nm_flags = 0,
145     .nm_filename = nullptr,
146     .nm_register_func = CustomConfigInit,
147     .nm_modname = "customConfig",
148     .nm_priv = ((void *)0),
149     .reserved = { 0 }
150 };
151 
CustomConfigRegister()152 extern "C" __attribute__((constructor)) void CustomConfigRegister()
153 {
154     napi_module_register(&g_customConfigModule);
155 }
156 } // namespace ConfigPolicy
157 } // namespace Customization
158 } // namespace OHOS
159