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 width = want.parameters['ohos.display.width']; 43 let height = want.parameters['ohos.display.height']; 44 let navigationBarRect = { 45 left: 0, 46 top: 0, 47 width: width, 48 height: height 49 }; 50 this.createWindow('SecurityDialog' + startId, window.WindowType.TYPE_DIALOG, navigationBarRect, want); 51 } catch (exception) { 52 console.error(TAG + 'Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 53 }; 54 } 55 56 /** 57 * Lifecycle function, called back before a service extension is destroyed. 58 */ 59 onDestroy(): void { 60 console.info(TAG + 'SecurityExtensionAbility onDestroy.'); 61 } 62 63 private async createWindow(name: string, windowType, rect, want): Promise<void> { 64 console.info(TAG + 'create securityWindow'); 65 let dialogSet: Set<number> = GlobalContext.load('dialogSet'); 66 if (!dialogSet) { 67 dialogSet = new Set<number>(); 68 console.info(TAG + 'new dialogSet'); 69 GlobalContext.store('dialogSet', dialogSet); 70 } 71 let callerToken: number = want.parameters['ohos.caller.uid']; 72 if (dialogSet.has(callerToken)) { 73 console.info(TAG + 'window of ' + callerToken + ' already exists'); 74 return; 75 } 76 try { 77 const win = await window.createWindow({ ctx: this.context, name, windowType }); 78 let storage: LocalStorage = new LocalStorage({ 'want': want, 'win': win }); 79 await win.bindDialogTarget(want.parameters['ohos.ability.params.token'].value, () => { 80 win.destroyWindow(); 81 let dialogSet: Set<number> = GlobalContext.load('dialogSet'); 82 let callerToken: number = want.parameters['ohos.caller.uid']; 83 dialogSet.delete(callerToken); 84 console.info(TAG + 'window of ' + callerToken + ' is destroyed'); 85 GlobalContext.store('dialogSet', dialogSet); 86 if (dialogSet.size === 0) { 87 this.context.terminateSelf(); 88 } 89 }); 90 await win.moveWindowTo(rect.left, rect.top); 91 await win.resize(rect.width, rect.height); 92 await win.loadContent('pages/securityDialog', storage); 93 win.setWindowBackgroundColor(BG_COLOR); 94 await win.showWindow(); 95 console.info(TAG + 'showWindow end.'); 96 dialogSet.add(callerToken); 97 console.info(TAG + 'window of ' + callerToken + ' is created'); 98 GlobalContext.store('dialogSet', dialogSet); 99 } catch (err) { 100 console.error(TAG + `window create failed! err: ${JSON.stringify(err)}`); 101 } 102 } 103}; 104