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 { GlobalContext } from '../common/utils/globalContext'; 19import { Configuration } from '@ohos.app.ability.Configuration'; 20import Want from '@ohos.app.ability.Want'; 21import { Property, NavigationBarRect } from '../common/model/typedef'; 22import { Log } from '../common/utils/utils'; 23 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: Want): void { 31 Log.info(`SecurityExtensionAbility onCreate, ability name is ${want.abilityName}.`); 32 globalThis.windowNum = 0; 33 } 34 35 /** 36 * Lifecycle function, called back when a service extension is started or recall. 37 */ 38 onRequest(want: Want, startId: number): void { 39 Log.info(`SecurityExtensionAbility onRequest. start id is ${startId}.`); 40 41 try { 42 let width = want.parameters?.['ohos.display.width'] as number ?? 0; 43 let height = want.parameters?.['ohos.display.height'] as number ?? 0; 44 let navigationBarRect: 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 Log.error(`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 Log.info('SecurityExtensionAbility onDestroy.'); 61 } 62 63 onConfigurationUpdate(newConfig: Configuration): void { 64 Log.info(`onConfigurationUpdate: ${JSON.stringify(newConfig)}.`); 65 } 66 67 private async createWindow(name: string, windowType: window.WindowType, rect: NavigationBarRect, 68 want: Want): Promise<void> { 69 Log.info('create securityWindow.'); 70 let dialogSet: Set<String> = GlobalContext.load('dialogSet'); 71 if (!dialogSet) { 72 dialogSet = new Set<String>(); 73 Log.info('new dialogSet.'); 74 GlobalContext.store('dialogSet', dialogSet); 75 } 76 let callerToken: number = want.parameters?.['ohos.caller.uid'] as number; 77 let windId: number = want.parameters?.['ohos.ability.params.windowId'] as number; 78 let token: String = String(callerToken) + '_' + String(windId); 79 if (dialogSet.has(token)) { 80 Log.info('window already exists.'); 81 return; 82 } 83 try { 84 const win = await window.createWindow({ ctx: this.context, name, windowType }); 85 let property: Record<string, Object> = { 'want': want, 'win': win }; 86 let storage: LocalStorage = new LocalStorage(property); 87 let tokenValue = want.parameters?.['ohos.ability.params.token'] as Property; 88 await win.bindDialogTarget(tokenValue.value, () => { 89 win.destroyWindow(); 90 let dialogSet: Set<String> = GlobalContext.load('dialogSet'); 91 let callerToken: number = want.parameters?.['ohos.caller.uid'] as number; 92 let windId: number = want.parameters?.['ohos.ability.params.windowId'] as number; 93 let token: String = String(callerToken) + '_' + String(windId); 94 dialogSet.delete(token); 95 GlobalContext.store('dialogSet', dialogSet); 96 if (dialogSet.size === 0) { 97 this.context.terminateSelf(); 98 } 99 }); 100 try { 101 await win.setFollowParentWindowLayoutEnabled(true); 102 } catch (error) { 103 Log.error(`setFollowParentWindowLayoutEnabled error: ${JSON.stringify(error)}.`); 104 await win.moveWindowTo(rect.left, rect.top); 105 await win.resize(rect.width, rect.height); 106 }; 107 await win.loadContent('pages/securityDialog', storage); 108 win.setWindowBackgroundColor(BG_COLOR); 109 await win.showWindow(); 110 Log.info('showWindow end.'); 111 dialogSet.add(token); 112 GlobalContext.store('dialogSet', dialogSet); 113 } catch (err) { 114 Log.error(`window create failed! err: ${JSON.stringify(err)}.`); 115 }; 116 } 117}; 118