1 /* 2 * Copyright (c) 2023-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #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 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(napi_env env, napi_ref ref); 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(napi_env env, napi_ref ref); 145 }; 146 147 struct GenerateDlpFileAsyncContext : public CommonAsyncContext { GenerateDlpFileAsyncContextGenerateDlpFileAsyncContext148 explicit GenerateDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 149 int64_t plaintextFd = -1; 150 int64_t ciphertextFd = -1; 151 DlpProperty property; 152 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 153 }; 154 155 struct DlpFileAsyncContext : public CommonAsyncContext { DlpFileAsyncContextDlpFileAsyncContext156 explicit DlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 157 int64_t ciphertextFd = -1; 158 std::string appId; 159 DlpProperty property; 160 bool isDlpFile = false; 161 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 162 }; 163 164 struct DlpLinkFileAsyncContext : public CommonAsyncContext { DlpLinkFileAsyncContextDlpLinkFileAsyncContext165 explicit DlpLinkFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 166 std::string linkFileName = ""; 167 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 168 }; 169 170 struct RecoverDlpFileAsyncContext : public CommonAsyncContext { RecoverDlpFileAsyncContextRecoverDlpFileAsyncContext171 explicit RecoverDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 172 int64_t plaintextFd = -1; 173 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 174 }; 175 176 struct CloseDlpFileAsyncContext : public CommonAsyncContext { CloseDlpFileAsyncContextCloseDlpFileAsyncContext177 explicit CloseDlpFileAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 178 std::shared_ptr<DlpFile> dlpFileNative = nullptr; 179 }; 180 181 struct DlpSandboxAsyncContext : public CommonAsyncContext { DlpSandboxAsyncContextDlpSandboxAsyncContext182 explicit DlpSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 183 std::string bundleName; 184 DLPFileAccess dlpFileAccess = NO_PERMISSION; 185 int32_t userId = -1; 186 SandboxInfo sandboxInfo; 187 std::string uri = ""; 188 }; 189 190 struct GetPermInfoAsyncContext : public CommonAsyncContext { GetPermInfoAsyncContextGetPermInfoAsyncContext191 explicit GetPermInfoAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 192 DLPPermissionInfo permInfo; 193 }; 194 195 struct IsInSandboxAsyncContext : public CommonAsyncContext { IsInSandboxAsyncContextIsInSandboxAsyncContext196 explicit IsInSandboxAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 197 bool inSandbox = false; 198 }; 199 200 struct IsDLPFeatureProvidedAsyncContext : public CommonAsyncContext { IsDLPFeatureProvidedAsyncContextIsDLPFeatureProvidedAsyncContext201 explicit IsDLPFeatureProvidedAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 202 bool isProvideDLPFeature = 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 struct GetGatheringPolicyContext : public CommonAsyncContext { GetGatheringPolicyContextGetGatheringPolicyContext222 explicit GetGatheringPolicyContext(napi_env env) : CommonAsyncContext(env) {}; 223 bool isGathering = false; 224 }; 225 226 struct RetentionStateAsyncContext : public CommonAsyncContext { RetentionStateAsyncContextRetentionStateAsyncContext227 explicit RetentionStateAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 228 std::vector<std::string> docUris; 229 }; 230 231 struct GetRetentionSandboxListAsyncContext : public CommonAsyncContext { GetRetentionSandboxListAsyncContextGetRetentionSandboxListAsyncContext232 explicit GetRetentionSandboxListAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 233 std::string bundleName = ""; 234 std::vector<RetentionSandBoxInfo> retentionSandBoxInfoVec; 235 }; 236 237 struct GetDLPFileVisitRecordAsyncContext : public CommonAsyncContext { GetDLPFileVisitRecordAsyncContextGetDLPFileVisitRecordAsyncContext238 explicit GetDLPFileVisitRecordAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 239 std::vector<VisitedDLPFileInfo> visitedDlpFileInfoVec; 240 }; 241 242 struct SandboxAppConfigAsyncContext : public CommonAsyncContext { SandboxAppConfigAsyncContextSandboxAppConfigAsyncContext243 explicit SandboxAppConfigAsyncContext(napi_env env) : CommonAsyncContext(env) {}; 244 std::string configInfo = ""; 245 }; 246 247 struct UIExtensionRequestContext : public CommonAsyncContext { UIExtensionRequestContextUIExtensionRequestContext248 explicit UIExtensionRequestContext(napi_env env) : CommonAsyncContext(env) {}; 249 std::shared_ptr<OHOS::AbilityRuntime::AbilityContext> context = nullptr; 250 OHOS::AAFwk::Want requestWant; 251 }; 252 253 class UIExtensionCallback { 254 public: 255 explicit UIExtensionCallback(std::shared_ptr<UIExtensionRequestContext>& reqContext); 256 void SetSessionId(int32_t sessionId); 257 void OnRelease(int32_t releaseCode); 258 void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result); 259 void OnReceive(const OHOS::AAFwk::WantParams& request); 260 void OnError(int32_t code, const std::string& name, const std::string& message); 261 void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy); 262 void OnDestroy(); 263 void SendMessageBack(); 264 265 private: 266 bool SetErrorCode(int32_t code); 267 int32_t sessionId_ = 0; 268 int32_t resultCode_ = 0; 269 OHOS::AAFwk::Want resultWant_; 270 std::shared_ptr<UIExtensionRequestContext> reqContext_ = nullptr; 271 bool alreadyCallback_ = false; 272 }; 273 274 void ThrowParamError(const napi_env env, const std::string& param, const std::string& type); 275 void DlpNapiThrow(napi_env env, int32_t nativeErrCode); 276 void DlpNapiThrow(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg); 277 napi_value GenerateBusinessError(napi_env env, int32_t jsErrCode, const std::string &jsErrMsg); 278 bool NapiCheckArgc(const napi_env env, int32_t argc, int32_t reqSize); 279 280 napi_value CreateEnumDLPFileAccess(napi_env env); 281 napi_value CreateEnumAccountType(napi_env env); 282 napi_value CreateEnumActionFlags(napi_env env); 283 napi_value CreateEnumGatheringPolicy(napi_env env); 284 285 void ProcessCallbackOrPromise(napi_env env, const CommonAsyncContext* asyncContext, napi_value data); 286 287 bool GetGenerateDlpFileParams( 288 const napi_env env, const napi_callback_info info, GenerateDlpFileAsyncContext& asyncContext); 289 bool GetOpenDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext); 290 bool GetIsDlpFileParams(const napi_env env, const napi_callback_info info, DlpFileAsyncContext& asyncContext); 291 292 bool GetDlpLinkFileParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext); 293 bool GetLinkFileStatusParams(const napi_env env, const napi_callback_info info, DlpLinkFileAsyncContext& asyncContext); 294 bool GetRecoverDlpFileParams( 295 const napi_env env, const napi_callback_info info, RecoverDlpFileAsyncContext& asyncContext); 296 bool GetCloseDlpFileParams(const napi_env env, const napi_callback_info info, CloseDlpFileAsyncContext& asyncContext); 297 bool GetInstallDlpSandboxParams( 298 const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext); 299 bool GetUninstallDlpSandboxParams( 300 const napi_env env, const napi_callback_info info, DlpSandboxAsyncContext& asyncContext); 301 bool GetThirdInterfaceParams( 302 const napi_env env, const napi_callback_info info, CommonAsyncContext& asyncContext); 303 304 bool FillDlpSandboxChangeInfo(const napi_env env, const napi_value* argv, const std::string& type, 305 const napi_value thisVar, RegisterDlpSandboxChangeInfo& registerSandboxChangeInfo); 306 bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, 307 RegisterDlpSandboxChangeInfo ®isterSandboxChangeInfo); 308 bool GetUnregisterSandboxParams(const napi_env env, const napi_callback_info info, 309 UnregisterSandboxChangeCallbackAsyncContext &asyncContext); 310 bool GetRetentionStateParams(const napi_env env, const napi_callback_info info, 311 RetentionStateAsyncContext& asyncContext); 312 bool GetRetentionSandboxListParams(const napi_env env, const napi_callback_info info, 313 GetRetentionSandboxListAsyncContext& asyncContext); 314 bool GetOriginalFilenameParams(const napi_env env, const napi_callback_info info, 315 GetOriginalFileAsyncContext& asyncContext); 316 bool GetSandboxAppConfigParams(const napi_env env, const napi_callback_info info, 317 SandboxAppConfigAsyncContext* asyncContext); 318 void GetDlpPropertyExpireTime(napi_env env, napi_value jsObject, DlpProperty& property); 319 bool GetDlpProperty(napi_env env, napi_value object, DlpProperty& property); 320 bool ParseCallback(const napi_env& env, const napi_value& value, napi_ref& callbackRef); 321 322 napi_value GetNapiValue(napi_env env, napi_value jsObject, const std::string& key); 323 bool GetStringValue(napi_env env, napi_value jsObject, std::string& result); 324 bool GetStringValueByKey(napi_env env, napi_value jsObject, const std::string& key, std::string& result); 325 bool GetBoolValueByKey(napi_env env, napi_value jsObject, const std::string& key, bool& result); 326 bool GetBoolValue(napi_env env, napi_value jsObject, bool& result); 327 bool GetInt64Value(napi_env env, napi_value jsObject, int64_t& result); 328 bool GetInt64ValueByKey(napi_env env, napi_value jsObject, const std::string& key, int64_t& result); 329 bool GetUint32Value(napi_env env, napi_value jsObject, uint32_t& result); 330 bool GetUint32ValueByKey(napi_env env, napi_value jsObject, const std::string& key, uint32_t& result); 331 napi_value GetArrayValueByKey(napi_env env, napi_value jsObject, const std::string& key); 332 bool GetVectorAuthUser(napi_env env, napi_value jsObject, std::vector<AuthUserInfo>& resultVec); 333 bool GetVectorAuthUserByKey( 334 napi_env env, napi_value jsObject, const std::string& key, std::vector<AuthUserInfo>& resultVec); 335 bool GetVectorDocUriByKey(napi_env env, napi_value jsObject, const std::string& key, 336 std::vector<std::string>& docUriVec); 337 napi_value VectorUint32ToJs(napi_env env, const std::vector<uint32_t>& value); 338 bool GetVectorUint32(napi_env env, napi_value jsObject, std::vector<uint32_t>& resultVec); 339 340 napi_value RetentionSandboxInfoToJs(napi_env env, const std::vector<RetentionSandBoxInfo>& infoVec); 341 napi_value VisitInfoToJs(napi_env env, const std::vector<VisitedDLPFileInfo>& infoVec); 342 napi_value DlpPropertyToJs(napi_env env, const DlpProperty& property); 343 napi_value VectorAuthUserToJs(napi_env env, const std::vector<AuthUserInfo>& users); 344 napi_value VectorStringToJs(napi_env env, const std::vector<std::string>& value); 345 napi_value SetStringToJs(napi_env env, const std::set<std::string>& value); 346 napi_value DlpPermissionInfoToJs(napi_env env, const DLPPermissionInfo& permInfo); 347 napi_value SandboxInfoToJs(napi_env env, const SandboxInfo& sandboxInfo); 348 349 bool ParseUIAbilityContextReq( 350 napi_env env, const napi_value& obj, std::shared_ptr<OHOS::AbilityRuntime::AbilityContext>& abilityContext); 351 bool ParseWantReq(napi_env env, const napi_value& obj, OHOS::AAFwk::Want& requestWant); 352 void StartUIExtensionAbility(std::shared_ptr<UIExtensionRequestContext> asyncContext); 353 } // namespace DlpPermission 354 } // namespace Security 355 } // namespace OHOS 356 #endif /* INTERFACES_KITS_NAPI_COMMON_INCLUDE_NAPI_H */ 357