• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <ani_signature_builder.h>
16 
17 #include "app_log_wrapper.h"
18 #include "bundle_errors.h"
19 #include "business_error_ani.h"
20 #include "common_fun_ani.h"
21 #include "common_func.h"
22 #include "napi_constants.h"
23 #include "overlay_module_info.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 constexpr const char* NS_NAME_OVERLAY = "@ohos.bundle.overlay.overlay";
29 } // namespace
30 
AniSetOverlayEnabled(ani_env * env,ani_string aniModuleName,ani_boolean aniIsEnabled)31 static void AniSetOverlayEnabled(ani_env *env, ani_string aniModuleName, ani_boolean aniIsEnabled)
32 {
33     APP_LOGD("ani SetOverlayEnabled called");
34     std::string moduleName;
35     if (!CommonFunAni::ParseString(env, aniModuleName, moduleName) || moduleName.empty()) {
36         APP_LOGE("moduleName %{public}s invalid", moduleName.c_str());
37         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING);
38         return;
39     }
40     bool isEnabled = CommonFunAni::AniBooleanToBool(aniIsEnabled);
41 
42     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
43     if (overlayMgrProxy == nullptr) {
44         APP_LOGE("overlayMgrProxy is null");
45         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
46             SET_OVERLAY_ENABLED, Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE);
47         return;
48     }
49 
50     ErrCode ret = overlayMgrProxy->SetOverlayEnabledForSelf(moduleName, isEnabled);
51     if (ret != ERR_OK) {
52         APP_LOGE("SetOverlayEnabledForSelf failed ret: %{public}d", ret);
53         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
54             SET_OVERLAY_ENABLED, Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE);
55         return;
56     }
57 }
58 
AniSetOverlayEnabledByBundleName(ani_env * env,ani_string aniBundleName,ani_string aniModuleName,ani_boolean aniIsEnabled)59 static void AniSetOverlayEnabledByBundleName(ani_env *env,
60     ani_string aniBundleName, ani_string aniModuleName, ani_boolean aniIsEnabled)
61 {
62     APP_LOGD("ani SetOverlayEnabledByBundleName called");
63     std::string bundleName;
64     if (!CommonFunAni::ParseString(env, aniBundleName, bundleName) || bundleName.empty()) {
65         APP_LOGE("bundleName %{public}s invalid", bundleName.c_str());
66         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
67         return;
68     }
69     std::string moduleName;
70     if (!CommonFunAni::ParseString(env, aniModuleName, moduleName) || moduleName.empty()) {
71         APP_LOGE("moduleName %{public}s invalid", moduleName.c_str());
72         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING);
73         return;
74     }
75     bool isEnabled = CommonFunAni::AniBooleanToBool(aniIsEnabled);
76 
77     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
78     if (overlayMgrProxy == nullptr) {
79         APP_LOGE("overlayMgrProxy is null");
80         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
81             SET_OVERLAY_ENABLED_BY_BUNDLE_NAME, Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE);
82         return;
83     }
84 
85     ErrCode ret = overlayMgrProxy->SetOverlayEnabled(bundleName, moduleName, isEnabled);
86     if (ret != ERR_OK) {
87         APP_LOGE("SetOverlayEnabled failed ret: %{public}d", ret);
88         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
89             SET_OVERLAY_ENABLED_BY_BUNDLE_NAME, Constants::PERMISSION_CHANGE_OVERLAY_ENABLED_STATE);
90         return;
91     }
92 }
93 
AniGetOverlayModuleInfo(ani_env * env,ani_string aniModuleName)94 static ani_object AniGetOverlayModuleInfo(ani_env *env, ani_string aniModuleName)
95 {
96     APP_LOGD("ani GetOverlayModuleInfo called");
97     std::string moduleName;
98     if (!CommonFunAni::ParseString(env, aniModuleName, moduleName) || moduleName.empty()) {
99         APP_LOGE("moduleName %{public}s invalid", moduleName.c_str());
100         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, MODULE_NAME, TYPE_STRING);
101         return nullptr;
102     }
103 
104     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
105     if (overlayMgrProxy == nullptr) {
106         APP_LOGE("overlayMgrProxy is null");
107         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
108             GET_OVERLAY_MODULE_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
109         return nullptr;
110     }
111 
112     OverlayModuleInfo overlayModuleInfo;
113 
114     ErrCode ret = overlayMgrProxy->GetOverlayModuleInfo(moduleName, overlayModuleInfo);
115     if (ret != ERR_OK) {
116         APP_LOGE("GetOverlayModuleInfo failed ret: %{public}d", ret);
117         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
118             GET_OVERLAY_MODULE_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
119         return nullptr;
120     }
121 
122     return CommonFunAni::ConvertOverlayModuleInfo(env, overlayModuleInfo);
123 }
124 
AniGetTargetOverlayModuleInfos(ani_env * env,ani_string aniTargetModuleName)125 static ani_object AniGetTargetOverlayModuleInfos(ani_env *env, ani_string aniTargetModuleName)
126 {
127     APP_LOGD("ani GetTargetOverlayModuleInfos called");
128     std::string targetModuleName;
129     if (!CommonFunAni::ParseString(env, aniTargetModuleName, targetModuleName) || targetModuleName.empty()) {
130         APP_LOGE("targetModuleName %{public}s invalid", targetModuleName.c_str());
131         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, TARGET_MODULE_NAME, TYPE_STRING);
132         return nullptr;
133     }
134 
135     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
136     if (overlayMgrProxy == nullptr) {
137         APP_LOGE("overlayMgrProxy is null");
138         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
139             GET_TARGET_OVERLAY_MODULE_INFOS, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
140         return nullptr;
141     }
142 
143     std::vector<OverlayModuleInfo> overlayModuleInfos;
144 
145     ErrCode ret = overlayMgrProxy->GetTargetOverlayModuleInfo(targetModuleName, overlayModuleInfos);
146     if (ret != ERR_OK) {
147         APP_LOGE("GetTargetOverlayModuleInfo failed ret: %{public}d", ret);
148         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
149             GET_TARGET_OVERLAY_MODULE_INFOS, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
150         return nullptr;
151     }
152 
153     ani_object overlayModuleInfosObject = CommonFunAni::ConvertAniArray(
154         env, overlayModuleInfos, CommonFunAni::ConvertOverlayModuleInfo);
155     if (overlayModuleInfosObject == nullptr) {
156         APP_LOGE("nullptr overlayModuleInfosObject");
157     }
158 
159     return overlayModuleInfosObject;
160 }
161 
AniGetOverlayModuleInfoByBundleName(ani_env * env,ani_string aniBundleName,ani_string aniModuleName)162 static ani_object AniGetOverlayModuleInfoByBundleName(ani_env *env, ani_string aniBundleName, ani_string aniModuleName)
163 {
164     APP_LOGD("ani GetOverlayModuleInfoByBundleName called");
165     std::string bundleName;
166     if (!CommonFunAni::ParseString(env, aniBundleName, bundleName) || bundleName.empty()) {
167         APP_LOGE("bundleName %{public}s invalid", bundleName.c_str());
168         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
169         return nullptr;
170     }
171     std::string moduleName;
172     if (!CommonFunAni::ParseString(env, aniModuleName, moduleName)) {
173         APP_LOGI("MoudleName undefined, default query for all module OverlayModuleInfo");
174     }
175 
176     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
177     if (overlayMgrProxy == nullptr) {
178         APP_LOGE("overlayMgrProxy is null");
179         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
180             GET_OVERLAY_MODULE_INFO_BY_BUNDLE_NAME, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
181         return nullptr;
182     }
183 
184     std::vector<OverlayModuleInfo> overlayModuleInfos;
185 
186     ErrCode ret = overlayMgrProxy->GetOverlayModuleInfoByBundleName(bundleName, moduleName, overlayModuleInfos);
187     if (ret != ERR_OK) {
188         APP_LOGE("GetOverlayModuleInfoByBundleName failed ret: %{public}d", ret);
189         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
190             GET_OVERLAY_MODULE_INFO_BY_BUNDLE_NAME, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
191         return nullptr;
192     }
193 
194     ani_object overlayModuleInfosObject = CommonFunAni::ConvertAniArray(
195         env, overlayModuleInfos, CommonFunAni::ConvertOverlayModuleInfo);
196     if (overlayModuleInfosObject == nullptr) {
197         APP_LOGE("nullptr overlayModuleInfosObject");
198     }
199 
200     return overlayModuleInfosObject;
201 }
202 
AniGetTargetOverlayModuleInfosByBundleName(ani_env * env,ani_string aniTargetBundleName,ani_string aniModuleName)203 static ani_object AniGetTargetOverlayModuleInfosByBundleName(ani_env *env,
204     ani_string aniTargetBundleName, ani_string aniModuleName)
205 {
206     APP_LOGD("ani GetTargetOverlayModuleInfosByBundleName called");
207     std::string targetBundleName;
208     if (!CommonFunAni::ParseString(env, aniTargetBundleName, targetBundleName) || targetBundleName.empty()) {
209         APP_LOGE("targetBundleName %{public}s invalid", targetBundleName.c_str());
210         BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, TARGET_BUNDLE_NAME, TYPE_STRING);
211         return nullptr;
212     }
213     std::string moduleName;
214     if (!CommonFunAni::ParseString(env, aniModuleName, moduleName)) {
215         APP_LOGI("MoudleName undefined, default query for all module OverlayModuleInfo");
216     }
217 
218     auto overlayMgrProxy = CommonFunc::GetOverlayMgrProxy();
219     if (overlayMgrProxy == nullptr) {
220         APP_LOGE("overlayMgrProxy is null");
221         BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND,
222             GET_TARGET_OVERLAY_MODULE_INFOS_BY_BUNDLE_NAME, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
223         return nullptr;
224     }
225 
226     std::vector<OverlayModuleInfo> overlayModuleInfos;
227 
228     ErrCode ret = overlayMgrProxy->GetOverlayModuleInfoForTarget(targetBundleName, moduleName, overlayModuleInfos);
229     if (ret != ERR_OK) {
230         APP_LOGE("GetOverlayModuleInfoForTarget failed ret: %{public}d", ret);
231         BusinessErrorAni::ThrowCommonError(env, CommonFunc::ConvertErrCode(ret),
232             GET_TARGET_OVERLAY_MODULE_INFOS_BY_BUNDLE_NAME, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED);
233         return nullptr;
234     }
235 
236     ani_object overlayModuleInfosObject = CommonFunAni::ConvertAniArray(
237         env, overlayModuleInfos, CommonFunAni::ConvertOverlayModuleInfo);
238     if (overlayModuleInfosObject == nullptr) {
239         APP_LOGE("nullptr overlayModuleInfosObject");
240     }
241 
242     return overlayModuleInfosObject;
243 }
244 
245 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)246 ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result)
247 {
248     APP_LOGI("ANI_Constructor overlay called");
249     ani_env* env;
250     ani_status status = vm->GetEnv(ANI_VERSION_1, &env);
251     RETURN_ANI_STATUS_IF_NOT_OK(status, "Unsupported ANI_VERSION_1");
252 
253     arkts::ani_signature::Namespace nsName = arkts::ani_signature::Builder::BuildNamespace(NS_NAME_OVERLAY);
254     ani_namespace kitNs = nullptr;
255     status = env->FindNamespace(nsName.Descriptor().c_str(), &kitNs);
256     if (status != ANI_OK) {
257         APP_LOGE("FindNamespace: %{public}s fail with %{public}d", NS_NAME_OVERLAY, status);
258         return status;
259     }
260 
261     std::array methods = {
262         ani_native_function { "setOverlayEnabledNative", nullptr,
263             reinterpret_cast<void*>(AniSetOverlayEnabled) },
264         ani_native_function { "setOverlayEnabledByBundleNameNative", nullptr,
265             reinterpret_cast<void*>(AniSetOverlayEnabledByBundleName) },
266         ani_native_function { "getOverlayModuleInfoNative", nullptr,
267             reinterpret_cast<void*>(AniGetOverlayModuleInfo) },
268         ani_native_function { "getTargetOverlayModuleInfosNative", nullptr,
269             reinterpret_cast<void*>(AniGetTargetOverlayModuleInfos) },
270         ani_native_function { "getOverlayModuleInfoByBundleNameNative", nullptr,
271             reinterpret_cast<void*>(AniGetOverlayModuleInfoByBundleName) },
272         ani_native_function { "getTargetOverlayModuleInfosByBundleNameNative", nullptr,
273             reinterpret_cast<void*>(AniGetTargetOverlayModuleInfosByBundleName) }
274     };
275 
276     status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size());
277     if (status != ANI_OK) {
278         APP_LOGE("Namespace_BindNativeFunctions: %{public}s fail with %{public}d", NS_NAME_OVERLAY, status);
279         return status;
280     }
281 
282     *result = ANI_VERSION_1;
283 
284     APP_LOGI("ANI_Constructor finished");
285 
286     return ANI_OK;
287 }
288 }
289 } // AppExecFwk
290 } // OHOS