1/* 2 * Copyright (c) 2024-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 16import { HiLog } from '../../../common/HiLog'; 17import { DecryptState, OpenDlpFileManager } from '../../manager/OpenDlpFileManager'; 18import DecryptContent from '../../data/DecryptContent'; 19import Constants from '../../../common/constant'; 20import FileUtil from '../../../common/external/FileUtil'; 21import { common } from '@kit.AbilityKit'; 22import TerminateView from '../../TerminateView/TerminateView'; 23import OpeningDialogManager from '../../manager/OpeningDialogManager'; 24 25const TAG = 'DataUtils'; 26 27async function clearDlpInfoByError(decryptContent: DecryptContent): Promise<void> { 28 HiLog.info(TAG, 'start clearDlpInfoByError'); 29 clearIsDecrypting(decryptContent); 30 await deleteDlpLinkFile(decryptContent); 31 await closeDlpFile(decryptContent); 32 FileUtil.closeSync(decryptContent.dlpFd); 33} 34 35async function clearDlpInfoByCancelSandbox(decryptContent: DecryptContent): Promise<void> { 36 HiLog.info(TAG, 'start clearDlpInfoByCancelSandbox'); 37 deleteDecryptData(decryptContent); 38 await deleteDlpLinkFile(decryptContent); 39 await closeDlpFile(decryptContent); 40 FileUtil.closeSync(decryptContent.dlpFd); 41} 42 43async function deleteDecryptData(decryptContent: DecryptContent): Promise<void> { 44 const manager = OpenDlpFileManager.getInstance(); 45 const rmRet = await manager.removeAllByUri(decryptContent.openDlpFileData.uri); 46 if (rmRet.errcode !== Constants.ERR_CODE_SUCCESS) { 47 HiLog.error(TAG, 'deleteDecryptData failed'); 48 } 49} 50 51async function clearDlpInfoBatch(bundleName: string, sandboxAppIndex: number, 52 context: common.ServiceExtensionContext): Promise<void> { 53 HiLog.debug(TAG, 'clearDlpInfoBatch start'); 54 const manager = OpenDlpFileManager.getInstance(); 55 const rmRet = await manager.removeByBundleNameAndAppIndex(bundleName, sandboxAppIndex); 56 if (rmRet.errcode !== Constants.ERR_CODE_SUCCESS || !rmRet.result) { 57 HiLog.error(TAG, 'clearDlpInfoBatch removeByBundleNameAndAppIndex error'); 58 return; 59 } 60 const decryptContents = rmRet.result; 61 for (const decryptContent of decryptContents) { 62 HiLog.debug(TAG, `clearDlpInfoByCancelSandbox fileName ${decryptContent.fileName}`); 63 await clearDlpInfoByCancelSandbox(decryptContent); 64 } 65 66 const getRet = manager.getHasDecryptedSize(); 67 const hasDecryptedNumber = getRet.result; 68 if (hasDecryptedNumber === 0) { 69 HiLog.info(TAG, 'sandbox2linkFile empty'); 70 await terminateDataAbility(context); 71 } 72} 73 74async function terminateDataAbility(dataContext: common.ServiceExtensionContext): Promise<void> { 75 HiLog.info(TAG, 'terminateDataAbility start'); 76 await terminateSelf(dataContext); 77 await TerminateView.terminate(); 78} 79 80async function terminateSelf(context: common.ServiceExtensionContext): Promise<void> { 81 try { 82 await context.terminateSelf(); 83 HiLog.info(TAG, 'DataAbility terminateSelf success'); 84 } catch (error) { 85 HiLog.wrapError(TAG, error, 'DataAbility terminateSelf exception'); 86 } 87} 88 89async function closeDlpFile(decryptContent: DecryptContent): Promise<void> { 90 try { 91 await decryptContent.dlpFile.closeDLPFile(); 92 } catch (err) { 93 HiLog.wrapError(TAG, err, 'dlpFile closeDLPFile failed'); 94 } 95} 96 97async function deleteDlpLinkFile(decryptContent: DecryptContent): Promise<void> { 98 try { 99 await decryptContent.dlpFile.deleteDLPLinkFile(decryptContent.linkFileName); 100 } catch (err) { 101 HiLog.wrapError(TAG, err, 'deleteDlpLinkFile error'); 102 } 103} 104 105function clearIsDecrypting(decryptContent: DecryptContent): void { 106 if (decryptContent.openDlpFileData?.uri) { 107 const getRet = OpenDlpFileManager.getInstance().getStatus(decryptContent.openDlpFileData.uri); 108 if (getRet.errcode === Constants.ERR_CODE_SUCCESS && (getRet.result?.state === DecryptState.DECRYPTING)) { 109 OpeningDialogManager.getInstance().deleteRequestId(decryptContent.openDlpFileData.requestId); 110 OpenDlpFileManager.getInstance().removeAllByUri(decryptContent.openDlpFileData.uri); 111 } 112 } 113} 114 115export { clearDlpInfoByError, clearDlpInfoBatch, closeDlpFile };