1 /*
2  * Copyright (c) 2023-2024 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 INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H
17 #define INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H
18 
19 #include <vector>
20 #include <unistd.h>
21 #include <uv.h>
22 
23 #include "ability_context.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "napi_base_context.h"
27 #include "napi_common_want.h"
28 #include "dlp_file.h"
29 #include "dlp_sandbox_callback_info.h"
30 #include "dlp_sandbox_change_callback_customize.h"
31 #include "open_dlp_file_callback_customize.h"
32 #include "permission_policy.h"
33 #include "retention_sandbox_info.h"
34 #include "ui_content.h"
35 #include "visited_dlp_file_info.h"
36 
37 namespace OHOS {
38 namespace Security {
39 namespace DlpPermission {
40 constexpr int32_t PARAM0 = 0;
41 constexpr int32_t PARAM1 = 1;
42 constexpr int32_t PARAM2 = 2;
43 constexpr int32_t PARAM3 = 3;
44 constexpr int32_t PARAM4 = 4;
45 constexpr int32_t PARAM_SIZE_ONE = 1;
46 constexpr int32_t PARAM_SIZE_TWO = 2;
47 constexpr int32_t PARAM_SIZE_THREE = 3;
48 constexpr int32_t PARAM_SIZE_FOUR = 4;
49 constexpr int32_t PARAM_SIZE_FIVE = 5;
50 
51 
52 #define NAPI_CALL_BASE_WITH_SCOPE(env, theCall, retVal, scope) \
53     do {                                                       \
54         if ((theCall) != napi_ok) {                            \
55             GET_AND_THROW_LAST_ERROR((env));                   \
56             napi_close_handle_scope(env, scope);               \
57             return retVal;                                     \
58         }                                                      \
59     } while (0)
60 
61 #define NAPI_CALL_RETURN_VOID_WITH_SCOPE(env, theCall, scope) \
62     NAPI_CALL_BASE_WITH_SCOPE(env, theCall, NAPI_RETVAL_NOTHING, scope)
63 
64 class RegisterDlpSandboxChangeScopePtr : public DlpSandboxChangeCallbackCustomize {
65 public:
66     RegisterDlpSandboxChangeScopePtr();
67     ~RegisterDlpSandboxChangeScopePtr() override;
68     void DlpSandboxChangeCallback(DlpSandboxCallbackInfo &result) override;
69     void SetEnv(const napi_env &env);
70     void SetCallbackRef(const napi_ref &ref);
71     void SetValid(bool valid);
72 
73 private:
74     napi_env env_ = nullptr;
75     napi_ref ref_ = nullptr;
76     bool valid_ = true;
77     std::mutex validMutex_;
78 };
79 
80 struct CommonAsyncContext {
81     explicit CommonAsyncContext(napi_env napiEnv);
82     virtual ~CommonAsyncContext();
83     napi_env env = nullptr;
84     napi_status status = napi_invalid_arg;
85     int32_t errCode = 0;
86     napi_deferred deferred = nullptr;  // promise handle
87     napi_ref callbackRef = nullptr;    // callback handle
88     napi_async_work work = nullptr;    // work handle
89 };
90 
91 struct RegisterDlpSandboxChangeWorker {
92     napi_env env = nullptr;
93     napi_ref ref = nullptr;
94     DlpSandboxCallbackInfo result;
95     RegisterDlpSandboxChangeScopePtr *subscriber = nullptr;
96 };
97 
98 struct DlpSandboxChangeContext {
99     virtual ~DlpSandboxChangeContext();
100     napi_env env = nullptr;
101     napi_ref callbackRef = nullptr;
102     int32_t errCode = 0;
103     std::string changeType;
104     std::shared_ptr<RegisterDlpSandboxChangeScopePtr> subscriber = nullptr;
105     void DeleteNapiRef();
106 };
107 
108 typedef DlpSandboxChangeContext RegisterDlpSandboxChangeInfo;
109 
110 struct UnregisterSandboxChangeCallbackAsyncContext : public CommonAsyncContext {
UnregisterSandboxChangeCallbackAsyncContextUnregisterSandboxChangeCallbackAsyncContext111     explicit UnregisterSandboxChangeCallbackAsyncContext(napi_env env) : CommonAsyncContext(env) {};
112     bool result = false;
113     std::string changeType;
114 };
115 class OpenDlpFileSubscriberPtr : public OpenDlpFileCallbackCustomize {
116 public:
117     OpenDlpFileSubscriberPtr();
118     ~OpenDlpFileSubscriberPtr() override;
119     void OnOpenDlpFile(OpenDlpFileCallbackInfo &result) override;
120     void SetEnv(const napi_env &env);
121     void SetCallbackRef(const napi_ref &ref);
122     void SetValid(bool valid);
123 
124 private:
125     napi_env env_ = nullptr;
126     napi_ref ref_ = nullptr;
127     bool valid_ = true;
128     std::mutex validMutex_;
129 };
130 
131 struct OpenDlpFileSubscriberWorker {
132     napi_env env = nullptr;
133     napi_ref ref = nullptr;
134     OpenDlpFileCallbackInfo result;
135     OpenDlpFileSubscriberPtr *subscriber = nullptr;
136 };
137 
138 struct OpenDlpFileSubscriberContext {
139     virtual ~OpenDlpFileSubscriberContext();
140     napi_env env = nullptr;
141     napi_ref callbackRef = nullptr;
142     int32_t errCode = 0;
143     std::shared_ptr<OpenDlpFileSubscriberPtr> subscriber = nullptr;
144     void DeleteNapiRef();
145 };
146 
147 struct OpenDlpFileUnSubscriberContext : public CommonAsyncContext {
OpenDlpFileUnSubscriberContextOpenDlpFileUnSubscriberContext148     explicit OpenDlpFileUnSubscriberContext(napi_env env) : CommonAsyncContext(env) {};
149     bool result = false;
150 };
151 
152 struct GenerateDlpFileAsyncContext : public CommonAsyncContext {
GenerateDlpFileAsyncContextGenerateDlpFileAsyncContext153     explicit GenerateDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
154     int64_t plaintextFd = -1;
155     int64_t ciphertextFd = -1;
156     DlpProperty property;
157     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
158 };
159 
160 struct DlpFileAsyncContext : public CommonAsyncContext {
DlpFileAsyncContextDlpFileAsyncContext161     explicit DlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
162     int64_t ciphertextFd = -1;
163     std::string appId;
164     DlpProperty property;
165     bool isDlpFile = false;
166     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
167 };
168 
169 struct DlpLinkFileAsyncContext : public CommonAsyncContext {
DlpLinkFileAsyncContextDlpLinkFileAsyncContext170     explicit DlpLinkFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
171     std::string linkFileName = "";
172     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
173 };
174 
175 struct RecoverDlpFileAsyncContext : public CommonAsyncContext {
RecoverDlpFileAsyncContextRecoverDlpFileAsyncContext176     explicit RecoverDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
177     int64_t plaintextFd = -1;
178     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
179 };
180 
181 struct CloseDlpFileAsyncContext : public CommonAsyncContext {
CloseDlpFileAsyncContextCloseDlpFileAsyncContext182     explicit CloseDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
183     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
184 };
185 
186 struct DlpSandboxAsyncContext : public CommonAsyncContext {
DlpSandboxAsyncContextDlpSandboxAsyncContext187     explicit DlpSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
188     std::string bundleName;
189     DLPFileAccess dlpFileAccess = NO_PERMISSION;
190     int32_t userId = -1;
191     SandboxInfo sandboxInfo;
192     std::string uri = "";
193 };
194 
195 struct GetPermInfoAsyncContext : public CommonAsyncContext {
GetPermInfoAsyncContextGetPermInfoAsyncContext196     explicit GetPermInfoAsyncContext(napi_env env) : CommonAsyncContext(env) {};
197     DLPPermissionInfo permInfo;
198 };
199 
200 struct IsInSandboxAsyncContext : public CommonAsyncContext {
IsInSandboxAsyncContextIsInSandboxAsyncContext201     explicit IsInSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
202     bool inSandbox = false;
203 };
204 
205 struct IsDLPFeatureProvidedAsyncContext : public CommonAsyncContext {
IsDLPFeatureProvidedAsyncContextIsDLPFeatureProvidedAsyncContext206     explicit IsDLPFeatureProvidedAsyncContext(napi_env env) : CommonAsyncContext(env) {};
207     bool isProvideDLPFeature = false;
208 };
209 
210 struct GetOriginalFileAsyncContext : public CommonAsyncContext {
GetOriginalFileAsyncContextGetOriginalFileAsyncContext211     explicit GetOriginalFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
212     std::string dlpFilename = "";
213     std::string oriFilename = "";
214 };
215 
216 struct GetSuffixAsyncContext : public CommonAsyncContext {
GetSuffixAsyncContextGetSuffixAsyncContext217     explicit GetSuffixAsyncContext(napi_env env) : CommonAsyncContext(env) {};
218     std::string extension = "";
219 };
220 
221 struct GetDlpSupportFileTypeAsyncContext : public CommonAsyncContext {
GetDlpSupportFileTypeAsyncContextGetDlpSupportFileTypeAsyncContext222     explicit GetDlpSupportFileTypeAsyncContext(napi_env env) : CommonAsyncContext(env) {};
223     std::vector<std::string> supportFileType;
224 };
225 
226 void UvQueueWorkDeleteRef(uv_work_t *work, int32_t status);
227 
228 struct GetGatheringPolicyContext : public CommonAsyncContext {
GetGatheringPolicyContextGetGatheringPolicyContext229     explicit GetGatheringPolicyContext(napi_env env) : CommonAsyncContext(env) {};
230     bool isGathering = false;
231 };
232 
233 struct RetentionStateAsyncContext : public CommonAsyncContext {
RetentionStateAsyncContextRetentionStateAsyncContext234     explicit RetentionStateAsyncContext(napi_env env) : CommonAsyncContext(env) {};
235     std::vector<std::string> docUris;
236 };
237 
238 struct GetRetentionSandboxListAsyncContext : public CommonAsyncContext {
GetRetentionSandboxListAsyncContextGetRetentionSandboxListAsyncContext239     explicit GetRetentionSandboxListAsyncContext(napi_env env) : CommonAsyncContext(env) {};
240     std::string bundleName = "";
241     std::vector<RetentionSandBoxInfo> retentionSandBoxInfoVec;
242 };
243 
244 struct GetDLPFileVisitRecordAsyncContext : public CommonAsyncContext {
GetDLPFileVisitRecordAsyncContextGetDLPFileVisitRecordAsyncContext245     explicit GetDLPFileVisitRecordAsyncContext(napi_env env) : CommonAsyncContext(env) {};
246     std::vector<VisitedDLPFileInfo> visitedDlpFileInfoVec;
247 };
248 
249 struct SandboxAppConfigAsyncContext : public CommonAsyncContext {
SandboxAppConfigAsyncContextSandboxAppConfigAsyncContext250     explicit SandboxAppConfigAsyncContext(napi_env env) : CommonAsyncContext(env) {};
251     std::string configInfo = "";
252 };
253 
254 struct UIExtensionRequestContext : public CommonAsyncContext {
UIExtensionRequestContextUIExtensionRequestContext255     explicit UIExtensionRequestContext(napi_env env) : CommonAsyncContext(env) {};
256     std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> context = nullptr;
257     OHOS::AAFwk::Want requestWant;
258 };
259 
260 class UIExtensionCallback {
261 public:
262     explicit UIExtensionCallback(std::shared_ptr<UIExtensionRequestContext>& reqContext);
263     void SetSessionId(int32_t sessionId);
264     void OnRelease(int32_t releaseCode);
265     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
266     void OnReceive(const OHOS::AAFwk::WantParams& request);
267     void OnError(int32_t code, const std::string& name, const std::string& message);
268     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
269     void OnDestroy();
270     void SendMessageBack();
271 
272 private:
273     bool SetErrorCode(int32_t code);
274     int32_t sessionId_ = 0;
275     int32_t resultCode_ = 0;
276     OHOS::AAFwk::Want resultWant_;
277     std::shared_ptr<UIExtensionRequestContext> reqContext_ = nullptr;
278     bool alreadyCallback_ = false;
279 };
280 
281 void ThrowParamError(const napi_env env, const std::string& param, const std::string& type);
282 void DlpNapiThrow(napi_env env, int32_t nativeErrCode);
283 void DlpNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg);
284 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg);
285 bool NapiCheckArgc(const napi_env env, int32_t argc, int32_t reqSize);
286 
287 napi_value CreateEnumDLPFileAccess(napi_env env);
288 napi_value CreateEnumAccountType(napi_env env);
289 napi_value CreateEnumActionFlags(napi_env env);
290 napi_value CreateEnumGatheringPolicy(napi_env env);
291 
292 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data);
293 
294 bool GetGenerateDlpFileParams(
295     const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext);
296 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
297 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
298 
299 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
300 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
301 bool GetRecoverDlpFileParams(
302     const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext);
303 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext);
304 bool GetInstallDlpSandboxParams(
305     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
306 bool GetUninstallDlpSandboxParams(
307     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
308 bool GetThirdInterfaceParams(
309     const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext);
310 
311 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value* argv, const std::string& type,
312     const napi_value thisVar, RegisterDlpSandboxChangeInfo& registerSandboxChangeInfo);
313 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo,
314     RegisterDlpSandboxChangeInfo &registerSandboxChangeInfo);
315 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info,
316     UnregisterSandboxChangeCallbackAsyncContext &asyncContext);
317 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info,
318     RetentionStateAsyncContext& asyncContext);
319 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info,
320     GetRetentionSandboxListAsyncContext& asyncContext);
321 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info,
322     GetOriginalFileAsyncContext& asyncContext);
323 bool GetSandboxAppConfigParams(const napi_env env, const napi_callback_info info,
324     SandboxAppConfigAsyncContext* asyncContext);
325 void GetDlpPropertyExpireTime(napi_env env, napi_value jsObject, DlpProperty& property);
326 bool GetDlpProperty(napi_env env, napi_value object, DlpProperty& property);
327 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef);
328 
329 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key);
330 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result);
331 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result);
332 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result);
333 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result);
334 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result);
335 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result);
336 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result);
337 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result);
338 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key);
339 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec);
340 bool GetVectorAuthUserByKey(
341     napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec);
342 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key,
343     std::vector<std::string>& docUriVec);
344 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value);
345 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec);
346 
347 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec);
348 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec);
349 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property);
350 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users);
351 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value);
352 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value);
353 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo);
354 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo);
355 
356 bool ParseUIAbilityContextReq(
357     napi_env env, const napi_value& obj, std::shared_ptr<OHOS::AbilityRuntime::AbilityContext>& abilityContext);
358 bool ParseWantReq(napi_env env, const napi_value& obj, OHOS::AAFwk::Want& requestWant);
359 void StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext);
360 }  // namespace DlpPermission
361 }  // namespace Security
362 }  // namespace OHOS
363 #endif /*  INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H */
364