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