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