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