1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "napi_dlp_feature.h"
17
18 #include "dlp_permission.h"
19 #include "dlp_permission_log.h"
20 #include "dlp_permission_kit.h"
21 #include "napi_error_msg.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi_common.h"
25
26 namespace OHOS {
27 namespace Security {
28 namespace DlpPermission {
29 namespace {
30 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "DlpFeatureNapi"};
31 const std::string PARAM_STATUS = "status";
32 const std::string PARAM_DLPFEATURESTATUS = "dlpFeatureStatus";
33 const std::string PERMISSION_ACCESS_DLP_FILE = "ohos.permission.ACCESS_DLP_FILE";
34 const std::string PERMISSION_ENTERPRISE_ACCESS_DLP_FILE = "ohos.permission.ENTERPRISE_ACCESS_DLP_FILE";
35
36 static constexpr int32_t THE_PARAM_ONE = 1;
37 static constexpr int32_t THE_PARAM_ZERO = 0;
38 } // namespace
39
40 typedef struct StatusSetInfo {
41 bool isSuccess = false;
42 } StatusSetInfo;
43
44 enum DlpFeatureStatus : uint32_t {
45 NOT_ENABLED_FEATURE = 0,
46 ENABLED_FEATURE = 1,
47 };
48
49 typedef struct DlpFeatureInfo {
50 DlpFeatureStatus status = NOT_ENABLED_FEATURE;
51 } DlpFeatureInfo;
52
53 struct SetDlpFeatureAsyncContext : public CommonAsyncContext {
SetDlpFeatureAsyncContextOHOS::Security::DlpPermission::SetDlpFeatureAsyncContext54 explicit SetDlpFeatureAsyncContext(napi_env env) : CommonAsyncContext(env) {};
55 DlpFeatureInfo dlpFeatureInfo;
56 StatusSetInfo statusSetInfo;
57 };
58
GetDlpFeatureParams(const napi_env env,const napi_callback_info info,SetDlpFeatureAsyncContext & asyncContext)59 bool GetDlpFeatureParams(
60 const napi_env env, const napi_callback_info info, SetDlpFeatureAsyncContext& asyncContext)
61 {
62 size_t argc = THE_PARAM_ONE;
63 napi_value argv[THE_PARAM_ONE] = {nullptr};
64 NAPI_CALL_BASE(env, napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr), false);
65
66 if (!NapiCheckArgc(env, argc, THE_PARAM_ONE)) {
67 return false;
68 }
69
70 int64_t res;
71 if (!GetInt64Value(env, argv[THE_PARAM_ZERO], res)) {
72 DLP_LOG_ERROR(LABEL, "js get dlpFeatureInfo fail");
73 ThrowParamError(env, "dlpFeatureInfo", "number");
74 return false;
75 }
76 asyncContext.dlpFeatureInfo.status = static_cast<DlpFeatureStatus>(res);
77 return true;
78 }
79
SetDlpFeature(napi_env env,napi_callback_info cbInfo)80 napi_value NapiDlpFeature::SetDlpFeature(napi_env env, napi_callback_info cbInfo)
81 {
82 auto* asyncContext = new (std::nothrow) SetDlpFeatureAsyncContext(env);
83 if (asyncContext == nullptr) {
84 DLP_LOG_ERROR(LABEL, "insufficient memory for asyncContext!");
85 DlpNapiThrow(env, ERR_JS_OUT_OF_MEMORY);
86 return nullptr;
87 }
88 std::unique_ptr<SetDlpFeatureAsyncContext> asyncContextPtr { asyncContext };
89
90 if (!GetDlpFeatureParams(env, cbInfo, *asyncContext)) {
91 return nullptr;
92 }
93
94 napi_value result = nullptr;
95 DLP_LOG_DEBUG(LABEL, "Create promise");
96 NAPI_CALL(env, napi_create_promise(env, &asyncContextPtr->deferred, &result));
97 napi_value resource = nullptr;
98 NAPI_CALL(env, napi_create_string_utf8(env, "SetDlpFeature", NAPI_AUTO_LENGTH, &resource));
99 NAPI_CALL(env, napi_create_async_work(env, nullptr, resource, SetDlpFeatureExecute, SetDlpFeatureComplete,
100 static_cast<void*>(asyncContext), &(asyncContext->work)));
101 NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work));
102 asyncContextPtr.release();
103 return result;
104 }
105
SetDlpFeatureExecute(napi_env env,void * data)106 void NapiDlpFeature::SetDlpFeatureExecute(napi_env env, void* data)
107 {
108 DLP_LOG_DEBUG(LABEL, "napi_create_async_work running");
109 auto asyncContext = reinterpret_cast<SetDlpFeatureAsyncContext*>(data);
110 if (asyncContext == nullptr) {
111 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
112 return;
113 }
114
115 asyncContext->errCode =
116 DlpPermissionKit::SetDlpFeature(asyncContext->dlpFeatureInfo.status, asyncContext->statusSetInfo.isSuccess);
117 }
118
SetDlpFeatureComplete(napi_env env,napi_status status,void * data)119 void NapiDlpFeature::SetDlpFeatureComplete(napi_env env, napi_status status, void* data)
120 {
121 DLP_LOG_DEBUG(LABEL, "napi_create_async_work complete");
122 auto asyncContext = reinterpret_cast<SetDlpFeatureAsyncContext*>(data);
123 if (asyncContext == nullptr) {
124 DLP_LOG_ERROR(LABEL, "asyncContext is nullptr");
125 return;
126 }
127 std::unique_ptr<SetDlpFeatureAsyncContext> asyncContextPtr { asyncContext };
128 napi_value resJs = nullptr;
129 if (asyncContext->errCode == DLP_OK) {
130 NAPI_CALL_RETURN_VOID(env, napi_get_boolean(env, asyncContextPtr->statusSetInfo.isSuccess, &resJs));
131 }
132
133 ProcessCallbackOrPromise(env, asyncContext, resJs);
134 }
135
CreateEnumDlpFeatureStatus(napi_env env)136 napi_value CreateEnumDlpFeatureStatus(napi_env env)
137 {
138 napi_value status = nullptr;
139 NAPI_CALL(env, napi_create_object(env, &status));
140
141 napi_value prop = nullptr;
142 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(DlpFeatureStatus::NOT_ENABLED_FEATURE), &prop));
143 NAPI_CALL(env, napi_set_named_property(env, status, "NOT_ENABLED_FEATURE", prop));
144
145 prop = nullptr;
146 NAPI_CALL(env, napi_create_int32(env, static_cast<int32_t>(DlpFeatureStatus::ENABLED_FEATURE), &prop));
147 NAPI_CALL(env, napi_set_named_property(env, status, "ENABLED_FEATURE", prop));
148
149 return status;
150 }
151
Init(napi_env env,napi_value exports)152 napi_value NapiDlpFeature::Init(napi_env env, napi_value exports)
153 {
154 napi_property_descriptor descriptor[] = {DECLARE_NAPI_FUNCTION("setDlpFeature", SetDlpFeature)};
155 NAPI_CALL(
156 env, napi_define_properties(env, exports, sizeof(descriptor) / sizeof(napi_property_descriptor), descriptor));
157
158 napi_property_descriptor descriptors[] = {
159 DECLARE_NAPI_PROPERTY("DlpFeatureStatus", CreateEnumDlpFeatureStatus(env)),
160 };
161 napi_define_properties(env, exports, sizeof(descriptors) / sizeof(napi_property_descriptor), descriptors);
162
163 return exports;
164 }
165 } // namespace DlpPermission
166 } // namespace Security
167 } // namespace OHOS
168
169 EXTERN_C_START
170 /*
171 * function for module exports
172 */
Init(napi_env env,napi_value exports)173 static napi_value Init(napi_env env, napi_value exports)
174 {
175 DLP_LOG_DEBUG(OHOS::Security::DlpPermission::LABEL, "dlpFeature start init.");
176
177 return OHOS::Security::DlpPermission::NapiDlpFeature::Init(env, exports);
178 }
179 EXTERN_C_END
180
181 /*
182 * Module define
183 */
184 static napi_module _module = {.nm_version = 1,
185 .nm_flags = 0,
186 .nm_filename = nullptr,
187 .nm_register_func = Init,
188 .nm_modname = "dlpSetDlpFeature",
189 .nm_priv = ((void*)0),
190 .reserved = {0}};
191
192 /*
193 * Module register function
194 */
DlpFeatureModuleRegister(void)195 extern "C" __attribute__((constructor)) void DlpFeatureModuleRegister(void)
196 {
197 napi_module_register(&_module);
198 }