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 #ifndef INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H 17 #define INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H 18 19 #include <vector> 20 #include <unistd.h> 21 #include <uv.h> 22 23 #include "ability_context.h" 24 #include "napi/native_api.h" 25 #include "napi/native_node_api.h" 26 #include "napi_base_context.h" 27 #include "napi_common_want.h" 28 #include "dlp_file.h" 29 #include "dlp_sandbox_callback_info.h" 30 #include "dlp_sandbox_change_callback_customize.h" 31 #include "open_dlp_file_callback_customize.h" 32 #include "permission_policy.h" 33 #include "retention_sandbox_info.h" 34 #include "ui_content.h" 35 #include "visited_dlp_file_info.h" 36 37 namespace OHOS { 38 namespace Security { 39 namespace DlpPermission { 40 constexpr int32_t PARAM0 = 0; 41 constexpr int32_t PARAM1 = 1; 42 constexpr int32_t PARAM2 = 2; 43 constexpr int32_t PARAM3 = 3; 44 constexpr int32_t PARAM4 = 4; 45 constexpr int32_t PARAM_SIZE_ONE = 1; 46 constexpr int32_t PARAM_SIZE_TWO = 2; 47 constexpr int32_t PARAM_SIZE_THREE = 3; 48 constexpr int32_t PARAM_SIZE_FOUR = 4; 49 constexpr int32_t PARAM_SIZE_FIVE = 5; 50 const std::string ON_OFF_SANDBOX = "uninstallDLPSandbox"; 51 52 #define NAPI_CALL_BASE_WITH_SCOPE(env, theCall, retVal, scope) \ 53 do { \ 54 if ((theCall) != napi_ok) { \ 55 GET_AND_THROW_LAST_ERROR((env)); \ 56 napi_close_handle_scope(env, scope); \ 57 return retVal; \ 58 } \ 59 } while (0) 60 61 #define NAPI_CALL_RETURN_VOID_WITH_SCOPE(env, theCall, scope) \ 62 NAPI_CALL_BASE_WITH_SCOPE(env, theCall, NAPI_RETVAL_NOTHING, scope) 63 64 class RegisterDlpSandboxChangeScopePtr : public DlpSandboxChangeCallbackCustomize { 65 public: 66 RegisterDlpSandboxChangeScopePtr(); 67 ~RegisterDlpSandboxChangeScopePtr() override; 68 void DlpSandboxChangeCallback(DlpSandboxCallbackInfo &result) override; 69 void SetEnv(const napi_env &env); 70 void SetCallbackRef(const napi_ref &ref); 71 void SetValid(bool valid); 72 73 private: 74 napi_env env_ = nullptr; 75 napi_ref ref_ = nullptr; 76 bool valid_ = true; 77 std::mutex validMutex_; 78 }; 79 80 struct CommonAsyncContext { 81 explicit CommonAsyncContext(napi_env napiEnv); 82 virtual ~CommonAsyncContext(); 83 napi_env env = nullptr; 84 napi_status status = napi_invalid_arg; 85 int32_t errCode = 0; 86 napi_deferred deferred = nullptr; // promise handle 87 napi_ref callbackRef = nullptr; // callback handle 88 napi_async_work work = nullptr; // work handle 89 }; 90 91 struct RegisterDlpSandboxChangeWorker { 92 napi_env env = nullptr; 93 napi_ref ref = nullptr; 94 DlpSandboxCallbackInfo result; 95 RegisterDlpSandboxChangeScopePtr *subscriber = nullptr; 96 }; 97 98 struct DlpSandboxChangeContext { 99 virtual ~DlpSandboxChangeContext(); 100 napi_env env = nullptr; 101 napi_ref callbackRef = nullptr; 102 int32_t errCode = 0; 103 std::string changeType; 104 std::shared_ptr<RegisterDlpSandboxChangeScopePtr> subscriber = nullptr; 105 void DeleteNapiRef(); 106 }; 107 108 typedef DlpSandboxChangeContext RegisterDlpSandboxChangeInfo; 109 110 struct UnregisterSandboxChangeCallbackAsyncContext : public CommonAsyncContext { UnregisterSandboxChangeCallbackAsyncContextUnregisterSandboxChangeCallbackAsyncContext111 explicit UnregisterSandboxChangeCallbackAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 112 bool result = false; 113 std::string changeType; 114 }; 115 class OpenDlpFileSubscriberPtr : public OpenDlpFileCallbackCustomize { 116 public: 117 OpenDlpFileSubscriberPtr(); 118 ~OpenDlpFileSubscriberPtr() override; 119 void OnOpenDlpFile(OpenDlpFileCallbackInfo &result) override; 120 void SetEnv(const napi_env &env); 121 void SetCallbackRef(const napi_ref &ref); 122 void SetValid(bool valid); 123 124 private: 125 napi_env env_ = nullptr; 126 napi_ref ref_ = nullptr; 127 bool valid_ = true; 128 std::mutex validMutex_; 129 }; 130 131 struct OpenDlpFileSubscriberWorker { 132 napi_env env = nullptr; 133 napi_ref ref = nullptr; 134 OpenDlpFileCallbackInfo result; 135 OpenDlpFileSubscriberPtr *subscriber = nullptr; 136 }; 137 138 struct OpenDlpFileSubscriberContext { 139 virtual ~OpenDlpFileSubscriberContext(); 140 napi_env env = nullptr; 141 napi_ref callbackRef = nullptr; 142 int32_t errCode = 0; 143 std::shared_ptr<OpenDlpFileSubscriberPtr> subscriber = nullptr; 144 void DeleteNapiRef(); 145 }; 146 147 struct OpenDlpFileUnSubscriberContext : public CommonAsyncContext { OpenDlpFileUnSubscriberContextOpenDlpFileUnSubscriberContext148 explicit OpenDlpFileUnSubscriberContext(napi_env env) : CommonAsyncContext(env) {}; 149 bool result = false; 150 }; 151 152 struct GenerateDlpFileAsyncContext : public CommonAsyncContext { GenerateDlpFileAsyncContextGenerateDlpFileAsyncContext153 explicit GenerateDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 154 int64_t plaintextFd = -1; 155 int64_t ciphertextFd = -1; 156 DlpProperty property; 157 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 158 }; 159 160 struct DlpFileAsyncContext : public CommonAsyncContext { DlpFileAsyncContextDlpFileAsyncContext161 explicit DlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 162 int64_t ciphertextFd = -1; 163 std::string appId; 164 DlpProperty property; 165 bool isDlpFile = false; 166 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 167 }; 168 169 struct DlpLinkFileAsyncContext : public CommonAsyncContext { DlpLinkFileAsyncContextDlpLinkFileAsyncContext170 explicit DlpLinkFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 171 std::string linkFileName = ""; 172 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 173 }; 174 175 struct RecoverDlpFileAsyncContext : public CommonAsyncContext { RecoverDlpFileAsyncContextRecoverDlpFileAsyncContext176 explicit RecoverDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 177 int64_t plaintextFd = -1; 178 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 179 }; 180 181 struct CloseDlpFileAsyncContext : public CommonAsyncContext { CloseDlpFileAsyncContextCloseDlpFileAsyncContext182 explicit CloseDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 183 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 184 }; 185 186 struct DlpSandboxAsyncContext : public CommonAsyncContext { DlpSandboxAsyncContextDlpSandboxAsyncContext187 explicit DlpSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 188 std::string bundleName; 189 DLPFileAccess dlpFileAccess = NO_PERMISSION; 190 int32_t userId = -1; 191 SandboxInfo sandboxInfo; 192 std::string uri = ""; 193 }; 194 195 struct GetPermInfoAsyncContext : public CommonAsyncContext { GetPermInfoAsyncContextGetPermInfoAsyncContext196 explicit GetPermInfoAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 197 DLPPermissionInfo permInfo; 198 }; 199 200 struct IsInSandboxAsyncContext : public CommonAsyncContext { IsInSandboxAsyncContextIsInSandboxAsyncContext201 explicit IsInSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 202 bool inSandbox = false; 203 }; 204 205 struct GetOriginalFileAsyncContext : public CommonAsyncContext { GetOriginalFileAsyncContextGetOriginalFileAsyncContext206 explicit GetOriginalFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 207 std::string dlpFilename = ""; 208 std::string oriFilename = ""; 209 }; 210 211 struct GetSuffixAsyncContext : public CommonAsyncContext { GetSuffixAsyncContextGetSuffixAsyncContext212 explicit GetSuffixAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 213 std::string extension = ""; 214 }; 215 216 struct GetDlpSupportFileTypeAsyncContext : public CommonAsyncContext { GetDlpSupportFileTypeAsyncContextGetDlpSupportFileTypeAsyncContext217 explicit GetDlpSupportFileTypeAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 218 std::vector<std::string> supportFileType; 219 }; 220 221 void UvQueueWorkDeleteRef(uv_work_t *work, int32_t status); 222 223 struct GetGatheringPolicyContext : public CommonAsyncContext { GetGatheringPolicyContextGetGatheringPolicyContext224 explicit GetGatheringPolicyContext(napi_env env) : CommonAsyncContext(env) {}; 225 bool isGathering = false; 226 }; 227 228 struct RetentionStateAsyncContext : public CommonAsyncContext { RetentionStateAsyncContextRetentionStateAsyncContext229 explicit RetentionStateAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 230 std::vector<std::string> docUris; 231 }; 232 233 struct GetRetentionSandboxListAsyncContext : public CommonAsyncContext { GetRetentionSandboxListAsyncContextGetRetentionSandboxListAsyncContext234 explicit GetRetentionSandboxListAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 235 std::string bundleName = ""; 236 std::vector<RetentionSandBoxInfo> retentionSandBoxInfoVec; 237 }; 238 239 struct GetDLPFileVisitRecordAsyncContext : public CommonAsyncContext { GetDLPFileVisitRecordAsyncContextGetDLPFileVisitRecordAsyncContext240 explicit GetDLPFileVisitRecordAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 241 std::vector<VisitedDLPFileInfo> visitedDlpFileInfoVec; 242 }; 243 244 struct SandboxAppConfigAsyncContext : public CommonAsyncContext { SandboxAppConfigAsyncContextSandboxAppConfigAsyncContext245 explicit SandboxAppConfigAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 246 std::string configInfo = ""; 247 }; 248 249 struct UIExtensionRequestContext : public CommonAsyncContext { UIExtensionRequestContextUIExtensionRequestContext250 explicit UIExtensionRequestContext(napi_env env) : CommonAsyncContext(env) {}; 251 std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> context = nullptr; 252 OHOS::AAFwk::Want requestWant; 253 }; 254 255 class UIExtensionCallback { 256 public: 257 explicit UIExtensionCallback(std::shared_ptr<UIExtensionRequestContext>& reqContext); 258 void SetSessionId(int32_t sessionId); 259 void OnRelease(int32_t releaseCode); 260 void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result); 261 void OnReceive(const OHOS::AAFwk::WantParams& request); 262 void OnError(int32_t code, const std::string& name, const std::string& message); 263 void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy); 264 void OnDestroy(); 265 void SendMessageBack(); 266 267 private: 268 bool SetErrorCode(int32_t code); 269 int32_t sessionId_ = 0; 270 int32_t resultCode_ = 0; 271 OHOS::AAFwk::Want resultWant_; 272 std::shared_ptr<UIExtensionRequestContext> reqContext_ = nullptr; 273 bool alreadyCallback_ = false; 274 }; 275 276 void ThrowParamError(const napi_env env, const std::string& param, const std::string& type); 277 void DlpNapiThrow(napi_env env, int32_t nativeErrCode); 278 void DlpNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg); 279 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg); 280 bool NapiCheckArgc(const napi_env env, int32_t argc, int32_t reqSize); 281 282 napi_value CreateEnumDLPFileAccess(napi_env env); 283 napi_value CreateEnumAccountType(napi_env env); 284 napi_value CreateEnumActionFlags(napi_env env); 285 napi_value CreateEnumGatheringPolicy(napi_env env); 286 287 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data); 288 289 bool GetGenerateDlpFileParams( 290 const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext); 291 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext); 292 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext); 293 294 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext); 295 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext); 296 bool GetRecoverDlpFileParams( 297 const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext); 298 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext); 299 bool GetInstallDlpSandboxParams( 300 const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext); 301 bool GetUninstallDlpSandboxParams( 302 const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext); 303 bool GetThirdInterfaceParams( 304 const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext); 305 306 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value* argv, const std::string& type, 307 const napi_value thisVar, RegisterDlpSandboxChangeInfo& registerSandboxChangeInfo); 308 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, 309 RegisterDlpSandboxChangeInfo ®isterSandboxChangeInfo); 310 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info, 311 UnregisterSandboxChangeCallbackAsyncContext &asyncContext); 312 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info, 313 RetentionStateAsyncContext& asyncContext); 314 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info, 315 GetRetentionSandboxListAsyncContext& asyncContext); 316 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info, 317 GetOriginalFileAsyncContext& asyncContext); 318 bool GetSandboxAppConfigParams(const napi_env env, const napi_callback_info info, 319 SandboxAppConfigAsyncContext* asyncContext); 320 void GetDlpPropertyExpireTime(napi_env env, napi_value jsObject, DlpProperty& property); 321 bool GetDlpProperty(napi_env env, napi_value object, DlpProperty& property); 322 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef); 323 324 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key); 325 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result); 326 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result); 327 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result); 328 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result); 329 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result); 330 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result); 331 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result); 332 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result); 333 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key); 334 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec); 335 bool GetVectorAuthUserByKey( 336 napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec); 337 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key, 338 std::vector<std::string>& docUriVec); 339 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value); 340 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec); 341 342 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec); 343 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec); 344 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property); 345 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users); 346 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value); 347 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value); 348 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo); 349 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo); 350 351 bool ParseUIAbilityContextReq( 352 napi_env env, const napi_value& obj, std::shared_ptr<OHOS::AbilityRuntime::AbilityContext>& abilityContext); 353 bool ParseWantReq(napi_env env, const napi_value& obj, OHOS::AAFwk::Want& requestWant); 354 void StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext); 355 } // namespace DlpPermission 356 } // namespace Security 357 } // namespace OHOS 358 #endif /* INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H */ 359