1/*
2 * Copyright (c) 2023 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 extension from '@ohos.app.ability.ServiceExtensionAbility';
17import window from '@ohos.window';
18import display from '@ohos.display';
19import { GlobalContext } from '../common/utils/globalContext';
20
21const TAG = 'PermissionManager_Log:';
22const BG_COLOR = '#00000000';
23
24export default class SecurityExtensionAbility extends extension {
25  /**
26   * Lifecycle function, called back when a service extension is started for initialization.
27   */
28  onCreate(want): void {
29    console.info(TAG + 'SecurityExtensionAbility onCreate, ability name is ' + want.abilityName);
30    globalThis.windowNum = 0;
31    console.info(TAG + 'Set windowNum = 0');
32  }
33
34  /**
35   * Lifecycle function, called back when a service extension is started or recall.
36   */
37  onRequest(want, startId): void {
38    console.info(TAG + 'SecurityExtensionAbility onRequest. start id is ' + startId);
39    console.info(TAG + 'want: ' + JSON.stringify(want));
40
41    try {
42      let dis = display.getDefaultDisplaySync();
43      let navigationBarRect = {
44        left: 0,
45        top: 0,
46        width: dis.width,
47        height: dis.height
48      };
49      this.createWindow('SecurityDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want);
50    } catch (exception) {
51      console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
52    };
53  }
54
55  /**
56   * Lifecycle function, called back before a service extension is destroyed.
57   */
58  onDestroy(): void {
59    console.info(TAG + 'SecurityExtensionAbility onDestroy.');
60  }
61
62  private async createWindow(name: string, windowType, rect, want): Promise<void> {
63    console.info(TAG + 'create securityWindow');
64    let dialogSet: Set<string> = GlobalContext.load('dialogSet');
65    if (!dialogSet) {
66      dialogSet = new Set<string>();
67      console.info(TAG + 'new dialogSet');
68      GlobalContext.store('dialogSet', dialogSet);
69    }
70    let callerToken: string = want.parameters['ohos.aafwk.param.callerBundleName'];
71    if (dialogSet.has(callerToken)) {
72      console.info(TAG + 'window already exists');
73      return;
74    }
75    try {
76      const win = await window.createWindow({ ctx: this.context, name, windowType });
77      let storage: LocalStorage = new LocalStorage({ 'want': want, 'win': win });
78      await win.bindDialogTarget(want.parameters['ohos.ability.params.token'].value, () => {
79        win.destroyWindow();
80        let dialogSet: Set<string> = GlobalContext.load('dialogSet');
81        let callerToken: string = want.parameters['ohos.aafwk.param.callerBundleName'];
82        dialogSet.delete(callerToken);
83        GlobalContext.store('dialogSet', dialogSet);
84        if (dialogSet.size === 0) {
85          this.context.terminateSelf();
86        }
87      });
88      await win.moveWindowTo(rect.left, rect.top);
89      await win.resize(rect.width, rect.height);
90      await win.loadContent('pages/securityDialog', storage);
91      win.setWindowBackgroundColor(BG_COLOR);
92      await win.showWindow();
93      console.info(TAG + 'showWindow end.');
94      dialogSet.add(callerToken);
95      GlobalContext.store('dialogSet', dialogSet);
96    } catch (err) {
97      console.error(TAG + `window create failed! err: ${JSON.stringify(err)}`);
98    }
99  }
100};
101