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 "napi_constants.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr const char* NS_NAME_RESOURCEMANAGER = "@ohos.bundle.bundleResourceManager.bundleResourceManager";
27 }
28
AniGetBundleResourceInfo(ani_env * env,ani_string aniBundleName,ani_double aniResFlag,ani_double aniAppIndex)29 static ani_object AniGetBundleResourceInfo(ani_env* env, ani_string aniBundleName,
30 ani_double aniResFlag, ani_double aniAppIndex)
31 {
32 APP_LOGI("SystemCapability.BundleManager.BundleFramework.Resource not supported");
33 BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, GET_BUNDLE_RESOURCE_INFO, "");
34 return nullptr;
35 }
36
AniGetLauncherAbilityResourceInfo(ani_env * env,ani_string aniBundleName,ani_double aniResFlag,ani_double aniAppIndex)37 static ani_object AniGetLauncherAbilityResourceInfo(ani_env* env, ani_string aniBundleName,
38 ani_double aniResFlag, ani_double aniAppIndex)
39 {
40 APP_LOGI("SystemCapability.BundleManager.BundleFramework.Resource not supported");
41 BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, GET_LAUNCHER_ABILITY_RESOURCE_INFO, "");
42 return nullptr;
43 }
44
AniGetAllBundleResourceInfo(ani_env * env,ani_double aniResFlag)45 static ani_object AniGetAllBundleResourceInfo(ani_env* env, ani_double aniResFlag)
46 {
47 APP_LOGI("SystemCapability.BundleManager.BundleFramework.Resource not supported");
48 BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, GET_ALL_BUNDLE_RESOURCE_INFO, "");
49 return nullptr;
50 }
51
AniGetAllLauncherAbilityResourceInfo(ani_env * env,ani_double aniResFlag)52 static ani_object AniGetAllLauncherAbilityResourceInfo(ani_env* env, ani_double aniResFlag)
53 {
54 APP_LOGI("SystemCapability.BundleManager.BundleFramework.Resource not supported");
55 BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, "");
56 return nullptr;
57 }
58
59 extern "C" {
ANI_Constructor(ani_vm * vm,uint32_t * result)60 ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result)
61 {
62 APP_LOGI("ANI_Constructor resourceManager called");
63 ani_env* env;
64 ani_status status = vm->GetEnv(ANI_VERSION_1, &env);
65 RETURN_ANI_STATUS_IF_NOT_OK(status, "Unsupported ANI_VERSION_1");
66
67 arkts::ani_signature::Namespace nsName = arkts::ani_signature::Builder::BuildNamespace(NS_NAME_RESOURCEMANAGER);
68 ani_namespace kitNs = nullptr;
69 status = env->FindNamespace(nsName.Descriptor().c_str(), &kitNs);
70 if (status != ANI_OK) {
71 APP_LOGE("FindNamespace: %{public}s fail with %{public}d", NS_NAME_RESOURCEMANAGER, status);
72 return status;
73 }
74
75 std::array methods = {
76 ani_native_function { "getBundleResourceInfoNative", nullptr,
77 reinterpret_cast<void*>(AniGetBundleResourceInfo) },
78 ani_native_function { "getLauncherAbilityResourceInfoNative", nullptr,
79 reinterpret_cast<void*>(AniGetLauncherAbilityResourceInfo) },
80 ani_native_function { "getAllBundleResourceInfoNative", nullptr,
81 reinterpret_cast<void*>(AniGetAllBundleResourceInfo) },
82 ani_native_function { "getAllLauncherAbilityResourceInfoNative", nullptr,
83 reinterpret_cast<void*>(AniGetAllLauncherAbilityResourceInfo) }
84 };
85
86 status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size());
87 if (status != ANI_OK) {
88 APP_LOGE("Namespace_BindNativeFunctions: %{public}s fail with %{public}d", NS_NAME_RESOURCEMANAGER, status);
89 return status;
90 }
91
92 *result = ANI_VERSION_1;
93
94 APP_LOGI("ANI_Constructor finished");
95
96 return ANI_OK;
97 }
98 }
99 } // AppExecFwk
100 } // OHOS