1 /*
2 * Copyright (c) 2021-2022 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 <pthread.h>
16 #include <cstdio>
17 #include <cstring>
18 #include <unistd.h>
19
20 #include "app_log_wrapper.h"
21 #include "bundle_mgr.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 using namespace OHOS::AbilityRuntime;
28 EXTERN_C_START
29 /*
30 * function for module exports
31 */
JsBundleMgrInit(NativeEngine * engine,NativeValue * exports)32 static NativeValue* JsBundleMgrInit(NativeEngine* engine, NativeValue* exports)
33 {
34 APP_LOGD("JsBundleMgrInit is called");
35 if (engine == nullptr || exports == nullptr) {
36 APP_LOGE("Invalid input parameters");
37 return nullptr;
38 }
39
40 NativeObject* object = ConvertNativeValueTo<NativeObject>(exports);
41 if (object == nullptr) {
42 APP_LOGE("object is nullptr");
43 return nullptr;
44 }
45
46 std::unique_ptr<JsBundleMgr> jsBundleMgr = std::make_unique<JsBundleMgr>();
47 object->SetNativePointer(jsBundleMgr.release(), JsBundleMgr::Finalizer, nullptr);
48 object->SetProperty("AbilityType", CreateAbilityTypeObject(engine));
49 object->SetProperty("AbilitySubType", CreateAbilitySubTypeObject(engine));
50 object->SetProperty("DisplayOrientation", CreateDisplayOrientationObject(engine));
51 object->SetProperty("LaunchMode", CreateLaunchModeObject(engine));
52 object->SetProperty("ColorMode", CreateColorModeObject(engine));
53 object->SetProperty("GrantStatus", CreateGrantStatusObject(engine));
54 object->SetProperty("ModuleRemoveFlag", CreateModuleRemoveFlagObject(engine));
55 object->SetProperty("SignatureCompareResult", CreateSignatureCompareResultObject(engine));
56 object->SetProperty("ShortcutExistence", CreateShortcutExistenceObject(engine));
57 object->SetProperty("QueryShortCutFlag", CreateQueryShortCutFlagObject(engine));
58 object->SetProperty("InstallErrorCode", CreateInstallErrorCodeObject(engine));
59 object->SetProperty("BundleFlag", CreateBundleFlagObject(engine));
60
61 const char *moduleName = "JsBundleMgr";
62 BindNativeFunction(*engine, *object, "getAllApplicationInfo", moduleName, JsBundleMgr::GetAllApplicationInfo);
63 BindNativeFunction(*engine, *object, "getApplicationInfo", moduleName, JsBundleMgr::GetApplicationInfo);
64 BindNativeFunction(*engine, *object, "getBundleArchiveInfo", moduleName, JsBundleMgr::GetBundleArchiveInfo);
65 BindNativeFunction(*engine, *object, "getLaunchWantForBundle", moduleName, JsBundleMgr::GetLaunchWantForBundle);
66 BindNativeFunction(*engine, *object, "isAbilityEnabled", moduleName, JsBundleMgr::IsAbilityEnabled);
67 BindNativeFunction(*engine, *object, "isApplicationEnabled", moduleName, JsBundleMgr::IsApplicationEnabled);
68 BindNativeFunction(*engine, *object, "getAbilityIcon", moduleName, JsBundleMgr::GetAbilityIcon);
69 BindNativeFunction(*engine, *object, "getBundleInfo", moduleName, JsBundleMgr::GetBundleInfo);
70 BindNativeFunction(*engine, *object, "getNameForUid", moduleName, JsBundleMgr::GetNameForUid);
71 BindNativeFunction(*engine, *object, "getAbilityInfo", moduleName, JsBundleMgr::GetAbilityInfo);
72 BindNativeFunction(*engine, *object, "getAbilityLabel", moduleName, JsBundleMgr::GetAbilityLabel);
73 BindNativeFunction(*engine, *object, "setAbilityEnabled", moduleName, JsBundleMgr::SetAbilityEnabled);
74 BindNativeFunction(*engine, *object, "setApplicationEnabled", moduleName, JsBundleMgr::SetApplicationEnabled);
75 BindNativeFunction(*engine, *object, "queryAbilityByWant", moduleName, JsBundleMgr::QueryAbilityInfos);
76 BindNativeFunction(*engine, *object, "getAllBundleInfo", moduleName, JsBundleMgr::GetAllBundleInfo);
77 BindNativeFunction(*engine, *object, "getPermissionDef", moduleName, JsBundleMgr::GetPermissionDef);
78 BindNativeFunction(*engine, *object, "getBundleInstaller", moduleName, JsBundleMgr::GetBundleInstaller);
79 return exports;
80 }
81
Init(napi_env env,napi_value exports)82 static napi_value Init(napi_env env, napi_value exports)
83 {
84 napi_property_descriptor desc[] = {
85 DECLARE_NAPI_FUNCTION("getApplicationInfos", GetApplicationInfos),
86 DECLARE_NAPI_FUNCTION("getBundleInfos", GetBundleInfos),
87 DECLARE_NAPI_FUNCTION("cleanBundleCacheFiles", ClearBundleCache),
88 };
89 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
90
91 APP_LOGI("Init end");
92 return reinterpret_cast<napi_value>(JsBundleMgrInit(reinterpret_cast<NativeEngine*>(env),
93 reinterpret_cast<NativeValue*>(exports)));
94 }
95 EXTERN_C_END
96
97 /*
98 * Module define
99 */
100 static napi_module _module = {
101 .nm_version = 1,
102 .nm_flags = 0,
103 .nm_filename = nullptr,
104 .nm_register_func = Init,
105 .nm_modname = "bundle",
106 .nm_priv = ((void *)0),
107 .reserved = {0}
108 };
109 /*
110 * Module register function
111 */
RegisterModule(void)112 extern "C" __attribute__((constructor)) void RegisterModule(void)
113 {
114 napi_module_register(&_module);
115 }
116 } // namespace AppExecFwk
117 } // namespace OHOS