• 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 "init_param.h"
21 
22 #undef LOG_DOMAIN
23 #define LOG_DOMAIN 0xD001E00
24 
25 #undef LOG_TAG
26 #define LOG_TAG "CustomConfigJs"
27 
28 namespace OHOS {
29 namespace Customization {
30 namespace ConfigPolicy {
31 using namespace OHOS::HiviewDFX;
32 
33 static const std::string CHANNEL_ID_PREFIX = "const.channelid.";
34 static const std::string CUSTOM_PRELOAD_LIST_PARA = "persist.custom.preload.list";
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 
IsInPreloadList(std::string bundleName)57  bool CustomConfigNapi::IsInPreloadList(std::string bundleName)
58  {
59     char *preloadList = CustGetSystemParam(CUSTOM_PRELOAD_LIST_PARA.c_str());
60     if (preloadList == nullptr) {
61         HILOG_DEBUG(LOG_CORE, "get preload list fail.");
62         return false;
63     }
64     if (preloadList[0] < '0' || preloadList[0] > '9' || preloadList[1] != ',') {
65         HILOG_ERROR(LOG_CORE, "preload list param format error.");
66         free(preloadList);
67         return false;
68     }
69     int listlen = preloadList[0] - '0';
70     std::string preloadListResult(preloadList + 1); // skip listlen
71     free(preloadList);
72 
73     for (int i = 1; i < listlen; i++) {
74         std::string tempPreloadListPara = CUSTOM_PRELOAD_LIST_PARA + std::to_string(i);
75         char *tempList = CustGetSystemParam(tempPreloadListPara.c_str());
76         if (tempList == nullptr) {
77             HILOG_ERROR(LOG_CORE, "preload list len error.");
78             return false;
79         }
80         preloadListResult.append(tempList + 1); // skip listlen
81         free(tempList);
82     }
83     preloadListResult.append(",");
84     return preloadListResult.find("," + bundleName + ",") != std::string::npos;
85 }
86 
NAPIGetChannelId(napi_env env,napi_callback_info info)87 napi_value CustomConfigNapi::NAPIGetChannelId(napi_env env, napi_callback_info info)
88 {
89     std::string bundleName;
90     if (GetBundleName(bundleName) != 0 || bundleName.empty() || IsInPreloadList(bundleName)) {
91         return CreateNapiStringValue(env, "");
92     }
93     std::string channelKey = CHANNEL_ID_PREFIX + bundleName;
94     return NativeGetChannelId(env, channelKey);
95 }
96 
CustGetSystemParam(const char * name)97 char *CustomConfigNapi::CustGetSystemParam(const char *name)
98 {
99     char *value = nullptr;
100     unsigned int len = 0;
101 
102     if (SystemGetParameter(name, nullptr, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) {
103         return nullptr;
104     }
105     value = (char *)calloc(len, sizeof(char));
106     if (value != nullptr && SystemGetParameter(name, value, &len) == 0 && value[0]) {
107         return value;
108     }
109     if (value != nullptr) {
110         free(value);
111     }
112     return nullptr;
113 }
114 
NativeGetChannelId(napi_env env,std::string channelKey)115 napi_value CustomConfigNapi::NativeGetChannelId(napi_env env, std::string channelKey)
116 {
117     char *channelId = CustGetSystemParam(channelKey.c_str());
118     napi_value result = nullptr;
119     if (channelId == nullptr) {
120         HILOG_WARN(LOG_CORE, "get channelId failed.");
121         return CreateNapiStringValue(env, "");
122     }
123     result = CreateNapiStringValue(env, channelId);
124     free(channelId);
125     return result;
126 }
127 
CreateNapiStringValue(napi_env env,const char * str)128 napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, const char *str)
129 {
130     napi_value result = nullptr;
131     NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &result));
132     return result;
133 }
134 
CustomConfigInit(napi_env env,napi_value exports)135 static napi_value CustomConfigInit(napi_env env, napi_value exports)
136 {
137     return CustomConfigNapi::Init(env, exports);
138 }
139 
140 static napi_module g_customConfigModule = {
141     .nm_version = 1,
142     .nm_flags = 0,
143     .nm_filename = nullptr,
144     .nm_register_func = CustomConfigInit,
145     .nm_modname = "customConfig",
146     .nm_priv = ((void *)0),
147     .reserved = { 0 }
148 };
149 
CustomConfigRegister()150 extern "C" __attribute__((constructor)) void CustomConfigRegister()
151 {
152     napi_module_register(&g_customConfigModule);
153 }
154 } // namespace ConfigPolicy
155 } // namespace Customization
156 } // namespace OHOS
157