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