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