• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "want.h"
29 
30 namespace OHOS {
31 namespace AppExecFwk {
32 using Want = OHOS::AAFwk::Want;
33 
34 struct PropertyInfo {
35     const std::string propertyName;
36     bool isNecessary = false;
37     napi_valuetype propertyType = napi_undefined;
38 };
39 
40 class CommonFunc {
41 public:
42 static napi_value WrapVoidToJS(napi_env env);
43 
44 static napi_value ParseInt(napi_env env, napi_value args, int32_t &param);
45 
46 static std::string GetStringFromNAPI(napi_env env, napi_value value);
47 
48 static sptr<IBundleMgr> GetBundleMgr();
49 
50 static sptr<IBundleInstaller> GetBundleInstaller();
51 
52 static bool ParsePropertyArray(napi_env env, napi_value args, const std::string &propertyName,
53     std::vector<napi_value> &valueVec);
54 
55 static bool ParseStringPropertyFromObject(napi_env env, napi_value args, const std::string &propertyName,
56     bool isNecessary, std::string &value);
57 
58 static bool ParsePropertyFromObject(napi_env env, napi_value args, const PropertyInfo &propertyInfo,
59     napi_value &property);
60 
61 static bool ParseBool(napi_env env, napi_value value, bool& result);
62 
63 static bool ParseString(napi_env env, napi_value value, std::string& result);
64 
65 static napi_value ParseStringArray(napi_env env, std::vector<std::string> &stringArray, napi_value args);
66 
67 static void ConvertWantInfo(napi_env env, napi_value objWantInfo, const Want &want);
68 
69 static bool ParseElementName(napi_env env, napi_value args, Want &want);
70 
71 static void ConvertElementName(napi_env env, napi_value elementInfo, const OHOS::AppExecFwk::ElementName &elementName);
72 
73 static bool ParseWant(napi_env env, napi_value args, Want &want);
74 
75 static bool ParseWantPerformance(napi_env env, napi_value args, Want &want);
76 
77 static bool ParseWantWithoutVerification(napi_env env, napi_value args, Want &want);
78 
79 static bool ParseAbilityInfo(napi_env env, napi_value param, AbilityInfo& abilityInfo);
80 
81 static ErrCode ConvertErrCode(ErrCode nativeErrCode);
82 
83 static void ConvertWindowSize(napi_env env, const AbilityInfo &abilityInfo, napi_value value);
84 
85 static void ConvertMetadata(napi_env env, const Metadata &metadata, napi_value value);
86 
87 static void ConvertAbilityInfos(napi_env env, const std::vector<AbilityInfo> &abilityInfos, napi_value value);
88 
89 static void ConvertAbilityInfo(napi_env env, const AbilityInfo &abilityInfo, napi_value objAbilityInfo);
90 
91 static void ConvertExtensionInfos(napi_env env, const std::vector<ExtensionAbilityInfo> &extensionInfos,
92     napi_value value);
93 
94 static void ConvertStringArrays(napi_env env, const std::vector<std::string> &strs, napi_value value);
95 
96 static void ConvertExtensionInfo(napi_env env, const ExtensionAbilityInfo &extensionInfo, napi_value objExtensionInfo);
97 
98 static void ConvertResource(napi_env env, const Resource &resource, napi_value objResource);
99 
100 static void ConvertApplicationInfo(napi_env env, napi_value objAppInfo, const ApplicationInfo &appInfo);
101 
102 static void ConvertPermissionDef(napi_env env, napi_value result, const PermissionDef &permissionDef);
103 
104 static void ConvertRequestPermission(napi_env env, const RequestPermission &requestPermission, napi_value result);
105 
106 static void ConvertRequestPermissionUsedScene(napi_env env,
107     const RequestPermissionUsedScene &requestPermissionUsedScene, napi_value result);
108 
109 static void ConvertSignatureInfo(napi_env env, const SignatureInfo &signatureInfo, napi_value value);
110 
111 static void ConvertHapModuleInfo(napi_env env, const HapModuleInfo &hapModuleInfo, napi_value objHapModuleInfo);
112 
113 static void ConvertDependency(napi_env env, const std::string &moduleName, napi_value value);
114 
115 static void ConvertBundleInfo(napi_env env, const BundleInfo &bundleInfo, napi_value objBundleInfo, int32_t flags);
116 
117 static void ConvertBundleChangeInfo(napi_env env, const std::string &bundleName,
118     int32_t userId, napi_value bundleChangeInfo);
119 
120 static void ConvertLauncherAbilityInfo(napi_env env, const LauncherAbilityInfo &launcherAbility, napi_value value);
121 
122 static void ConvertLauncherAbilityInfos(napi_env env,
123     const std::vector<LauncherAbilityInfo> &launcherAbilities, napi_value value);
124 
125 static void ConvertShortcutIntent(napi_env env,
126     const OHOS::AppExecFwk::ShortcutIntent &shortcutIntent, napi_value value);
127 
128 static void ConvertShortCutInfo(napi_env env, const ShortcutInfo &shortcutInfo, napi_value value);
129 
130 static void ConvertShortCutInfos(napi_env env, const std::vector<ShortcutInfo> &shortcutInfos, napi_value value);
131 
132 static void ConvertPreloadItem(napi_env env, const PreloadItem &preloadItem, napi_value value);
133 
134 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 *))135 static napi_value AsyncCallNativeMethod(napi_env env,
136                                  T *asyncCallbackInfo,
137                                  const std::string &methodName,
138                                  void (*execFunc)(napi_env, void *),
139                                  void (*completeFunc)(napi_env, napi_status, void *))
140 {
141     if (asyncCallbackInfo == nullptr) {
142         APP_LOGE("asyncCallbackInfo is null");
143         return nullptr;
144     }
145     napi_value promise = nullptr;
146     if (asyncCallbackInfo->callback == nullptr) {
147         NAPI_CALL(env, napi_create_promise(env, &asyncCallbackInfo->deferred, &promise));
148     } else {
149         NAPI_CALL(env, napi_get_undefined(env, &promise));
150     }
151     napi_value resource = nullptr;
152     NAPI_CALL(env, napi_create_string_utf8(env, methodName.c_str(), NAPI_AUTO_LENGTH, &resource));
153     NAPI_CALL(env, napi_create_async_work(
154         env, nullptr, resource, execFunc, completeFunc,
155         reinterpret_cast<void*>(asyncCallbackInfo), &asyncCallbackInfo->asyncWork));
156     NAPI_CALL(env, napi_queue_async_work(env, asyncCallbackInfo->asyncWork));
157     return promise;
158 }
159 
160 private:
161     static sptr<IBundleMgr> bundleMgr_;
162     static std::mutex bundleMgrMutex_;
163 };
164 
165 #define PARSE_PROPERTY(env, property, funcType, value)                                        \
166     do {                                                                                      \
167         NAPI_CALL_BASE(env, napi_get_value_##funcType(env, property, (&(value))), false);         \
168     } while (0)
169 
170 #define CHKRV_SCOPE(env, state, scope) \
171     do { \
172         if ((state) != napi_ok) { \
173             napi_close_handle_scope(env, scope); \
174             return; \
175         } \
176     } while (0)
177 } // AppExecFwk
178 } // OHOS
179 #endif