• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 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 "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "dlp_file.h"
26 #include "dlp_sandbox_callback_info.h"
27 #include "dlp_sandbox_change_callback_customize.h"
28 #include "open_dlp_file_callback_customize.h"
29 #include "permission_policy.h"
30 #include "retention_sandbox_info.h"
31 #include "visited_dlp_file_info.h"
32 
33 namespace OHOS {
34 namespace Security {
35 namespace DlpPermission {
36 constexpr int32_t PARAM0 = 0;
37 constexpr int32_t PARAM1 = 1;
38 constexpr int32_t PARAM2 = 2;
39 constexpr int32_t PARAM3 = 3;
40 constexpr int32_t PARAM4 = 4;
41 constexpr int32_t PARAM_SIZE_ONE = 1;
42 constexpr int32_t PARAM_SIZE_TWO = 2;
43 constexpr int32_t PARAM_SIZE_THREE = 3;
44 constexpr int32_t PARAM_SIZE_FOUR = 4;
45 constexpr int32_t PARAM_SIZE_FIVE = 5;
46 const std::string ON_OFF_SANDBOX = "uninstallDLPSandbox";
47 
48 #define NAPI_CALL_BASE_WITH_SCOPE(env, theCall, retVal, scope) \
49     do {                                                       \
50         if ((theCall) != napi_ok) {                            \
51             GET_AND_THROW_LAST_ERROR((env));                   \
52             napi_close_handle_scope(env, scope);               \
53             return retVal;                                     \
54         }                                                      \
55     } while (0)
56 
57 #define NAPI_CALL_RETURN_VOID_WITH_SCOPE(env, theCall, scope) \
58     NAPI_CALL_BASE_WITH_SCOPE(env, theCall, NAPI_RETVAL_NOTHING, scope)
59 
60 class RegisterDlpSandboxChangeScopePtr : public DlpSandboxChangeCallbackCustomize {
61 public:
62     RegisterDlpSandboxChangeScopePtr();
63     ~RegisterDlpSandboxChangeScopePtr() override;
64     void DlpSandboxChangeCallback(DlpSandboxCallbackInfo &result) override;
65     void SetEnv(const napi_env &env);
66     void SetCallbackRef(const napi_ref &ref);
67     void SetValid(bool valid);
68 
69 private:
70     napi_env env_ = nullptr;
71     napi_ref ref_ = nullptr;
72     bool valid_ = true;
73     std::mutex validMutex_;
74 };
75 
76 struct CommonAsyncContext {
77     explicit CommonAsyncContext(napi_env napiEnv);
78     virtual ~CommonAsyncContext();
79     napi_env env = nullptr;
80     napi_status status = napi_invalid_arg;
81     int32_t errCode = 0;
82     napi_deferred deferred = nullptr;  // promise handle
83     napi_ref callbackRef = nullptr;    // callback handle
84     napi_async_work work = nullptr;    // work handle
85 };
86 
87 struct RegisterDlpSandboxChangeWorker {
88     napi_env env = nullptr;
89     napi_ref ref = nullptr;
90     DlpSandboxCallbackInfo result;
91     RegisterDlpSandboxChangeScopePtr *subscriber = nullptr;
92 };
93 
94 struct DlpSandboxChangeContext {
95     virtual ~DlpSandboxChangeContext();
96     napi_env env = nullptr;
97     napi_ref callbackRef = nullptr;
98     int32_t errCode = 0;
99     std::string changeType;
100     std::shared_ptr<RegisterDlpSandboxChangeScopePtr> subscriber = nullptr;
101     void DeleteNapiRef();
102 };
103 
104 typedef DlpSandboxChangeContext RegisterDlpSandboxChangeInfo;
105 
106 struct UnregisterSandboxChangeCallbackAsyncContext : public CommonAsyncContext {
UnregisterSandboxChangeCallbackAsyncContextUnregisterSandboxChangeCallbackAsyncContext107     explicit UnregisterSandboxChangeCallbackAsyncContext(napi_env env) : CommonAsyncContext(env) {};
108     bool result = false;
109     std::string changeType;
110 };
111 class OpenDlpFileSubscriberPtr : public OpenDlpFileCallbackCustomize {
112 public:
113     OpenDlpFileSubscriberPtr();
114     ~OpenDlpFileSubscriberPtr() override;
115     void OnOpenDlpFile(OpenDlpFileCallbackInfo &result) override;
116     void SetEnv(const napi_env &env);
117     void SetCallbackRef(const napi_ref &ref);
118     void SetValid(bool valid);
119 
120 private:
121     napi_env env_ = nullptr;
122     napi_ref ref_ = nullptr;
123     bool valid_ = true;
124     std::mutex validMutex_;
125 };
126 
127 struct OpenDlpFileSubscriberWorker {
128     napi_env env = nullptr;
129     napi_ref ref = nullptr;
130     OpenDlpFileCallbackInfo result;
131     OpenDlpFileSubscriberPtr *subscriber = nullptr;
132 };
133 
134 struct OpenDlpFileSubscriberContext {
135     virtual ~OpenDlpFileSubscriberContext();
136     napi_env env = nullptr;
137     napi_ref callbackRef = nullptr;
138     int32_t errCode = 0;
139     std::shared_ptr<OpenDlpFileSubscriberPtr> subscriber = nullptr;
140     void DeleteNapiRef();
141 };
142 
143 struct OpenDlpFileUnSubscriberContext : public CommonAsyncContext {
OpenDlpFileUnSubscriberContextOpenDlpFileUnSubscriberContext144     explicit OpenDlpFileUnSubscriberContext(napi_env env) : CommonAsyncContext(env) {};
145     bool result = false;
146 };
147 
148 struct GenerateDlpFileAsyncContext : public CommonAsyncContext {
GenerateDlpFileAsyncContextGenerateDlpFileAsyncContext149     explicit GenerateDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
150     int64_t plaintextFd = -1;
151     int64_t ciphertextFd = -1;
152     DlpProperty property;
153     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
154 };
155 
156 struct DlpFileAsyncContext : public CommonAsyncContext {
DlpFileAsyncContextDlpFileAsyncContext157     explicit DlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
158     int64_t ciphertextFd = -1;
159     DlpProperty property;
160     bool isDlpFile = false;
161     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
162 };
163 
164 struct DlpLinkFileAsyncContext : public CommonAsyncContext {
DlpLinkFileAsyncContextDlpLinkFileAsyncContext165     explicit DlpLinkFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
166     std::string linkFileName = "";
167     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
168 };
169 
170 struct RecoverDlpFileAsyncContext : public CommonAsyncContext {
RecoverDlpFileAsyncContextRecoverDlpFileAsyncContext171     explicit RecoverDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
172     int64_t plaintextFd = -1;
173     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
174 };
175 
176 struct CloseDlpFileAsyncContext : public CommonAsyncContext {
CloseDlpFileAsyncContextCloseDlpFileAsyncContext177     explicit CloseDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
178     std::shared_ptr<DlpFile> dlpFileNative = nullptr;
179 };
180 
181 struct DlpSandboxAsyncContext : public CommonAsyncContext {
DlpSandboxAsyncContextDlpSandboxAsyncContext182     explicit DlpSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
183     std::string bundleName;
184     DLPFileAccess dlpFileAccess = NO_PERMISSION;
185     int32_t userId = -1;
186     SandboxInfo sandboxInfo;
187     std::string uri = "";
188 };
189 
190 struct GetPermInfoAsyncContext : public CommonAsyncContext {
GetPermInfoAsyncContextGetPermInfoAsyncContext191     explicit GetPermInfoAsyncContext(napi_env env) : CommonAsyncContext(env) {};
192     DLPPermissionInfo permInfo;
193 };
194 
195 struct IsInSandboxAsyncContext : public CommonAsyncContext {
IsInSandboxAsyncContextIsInSandboxAsyncContext196     explicit IsInSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {};
197     bool inSandbox = false;
198 };
199 
200 struct GetOriginalFileAsyncContext : public CommonAsyncContext {
GetOriginalFileAsyncContextGetOriginalFileAsyncContext201     explicit GetOriginalFileAsyncContext(napi_env env) : CommonAsyncContext(env) {};
202     std::string dlpFilename = "";
203     std::string oriFilename = "";
204 };
205 
206 struct GetSuffixAsyncContext : public CommonAsyncContext {
GetSuffixAsyncContextGetSuffixAsyncContext207     explicit GetSuffixAsyncContext(napi_env env) : CommonAsyncContext(env) {};
208     std::string extension = "";
209 };
210 
211 struct GetDlpSupportFileTypeAsyncContext : public CommonAsyncContext {
GetDlpSupportFileTypeAsyncContextGetDlpSupportFileTypeAsyncContext212     explicit GetDlpSupportFileTypeAsyncContext(napi_env env) : CommonAsyncContext(env) {};
213     std::vector<std::string> supportFileType;
214 };
215 
216 void UvQueueWorkDeleteRef(uv_work_t *work, int32_t status);
217 
218 struct GetGatheringPolicyContext : public CommonAsyncContext {
GetGatheringPolicyContextGetGatheringPolicyContext219     explicit GetGatheringPolicyContext(napi_env env) : CommonAsyncContext(env) {};
220     bool isGathering = false;
221 };
222 
223 struct RetentionStateAsyncContext : public CommonAsyncContext {
RetentionStateAsyncContextRetentionStateAsyncContext224     explicit RetentionStateAsyncContext(napi_env env) : CommonAsyncContext(env) {};
225     std::vector<std::string> docUris;
226 };
227 
228 struct GetRetentionSandboxListAsyncContext : public CommonAsyncContext {
GetRetentionSandboxListAsyncContextGetRetentionSandboxListAsyncContext229     explicit GetRetentionSandboxListAsyncContext(napi_env env) : CommonAsyncContext(env) {};
230     std::string bundleName = "";
231     std::vector<RetentionSandBoxInfo> retentionSandBoxInfoVec;
232 };
233 
234 struct GetDLPFileVisitRecordAsyncContext : public CommonAsyncContext {
GetDLPFileVisitRecordAsyncContextGetDLPFileVisitRecordAsyncContext235     explicit GetDLPFileVisitRecordAsyncContext(napi_env env) : CommonAsyncContext(env) {};
236     std::vector<VisitedDLPFileInfo> visitedDlpFileInfoVec;
237 };
238 
239 void ThrowParamError(const napi_env env, const std::string& param, const std::string& type);
240 void DlpNapiThrow(napi_env env, int32_t nativeErrCode);
241 void DlpNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg);
242 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg);
243 bool NapiCheckArgc(const napi_env env, int32_t argc, int32_t reqSize);
244 
245 napi_value CreateEnumDLPFileAccess(napi_env env);
246 napi_value CreateEnumAccountType(napi_env env);
247 napi_value CreateEnumActionFlags(napi_env env);
248 napi_value CreateEnumGatheringPolicy(napi_env env);
249 
250 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data);
251 
252 bool GetGenerateDlpFileParams(
253     const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext);
254 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
255 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext);
256 
257 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
258 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext);
259 bool GetRecoverDlpFileParams(
260     const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext);
261 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext);
262 bool GetInstallDlpSandboxParams(
263     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
264 bool GetUninstallDlpSandboxParams(
265     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext);
266 bool GetThirdInterfaceParams(
267     const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext);
268 
269 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value* argv, const std::string& type,
270     const napi_value thisVar, RegisterDlpSandboxChangeInfo& registerSandboxChangeInfo);
271 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo,
272     RegisterDlpSandboxChangeInfo &registerSandboxChangeInfo);
273 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info,
274     UnregisterSandboxChangeCallbackAsyncContext &asyncContext);
275 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info,
276     RetentionStateAsyncContext& asyncContext);
277 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info,
278     GetRetentionSandboxListAsyncContext& asyncContext);
279 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info,
280     GetOriginalFileAsyncContext& asyncContext);
281 bool GetDlpProperty(napi_env env, napi_value object, DlpProperty& property);
282 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef);
283 
284 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key);
285 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result);
286 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result);
287 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result);
288 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result);
289 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result);
290 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result);
291 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result);
292 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result);
293 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key);
294 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec);
295 bool GetVectorAuthUserByKey(
296     napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec);
297 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key,
298     std::vector<std::string>& docUriVec);
299 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value);
300 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec);
301 
302 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec);
303 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec);
304 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property);
305 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users);
306 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value);
307 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value);
308 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo);
309 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo);
310 }  // namespace DlpPermission
311 }  // namespace Security
312 }  // namespace OHOS
313 #endif /*  INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H */
314