• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "napi_common.h"
17 #include <algorithm>
18 #include <unistd.h>
19 #include "dlp_file_kits.h"
20 #include "dlp_permission.h"
21 #include "dlp_permission_log.h"
22 #include "napi_error_msg.h"
23 #include "securec.h"
24 #include "string_wrapper.h"
25 #include "want_params_wrapper.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace DlpPermission {
30 namespace {
31 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpPermissionCommon"};
32 static const int MAX_FILE_NAME_LEN = 256;
33 const std::string PARAM_UI_EXTENSION_TYPE = "ability.want.params.uiExtensionType";
34 const std::string SYS_COMMON_UI = "sys/commonUI";
35 const std::string DLP_MANAGER_BUNDLENAME = "com.ohos.dlpmanager";
36 const std::string DLP_MANAGER_ABILITYNAME = "MainAbilityEx";
37 
ConvertDlpSandboxChangeInfo(napi_env env,napi_value value,const DlpSandboxCallbackInfo & result)38 static bool ConvertDlpSandboxChangeInfo(napi_env env, napi_value value, const DlpSandboxCallbackInfo &result)
39 {
40     napi_value element;
41     NAPI_CALL_BASE(env, napi_create_int32(env, result.appIndex, &element), false);
42     NAPI_CALL_BASE(env, napi_set_named_property(env, value, "appIndex", element), false);
43     element = nullptr;
44     NAPI_CALL_BASE(env, napi_create_string_utf8(env, result.bundleName.c_str(), NAPI_AUTO_LENGTH, &element), false);
45     NAPI_CALL_BASE(env, napi_set_named_property(env, value, "bundleName", element), false);
46     return true;
47 };
48 
UvQueueWorkDlpSandboxChanged(uv_work_t * work,int status)49 static void UvQueueWorkDlpSandboxChanged(uv_work_t *work, int status)
50 {
51     DLP_LOG_INFO(LABEL, "enter UvQueueWorkDlpSandboxChanged");
52     if ((work == nullptr) || (work->data == nullptr)) {
53         DLP_LOG_ERROR(LABEL, "work == nullptr || work->data == nullptr");
54         return;
55     }
56     std::unique_ptr<uv_work_t> uvWorkPtr { work };
57     RegisterDlpSandboxChangeWorker *registerSandboxChangeData =
58         reinterpret_cast<RegisterDlpSandboxChangeWorker *>(work->data);
59     std::unique_ptr<RegisterDlpSandboxChangeWorker> workPtr { registerSandboxChangeData };
60     napi_handle_scope scope = nullptr;
61     napi_open_handle_scope(registerSandboxChangeData->env, &scope);
62     if (scope == nullptr) {
63         DLP_LOG_ERROR(LABEL, "scope is nullptr");
64         return;
65     }
66     napi_value result = { nullptr };
67     NAPI_CALL_RETURN_VOID_WITH_SCOPE(registerSandboxChangeData->env,
68         napi_create_array(registerSandboxChangeData->env, &result), scope);
69     if (!ConvertDlpSandboxChangeInfo(registerSandboxChangeData->env, result, registerSandboxChangeData->result)) {
70         napi_close_handle_scope(registerSandboxChangeData->env, scope);
71         DLP_LOG_ERROR(LABEL, "ConvertDlpSandboxChangeInfo failed");
72         return;
73     }
74 
75     napi_value undefined = nullptr;
76     napi_value callback = nullptr;
77     napi_value resultout = nullptr;
78     NAPI_CALL_RETURN_VOID_WITH_SCOPE(registerSandboxChangeData->env,
79         napi_get_undefined(registerSandboxChangeData->env, &undefined), scope);
80     NAPI_CALL_RETURN_VOID_WITH_SCOPE(registerSandboxChangeData->env,
81         napi_get_reference_value(registerSandboxChangeData->env, registerSandboxChangeData->ref, &callback), scope);
82     NAPI_CALL_RETURN_VOID_WITH_SCOPE(registerSandboxChangeData->env,
83         napi_call_function(registerSandboxChangeData->env, undefined, callback, 1, &result, &resultout), scope);
84     napi_close_handle_scope(registerSandboxChangeData->env, scope);
85     DLP_LOG_DEBUG(LABEL, "UvQueueWorkDlpSandboxChanged end");
86 };
87 
ConvertOpenDlpFileCallbackInfo(napi_env env,napi_value value,const OpenDlpFileCallbackInfo & result)88 static bool ConvertOpenDlpFileCallbackInfo(napi_env env, napi_value value, const OpenDlpFileCallbackInfo &result)
89 {
90     napi_value element = nullptr;
91     NAPI_CALL_BASE(env, napi_create_string_utf8(env, result.uri.c_str(), NAPI_AUTO_LENGTH, &element), false);
92     NAPI_CALL_BASE(env, napi_set_named_property(env, value, "uri", element), false);
93     element = nullptr;
94     NAPI_CALL_BASE(env, napi_create_bigint_uint64(env, result.timeStamp, &element), false);
95     NAPI_CALL_BASE(env, napi_set_named_property(env, value, "lastOpenTime", element), false);
96     return true;
97 };
98 
UvQueueWorkOpenDlpFile(uv_work_t * work,int status)99 static void UvQueueWorkOpenDlpFile(uv_work_t *work, int status)
100 {
101     DLP_LOG_INFO(LABEL, "enter UvQueueWorkOpenDlpFile");
102     if ((work == nullptr) || (work->data == nullptr)) {
103         DLP_LOG_ERROR(LABEL, "work == nullptr || work->data == nullptr");
104         return;
105     }
106     std::unique_ptr<uv_work_t> uvWorkPtr { work };
107     OpenDlpFileSubscriberWorker *oepnDlpFileDate =
108         reinterpret_cast<OpenDlpFileSubscriberWorker *>(work->data);
109     std::unique_ptr<OpenDlpFileSubscriberWorker> workPtr { oepnDlpFileDate };
110     napi_handle_scope scope = nullptr;
111     napi_open_handle_scope(oepnDlpFileDate->env, &scope);
112     if (scope == nullptr) {
113         DLP_LOG_ERROR(LABEL, "scope is nullptr");
114         return;
115     }
116     napi_value result = { nullptr };
117     NAPI_CALL_RETURN_VOID_WITH_SCOPE(oepnDlpFileDate->env,
118         napi_create_array(oepnDlpFileDate->env, &result), scope);
119     if (!ConvertOpenDlpFileCallbackInfo(oepnDlpFileDate->env, result, oepnDlpFileDate->result)) {
120         napi_close_handle_scope(oepnDlpFileDate->env, scope);
121         DLP_LOG_ERROR(LABEL, "ConvertOpenDlpFileCallbackInfo failed");
122         return;
123     }
124 
125     napi_value undefined = nullptr;
126     napi_value callback = nullptr;
127     napi_value resultout = nullptr;
128     NAPI_CALL_RETURN_VOID_WITH_SCOPE(oepnDlpFileDate->env,
129         napi_get_undefined(oepnDlpFileDate->env, &undefined), scope);
130     NAPI_CALL_RETURN_VOID_WITH_SCOPE(oepnDlpFileDate->env,
131         napi_get_reference_value(oepnDlpFileDate->env, oepnDlpFileDate->ref, &callback), scope);
132     NAPI_CALL_RETURN_VOID_WITH_SCOPE(oepnDlpFileDate->env,
133         napi_call_function(oepnDlpFileDate->env, undefined, callback, 1, &result, &resultout), scope);
134     napi_close_handle_scope(oepnDlpFileDate->env, scope);
135     DLP_LOG_INFO(LABEL, "UvQueueWorkOpenDlpFile end");
136 };
137 } // namespace
138 
RegisterDlpSandboxChangeScopePtr()139 RegisterDlpSandboxChangeScopePtr::RegisterDlpSandboxChangeScopePtr() {}
140 
~RegisterDlpSandboxChangeScopePtr()141 RegisterDlpSandboxChangeScopePtr::~RegisterDlpSandboxChangeScopePtr() {}
142 
DlpSandboxChangeCallback(DlpSandboxCallbackInfo & result)143 void RegisterDlpSandboxChangeScopePtr::DlpSandboxChangeCallback(DlpSandboxCallbackInfo &result)
144 {
145     DLP_LOG_INFO(LABEL, "enter DlpSandboxChangeCallback");
146     std::lock_guard<std::mutex> lock(validMutex_);
147     if (!valid_) {
148         DLP_LOG_ERROR(LABEL, "object is invalid.");
149         return;
150     }
151     uv_loop_s *loop = nullptr;
152     NAPI_CALL_RETURN_VOID(env_, napi_get_uv_event_loop(env_, &loop));
153     if (loop == nullptr) {
154         DLP_LOG_ERROR(LABEL, "loop instance is nullptr");
155         return;
156     }
157     uv_work_t *work = new (std::nothrow) uv_work_t;
158     if (work == nullptr) {
159         DLP_LOG_ERROR(LABEL, "insufficient memory for work!");
160         return;
161     }
162     std::unique_ptr<uv_work_t> uvWorkPtr { work };
163     RegisterDlpSandboxChangeWorker *registerSandboxChangeWorker = new (std::nothrow) RegisterDlpSandboxChangeWorker();
164     if (registerSandboxChangeWorker == nullptr) {
165         DLP_LOG_ERROR(LABEL, "insufficient memory for RegisterDlpSandboxChangeWorker!");
166         return;
167     }
168     std::unique_ptr<RegisterDlpSandboxChangeWorker> workPtr { registerSandboxChangeWorker };
169     registerSandboxChangeWorker->env = env_;
170     registerSandboxChangeWorker->ref = ref_;
171     registerSandboxChangeWorker->result = result;
172     DLP_LOG_DEBUG(LABEL, "result appIndex = %{public}d, bundleName = %{public}s", result.appIndex,
173         result.bundleName.c_str());
174     registerSandboxChangeWorker->subscriber = this;
175     work->data = reinterpret_cast<void *>(registerSandboxChangeWorker);
176     NAPI_CALL_RETURN_VOID(env_, uv_queue_work(
177         loop, work, [](uv_work_t *work) {}, UvQueueWorkDlpSandboxChanged));
178     uvWorkPtr.release();
179     workPtr.release();
180 }
181 
SetEnv(const napi_env & env)182 void RegisterDlpSandboxChangeScopePtr::SetEnv(const napi_env &env)
183 {
184     env_ = env;
185 }
186 
SetCallbackRef(const napi_ref & ref)187 void RegisterDlpSandboxChangeScopePtr::SetCallbackRef(const napi_ref &ref)
188 {
189     ref_ = ref;
190 }
191 
SetValid(bool valid)192 void RegisterDlpSandboxChangeScopePtr::SetValid(bool valid)
193 {
194     std::lock_guard<std::mutex> lock(validMutex_);
195     valid_ = valid;
196 }
197 
~DlpSandboxChangeContext()198 DlpSandboxChangeContext::~DlpSandboxChangeContext()
199 {
200     if (callbackRef == nullptr) {
201         return;
202     }
203     DeleteNapiRef();
204 }
205 
DeleteNapiRef()206 void DlpSandboxChangeContext::DeleteNapiRef()
207 {
208     DLP_LOG_INFO(LABEL, "enter DeleteNapiRef");
209     uv_loop_s *loop = nullptr;
210     NAPI_CALL_RETURN_VOID(env, napi_get_uv_event_loop(env, &loop));
211     if (loop == nullptr) {
212         DLP_LOG_ERROR(LABEL, "loop instance is nullptr");
213         return;
214     }
215     uv_work_t *work = new (std::nothrow) uv_work_t;
216     if (work == nullptr) {
217         DLP_LOG_ERROR(LABEL, "insufficient memory for work!");
218         return;
219     }
220 
221     std::unique_ptr<uv_work_t> uvWorkPtr { work };
222     RegisterDlpSandboxChangeWorker *registerSandboxChangeWorker = new (std::nothrow) RegisterDlpSandboxChangeWorker();
223     if (registerSandboxChangeWorker == nullptr) {
224         DLP_LOG_ERROR(LABEL, "insufficient memory for registerSandboxChangeWorker!");
225         return;
226     }
227     std::unique_ptr<RegisterDlpSandboxChangeWorker> workPtr { registerSandboxChangeWorker };
228     registerSandboxChangeWorker->env = env;
229     registerSandboxChangeWorker->ref = callbackRef;
230 
231     work->data = reinterpret_cast<void *>(registerSandboxChangeWorker);
232     NAPI_CALL_RETURN_VOID(env, uv_queue_work(
233         loop, work, [](uv_work_t *work) {}, UvQueueWorkDeleteRef));
234     DLP_LOG_DEBUG(LABEL, "DeleteNapiRef");
235     uvWorkPtr.release();
236     workPtr.release();
237 }
238 
UvQueueWorkDeleteRef(uv_work_t * work,int32_t status)239 void UvQueueWorkDeleteRef(uv_work_t *work, int32_t status)
240 {
241     DLP_LOG_INFO(LABEL, "enter UvQueueWorkDeleteRef");
242     if (work == nullptr) {
243         DLP_LOG_ERROR(LABEL, "work == nullptr : %{public}d", work == nullptr);
244         return;
245     } else if (work->data == nullptr) {
246         DLP_LOG_ERROR(LABEL, "work->data == nullptr : %{public}d", work->data == nullptr);
247         return;
248     }
249     RegisterDlpSandboxChangeWorker *registerSandboxChangeWorker =
250         reinterpret_cast<RegisterDlpSandboxChangeWorker *>(work->data);
251     if (registerSandboxChangeWorker == nullptr) {
252         delete work;
253         return;
254     }
255     napi_delete_reference(registerSandboxChangeWorker->env, registerSandboxChangeWorker->ref);
256     delete registerSandboxChangeWorker;
257     registerSandboxChangeWorker = nullptr;
258     delete work;
259     DLP_LOG_DEBUG(LABEL, "UvQueueWorkDeleteRef end");
260 }
261 
OpenDlpFileSubscriberPtr()262 OpenDlpFileSubscriberPtr::OpenDlpFileSubscriberPtr() {}
263 
~OpenDlpFileSubscriberPtr()264 OpenDlpFileSubscriberPtr::~OpenDlpFileSubscriberPtr() {}
265 
OnOpenDlpFile(OpenDlpFileCallbackInfo & result)266 void OpenDlpFileSubscriberPtr::OnOpenDlpFile(OpenDlpFileCallbackInfo &result)
267 {
268     DLP_LOG_INFO(LABEL, "enter OnOpenDlpFile");
269     std::lock_guard<std::mutex> lock(validMutex_);
270     if (!valid_) {
271         DLP_LOG_ERROR(LABEL, "object is invalid.");
272         return;
273     }
274     uv_loop_s *loop = nullptr;
275     NAPI_CALL_RETURN_VOID(env_, napi_get_uv_event_loop(env_, &loop));
276     if (loop == nullptr) {
277         DLP_LOG_ERROR(LABEL, "loop instance is nullptr");
278         return;
279     }
280     uv_work_t *work = new (std::nothrow) uv_work_t;
281     if (work == nullptr) {
282         DLP_LOG_ERROR(LABEL, "insufficient memory for work!");
283         return;
284     }
285     std::unique_ptr<uv_work_t> uvWorkPtr { work };
286     OpenDlpFileSubscriberWorker *openDlpFileWorker = new (std::nothrow) OpenDlpFileSubscriberWorker();
287     if (openDlpFileWorker == nullptr) {
288         DLP_LOG_ERROR(LABEL, "insufficient memory for OpenDlpFileSubscriberWorker!");
289         return;
290     }
291     std::unique_ptr<OpenDlpFileSubscriberWorker> workPtr { openDlpFileWorker };
292     openDlpFileWorker->env = env_;
293     openDlpFileWorker->ref = ref_;
294     openDlpFileWorker->result = result;
295     DLP_LOG_DEBUG(LABEL, "result uri = %{public}s, openTime = %{public}" PRIu64, result.uri.c_str(),
296         result.timeStamp);
297     openDlpFileWorker->subscriber = this;
298     work->data = reinterpret_cast<void *>(openDlpFileWorker);
299     NAPI_CALL_RETURN_VOID(env_, uv_queue_work(
300         loop, work, [](uv_work_t *work) {}, UvQueueWorkOpenDlpFile));
301     uvWorkPtr.release();
302     workPtr.release();
303 }
304 
SetEnv(const napi_env & env)305 void OpenDlpFileSubscriberPtr::SetEnv(const napi_env &env)
306 {
307     env_ = env;
308 }
309 
SetCallbackRef(const napi_ref & ref)310 void OpenDlpFileSubscriberPtr::SetCallbackRef(const napi_ref &ref)
311 {
312     ref_ = ref;
313 }
314 
SetValid(bool valid)315 void OpenDlpFileSubscriberPtr::SetValid(bool valid)
316 {
317     std::lock_guard<std::mutex> lock(validMutex_);
318     valid_ = valid;
319 }
320 
~OpenDlpFileSubscriberContext()321 OpenDlpFileSubscriberContext::~OpenDlpFileSubscriberContext()
322 {
323     if (callbackRef == nullptr) {
324         return;
325     }
326     DeleteNapiRef();
327 }
328 
OpenDlpFileUvQueueWorkDeleteRef(uv_work_t * work,int32_t status)329 void OpenDlpFileUvQueueWorkDeleteRef(uv_work_t *work, int32_t status)
330 {
331     DLP_LOG_INFO(LABEL, "enter OpenDlpFileUvQueueWorkDeleteRef");
332     if (work == nullptr) {
333         DLP_LOG_ERROR(LABEL, "work == nullptr : %{public}d", work == nullptr);
334         return;
335     } else if (work->data == nullptr) {
336         DLP_LOG_ERROR(LABEL, "work->data == nullptr : %{public}d", work->data == nullptr);
337         return;
338     }
339     OpenDlpFileSubscriberWorker *openDlpFileWorker =
340         reinterpret_cast<OpenDlpFileSubscriberWorker *>(work->data);
341     if (openDlpFileWorker == nullptr) {
342         delete work;
343         return;
344     }
345     napi_delete_reference(openDlpFileWorker->env, openDlpFileWorker->ref);
346     delete openDlpFileWorker;
347     openDlpFileWorker = nullptr;
348     delete work;
349     DLP_LOG_INFO(LABEL, "OpenDlpFileUvQueueWorkDeleteRef end");
350 }
351 
DeleteNapiRef()352 void OpenDlpFileSubscriberContext::DeleteNapiRef()
353 {
354     DLP_LOG_INFO(LABEL, "enter DeleteNapiRef");
355     uv_loop_s *loop = nullptr;
356     NAPI_CALL_RETURN_VOID(env, napi_get_uv_event_loop(env, &loop));
357     if (loop == nullptr) {
358         DLP_LOG_ERROR(LABEL, "loop instance is nullptr");
359         return;
360     }
361     uv_work_t *work = new (std::nothrow) uv_work_t;
362     if (work == nullptr) {
363         DLP_LOG_ERROR(LABEL, "insufficient memory for work!");
364         return;
365     }
366 
367     std::unique_ptr<uv_work_t> uvWorkPtr { work };
368     OpenDlpFileSubscriberWorker *openDlpFileWorker = new (std::nothrow) OpenDlpFileSubscriberWorker();
369     if (openDlpFileWorker == nullptr) {
370         DLP_LOG_ERROR(LABEL, "insufficient memory for openDlpFileWorker!");
371         return;
372     }
373     std::unique_ptr<OpenDlpFileSubscriberWorker> workPtr { openDlpFileWorker };
374     openDlpFileWorker->env = env;
375     openDlpFileWorker->ref = callbackRef;
376 
377     work->data = reinterpret_cast<void *>(openDlpFileWorker);
378     NAPI_CALL_RETURN_VOID(env, uv_queue_work(
379         loop, work, [](uv_work_t *work) {}, OpenDlpFileUvQueueWorkDeleteRef));
380     DLP_LOG_DEBUG(LABEL, "DeleteNapiRef");
381     uvWorkPtr.release();
382     workPtr.release();
383 }
384 
GenerateBusinessError(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)385 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
386 {
387     napi_value errCodeJs = nullptr;
388     NAPI_CALL(env, napi_create_uint32(env, jsErrCode, &errCodeJs));
389 
390     napi_value errMsgJs = nullptr;
391     NAPI_CALL(env, napi_create_string_utf8(env, jsErrMsg.c_str(), NAPI_AUTO_LENGTH, &errMsgJs));
392 
393     napi_value errJs = nullptr;
394     NAPI_CALL(env, napi_create_error(env, nullptr, errMsgJs, &errJs));
395     NAPI_CALL(env, napi_set_named_property(env, errJs, "code", errCodeJs));
396     NAPI_CALL(env, napi_set_named_property(env, errJs, "message", errMsgJs));
397     return errJs;
398 }
399 
DlpNapiThrow(napi_env env,int32_t nativeErrCode)400 void DlpNapiThrow(napi_env env, int32_t nativeErrCode)
401 {
402     int32_t jsErrCode = NativeCodeToJsCode(nativeErrCode);
403     NAPI_CALL_RETURN_VOID(env, napi_throw(env, GenerateBusinessError(env, jsErrCode, GetJsErrMsg(jsErrCode))));
404 }
405 
DlpNapiThrow(napi_env env,int32_t jsErrCode,const std::string & jsErrMsg)406 void DlpNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg)
407 {
408     NAPI_CALL_RETURN_VOID(env, napi_throw(env, GenerateBusinessError(env, jsErrCode, jsErrMsg)));
409 }
410 
ThrowParamError(const napi_env env,const std::string & param,const std::string & type)411 void ThrowParamError(const napi_env env, const std::string& param, const std::string& type)
412 {
413     std::string msg = "Parameter Error. The type of \"" + param + "\" must be " + type + ".";
414     DlpNapiThrow(env, ERR_JS_PARAMETER_ERROR, msg);
415 }
416 
CommonAsyncContext(napi_env napiEnv)417 CommonAsyncContext::CommonAsyncContext(napi_env napiEnv)
418 {
419     env = napiEnv;
420 }
421 
~CommonAsyncContext()422 CommonAsyncContext::~CommonAsyncContext()
423 {
424     if (callbackRef) {
425         DLP_LOG_DEBUG(LABEL, "~CommonAsyncContext delete callbackRef");
426         napi_delete_reference(env, callbackRef);
427         callbackRef = nullptr;
428     }
429     if (work) {
430         DLP_LOG_DEBUG(LABEL, "~CommonAsyncContext delete work");
431         napi_delete_async_work(env, work);
432         work = nullptr;
433     }
434 }
435 
CreateEnumDLPFileAccess(napi_env env)436 napi_value CreateEnumDLPFileAccess(napi_env env)
437 {
438     napi_value authPerm = nullptr;
439     NAPI_CALL(env, napi_create_object(env, &authPerm));
440 
441     napi_value prop = nullptr;
442     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(NO_PERMISSION), &prop));
443     NAPI_CALL(env, napi_set_named_property(env, authPerm, "NO_PERMISSION", prop));
444 
445     prop = nullptr;
446     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(READ_ONLY), &prop));
447     NAPI_CALL(env, napi_set_named_property(env, authPerm, "READ_ONLY", prop));
448 
449     prop = nullptr;
450     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(CONTENT_EDIT), &prop));
451     NAPI_CALL(env, napi_set_named_property(env, authPerm, "CONTENT_EDIT", prop));
452 
453     prop = nullptr;
454     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(FULL_CONTROL), &prop));
455     NAPI_CALL(env, napi_set_named_property(env, authPerm, "FULL_CONTROL", prop));
456 
457     return authPerm;
458 }
459 
CreateEnumAccountType(napi_env env)460 napi_value CreateEnumAccountType(napi_env env)
461 {
462     napi_value accountType = nullptr;
463     NAPI_CALL(env, napi_create_object(env, &accountType));
464 
465     napi_value prop = nullptr;
466     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(CLOUD_ACCOUNT), &prop));
467     NAPI_CALL(env, napi_set_named_property(env, accountType, "CLOUD_ACCOUNT", prop));
468 
469     prop = nullptr;
470     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(DOMAIN_ACCOUNT), &prop));
471     NAPI_CALL(env, napi_set_named_property(env, accountType, "DOMAIN_ACCOUNT", prop));
472 
473     prop = nullptr;
474     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(APPLICATION_ACCOUNT), &prop));
475     NAPI_CALL(env, napi_set_named_property(env, accountType, "APPLICATION_ACCOUNT", prop));
476 
477     return accountType;
478 }
479 
CreateEnumActionFlags(napi_env env)480 napi_value CreateEnumActionFlags(napi_env env)
481 {
482     napi_value actionFlags = nullptr;
483     NAPI_CALL(env, napi_create_object(env, &actionFlags));
484 
485     napi_value prop = nullptr;
486     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_INVALID), &prop));
487     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_INVALID", prop));
488 
489     prop = nullptr;
490     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_VIEW), &prop));
491     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_VIEW", prop));
492 
493     prop = nullptr;
494     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_SAVE), &prop));
495     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_SAVE", prop));
496 
497     prop = nullptr;
498     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_SAVE_AS), &prop));
499     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_SAVE_AS", prop));
500 
501     prop = nullptr;
502     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_EDIT), &prop));
503     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_EDIT", prop));
504 
505     prop = nullptr;
506     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_SCREEN_CAPTURE), &prop));
507     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_SCREEN_CAPTURE", prop));
508 
509     prop = nullptr;
510     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_SCREEN_SHARE), &prop));
511     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_SCREEN_SHARE", prop));
512 
513     prop = nullptr;
514     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_SCREEN_RECORD), &prop));
515     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_SCREEN_RECORD", prop));
516 
517     prop = nullptr;
518     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_COPY), &prop));
519     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_COPY", prop));
520 
521     prop = nullptr;
522     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_PRINT), &prop));
523     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_PRINT", prop));
524 
525     prop = nullptr;
526     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_EXPORT), &prop));
527     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_EXPORT", prop));
528 
529     prop = nullptr;
530     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(ACTION_PERMISSION_CHANGE), &prop));
531     NAPI_CALL(env, napi_set_named_property(env, actionFlags, "ACTION_PERMISSION_CHANGE", prop));
532     return actionFlags;
533 }
534 
CreateEnumGatheringPolicy(napi_env env)535 napi_value CreateEnumGatheringPolicy(napi_env env)
536 {
537     napi_value gatheringPolicy = nullptr;
538     NAPI_CALL(env, napi_create_object(env, &gatheringPolicy));
539 
540     napi_value prop = nullptr;
541     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(GATHERING), &prop));
542     NAPI_CALL(env, napi_set_named_property(env, gatheringPolicy, "GATHERING", prop));
543 
544     prop = nullptr;
545     NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(NON_GATHERING), &prop));
546     NAPI_CALL(env, napi_set_named_property(env, gatheringPolicy, "NON_GATHERING", prop));
547 
548     return gatheringPolicy;
549 }
550 
ProcessCallbackOrPromise(napi_env env,const CommonAsyncContext * asyncContext,napi_value data)551 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data)
552 {
553     size_t argc = PARAM_SIZE_TWO;
554     napi_value args[PARAM_SIZE_TWO] = {nullptr};
555 
556     if (asyncContext->errCode == DLP_OK) {
557         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &args[PARAM0]));
558         args[PARAM1] = data;
559     } else {
560         int32_t jsErrCode = NativeCodeToJsCode(asyncContext->errCode);
561         napi_value errObj = GenerateBusinessError(env, jsErrCode, GetJsErrMsg(jsErrCode));
562         if (data != nullptr && (asyncContext->errCode == DLP_CREDENTIAL_ERROR_NO_PERMISSION_ERROR ||
563             asyncContext->errCode == DLP_CREDENTIAL_ERROR_TIME_EXPIRED)) {
564             std::string errContacter;
565             if (!GetStringValue(env, data, errContacter)) {
566                 DLP_LOG_ERROR(LABEL, "js get contacter data fail");
567                 ThrowParamError(env, "contacter data", "string");
568                 return ;
569             }
570             std::string errMessage = GetJsErrMsg(jsErrCode) + ", contact:" + errContacter;
571             napi_value jsMessageStr;
572             napi_value jsErrMessage;
573             NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, "message", NAPI_AUTO_LENGTH, &jsMessageStr));
574             NAPI_CALL_RETURN_VOID(env,
575                 napi_create_string_utf8(env, errMessage.c_str(), NAPI_AUTO_LENGTH, &jsErrMessage));
576             NAPI_CALL_RETURN_VOID(env, napi_delete_property(env, errObj, jsMessageStr, nullptr));
577             NAPI_CALL_RETURN_VOID(env, napi_set_property(env, errObj, jsMessageStr, jsErrMessage));
578         }
579         args[PARAM0] = errObj;
580         NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &args[PARAM1]));
581     }
582 
583     if (asyncContext->deferred) {
584         DLP_LOG_DEBUG(LABEL, "Promise");
585         if (asyncContext->errCode == DLP_OK) {
586             NAPI_CALL_RETURN_VOID(env, napi_resolve_deferred(env, asyncContext->deferred, args[PARAM1]));
587         } else {
588             DLP_LOG_ERROR(LABEL, "Promise reject, errCode=%{public}d", asyncContext->errCode);
589             NAPI_CALL_RETURN_VOID(env, napi_reject_deferred(env, asyncContext->deferred, args[PARAM0]));
590         }
591     } else {
592         DLP_LOG_DEBUG(LABEL, "Callback");
593         napi_value callback = nullptr;
594         NAPI_CALL_RETURN_VOID(env, napi_get_reference_value(env, asyncContext->callbackRef, &callback));
595         napi_value returnVal = nullptr;
596         NAPI_CALL_RETURN_VOID(env, napi_call_function(env, nullptr, callback, argc, &args[PARAM0], &returnVal));
597     }
598 }
599 
NapiCheckArgc(const napi_env env,int32_t argc,int32_t reqSize)600 bool NapiCheckArgc(const napi_env env, int32_t argc, int32_t reqSize)
601 {
602     if (argc < (reqSize - 1)) {
603         DLP_LOG_ERROR(LABEL, "params number mismatch");
604         std::string errMsg = "Parameter Error. Params number mismatch, need at least " + std::to_string(reqSize - 1) +
605             ", given " + std::to_string(argc);
606         DlpNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg);
607         return false;
608     }
609     return true;
610 }
611 
GetGenerateDlpFileParams(const napi_env env,const napi_callback_info info,GenerateDlpFileAsyncContext & asyncContext)612 bool GetGenerateDlpFileParams(
613     const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext)
614 {
615     size_t argc = PARAM_SIZE_FOUR;
616     napi_value argv[PARAM_SIZE_FOUR] = {nullptr};
617     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
618 
619     if (!NapiCheckArgc(env, argc, PARAM_SIZE_FOUR)) {
620         return false;
621     }
622 
623     if (!GetInt64Value(env, argv[PARAM0], asyncContext.plaintextFd)) {
624         DLP_LOG_ERROR(LABEL, "js get plain fd fail");
625         ThrowParamError(env, "plaintextFd", "number");
626         return false;
627     }
628     if (!GetInt64Value(env, argv[PARAM1], asyncContext.ciphertextFd)) {
629         DLP_LOG_ERROR(LABEL, "js get cipher fd fail");
630         ThrowParamError(env, "ciphertextFd", "number");
631         return false;
632     }
633 
634     if (!GetDlpProperty(env, argv[PARAM2], asyncContext.property)) {
635         DLP_LOG_ERROR(LABEL, "js get property fail");
636         ThrowParamError(env, "property", "DlpProperty");
637         return false;
638     }
639 
640     if (argc == PARAM_SIZE_FOUR) {
641         if (!ParseCallback(env, argv[PARAM3], asyncContext.callbackRef)) {
642             ThrowParamError(env, "callback", "function");
643             return false;
644         }
645     }
646 
647     DLP_LOG_DEBUG(LABEL,
648         "Fd: %{private}" PRId64 ",ownerAccount:%{private}s,ownerAccountId: %{private}s, ownerAccountType: %{private}d,"
649         "contactAccount: %{private}s, size: %{private}zu, expireTime: %{public}" PRId64,
650         asyncContext.plaintextFd, asyncContext.property.ownerAccount.c_str(),
651         asyncContext.property.ownerAccountId.c_str(), asyncContext.property.ownerAccountType,
652         asyncContext.property.contactAccount.c_str(), asyncContext.property.authUsers.size(),
653         asyncContext.property.expireTime);
654     return true;
655 }
656 
GetOpenDlpFileParams(const napi_env env,const napi_callback_info info,DlpFileAsyncContext & asyncContext)657 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext)
658 {
659     size_t argc = PARAM_SIZE_THREE;
660     napi_value argv[PARAM_SIZE_THREE] = {nullptr};
661     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
662 
663     if (!NapiCheckArgc(env, argc, PARAM_SIZE_THREE)) {
664         return false;
665     }
666 
667     if (!GetInt64Value(env, argv[PARAM0], asyncContext.ciphertextFd)) {
668         DLP_LOG_ERROR(LABEL, "js get cipher fd fail");
669         ThrowParamError(env, "ciphertextFd", "number");
670         return false;
671     }
672 
673     if (!GetStringValue(env, argv[PARAM1], asyncContext.appId)) {
674         DLP_LOG_ERROR(LABEL, "js get appId fail");
675         ThrowParamError(env, "appId", "string");
676         return false;
677     }
678 
679     if (argc == PARAM_SIZE_THREE) {
680         if (!ParseCallback(env, argv[PARAM2], asyncContext.callbackRef)) {
681             ThrowParamError(env, "callback", "function");
682             return false;
683         }
684     }
685 
686     DLP_LOG_DEBUG(LABEL, "Fd: %{private}" PRId64, asyncContext.ciphertextFd);
687     return true;
688 }
689 
GetIsDlpFileParams(const napi_env env,const napi_callback_info info,DlpFileAsyncContext & asyncContext)690 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext)
691 {
692     size_t argc = PARAM_SIZE_TWO;
693     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
694     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
695 
696     if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
697         return false;
698     }
699 
700     if (!GetInt64Value(env, argv[PARAM0], asyncContext.ciphertextFd)) {
701         DLP_LOG_ERROR(LABEL, "js get cipher fd fail");
702         ThrowParamError(env, "fd", "number");
703         return false;
704     }
705 
706     if (asyncContext.ciphertextFd < 0) {
707         DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, GetJsErrMsg(ERR_JS_INVALID_PARAMETER));
708         return false;
709     }
710 
711     if (argc == PARAM_SIZE_TWO) {
712         if (!ParseCallback(env, argv[PARAM1], asyncContext.callbackRef)) {
713             ThrowParamError(env, "callback", "function");
714             return false;
715         }
716     }
717 
718     DLP_LOG_DEBUG(LABEL, "Fd: %{private}" PRId64, asyncContext.ciphertextFd);
719     return true;
720 }
721 
GetDlpLinkFileParams(const napi_env env,const napi_callback_info info,DlpLinkFileAsyncContext & asyncContext)722 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext)
723 {
724     napi_value thisVar = nullptr;
725     size_t argc = PARAM_SIZE_TWO;
726     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
727     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr), false);
728     if (thisVar == nullptr) {
729         DLP_LOG_ERROR(LABEL, "This var is null");
730         return false;
731     }
732 
733     if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
734         return false;
735     }
736 
737     NAPI_CALL_BASE(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext.dlpFileNative)), false);
738     if (asyncContext.dlpFileNative == nullptr) {
739         DLP_LOG_ERROR(LABEL, "cannot get native object");
740         return false;
741     }
742 
743     if (!GetStringValue(env, argv[PARAM0], asyncContext.linkFileName)) {
744         DLP_LOG_ERROR(LABEL, "linkFileName is invalid");
745         ThrowParamError(env, "linkFileName", "string");
746         return false;
747     }
748 
749     if (argc == PARAM_SIZE_TWO) {
750         if (!ParseCallback(env, argv[PARAM1], asyncContext.callbackRef)) {
751             ThrowParamError(env, "callback", "function");
752             return false;
753         }
754     }
755 
756     DLP_LOG_DEBUG(LABEL, "linkFileName: %{private}s", asyncContext.linkFileName.c_str());
757     return true;
758 }
759 
GetLinkFileStatusParams(const napi_env env,const napi_callback_info info,DlpLinkFileAsyncContext & asyncContext)760 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext)
761 {
762     napi_value thisVar = nullptr;
763     size_t argc = PARAM_SIZE_ONE;
764     napi_value argv[PARAM_SIZE_ONE] = {nullptr};
765     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr), false);
766     if (thisVar == nullptr) {
767         DLP_LOG_ERROR(LABEL, "This var is null");
768         return false;
769     }
770 
771     if (!NapiCheckArgc(env, argc, PARAM_SIZE_ONE)) {
772         return false;
773     }
774 
775     NAPI_CALL_BASE(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext.dlpFileNative)), false);
776     if (asyncContext.dlpFileNative == nullptr) {
777         DLP_LOG_ERROR(LABEL, "cannot get native object");
778         return false;
779     }
780 
781     if (argc == PARAM_SIZE_ONE) {
782         if (!ParseCallback(env, argv[PARAM0], asyncContext.callbackRef)) {
783             ThrowParamError(env, "callback", "function");
784             return false;
785         }
786     }
787 
788     return true;
789 }
790 
GetRecoverDlpFileParams(const napi_env env,const napi_callback_info info,RecoverDlpFileAsyncContext & asyncContext)791 bool GetRecoverDlpFileParams(
792     const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext)
793 {
794     napi_value thisVar = nullptr;
795     size_t argc = PARAM_SIZE_TWO;
796     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
797     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr), false);
798     if (thisVar == nullptr) {
799         DLP_LOG_ERROR(LABEL, "This var is null");
800         return false;
801     }
802 
803     if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
804         return false;
805     }
806 
807     NAPI_CALL_BASE(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext.dlpFileNative)), false);
808     if (asyncContext.dlpFileNative == nullptr) {
809         DLP_LOG_ERROR(LABEL, "cannot get native object");
810         return false;
811     }
812 
813     if (!GetInt64Value(env, argv[PARAM0], asyncContext.plaintextFd)) {
814         DLP_LOG_ERROR(LABEL, "js get cipher fd fail");
815         ThrowParamError(env, "plaintextFd", "number");
816         return false;
817     }
818 
819     if (argc == PARAM_SIZE_TWO) {
820         if (!ParseCallback(env, argv[PARAM1], asyncContext.callbackRef)) {
821             ThrowParamError(env, "callback", "function");
822             return false;
823         }
824     }
825 
826     DLP_LOG_DEBUG(LABEL, "plaintextFd: %{private}" PRId64, asyncContext.plaintextFd);
827     return true;
828 }
829 
GetCloseDlpFileParams(const napi_env env,const napi_callback_info info,CloseDlpFileAsyncContext & asyncContext)830 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext)
831 {
832     napi_value thisVar = nullptr;
833     size_t argc = PARAM_SIZE_ONE;
834     napi_value argv[PARAM_SIZE_ONE] = {nullptr};
835     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr), false);
836     if (thisVar == nullptr) {
837         DLP_LOG_ERROR(LABEL, "This var is null");
838         return false;
839     }
840 
841     NAPI_CALL_BASE(env, napi_unwrap(env, thisVar, reinterpret_cast<void**>(&asyncContext.dlpFileNative)), false);
842     if (asyncContext.dlpFileNative == nullptr) {
843         DLP_LOG_ERROR(LABEL, "cannot get native object");
844         return false;
845     }
846 
847     if (argc == PARAM_SIZE_ONE) {
848         if (!ParseCallback(env, argv[PARAM0], asyncContext.callbackRef)) {
849             ThrowParamError(env, "callback", "function");
850             return false;
851         }
852     }
853 
854     return true;
855 }
856 
GetInstallDlpSandboxParams(const napi_env env,const napi_callback_info info,DlpSandboxAsyncContext & asyncContext)857 bool GetInstallDlpSandboxParams(const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext)
858 {
859     size_t argc = PARAM_SIZE_FIVE;
860     napi_value argv[PARAM_SIZE_FIVE] = {nullptr};
861     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
862 
863     if (!NapiCheckArgc(env, argc, PARAM_SIZE_FIVE)) {
864         return false;
865     }
866 
867     if (!GetStringValue(env, argv[PARAM0], asyncContext.bundleName)) {
868         DLP_LOG_ERROR(LABEL, "js get bundle name fail");
869         ThrowParamError(env, "bundleName", "string");
870         return false;
871     }
872     int64_t res;
873     if (!GetInt64Value(env, argv[PARAM1], res)) {
874         DLP_LOG_ERROR(LABEL, "js get perm fail");
875         ThrowParamError(env, "access", "number");
876         return false;
877     }
878     asyncContext.dlpFileAccess = static_cast<DLPFileAccess>(res);
879     if (!GetInt64Value(env, argv[PARAM2], res)) {
880         DLP_LOG_ERROR(LABEL, "js get user id fail");
881         ThrowParamError(env, "userId", "number");
882         return false;
883     }
884     asyncContext.userId = static_cast<int32_t>(res);
885     if (!GetStringValue(env, argv[PARAM3], asyncContext.uri)) {
886         DLP_LOG_ERROR(LABEL, "js get uri fail");
887         ThrowParamError(env, "uri", "string");
888         return false;
889     }
890 
891     if (argc == PARAM_SIZE_FIVE) {
892         if (!ParseCallback(env, argv[PARAM4], asyncContext.callbackRef)) {
893             ThrowParamError(env, "callback", "function");
894             return false;
895         }
896     }
897 
898     DLP_LOG_DEBUG(LABEL, "bundleName: %{private}s, dlpFileAccess: %{private}d, userId: %{private}d,uri: %{private}s",
899         asyncContext.bundleName.c_str(), asyncContext.dlpFileAccess, asyncContext.userId, asyncContext.uri.c_str());
900     return true;
901 }
902 
GetUninstallDlpSandboxParams(const napi_env env,const napi_callback_info info,DlpSandboxAsyncContext & asyncContext)903 bool GetUninstallDlpSandboxParams(
904     const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext)
905 {
906     size_t argc = PARAM_SIZE_FOUR;
907     napi_value argv[PARAM_SIZE_FOUR] = {nullptr};
908     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
909 
910     if (!NapiCheckArgc(env, argc, PARAM_SIZE_FOUR)) {
911         return false;
912     }
913 
914     if (!GetStringValue(env, argv[PARAM0], asyncContext.bundleName)) {
915         DLP_LOG_ERROR(LABEL, "js get bundle name fail");
916         ThrowParamError(env, "bundleName", "string");
917         return false;
918     }
919 
920     int64_t res;
921     if (!GetInt64Value(env, argv[PARAM1], res)) {
922         DLP_LOG_ERROR(LABEL, "js get user id fail");
923         ThrowParamError(env, "userId", "number");
924         return false;
925     }
926     asyncContext.userId = static_cast<int32_t>(res);
927 
928     if (!GetInt64Value(env, argv[PARAM2], res)) {
929         DLP_LOG_ERROR(LABEL, "js get app index fail");
930         ThrowParamError(env, "appIndex", "number");
931         return false;
932     }
933     asyncContext.sandboxInfo.appIndex = static_cast<int32_t>(res);
934 
935     if (argc == PARAM_SIZE_FOUR) {
936         if (!ParseCallback(env, argv[PARAM3], asyncContext.callbackRef)) {
937             ThrowParamError(env, "callback", "function");
938             return false;
939         }
940     }
941 
942     DLP_LOG_DEBUG(LABEL, "bundleName: %{private}s, userId: %{private}d, appIndex: %{private}d",
943         asyncContext.bundleName.c_str(), asyncContext.userId, asyncContext.sandboxInfo.appIndex);
944     return true;
945 }
946 
ParseInputToRegister(const napi_env env,const napi_callback_info cbInfo,RegisterDlpSandboxChangeInfo & registerSandboxChangeInfo)947 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo,
948     RegisterDlpSandboxChangeInfo &registerSandboxChangeInfo)
949 {
950     size_t argc = PARAM_SIZE_TWO;
951     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
952     napi_value thisVar = nullptr;
953     NAPI_CALL_BASE(env, napi_get_cb_info(env, cbInfo, &argc, argv, &thisVar, nullptr), false);
954     if (argc < PARAM_SIZE_TWO) {
955         ThrowParamError(env, "params", " missing.");
956         return false;
957     }
958     if (thisVar == nullptr) {
959         DLP_LOG_ERROR(LABEL, "thisVar is nullptr");
960         return false;
961     }
962     napi_valuetype valueTypeOfThis = napi_undefined;
963     NAPI_CALL_BASE(env, napi_typeof(env, thisVar, &valueTypeOfThis), false);
964     if (valueTypeOfThis == napi_undefined) {
965         DLP_LOG_ERROR(LABEL, "thisVar is undefined");
966         return false;
967     }
968     // 0: the first parameter of argv
969     std::string type;
970     if (!GetStringValue(env, argv[0], type)) {
971         ThrowParamError(env, "type", "string");
972         return false;
973     }
974 
975     if (type.compare(ON_OFF_SANDBOX) != 0) {
976         ThrowParamError(env, "type", "uninstallsandbox");
977         return false;
978     }
979 
980     if (!FillDlpSandboxChangeInfo(env, argv, type, thisVar, registerSandboxChangeInfo)) {
981         return false;
982     }
983 
984     return true;
985 }
986 
FillDlpSandboxChangeInfo(const napi_env env,const napi_value * argv,const std::string & type,const napi_value thisVar,RegisterDlpSandboxChangeInfo & registerSandboxChangeInfo)987 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value *argv, const std::string &type,
988     const napi_value thisVar, RegisterDlpSandboxChangeInfo &registerSandboxChangeInfo)
989 {
990     std::string errMsg;
991     napi_ref callback = nullptr;
992 
993     // 1: the second parameter of argv
994     if (!ParseCallback(env, argv[1], callback)) {
995         napi_throw(env, GenerateBusinessError(env, ERR_JS_PARAMETER_ERROR, "callback is wrong"));
996         return false;
997     }
998 
999     registerSandboxChangeInfo.env = env;
1000     registerSandboxChangeInfo.callbackRef = callback;
1001     registerSandboxChangeInfo.changeType = type;
1002     registerSandboxChangeInfo.subscriber = std::make_shared<RegisterDlpSandboxChangeScopePtr>();
1003     registerSandboxChangeInfo.subscriber->SetEnv(env);
1004     registerSandboxChangeInfo.subscriber->SetCallbackRef(callback);
1005     std::shared_ptr<RegisterDlpSandboxChangeScopePtr> *subscriber =
1006         new (std::nothrow) std::shared_ptr<RegisterDlpSandboxChangeScopePtr>(registerSandboxChangeInfo.subscriber);
1007     if (subscriber == nullptr) {
1008         DLP_LOG_ERROR(LABEL, "failed to create subscriber");
1009         return false;
1010     }
1011     napi_wrap(
1012         env, thisVar, reinterpret_cast<void *>(subscriber),
1013         [](napi_env nev, void *data, void *hint) {
1014             DLP_LOG_DEBUG(LABEL, "RegisterDlpSandboxChangeScopePtr delete");
1015             std::shared_ptr<RegisterDlpSandboxChangeScopePtr> *subscriber =
1016                 static_cast<std::shared_ptr<RegisterDlpSandboxChangeScopePtr> *>(data);
1017             if (subscriber != nullptr && *subscriber != nullptr) {
1018                 (*subscriber)->SetValid(false);
1019                 delete subscriber;
1020             }
1021         },
1022         nullptr, nullptr);
1023     return true;
1024 }
1025 
GetUnregisterSandboxParams(const napi_env env,const napi_callback_info info,UnregisterSandboxChangeCallbackAsyncContext & asyncContext)1026 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info,
1027     UnregisterSandboxChangeCallbackAsyncContext &asyncContext)
1028 {
1029     DLP_LOG_INFO(LABEL, "enter GetUnregisterSandboxParams");
1030     size_t argc = PARAM_SIZE_TWO;
1031     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
1032     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1033 
1034     if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
1035         return false;
1036     }
1037 
1038     if (!GetStringValue(env, argv[PARAM0], asyncContext.changeType)) {
1039         DLP_LOG_ERROR(LABEL, "js get changeType fail");
1040         ThrowParamError(env, "changeType", "string");
1041         return false;
1042     }
1043 
1044     if (asyncContext.changeType.compare(ON_OFF_SANDBOX) != 0) {
1045         ThrowParamError(env, "type", "uninstallsandbox");
1046         return false;
1047     }
1048 
1049     DLP_LOG_DEBUG(LABEL, "changeType: %{private}s", asyncContext.changeType.c_str());
1050     return true;
1051 }
1052 
GetRetentionStateParams(const napi_env env,const napi_callback_info info,RetentionStateAsyncContext & asyncContext)1053 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info,
1054     RetentionStateAsyncContext& asyncContext)
1055 {
1056     size_t argc = PARAM_SIZE_TWO;
1057     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
1058     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1059 
1060     if (!NapiCheckArgc(env, argc, PARAM_SIZE_TWO)) {
1061         return false;
1062     }
1063 
1064     if (!GetVectorDocUriByKey(env, argv[PARAM0], "docUris", asyncContext.docUris)) {
1065         DLP_LOG_ERROR(LABEL, "js get auth users fail");
1066         return false;
1067     }
1068 
1069     if (argc == PARAM_SIZE_TWO) {
1070         if (!ParseCallback(env, argv[PARAM1], asyncContext.callbackRef)) {
1071             ThrowParamError(env, "callback", "function");
1072             return false;
1073         }
1074     }
1075 
1076     DLP_LOG_DEBUG(LABEL, "docUriVec size: %{private}zu", asyncContext.docUris.size());
1077     return true;
1078 }
1079 
GetRetentionSandboxListParams(const napi_env env,const napi_callback_info info,GetRetentionSandboxListAsyncContext & asyncContext)1080 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info,
1081     GetRetentionSandboxListAsyncContext& asyncContext)
1082 {
1083     size_t argc = PARAM_SIZE_TWO;
1084     napi_value argv[PARAM_SIZE_TWO] = {nullptr};
1085     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1086     if (argc == PARAM_SIZE_TWO) {
1087         if (!ParseCallback(env, argv[PARAM1], asyncContext.callbackRef)) {
1088             ThrowParamError(env, "callback", "function");
1089             return false;
1090         }
1091         if (!GetStringValue(env, argv[PARAM0], asyncContext.bundleName)) {
1092             DLP_LOG_ERROR(LABEL, "js get bundle name fail");
1093             ThrowParamError(env, "bundleName", "string");
1094             return false;
1095         }
1096     }
1097     if (argc == PARAM_SIZE_ONE) {
1098         if (!GetStringValue(env, argv[PARAM0], asyncContext.bundleName) &&
1099             !ParseCallback(env, argv[PARAM0], asyncContext.callbackRef)) {
1100             DLP_LOG_ERROR(LABEL, "js get bundle name or callback fail");
1101             ThrowParamError(env, "bundleName or callback", "string or function");
1102             return false;
1103         }
1104     }
1105     return true;
1106 }
1107 
GetOriginalFilenameParams(const napi_env env,const napi_callback_info info,GetOriginalFileAsyncContext & asyncContext)1108 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info,
1109     GetOriginalFileAsyncContext& asyncContext)
1110 {
1111     size_t argc = PARAM_SIZE_ONE;
1112     napi_value argv[PARAM_SIZE_ONE] = {nullptr};
1113     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1114 
1115     if (argc == PARAM_SIZE_ONE) {
1116         if (!GetStringValue(env, argv[PARAM0], asyncContext.dlpFilename)) {
1117             ThrowParamError(env, "fileName", "string");
1118             return false;
1119         }
1120     }
1121 
1122     std::string filename = asyncContext.dlpFilename;
1123     size_t size = filename.size();
1124     if ((size <= DLP_FILE_SUFFIX.size()) || (size > MAX_FILE_NAME_LEN)) {
1125         DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, GetJsErrMsg(ERR_JS_INVALID_PARAMETER));
1126         return false;
1127     }
1128 
1129     if (filename.substr(filename.size() - DLP_FILE_SUFFIX.size()) != DLP_FILE_SUFFIX) {
1130         DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, GetJsErrMsg(ERR_JS_INVALID_PARAMETER));
1131         return false;
1132     }
1133     return true;
1134 }
1135 
GetSandboxAppConfigParams(const napi_env env,const napi_callback_info info,SandboxAppConfigAsyncContext * asyncContext)1136 bool GetSandboxAppConfigParams(const napi_env env, const napi_callback_info info,
1137     SandboxAppConfigAsyncContext* asyncContext)
1138 {
1139     size_t argc = PARAM_SIZE_ONE;
1140     napi_value argv[PARAM_SIZE_ONE] = {nullptr};
1141     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1142     if (!NapiCheckArgc(env, argc, PARAM_SIZE_ONE)) {
1143         return false;
1144     }
1145     if (!GetStringValue(env, argv[PARAM0], asyncContext->configInfo)) {
1146         ThrowParamError(env, "config", "string");
1147         return false;
1148     }
1149     if (asyncContext->configInfo.empty()) {
1150         DlpNapiThrow(env, ERR_JS_INVALID_PARAMETER, GetJsErrMsg(ERR_JS_INVALID_PARAMETER));
1151         return false;
1152     }
1153 
1154     return true;
1155 }
1156 
GetThirdInterfaceParams(const napi_env env,const napi_callback_info info,CommonAsyncContext & asyncContext)1157 bool GetThirdInterfaceParams(
1158     const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext)
1159 {
1160     size_t argc = PARAM_SIZE_ONE;
1161     napi_value argv[PARAM_SIZE_ONE] = {nullptr};
1162     NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
1163 
1164     if (argc == PARAM_SIZE_ONE) {
1165         if (!ParseCallback(env, argv[PARAM0], asyncContext.callbackRef)) {
1166             ThrowParamError(env, "callback", "function");
1167             return false;
1168         }
1169     }
1170     return true;
1171 }
1172 
GetDlpPropertyExpireTime(napi_env env,napi_value jsObject,DlpProperty & property)1173 void GetDlpPropertyExpireTime(napi_env env, napi_value jsObject, DlpProperty& property)
1174 {
1175     int64_t jsExpireTime = 0;
1176     if (!GetInt64ValueByKey(env, jsObject, "expireTime", jsExpireTime)) {
1177         DLP_LOG_INFO(LABEL, "js get expity time fail, set zero");
1178     }
1179     property.expireTime = static_cast<uint64_t>(jsExpireTime);
1180 }
1181 
GetDlpProperty(napi_env env,napi_value jsObject,DlpProperty & property)1182 bool GetDlpProperty(napi_env env, napi_value jsObject, DlpProperty& property)
1183 {
1184     if (!GetStringValueByKey(env, jsObject, "ownerAccount", property.ownerAccount)) {
1185         DLP_LOG_ERROR(LABEL, "js get owner account fail");
1186         return false;
1187     }
1188     if (!GetStringValueByKey(env, jsObject, "ownerAccountID", property.ownerAccountId)) {
1189         DLP_LOG_ERROR(LABEL, "js get owner accountId fail");
1190         return false;
1191     }
1192     int64_t type;
1193     if (!GetInt64ValueByKey(env, jsObject, "ownerAccountType", type)) {
1194         DLP_LOG_ERROR(LABEL, "js get owner account type fail");
1195         return false;
1196     }
1197     property.ownerAccountType = static_cast<DlpAccountType>(type);
1198     napi_value authUserListObj = GetNapiValue(env, jsObject, "authUserList");
1199     if (authUserListObj != nullptr) {
1200         if (!GetVectorAuthUser(env, authUserListObj, property.authUsers)) {
1201             DLP_LOG_ERROR(LABEL, "js get auth users fail");
1202             return false;
1203         }
1204     }
1205     if (!GetStringValueByKey(env, jsObject, "contactAccount", property.contactAccount)) {
1206         DLP_LOG_ERROR(LABEL, "js get contact account fail");
1207         return false;
1208     }
1209     if (!GetBoolValueByKey(env, jsObject, "offlineAccess", property.offlineAccess)) {
1210         DLP_LOG_ERROR(LABEL, "js get offline access flag fail");
1211         return false;
1212     }
1213     GetDlpPropertyExpireTime(env, jsObject, property);
1214 
1215     napi_value everyoneAccessListObj = GetNapiValue(env, jsObject, "everyoneAccessList");
1216     if (everyoneAccessListObj != nullptr) {
1217         std::vector<uint32_t> permList = {};
1218         if (!GetVectorUint32(env, everyoneAccessListObj, permList)) {
1219             DLP_LOG_ERROR(LABEL, "js get everyoneAccessList fail");
1220             return false;
1221         }
1222         if (permList.size() > 0) {
1223             uint32_t perm = *(std::max_element(permList.begin(), permList.end()));
1224             property.everyonePerm = static_cast<DLPFileAccess>(perm);
1225             property.supportEveryone = true;
1226         }
1227     }
1228     return true;
1229 }
1230 
RetentionSandboxInfoToJs(napi_env env,const std::vector<RetentionSandBoxInfo> & infoVec)1231 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec)
1232 {
1233     napi_value vectorJs = nullptr;
1234     uint32_t index = 0;
1235     NAPI_CALL(env, napi_create_array(env, &vectorJs));
1236     for (auto item : infoVec) {
1237         napi_value objInfo = nullptr;
1238         NAPI_CALL(env, napi_create_object(env, &objInfo));
1239 
1240         napi_value appIndexJs;
1241         NAPI_CALL(env, napi_create_int32(env, item.appIndex_, &appIndexJs));
1242         NAPI_CALL(env, napi_set_named_property(env, objInfo, "appIndex", appIndexJs));
1243         DLP_LOG_INFO(LABEL, "GetAppIndex %{public}d", item.appIndex_);
1244         napi_value bundleNameJs;
1245         NAPI_CALL(env, napi_create_string_utf8(env, item.bundleName_.c_str(), NAPI_AUTO_LENGTH, &bundleNameJs));
1246         NAPI_CALL(env, napi_set_named_property(env, objInfo, "bundleName", bundleNameJs));
1247 
1248         napi_value docUriVecJs = SetStringToJs(env, item.docUriSet_);
1249         NAPI_CALL(env, napi_set_named_property(env, objInfo, "docUris", docUriVecJs));
1250 
1251         NAPI_CALL(env, napi_set_element(env, vectorJs, index, objInfo));
1252         index++;
1253     }
1254     return vectorJs;
1255 }
1256 
VisitInfoToJs(napi_env env,const std::vector<VisitedDLPFileInfo> & infoVec)1257 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec)
1258 {
1259     napi_value vectorJs = nullptr;
1260     uint32_t index = 0;
1261     NAPI_CALL(env, napi_create_array(env, &vectorJs));
1262     for (auto& item : infoVec) {
1263         napi_value objInfo = nullptr;
1264         NAPI_CALL(env, napi_create_object(env, &objInfo));
1265 
1266         napi_value timestampJs;
1267         NAPI_CALL(env, napi_create_int64(env, item.visitTimestamp, &timestampJs));
1268         NAPI_CALL(env, napi_set_named_property(env, objInfo, "lastOpenTime", timestampJs));
1269         DLP_LOG_DEBUG(LABEL, "Get visitTimestamp %{public}" PRId64, item.visitTimestamp);
1270         napi_value uriJs;
1271         NAPI_CALL(env, napi_create_string_utf8(env, item.docUri.c_str(), NAPI_AUTO_LENGTH, &uriJs));
1272         NAPI_CALL(env, napi_set_named_property(env, objInfo, "uri", uriJs));
1273 
1274         NAPI_CALL(env, napi_set_element(env, vectorJs, index, objInfo));
1275         index++;
1276     }
1277     return vectorJs;
1278 }
1279 
DlpPropertyToJs(napi_env env,const DlpProperty & property)1280 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property)
1281 {
1282     napi_value dlpPropertyJs = nullptr;
1283     NAPI_CALL(env, napi_create_object(env, &dlpPropertyJs));
1284 
1285     napi_value everyoneAccessListJs = nullptr;
1286     if (property.supportEveryone) {
1287         everyoneAccessListJs = VectorUint32ToJs(env, {property.everyonePerm});
1288     } else {
1289         everyoneAccessListJs = VectorUint32ToJs(env, {});
1290     }
1291     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "everyoneAccessList", everyoneAccessListJs));
1292 
1293     napi_value offlineAccessJs;
1294     napi_get_boolean(env, property.offlineAccess, &offlineAccessJs);
1295     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "offlineAccess", offlineAccessJs));
1296 
1297     napi_value expireTimeJs;
1298     napi_create_int64(env, property.expireTime, &expireTimeJs);
1299     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "expireTime", expireTimeJs));
1300 
1301     napi_value ownerAccountJs;
1302     NAPI_CALL(env, napi_create_string_utf8(env, property.ownerAccount.c_str(), NAPI_AUTO_LENGTH, &ownerAccountJs));
1303     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "ownerAccount", ownerAccountJs));
1304 
1305     napi_value ownerAccountIdJs;
1306     NAPI_CALL(env, napi_create_string_utf8(env, property.ownerAccountId.c_str(), NAPI_AUTO_LENGTH, &ownerAccountIdJs));
1307     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "ownerAccountID", ownerAccountIdJs));
1308 
1309     napi_value vectorAuthUserJs = VectorAuthUserToJs(env, property.authUsers);
1310     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "authUserList", vectorAuthUserJs));
1311 
1312     napi_value contractAccountJs;
1313     NAPI_CALL(
1314         env, napi_create_string_utf8(env, property.contactAccount.c_str(), NAPI_AUTO_LENGTH, &contractAccountJs));
1315     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "contactAccount", contractAccountJs));
1316 
1317     napi_value ownerAccountTypeJs;
1318     NAPI_CALL(env, napi_create_int64(env, property.ownerAccountType, &ownerAccountTypeJs));
1319     NAPI_CALL(env, napi_set_named_property(env, dlpPropertyJs, "ownerAccountType", ownerAccountTypeJs));
1320 
1321     return dlpPropertyJs;
1322 }
1323 
DlpPermissionInfoToJs(napi_env env,const DLPPermissionInfo & permInfo)1324 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo)
1325 {
1326     napi_value dlpPermInfoJs = nullptr;
1327     NAPI_CALL(env, napi_create_object(env, &dlpPermInfoJs));
1328 
1329     napi_value accessJs;
1330     NAPI_CALL(env, napi_create_uint32(env, permInfo.dlpFileAccess, &accessJs));
1331     NAPI_CALL(env, napi_set_named_property(env, dlpPermInfoJs, "dlpFileAccess", accessJs));
1332 
1333     napi_value flagsJs;
1334     NAPI_CALL(env, napi_create_uint32(env, permInfo.flags, &flagsJs));
1335     NAPI_CALL(env, napi_set_named_property(env, dlpPermInfoJs, "flags", flagsJs));
1336 
1337     return dlpPermInfoJs;
1338 }
1339 
SandboxInfoToJs(napi_env env,const SandboxInfo & sandboxInfo)1340 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo)
1341 {
1342     napi_value sandboxInfoJs = nullptr;
1343     NAPI_CALL(env, napi_create_object(env, &sandboxInfoJs));
1344 
1345     napi_value appIndexJs;
1346     NAPI_CALL(env, napi_create_int64(env, sandboxInfo.appIndex, &appIndexJs));
1347     NAPI_CALL(env, napi_set_named_property(env, sandboxInfoJs, "appIndex", appIndexJs));
1348 
1349     napi_value tokenIdJs;
1350     NAPI_CALL(env, napi_create_int64(env, sandboxInfo.tokenId, &tokenIdJs));
1351     NAPI_CALL(env, napi_set_named_property(env, sandboxInfoJs, "tokenID", tokenIdJs));
1352 
1353     return sandboxInfoJs;
1354 }
1355 
VectorAuthUserToJs(napi_env env,const std::vector<AuthUserInfo> & users)1356 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users)
1357 {
1358     napi_value vectorAuthUserJs = nullptr;
1359     uint32_t index = 0;
1360     NAPI_CALL(env, napi_create_array(env, &vectorAuthUserJs));
1361     for (auto item : users) {
1362         napi_value objAuthUserInfo = nullptr;
1363         NAPI_CALL(env, napi_create_object(env, &objAuthUserInfo));
1364 
1365         napi_value authAccountJs;
1366         NAPI_CALL(env, napi_create_string_utf8(env, item.authAccount.c_str(), NAPI_AUTO_LENGTH, &authAccountJs));
1367         NAPI_CALL(env, napi_set_named_property(env, objAuthUserInfo, "authAccount", authAccountJs));
1368 
1369         napi_value authPermJs;
1370         NAPI_CALL(env, napi_create_int64(env, item.authPerm, &authPermJs));
1371         NAPI_CALL(env, napi_set_named_property(env, objAuthUserInfo, "dlpFileAccess", authPermJs));
1372 
1373         napi_value permExpiryTimeJs;
1374         NAPI_CALL(env, napi_create_int64(env, item.permExpiryTime, &permExpiryTimeJs));
1375         NAPI_CALL(env, napi_set_named_property(env, objAuthUserInfo, "permExpiryTime", permExpiryTimeJs));
1376 
1377         napi_value authAccountTypeJs;
1378         NAPI_CALL(env, napi_create_int64(env, item.authAccountType, &authAccountTypeJs));
1379         NAPI_CALL(env, napi_set_named_property(env, objAuthUserInfo, "authAccountType", authAccountTypeJs));
1380 
1381         NAPI_CALL(env, napi_set_element(env, vectorAuthUserJs, index, objAuthUserInfo));
1382         index++;
1383     }
1384     return vectorAuthUserJs;
1385 }
1386 
VectorStringToJs(napi_env env,const std::vector<std::string> & value)1387 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value)
1388 {
1389     napi_value jsArray = nullptr;
1390     uint32_t index = 0;
1391     NAPI_CALL(env, napi_create_array(env, &jsArray));
1392     for (const auto& iter : value) {
1393         napi_value jsValue = nullptr;
1394         if (napi_create_string_utf8(env, iter.c_str(), NAPI_AUTO_LENGTH, &jsValue) == napi_ok) {
1395             if (napi_set_element(env, jsArray, index, jsValue) == napi_ok) {
1396                 index++;
1397             }
1398         }
1399     }
1400     return jsArray;
1401 }
1402 
VectorUint32ToJs(napi_env env,const std::vector<uint32_t> & value)1403 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value)
1404 {
1405     napi_value jsArray = nullptr;
1406     uint32_t index = 0;
1407     NAPI_CALL(env, napi_create_array(env, &jsArray));
1408     for (const auto& iter : value) {
1409         napi_value jsValue = nullptr;
1410         if (napi_create_int64(env, iter, &jsValue) == napi_ok) {
1411             if (napi_set_element(env, jsArray, index, jsValue) == napi_ok) {
1412                 index++;
1413             }
1414         }
1415     }
1416     return jsArray;
1417 }
1418 
SetStringToJs(napi_env env,const std::set<std::string> & value)1419 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value)
1420 {
1421     napi_value jsArray = nullptr;
1422     uint32_t index = 0;
1423     NAPI_CALL(env, napi_create_array(env, &jsArray));
1424     for (const auto& iter : value) {
1425         napi_value jsValue = nullptr;
1426         if (napi_create_string_utf8(env, iter.c_str(), NAPI_AUTO_LENGTH, &jsValue) == napi_ok) {
1427             if (napi_set_element(env, jsArray, index, jsValue) == napi_ok) {
1428                 index++;
1429             } else {
1430                 DLP_LOG_ERROR(LABEL, "napi_set_element error index:%{public}d,value:%{private}s", index, iter.c_str());
1431             }
1432         } else {
1433             DLP_LOG_ERROR(LABEL, "napi_create_string_utf8 error index:%{public}d,value:%{private}s", index,
1434                 iter.c_str());
1435         }
1436     }
1437     return jsArray;
1438 }
1439 
ParseCallback(const napi_env & env,const napi_value & value,napi_ref & callbackRef)1440 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef)
1441 {
1442     napi_valuetype valuetype = napi_undefined;
1443     NAPI_CALL_BASE(env, napi_typeof(env, value, &valuetype), false);
1444 
1445     if (valuetype == napi_function) {
1446         NAPI_CALL_BASE(env, napi_create_reference(env, value, 1, &callbackRef), false);
1447         return true;
1448     }
1449     if (valuetype == napi_null || valuetype == napi_undefined) {
1450         return true;
1451     }
1452     return false;
1453 }
1454 
GetNapiValue(napi_env env,napi_value jsObject,const std::string & key)1455 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key)
1456 {
1457     if (jsObject == nullptr) {
1458         DLP_LOG_ERROR(LABEL, "Js object is nullptr");
1459         return nullptr;
1460     }
1461     napi_value keyValue;
1462     NAPI_CALL(env, napi_create_string_utf8(env, key.c_str(), NAPI_AUTO_LENGTH, &keyValue));
1463     bool result = false;
1464     NAPI_CALL(env, napi_has_property(env, jsObject, keyValue, &result));
1465     if (result) {
1466         napi_value value = nullptr;
1467         NAPI_CALL(env, napi_get_property(env, jsObject, keyValue, &value));
1468         return value;
1469     }
1470     DLP_LOG_ERROR(LABEL, "get napi value fail");
1471     return nullptr;
1472 }
1473 
GetStringValue(napi_env env,napi_value jsObject,std::string & result)1474 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result)
1475 {
1476     napi_valuetype valueType = napi_undefined;
1477     if (napi_typeof(env, jsObject, &valueType) != napi_ok) {
1478         DLP_LOG_ERROR(LABEL, "Can not get napi type");
1479         return false;
1480     }
1481     if (valueType != napi_string) {
1482         DLP_LOG_ERROR(LABEL, "object is no a string");
1483         return false;
1484     }
1485 
1486     size_t size = 0;
1487     if (napi_get_value_string_utf8(env, jsObject, nullptr, 0, &size) != napi_ok) {
1488         DLP_LOG_ERROR(LABEL, "Can not get string size");
1489         return false;
1490     }
1491     result.reserve(size + 1);
1492     result.resize(size);
1493     if (napi_get_value_string_utf8(env, jsObject, result.data(), (size + 1), &size) != napi_ok) {
1494         DLP_LOG_ERROR(LABEL, "Can not get string value");
1495         return false;
1496     }
1497     return true;
1498 }
1499 
GetStringValueByKey(napi_env env,napi_value jsObject,const std::string & key,std::string & result)1500 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result)
1501 {
1502     napi_value value = GetNapiValue(env, jsObject, key);
1503     return GetStringValue(env, value, result);
1504 }
1505 
GetBoolValue(napi_env env,napi_value jsObject,bool & result)1506 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result)
1507 {
1508     napi_valuetype valuetype;
1509     if (napi_typeof(env, jsObject, &valuetype) != napi_ok) {
1510         DLP_LOG_ERROR(LABEL, "Can not get napi type");
1511         return false;
1512     }
1513 
1514     if (valuetype != napi_boolean) {
1515         DLP_LOG_ERROR(LABEL, "Wrong argument type. Boolean expected.");
1516         return false;
1517     }
1518 
1519     napi_get_value_bool(env, jsObject, &result);
1520     return true;
1521 }
1522 
GetBoolValueByKey(napi_env env,napi_value jsObject,const std::string & key,bool & result)1523 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result)
1524 {
1525     napi_value value = GetNapiValue(env, jsObject, key);
1526     return GetBoolValue(env, value, result);
1527 }
1528 
GetInt64Value(napi_env env,napi_value jsObject,int64_t & result)1529 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result)
1530 {
1531     napi_valuetype valueType = napi_undefined;
1532     if (napi_typeof(env, jsObject, &valueType) != napi_ok) {
1533         DLP_LOG_ERROR(LABEL, "Can not get napi type");
1534         return false;
1535     }
1536     if (valueType != napi_number) {
1537         DLP_LOG_ERROR(LABEL, "object is no a number");
1538         return false;
1539     }
1540     NAPI_CALL_BASE(env, napi_get_value_int64(env, jsObject, &result), false);
1541     return true;
1542 }
1543 
GetInt64ValueByKey(napi_env env,napi_value jsObject,const std::string & key,int64_t & result)1544 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result)
1545 {
1546     napi_value value = GetNapiValue(env, jsObject, key);
1547     return GetInt64Value(env, value, result);
1548 }
1549 
GetUint32Value(napi_env env,napi_value jsObject,uint32_t & result)1550 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result)
1551 {
1552     napi_valuetype valueType = napi_undefined;
1553     if (napi_typeof(env, jsObject, &valueType) != napi_ok) {
1554         DLP_LOG_ERROR(LABEL, "Can not get napi type");
1555         return false;
1556     }
1557     if (valueType != napi_number) {
1558         DLP_LOG_ERROR(LABEL, "object is no a number");
1559         return false;
1560     }
1561     NAPI_CALL_BASE(env, napi_get_value_uint32(env, jsObject, &result), false);
1562     return true;
1563 }
1564 
GetUint32ValueByKey(napi_env env,napi_value jsObject,const std::string & key,uint32_t & result)1565 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result)
1566 {
1567     napi_value value = GetNapiValue(env, jsObject, key);
1568     return GetUint32Value(env, value, result);
1569 }
1570 
GetArrayValueByKey(napi_env env,napi_value jsObject,const std::string & key)1571 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key)
1572 {
1573     napi_value array = GetNapiValue(env, jsObject, key);
1574     bool isArray = false;
1575     NAPI_CALL(env, napi_is_array(env, array, &isArray));
1576     if (!isArray) {
1577         DLP_LOG_ERROR(LABEL, "value is not array");
1578         return nullptr;
1579     }
1580     return array;
1581 }
1582 
GetVectorAuthUser(napi_env env,napi_value jsObject,std::vector<AuthUserInfo> & resultVec)1583 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec)
1584 {
1585     uint32_t size = 0;
1586     if (napi_get_array_length(env, jsObject, &size) != napi_ok) {
1587         DLP_LOG_ERROR(LABEL, "js get array size fail");
1588         return false;
1589     }
1590     for (uint32_t i = 0; i < size; i++) {
1591         napi_value obj;
1592         NAPI_CALL_BASE(env, napi_get_element(env, jsObject, i, &obj), false);
1593         AuthUserInfo userInfo;
1594         if (!GetStringValueByKey(env, obj, "authAccount", userInfo.authAccount)) {
1595             DLP_LOG_ERROR(LABEL, "js get auth account fail");
1596             resultVec.clear();
1597             return false;
1598         }
1599         int64_t perm;
1600         if (!GetInt64ValueByKey(env, obj, "dlpFileAccess", perm)) {
1601             DLP_LOG_ERROR(LABEL, "js get auth perm fail");
1602             resultVec.clear();
1603             return false;
1604         }
1605         userInfo.authPerm = static_cast<DLPFileAccess>(perm);
1606         int64_t time;
1607         if (!GetInt64ValueByKey(env, obj, "permExpiryTime", time)) {
1608             DLP_LOG_ERROR(LABEL, "js get time fail");
1609             resultVec.clear();
1610             return false;
1611         }
1612         userInfo.permExpiryTime = static_cast<uint64_t>(time);
1613         int64_t type;
1614         if (!GetInt64ValueByKey(env, obj, "authAccountType", type)) {
1615             DLP_LOG_ERROR(LABEL, "js get type fail");
1616             resultVec.clear();
1617             return false;
1618         }
1619         userInfo.authAccountType = static_cast<DlpAccountType>(type);
1620         resultVec.push_back(userInfo);
1621     }
1622     return true;
1623 }
1624 
GetVectorAuthUserByKey(napi_env env,napi_value jsObject,const std::string & key,std::vector<AuthUserInfo> & resultVec)1625 bool GetVectorAuthUserByKey(
1626     napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec)
1627 {
1628     napi_value userArray = GetArrayValueByKey(env, jsObject, key);
1629     if (userArray == nullptr) {
1630         DLP_LOG_ERROR(LABEL, "User array is null");
1631         return false;
1632     }
1633     return GetVectorAuthUser(env, userArray, resultVec);
1634 }
1635 
GetVectorDocUriByKey(napi_env env,napi_value jsObject,const std::string & key,std::vector<std::string> & docUriVec)1636 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key,
1637     std::vector<std::string>& docUriVec)
1638 {
1639     bool isArray = false;
1640     NAPI_CALL_BASE(env, napi_is_array(env, jsObject, &isArray), false);
1641     if (!isArray) {
1642         DLP_LOG_ERROR(LABEL, "value is not array");
1643         return false;
1644     }
1645     uint32_t size = 0;
1646     if (napi_get_array_length(env, jsObject, &size) != napi_ok) {
1647         DLP_LOG_ERROR(LABEL, "js get array size fail");
1648         return false;
1649     }
1650     for (uint32_t i = 0; i < size; i++) {
1651         napi_value obj;
1652         NAPI_CALL_BASE(env, napi_get_element(env, jsObject, i, &obj), false);
1653         std::string docUri;
1654         if (!GetStringValue(env, obj, docUri)) {
1655             DLP_LOG_ERROR(LABEL, "js get docUri fail");
1656             ThrowParamError(env, "docUri", "string");
1657             return false;
1658         }
1659         docUriVec.push_back(docUri);
1660     }
1661     return true;
1662 }
1663 
GetVectorUint32(napi_env env,napi_value jsObject,std::vector<uint32_t> & resultVec)1664 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec)
1665 {
1666     bool isArray = false;
1667     NAPI_CALL_BASE(env, napi_is_array(env, jsObject, &isArray), false);
1668     if (!isArray) {
1669         DLP_LOG_ERROR(LABEL, "value is not array");
1670         return false;
1671     }
1672     uint32_t size = 0;
1673     if (napi_get_array_length(env, jsObject, &size) != napi_ok) {
1674         DLP_LOG_ERROR(LABEL, "js get array size fail");
1675         return false;
1676     }
1677     for (uint32_t i = 0; i < size; i++) {
1678         napi_value obj;
1679         NAPI_CALL_BASE(env, napi_get_element(env, jsObject, i, &obj), false);
1680         uint32_t num;
1681         if (!GetUint32Value(env, obj, num)) {
1682             DLP_LOG_ERROR(LABEL, "js get num fail");
1683             return false;
1684         }
1685         resultVec.emplace_back(num);
1686     }
1687     return true;
1688 }
1689 
ParseUIAbilityContextReq(napi_env env,const napi_value & obj,std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> & abilityContext)1690 bool ParseUIAbilityContextReq(
1691     napi_env env, const napi_value& obj, std::shared_ptr<OHOS::AbilityRuntime::AbilityContext>& abilityContext)
1692 {
1693     bool stageMode = false;
1694     napi_status status = OHOS::AbilityRuntime::IsStageContext(env, obj, stageMode);
1695     if (status != napi_ok || !stageMode) {
1696         DLP_LOG_ERROR(LABEL, "not stage mode");
1697         return false;
1698     }
1699 
1700     auto context = OHOS::AbilityRuntime::GetStageModeContext(env, obj);
1701     if (context == nullptr) {
1702         DLP_LOG_ERROR(LABEL, "get context failed");
1703         return false;
1704     }
1705 
1706     abilityContext = OHOS::AbilityRuntime::Context::ConvertTo<OHOS::AbilityRuntime::AbilityContext>(context);
1707     if (abilityContext == nullptr) {
1708         DLP_LOG_ERROR(LABEL, "get abilityContext failed");
1709         return false;
1710     }
1711     DLP_LOG_DEBUG(LABEL, "end ParseUIAbilityContextReq");
1712     return true;
1713 }
1714 
ParseWantReq(napi_env env,const napi_value & obj,OHOS::AAFwk::Want & requestWant)1715 bool ParseWantReq(napi_env env, const napi_value& obj, OHOS::AAFwk::Want& requestWant)
1716 {
1717     requestWant.SetElementName(DLP_MANAGER_BUNDLENAME, DLP_MANAGER_ABILITYNAME);
1718     std::string uri;
1719     bool ret = GetStringValueByKey(env, obj, "uri", uri);
1720     if (!ret || uri.empty()) {
1721         DLP_LOG_ERROR(LABEL, "get uri failed");
1722         DlpNapiThrow(env, ERR_JS_URI_NOT_EXIST, "uri not exist in want");
1723         return false;
1724     }
1725     requestWant.SetUri(uri);
1726 
1727     napi_value wantParameters = GetNapiValue(env, obj, "parameters");
1728     if (wantParameters == nullptr) {
1729         DLP_LOG_ERROR(LABEL, "get wantParameters failed");
1730         DlpNapiThrow(env, ERR_JS_PARAM_DISPLAY_NAME_NOT_EXIST, "parameters not exist in want");
1731         return false;
1732     }
1733     std::string displayName;
1734     ret = GetStringValueByKey(env, wantParameters, "displayName", displayName);
1735     if (!ret || displayName.empty()) {
1736         DLP_LOG_ERROR(LABEL, "get displayName failed");
1737         DlpNapiThrow(env, ERR_JS_PARAM_DISPLAY_NAME_NOT_EXIST, "displayName not exist in want parameters");
1738         return false;
1739     }
1740     AAFwk::WantParams requestWantParam;
1741     requestWantParam.SetParam("displayName", AAFwk::String::Box(displayName));
1742     AAFwk::WantParams fileNameObj;
1743     fileNameObj.SetParam("name", AAFwk::String::Box(displayName));
1744     requestWantParam.SetParam("fileName", AAFwk::WantParamWrapper::Box(fileNameObj));
1745 
1746     napi_status result = napi_has_named_property(env, wantParameters, "linkFileName", &ret);
1747     if (result == napi_ok && ret) {
1748         napi_value linkFileName = GetNapiValue(env, wantParameters, "linkFileName");
1749         std::string linkFileNameStr;
1750         ret = GetStringValueByKey(env, linkFileName, "name", linkFileNameStr);
1751         if (ret && !linkFileNameStr.empty()) {
1752             AAFwk::WantParams linkFileNameObj;
1753             linkFileNameObj.SetParam("name", AAFwk::String::Box(linkFileNameStr));
1754             requestWantParam.SetParam("linkFileName", AAFwk::WantParamWrapper::Box(linkFileNameObj));
1755             DLP_LOG_DEBUG(LABEL, "set linkFileName");
1756         }
1757     }
1758 
1759     requestWant.SetParams(requestWantParam);
1760     requestWant.SetParam(PARAM_UI_EXTENSION_TYPE, SYS_COMMON_UI);
1761     DLP_LOG_DEBUG(LABEL, "end ParseWantReq");
1762     return true;
1763 }
1764 
StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext)1765 void StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext)
1766 {
1767     DLP_LOG_DEBUG(LABEL, "begin StartUIExtensionAbility");
1768     if (asyncContext == nullptr) {
1769         DLP_LOG_ERROR(LABEL, "asyncContext is null");
1770         return;
1771     }
1772     auto abilityContext = asyncContext->context;
1773     if (abilityContext == nullptr) {
1774         DLP_LOG_ERROR(LABEL, "abilityContext is null");
1775         DlpNapiThrow(asyncContext->env, ERR_JS_INVALID_PARAMETER, "abilityContext is null");
1776         return;
1777     }
1778     auto uiContent = abilityContext->GetUIContent();
1779     if (uiContent == nullptr) {
1780         DLP_LOG_ERROR(LABEL, "uiContent is null");
1781         DlpNapiThrow(asyncContext->env, ERR_JS_INVALID_PARAMETER, "uiContent is null");
1782         return;
1783     }
1784 
1785     auto uiExtCallback = std::make_shared<UIExtensionCallback>(asyncContext);
1786     OHOS::Ace::ModalUIExtensionCallbacks extensionCallbacks = {
1787         std::bind(&UIExtensionCallback::OnRelease, uiExtCallback, std::placeholders::_1),
1788         std::bind(&UIExtensionCallback::OnResult, uiExtCallback, std::placeholders::_1, std::placeholders::_2),
1789         std::bind(&UIExtensionCallback::OnReceive, uiExtCallback, std::placeholders::_1),
1790         std::bind(&UIExtensionCallback::OnError, uiExtCallback, std::placeholders::_1,
1791             std::placeholders::_2, std::placeholders::_3),
1792         std::bind(&UIExtensionCallback::OnRemoteReady, uiExtCallback, std::placeholders::_1),
1793         std::bind(&UIExtensionCallback::OnDestroy, uiExtCallback)
1794     };
1795 
1796     OHOS::Ace::ModalUIExtensionConfig uiExtConfig;
1797     uiExtConfig.isProhibitBack = false;
1798     int32_t sessionId = uiContent->CreateModalUIExtension(asyncContext->requestWant, extensionCallbacks, uiExtConfig);
1799     DLP_LOG_INFO(LABEL, "end CreateModalUIExtension sessionId = %{public}d", sessionId);
1800     if (sessionId == 0) {
1801         DLP_LOG_ERROR(LABEL, "CreateModalUIExtension failed, sessionId is %{public}d", sessionId);
1802     }
1803     uiExtCallback->SetSessionId(sessionId);
1804     return;
1805 }
1806 
UIExtensionCallback(std::shared_ptr<UIExtensionRequestContext> & reqContext)1807 UIExtensionCallback::UIExtensionCallback(std::shared_ptr<UIExtensionRequestContext>& reqContext)
1808 {
1809     this->reqContext_ = reqContext;
1810 }
1811 
SetSessionId(int32_t sessionId)1812 void UIExtensionCallback::SetSessionId(int32_t sessionId)
1813 {
1814     this->sessionId_ = sessionId;
1815 }
1816 
SetErrorCode(int32_t code)1817 bool UIExtensionCallback::SetErrorCode(int32_t code)
1818 {
1819     if (this->reqContext_ == nullptr) {
1820         DLP_LOG_ERROR(LABEL, "OnError reqContext is nullptr");
1821         return false;
1822     }
1823     if (this->alreadyCallback_) {
1824         DLP_LOG_DEBUG(LABEL, "alreadyCallback");
1825         return false;
1826     }
1827     this->alreadyCallback_ = true;
1828     this->reqContext_->errCode = code;
1829     return true;
1830 }
1831 
OnRelease(int32_t releaseCode)1832 void UIExtensionCallback::OnRelease(int32_t releaseCode)
1833 {
1834     DLP_LOG_DEBUG(LABEL, "UIExtensionComponent OnRelease(), releaseCode = %{public}d", releaseCode);
1835     if (SetErrorCode(releaseCode)) {
1836         SendMessageBack();
1837     }
1838 }
1839 
OnResult(int32_t resultCode,const OHOS::AAFwk::Want & result)1840 void UIExtensionCallback::OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result)
1841 {
1842     DLP_LOG_DEBUG(LABEL, "UIExtensionComponent OnResult(), resultCode = %{public}d", resultCode);
1843     this->resultCode_ = resultCode;
1844     this->resultWant_ = result;
1845     if (SetErrorCode(0)) {
1846         SendMessageBack();
1847     }
1848 }
1849 
OnReceive(const OHOS::AAFwk::WantParams & request)1850 void UIExtensionCallback::OnReceive(const OHOS::AAFwk::WantParams& request)
1851 {
1852     DLP_LOG_DEBUG(LABEL, "UIExtensionComponent OnReceive()");
1853 }
1854 
OnError(int32_t errorCode,const std::string & name,const std::string & message)1855 void UIExtensionCallback::OnError(int32_t errorCode, const std::string& name, const std::string& message)
1856 {
1857     DLP_LOG_ERROR(LABEL,
1858         "UIExtensionComponent OnError(), errorCode = %{public}d, name = %{public}s, message = %{public}s",
1859         errorCode, name.c_str(), message.c_str());
1860     if (SetErrorCode(errorCode)) {
1861         SendMessageBack();
1862     }
1863 }
1864 
OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy> & uiProxy)1865 void UIExtensionCallback::OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy)
1866 {
1867     DLP_LOG_DEBUG(LABEL, "UIExtensionComponent OnRemoteReady()");
1868 }
1869 
OnDestroy()1870 void UIExtensionCallback::OnDestroy()
1871 {
1872     DLP_LOG_DEBUG(LABEL, "UIExtensionComponent OnDestroy()");
1873     if (SetErrorCode(0)) {
1874         SendMessageBack();
1875     }
1876 }
1877 
SendMessageBack()1878 void UIExtensionCallback::SendMessageBack()
1879 {
1880     DLP_LOG_INFO(LABEL, "start SendMessageBack");
1881     if (this->reqContext_ == nullptr) {
1882         DLP_LOG_ERROR(LABEL, "reqContext is nullptr");
1883         return;
1884     }
1885 
1886     auto abilityContext = this->reqContext_->context;
1887     if (abilityContext != nullptr) {
1888         auto uiContent = abilityContext->GetUIContent();
1889         if (uiContent != nullptr) {
1890             DLP_LOG_DEBUG(LABEL, "CloseModalUIExtension");
1891             uiContent->CloseModalUIExtension(this->sessionId_);
1892         }
1893     }
1894 
1895     napi_value nativeObjJs = nullptr;
1896     NAPI_CALL_RETURN_VOID(this->reqContext_->env, napi_create_object(this->reqContext_->env, &nativeObjJs));
1897     napi_value resultCode = nullptr;
1898     NAPI_CALL_RETURN_VOID(this->reqContext_->env,
1899         napi_create_int32(this->reqContext_->env, this->resultCode_, &resultCode));
1900     NAPI_CALL_RETURN_VOID(this->reqContext_->env,
1901         napi_set_named_property(this->reqContext_->env, nativeObjJs, "resultCode", resultCode));
1902     napi_value resultWant = nullptr;
1903     resultWant = OHOS::AppExecFwk::WrapWant(this->reqContext_->env, this->resultWant_);
1904     NAPI_CALL_RETURN_VOID(this->reqContext_->env,
1905         napi_set_named_property(this->reqContext_->env, nativeObjJs, "want", resultWant));
1906 
1907     DLP_LOG_DEBUG(LABEL, "ProcessCallbackOrPromise");
1908     ProcessCallbackOrPromise(this->reqContext_->env, this->reqContext_.get(), nativeObjJs);
1909 }
1910 }  // namespace DlpPermission
1911 }  // namespace Security
1912 }  // namespace OHOS
1913