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 "bundle_resource_info.h"
20 #include "bundle_resource_interface.h"
21 #include "business_error_ani.h"
22 #include "common_fun_ani.h"
23 #include "common_func.h"
24 #include "resource_helper.h"
25 #include "napi_constants.h"
26
27 namespace OHOS {
28 namespace AppExecFwk {
29
30 namespace {
31 constexpr int32_t INVALID_INT = -500;
32 constexpr int32_t DEFAULT_RES_FLAG = 1;
33 constexpr int32_t DEFAULT_IDX = 0;
34 constexpr const char* NS_NAME_RESOURCEMANAGER = "@ohos.bundle.bundleResourceManager.bundleResourceManager";
35 }
36
AniGetBundleResourceInfo(ani_env * env,ani_string aniBundleName,ani_double aniResFlag,ani_double aniAppIndex)37 static ani_object AniGetBundleResourceInfo(ani_env* env, ani_string aniBundleName,
38 ani_double aniResFlag, ani_double aniAppIndex)
39 {
40 APP_LOGD("ani GetBundleResourceInfo called");
41 std::string bundleName;
42 if (!CommonFunAni::ParseString(env, aniBundleName, bundleName) || bundleName.empty()) {
43 APP_LOGE("parse bundleName %{public}s failed", bundleName.c_str());
44 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
45 return nullptr;
46 }
47 int32_t resFlag = 0;
48 if (!CommonFunAni::TryCastDoubleTo(aniResFlag, &resFlag)) {
49 APP_LOGE("Cast aniResFlag failed");
50 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
51 return nullptr;
52 }
53 int32_t appIndex = 0;
54 if (!CommonFunAni::TryCastDoubleTo(aniAppIndex, &appIndex)) {
55 APP_LOGE("Cast aniAppIndex failed");
56 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, APP_INDEX, TYPE_NUMBER);
57 return nullptr;
58 }
59
60 if (resFlag == INVALID_INT) {
61 resFlag = DEFAULT_RES_FLAG;
62 }
63
64 if (appIndex == INVALID_INT) {
65 appIndex = DEFAULT_IDX;
66 }
67
68 BundleResourceInfo bundleResInfo;
69 int32_t ret = ResourceHelper::InnerGetBundleResourceInfo(bundleName, resFlag, appIndex, bundleResInfo);
70 if (ret != ERR_OK) {
71 APP_LOGE("GetBundleResourceInfo failed ret: %{public}d", ret);
72 BusinessErrorAni::ThrowCommonError(
73 env, ret, GET_BUNDLE_RESOURCE_INFO, PERMISSION_GET_BUNDLE_RESOURCES);
74 return nullptr;
75 }
76
77 return CommonFunAni::ConvertBundleResourceInfo(env, bundleResInfo);
78 }
79
AniGetLauncherAbilityResourceInfo(ani_env * env,ani_string aniBundleName,ani_double aniResFlag,ani_double aniAppIndex)80 static ani_object AniGetLauncherAbilityResourceInfo(ani_env* env, ani_string aniBundleName,
81 ani_double aniResFlag, ani_double aniAppIndex)
82 {
83 APP_LOGD("ani GetLauncherAbilityResourceInfo called");
84 std::string bundleName;
85 if (!CommonFunAni::ParseString(env, aniBundleName, bundleName) || bundleName.empty()) {
86 APP_LOGE("parse bundleName %{public}s failed", bundleName.c_str());
87 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_NAME, TYPE_STRING);
88 return nullptr;
89 }
90 int32_t resFlag = 0;
91 if (!CommonFunAni::TryCastDoubleTo(aniResFlag, &resFlag)) {
92 APP_LOGE("Cast aniResFlag failed");
93 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
94 return nullptr;
95 }
96 int32_t appIndex = 0;
97 if (!CommonFunAni::TryCastDoubleTo(aniAppIndex, &appIndex)) {
98 APP_LOGE("Cast aniAppIndex failed");
99 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, APP_INDEX, TYPE_NUMBER);
100 return nullptr;
101 }
102
103 if (resFlag == INVALID_INT) {
104 resFlag = DEFAULT_RES_FLAG;
105 }
106
107 if (appIndex == INVALID_INT) {
108 appIndex = DEFAULT_IDX;
109 }
110
111 std::vector<LauncherAbilityResourceInfo> launcherAbilityResourceInfos;
112 int32_t ret = ResourceHelper::InnerGetLauncherAbilityResourceInfo(
113 bundleName, resFlag, appIndex, launcherAbilityResourceInfos);
114 if (ret != ERR_OK) {
115 APP_LOGE("GetLauncherAbilityResourceInfo failed ret: %{public}d", ret);
116 BusinessErrorAni::ThrowCommonError(env, ret,
117 GET_LAUNCHER_ABILITY_RESOURCE_INFO, PERMISSION_GET_BUNDLE_RESOURCES);
118 return nullptr;
119 }
120
121 ani_object launcherAbilityResourceInfosObject = CommonFunAni::ConvertAniArray(
122 env, launcherAbilityResourceInfos, CommonFunAni::ConvertLauncherAbilityResourceInfo);
123 if (launcherAbilityResourceInfosObject == nullptr) {
124 APP_LOGE("nullptr launcherAbilityResourceInfosObject");
125 }
126
127 return launcherAbilityResourceInfosObject;
128 }
129
AniGetAllBundleResourceInfo(ani_env * env,ani_double aniResFlag)130 static ani_object AniGetAllBundleResourceInfo(ani_env* env, ani_double aniResFlag)
131 {
132 APP_LOGD("ani GetAllBundleResourceInfo called");
133 int32_t resFlag = 0;
134 if (!CommonFunAni::TryCastDoubleTo(aniResFlag, &resFlag)) {
135 APP_LOGE("Cast aniResFlag failed");
136 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
137 return nullptr;
138 }
139
140 std::vector<BundleResourceInfo> bundleResourceInfos;
141 int32_t ret = ResourceHelper::InnerGetAllBundleResourceInfo(resFlag, bundleResourceInfos);
142 if (ret != ERR_OK) {
143 APP_LOGE("GetLauncherAbilityResourceInfo failed ret: %{public}d", ret);
144 BusinessErrorAni::ThrowCommonError(env, ret, GET_ALL_BUNDLE_RESOURCE_INFO, PERMISSION_GET_ALL_BUNDLE_RESOURCES);
145 return nullptr;
146 }
147
148 ani_object bundleResourceInfosObject = CommonFunAni::ConvertAniArray(
149 env, bundleResourceInfos, CommonFunAni::ConvertBundleResourceInfo);
150 if (bundleResourceInfosObject == nullptr) {
151 APP_LOGE("nullptr bundleResourceInfosObject");
152 }
153
154 return bundleResourceInfosObject;
155 }
156
AniGetAllLauncherAbilityResourceInfo(ani_env * env,ani_double aniResFlag)157 static ani_object AniGetAllLauncherAbilityResourceInfo(ani_env* env, ani_double aniResFlag)
158 {
159 APP_LOGD("ani GetAllLauncherAbilityResourceInfo called");
160 int32_t resFlag = 0;
161 if (!CommonFunAni::TryCastDoubleTo(aniResFlag, &resFlag)) {
162 APP_LOGE("Cast aniResFlag failed");
163 BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER);
164 return nullptr;
165 }
166
167 std::vector<LauncherAbilityResourceInfo> launcherAbilityResourceInfos;
168 int32_t ret = ResourceHelper::InnerGetAllLauncherAbilityResourceInfo(resFlag, launcherAbilityResourceInfos);
169 if (ret != ERR_OK) {
170 APP_LOGE("GetLauncherAbilityResourceInfo failed ret: %{public}d", ret);
171 BusinessErrorAni::ThrowCommonError(env, ret,
172 GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, PERMISSION_GET_ALL_BUNDLE_RESOURCES);
173 return nullptr;
174 }
175
176 ani_object launcherAbilityResourceInfosObject = CommonFunAni::ConvertAniArray(
177 env, launcherAbilityResourceInfos, CommonFunAni::ConvertLauncherAbilityResourceInfo);
178 if (launcherAbilityResourceInfosObject == nullptr) {
179 APP_LOGE("nullptr launcherAbilityResourceInfosObject");
180 }
181
182 return launcherAbilityResourceInfosObject;
183 }
184
185 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)186 ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result)
187 {
188 APP_LOGI("ANI_Constructor resourceManager called");
189 ani_env* env;
190 ani_status status = vm->GetEnv(ANI_VERSION_1, &env);
191 RETURN_ANI_STATUS_IF_NOT_OK(status, "Unsupported ANI_VERSION_1");
192
193 arkts::ani_signature::Namespace nsName = arkts::ani_signature::Builder::BuildNamespace(NS_NAME_RESOURCEMANAGER);
194 ani_namespace kitNs = nullptr;
195 status = env->FindNamespace(nsName.Descriptor().c_str(), &kitNs);
196 if (status != ANI_OK) {
197 APP_LOGE("FindNamespace: %{public}s fail with %{public}d", NS_NAME_RESOURCEMANAGER, status);
198 return status;
199 }
200
201 std::array methods = {
202 ani_native_function { "getBundleResourceInfoNative", nullptr,
203 reinterpret_cast<void*>(AniGetBundleResourceInfo) },
204 ani_native_function { "getLauncherAbilityResourceInfoNative", nullptr,
205 reinterpret_cast<void*>(AniGetLauncherAbilityResourceInfo) },
206 ani_native_function { "getAllBundleResourceInfoNative", nullptr,
207 reinterpret_cast<void*>(AniGetAllBundleResourceInfo) },
208 ani_native_function { "getAllLauncherAbilityResourceInfoNative", nullptr,
209 reinterpret_cast<void*>(AniGetAllLauncherAbilityResourceInfo) }
210 };
211
212 status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size());
213 if (status != ANI_OK) {
214 APP_LOGE("Namespace_BindNativeFunctions: %{public}s fail with %{public}d", NS_NAME_RESOURCEMANAGER, status);
215 return status;
216 }
217
218 *result = ANI_VERSION_1;
219
220 APP_LOGI("ANI_Constructor finished");
221
222 return ANI_OK;
223 }
224 }
225 } // AppExecFwk
226 } // OHOS