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
49 const char *moduleName = "JsBundleMgr";
50 BindNativeFunction(*engine, *object, "getAllApplicationInfo", moduleName, JsBundleMgr::GetAllApplicationInfo);
51 BindNativeFunction(*engine, *object, "getApplicationInfo", moduleName, JsBundleMgr::GetApplicationInfo);
52 BindNativeFunction(*engine, *object, "getBundleArchiveInfo", moduleName, JsBundleMgr::GetBundleArchiveInfo);
53 BindNativeFunction(*engine, *object, "getLaunchWantForBundle", moduleName, JsBundleMgr::GetLaunchWantForBundle);
54 BindNativeFunction(*engine, *object, "isAbilityEnabled", moduleName, JsBundleMgr::IsAbilityEnabled);
55 BindNativeFunction(*engine, *object, "isApplicationEnabled", moduleName, JsBundleMgr::IsApplicationEnabled);
56
57 return exports;
58 }
59
Init(napi_env env,napi_value exports)60 static napi_value Init(napi_env env, napi_value exports)
61 {
62 napi_value nAbilityType = nullptr;
63 NAPI_CALL(env, napi_create_object(env, &nAbilityType));
64 CreateAbilityTypeObject(env, nAbilityType);
65
66 napi_value nAbilitySubType = nullptr;
67 NAPI_CALL(env, napi_create_object(env, &nAbilitySubType));
68 CreateAbilitySubTypeObject(env, nAbilitySubType);
69
70 napi_value nDisplayOrientation = nullptr;
71 NAPI_CALL(env, napi_create_object(env, &nDisplayOrientation));
72 CreateDisplayOrientationObject(env, nDisplayOrientation);
73
74 napi_value nLaunchMode = nullptr;
75 NAPI_CALL(env, napi_create_object(env, &nLaunchMode));
76 CreateLaunchModeObject(env, nLaunchMode);
77
78 napi_value nColorMode = nullptr;
79 NAPI_CALL(env, napi_create_object(env, &nColorMode));
80 CreateColorModeObject(env, nColorMode);
81
82 napi_value nGrantStatus = nullptr;
83 NAPI_CALL(env, napi_create_object(env, &nGrantStatus));
84 CreateGrantStatusObject(env, nGrantStatus);
85
86 napi_value nModuleRemoveFlag = nullptr;
87 NAPI_CALL(env, napi_create_object(env, &nModuleRemoveFlag));
88 CreateModuleRemoveFlagObject(env, nModuleRemoveFlag);
89
90 napi_value nSignatureCompareResult = nullptr;
91 NAPI_CALL(env, napi_create_object(env, &nSignatureCompareResult));
92 CreateSignatureCompareResultObject(env, nSignatureCompareResult);
93
94 napi_value nShortcutExistence = nullptr;
95 NAPI_CALL(env, napi_create_object(env, &nShortcutExistence));
96 CreateShortcutExistenceObject(env, nShortcutExistence);
97
98 napi_value nQueryShortCutFlag = nullptr;
99 NAPI_CALL(env, napi_create_object(env, &nQueryShortCutFlag));
100 CreateQueryShortCutFlagObject(env, nShortcutExistence);
101
102 napi_value nBundleFlag = nullptr;
103 NAPI_CALL(env, napi_create_object(env, &nBundleFlag));
104 CreateBundleFlagObject(env, nBundleFlag);
105
106 napi_value nInstallErrorCode = nullptr;
107 NAPI_CALL(env, napi_create_object(env, &nInstallErrorCode));
108 CreateInstallErrorCodeObject(env, nInstallErrorCode);
109 /*
110 * Propertise define
111 */
112 napi_property_descriptor desc[] = {
113 DECLARE_NAPI_FUNCTION("getApplicationInfos", GetApplicationInfos),
114 DECLARE_NAPI_FUNCTION("getAllBundleInfo", GetBundleInfos),
115 DECLARE_NAPI_FUNCTION("getBundleInfos", GetBundleInfos),
116 DECLARE_NAPI_FUNCTION("getBundleInfo", GetBundleInfo),
117 DECLARE_NAPI_FUNCTION("getPermissionDef", GetPermissionDef),
118 DECLARE_NAPI_FUNCTION("queryAbilityByWant", QueryAbilityInfos),
119 DECLARE_NAPI_FUNCTION("getBundleInstaller", GetBundleInstaller),
120 DECLARE_NAPI_FUNCTION("cleanBundleCacheFiles", ClearBundleCache),
121 DECLARE_NAPI_FUNCTION("setApplicationEnabled", SetApplicationEnabled),
122 DECLARE_NAPI_FUNCTION("setAbilityEnabled", SetAbilityEnabled),
123 DECLARE_NAPI_FUNCTION("getNameForUid", GetNameForUid),
124 DECLARE_NAPI_FUNCTION("getAbilityInfo", GetAbilityInfo),
125 DECLARE_NAPI_FUNCTION("getAbilityLabel", GetAbilityLabel),
126 DECLARE_NAPI_FUNCTION("getAbilityIcon", GetAbilityIcon),
127 DECLARE_NAPI_PROPERTY("AbilityType", nAbilityType),
128 DECLARE_NAPI_PROPERTY("AbilitySubType", nAbilitySubType),
129 DECLARE_NAPI_PROPERTY("DisplayOrientation", nDisplayOrientation),
130 DECLARE_NAPI_PROPERTY("LaunchMode", nLaunchMode),
131 DECLARE_NAPI_PROPERTY("ColorMode", nColorMode),
132 DECLARE_NAPI_PROPERTY("GrantStatus", nGrantStatus),
133 DECLARE_NAPI_PROPERTY("ModuleRemoveFlag", nModuleRemoveFlag),
134 DECLARE_NAPI_PROPERTY("SignatureCompareResult", nSignatureCompareResult),
135 DECLARE_NAPI_PROPERTY("ShortcutExistence", nShortcutExistence),
136 DECLARE_NAPI_PROPERTY("QueryShortCutFlag", nQueryShortCutFlag),
137 DECLARE_NAPI_PROPERTY("InstallErrorCode", nInstallErrorCode),
138 DECLARE_NAPI_PROPERTY("BundleFlag", nBundleFlag),
139 };
140 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
141
142 napi_value m_classBundleInstaller;
143 napi_property_descriptor properties[] = {
144 DECLARE_NAPI_FUNCTION("install", Install),
145 DECLARE_NAPI_FUNCTION("recover", Recover),
146 DECLARE_NAPI_FUNCTION("uninstall", Uninstall),
147 };
148 NAPI_CALL(env,
149 napi_define_class(env,
150 "BundleInstaller",
151 NAPI_AUTO_LENGTH,
152 BundleInstallerConstructor,
153 nullptr,
154 sizeof(properties) / sizeof(*properties),
155 properties,
156 &m_classBundleInstaller));
157 napi_create_reference(env, m_classBundleInstaller, 1, &g_classBundleInstaller);
158 APP_LOGI("Init end");
159 return reinterpret_cast<napi_value>(JsBundleMgrInit(reinterpret_cast<NativeEngine*>(env),
160 reinterpret_cast<NativeValue*>(exports)));
161 }
162 EXTERN_C_END
163
164 /*
165 * Module define
166 */
167 static napi_module _module = {
168 .nm_version = 1,
169 .nm_flags = 0,
170 .nm_filename = nullptr,
171 .nm_register_func = Init,
172 .nm_modname = "bundle",
173 .nm_priv = ((void *)0),
174 .reserved = {0}
175 };
176 /*
177 * Module register function
178 */
RegisterModule(void)179 extern "C" __attribute__((constructor)) void RegisterModule(void)
180 {
181 napi_module_register(&_module);
182 }
183 } // namespace AppExecFwk
184 } // namespace OHOS