1/* 2 * Copyright (c) 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 16import { HiLog } from '../common/HiLog'; 17import rpc from '@ohos.rpc' 18import Constant from '../common/constant' 19import fs from '@ohos.file.fs' 20import { BusinessError } from '@ohos.base'; 21 22const TAG = 'CleanDLPFileInCacheServiceStub'; 23 24export default class DlpPermissionAbilityServiceStub extends rpc.RemoteObject { 25 private pathDir: string = ''; 26 27 constructor(des: string) { 28 super(des); 29 } 30 31 private clearAllExistDlpFileInCache() { 32 let pathDir = this.pathDir + '/Share'; 33 fs.listFile(pathDir).then((filenames: Array<string>) => { 34 filenames.forEach((item) => { 35 fs.rmdirSync(pathDir + `/${item}`) 36 }) 37 }).catch((err: BusinessError) => { 38 HiLog.error(TAG, `list delete dlp file failed with error: ${JSON.stringify(err)}`); 39 }); 40 } 41 42 public setPathDir(pathDir: string) { 43 this.pathDir = pathDir; 44 } 45 46 private checkCallerIdentity(data: rpc.MessageSequence): boolean { 47 try { 48 let token = data.readInterfaceToken(); 49 if (token !== Constant.SA_INTERFACE_TOKEN) { 50 HiLog.error(TAG, `Interface token is invalid`); 51 return false; 52 } 53 return true; 54 } catch (error) { 55 HiLog.error(TAG, `check caller identity failed, error: ${error}`); 56 } 57 return false 58 } 59 60 onRemoteMessageRequest(code: number, data: rpc.MessageSequence, reply: rpc.MessageSequence, 61 options: rpc.MessageOption): boolean { 62 if (!code || !data || !reply) { 63 HiLog.error(TAG, `Input params is invalid`); 64 return false; 65 } 66 if (!this.checkCallerIdentity(data)) { 67 HiLog.error(TAG, `onRemoteMessageRequest: check clean called failed`); 68 reply.writeInt(Constant.INTERFACE_SUCCESS); 69 return false; 70 } 71 try { 72 this.clearAllExistDlpFileInCache(); 73 reply.writeInt(Constant.INTERFACE_SUCCESS); 74 } catch (error) { 75 HiLog.error(TAG, `onRemoteMessageRequest clean dlp file failed`); 76 reply.writeInt(Constant.INTERFACE_SUCCESS); 77 } 78 return true 79 } 80};