1 /*
2 * Copyright (c) 2022-2023 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
16 #ifndef COMMON_FUNC_H
17 #define COMMON_FUNC_H
18
19 #include <vector>
20 #include <mutex>
21
22 #include "app_log_wrapper.h"
23 #include "bundle_mgr_interface.h"
24 #include "iremote_object.h"
25 #include "launcher_ability_info.h"
26 #include "napi/native_api.h"
27 #include "napi/native_common.h"
28 #include "napi/native_node_api.h"
29 #include "shared/shared_bundle_info.h"
30 #include "shared/shared_module_info.h"
31 #include "want.h"
32
33 namespace OHOS {
34 namespace AppExecFwk {
35 using Want = OHOS::AAFwk::Want;
36
37 struct PropertyInfo {
38 const std::string propertyName;
39 bool isNecessary = false;
40 napi_valuetype propertyType = napi_undefined;
41 };
42
43 class CommonFunc {
44 public:
45 static napi_value WrapVoidToJS(napi_env env);
46
47 static bool ParseInt(napi_env env, napi_value args, int32_t ¶m);
48
49 static std::string GetStringFromNAPI(napi_env env, napi_value value);
50
51 static sptr<IBundleMgr> GetBundleMgr();
52
53 static sptr<IBundleInstaller> GetBundleInstaller();
54
55 static sptr<IVerifyManager> GetVerifyManager();
56
57 static bool ParsePropertyArray(napi_env env, napi_value args, const std::string &propertyName,
58 std::vector<napi_value> &valueVec);
59
60 static bool ParseStringPropertyFromObject(napi_env env, napi_value args, const std::string &propertyName,
61 bool isNecessary, std::string &value);
62
63 static bool ParsePropertyFromObject(napi_env env, napi_value args, const PropertyInfo &propertyInfo,
64 napi_value &property);
65
66 static bool ParseBool(napi_env env, napi_value value, bool& result);
67
68 static bool ParseString(napi_env env, napi_value value, std::string& result);
69
70 static napi_value ParseStringArray(napi_env env, std::vector<std::string> &stringArray, napi_value args);
71
72 static void ConvertWantInfo(napi_env env, napi_value objWantInfo, const Want &want);
73
74 static bool ParseElementName(napi_env env, napi_value args, Want &want);
75
76 static bool ParseElementName(napi_env env, napi_value args, ElementName &elementName);
77
78 static void ConvertElementName(napi_env env, napi_value elementInfo, const OHOS::AppExecFwk::ElementName &elementName);
79
80 static bool ParseWant(napi_env env, napi_value args, Want &want);
81
82 static bool ParseWantPerformance(napi_env env, napi_value args, Want &want);
83
84 static bool ParseWantWithoutVerification(napi_env env, napi_value args, Want &want);
85
86 static bool ParseAbilityInfo(napi_env env, napi_value param, AbilityInfo& abilityInfo);
87
88 static ErrCode ConvertErrCode(ErrCode nativeErrCode);
89
90 static void ConvertWindowSize(napi_env env, const AbilityInfo &abilityInfo, napi_value value);
91
92 static void ConvertMetadata(napi_env env, const Metadata &metadata, napi_value value);
93
94 static void ConvertAbilityInfos(napi_env env, const std::vector<AbilityInfo> &abilityInfos, napi_value value);
95
96 static void ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo, napi_value objAbilityInfo);
97
98 static void ConvertExtensionInfos(napi_env env, const std::vector<ExtensionAbilityInfo> &extensionInfos,
99 napi_value value);
100
101 static void ConvertStringArrays(napi_env env, const std::vector<std::string> &strs, napi_value value);
102
103 static void ConvertValidity(napi_env env, const Validity &validity, napi_value objValidity);
104
105 static void ConvertAppProvisionInfo(
106 napi_env env, const AppProvisionInfo &appProvisionInfo, napi_value objAppProvisionInfo);
107
108 static void ConvertExtensionInfo(napi_env env, const ExtensionAbilityInfo &extensionInfo, napi_value objExtensionInfo);
109
110 static void ConvertResource(napi_env env, const Resource &resource, napi_value objResource);
111
112 static void ConvertApplicationInfo(napi_env env, napi_value objAppInfo, const ApplicationInfo &appInfo);
113
114 static void ConvertPermissionDef(napi_env env, napi_value result, const PermissionDef &permissionDef);
115
116 static void ConvertRequestPermission(napi_env env, const RequestPermission &requestPermission, napi_value result);
117
118 static void ConvertRequestPermissionUsedScene(napi_env env,
119 const RequestPermissionUsedScene &requestPermissionUsedScene, napi_value result);
120
121 static void ConvertSignatureInfo(napi_env env, const SignatureInfo &signatureInfo, napi_value value);
122
123 static void ConvertHapModuleInfo(napi_env env, const HapModuleInfo &hapModuleInfo, napi_value objHapModuleInfo);
124
125 static void ConvertDependency(napi_env env, const Dependency &dependency, napi_value value);
126
127 static void ConvertBundleInfo(napi_env env, const BundleInfo &bundleInfo, napi_value objBundleInfo, int32_t flags);
128
129 static void ConvertBundleChangeInfo(napi_env env, const std::string &bundleName,
130 int32_t userId, napi_value bundleChangeInfo);
131
132 static void ConvertLauncherAbilityInfo(napi_env env, const LauncherAbilityInfo &launcherAbility, napi_value value);
133
134 static void ConvertLauncherAbilityInfos(napi_env env,
135 const std::vector<LauncherAbilityInfo> &launcherAbilities, napi_value value);
136
137 static void ConvertShortcutIntent(napi_env env,
138 const OHOS::AppExecFwk::ShortcutIntent &shortcutIntent, napi_value value);
139
140 static void ConvertShortCutInfo(napi_env env, const ShortcutInfo &shortcutInfo, napi_value value);
141
142 static void ConvertShortCutInfos(napi_env env, const std::vector<ShortcutInfo> &shortcutInfos, napi_value value);
143
144 static void ConvertPreloadItem(napi_env env, const PreloadItem &preloadItem, napi_value value);
145
146 static void ConvertOverlayModuleInfo(napi_env env, const OverlayModuleInfo &info, napi_value objOverlayModuleInfo);
147
148 static void ConvertOverlayModuleInfos(napi_env env, const std::vector<OverlayModuleInfo> &Infos, napi_value objInfos);
149
150 static void ConvertModuleMetaInfos(napi_env env,
151 const std::map<std::string, std::vector<Metadata>> &metadata, napi_value objInfos);
152
153 static std::string ObtainCallingBundleName();
154
155 static void ConvertSharedModuleInfo(napi_env env, napi_value value, const SharedModuleInfo &moduleInfo);
156 static void ConvertSharedBundleInfo(napi_env env, napi_value value, const SharedBundleInfo &bundleInfo);
157 static void ConvertAllSharedBundleInfo(napi_env env, napi_value value,
158 const std::vector<SharedBundleInfo> &sharedBundles);
159 static void ConvertRecoverableApplicationInfo(
160 napi_env env, napi_value value, const RecoverableApplicationInfo &recoverableApplication);
161 static void ConvertRecoverableApplicationInfos(napi_env env, napi_value value,
162 const std::vector<RecoverableApplicationInfo> &recoverableApplications);
163
164 class BundleMgrCommonDeathRecipient : public IRemoteObject::DeathRecipient {
165 void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override;
166 };
167
168 template<typename T>
AsyncCallNativeMethod(napi_env env,T * asyncCallbackInfo,const std::string & methodName,void (* execFunc)(napi_env,void *),void (* completeFunc)(napi_env,napi_status,void *))169 static napi_value AsyncCallNativeMethod(napi_env env,
170 T *asyncCallbackInfo,
171 const std::string &methodName,
172 void (*execFunc)(napi_env, void *),
173 void (*completeFunc)(napi_env, napi_status, void *))
174 {
175 if (asyncCallbackInfo == nullptr) {
176 APP_LOGE("asyncCallbackInfo is null");
177 return nullptr;
178 }
179 napi_value promise = nullptr;
180 if (asyncCallbackInfo->callback == nullptr) {
181 NAPI_CALL(env, napi_create_promise(env, &asyncCallbackInfo->deferred, &promise));
182 } else {
183 NAPI_CALL(env, napi_get_undefined(env, &promise));
184 }
185 napi_value resource = nullptr;
186 NAPI_CALL(env, napi_create_string_utf8(env, methodName.c_str(), NAPI_AUTO_LENGTH, &resource));
187 NAPI_CALL(env, napi_create_async_work(
188 env, nullptr, resource, execFunc, completeFunc,
189 reinterpret_cast<void*>(asyncCallbackInfo), &asyncCallbackInfo->asyncWork));
190 NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
191 return promise;
192 }
193
194 template<typename T>
NapiReturnDeferred(napi_env env,T * asyncCallbackInfo,napi_value result[],const size_t resultSize)195 static void NapiReturnDeferred(napi_env env, T *asyncCallbackInfo, napi_value result[], const size_t resultSize)
196 {
197 const size_t size = 1;
198 if (resultSize < size) {
199 return;
200 }
201 if (asyncCallbackInfo->deferred) {
202 if (asyncCallbackInfo->err == 0) {
203 if (resultSize == size) {
204 napi_get_undefined(env, &result[0]);
205 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[0]));
206 } else {
207 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[size]));
208 }
209 } else {
210 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]));
211 }
212 } else {
213 napi_value callback = nullptr;
214 napi_value placeHolder = nullptr;
215 NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback));
216 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, resultSize, result, &placeHolder));
217 }
218 }
219
220 private:
221 static sptr<IBundleMgr> bundleMgr_;
222 static std::mutex bundleMgrMutex_;
223 static sptr<IRemoteObject::DeathRecipient> deathRecipient_;
224 };
225
226 #define PARSE_PROPERTY(env, property, funcType, value) \
227 do { \
228 NAPI_CALL_BASE(env, napi_get_value_##funcType(env, property, (&(value))), false); \
229 } while (0)
230
231 #define CHKRV_SCOPE(env, state, scope) \
232 do { \
233 if ((state) != napi_ok) { \
234 napi_close_handle_scope(env, scope); \
235 return; \
236 } \
237 } while (0)
238 } // AppExecFwk
239 } // OHOS
240 #endif