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 bool ParseUint(napi_env env, napi_value args, uint32_t ¶m);
50
51 static std::string GetStringFromNAPI(napi_env env, napi_value value);
52
53 static sptr<IBundleMgr> GetBundleMgr();
54
55 static sptr<IBundleInstaller> GetBundleInstaller();
56
57 static sptr<IVerifyManager> GetVerifyManager();
58
59 static sptr<IExtendResourceManager> GetExtendResourceManager();
60
61 static bool CheckBundleFlagWithPermission(int32_t flag);
62
63 static bool ParsePropertyArray(napi_env env, napi_value args, const std::string &propertyName,
64 std::vector<napi_value> &valueVec);
65
66 static bool ParseStringPropertyFromObject(napi_env env, napi_value args, const std::string &propertyName,
67 bool isNecessary, std::string &value);
68
69 static bool ParsePropertyFromObject(napi_env env, napi_value args, const PropertyInfo &propertyInfo,
70 napi_value &property);
71
72 static bool ParseBool(napi_env env, napi_value value, bool& result);
73
74 static bool ParseString(napi_env env, napi_value value, std::string& result);
75
76 static napi_value ParseStringArray(napi_env env, std::vector<std::string> &stringArray, napi_value args);
77
78 static void ConvertWantInfo(napi_env env, napi_value objWantInfo, const Want &want);
79
80 static bool ParseElementName(napi_env env, napi_value args, Want &want);
81
82 static bool ParseElementName(napi_env env, napi_value args, ElementName &elementName);
83
84 static void ConvertElementName(napi_env env, napi_value elementInfo, const OHOS::AppExecFwk::ElementName &elementName);
85
86 static bool ParseWant(napi_env env, napi_value args, Want &want);
87
88 static bool ParseWantPerformance(napi_env env, napi_value args, Want &want);
89
90 static bool ParseWantWithoutVerification(napi_env env, napi_value args, Want &want);
91
92 static bool ParseAbilityInfo(napi_env env, napi_value param, AbilityInfo& abilityInfo);
93
94 static bool ParseShortCutInfo(napi_env env, napi_value param, ShortcutInfo &shortcutInfo);
95
96 static bool ParseShortcutWant(napi_env env, napi_value param, ShortcutIntent &shortcutIntent);
97
98 static bool ParseShortcutWantArray(
99 napi_env env, napi_value args, std::vector<ShortcutIntent> &shortcutIntents);
100
101 static bool ParseParameters(
102 napi_env env, napi_value args, std::map<std::string, std::string> ¶meters);
103
104 static bool ParseParameterItem(napi_env env, napi_value param, std::string &key, std::string &value);
105
106 static ErrCode ConvertErrCode(ErrCode nativeErrCode);
107
108 static void ConvertWindowSize(napi_env env, const AbilityInfo &abilityInfo, napi_value value);
109
110 static void ConvertMetadata(napi_env env, const Metadata &metadata, napi_value value);
111
112 static void ConvertAbilityInfos(napi_env env, const std::vector<AbilityInfo> &abilityInfos, napi_value value);
113
114 static void ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo, napi_value objAbilityInfo);
115
116 static void ConvertExtensionInfos(napi_env env, const std::vector<ExtensionAbilityInfo> &extensionInfos,
117 napi_value value);
118
119 static void ConvertStringArrays(napi_env env, const std::vector<std::string> &strs, napi_value value);
120
121 static void ConvertValidity(napi_env env, const Validity &validity, napi_value objValidity);
122
123 static void ConvertAppProvisionInfo(
124 napi_env env, const AppProvisionInfo &appProvisionInfo, napi_value objAppProvisionInfo);
125
126 static void ConvertExtensionInfo(napi_env env, const ExtensionAbilityInfo &extensionInfo, napi_value objExtensionInfo);
127
128 static void ConvertResource(napi_env env, const Resource &resource, napi_value objResource);
129
130 static void ConvertApplicationInfo(napi_env env, napi_value objAppInfo, const ApplicationInfo &appInfo);
131
132 static void ConvertPermissionDef(napi_env env, napi_value result, const PermissionDef &permissionDef);
133
134 static void ConvertRequestPermission(napi_env env, const RequestPermission &requestPermission, napi_value result);
135
136 static void ConvertRequestPermissionUsedScene(napi_env env,
137 const RequestPermissionUsedScene &requestPermissionUsedScene, napi_value result);
138
139 static void ConvertSignatureInfo(napi_env env, const SignatureInfo &signatureInfo, napi_value value);
140
141 static void ConvertHapModuleInfo(napi_env env, const HapModuleInfo &hapModuleInfo, napi_value objHapModuleInfo);
142
143 static void ConvertDependency(napi_env env, const Dependency &dependency, napi_value value);
144
145 static void ConvertBundleInfo(napi_env env, const BundleInfo &bundleInfo, napi_value objBundleInfo, int32_t flags);
146
147 static void ConvertBundleChangeInfo(napi_env env, const std::string &bundleName,
148 int32_t userId, int32_t appIndex, napi_value bundleChangeInfo);
149
150 static void ConvertLauncherAbilityInfo(napi_env env, const LauncherAbilityInfo &launcherAbility, napi_value value);
151
152 static void ConvertLauncherAbilityInfos(napi_env env,
153 const std::vector<LauncherAbilityInfo> &launcherAbilities, napi_value value);
154
155 static void ConvertShortcutIntent(napi_env env,
156 const OHOS::AppExecFwk::ShortcutIntent &shortcutIntent, napi_value value);
157
158 static void ConvertShortCutInfo(napi_env env, const ShortcutInfo &shortcutInfo, napi_value value);
159
160 static void ConvertShortCutInfos(napi_env env, const std::vector<ShortcutInfo> &shortcutInfos, napi_value value);
161
162 static void ConvertPreloadItem(napi_env env, const PreloadItem &preloadItem, napi_value value);
163
164 static void ConvertOverlayModuleInfo(napi_env env, const OverlayModuleInfo &info, napi_value objOverlayModuleInfo);
165
166 static void ConvertOverlayModuleInfos(napi_env env, const std::vector<OverlayModuleInfo> &Infos, napi_value objInfos);
167
168 static void ConvertModuleMetaInfos(napi_env env,
169 const std::map<std::string, std::vector<Metadata>> &metadata, napi_value objInfos);
170
171 static std::string ObtainCallingBundleName();
172
173 static void ConvertSharedModuleInfo(napi_env env, napi_value value, const SharedModuleInfo &moduleInfo);
174 static void ConvertSharedBundleInfo(napi_env env, napi_value value, const SharedBundleInfo &bundleInfo);
175 static void ConvertAllSharedBundleInfo(napi_env env, napi_value value,
176 const std::vector<SharedBundleInfo> &sharedBundles);
177 static void ConvertRecoverableApplicationInfo(
178 napi_env env, napi_value value, const RecoverableApplicationInfo &recoverableApplication);
179 static void ConvertRecoverableApplicationInfos(napi_env env, napi_value value,
180 const std::vector<RecoverableApplicationInfo> &recoverableApplications);
181
182 static void ConvertRouterItem(napi_env env, const RouterItem &routerItem, napi_value value);
183 static void ConvertRouterDataInfos(napi_env env, const std::map<std::string, std::string> &data, napi_value objInfos);
184 static void ConvertAbilitySkillUri(napi_env env, const SkillUri &skillUri, napi_value value, bool isExtension);
185 static void ConvertAbilitySkill(napi_env env, const Skill &skill, napi_value value, bool isExtension);
186
187 static void ConvertParameters(napi_env env,
188 const std::map<std::string, std::string> &data, napi_value objInfos);
189
190 static bool CheckShortcutInfo(const ShortcutInfo &shortcutInfo);
191
192 class BundleMgrCommonDeathRecipient : public IRemoteObject::DeathRecipient {
193 void OnRemoteDied([[maybe_unused]] const wptr<IRemoteObject>& remote) override;
194 };
195
196 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 *))197 static napi_value AsyncCallNativeMethod(napi_env env,
198 T *asyncCallbackInfo,
199 const std::string &methodName,
200 void (*execFunc)(napi_env, void *),
201 void (*completeFunc)(napi_env, napi_status, void *))
202 {
203 if (asyncCallbackInfo == nullptr) {
204 APP_LOGE("asyncCallbackInfo is null");
205 return nullptr;
206 }
207 napi_value promise = nullptr;
208 if (asyncCallbackInfo->callback == nullptr) {
209 NAPI_CALL(env, napi_create_promise(env, &asyncCallbackInfo->deferred, &promise));
210 } else {
211 NAPI_CALL(env, napi_get_undefined(env, &promise));
212 }
213 napi_value resource = nullptr;
214 NAPI_CALL(env, napi_create_string_utf8(env, methodName.c_str(), NAPI_AUTO_LENGTH, &resource));
215 NAPI_CALL(env, napi_create_async_work(
216 env, nullptr, resource, execFunc, completeFunc,
217 reinterpret_cast<void*>(asyncCallbackInfo), &asyncCallbackInfo->asyncWork));
218 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncCallbackInfo->asyncWork, napi_qos_user_initiated));
219 return promise;
220 }
221
222 template<typename T>
NapiReturnDeferred(napi_env env,T * asyncCallbackInfo,napi_value result[],const size_t resultSize)223 static void NapiReturnDeferred(napi_env env, T *asyncCallbackInfo, napi_value result[], const size_t resultSize)
224 {
225 const size_t size = 1;
226 if (resultSize < size) {
227 return;
228 }
229 if (asyncCallbackInfo->deferred) {
230 if (asyncCallbackInfo->err == 0) {
231 if (resultSize == size) {
232 napi_get_undefined(env, &result[0]);
233 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[0]));
234 } else {
235 NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncCallbackInfo->deferred, result[size]));
236 }
237 } else {
238 NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncCallbackInfo->deferred, result[0]));
239 }
240 } else {
241 napi_value callback = nullptr;
242 napi_value placeHolder = nullptr;
243 NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncCallbackInfo->callback, &callback));
244 NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, resultSize, result, &placeHolder));
245 }
246 }
247
248 private:
249 static sptr<IBundleMgr> bundleMgr_;
250 static std::mutex bundleMgrMutex_;
251 static sptr<IRemoteObject::DeathRecipient> deathRecipient_;
252 };
253
254 #define PARSE_PROPERTY(env, property, funcType, value) \
255 do { \
256 NAPI_CALL_BASE(env, napi_get_value_##funcType(env, property, (&(value))), false); \
257 } while (0)
258
259 #define CHKRV_SCOPE(env, state, scope) \
260 do { \
261 if ((state) != napi_ok) { \
262 napi_close_handle_scope(env, scope); \
263 return; \
264 } \
265 } while (0)
266 } // AppExecFwk
267 } // OHOS
268 #endif