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_ci
16df226684Sopenharmony_ciimport { BusinessError } from '@ohos.base';
17df226684Sopenharmony_ciimport common from '@ohos.app.ability.common';
18df226684Sopenharmony_ciimport IDLDLPProperty from '../serviceExtensionAbility/sequenceable/dlpClass';
19df226684Sopenharmony_ciimport GlobalContext from '../common/GlobalContext';
20df226684Sopenharmony_ciimport {
21df226684Sopenharmony_ci  closeDlpFileCallback,
22df226684Sopenharmony_ci  sandBoxLinkFileCallback,
23df226684Sopenharmony_ci  fileOpenHistoryCallback,
24df226684Sopenharmony_ci  linkSetCallback,
25df226684Sopenharmony_ci  genDlpFileCallback,
26df226684Sopenharmony_ci  openDlpFileCallback,
27df226684Sopenharmony_ci  recoverDlpFileCallback,
28df226684Sopenharmony_ci  replaceDlpLinkFileCallback,
29df226684Sopenharmony_ci  resumeFuseLinkCallback,
30df226684Sopenharmony_ci  stopFuseLinkCallback
31df226684Sopenharmony_ci} from '../MainAbility/data/IIdlDlpRpcServiceTs/i_id_dlpRpc_service';
32df226684Sopenharmony_ciimport IdDlpRpcServiceProxy from '../MainAbility/data/IIdlDlpRpcServiceTs/id_dlpRpc_service_proxy';
33df226684Sopenharmony_ciimport Constants from '../common/constant';
34df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog';
35df226684Sopenharmony_ci
36df226684Sopenharmony_ciconst SERVICE_EXTENSION_ABILITY_NAME = 'ServiceExtAbility';
37df226684Sopenharmony_ciconst ERROR_CODE = -1;
38df226684Sopenharmony_ciconst SUCCESS_CODE = 0;
39df226684Sopenharmony_ciconst TAG = 'HomeFeature';
40df226684Sopenharmony_ci
41df226684Sopenharmony_ciexport default class HomeFeature {
42df226684Sopenharmony_ci  private connection = -1;
43df226684Sopenharmony_ci  private remoteCallback!: () => void;
44df226684Sopenharmony_ci  private context: ESObject;
45df226684Sopenharmony_ci  private options: common.ConnectOptions;
46df226684Sopenharmony_ci  private dlpRpcProxy?: IdDlpRpcServiceProxy = undefined;
47df226684Sopenharmony_ci
48df226684Sopenharmony_ci  constructor(context: ESObject) {
49df226684Sopenharmony_ci    this.context = context;
50df226684Sopenharmony_ci    this.options = {
51df226684Sopenharmony_ci      onConnect: (elementName, proxy) => {
52df226684Sopenharmony_ci        HiLog.info(TAG, `onConnect success`);
53df226684Sopenharmony_ci        this.dlpRpcProxy = new IdDlpRpcServiceProxy(proxy);
54df226684Sopenharmony_ci        GlobalContext.store('dlpRpcProxy', this.dlpRpcProxy);
55df226684Sopenharmony_ci        HiLog.info(TAG, `DLPManager IDL onConnect success: ${JSON.stringify(this.dlpRpcProxy)}`);
56df226684Sopenharmony_ci        this.remoteCallback();
57df226684Sopenharmony_ci      },
58df226684Sopenharmony_ci      onDisconnect: () => {
59df226684Sopenharmony_ci        HiLog.info(TAG, `onDisconnect`);
60df226684Sopenharmony_ci      },
61df226684Sopenharmony_ci      onFailed: () => {
62df226684Sopenharmony_ci        HiLog.info(TAG, `onFailed`);
63df226684Sopenharmony_ci      }
64df226684Sopenharmony_ci    }
65df226684Sopenharmony_ci  }
66df226684Sopenharmony_ci
67df226684Sopenharmony_ci  connectServiceExtAbility(callback: () => void) {
68df226684Sopenharmony_ci    HiLog.info(TAG, `connectServiceExtAbility`);
69df226684Sopenharmony_ci    if (this.dlpRpcProxy !== undefined) {
70df226684Sopenharmony_ci      return;
71df226684Sopenharmony_ci    }
72df226684Sopenharmony_ci    this.remoteCallback = callback;
73df226684Sopenharmony_ci    let want = {
74df226684Sopenharmony_ci      'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME,
75df226684Sopenharmony_ci      'abilityName': SERVICE_EXTENSION_ABILITY_NAME
76df226684Sopenharmony_ci    } as Record<string, string>;
77df226684Sopenharmony_ci    if (this.dlpRpcProxy === undefined) {
78df226684Sopenharmony_ci      HiLog.info(TAG, `try connect`);
79df226684Sopenharmony_ci      try {
80df226684Sopenharmony_ci        this.connection = this.context.connectServiceExtensionAbility(want, this.options);
81df226684Sopenharmony_ci      } catch (err) {
82df226684Sopenharmony_ci        HiLog.error(TAG, `connectServiceExtensionAbility failed: ${JSON.stringify(err)}`);
83df226684Sopenharmony_ci      }
84df226684Sopenharmony_ci    }
85df226684Sopenharmony_ci    HiLog.info(TAG, `connectServiceExtAbility result: ${this.connection}`);
86df226684Sopenharmony_ci  }
87df226684Sopenharmony_ci
88df226684Sopenharmony_ci  async genDlpFileHome(src:string, des:string, pop:IDLDLPProperty, callback: genDlpFileCallback) {
89df226684Sopenharmony_ci    HiLog.info(TAG, `genDlpFileHome start`);
90df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').genDlpFile(src, des, pop, callback);
91df226684Sopenharmony_ci  }
92df226684Sopenharmony_ci
93df226684Sopenharmony_ci  async openDlpFileHome(src:string, callerAppId: string, callback: openDlpFileCallback) {
94df226684Sopenharmony_ci    HiLog.info(TAG, `openDlpFileHome start`);
95df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').openDlpFile(src, callerAppId, callback);
96df226684Sopenharmony_ci  }
97df226684Sopenharmony_ci
98df226684Sopenharmony_ci  replaceDLPLinkFileHome(src:string, linkFileName:string, callback: replaceDlpLinkFileCallback) {
99df226684Sopenharmony_ci    HiLog.info(TAG, `replaceDLPLinkFileHome start`);
100df226684Sopenharmony_ci    GlobalContext.load('dlpRpcProxy').replaceDlpLinkFile(src, linkFileName, callback);
101df226684Sopenharmony_ci  }
102df226684Sopenharmony_ci
103df226684Sopenharmony_ci  async closeDLPFileHome(src:string, callback: closeDlpFileCallback) {
104df226684Sopenharmony_ci    HiLog.info(TAG, `closeDLPFileHome start`);
105df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').closeDlpFile(src, callback);
106df226684Sopenharmony_ci  }
107df226684Sopenharmony_ci
108df226684Sopenharmony_ci  resumeFuseLinkHome(src:string, callback: resumeFuseLinkCallback) {
109df226684Sopenharmony_ci    HiLog.info(TAG, `resumeFuseLinkHome start`);
110df226684Sopenharmony_ci    GlobalContext.load('dlpRpcProxy').resumeFuseLink(src, callback);
111df226684Sopenharmony_ci  }
112df226684Sopenharmony_ci
113df226684Sopenharmony_ci  stopFuseLinkHome(src:string, callback: stopFuseLinkCallback) {
114df226684Sopenharmony_ci    HiLog.info(TAG, `stopFuseLinkHome start`);
115df226684Sopenharmony_ci    GlobalContext.load('dlpRpcProxy').stopFuseLink(src, callback);
116df226684Sopenharmony_ci  }
117df226684Sopenharmony_ci
118df226684Sopenharmony_ci  async recoverDLPFileHome(src:string, pathUri: string, callback: recoverDlpFileCallback) {
119df226684Sopenharmony_ci    HiLog.info(TAG, `recoverDLPFileHome start`);
120df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').recoverDlpFile(src, pathUri, callback);
121df226684Sopenharmony_ci  }
122df226684Sopenharmony_ci
123df226684Sopenharmony_ci  async sandBoxLinkFileHome(linkFileName: string, callerToken: number, callback: sandBoxLinkFileCallback) {
124df226684Sopenharmony_ci    HiLog.info(TAG, `sandBoxLinkFileHome start`);
125df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').sandBoxLinkFile(linkFileName, callerToken, callback);
126df226684Sopenharmony_ci  }
127df226684Sopenharmony_ci
128df226684Sopenharmony_ci  async fileOpenHistoryHome(src:string, callback: fileOpenHistoryCallback) {
129df226684Sopenharmony_ci    HiLog.info(TAG, `fileOpenHistoryHome start: ${src}`);
130df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').fileOpenHistory(src, callback);
131df226684Sopenharmony_ci  }
132df226684Sopenharmony_ci
133df226684Sopenharmony_ci  async linkSetHome(src:string, callback: linkSetCallback) {
134df226684Sopenharmony_ci    HiLog.info(TAG, `linkSetHome start: ${src}`);
135df226684Sopenharmony_ci    await GlobalContext.load('dlpRpcProxy').linkSet(src, callback);
136df226684Sopenharmony_ci  }
137df226684Sopenharmony_ci
138df226684Sopenharmony_ci  disconnectServiceExtAbility(callback: (ans:number) => void) {
139df226684Sopenharmony_ci    HiLog.info(TAG, `disconnectServiceExtAbility`);
140df226684Sopenharmony_ci    if (this.context) {
141df226684Sopenharmony_ci      this.context.disconnectServiceExtensionAbility(this.connection).then(() => {
142df226684Sopenharmony_ci        callback(SUCCESS_CODE);
143df226684Sopenharmony_ci      }).catch((error: BusinessError) => {
144df226684Sopenharmony_ci        HiLog.error(TAG, `disconnectAbility failed: ${JSON.stringify(error)}`);
145df226684Sopenharmony_ci        callback(ERROR_CODE);
146df226684Sopenharmony_ci      })
147df226684Sopenharmony_ci    }
148df226684Sopenharmony_ci  };
149df226684Sopenharmony_ci}