1df226684Sopenharmony_ci/*
2df226684Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3df226684Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4df226684Sopenharmony_ci * you may not use this file except in compliance with the License.
5df226684Sopenharmony_ci * You may obtain a copy of the License at
6df226684Sopenharmony_ci *
7df226684Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8df226684Sopenharmony_ci *
9df226684Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10df226684Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11df226684Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12df226684Sopenharmony_ci * See the License for the specific language governing permissions and
13df226684Sopenharmony_ci * limitations under the License.
14df226684Sopenharmony_ci */
15df226684Sopenharmony_ciimport ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility';
16df226684Sopenharmony_ciimport Want from '@ohos.app.ability.Want';
17df226684Sopenharmony_ciimport fileio from '@ohos.fileio';
18df226684Sopenharmony_ciimport dlpPermission from '@ohos.dlpPermission';
19df226684Sopenharmony_ciimport common from '@ohos.app.ability.common';
20df226684Sopenharmony_ciimport GlobalContext from '../common/GlobalContext';
21df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog';
22df226684Sopenharmony_ciimport {
23df226684Sopenharmony_ci  deleteSandbox2linkFileData,
24df226684Sopenharmony_ci  deleteFileOpenHistoryData,
25df226684Sopenharmony_ci  deleteLinkSetData,
26df226684Sopenharmony_ci  deleteToken2FileData,
27df226684Sopenharmony_ci} from '../common/DataUtils'
28df226684Sopenharmony_ci
29df226684Sopenharmony_ciconst TAG = 'DataAbility';
30df226684Sopenharmony_ciconst INDEX_TWO = 2;
31df226684Sopenharmony_ciconst INDEX_ONE = 1;
32df226684Sopenharmony_ciconst INDEX_ZERO = 0;
33df226684Sopenharmony_ciconst INDEX_THREE = 3;
34df226684Sopenharmony_ci
35df226684Sopenharmony_ciexport default class DataAbility extends ServiceExtensionAbility {
36df226684Sopenharmony_ci  private isSubscriber = false;
37df226684Sopenharmony_ci
38df226684Sopenharmony_ci  async sandbox2linkFile(key: string) {
39df226684Sopenharmony_ci    let sandbox2linkFile: Map<string, (number | string | dlpPermission.DLPFile)[][]> =
40df226684Sopenharmony_ci      GlobalContext.load('sandbox2linkFile') as Map<string, (number | string | dlpPermission.DLPFile)[][]>;
41df226684Sopenharmony_ci    if (sandbox2linkFile.has(key)) {
42df226684Sopenharmony_ci      try {
43df226684Sopenharmony_ci        await deleteSandbox2linkFileData(key, true);
44df226684Sopenharmony_ci      } catch (err) {
45df226684Sopenharmony_ci        HiLog.error(TAG, `deleteSandbox2linkFileData error`);
46df226684Sopenharmony_ci      }
47df226684Sopenharmony_ci      let fileOpenHistory:Map<string, (number | string)[]> =
48df226684Sopenharmony_ci        GlobalContext.load('fileOpenHistory') as Map<string, (number | string)[]>;
49df226684Sopenharmony_ci      for (let item of Array.from<(string | (number | string)[])[]>(fileOpenHistory)) {
50df226684Sopenharmony_ci        if (item.length < INDEX_TWO || Object.keys(item[INDEX_ONE]).length < INDEX_TWO) {
51df226684Sopenharmony_ci          continue;
52df226684Sopenharmony_ci        }
53df226684Sopenharmony_ci        let itemKey = item[INDEX_ZERO];
54df226684Sopenharmony_ci        let itemValue = item[INDEX_ONE];
55df226684Sopenharmony_ci        let tmp: string = (itemValue[INDEX_ZERO] as string) + (itemValue[INDEX_ONE] as number);
56df226684Sopenharmony_ci        if (tmp === key && itemValue.length > INDEX_THREE) {
57df226684Sopenharmony_ci          await deleteLinkSetData(itemValue[INDEX_THREE] as string);
58df226684Sopenharmony_ci          await deleteFileOpenHistoryData(itemKey as string);
59df226684Sopenharmony_ci        }
60df226684Sopenharmony_ci      }
61df226684Sopenharmony_ci    }
62df226684Sopenharmony_ci    if (sandbox2linkFile.size === 0) {
63df226684Sopenharmony_ci        HiLog.info(TAG, `sandbox2linkFile empty`);
64df226684Sopenharmony_ci        (GlobalContext.load('dataContext') as common.ServiceExtensionContext).terminateSelf();
65df226684Sopenharmony_ci        (AppStorage.get('viewContext') as common.ServiceExtensionContext).terminateSelf();
66df226684Sopenharmony_ci    }
67df226684Sopenharmony_ci  }
68df226684Sopenharmony_ci
69df226684Sopenharmony_ci  createSubscriber(): void {
70df226684Sopenharmony_ci    HiLog.info(TAG, `createSubscriber`);
71df226684Sopenharmony_ci    try {
72df226684Sopenharmony_ci      dlpPermission.on('uninstallDLPSandbox', (data: dlpPermission.DLPSandboxState) => {
73df226684Sopenharmony_ci        let bundleName: string = data.bundleName;
74df226684Sopenharmony_ci        let sandboxAppIndex: number = data.appIndex;
75df226684Sopenharmony_ci        let key: string = bundleName + sandboxAppIndex;
76df226684Sopenharmony_ci
77df226684Sopenharmony_ci        let token2File:Map<number, (number | string | dlpPermission.DLPFile)[]> =
78df226684Sopenharmony_ci          GlobalContext.load('token2File') as Map<number, (number | string | dlpPermission.DLPFile)[]>;
79df226684Sopenharmony_ci        for (let item of Array.from<(number | (number | string | dlpPermission.DLPFile)[])[]>(token2File)) {
80df226684Sopenharmony_ci          if (item.length < INDEX_TWO || Object.keys(item[INDEX_ONE]).length < INDEX_THREE) {
81df226684Sopenharmony_ci            continue;
82df226684Sopenharmony_ci          }
83df226684Sopenharmony_ci          let itemKey = item[INDEX_ZERO];
84df226684Sopenharmony_ci          let itemValue = item[INDEX_ONE] as Record<number, Object>;
85df226684Sopenharmony_ci          const APP_ID: string = (itemValue[INDEX_ONE] as string) + (itemValue[INDEX_TWO] as number);
86df226684Sopenharmony_ci          if (key === APP_ID) {
87df226684Sopenharmony_ci            deleteToken2FileData(itemKey as number);
88df226684Sopenharmony_ci          }
89df226684Sopenharmony_ci        }
90df226684Sopenharmony_ci        try {
91df226684Sopenharmony_ci          this.sandbox2linkFile(key);
92df226684Sopenharmony_ci        } catch (err) {
93df226684Sopenharmony_ci          HiLog.error(TAG, `release resource error: ${JSON.stringify(err)}`);
94df226684Sopenharmony_ci        }
95df226684Sopenharmony_ci      });
96df226684Sopenharmony_ci      this.isSubscriber = true;
97df226684Sopenharmony_ci    } catch (err) {
98df226684Sopenharmony_ci      HiLog.info(TAG, `createSubscriber uninstallDLPSandbox failed: ${JSON.stringify(err)}`);
99df226684Sopenharmony_ci    }
100df226684Sopenharmony_ci  }
101df226684Sopenharmony_ci
102df226684Sopenharmony_ci  onCreate(want: Want): void {
103df226684Sopenharmony_ci    const context = this.context;
104df226684Sopenharmony_ci    GlobalContext.store('dataContext', context);
105df226684Sopenharmony_ci  }
106df226684Sopenharmony_ci
107df226684Sopenharmony_ci  onRequest(want: Want, startId: number): void {
108df226684Sopenharmony_ci    if (!this.isSubscriber) {
109df226684Sopenharmony_ci      this.createSubscriber();
110df226684Sopenharmony_ci    }
111df226684Sopenharmony_ci  }
112df226684Sopenharmony_ci
113df226684Sopenharmony_ci  onDestroy(): void {
114df226684Sopenharmony_ci    HiLog.info(TAG, `onDestroy`);
115df226684Sopenharmony_ci    if (this.isSubscriber) {
116df226684Sopenharmony_ci      HiLog.info(TAG, `cancelSubscriber uninstallDLPSandbox`);
117df226684Sopenharmony_ci      try {
118df226684Sopenharmony_ci        dlpPermission.off('uninstallDLPSandbox');
119df226684Sopenharmony_ci        this.isSubscriber = false;
120df226684Sopenharmony_ci      } catch (err) {
121df226684Sopenharmony_ci        HiLog.error(TAG, `cancelSubscriber uninstallDLPSandbox error: ${JSON.stringify(err)}`);
122df226684Sopenharmony_ci      }
123df226684Sopenharmony_ci    }
124df226684Sopenharmony_ci  }
125df226684Sopenharmony_ci}
126