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