• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class RegisterDlpSandboxChangeScopePtr : public DlpSandboxChangeCallbackCustomize {
52 public:
53     RegisterDlpSandboxChangeScopePtr();
54     ~RegisterDlpSandboxChangeScopePtr() override;
55     void DlpSandboxChangeCallback(DlpSandboxCallbackInfo &result) override;
56     void SetEnv(const napi_env &env);
57     void SetCallbackRef(const napi_ref &ref);
58     void SetValid(bool valid);
59 
60 private:
61     napi_env env_ = nullptr;
62     napi_ref ref_ = nullptr;
63     bool valid_ = true;
64     std::mutex validMutex_;
65 };
66 
67 struct CommonAsyncContext {
68     explicit CommonAsyncContext(napi_env napiEnv);
69     virtual ~CommonAsyncContext();
70     napi_env env = nullptr;
71     napi_status status = napi_invalid_arg;
72     int32_t errCode = 0;
73     napi_deferred deferred = nullptr;  // promise handle
74     napi_ref callbackRef = nullptr;    // callback handle
75     napi_async_work work = nullptr;    // work handle
76 };
77 
78 struct RegisterDlpSandboxChangeWorker {
79     napi_env env = nullptr;
80     napi_ref ref = nullptr;
81     DlpSandboxCallbackInfo result;
82     RegisterDlpSandboxChangeScopePtr *subscriber = nullptr;
83 };
84 
85 struct DlpSandboxChangeContext {
86     virtual ~DlpSandboxChangeContext();
87     napi_env env = nullptr;
88     napi_ref callbackRef = nullptr;
89     int32_t errCode = 0;
90     std::string changeType;
91     std::shared_ptr<RegisterDlpSandboxChangeScopePtr> subscriber = nullptr;
92     void DeleteNapiRef(napi_env env, napi_ref ref);
93 };
94 
95 typedef DlpSandboxChangeContext RegisterDlpSandboxChangeInfo;
96 
97 struct UnregisterSandboxChangeCallbackAsyncContext : public CommonAsyncContext {
UnregisterSandboxChangeCallbackAsyncContextUnregisterSandboxChangeCallbackAsyncContext98     explicit UnregisterSandboxChangeCallbackAsyncContext(napi_env env) : CommonAsyncContext(env) {};
99     bool result = false;
100     std::string changeType;
101 };
102 class OpenDlpFileSubscriberPtr : public OpenDlpFileCallbackCustomize {
103 public:
104     OpenDlpFileSubscriberPtr();
105     ~OpenDlpFileSubscriberPtr() override;
106     void OnOpenDlpFile(OpenDlpFileCallbackInfo &result) override;
107     void SetEnv(const napi_env &env);
108     void SetCallbackRef(const napi_ref &ref);
109     void SetValid(bool valid);
110 
111 private:
112     napi_env env_ = nullptr;
113     napi_ref ref_ = nullptr;
114     bool valid_ = true;
115     std::mutex validMutex_;
116 };
117 
118 struct OpenDlpFileSubscriberWorker {
119     napi_env env = nullptr;
120     napi_ref ref = nullptr;
121     OpenDlpFileCallbackInfo result;
122     OpenDlpFileSubscriberPtr *subscriber = nullptr;
123 };
124 
125 struct OpenDlpFileSubscriberContext {
126     virtual ~OpenDlpFileSubscriberContext();
127     napi_env env = nullptr;
128     napi_ref callbackRef = nullptr;
129     int32_t errCode = 0;
130     std::shared_ptr<OpenDlpFileSubscriberPtr> subscriber = nullptr;
131     void DeleteNapiRef(napi_env env, napi_ref ref);
132 };
133 
134 struct GenerateDlpFileAsyncContext : public CommonAsyncContext {
GenerateDlpFileAsyncContextGenerateDlpFileAsyncContext135     explicit GenerateDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
136     int64_t plaintextFd = -1;
137     int64_t ciphertextFd = -1;
138     DlpProperty property;
139     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
140 };
141 
142 struct DlpFileAsyncContext : public CommonAsyncContext {
DlpFileAsyncContextDlpFileAsyncContext143     explicit DlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
144     int64_t ciphertextFd = -1;
145     std::string appId;
146     DlpProperty property;
147     bool isDlpFile = false;
148     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
149 };
150 
151 struct DlpLinkFileAsyncContext : public CommonAsyncContext {
DlpLinkFileAsyncContextDlpLinkFileAsyncContext152     explicit DlpLinkFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
153     std::string linkFileName = "";
154     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
155 };
156 
157 struct RecoverDlpFileAsyncContext : public CommonAsyncContext {
RecoverDlpFileAsyncContextRecoverDlpFileAsyncContext158     explicit RecoverDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
159     int64_t plaintextFd = -1;
160     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
161 };
162 
163 struct CloseDlpFileAsyncContext : public CommonAsyncContext {
CloseDlpFileAsyncContextCloseDlpFileAsyncContext164     explicit CloseDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
165     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
166 };
167 
168 struct DlpSandboxAsyncContext : public CommonAsyncContext {
DlpSandboxAsyncContextDlpSandboxAsyncContext169     explicit DlpSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
170     std::string bundleName;
171     DLPFileAccess dlpFileAccess = DLPFileAccess::NO_PERMISSION;
172     int32_t userId = -1;
173     SandboxInfo sandboxInfo;
174     std::string uri = "";
175 };
176 
177 struct GetPermInfoAsyncContext : public CommonAsyncContext {
GetPermInfoAsyncContextGetPermInfoAsyncContext178     explicit GetPermInfoAsyncContext(napi_env env) : CommonAsyncContext(env) {};
179     DLPPermissionInfo permInfo;
180 };
181 
182 struct IsInSandboxAsyncContext : public CommonAsyncContext {
IsInSandboxAsyncContextIsInSandboxAsyncContext183     explicit IsInSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
184     bool inSandbox = false;
185 };
186 
187 struct IsDLPFeatureProvidedAsyncContext : public CommonAsyncContext {
IsDLPFeatureProvidedAsyncContextIsDLPFeatureProvidedAsyncContext188     explicit IsDLPFeatureProvidedAsyncContext(napi_env env) : CommonAsyncContext(env) {};
189     bool isProvideDLPFeature = false;
190 };
191 
192 struct GenerateDlpFileForEnterpriseAsyncContext : public CommonAsyncContext {
GenerateDlpFileForEnterpriseAsyncContextGenerateDlpFileForEnterpriseAsyncContext193     explicit GenerateDlpFileForEnterpriseAsyncContext(napi_env env) : CommonAsyncContext(env) {};
194     DlpProperty property;
195     CustomProperty customProperty;
196     int64_t plaintextFd = -1;
197     int64_t dlpFd = -1;
198 };
199 
200 struct DecryptDlpFileAsyncContext : public CommonAsyncContext {
DecryptDlpFileAsyncContextDecryptDlpFileAsyncContext201     explicit DecryptDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
202     int64_t dlpFd = -1;
203     int64_t plainFileFd = -1;
204 };
205 
206 struct QueryDlpPolicyAsyncContext : public CommonAsyncContext {
QueryDlpPolicyAsyncContextQueryDlpPolicyAsyncContext207     explicit QueryDlpPolicyAsyncContext(napi_env env) : CommonAsyncContext(env) {};
208     int64_t dlpFd = -1;
209     std::string policyJsonString = "";
210 };
211 
212 struct GetOriginalFileAsyncContext : public CommonAsyncContext {
GetOriginalFileAsyncContextGetOriginalFileAsyncContext213     explicit GetOriginalFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
214     std::string dlpFilename = "";
215     std::string oriFilename = "";
216 };
217 
218 struct GetSuffixAsyncContext : public CommonAsyncContext {
GetSuffixAsyncContextGetSuffixAsyncContext219     explicit GetSuffixAsyncContext(napi_env env) : CommonAsyncContext(env) {};
220     std::string extension = "";
221 };
222 
223 struct GetDlpSupportFileTypeAsyncContext : public CommonAsyncContext {
GetDlpSupportFileTypeAsyncContextGetDlpSupportFileTypeAsyncContext224     explicit GetDlpSupportFileTypeAsyncContext(napi_env env) : CommonAsyncContext(env) {};
225     std::vector<std::string> supportFileType;
226 };
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 napi_value CreateEnumActionType(napi_env env);
292 
293 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data);
294 
295 bool GetGenerateDlpFileParams(
296     const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext);
297 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
298 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
299 
300 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
301 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
302 bool GetRecoverDlpFileParams(
303     const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext);
304 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext);
305 bool GetInstallDlpSandboxParams(
306     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
307 bool GetUninstallDlpSandboxParams(
308     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
309 bool GetThirdInterfaceParams(
310     const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext);
311 
312 bool GetGenerateDlpFileForEnterpriseParam(
313     const napi_env env, const napi_callback_info info, GenerateDlpFileForEnterpriseAsyncContext& asyncContext);
314 bool GetDecryptDlpFileParam(
315     const napi_env env, const napi_callback_info info, DecryptDlpFileAsyncContext& asyncContext);
316 bool GetQueryDlpPolicyParam(
317     const napi_env env, const napi_callback_info info, QueryDlpPolicyAsyncContext& asyncContext);
318 
319 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value* argv, const std::string& type,
320     const napi_value thisVar, RegisterDlpSandboxChangeInfo& registerSandboxChangeInfo);
321 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo,
322     RegisterDlpSandboxChangeInfo &registerSandboxChangeInfo);
323 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info,
324     UnregisterSandboxChangeCallbackAsyncContext &asyncContext);
325 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info,
326     RetentionStateAsyncContext& asyncContext);
327 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info,
328     GetRetentionSandboxListAsyncContext& asyncContext);
329 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info,
330     GetOriginalFileAsyncContext& asyncContext);
331 bool GetSandboxAppConfigParams(const napi_env env, const napi_callback_info info,
332     SandboxAppConfigAsyncContext* asyncContext);
333 void GetDlpPropertyExpireTime(napi_env env, napi_value jsObject, DlpProperty& property);
334 bool GetDlpProperty(napi_env env, napi_value object, DlpProperty& property);
335 bool GetCustomProperty(napi_env env, napi_value object, CustomProperty& customProperty);
336 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef);
337 
338 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key);
339 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result);
340 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result);
341 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result);
342 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result);
343 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result);
344 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result);
345 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result);
346 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result);
347 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key);
348 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec);
349 bool GetVectorAuthUserByKey(
350     napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec);
351 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key,
352     std::vector<std::string>& docUriVec);
353 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value);
354 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec);
355 
356 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec);
357 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec);
358 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property);
359 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users);
360 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value);
361 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value);
362 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo);
363 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo);
364 
365 bool ParseUIAbilityContextReq(
366     napi_env env, const napi_value& obj, std::shared_ptr<OHOS::AbilityRuntime::AbilityContext>& abilityContext);
367 bool ParseWantReq(napi_env env, const napi_value& obj, OHOS::AAFwk::Want& requestWant);
368 void StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext);
369 
370 bool IsStringLengthValid(std::string str, size_t maxLen, size_t minLen = 0);
371 }  // namespace DlpPermission
372 }  // namespace Security
373 }  // namespace OHOS
374 #endif /*  INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H */
375