• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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
16/**
17 * @file
18 * @kit AbilityKit
19 */
20
21import { AsyncCallback, BusinessError } from '@ohos.base';
22import installer from '@ohos.bundle.installer';
23
24export class HashParamInner implements installer.HashParam {
25  moduleName: string = "";
26  hashValue: string = "";
27}
28
29export class PGOParamInner implements installer.PGOParam {
30  moduleName: string = "";
31  pgoFilePath: string = "";
32}
33
34export class ParametersInner implements installer.Parameters {
35  key: string = "";
36  value: string = "";
37}
38
39export class InstallParamInner implements installer.InstallParam {
40  userId?: number;
41  installFlag?: number;
42  isKeepData?: boolean;
43  hashParams?: Array<installer.HashParam>;
44  crowdtestDeadline?: number;
45  sharedBundleDirPaths?: Array<string>;
46  specifiedDistributionType?: string;
47  additionalInfo?: string;
48  pgoParams?: Array<installer.PGOParam>;
49  parameters?: Array<installer.Parameters>;
50}
51
52export class UninstallParamInner implements installer.UninstallParam {
53  bundleName: string = "";
54  versionCode?: number;
55}
56
57export class CreateAppCloneParamInner implements installer.CreateAppCloneParam {
58  userId?: number;
59  appIndex?: number;
60}
61
62export class DestroyAppCloneParamInner implements installer.DestroyAppCloneParam {
63  userId?: number;
64  parameters?: Array<installer.Parameters>;
65  constructor() {
66    super();
67  }
68  constructor(param: installer.DestroyAppCloneParam) {
69    super();
70    this.userId = param.userId;
71    this.parameters = param.parameters;
72  }
73}
74
75export class PluginParamInner implements installer.PluginParam {
76  userId?: number;
77  parameters?: Array<installer.Parameters>;
78}
79
80export class BundleInstallerInner implements installer.BundleInstaller {
81  native installNative(hapFilePaths: Array<string>, installParam: installer.InstallParam): void;
82  native uninstallNative(bundleName: string, installParam: installer.InstallParam): void;
83  native recoverNative(bundleName: string, installParam: installer.InstallParam): void;
84  native uninstallByOwnParamNative(uninstallParam: installer.UninstallParam): void;
85  native updateBundleForSelfNative(hapFilePaths: Array<string>, installParam: installer.InstallParam): void;
86  native uninstallUpdatesNative(bundleName: string, installParam: installer.InstallParam): void;
87  native addExtResourceNative(bundleName: string, filePaths: Array<string>): void;
88  native removeExtResourceNative(bundleName: string, moduleNames: Array<string>): void;
89  native createAppCloneNative(bundleName: string, createAppCloneParam: installer.CreateAppCloneParam): number;
90  native destroyAppCloneNative(bundleName: string, appIndex: number, options: installer.DestroyAppCloneParam): void;
91  native installPreexistingAppNative(bundleName: string, userId: number): void;
92  native installPluginNative(hostBundleName: string, pluginFilePaths: Array<string>, pluginParam: installer.PluginParam): void;
93  native uninstallPluginNative(hostBundleName: string, pluginBundleName: string, pluginParam: installer.PluginParam): void;
94
95  install(hapFilePaths: Array<string>, installParam?: installer.InstallParam): Promise<void> {
96      let emptyParam = new InstallParamInner();
97      let params = installParam ?? emptyParam;
98      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
99      let execFun = ():NullishType=>{ this.installNative(hapFilePaths, params); }
100      let p1 = taskpool.execute(execFun);
101      p1.then(():void =>  {
102          resolve(undefined);
103        }, (err: Error): void => {
104          reject(err as BusinessError);
105        });
106      });
107      return p;
108  }
109
110  install(hapFilePaths: Array<string>, installParam: installer.InstallParam, callback: AsyncCallback<void>): void {
111      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
112          let execFun = ():NullishType=>{ this.installNative(hapFilePaths, installParam); }
113          let p1 = taskpool.execute(execFun);
114          p1.then(() => {
115              callback(null, undefined);
116          }, (err: Error): void => {
117              callback(err as BusinessError, undefined);
118          });
119      });
120  }
121
122  install(hapFilePaths: Array<string>, callback: AsyncCallback<void>): void {
123      let emptyParam = new InstallParamInner();
124      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
125          let execFun = ():NullishType=>{ this.installNative(hapFilePaths, emptyParam); }
126          let p1 = taskpool.execute(execFun);
127          p1.then(() => {
128              callback(null, undefined);
129          }, (err: Error): void => {
130              callback(err as BusinessError, undefined);
131          });
132      });
133  }
134
135  uninstall(bundleName: string, installParam?: installer.InstallParam): Promise<void> {
136      let emptyParam = new InstallParamInner();
137      let params = installParam ?? emptyParam;
138      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
139      let execFun = ():NullishType=>{ this.uninstallNative(bundleName, params); }
140      let p1 = taskpool.execute(execFun);
141      p1.then(():void =>  {
142          resolve(undefined);
143        }, (err: Error): void => {
144          reject(err as BusinessError);
145        });
146      });
147      return p;
148  }
149
150  uninstall(bundleName: string, installParam: installer.InstallParam, callback: AsyncCallback<void>): void {
151      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
152          let execFun = ():NullishType=>{ this.uninstallNative(bundleName, installParam); }
153          let p1 = taskpool.execute(execFun);
154          p1.then(() => {
155              callback(null, undefined);
156          }, (err: Error): void => {
157              callback(err as BusinessError, undefined);
158          });
159      });
160  }
161
162  uninstall(bundleName: string, callback: AsyncCallback<void>): void {
163      let emptyParam = new InstallParamInner();
164      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
165          let execFun = ():NullishType=>{ this.uninstallNative(bundleName, emptyParam); }
166          let p1 = taskpool.execute(execFun);
167          p1.then(() => {
168              callback(null, undefined);
169          }, (err: Error): void => {
170              callback(err as BusinessError, undefined);
171          });
172      });
173  }
174
175  recover(bundleName: string, installParam?: installer.InstallParam): Promise<void> {
176      let emptyParam = new InstallParamInner();
177      let params = installParam ?? emptyParam;
178      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
179      let execFun = ():NullishType=>{ this.recoverNative(bundleName, params); }
180      let p1 = taskpool.execute(execFun);
181      p1.then(():void =>  {
182          resolve(undefined);
183        }, (err: Error): void => {
184          reject(err as BusinessError);
185        });
186      });
187      return p;
188  }
189
190  recover(bundleName: string, installParam: installer.InstallParam, callback: AsyncCallback<void>): void {
191      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
192          let execFun = ():NullishType=>{ this.recoverNative(bundleName, installParam); }
193          let p1 = taskpool.execute(execFun);
194          p1.then(() => {
195              callback(null, undefined);
196          }, (err: Error): void => {
197              callback(err as BusinessError, undefined);
198          });
199      });
200  }
201
202  recover(bundleName: string, callback: AsyncCallback<void>): void {
203      let emptyParam = new InstallParamInner();
204      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
205          let execFun = ():NullishType=>{ this.recoverNative(bundleName, emptyParam); }
206          let p1 = taskpool.execute(execFun);
207          p1.then(() => {
208              callback(null, undefined);
209          }, (err: Error): void => {
210              callback(err as BusinessError, undefined);
211          });
212      });
213  }
214
215  uninstall(uninstallParam: installer.UninstallParam): Promise<void> {
216      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
217      let execFun = ():NullishType=>{ this.uninstallByOwnParamNative(uninstallParam); }
218      let p1 = taskpool.execute(execFun);
219      p1.then(():void =>  {
220          resolve(undefined);
221        }, (err: Error): void => {
222          reject(err as BusinessError);
223        });
224      });
225      return p;
226  }
227
228  uninstall(uninstallParam: installer.UninstallParam, callback: AsyncCallback<void>): void {
229      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
230          let execFun = ():NullishType=>{ this.uninstallByOwnParamNative(uninstallParam); }
231          let p1 = taskpool.execute(execFun);
232          p1.then(() => {
233              callback(null, undefined);
234          }, (err: Error): void => {
235              callback(err as BusinessError, undefined);
236          });
237      });
238  }
239
240  updateBundleForSelf(hapFilePaths: Array<string>, installParam?: installer.InstallParam): Promise<void> {
241      let emptyParam = new InstallParamInner();
242      let params = installParam ?? emptyParam;
243      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
244      let execFun = ():NullishType=>{ this.updateBundleForSelfNative(hapFilePaths, params); }
245      let p1 = taskpool.execute(execFun);
246      p1.then(():void =>  {
247          resolve(undefined);
248        }, (err: Error): void => {
249          reject(err as BusinessError);
250        });
251      });
252      return p;
253  }
254
255  updateBundleForSelf(hapFilePaths: Array<string>, installParam: installer.InstallParam, callback: AsyncCallback<void>): void {
256      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
257          let execFun = ():NullishType=>{ this.updateBundleForSelfNative(hapFilePaths, installParam); }
258          let p1 = taskpool.execute(execFun);
259          p1.then(() => {
260              callback(null, undefined);
261          }, (err: Error): void => {
262              callback(err as BusinessError, undefined);
263          });
264      });
265  }
266
267  updateBundleForSelf(hapFilePaths: Array<string>, callback: AsyncCallback<void>): void {
268      let emptyParam = new InstallParamInner();
269      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
270          let execFun = ():NullishType=>{ this.updateBundleForSelfNative(hapFilePaths, emptyParam); }
271          let p1 = taskpool.execute(execFun);
272          p1.then(() => {
273              callback(null, undefined);
274          }, (err: Error): void => {
275              callback(err as BusinessError, undefined);
276          });
277      });
278  }
279
280  uninstallUpdates(bundleName: string, installParam?: installer.InstallParam): Promise<void> {
281      let emptyParam = new InstallParamInner();
282      let params = installParam ?? emptyParam;
283      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
284      let execFun = ():NullishType=>{ this.uninstallUpdatesNative(bundleName, params); }
285      let p1 = taskpool.execute(execFun);
286      p1.then(():void =>  {
287          resolve(undefined);
288        }, (err: Error): void => {
289          reject(err as BusinessError);
290        });
291      });
292      return p;
293  }
294
295  addExtResource(bundleName: string, filePaths: Array<string>): Promise<void> {
296      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
297      let execFun = ():NullishType=>{ this.addExtResourceNative(bundleName, filePaths); }
298      let p1 = taskpool.execute(execFun);
299      p1.then(():void =>  {
300          resolve(undefined);
301        }, (err: Error): void => {
302          reject(err as BusinessError);
303        });
304      });
305      return p;
306  }
307
308  removeExtResource(bundleName: string, moduleNames: Array<string>): Promise<void> {
309      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
310      let execFun = ():NullishType=>{ this.removeExtResourceNative(bundleName, moduleNames); }
311      let p1 = taskpool.execute(execFun);
312      p1.then(():void =>  {
313          resolve(undefined);
314        }, (err: Error): void => {
315          reject(err as BusinessError);
316        });
317      });
318      return p;
319  }
320
321  createAppClone(bundleName: string, createAppCloneParam?: installer.CreateAppCloneParam): Promise<number> {
322      let emptyParam = new CreateAppCloneParamInner();
323      let params = createAppCloneParam ?? emptyParam;
324      let p = new Promise<number>((resolve: (v:number) => void, reject: (error: BusinessError) => void):void => {
325      let execFun = ():number=>{ return this.createAppCloneNative(bundleName, emptyParam); }
326      let p1 = taskpool.execute(execFun);
327      p1.then((appIdx: NullishType) => {
328          resolve(appIdx as number);
329        }, (err: Error): void => {
330          reject(err as BusinessError);
331        });
332      });
333      return p;
334  }
335
336  destroyAppClone(bundleName: string, appIndex: number, options?: number | installer.DestroyAppCloneParam): Promise<void> {
337      let defaultParam = new DestroyAppCloneParamInner();
338      let option = options ?? defaultParam;
339      if (option instanceof installer.DestroyAppCloneParam) {
340        defaultParam = new DestroyAppCloneParamInner(option);
341      } else if (typeof option === "number") {
342        defaultParam.userId = option;
343      }
344      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
345      let execFun = ():NullishType=>{
346          this.destroyAppCloneNative(bundleName, appIndex, defaultParam);
347      }
348      let p1 = taskpool.execute(execFun);
349      p1.then(():void =>  {
350          resolve(undefined);
351        }, (err: Error): void => {
352          reject(err as BusinessError);
353        });
354      });
355      return p;
356  }
357
358  installPreexistingApp(bundleName: string, userId?: number): Promise<void> {
359      let userIdNum = userId ?? -500;
360      let p = new Promise<void>((resolve: (v:undefined) => void, reject: (error: BusinessError) => void):void => {
361      let execFun = ():NullishType=>{ this.installPreexistingAppNative(bundleName, userIdNum); }
362      let p1 = taskpool.execute(execFun);
363      p1.then(():void =>  {
364          resolve(undefined);
365        }, (err: Error): void => {
366          reject(err as BusinessError);
367        });
368      });
369      return p;
370  }
371
372  installPlugin(hostBundleName: string, pluginFilePaths: Array<string>, pluginParam?: installer.PluginParam): Promise<void> {
373    let emptyParam = new PluginParamInner();
374    let params = pluginParam ?? emptyParam;
375    let p = new Promise<void> ((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => {
376      let execFun = (): void => { return this.installPluginNative(hostBundleName, pluginFilePaths, params); }
377      let p1 = taskpool.execute(execFun);
378      p1.then((): void => {
379        resolve(undefined);
380      }, (err: Error): void => {
381        reject(err as BusinessError);
382      });
383    });
384    return p;
385  }
386
387  uninstallPlugin(hostBundleName: string, pluginBundleName: string, pluginParam?: installer.PluginParam): Promise<void> {
388    let emptyParam = new PluginParamInner();
389    let params = pluginParam ?? emptyParam;
390    let p = new Promise<void> ((resolve: (v: undefined) => void, reject: (error: BusinessError) => void): void => {
391      let execFun = (): void => { return this.uninstallPluginNative(hostBundleName, pluginBundleName, params); }
392      let p1 = taskpool.execute(execFun);
393      p1.then((): void => {
394        resolve(undefined);
395      }, (err: Error): void => {
396        reject(err as BusinessError);
397      });
398    });
399    return p;
400  }
401}