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