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 #include "napi_dlp_permission.h"
17 #include <functional>
18 #include <string>
19 #include "accesstoken_kit.h"
20 #include "application_context.h"
21 #include "dlp_fuse_helper.h"
22 #include "dlp_file_kits.h"
23 #include "dlp_permission.h"
24 #include "dlp_permission_log.h"
25 #include "dlp_permission_kit.h"
26 #include "dlp_file_manager.h"
27 #include "dlp_file_operator.h"
28 #include "ipc_skeleton.h"
29 #include "js_native_api_types.h"
30 #include "napi_error_msg.h"
31 #include "napi/native_api.h"
32 #include "napi/native_node_api.h"
33 #include "napi_common.h"
34 #include "permission_policy.h"
35 #include "securec.h"
36 #include "tokenid_kit.h"
37 #include "token_setproc.h"
38
39 namespace OHOS {
40 namespace Security {
41 namespace DlpPermission {
42 namespace {
43 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpPermissionNapi"};
44 std::mutex g_lockForOpenDlpFileSubscriber;
45 std::set<OpenDlpFileSubscriberContext*> g_openDlpFileSubscribers;
46 RegisterDlpSandboxChangeInfo *g_dlpSandboxChangeInfoRegister = nullptr;
47 const std::string PERMISSION_ACCESS_DLP_FILE = "ohos.permission.ACCESS_DLP_FILE";
48 const std::string PERMISSION_ENTERPRISE_ACCESS_DLP_FILE = "ohos.permission.ENTERPRISE_ACCESS_DLP_FILE";
49 static thread_local napi_ref dlpFileRef_;
50 const std::string DLP_FILE_CLASS_NAME = "dlpFile";
51 static constexpr size_t MAX_TYPE_LEN = 64;
52 } // namespace
53
CheckPermission(napi_env env,const std::string & permission)54 static bool CheckPermission(napi_env env, const std::string& permission)
55 {
56 Security::AccessToken::AccessTokenID selfToken = GetSelfTokenID();
57 int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(selfToken, permission);
58 if (res == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
59 DLP_LOG_INFO(LABEL, "Check permission %{public}s pass", permission.c_str());
60 return true;
61 }
62 DLP_LOG_ERROR(LABEL, "Check permission %{public}s fail", permission.c_str());
63 int32_t jsErrCode = ERR_JS_PERMISSION_DENIED;
64 NAPI_CALL_BASE(env, napi_throw(env, GenerateBusinessError(env, jsErrCode, GetJsErrMsg(jsErrCode))), false);
65 return false;
66 }
67
BindingJsWithNative(napi_env env,napi_value * argv,size_t argc)68 static napi_value BindingJsWithNative(napi_env env, napi_value* argv, size_t argc)
69 {
70 napi_value instance = nullptr;
71 napi_value constructor = nullptr;
72 if (napi_get_reference_value(env, dlpFileRef_, &constructor) != napi_ok) {
73 return nullptr;
74 }
75 DLP_LOG_DEBUG(LABEL, "Get a reference to the global variable dlpFileRef_ complete");
76 if (napi_new_instance(env, constructor, argc, argv, &instance) != napi_ok) {
77 return nullptr;
78 }
79 DLP_LOG_DEBUG(LABEL, "New the js instance complete");
80 return instance;
81 }
82
GenerateDlpFile(napi_env env,napi_callback_info cbInfo)83 napi_value NapiDlpPermission::GenerateDlpFile(napi_env env, napi_callback_info cbInfo)
84 {
85 if (!IsSystemApp(env)) {
86 return nullptr;
87 }
88 auto* asyncContext = new (std::nothrow) GenerateDlpFileAsyncContext(env);
89 if (asyncContext == nullptr) {
90 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
91 return nullptr;
92 }
93 std::unique_ptr<GenerateDlpFileAsyncContext> asyncContextPtr { asyncContext };
94
95 if (!GetGenerateDlpFileParams(env, cbInfo, *asyncContext)) {
96 return nullptr;
97 }
98
99 napi_value result = nullptr;
100 if (asyncContext->callbackRef == nullptr) {
101 DLP_LOG_DEBUG(LABEL, "Create promise");
102 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
103 } else {
104 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
105 NAPI_CALL(env, napi_get_undefined(env, &result));
106 }
107
108 napi_value resource = nullptr;
109 NAPI_CALL(env, napi_create_string_utf8(env, "GenerateDlpFile", NAPI_AUTO_LENGTH, &resource));
110 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GenerateDlpFileExcute, GenerateDlpFileComplete,
111 static_cast<void*>(asyncContext), &(asyncContext->work)));
112 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated));
113 asyncContextPtr.release();
114 return result;
115 }
116
GenerateDlpFileExcute(napi_env env,void * data)117 void NapiDlpPermission::GenerateDlpFileExcute(napi_env env, void* data)
118 {
119 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
120 auto asyncContext = reinterpret_cast<GenerateDlpFileAsyncContext*>(data);
121 if (asyncContext == nullptr) {
122 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
123 return;
124 }
125
126 auto context = AbilityRuntime::ApplicationContext::GetInstance();
127 if (context == nullptr) {
128 DLP_LOG_ERROR(LABEL, "get application context is nullptr");
129 return;
130 }
131
132 std::string workDir = context->GetFilesDir();
133 if (workDir.empty() || access(workDir.c_str(), 0) != 0) {
134 DLP_LOG_ERROR(LABEL, "path is null or workDir doesn't exist");
135 return;
136 }
137
138 char realPath[PATH_MAX] = {0};
139 if ((realpath(workDir.c_str(), realPath) == nullptr) && (errno != ENOENT)) {
140 DLP_LOG_ERROR(LABEL, "realpath, %{public}s, workDir %{private}s", strerror(errno), workDir.c_str());
141 return;
142 }
143 std::string rPath(realPath);
144
145 asyncContext->errCode = DlpFileManager::GetInstance().GenerateDlpFile(
146 asyncContext->plaintextFd, asyncContext->ciphertextFd, asyncContext->property,
147 asyncContext->dlpFileNative, rPath);
148 }
149
GenerateDlpFileComplete(napi_env env,napi_status status,void * data)150 void NapiDlpPermission::GenerateDlpFileComplete(napi_env env, napi_status status, void* data)
151 {
152 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
153 auto asyncContext = reinterpret_cast<GenerateDlpFileAsyncContext*>(data);
154 if (asyncContext == nullptr) {
155 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
156 return;
157 }
158 std::unique_ptr<GenerateDlpFileAsyncContext> asyncContextPtr { asyncContext };
159 napi_value resJs = nullptr;
160 if (asyncContext->errCode == DLP_OK) {
161 napi_value nativeObjJs;
162 NAPI_CALL_RETURN_VOID(
163 env, napi_create_int64(env, reinterpret_cast<int64_t>(asyncContext->dlpFileNative.get()), &nativeObjJs));
164
165 napi_value dlpPropertyJs = DlpPropertyToJs(env, asyncContext->property);
166 napi_value argv[PARAM_SIZE_TWO] = {nativeObjJs, dlpPropertyJs};
167 napi_value instance = BindingJsWithNative(env, argv, PARAM_SIZE_TWO);
168 if (instance == nullptr) {
169 DLP_LOG_ERROR(LABEL, "native instance binding fail");
170 asyncContext->errCode = DLP_NAPI_ERROR_NATIVE_BINDING_FAIL;
171 } else {
172 resJs = instance;
173 }
174 }
175
176 ProcessCallbackOrPromise(env, asyncContext, resJs);
177 }
178
OpenDlpFile(napi_env env,napi_callback_info cbInfo)179 napi_value NapiDlpPermission::OpenDlpFile(napi_env env, napi_callback_info cbInfo)
180 {
181 if (!IsSystemApp(env)) {
182 return nullptr;
183 }
184 auto* asyncContext = new (std::nothrow) DlpFileAsyncContext(env);
185 if (asyncContext == nullptr) {
186 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
187 return nullptr;
188 }
189 std::unique_ptr<DlpFileAsyncContext> asyncContextPtr { asyncContext };
190
191 if (!GetOpenDlpFileParams(env, cbInfo, *asyncContext)) {
192 return nullptr;
193 }
194
195 napi_value result = nullptr;
196 if (asyncContext->callbackRef == nullptr) {
197 DLP_LOG_DEBUG(LABEL, "Create promise");
198 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
199 } else {
200 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
201 NAPI_CALL(env, napi_get_undefined(env, &result));
202 }
203
204 napi_value resource = nullptr;
205 NAPI_CALL(env, napi_create_string_utf8(env, "OpenDlpFile", NAPI_AUTO_LENGTH, &resource));
206 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, OpenDlpFileExcute, OpenDlpFileComplete,
207 static_cast<void*>(asyncContext), &(asyncContext->work)));
208 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContext->work, napi_qos_user_initiated));
209 asyncContextPtr.release();
210 return result;
211 }
212
OpenDlpFileExcute(napi_env env,void * data)213 void NapiDlpPermission::OpenDlpFileExcute(napi_env env, void* data)
214 {
215 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
216 auto asyncContext = reinterpret_cast<DlpFileAsyncContext*>(data);
217 if (asyncContext == nullptr) {
218 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
219 return;
220 }
221
222 auto context = AbilityRuntime::ApplicationContext::GetInstance();
223 if (context == nullptr) {
224 DLP_LOG_ERROR(LABEL, "get applicationContext fail");
225 return;
226 }
227
228 std::string workDir = context->GetFilesDir();
229 if (workDir.empty() || access(workDir.c_str(), 0) != 0) {
230 DLP_LOG_ERROR(LABEL, "path is null or workDir doesn't exist");
231 return;
232 }
233
234 char realPath[PATH_MAX] = {0};
235 if (realpath(workDir.c_str(), realPath) == nullptr) {
236 DLP_LOG_ERROR(LABEL, "realpath, %{public}s, workDir %{private}s", strerror(errno), workDir.c_str());
237 return;
238 }
239 std::string rPath(realPath);
240 asyncContext->errCode =
241 DlpFileManager::GetInstance().OpenDlpFile(asyncContext->ciphertextFd, asyncContext->dlpFileNative, rPath,
242 asyncContext->appId);
243 }
244
GetDlpProperty(std::shared_ptr<DlpFile> & dlpFileNative,DlpProperty & property)245 static void GetDlpProperty(std::shared_ptr<DlpFile>& dlpFileNative, DlpProperty& property)
246 {
247 PermissionPolicy policy;
248 dlpFileNative->GetPolicy(policy);
249 std::string contactAccount;
250 dlpFileNative->GetContactAccount(contactAccount);
251 property = {
252 .ownerAccount = policy.ownerAccount_,
253 .ownerAccountId = policy.ownerAccountId_,
254 .authUsers = policy.authUsers_,
255 .contactAccount = contactAccount,
256 .ownerAccountType = policy.ownerAccountType_,
257 .offlineAccess = dlpFileNative->GetOfflineAccess(),
258 .supportEveryone = policy.supportEveryone_,
259 .everyonePerm = policy.everyonePerm_,
260 .expireTime = policy.expireTime_,
261 };
262 }
263
OpenDlpFileComplete(napi_env env,napi_status status,void * data)264 void NapiDlpPermission::OpenDlpFileComplete(napi_env env, napi_status status, void* data)
265 {
266 auto asyncContext = reinterpret_cast<DlpFileAsyncContext*>(data);
267 if (asyncContext == nullptr) {
268 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
269 return;
270 }
271 std::unique_ptr<DlpFileAsyncContext> asyncContextPtr { asyncContext };
272 napi_value resJs = nullptr;
273 if (asyncContext->errCode == DLP_OK) {
274 napi_value nativeObjJs;
275 if (asyncContext->dlpFileNative == nullptr) {
276 DLP_LOG_ERROR(LABEL, "asyncContext dlpFileNative is nullptr");
277 return;
278 }
279 NAPI_CALL_RETURN_VOID(
280 env, napi_create_int64(env, reinterpret_cast<int64_t>(asyncContext->dlpFileNative.get()), &nativeObjJs));
281 DlpProperty property;
282 GetDlpProperty(asyncContext->dlpFileNative, property);
283 napi_value dlpPropertyJs = DlpPropertyToJs(env, property);
284 napi_value argv[PARAM_SIZE_TWO] = {nativeObjJs, dlpPropertyJs};
285 napi_value instance = BindingJsWithNative(env, argv, PARAM_SIZE_TWO);
286 if (instance == nullptr) {
287 asyncContext->errCode = DLP_NAPI_ERROR_NATIVE_BINDING_FAIL;
288 } else {
289 resJs = instance;
290 }
291 } else {
292 if (asyncContext->dlpFileNative != nullptr &&
293 (asyncContext->errCode == DLP_CREDENTIAL_ERROR_NO_PERMISSION_ERROR ||
294 asyncContext->errCode == DLP_CREDENTIAL_ERROR_TIME_EXPIRED)) {
295 std::string contactAccount = "";
296 asyncContext->dlpFileNative->GetContactAccount(contactAccount);
297 if (!contactAccount.empty()) {
298 NAPI_CALL_RETURN_VOID(
299 env, napi_create_string_utf8(env, contactAccount.c_str(), NAPI_AUTO_LENGTH, &resJs));
300 }
301 }
302 }
303 ProcessCallbackOrPromise(env, asyncContext, resJs);
304 }
305
IsDlpFile(napi_env env,napi_callback_info cbInfo)306 napi_value NapiDlpPermission::IsDlpFile(napi_env env, napi_callback_info cbInfo)
307 {
308 auto* asyncContext = new (std::nothrow) DlpFileAsyncContext(env);
309 if (asyncContext == nullptr) {
310 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
311 return nullptr;
312 }
313 std::unique_ptr<DlpFileAsyncContext> asyncContextPtr { asyncContext };
314
315 if (!GetIsDlpFileParams(env, cbInfo, *asyncContext)) {
316 return nullptr;
317 }
318
319 napi_value result = nullptr;
320 if (asyncContext->callbackRef == nullptr) {
321 DLP_LOG_DEBUG(LABEL, "Create promise");
322 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
323 } else {
324 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
325 NAPI_CALL(env, napi_get_undefined(env, &result));
326 }
327
328 napi_value resource = nullptr;
329 NAPI_CALL(env, napi_create_string_utf8(env, "IsDlpFile", NAPI_AUTO_LENGTH, &resource));
330 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, IsDlpFileExcute, IsDlpFileComplete,
331 static_cast<void*>(asyncContext), &(asyncContext->work)));
332 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
333 asyncContextPtr.release();
334 return result;
335 }
336
IsDlpFileExcute(napi_env env,void * data)337 void NapiDlpPermission::IsDlpFileExcute(napi_env env, void* data)
338 {
339 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
340 auto asyncContext = reinterpret_cast<DlpFileAsyncContext*>(data);
341 if (asyncContext == nullptr) {
342 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
343 return;
344 }
345
346 asyncContext->isDlpFile = DlpFileKits::IsDlpFile(asyncContext->ciphertextFd);
347 }
348
IsDlpFileComplete(napi_env env,napi_status status,void * data)349 void NapiDlpPermission::IsDlpFileComplete(napi_env env, napi_status status, void* data)
350 {
351 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
352 auto asyncContext = reinterpret_cast<DlpFileAsyncContext*>(data);
353 if (asyncContext == nullptr) {
354 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
355 return;
356 }
357 std::unique_ptr<DlpFileAsyncContext> asyncContextPtr { asyncContext };
358
359 napi_value isDlpFileJs = nullptr;
360 if (asyncContext->errCode == DLP_OK) {
361 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, asyncContext->isDlpFile, &isDlpFileJs));
362 }
363
364 ProcessCallbackOrPromise(env, asyncContext, isDlpFileJs);
365 }
366
AddDlpLinkFile(napi_env env,napi_callback_info cbInfo)367 napi_value NapiDlpPermission::AddDlpLinkFile(napi_env env, napi_callback_info cbInfo)
368 {
369 if (!IsSystemApp(env)) {
370 return nullptr;
371 }
372 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
373 return nullptr;
374 }
375 auto* asyncContext = new (std::nothrow) DlpLinkFileAsyncContext(env);
376 if (asyncContext == nullptr) {
377 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
378 return nullptr;
379 }
380 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
381
382 if (!GetDlpLinkFileParams(env, cbInfo, *asyncContext)) {
383 return nullptr;
384 }
385
386 napi_value result = nullptr;
387 if (asyncContext->callbackRef == nullptr) {
388 DLP_LOG_DEBUG(LABEL, "Create promise");
389 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
390 } else {
391 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
392 NAPI_CALL(env, napi_get_undefined(env, &result));
393 }
394
395 napi_value resource = nullptr;
396 NAPI_CALL(env, napi_create_string_utf8(env, "AddDlpLinkFile", NAPI_AUTO_LENGTH, &resource));
397 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, AddDlpLinkFileExcute, AddDlpLinkFileComplete,
398 static_cast<void*>(asyncContext), &(asyncContext->work)));
399 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
400 asyncContextPtr.release();
401 return result;
402 }
403
AddDlpLinkFileExcute(napi_env env,void * data)404 void NapiDlpPermission::AddDlpLinkFileExcute(napi_env env, void* data)
405 {
406 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
407 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
408 if (asyncContext == nullptr) {
409 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
410 return;
411 }
412 DlpLinkManager* manager = DlpFuseHelper::GetDlpLinkManagerInstance();
413 if (!manager) {
414 DLP_LOG_ERROR(LABEL, "Get instance failed.");
415 return;
416 }
417 asyncContext->errCode = manager->AddDlpLinkFile(asyncContext->dlpFileNative, asyncContext->linkFileName);
418 }
419
AddDlpLinkFileComplete(napi_env env,napi_status status,void * data)420 void NapiDlpPermission::AddDlpLinkFileComplete(napi_env env, napi_status status, void* data)
421 {
422 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
423 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
424 if (asyncContext == nullptr) {
425 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
426 return;
427 }
428 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
429 napi_value resJs = nullptr;
430 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
431 ProcessCallbackOrPromise(env, asyncContext, resJs);
432 }
433
StopDlpLinkFile(napi_env env,napi_callback_info cbInfo)434 napi_value NapiDlpPermission::StopDlpLinkFile(napi_env env, napi_callback_info cbInfo)
435 {
436 if (!IsSystemApp(env)) {
437 return nullptr;
438 }
439 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
440 return nullptr;
441 }
442 auto* asyncContext = new (std::nothrow) DlpLinkFileAsyncContext(env);
443 if (asyncContext == nullptr) {
444 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
445 return nullptr;
446 }
447 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
448
449 if (!GetLinkFileStatusParams(env, cbInfo, *asyncContext)) {
450 return nullptr;
451 }
452
453 napi_value result = nullptr;
454 if (asyncContext->callbackRef == nullptr) {
455 DLP_LOG_DEBUG(LABEL, "Create promise");
456 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
457 } else {
458 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
459 NAPI_CALL(env, napi_get_undefined(env, &result));
460 }
461
462 napi_value resource = nullptr;
463 NAPI_CALL(env, napi_create_string_utf8(env, "StopDlpLinkFile", NAPI_AUTO_LENGTH, &resource));
464 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, StopDlpLinkFileExcute, StopDlpLinkFileComplete,
465 static_cast<void*>(asyncContext), &(asyncContext->work)));
466 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
467 asyncContextPtr.release();
468 return result;
469 }
470
StopDlpLinkFileExcute(napi_env env,void * data)471 void NapiDlpPermission::StopDlpLinkFileExcute(napi_env env, void* data)
472 {
473 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
474 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
475 if (asyncContext == nullptr) {
476 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
477 return;
478 }
479 DlpLinkManager* manager = DlpFuseHelper::GetDlpLinkManagerInstance();
480 if (!manager) {
481 DLP_LOG_ERROR(LABEL, "Get instance failed.");
482 return;
483 }
484 asyncContext->errCode = manager->StopDlpLinkFile(asyncContext->dlpFileNative);
485 }
486
StopDlpLinkFileComplete(napi_env env,napi_status status,void * data)487 void NapiDlpPermission::StopDlpLinkFileComplete(napi_env env, napi_status status, void* data)
488 {
489 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
490 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
491 if (asyncContext == nullptr) {
492 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
493 return;
494 }
495 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
496 napi_value resJs = nullptr;
497 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
498 ProcessCallbackOrPromise(env, asyncContext, resJs);
499 }
500
RestartDlpLinkFile(napi_env env,napi_callback_info cbInfo)501 napi_value NapiDlpPermission::RestartDlpLinkFile(napi_env env, napi_callback_info cbInfo)
502 {
503 if (!IsSystemApp(env)) {
504 return nullptr;
505 }
506 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
507 return nullptr;
508 }
509 auto* asyncContext = new (std::nothrow) DlpLinkFileAsyncContext(env);
510 if (asyncContext == nullptr) {
511 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
512 return nullptr;
513 }
514 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
515
516 if (!GetLinkFileStatusParams(env, cbInfo, *asyncContext)) {
517 return nullptr;
518 }
519
520 napi_value result = nullptr;
521 if (asyncContext->callbackRef == nullptr) {
522 DLP_LOG_DEBUG(LABEL, "Create promise");
523 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
524 } else {
525 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
526 NAPI_CALL(env, napi_get_undefined(env, &result));
527 }
528
529 napi_value resource = nullptr;
530 NAPI_CALL(env, napi_create_string_utf8(env, "RestartDlpLinkFile", NAPI_AUTO_LENGTH, &resource));
531 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, RestartDlpLinkFileExcute, RestartDlpLinkFileComplete,
532 static_cast<void*>(asyncContext), &(asyncContext->work)));
533 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
534 asyncContextPtr.release();
535 return result;
536 }
537
RestartDlpLinkFileExcute(napi_env env,void * data)538 void NapiDlpPermission::RestartDlpLinkFileExcute(napi_env env, void* data)
539 {
540 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
541 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
542 if (asyncContext == nullptr) {
543 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
544 return;
545 }
546 DlpLinkManager* manager = DlpFuseHelper::GetDlpLinkManagerInstance();
547 if (!manager) {
548 DLP_LOG_ERROR(LABEL, "Get instance failed.");
549 return;
550 }
551 asyncContext->errCode = manager->RestartDlpLinkFile(asyncContext->dlpFileNative);
552 }
553
RestartDlpLinkFileComplete(napi_env env,napi_status status,void * data)554 void NapiDlpPermission::RestartDlpLinkFileComplete(napi_env env, napi_status status, void* data)
555 {
556 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
557 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
558 if (asyncContext == nullptr) {
559 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
560 return;
561 }
562 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
563 napi_value resJs = nullptr;
564 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
565 ProcessCallbackOrPromise(env, asyncContext, resJs);
566 }
567
ReplaceDlpLinkFile(napi_env env,napi_callback_info cbInfo)568 napi_value NapiDlpPermission::ReplaceDlpLinkFile(napi_env env, napi_callback_info cbInfo)
569 {
570 if (!IsSystemApp(env)) {
571 return nullptr;
572 }
573 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
574 return nullptr;
575 }
576 auto* asyncContext = new (std::nothrow) DlpLinkFileAsyncContext(env);
577 if (asyncContext == nullptr) {
578 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
579 return nullptr;
580 }
581 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
582
583 if (!GetDlpLinkFileParams(env, cbInfo, *asyncContext)) {
584 return nullptr;
585 }
586
587 napi_value result = nullptr;
588 if (asyncContext->callbackRef == nullptr) {
589 DLP_LOG_DEBUG(LABEL, "Create promise");
590 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
591 } else {
592 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
593 NAPI_CALL(env, napi_get_undefined(env, &result));
594 }
595
596 napi_value resource = nullptr;
597 NAPI_CALL(env, napi_create_string_utf8(env, "ReplaceDlpLinkFile", NAPI_AUTO_LENGTH, &resource));
598 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, ReplaceDlpLinkFileExcute, ReplaceDlpLinkFileComplete,
599 static_cast<void*>(asyncContext), &(asyncContext->work)));
600 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
601 asyncContextPtr.release();
602 return result;
603 }
604
ReplaceDlpLinkFileExcute(napi_env env,void * data)605 void NapiDlpPermission::ReplaceDlpLinkFileExcute(napi_env env, void* data)
606 {
607 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
608 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
609 if (asyncContext == nullptr) {
610 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
611 return;
612 }
613 DlpLinkManager* manager = DlpFuseHelper::GetDlpLinkManagerInstance();
614 if (!manager) {
615 DLP_LOG_ERROR(LABEL, "Get instance failed.");
616 return;
617 }
618 asyncContext->errCode = manager->ReplaceDlpLinkFile(asyncContext->dlpFileNative, asyncContext->linkFileName);
619 }
620
ReplaceDlpLinkFileComplete(napi_env env,napi_status status,void * data)621 void NapiDlpPermission::ReplaceDlpLinkFileComplete(napi_env env, napi_status status, void* data)
622 {
623 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
624 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
625 if (asyncContext == nullptr) {
626 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
627 return;
628 }
629 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
630 napi_value resJs = nullptr;
631 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
632 ProcessCallbackOrPromise(env, asyncContext, resJs);
633 }
634
DeleteDlpLinkFile(napi_env env,napi_callback_info cbInfo)635 napi_value NapiDlpPermission::DeleteDlpLinkFile(napi_env env, napi_callback_info cbInfo)
636 {
637 if (!IsSystemApp(env)) {
638 return nullptr;
639 }
640 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
641 return nullptr;
642 }
643 auto* asyncContext = new (std::nothrow) DlpLinkFileAsyncContext(env);
644 if (asyncContext == nullptr) {
645 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
646 return nullptr;
647 }
648 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
649
650 if (!GetDlpLinkFileParams(env, cbInfo, *asyncContext)) {
651 return nullptr;
652 }
653
654 napi_value result = nullptr;
655 if (asyncContext->callbackRef == nullptr) {
656 DLP_LOG_DEBUG(LABEL, "Create promise");
657 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
658 } else {
659 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
660 NAPI_CALL(env, napi_get_undefined(env, &result));
661 }
662
663 napi_value resource = nullptr;
664 NAPI_CALL(env, napi_create_string_utf8(env, "DeleteDlpLinkFile", NAPI_AUTO_LENGTH, &resource));
665 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, DeleteDlpLinkFileExcute, DeleteDlpLinkFileComplete,
666 static_cast<void*>(asyncContext), &(asyncContext->work)));
667 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
668 asyncContextPtr.release();
669 return result;
670 }
671
DeleteDlpLinkFileExcute(napi_env env,void * data)672 void NapiDlpPermission::DeleteDlpLinkFileExcute(napi_env env, void* data)
673 {
674 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
675 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
676 if (asyncContext == nullptr) {
677 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
678 return;
679 }
680 DlpLinkManager* manager = DlpFuseHelper::GetDlpLinkManagerInstance();
681 if (!manager) {
682 DLP_LOG_ERROR(LABEL, "Get instance failed.");
683 return;
684 }
685 asyncContext->errCode = manager->DeleteDlpLinkFile(asyncContext->dlpFileNative);
686 }
687
DeleteDlpLinkFileComplete(napi_env env,napi_status status,void * data)688 void NapiDlpPermission::DeleteDlpLinkFileComplete(napi_env env, napi_status status, void* data)
689 {
690 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
691 auto asyncContext = reinterpret_cast<DlpLinkFileAsyncContext*>(data);
692 if (asyncContext == nullptr) {
693 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
694 return;
695 }
696 std::unique_ptr<DlpLinkFileAsyncContext> asyncContextPtr { asyncContext };
697 napi_value resJs = nullptr;
698 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
699 ProcessCallbackOrPromise(env, asyncContext, resJs);
700 }
701
RecoverDlpFile(napi_env env,napi_callback_info cbInfo)702 napi_value NapiDlpPermission::RecoverDlpFile(napi_env env, napi_callback_info cbInfo)
703 {
704 if (!IsSystemApp(env)) {
705 return nullptr;
706 }
707 if (!CheckPermission(env, PERMISSION_ACCESS_DLP_FILE)) {
708 return nullptr;
709 }
710 auto* asyncContext = new (std::nothrow) RecoverDlpFileAsyncContext(env);
711 if (asyncContext == nullptr) {
712 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
713 return nullptr;
714 }
715 std::unique_ptr<RecoverDlpFileAsyncContext> asyncContextPtr { asyncContext };
716
717 if (!GetRecoverDlpFileParams(env, cbInfo, *asyncContext)) {
718 return nullptr;
719 }
720
721 napi_value result = nullptr;
722 if (asyncContext->callbackRef == nullptr) {
723 DLP_LOG_DEBUG(LABEL, "Create promise");
724 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
725 } else {
726 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
727 NAPI_CALL(env, napi_get_undefined(env, &result));
728 }
729
730 napi_value resource = nullptr;
731 NAPI_CALL(env, napi_create_string_utf8(env, "RecoverDlpFile", NAPI_AUTO_LENGTH, &resource));
732 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, RecoverDlpFileExcute, RecoverDlpFileComplete,
733 static_cast<void*>(asyncContext), &(asyncContext->work)));
734 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
735 asyncContextPtr.release();
736 return result;
737 }
738
RecoverDlpFileExcute(napi_env env,void * data)739 void NapiDlpPermission::RecoverDlpFileExcute(napi_env env, void* data)
740 {
741 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
742 auto asyncContext = reinterpret_cast<RecoverDlpFileAsyncContext*>(data);
743 if (asyncContext == nullptr) {
744 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
745 return;
746 }
747
748 asyncContext->errCode =
749 DlpFileManager::GetInstance().RecoverDlpFile(asyncContext->dlpFileNative, asyncContext->plaintextFd);
750 }
751
RecoverDlpFileComplete(napi_env env,napi_status status,void * data)752 void NapiDlpPermission::RecoverDlpFileComplete(napi_env env, napi_status status, void* data)
753 {
754 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
755 auto asyncContext = reinterpret_cast<RecoverDlpFileAsyncContext*>(data);
756 if (asyncContext == nullptr) {
757 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
758 return;
759 }
760 std::unique_ptr<RecoverDlpFileAsyncContext> asyncContextPtr { asyncContext };
761 napi_value resJs = nullptr;
762 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
763 ProcessCallbackOrPromise(env, asyncContext, resJs);
764 }
765
CloseDlpFile(napi_env env,napi_callback_info cbInfo)766 napi_value NapiDlpPermission::CloseDlpFile(napi_env env, napi_callback_info cbInfo)
767 {
768 if (!IsSystemApp(env)) {
769 return nullptr;
770 }
771 auto* asyncContext = new (std::nothrow) CloseDlpFileAsyncContext(env);
772 if (asyncContext == nullptr) {
773 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
774 return nullptr;
775 }
776 std::unique_ptr<CloseDlpFileAsyncContext> asyncContextPtr { asyncContext };
777
778 if (!GetCloseDlpFileParams(env, cbInfo, *asyncContext)) {
779 return nullptr;
780 }
781
782 napi_value result = nullptr;
783 if (asyncContext->callbackRef == nullptr) {
784 DLP_LOG_DEBUG(LABEL, "Create promise");
785 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
786 } else {
787 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
788 NAPI_CALL(env, napi_get_undefined(env, &result));
789 }
790
791 napi_value resource = nullptr;
792 NAPI_CALL(env, napi_create_string_utf8(env, "CloseDlpFile", NAPI_AUTO_LENGTH, &resource));
793 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, CloseDlpFileExcute, CloseDlpFileComplete,
794 static_cast<void*>(asyncContext), &(asyncContext->work)));
795 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
796 asyncContextPtr.release();
797 return result;
798 }
799
CloseDlpFileExcute(napi_env env,void * data)800 void NapiDlpPermission::CloseDlpFileExcute(napi_env env, void* data)
801 {
802 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
803 auto asyncContext = reinterpret_cast<CloseDlpFileAsyncContext*>(data);
804 if (asyncContext == nullptr) {
805 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
806 return;
807 }
808
809 asyncContext->errCode = DlpFileManager::GetInstance().CloseDlpFile(asyncContext->dlpFileNative);
810 }
811
CloseDlpFileComplete(napi_env env,napi_status status,void * data)812 void NapiDlpPermission::CloseDlpFileComplete(napi_env env, napi_status status, void* data)
813 {
814 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
815 auto asyncContext = reinterpret_cast<CloseDlpFileAsyncContext*>(data);
816 if (asyncContext == nullptr) {
817 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
818 return;
819 }
820 std::unique_ptr<CloseDlpFileAsyncContext> asyncContextPtr { asyncContext };
821 napi_value resJs = nullptr;
822 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
823 ProcessCallbackOrPromise(env, asyncContext, resJs);
824 }
825
InstallDlpSandbox(napi_env env,napi_callback_info cbInfo)826 napi_value NapiDlpPermission::InstallDlpSandbox(napi_env env, napi_callback_info cbInfo)
827 {
828 if (!IsSystemApp(env)) {
829 return nullptr;
830 }
831
832 auto* asyncContext = new (std::nothrow) DlpSandboxAsyncContext(env);
833 if (asyncContext == nullptr) {
834 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
835 return nullptr;
836 }
837 std::unique_ptr<DlpSandboxAsyncContext> asyncContextPtr { asyncContext };
838
839 if (!GetInstallDlpSandboxParams(env, cbInfo, *asyncContext)) {
840 return nullptr;
841 }
842
843 napi_value result = nullptr;
844 if (asyncContext->callbackRef == nullptr) {
845 DLP_LOG_DEBUG(LABEL, "Create promise");
846 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
847 } else {
848 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
849 NAPI_CALL(env, napi_get_undefined(env, &result));
850 }
851
852 napi_value resource = nullptr;
853 NAPI_CALL(env, napi_create_string_utf8(env, "InstallDlpSandbox", NAPI_AUTO_LENGTH, &resource));
854 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, InstallDlpSandboxExcute, InstallDlpSandboxComplete,
855 static_cast<void*>(asyncContext), &(asyncContext->work)));
856 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
857 asyncContextPtr.release();
858 return result;
859 }
860
InstallDlpSandboxExcute(napi_env env,void * data)861 void NapiDlpPermission::InstallDlpSandboxExcute(napi_env env, void* data)
862 {
863 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
864 auto asyncContext = reinterpret_cast<DlpSandboxAsyncContext*>(data);
865 if (asyncContext == nullptr) {
866 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
867 return;
868 }
869
870 asyncContext->errCode = DlpPermissionKit::InstallDlpSandbox(asyncContext->bundleName, asyncContext->dlpFileAccess,
871 asyncContext->userId, asyncContext->sandboxInfo, asyncContext->uri);
872 }
873
InstallDlpSandboxComplete(napi_env env,napi_status status,void * data)874 void NapiDlpPermission::InstallDlpSandboxComplete(napi_env env, napi_status status, void* data)
875 {
876 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
877 auto asyncContext = reinterpret_cast<DlpSandboxAsyncContext*>(data);
878 if (asyncContext == nullptr) {
879 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
880 return;
881 }
882 std::unique_ptr<DlpSandboxAsyncContext> asyncContextPtr { asyncContext };
883 napi_value sandboxInfoJs = nullptr;
884 if (asyncContext->errCode == DLP_OK) {
885 sandboxInfoJs = SandboxInfoToJs(env, asyncContext->sandboxInfo);
886 }
887 ProcessCallbackOrPromise(env, asyncContext, sandboxInfoJs);
888 }
889
UninstallDlpSandbox(napi_env env,napi_callback_info cbInfo)890 napi_value NapiDlpPermission::UninstallDlpSandbox(napi_env env, napi_callback_info cbInfo)
891 {
892 if (!IsSystemApp(env)) {
893 return nullptr;
894 }
895
896 auto* asyncContext = new (std::nothrow) DlpSandboxAsyncContext(env);
897 if (asyncContext == nullptr) {
898 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
899 return nullptr;
900 }
901 std::unique_ptr<DlpSandboxAsyncContext> asyncContextPtr { asyncContext };
902
903 if (!GetUninstallDlpSandboxParams(env, cbInfo, *asyncContext)) {
904 return nullptr;
905 }
906
907 napi_value result = nullptr;
908 if (asyncContext->callbackRef == nullptr) {
909 DLP_LOG_DEBUG(LABEL, "Create promise");
910 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
911 } else {
912 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
913 NAPI_CALL(env, napi_get_undefined(env, &result));
914 }
915
916 napi_value resource = nullptr;
917 NAPI_CALL(env, napi_create_string_utf8(env, "UninstallDlpSandbox", NAPI_AUTO_LENGTH, &resource));
918 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, UninstallDlpSandboxExcute,
919 UninstallDlpSandboxComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
920 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
921 asyncContextPtr.release();
922 return result;
923 }
924
UninstallDlpSandboxExcute(napi_env env,void * data)925 void NapiDlpPermission::UninstallDlpSandboxExcute(napi_env env, void* data)
926 {
927 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
928 auto asyncContext = reinterpret_cast<DlpSandboxAsyncContext*>(data);
929 if (asyncContext == nullptr) {
930 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
931 return;
932 }
933
934 asyncContext->errCode = DlpPermissionKit::UninstallDlpSandbox(
935 asyncContext->bundleName, asyncContext->sandboxInfo.appIndex, asyncContext->userId);
936 }
937
UninstallDlpSandboxComplete(napi_env env,napi_status status,void * data)938 void NapiDlpPermission::UninstallDlpSandboxComplete(napi_env env, napi_status status, void* data)
939 {
940 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
941 auto asyncContext = reinterpret_cast<DlpSandboxAsyncContext*>(data);
942 if (asyncContext == nullptr) {
943 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
944 return;
945 }
946 std::unique_ptr<DlpSandboxAsyncContext> asyncContextPtr { asyncContext };
947 napi_value resJs = nullptr;
948 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
949 ProcessCallbackOrPromise(env, asyncContext, resJs);
950 }
951
GetDLPPermissionInfo(napi_env env,napi_callback_info cbInfo)952 napi_value NapiDlpPermission::GetDLPPermissionInfo(napi_env env, napi_callback_info cbInfo)
953 {
954 auto* asyncContext = new (std::nothrow) GetPermInfoAsyncContext(env);
955 if (asyncContext == nullptr) {
956 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
957 return nullptr;
958 }
959 std::unique_ptr<GetPermInfoAsyncContext> asyncContextPtr { asyncContext };
960
961 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContext)) {
962 return nullptr;
963 }
964
965 napi_value result = nullptr;
966 if (asyncContext->callbackRef == nullptr) {
967 DLP_LOG_DEBUG(LABEL, "Create promise");
968 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
969 } else {
970 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
971 NAPI_CALL(env, napi_get_undefined(env, &result));
972 }
973
974 napi_value resource = nullptr;
975 NAPI_CALL(env, napi_create_string_utf8(env, "GetDLPPermissionInfo", NAPI_AUTO_LENGTH, &resource));
976 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetDLPPermissionInfoExcute,
977 GetDLPPermissionInfoComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
978 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
979 asyncContextPtr.release();
980 return result;
981 }
982
GetDLPPermissionInfoExcute(napi_env env,void * data)983 void NapiDlpPermission::GetDLPPermissionInfoExcute(napi_env env, void* data)
984 {
985 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
986 auto asyncContext = reinterpret_cast<GetPermInfoAsyncContext*>(data);
987 if (asyncContext == nullptr) {
988 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
989 return;
990 }
991
992 asyncContext->errCode = DlpPermissionKit::QueryDlpFileAccess(asyncContext->permInfo);
993 }
994
GetDLPPermissionInfoComplete(napi_env env,napi_status status,void * data)995 void NapiDlpPermission::GetDLPPermissionInfoComplete(napi_env env, napi_status status, void* data)
996 {
997 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
998 auto asyncContext = reinterpret_cast<GetPermInfoAsyncContext*>(data);
999 if (asyncContext == nullptr) {
1000 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1001 return;
1002 }
1003 std::unique_ptr<GetPermInfoAsyncContext> asyncContextPtr { asyncContext };
1004 napi_value permInfoJs = nullptr;
1005 if (asyncContext->errCode == DLP_OK) {
1006 permInfoJs = DlpPermissionInfoToJs(env, asyncContext->permInfo);
1007 }
1008 ProcessCallbackOrPromise(env, asyncContext, permInfoJs);
1009 }
1010
IsInSandbox(napi_env env,napi_callback_info cbInfo)1011 napi_value NapiDlpPermission::IsInSandbox(napi_env env, napi_callback_info cbInfo)
1012 {
1013 auto* asyncContext = new (std::nothrow) IsInSandboxAsyncContext(env);
1014 if (asyncContext == nullptr) {
1015 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1016 return nullptr;
1017 }
1018 std::unique_ptr<IsInSandboxAsyncContext> asyncContextPtr { asyncContext };
1019
1020 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContext)) {
1021 return nullptr;
1022 }
1023
1024 napi_value result = nullptr;
1025 if (asyncContext->callbackRef == nullptr) {
1026 DLP_LOG_DEBUG(LABEL, "Create promise");
1027 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1028 } else {
1029 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1030 NAPI_CALL(env, napi_get_undefined(env, &result));
1031 }
1032
1033 napi_value resource = nullptr;
1034 NAPI_CALL(env, napi_create_string_utf8(env, "IsInSandbox", NAPI_AUTO_LENGTH, &resource));
1035 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, IsInSandboxExcute, IsInSandboxComplete,
1036 static_cast<void*>(asyncContext), &(asyncContext->work)));
1037 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1038 asyncContextPtr.release();
1039 return result;
1040 }
1041
IsInSandboxExcute(napi_env env,void * data)1042 void NapiDlpPermission::IsInSandboxExcute(napi_env env, void* data)
1043 {
1044 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1045 auto asyncContext = reinterpret_cast<IsInSandboxAsyncContext*>(data);
1046 if (asyncContext == nullptr) {
1047 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1048 return;
1049 }
1050
1051 asyncContext->errCode = DlpPermissionKit::IsInDlpSandbox(asyncContext->inSandbox);
1052 }
1053
IsInSandboxComplete(napi_env env,napi_status status,void * data)1054 void NapiDlpPermission::IsInSandboxComplete(napi_env env, napi_status status, void* data)
1055 {
1056 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1057 auto asyncContext = reinterpret_cast<IsInSandboxAsyncContext*>(data);
1058 if (asyncContext == nullptr) {
1059 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1060 return;
1061 }
1062 std::unique_ptr<IsInSandboxAsyncContext> asyncContextPtr { asyncContext };
1063 napi_value inSandboxJs = nullptr;
1064 if (asyncContext->errCode == DLP_OK) {
1065 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, asyncContext->inSandbox, &inSandboxJs));
1066 }
1067 ProcessCallbackOrPromise(env, asyncContext, inSandboxJs);
1068 }
1069
GetDlpSupportFileType(napi_env env,napi_callback_info cbInfo)1070 napi_value NapiDlpPermission::GetDlpSupportFileType(napi_env env, napi_callback_info cbInfo)
1071 {
1072 auto* asyncContext = new (std::nothrow) GetDlpSupportFileTypeAsyncContext(env);
1073 if (asyncContext == nullptr) {
1074 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1075 return nullptr;
1076 }
1077 std::unique_ptr<GetDlpSupportFileTypeAsyncContext> asyncContextPtr { asyncContext };
1078
1079 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContext)) {
1080 return nullptr;
1081 }
1082
1083 napi_value result = nullptr;
1084 if (asyncContext->callbackRef == nullptr) {
1085 DLP_LOG_DEBUG(LABEL, "Create promise");
1086 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1087 } else {
1088 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1089 NAPI_CALL(env, napi_get_undefined(env, &result));
1090 }
1091
1092 napi_value resource = nullptr;
1093 NAPI_CALL(env, napi_create_string_utf8(env, "GetDlpSupportFileType", NAPI_AUTO_LENGTH, &resource));
1094 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetDlpSupportFileTypeExcute,
1095 GetDlpSupportFileTypeComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
1096 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1097 asyncContextPtr.release();
1098 return result;
1099 }
1100
GetDlpSupportFileTypeExcute(napi_env env,void * data)1101 void NapiDlpPermission::GetDlpSupportFileTypeExcute(napi_env env, void* data)
1102 {
1103 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1104 auto asyncContext = reinterpret_cast<GetDlpSupportFileTypeAsyncContext*>(data);
1105 if (asyncContext == nullptr) {
1106 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1107 return;
1108 }
1109
1110 asyncContext->errCode = DlpPermissionKit::GetDlpSupportFileType(asyncContext->supportFileType);
1111 }
1112
GetDlpSupportFileTypeComplete(napi_env env,napi_status status,void * data)1113 void NapiDlpPermission::GetDlpSupportFileTypeComplete(napi_env env, napi_status status, void* data)
1114 {
1115 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1116 auto asyncContext = reinterpret_cast<GetDlpSupportFileTypeAsyncContext*>(data);
1117 if (asyncContext == nullptr) {
1118 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1119 return;
1120 }
1121 std::unique_ptr<GetDlpSupportFileTypeAsyncContext> asyncContextPtr { asyncContext };
1122 napi_value supportFileTypeJs = nullptr;
1123 if (asyncContext->errCode == DLP_OK) {
1124 supportFileTypeJs = VectorStringToJs(env, asyncContext->supportFileType);
1125 }
1126 ProcessCallbackOrPromise(env, asyncContext, supportFileTypeJs);
1127 }
1128
RegisterSandboxChangeCallback(napi_env env,napi_callback_info cbInfo)1129 napi_value NapiDlpPermission::RegisterSandboxChangeCallback(napi_env env, napi_callback_info cbInfo)
1130 {
1131 RegisterDlpSandboxChangeInfo *registerDlpSandboxChangeInfo = new (std::nothrow) RegisterDlpSandboxChangeInfo();
1132 if (registerDlpSandboxChangeInfo == nullptr) {
1133 DLP_LOG_ERROR(LABEL, "insufficient memory for subscribeCBInfo!");
1134 return nullptr;
1135 }
1136 std::unique_ptr<RegisterDlpSandboxChangeInfo> callbackPtr { registerDlpSandboxChangeInfo };
1137 if (!ParseInputToRegister(env, cbInfo, *registerDlpSandboxChangeInfo)) {
1138 return nullptr;
1139 }
1140 int32_t result = DlpPermissionKit::RegisterDlpSandboxChangeCallback(registerDlpSandboxChangeInfo->subscriber);
1141 if (result != DLP_OK) {
1142 DLP_LOG_ERROR(LABEL, "RegisterSandboxChangeCallback failed");
1143 DlpNapiThrow(env, result);
1144 return nullptr;
1145 }
1146 if (g_dlpSandboxChangeInfoRegister != nullptr) {
1147 delete g_dlpSandboxChangeInfoRegister;
1148 g_dlpSandboxChangeInfoRegister = nullptr;
1149 }
1150 g_dlpSandboxChangeInfoRegister = callbackPtr.release();
1151 return nullptr;
1152 }
1153
UnregisterSandboxChangeCallback(napi_env env,napi_callback_info cbInfo)1154 napi_value NapiDlpPermission::UnregisterSandboxChangeCallback(napi_env env, napi_callback_info cbInfo)
1155 {
1156 auto *asyncContext = new (std::nothrow) UnregisterSandboxChangeCallbackAsyncContext(env);
1157 if (asyncContext == nullptr) {
1158 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1159 return nullptr;
1160 }
1161 std::unique_ptr<UnregisterSandboxChangeCallbackAsyncContext> asyncContextPtr { asyncContext };
1162 if (!GetUnregisterSandboxParams(env, cbInfo, *asyncContext)) {
1163 return nullptr;
1164 }
1165
1166 int32_t result = DlpPermissionKit::UnregisterDlpSandboxChangeCallback(asyncContext->result);
1167 if (result != DLP_OK) {
1168 DLP_LOG_ERROR(LABEL, "UnregisterSandboxChangeCallback failed");
1169 DlpNapiThrow(env, result);
1170 return nullptr;
1171 }
1172 if (g_dlpSandboxChangeInfoRegister != nullptr) {
1173 delete g_dlpSandboxChangeInfoRegister;
1174 g_dlpSandboxChangeInfoRegister = nullptr;
1175 }
1176 return nullptr;
1177 }
1178
CompareOnAndOffRef(const napi_env env,napi_ref subscriberRef,napi_ref unsubscriberRef)1179 bool CompareOnAndOffRef(const napi_env env, napi_ref subscriberRef, napi_ref unsubscriberRef)
1180 {
1181 napi_value subscriberCallback;
1182 napi_get_reference_value(env, subscriberRef, &subscriberCallback);
1183 napi_value unsubscriberCallback;
1184 napi_get_reference_value(env, unsubscriberRef, &unsubscriberCallback);
1185 bool result = false;
1186 napi_strict_equals(env, subscriberCallback, unsubscriberCallback, &result);
1187 return result;
1188 }
1189
IsSubscribeExist(napi_env env,OpenDlpFileSubscriberContext * subscribeCBInfo)1190 static bool IsSubscribeExist(napi_env env, OpenDlpFileSubscriberContext* subscribeCBInfo)
1191 {
1192 return std::any_of(g_openDlpFileSubscribers.begin(), g_openDlpFileSubscribers.end(),
1193 [env, subscribeCBInfo](const auto& it) {
1194 return CompareOnAndOffRef(env, it->callbackRef, subscribeCBInfo->callbackRef);
1195 });
1196 }
1197
SubscribeOpenDlpFile(const napi_env env,const napi_value thisVar,napi_ref & callback)1198 napi_value NapiDlpPermission::SubscribeOpenDlpFile(const napi_env env, const napi_value thisVar, napi_ref& callback)
1199 {
1200 DLP_LOG_INFO(LABEL, "Subscribe open dlp file");
1201 OpenDlpFileSubscriberContext* syncContext = new (std::nothrow) OpenDlpFileSubscriberContext();
1202 if (syncContext == nullptr) {
1203 DLP_LOG_ERROR(LABEL, "insufficient memory for syncContext!");
1204 return nullptr;
1205 }
1206 std::unique_ptr<OpenDlpFileSubscriberContext> syncContextPtr { syncContext };
1207 syncContextPtr->env = env;
1208 syncContextPtr->callbackRef = callback;
1209 syncContextPtr->subscriber = std::make_shared<OpenDlpFileSubscriberPtr>();
1210 syncContextPtr->subscriber->SetEnv(env);
1211 syncContextPtr->subscriber->SetCallbackRef(callback);
1212
1213 std::lock_guard<std::mutex> lock(g_lockForOpenDlpFileSubscriber);
1214 if (IsSubscribeExist(env, syncContext)) {
1215 DLP_LOG_ERROR(LABEL, "Subscribe failed. The current subscriber has been existed");
1216 return nullptr;
1217 }
1218 int32_t result = DlpPermissionKit::RegisterOpenDlpFileCallback(syncContextPtr->subscriber);
1219 if (result != DLP_OK) {
1220 DLP_LOG_ERROR(LABEL, "RegisterSandboxChangeCallback failed");
1221 DlpNapiThrow(env, result);
1222 return nullptr;
1223 }
1224 napi_status wrapStatus = napi_wrap(
1225 env, thisVar, reinterpret_cast<void*>(syncContextPtr->subscriber.get()),
1226 [](napi_env nev, void* data, void* hint) {
1227 DLP_LOG_INFO(LABEL, "OpenDlpFileSubscriberPtr delete");
1228 OpenDlpFileSubscriberPtr* subscriber = static_cast<OpenDlpFileSubscriberPtr*>(data);
1229 if (subscriber != nullptr) {
1230 subscriber->SetValid(false);
1231 }
1232 },
1233 nullptr, nullptr);
1234 if (wrapStatus != napi_ok) {
1235 DLP_LOG_ERROR(LABEL, "Wrap js and native option failed");
1236 DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, "Wrap js and native option failed");
1237 return nullptr;
1238 }
1239 g_openDlpFileSubscribers.emplace(syncContext);
1240 DLP_LOG_INFO(LABEL, "Subscribe open dlp file success");
1241 syncContextPtr.release();
1242 return nullptr;
1243 }
1244
Subscribe(napi_env env,napi_callback_info cbInfo)1245 napi_value NapiDlpPermission::Subscribe(napi_env env, napi_callback_info cbInfo)
1246 {
1247 size_t argc = PARAM_SIZE_TWO;
1248 napi_value argv[PARAM_SIZE_TWO] = {nullptr};
1249 napi_value thisVar = nullptr;
1250 NAPI_CALL(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr));
1251 if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO + 1)) {
1252 return nullptr;
1253 }
1254 std::string type;
1255 if (!GetStringValue(env, argv[PARAM0], type) || !IsStringLengthValid(type, MAX_TYPE_LEN)) {
1256 DLP_LOG_ERROR(LABEL, "event type is invalid");
1257 ThrowParamError(env, "type", "string");
1258 return nullptr;
1259 }
1260 napi_ref callback = nullptr;
1261 if (!ParseCallback(env, argv[PARAM1], callback)) {
1262 DLP_LOG_ERROR(LABEL, "event listener is invalid");
1263 ThrowParamError(env, "listener", "function");
1264 return nullptr;
1265 }
1266
1267 if (type == "openDLPFile") {
1268 return SubscribeOpenDlpFile(env, thisVar, callback);
1269 } else if (type == "uninstallDLPSandbox") {
1270 return RegisterSandboxChangeCallback(env, cbInfo);
1271 } else {
1272 NAPI_CALL(env, napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, "event type is wrong")));
1273 return nullptr;
1274 }
1275 }
1276
UnSubscribeOpenDlpFile(const napi_env env,napi_ref & callback)1277 napi_value NapiDlpPermission::UnSubscribeOpenDlpFile(const napi_env env, napi_ref& callback)
1278 {
1279 std::lock_guard<std::mutex> lock(g_lockForOpenDlpFileSubscriber);
1280 if (callback == nullptr) {
1281 auto iter = g_openDlpFileSubscribers.begin();
1282 while (iter != g_openDlpFileSubscribers.end()) {
1283 int32_t result = DlpPermissionKit::UnRegisterOpenDlpFileCallback((*iter)->subscriber);
1284 if (result != DLP_OK) {
1285 DLP_LOG_ERROR(LABEL, "UnSubscribeOpenDlpFile failed");
1286 DlpNapiThrow(env, result);
1287 return nullptr;
1288 }
1289 delete *iter;
1290 iter = g_openDlpFileSubscribers.erase(iter);
1291 }
1292 } else {
1293 auto iter = g_openDlpFileSubscribers.begin();
1294 while (iter != g_openDlpFileSubscribers.end()) {
1295 if (!CompareOnAndOffRef(env, (*iter)->callbackRef, callback)) {
1296 iter++;
1297 continue;
1298 }
1299 int32_t result = DlpPermissionKit::UnRegisterOpenDlpFileCallback((*iter)->subscriber);
1300 if (result != DLP_OK) {
1301 DLP_LOG_ERROR(LABEL, "UnSubscribeOpenDlpFile failed");
1302 DlpNapiThrow(env, result);
1303 return nullptr;
1304 }
1305 delete *iter;
1306 g_openDlpFileSubscribers.erase(iter);
1307 break;
1308 }
1309 }
1310 return nullptr;
1311 }
1312
UnSubscribe(napi_env env,napi_callback_info cbInfo)1313 napi_value NapiDlpPermission::UnSubscribe(napi_env env, napi_callback_info cbInfo)
1314 {
1315 size_t argc = PARAM_SIZE_TWO;
1316 napi_value argv[PARAM_SIZE_TWO] = {nullptr};
1317 napi_value thisVar = nullptr;
1318 NAPI_CALL(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr));
1319 if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
1320 return nullptr;
1321 }
1322 std::string type;
1323 if (!GetStringValue(env, argv[PARAM0], type) || !IsStringLengthValid(type, MAX_TYPE_LEN)) {
1324 DLP_LOG_ERROR(LABEL, "event type is invalid");
1325 ThrowParamError(env, "type", "string");
1326 return nullptr;
1327 }
1328 napi_ref callback = nullptr;
1329 if (argc == PARAM_SIZE_TWO) {
1330 if (!ParseCallback(env, argv[PARAM1], callback)) {
1331 DLP_LOG_ERROR(LABEL, "event listener is invalid");
1332 ThrowParamError(env, "listener", "function");
1333 return nullptr;
1334 }
1335 }
1336
1337 if (type == "openDLPFile") {
1338 return UnSubscribeOpenDlpFile(env, callback);
1339 } else if (type == "uninstallDLPSandbox") {
1340 return UnregisterSandboxChangeCallback(env, cbInfo);
1341 } else {
1342 NAPI_CALL(env, napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, "event type is wrong")));
1343 return nullptr;
1344 }
1345 }
1346
GetDlpGatheringPolicy(napi_env env,napi_callback_info cbInfo)1347 napi_value NapiDlpPermission::GetDlpGatheringPolicy(napi_env env, napi_callback_info cbInfo)
1348 {
1349 auto* asyncContext = new (std::nothrow) GetGatheringPolicyContext(env);
1350 if (asyncContext == nullptr) {
1351 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1352 return nullptr;
1353 }
1354 std::unique_ptr<GetGatheringPolicyContext> asyncContextPtr { asyncContext };
1355
1356 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContext)) {
1357 return nullptr;
1358 }
1359
1360 napi_value result = nullptr;
1361 if (asyncContext->callbackRef == nullptr) {
1362 DLP_LOG_DEBUG(LABEL, "Create promise");
1363 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1364 } else {
1365 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1366 NAPI_CALL(env, napi_get_undefined(env, &result));
1367 }
1368
1369 napi_value resource = nullptr;
1370 NAPI_CALL(env, napi_create_string_utf8(env, "GetDlpGatheringPolicy", NAPI_AUTO_LENGTH, &resource));
1371 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetDlpGatheringPolicyExcute,
1372 GetDlpGatheringPolicyComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
1373 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1374 asyncContextPtr.release();
1375 return result;
1376 }
1377
GetDlpGatheringPolicyExcute(napi_env env,void * data)1378 void NapiDlpPermission::GetDlpGatheringPolicyExcute(napi_env env, void* data)
1379 {
1380 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1381 auto asyncContext = reinterpret_cast<GetGatheringPolicyContext*>(data);
1382 if (asyncContext == nullptr) {
1383 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1384 return;
1385 }
1386
1387 asyncContext->errCode = DlpPermissionKit::GetDlpGatheringPolicy(asyncContext->isGathering);
1388 }
1389
GetDlpGatheringPolicyComplete(napi_env env,napi_status status,void * data)1390 void NapiDlpPermission::GetDlpGatheringPolicyComplete(napi_env env, napi_status status, void* data)
1391 {
1392 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1393 auto asyncContext = reinterpret_cast<GetGatheringPolicyContext*>(data);
1394 if (asyncContext == nullptr) {
1395 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1396 return;
1397 }
1398 std::unique_ptr<GetGatheringPolicyContext> asyncContextPtr { asyncContext };
1399 napi_value isGatheringJs = nullptr;
1400 if (asyncContext->errCode == DLP_OK) {
1401 GatheringPolicyType policy = asyncContext->isGathering ? GATHERING : NON_GATHERING;
1402 NAPI_CALL_RETURN_VOID(env, napi_create_uint32(env, policy, &isGatheringJs));
1403 }
1404 ProcessCallbackOrPromise(env, asyncContext, isGatheringJs);
1405 }
1406
DlpFile(napi_env env,napi_callback_info cbInfo)1407 napi_value NapiDlpPermission::DlpFile(napi_env env, napi_callback_info cbInfo)
1408 {
1409 napi_value instance = nullptr;
1410 napi_value constructor = nullptr;
1411
1412 if (napi_get_reference_value(env, dlpFileRef_, &constructor) != napi_ok) {
1413 return nullptr;
1414 }
1415
1416 DLP_LOG_DEBUG(LABEL, "Get a reference to the global variable dlpFileRef_ complete");
1417
1418 if (napi_new_instance(env, constructor, 0, nullptr, &instance) != napi_ok) {
1419 return nullptr;
1420 }
1421
1422 DLP_LOG_DEBUG(LABEL, "New the js instance complete");
1423
1424 return instance;
1425 }
1426
SetRetentionState(napi_env env,napi_callback_info cbInfo)1427 napi_value NapiDlpPermission::SetRetentionState(napi_env env, napi_callback_info cbInfo)
1428 {
1429 auto* asyncContext = new (std::nothrow) RetentionStateAsyncContext(env);
1430 if (asyncContext == nullptr) {
1431 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1432 return nullptr;
1433 }
1434 std::unique_ptr<RetentionStateAsyncContext> asyncContextPtr { asyncContext };
1435
1436 if (!GetRetentionStateParams(env, cbInfo, *asyncContext)) {
1437 return nullptr;
1438 }
1439
1440 napi_value result = nullptr;
1441 if (asyncContext->callbackRef == nullptr) {
1442 DLP_LOG_DEBUG(LABEL, "Create promise");
1443 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1444 } else {
1445 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1446 NAPI_CALL(env, napi_get_undefined(env, &result));
1447 }
1448
1449 napi_value resource = nullptr;
1450 NAPI_CALL(env, napi_create_string_utf8(env, "SetRetentionState", NAPI_AUTO_LENGTH, &resource));
1451 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, SetRetentionStateExcute, SetRetentionStateComplete,
1452 static_cast<void*>(asyncContext), &(asyncContext->work)));
1453 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1454 asyncContextPtr.release();
1455 return result;
1456 }
1457
SetRetentionStateExcute(napi_env env,void * data)1458 void NapiDlpPermission::SetRetentionStateExcute(napi_env env, void* data)
1459 {
1460 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1461 auto asyncContext = reinterpret_cast<RetentionStateAsyncContext*>(data);
1462 asyncContext->errCode = DlpPermissionKit::SetRetentionState(asyncContext->docUris);
1463 }
1464
SetRetentionStateComplete(napi_env env,napi_status status,void * data)1465 void NapiDlpPermission::SetRetentionStateComplete(napi_env env, napi_status status, void* data)
1466 {
1467 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1468 auto asyncContext = reinterpret_cast<RetentionStateAsyncContext*>(data);
1469 std::unique_ptr<RetentionStateAsyncContext> asyncContextPtr { asyncContext };
1470 napi_value resJs = nullptr;
1471 if (asyncContext->errCode == DLP_OK) {
1472 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
1473 }
1474 ProcessCallbackOrPromise(env, asyncContext, resJs);
1475 }
1476
CancelRetentionState(napi_env env,napi_callback_info cbInfo)1477 napi_value NapiDlpPermission::CancelRetentionState(napi_env env, napi_callback_info cbInfo)
1478 {
1479 auto* asyncContext = new (std::nothrow) RetentionStateAsyncContext(env);
1480 if (asyncContext == nullptr) {
1481 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1482 return nullptr;
1483 }
1484 std::unique_ptr<RetentionStateAsyncContext> asyncContextPtr { asyncContext };
1485
1486 if (!GetRetentionStateParams(env, cbInfo, *asyncContext)) {
1487 return nullptr;
1488 }
1489
1490 napi_value result = nullptr;
1491 if (asyncContext->callbackRef == nullptr) {
1492 DLP_LOG_DEBUG(LABEL, "Create promise");
1493 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1494 } else {
1495 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1496 NAPI_CALL(env, napi_get_undefined(env, &result));
1497 }
1498
1499 napi_value resource = nullptr;
1500 NAPI_CALL(env, napi_create_string_utf8(env, "CancelRetentionState", NAPI_AUTO_LENGTH, &resource));
1501 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, CancelRetentionStateExcute,
1502 CancelRetentionStateComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
1503 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1504 asyncContextPtr.release();
1505 return result;
1506 }
1507
CancelRetentionStateExcute(napi_env env,void * data)1508 void NapiDlpPermission::CancelRetentionStateExcute(napi_env env, void* data)
1509 {
1510 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1511 auto asyncContext = reinterpret_cast<RetentionStateAsyncContext*>(data);
1512 asyncContext->errCode = DlpPermissionKit::CancelRetentionState(asyncContext->docUris);
1513 }
1514
CancelRetentionStateComplete(napi_env env,napi_status status,void * data)1515 void NapiDlpPermission::CancelRetentionStateComplete(napi_env env, napi_status status, void* data)
1516 {
1517 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1518 auto asyncContext = reinterpret_cast<RetentionStateAsyncContext*>(data);
1519 std::unique_ptr<RetentionStateAsyncContext> asyncContextPtr { asyncContext };
1520 napi_value resJs = nullptr;
1521 if (asyncContext->errCode == DLP_OK) {
1522 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
1523 }
1524 ProcessCallbackOrPromise(env, asyncContext, resJs);
1525 }
1526
GetRetentionSandboxList(napi_env env,napi_callback_info cbInfo)1527 napi_value NapiDlpPermission::GetRetentionSandboxList(napi_env env, napi_callback_info cbInfo)
1528 {
1529 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1530 auto* asyncContext = new (std::nothrow) GetRetentionSandboxListAsyncContext(env);
1531 if (asyncContext == nullptr) {
1532 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1533 return nullptr;
1534 }
1535 std::unique_ptr<GetRetentionSandboxListAsyncContext> asyncContextPtr { asyncContext };
1536
1537 if (!GetRetentionSandboxListParams(env, cbInfo, *asyncContext)) {
1538 return nullptr;
1539 }
1540
1541 napi_value result = nullptr;
1542 if (asyncContext->callbackRef == nullptr) {
1543 DLP_LOG_DEBUG(LABEL, "Create promise");
1544 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1545 } else {
1546 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1547 NAPI_CALL(env, napi_get_undefined(env, &result));
1548 }
1549
1550 napi_value resource = nullptr;
1551 NAPI_CALL(env, napi_create_string_utf8(env, "GetRetentionSandboxList", NAPI_AUTO_LENGTH, &resource));
1552 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetRetentionSandboxListExcute,
1553 GetRetentionSandboxListComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
1554 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1555 asyncContextPtr.release();
1556 return result;
1557 }
1558
GetRetentionSandboxListExcute(napi_env env,void * data)1559 void NapiDlpPermission::GetRetentionSandboxListExcute(napi_env env, void* data)
1560 {
1561 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1562 auto asyncContext = reinterpret_cast<GetRetentionSandboxListAsyncContext*>(data);
1563 asyncContext->errCode =
1564 DlpPermissionKit::GetRetentionSandboxList(asyncContext->bundleName, asyncContext->retentionSandBoxInfoVec);
1565 }
1566
GetRetentionSandboxListComplete(napi_env env,napi_status status,void * data)1567 void NapiDlpPermission::GetRetentionSandboxListComplete(napi_env env, napi_status status, void* data)
1568 {
1569 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1570 auto asyncContext = reinterpret_cast<GetRetentionSandboxListAsyncContext*>(data);
1571 std::unique_ptr<GetRetentionSandboxListAsyncContext> asyncContextPtr { asyncContext };
1572 napi_value resJs = nullptr;
1573 if (asyncContext->errCode == DLP_OK) {
1574 resJs = RetentionSandboxInfoToJs(env, asyncContext->retentionSandBoxInfoVec);
1575 }
1576 ProcessCallbackOrPromise(env, asyncContext, resJs);
1577 }
1578
GetDLPFileVisitRecord(napi_env env,napi_callback_info cbInfo)1579 napi_value NapiDlpPermission::GetDLPFileVisitRecord(napi_env env, napi_callback_info cbInfo)
1580 {
1581 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1582 auto* asyncContext = new (std::nothrow) GetDLPFileVisitRecordAsyncContext(env);
1583 if (asyncContext == nullptr) {
1584 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
1585 return nullptr;
1586 }
1587 std::unique_ptr<GetDLPFileVisitRecordAsyncContext> asyncContextPtr { asyncContext };
1588
1589 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContext)) {
1590 return nullptr;
1591 }
1592
1593 napi_value result = nullptr;
1594 if (asyncContext->callbackRef == nullptr) {
1595 DLP_LOG_DEBUG(LABEL, "Create promise");
1596 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1597 } else {
1598 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1599 NAPI_CALL(env, napi_get_undefined(env, &result));
1600 }
1601
1602 napi_value resource = nullptr;
1603 NAPI_CALL(env, napi_create_string_utf8(env, "GetDLPFileVisitRecord", NAPI_AUTO_LENGTH, &resource));
1604 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetDLPFileVisitRecordExcute,
1605 GetDLPFileVisitRecordComplete, static_cast<void*>(asyncContext), &(asyncContext->work)));
1606 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
1607 asyncContextPtr.release();
1608 return result;
1609 }
1610
GetDLPFileVisitRecordExcute(napi_env env,void * data)1611 void NapiDlpPermission::GetDLPFileVisitRecordExcute(napi_env env, void* data)
1612 {
1613 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
1614 auto asyncContext = reinterpret_cast<GetDLPFileVisitRecordAsyncContext*>(data);
1615 asyncContext->errCode = DlpPermissionKit::GetDLPFileVisitRecord(asyncContext->visitedDlpFileInfoVec);
1616 }
1617
GetDLPFileVisitRecordComplete(napi_env env,napi_status status,void * data)1618 void NapiDlpPermission::GetDLPFileVisitRecordComplete(napi_env env, napi_status status, void* data)
1619 {
1620 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
1621 auto asyncContext = reinterpret_cast<GetDLPFileVisitRecordAsyncContext*>(data);
1622 std::unique_ptr<GetDLPFileVisitRecordAsyncContext> asyncContextPtr { asyncContext };
1623 napi_value resJs = nullptr;
1624 if (asyncContext->errCode == DLP_OK) {
1625 resJs = VisitInfoToJs(env, asyncContext->visitedDlpFileInfoVec);
1626 }
1627 ProcessCallbackOrPromise(env, asyncContext, resJs);
1628 }
1629
SetSandboxAppConfig(napi_env env,napi_callback_info cbInfo)1630 napi_value NapiDlpPermission::SetSandboxAppConfig(napi_env env, napi_callback_info cbInfo)
1631 {
1632 DLP_LOG_DEBUG(LABEL, "napi_create_async_work SetSandboxAppConfig running");
1633 auto asyncContextPtr = std::make_unique<SandboxAppConfigAsyncContext>(env);
1634 if (!GetSandboxAppConfigParams(env, cbInfo, asyncContextPtr.get())) {
1635 return nullptr;
1636 }
1637 napi_value result = nullptr;
1638 DLP_LOG_DEBUG(LABEL, "Create promise");
1639 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1640 napi_value resource = nullptr;
1641 NAPI_CALL(env, napi_create_string_utf8(env, "SetSandboxAppConfig", NAPI_AUTO_LENGTH, &resource));
1642 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, SetSandboxAppConfigExecute,
1643 SetSandboxAppConfigComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1644 NAPI_CALL(env, napi_queue_async_work(env, asyncContextPtr->work));
1645 asyncContextPtr.release();
1646 return result;
1647 }
1648
SetSandboxAppConfigExecute(napi_env env,void * data)1649 void NapiDlpPermission::SetSandboxAppConfigExecute(napi_env env, void* data)
1650 {
1651 DLP_LOG_DEBUG(LABEL, "napi_create_async_work SetSandboxAppConfigExecute running");
1652 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1653 if (asyncContext == nullptr) {
1654 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1655 return;
1656 }
1657 asyncContext->errCode = DlpPermissionKit::SetSandboxAppConfig(asyncContext->configInfo);
1658 }
1659
SetSandboxAppConfigComplete(napi_env env,napi_status status,void * data)1660 void NapiDlpPermission::SetSandboxAppConfigComplete(napi_env env, napi_status status, void* data)
1661 {
1662 DLP_LOG_DEBUG(LABEL, "napi_create_async_work SetSandboxAppConfig complete");
1663 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1664 if (asyncContext == nullptr) {
1665 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1666 return;
1667 }
1668 std::unique_ptr<SandboxAppConfigAsyncContext> asyncContextPtr { asyncContext };
1669 napi_value resJs = nullptr;
1670 if (asyncContext->errCode == DLP_OK) {
1671 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
1672 }
1673 ProcessCallbackOrPromise(env, asyncContext, resJs);
1674 }
1675
CleanSandboxAppConfig(napi_env env,napi_callback_info cbInfo)1676 napi_value NapiDlpPermission::CleanSandboxAppConfig(napi_env env, napi_callback_info cbInfo)
1677 {
1678 auto asyncContextPtr = std::make_unique<SandboxAppConfigAsyncContext>(env);
1679 napi_value result = nullptr;
1680 DLP_LOG_DEBUG(LABEL, "Create promise");
1681 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1682 napi_value resource = nullptr;
1683 NAPI_CALL(env, napi_create_string_utf8(env, "CleanSandboxAppConfig", NAPI_AUTO_LENGTH, &resource));
1684 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, CleanSandboxAppConfigExecute,
1685 CleanSandboxAppConfigComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1686 NAPI_CALL(env, napi_queue_async_work(env, asyncContextPtr->work));
1687 asyncContextPtr.release();
1688 return result;
1689 }
1690
CleanSandboxAppConfigExecute(napi_env env,void * data)1691 void NapiDlpPermission::CleanSandboxAppConfigExecute(napi_env env, void* data)
1692 {
1693 DLP_LOG_DEBUG(LABEL, "napi_create_async_work CleanSandboxAppConfigExecute running");
1694 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1695 if (asyncContext == nullptr) {
1696 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1697 return;
1698 }
1699 asyncContext->errCode = DlpPermissionKit::CleanSandboxAppConfig();
1700 }
1701
CleanSandboxAppConfigComplete(napi_env env,napi_status status,void * data)1702 void NapiDlpPermission::CleanSandboxAppConfigComplete(napi_env env, napi_status status, void* data)
1703 {
1704 DLP_LOG_DEBUG(LABEL, "napi_create_async_work CleanSandboxAppConfig complete");
1705 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1706 if (asyncContext == nullptr) {
1707 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1708 return;
1709 }
1710 std::unique_ptr<SandboxAppConfigAsyncContext> asyncContextPtr { asyncContext };
1711 napi_value resJs = nullptr;
1712 if (asyncContext->errCode == DLP_OK) {
1713 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
1714 }
1715 ProcessCallbackOrPromise(env, asyncContext, resJs);
1716 }
1717
GetSandboxAppConfig(napi_env env,napi_callback_info cbInfo)1718 napi_value NapiDlpPermission::GetSandboxAppConfig(napi_env env, napi_callback_info cbInfo)
1719 {
1720 auto asyncContextPtr = std::make_unique<SandboxAppConfigAsyncContext>(env);
1721 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContextPtr.get())) {
1722 return nullptr;
1723 }
1724 napi_value result = nullptr;
1725 DLP_LOG_DEBUG(LABEL, "Create promise");
1726 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1727 napi_value resource = nullptr;
1728 NAPI_CALL(env, napi_create_string_utf8(env, "GetSandboxAppConfig", NAPI_AUTO_LENGTH, &resource));
1729 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GetSandboxAppConfigExecute,
1730 GetSandboxAppConfigComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1731 NAPI_CALL(env, napi_queue_async_work(env, asyncContextPtr->work));
1732 asyncContextPtr.release();
1733 return result;
1734 }
1735
GetSandboxAppConfigExecute(napi_env env,void * data)1736 void NapiDlpPermission::GetSandboxAppConfigExecute(napi_env env, void* data)
1737 {
1738 DLP_LOG_DEBUG(LABEL, "napi_create_async_work GetSandboxAppConfigExecute running");
1739 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1740 if (asyncContext == nullptr) {
1741 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1742 return;
1743 }
1744 asyncContext->errCode = DlpPermissionKit::GetSandboxAppConfig(asyncContext->configInfo);
1745 }
1746
GetSandboxAppConfigComplete(napi_env env,napi_status status,void * data)1747 void NapiDlpPermission::GetSandboxAppConfigComplete(napi_env env, napi_status status, void* data)
1748 {
1749 DLP_LOG_DEBUG(LABEL, "napi_create_async_work GetSandboxAppConfig complete");
1750 auto asyncContext = reinterpret_cast<SandboxAppConfigAsyncContext*>(data);
1751 if (asyncContext == nullptr) {
1752 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
1753 return;
1754 }
1755 std::unique_ptr<SandboxAppConfigAsyncContext> asyncContextPtr { asyncContext };
1756 napi_value configInfoJs = nullptr;
1757 if (asyncContext->errCode == DLP_OK) {
1758 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, asyncContext->configInfo.c_str(),
1759 NAPI_AUTO_LENGTH, &configInfoJs));
1760 }
1761 ProcessCallbackOrPromise(env, asyncContext, configInfoJs);
1762 }
1763
1764
IsDLPFeatureProvided(napi_env env,napi_callback_info cbInfo)1765 napi_value NapiDlpPermission::IsDLPFeatureProvided(napi_env env, napi_callback_info cbInfo)
1766 {
1767 auto asyncContextPtr = std::make_unique<IsDLPFeatureProvidedAsyncContext>(env);
1768 if (!GetThirdInterfaceParams(env, cbInfo, *asyncContextPtr.get())) {
1769 return nullptr;
1770 }
1771 napi_value result = nullptr;
1772 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1773 napi_value resource = nullptr;
1774 NAPI_CALL(env, napi_create_string_utf8(env, "IsDLPFeatureProvided", NAPI_AUTO_LENGTH, &resource));
1775 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, IsDLPFeatureProvidedExcute,
1776 IsDLPFeatureProvidedComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1777 NAPI_CALL(env, napi_queue_async_work(env, asyncContextPtr->work));
1778 asyncContextPtr.release();
1779 return result;
1780 }
1781
IsDLPFeatureProvidedExcute(napi_env env,void * data)1782 void NapiDlpPermission::IsDLPFeatureProvidedExcute(napi_env env, void* data)
1783 {
1784 DLP_LOG_DEBUG(LABEL, "IsDLPFeatureProvidedExcute start run.");
1785 auto asyncContext = reinterpret_cast<IsDLPFeatureProvidedAsyncContext*>(data);
1786 if (asyncContext == nullptr) {
1787 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
1788 return;
1789 }
1790 asyncContext->errCode = DlpPermissionKit::IsDLPFeatureProvided(asyncContext->isProvideDLPFeature);
1791 }
1792
IsDLPFeatureProvidedComplete(napi_env env,napi_status status,void * data)1793 void NapiDlpPermission::IsDLPFeatureProvidedComplete(napi_env env, napi_status status, void* data)
1794 {
1795 DLP_LOG_DEBUG(LABEL, "IsDLPFeatureProvidedComplete start run.");
1796 auto asyncContext = reinterpret_cast<IsDLPFeatureProvidedAsyncContext*>(data);
1797 if (asyncContext == nullptr) {
1798 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
1799 return;
1800 }
1801 std::unique_ptr<IsDLPFeatureProvidedAsyncContext> asyncContextPtr { asyncContext };
1802 napi_value isProvideDLPFeatureJs = nullptr;
1803 if (asyncContext->errCode == DLP_OK) {
1804 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, asyncContext->isProvideDLPFeature, &isProvideDLPFeatureJs));
1805 }
1806 ProcessCallbackOrPromise(env, asyncContext, isProvideDLPFeatureJs);
1807 }
1808
GetDLPSuffix(napi_env env,napi_callback_info cbInfo)1809 napi_value NapiDlpPermission::GetDLPSuffix(napi_env env, napi_callback_info cbInfo)
1810 {
1811 GetSuffixAsyncContext *asyncContext = new (std::nothrow) GetSuffixAsyncContext(env);
1812 if (asyncContext == nullptr) {
1813 DLP_LOG_ERROR(LABEL, "insufficient memory for GetSuffixAsyncContext!");
1814 return nullptr;
1815 }
1816 std::unique_ptr<GetSuffixAsyncContext> callbackPtr { asyncContext };
1817
1818 napi_value result = nullptr;
1819 NAPI_CALL(env, napi_create_string_utf8(env, DLP_FILE_SUFFIX.c_str(), NAPI_AUTO_LENGTH, &result));
1820 return result;
1821 }
1822
GetOriginalFileName(napi_env env,napi_callback_info cbInfo)1823 napi_value NapiDlpPermission::GetOriginalFileName(napi_env env, napi_callback_info cbInfo)
1824 {
1825 GetOriginalFileAsyncContext *asyncContext = new (std::nothrow) GetOriginalFileAsyncContext(env);
1826 if (asyncContext == nullptr) {
1827 DLP_LOG_ERROR(LABEL, "insufficient memory for GetFileNameAsyncContext!");
1828 return nullptr;
1829 }
1830 std::unique_ptr<GetOriginalFileAsyncContext> callbackPtr { asyncContext };
1831 if (!GetOriginalFilenameParams(env, cbInfo, *asyncContext)) {
1832 return nullptr;
1833 }
1834
1835 std::string resultStr =
1836 asyncContext->dlpFilename.substr(0, asyncContext->dlpFilename.size() - DLP_FILE_SUFFIX.size());
1837 napi_value resultJs = nullptr;
1838 NAPI_CALL(env, napi_create_string_utf8(env, resultStr.c_str(), NAPI_AUTO_LENGTH, &resultJs));
1839 return resultJs;
1840 }
1841
IsSystemApp(napi_env env)1842 bool NapiDlpPermission::IsSystemApp(napi_env env)
1843 {
1844 uint64_t fullTokenId = IPCSkeleton::GetSelfTokenID();
1845 bool isSystemApp = AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
1846 if (!isSystemApp) {
1847 int32_t jsErrCode = ERR_JS_NOT_SYSTEM_APP;
1848 NAPI_CALL_BASE(env, napi_throw(env, GenerateBusinessError(env, jsErrCode, GetJsErrMsg(jsErrCode))), false);
1849 return false;
1850 }
1851 return true;
1852 }
1853
StartDLPManagerForResult(napi_env env,napi_callback_info cbInfo)1854 napi_value NapiDlpPermission::StartDLPManagerForResult(napi_env env, napi_callback_info cbInfo)
1855 {
1856 DLP_LOG_INFO(LABEL, "begin StartDLPManagerForResult");
1857 size_t argc = PARAM_SIZE_TWO;
1858 size_t maxArgcNum = PARAM_SIZE_TWO;
1859 size_t contextIndex = PARAM0;
1860 size_t requestIndex = PARAM1;
1861
1862 napi_value argv[PARAM2] = {nullptr};
1863 napi_value thisVar = nullptr;
1864 napi_value result = nullptr;
1865 NAPI_CALL(env, napi_get_undefined(env, &result));
1866 NAPI_CALL(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr));
1867 if (argc != maxArgcNum) {
1868 DLP_LOG_ERROR(LABEL, "params number mismatch");
1869 std::string errMsg = "Parameter Error. Params number mismatch, need " + std::to_string(maxArgcNum) +
1870 ", given " + std::to_string(argc);
1871 DlpNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg);
1872 return result;
1873 }
1874
1875 auto asyncContext = std::make_shared<UIExtensionRequestContext>(env);
1876 if (!ParseUIAbilityContextReq(env, argv[contextIndex], asyncContext->context)) {
1877 DLP_LOG_ERROR(LABEL, "ParseUIAbilityContextReq failed");
1878 DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, "get context failed");
1879 return result;
1880 }
1881 if (!ParseWantReq(env, argv[requestIndex], asyncContext->requestWant)) {
1882 DLP_LOG_ERROR(LABEL, "ParseWantReq failed");
1883 return result;
1884 }
1885 NAPI_CALL(env, napi_create_promise(env, &asyncContext->deferred, &result));
1886
1887 StartUIExtensionAbility(asyncContext);
1888 DLP_LOG_DEBUG(LABEL, "end StartDLPManagerForResult");
1889 return result;
1890 }
1891
GenerateDlpFileForEnterprise(napi_env env,napi_callback_info cbInfo)1892 napi_value NapiDlpPermission::GenerateDlpFileForEnterprise(napi_env env, napi_callback_info cbInfo)
1893 {
1894 if (!IsSystemApp(env)) {
1895 return nullptr;
1896 }
1897 if (!CheckPermission(env, PERMISSION_ENTERPRISE_ACCESS_DLP_FILE)) {
1898 return nullptr;
1899 }
1900 auto asyncContextPtr = std::make_unique<GenerateDlpFileForEnterpriseAsyncContext>(env);
1901
1902 if (!GetGenerateDlpFileForEnterpriseParam(env, cbInfo, *asyncContextPtr)) {
1903 return nullptr;
1904 }
1905 napi_value result = nullptr;
1906 if (asyncContextPtr->callbackRef == nullptr) {
1907 DLP_LOG_DEBUG(LABEL, "Create promise");
1908 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1909 } else {
1910 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1911 NAPI_CALL(env, napi_get_undefined(env, &result));
1912 }
1913 napi_value resource = nullptr;
1914 NAPI_CALL(env, napi_create_string_utf8(env, "GenerateDlpFileForEnterprise", NAPI_AUTO_LENGTH, &resource));
1915 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, GenerateDlpFileForEnterpriseExcute,
1916 GenerateDlpFileForEnterpriseComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1917 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContextPtr->work, napi_qos_user_initiated));
1918 asyncContextPtr.release();
1919 return result;
1920 }
1921
GenerateDlpFileForEnterpriseExcute(napi_env env,void * data)1922 void NapiDlpPermission::GenerateDlpFileForEnterpriseExcute(napi_env env, void* data)
1923 {
1924 DLP_LOG_DEBUG(LABEL, "GenerateDlpFileForEnterprise start run.");
1925 auto asyncContext = reinterpret_cast<GenerateDlpFileForEnterpriseAsyncContext*>(data);
1926 if (asyncContext == nullptr) {
1927 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
1928 return;
1929 }
1930
1931 asyncContext->errCode = EnterpriseSpaceDlpPermissionKit::GetInstance()->EncryptDlpFile(
1932 asyncContext->property, asyncContext->customProperty, asyncContext->plaintextFd, asyncContext->dlpFd);
1933 }
1934
GenerateDlpFileForEnterpriseComplete(napi_env env,napi_status status,void * data)1935 void NapiDlpPermission::GenerateDlpFileForEnterpriseComplete(napi_env env, napi_status status, void* data)
1936 {
1937 DLP_LOG_DEBUG(LABEL, "GenerateDlpFileForEnterprise start run.");
1938 auto asyncContext = reinterpret_cast<GenerateDlpFileForEnterpriseAsyncContext*>(data);
1939 if (asyncContext == nullptr) {
1940 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
1941 return;
1942 }
1943 std::unique_ptr<GenerateDlpFileForEnterpriseAsyncContext> asyncContextPtr { asyncContext };
1944 napi_value resJs = nullptr;
1945 if (asyncContext->errCode == DLP_OK) {
1946 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
1947 }
1948
1949 ProcessCallbackOrPromise(env, asyncContext, resJs);
1950 }
1951
DecryptDlpFile(napi_env env,napi_callback_info cbInfo)1952 napi_value NapiDlpPermission::DecryptDlpFile(napi_env env, napi_callback_info cbInfo)
1953 {
1954 if (!IsSystemApp(env)) {
1955 return nullptr;
1956 }
1957 if (!CheckPermission(env, PERMISSION_ENTERPRISE_ACCESS_DLP_FILE)) {
1958 return nullptr;
1959 }
1960 auto asyncContextPtr = std::make_unique<DecryptDlpFileAsyncContext>(env);
1961
1962 if (!GetDecryptDlpFileParam(env, cbInfo, *asyncContextPtr)) {
1963 return nullptr;
1964 }
1965 napi_value result = nullptr;
1966 if (asyncContextPtr->callbackRef == nullptr) {
1967 DLP_LOG_DEBUG(LABEL, "Create promise");
1968 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
1969 } else {
1970 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
1971 NAPI_CALL(env, napi_get_undefined(env, &result));
1972 }
1973 napi_value resource = nullptr;
1974 NAPI_CALL(env, napi_create_string_utf8(env, "QueryDlpPolicy", NAPI_AUTO_LENGTH, &resource));
1975 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, DecryptDlpFileExcute,
1976 DecryptDlpFileComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
1977 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContextPtr->work, napi_qos_user_initiated));
1978 asyncContextPtr.release();
1979 return result;
1980 }
1981
DecryptDlpFileExcute(napi_env env,void * data)1982 void NapiDlpPermission::DecryptDlpFileExcute(napi_env env, void* data)
1983 {
1984 DLP_LOG_DEBUG(LABEL, "QueryDlpPolicy start run.");
1985 auto asyncContext = reinterpret_cast<DecryptDlpFileAsyncContext*>(data);
1986 if (asyncContext == nullptr) {
1987 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
1988 return;
1989 }
1990
1991 asyncContext->errCode = EnterpriseSpaceDlpPermissionKit::GetInstance()->DecryptDlpFile(
1992 asyncContext->plainFileFd, asyncContext->dlpFd);
1993 }
1994
DecryptDlpFileComplete(napi_env env,napi_status status,void * data)1995 void NapiDlpPermission::DecryptDlpFileComplete(napi_env env, napi_status status, void* data)
1996 {
1997 DLP_LOG_DEBUG(LABEL, "DecryptDlpFile start run.");
1998 auto asyncContext = reinterpret_cast<DecryptDlpFileAsyncContext*>(data);
1999 if (asyncContext == nullptr) {
2000 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
2001 return;
2002 }
2003 std::unique_ptr<DecryptDlpFileAsyncContext> asyncContextPtr { asyncContext };
2004 napi_value resJs = nullptr;
2005 if (asyncContext->errCode == DLP_OK) {
2006 NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &resJs));
2007 }
2008
2009 ProcessCallbackOrPromise(env, asyncContext, resJs);
2010 }
2011
QueryDlpPolicy(napi_env env,napi_callback_info cbInfo)2012 napi_value NapiDlpPermission::QueryDlpPolicy(napi_env env, napi_callback_info cbInfo)
2013 {
2014 if (!IsSystemApp(env)) {
2015 return nullptr;
2016 }
2017 if (!CheckPermission(env, PERMISSION_ENTERPRISE_ACCESS_DLP_FILE)) {
2018 return nullptr;
2019 }
2020 auto asyncContextPtr = std::make_unique<QueryDlpPolicyAsyncContext>(env);
2021
2022 if (!GetQueryDlpPolicyParam(env, cbInfo, *asyncContextPtr)) {
2023 return nullptr;
2024 }
2025 napi_value result = nullptr;
2026 if (asyncContextPtr->callbackRef == nullptr) {
2027 DLP_LOG_DEBUG(LABEL, "Create promise");
2028 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
2029 } else {
2030 DLP_LOG_DEBUG(LABEL, "Undefined the result parameter");
2031 NAPI_CALL(env, napi_get_undefined(env, &result));
2032 }
2033 napi_value resource = nullptr;
2034 NAPI_CALL(env, napi_create_string_utf8(env, "QueryDlpPolicy", NAPI_AUTO_LENGTH, &resource));
2035 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, QueryDlpPolicyExcute,
2036 QueryDlpPolicyComplete, static_cast<void*>(asyncContextPtr.get()), &(asyncContextPtr->work)));
2037 NAPI_CALL(env, napi_queue_async_work_with_qos(env, asyncContextPtr->work, napi_qos_user_initiated));
2038 asyncContextPtr.release();
2039 return result;
2040 }
2041
QueryDlpPolicyExcute(napi_env env,void * data)2042 void NapiDlpPermission::QueryDlpPolicyExcute(napi_env env, void* data)
2043 {
2044 DLP_LOG_DEBUG(LABEL, "QueryDlpPolicy start run.");
2045 auto asyncContext = reinterpret_cast<QueryDlpPolicyAsyncContext*>(data);
2046 if (asyncContext == nullptr) {
2047 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
2048 return;
2049 }
2050
2051 asyncContext->errCode = EnterpriseSpaceDlpPermissionKit::GetInstance()->QueryDlpFileProperty(
2052 asyncContext->dlpFd, asyncContext->policyJsonString);
2053 }
2054
QueryDlpPolicyComplete(napi_env env,napi_status status,void * data)2055 void NapiDlpPermission::QueryDlpPolicyComplete(napi_env env, napi_status status, void* data)
2056 {
2057 DLP_LOG_DEBUG(LABEL, "QueryDlpPolicy start run.");
2058 auto asyncContext = reinterpret_cast<QueryDlpPolicyAsyncContext*>(data);
2059 if (asyncContext == nullptr) {
2060 DLP_LOG_ERROR(LABEL, "AsyncContext is nullptr.");
2061 return;
2062 }
2063 std::unique_ptr<QueryDlpPolicyAsyncContext> asyncContextPtr { asyncContext };
2064 napi_value resJs = nullptr;
2065 if (asyncContext->errCode == DLP_OK) {
2066 NAPI_CALL_RETURN_VOID(
2067 env, napi_create_string_utf8(env, asyncContextPtr->policyJsonString.c_str(), NAPI_AUTO_LENGTH, &resJs));
2068 }
2069
2070 ProcessCallbackOrPromise(env, asyncContext, resJs);
2071 }
2072
InitFunction(napi_env env,napi_value exports)2073 void NapiDlpPermission::InitFunction(napi_env env, napi_value exports)
2074 {
2075 napi_property_descriptor desc[] = {
2076 DECLARE_NAPI_FUNCTION("isDLPFile", IsDlpFile),
2077 DECLARE_NAPI_FUNCTION("getDLPPermissionInfo", GetDLPPermissionInfo),
2078 DECLARE_NAPI_FUNCTION("getDLPSuffix", GetDLPSuffix),
2079 DECLARE_NAPI_FUNCTION("getOriginalFileName", GetOriginalFileName),
2080 DECLARE_NAPI_FUNCTION("isInSandbox", IsInSandbox),
2081 DECLARE_NAPI_FUNCTION("getDlpSupportFileType", GetDlpSupportFileType),
2082 DECLARE_NAPI_FUNCTION("getDLPSupportedFileTypes", GetDlpSupportFileType),
2083 DECLARE_NAPI_FUNCTION("setRetentionState", SetRetentionState),
2084 DECLARE_NAPI_FUNCTION("cancelRetentionState", CancelRetentionState),
2085 DECLARE_NAPI_FUNCTION("getRetentionSandboxList", GetRetentionSandboxList),
2086 DECLARE_NAPI_FUNCTION("getDLPFileAccessRecords", GetDLPFileVisitRecord),
2087 DECLARE_NAPI_FUNCTION("startDLPManagerForResult", StartDLPManagerForResult),
2088
2089 DECLARE_NAPI_FUNCTION("generateDLPFile", GenerateDlpFile),
2090 DECLARE_NAPI_FUNCTION("openDLPFile", OpenDlpFile),
2091 DECLARE_NAPI_FUNCTION("installDLPSandbox", InstallDlpSandbox),
2092 DECLARE_NAPI_FUNCTION("uninstallDLPSandbox", UninstallDlpSandbox),
2093 DECLARE_NAPI_FUNCTION("on", Subscribe),
2094 DECLARE_NAPI_FUNCTION("off", UnSubscribe),
2095 DECLARE_NAPI_FUNCTION("getDLPGatheringPolicy", GetDlpGatheringPolicy),
2096 DECLARE_NAPI_FUNCTION("setSandboxAppConfig", SetSandboxAppConfig),
2097 DECLARE_NAPI_FUNCTION("cleanSandboxAppConfig", CleanSandboxAppConfig),
2098 DECLARE_NAPI_FUNCTION("getSandboxAppConfig", GetSandboxAppConfig),
2099 DECLARE_NAPI_FUNCTION("isDLPFeatureProvided", IsDLPFeatureProvided),
2100
2101 DECLARE_NAPI_FUNCTION("generateDlpFileForEnterprise", GenerateDlpFileForEnterprise),
2102 DECLARE_NAPI_FUNCTION("decryptDlpFile", DecryptDlpFile),
2103 DECLARE_NAPI_FUNCTION("queryDlpPolicy", QueryDlpPolicy),
2104 };
2105 NAPI_CALL_RETURN_VOID(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[PARAM0]), desc));
2106 }
2107
Init(napi_env env,napi_value exports)2108 napi_value NapiDlpPermission::Init(napi_env env, napi_value exports)
2109 {
2110 InitFunction(env, exports);
2111 napi_property_descriptor descriptor[] = {DECLARE_NAPI_FUNCTION("DLPFile", DlpFile)};
2112 NAPI_CALL(
2113 env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
2114
2115 napi_property_descriptor properties[] = {
2116 DECLARE_NAPI_FUNCTION("addDLPLinkFile", AddDlpLinkFile),
2117 DECLARE_NAPI_FUNCTION("stopFuseLink", StopDlpLinkFile),
2118 DECLARE_NAPI_FUNCTION("resumeFuseLink", RestartDlpLinkFile),
2119 DECLARE_NAPI_FUNCTION("replaceDLPLinkFile", ReplaceDlpLinkFile),
2120 DECLARE_NAPI_FUNCTION("deleteDLPLinkFile", DeleteDlpLinkFile),
2121 DECLARE_NAPI_FUNCTION("recoverDLPFile", RecoverDlpFile),
2122 DECLARE_NAPI_FUNCTION("closeDLPFile", CloseDlpFile),
2123 };
2124
2125 napi_value constructor = nullptr;
2126 NAPI_CALL(env, napi_define_class(env, DLP_FILE_CLASS_NAME.c_str(), DLP_FILE_CLASS_NAME.size(), JsConstructor,
2127 nullptr, sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor));
2128
2129 NAPI_CALL(env, napi_create_reference(env, constructor, 1, &dlpFileRef_));
2130 NAPI_CALL(env, napi_set_named_property(env, exports, DLP_FILE_CLASS_NAME.c_str(), constructor));
2131
2132 napi_property_descriptor descriptors[] = {
2133 DECLARE_NAPI_PROPERTY("ActionFlagType", CreateEnumActionFlags(env)),
2134 DECLARE_NAPI_PROPERTY("DLPFileAccess", CreateEnumDLPFileAccess(env)),
2135 DECLARE_NAPI_PROPERTY("AccountType", CreateEnumAccountType(env)),
2136 DECLARE_NAPI_PROPERTY("GatheringPolicyType", CreateEnumGatheringPolicy(env)),
2137 DECLARE_NAPI_PROPERTY("ActionType", CreateEnumActionType(env)),
2138 };
2139 napi_define_properties(env, exports, sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors);
2140
2141 int32_t result = AccessToken::AccessTokenKit::VerifyAccessToken(GetSelfTokenID(),
2142 "ohos.permission.ACCESS_DLP_FILE", false);
2143 if (result == AccessToken::TypePermissionState::PERMISSION_GRANTED) {
2144 DLP_LOG_INFO(LABEL, "Check dlp permission success, start init dlp link manager.");
2145 DlpPermission::DlpFuseHelper::GetDlpLinkManagerInstance();
2146 }
2147 return exports;
2148 }
2149
JsConstructor(napi_env env,napi_callback_info cbinfo)2150 napi_value NapiDlpPermission::JsConstructor(napi_env env, napi_callback_info cbinfo)
2151 {
2152 napi_value thisVar = nullptr;
2153 size_t argc = PARAM_SIZE_TWO;
2154 napi_value argv[PARAM_SIZE_TWO] = {nullptr};
2155 NAPI_CALL(env, napi_get_cb_info(env, cbinfo, &argc, argv, &thisVar, nullptr));
2156 int64_t nativeObjAddr;
2157 if (!GetInt64Value(env, argv[PARAM0], nativeObjAddr)) {
2158 return nullptr;
2159 }
2160
2161 auto obj = reinterpret_cast<class DlpFile*>(nativeObjAddr);
2162 if (obj == nullptr) {
2163 DLP_LOG_ERROR(LABEL, "obj is nullptr");
2164 return nullptr;
2165 }
2166 napi_status wrapStatus = napi_wrap(env, thisVar, obj,
2167 [](napi_env env, void* data, void* hint) {
2168 DLP_LOG_INFO(LABEL, "native obj destructed by js callback");
2169 return;
2170 },
2171 nullptr, nullptr);
2172 if (wrapStatus != napi_ok) {
2173 DLP_LOG_ERROR(LABEL, "Wrap js and native option failed");
2174 } else {
2175 DLP_LOG_INFO(LABEL, "native obj construct");
2176 }
2177 if (argc < PARAM_SIZE_TWO) {
2178 DLP_LOG_ERROR(LABEL, "property is null");
2179 }
2180 NAPI_CALL(env, napi_set_named_property(env, thisVar, "dlpProperty", argv[PARAM1]));
2181
2182 return thisVar;
2183 }
2184 } // namespace DlpPermission
2185 } // namespace Security
2186 } // namespace OHOS
2187
2188 EXTERN_C_START
2189 /*
2190 * function for module exports
2191 */
Init(napi_env env,napi_value exports)2192 static napi_value Init(napi_env env, napi_value exports)
2193 {
2194 DLP_LOG_DEBUG(OHOS::Security::DlpPermission::LABEL, "Register end, start init.");
2195
2196 return OHOS::Security::DlpPermission::NapiDlpPermission::Init(env, exports);
2197 }
2198 EXTERN_C_END
2199
2200 /*
2201 * Module define
2202 */
2203 static napi_module _module = {.nm_version = 1,
2204 .nm_flags = 0,
2205 .nm_filename = nullptr,
2206 .nm_register_func = Init,
2207 .nm_modname = "dlpPermission",
2208 .nm_priv = ((void*)0),
2209 .reserved = {0}};
2210
2211 /*
2212 * Module register function
2213 */
DlpPermissionModuleRegister(void)2214 extern "C" __attribute__((constructor)) void DlpPermissionModuleRegister(void)
2215 {
2216 napi_module_register(&_module);
2217 }
2218