1// @ts-nocheck 2/* 3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import pluginComponentManager from '@ohos.pluginComponent'; 18import { Want } from 'ability/want'; 19import { ItemComponentData } from './Constants'; 20import Log from '../../default/Log'; 21 22const TAG = 'PluginComponentManagerUtil'; 23 24export class PluginComponentInfo { 25 bundleName: string; 26 moduleName: string; 27 abilityName: string; 28 template: string; 29 componentTemplate; 30 data; 31} 32 33export interface PushListener { 34 onPushPluginComponentData(pluginComponentInfo: PluginComponentInfo): Promise<void>; 35} 36 37export interface ListenerHandle { 38 unRegister(): void; 39} 40 41export async function requestFunction(owner: Want, itemData: ItemComponentData): Promise<PluginComponentInfo> { 42 Log.showDebug(TAG, `requestFunction, owner: ${JSON.stringify(owner)}`); 43 Log.showDebug(TAG, `requestFunction, itemData: ${JSON.stringify(itemData)}`); 44 let param = { 45 owner: owner, 46 want: { 47 bundleName: itemData.bundleName, abilityName: itemData.abilityName 48 }, 49 name: itemData.template, 50 data: {} 51 }; 52 let ret = await pluginComponentManager.request(param); 53 Log.showDebug(TAG, `requestFunction, callback ret: ${JSON.stringify(ret)}`); 54 if (!ret) { 55 throw new Error(); 56 } 57 let info = new PluginComponentInfo(); 58 info.bundleName = itemData.bundleName; 59 info.moduleName = itemData.moduleName; 60 info.abilityName = itemData.abilityName; 61 info.template = itemData.template; 62 info.componentTemplate = ret.componentTemplate; 63 info.data = ret.data; 64 return info; 65} 66 67export function registerPushListener(owner: Want, listener: PushListener, callback: (handle: ListenerHandle) => void): void { 68 Log.showDebug(TAG, `registerPushListener start, owner: ${JSON.stringify(owner)}`); 69 pluginComponentManager.on(owner, 'push', (source, template, data, extraData) => { 70 Log.showDebug(TAG, `onPush, source: ${JSON.stringify(source)}`); 71 Log.showDebug(TAG, `onPush, template: ${JSON.stringify(template)}`); 72 Log.showDebug(TAG, `onPush, data: ${JSON.stringify(data)}`); 73 Log.showDebug(TAG, `onPush, extraData: ${JSON.stringify(extraData)}`); 74 let info = new PluginComponentInfo(); 75 info.bundleName = source.bundleName; 76 info.abilityName = source.abilityName; 77 info.template = template.source; 78 info.componentTemplate = template; 79 info.data = data; 80 Log.showDebug(TAG, `onPush, info: ${JSON.stringify(info)}`); 81 listener.onPushPluginComponentData(info).then(() => { 82 }).catch((err) => { 83 }); 84 Log.showDebug(TAG, 'onPush end'); 85 }); 86 Log.showDebug(TAG, 'registerPushListener end'); 87 88 callback({ 89 unRegister: () => { 90 Log.showDebug(TAG, 'unRegisterPushListener start'); 91 pluginComponentManager.off(owner, (err) => { 92 Log.showDebug(TAG, `unRegisterPushListener callback err: ${JSON.stringify(err)}`); 93 }); 94 Log.showDebug(TAG, 'unRegisterPushListener end'); 95 }, 96 }); 97 Log.showDebug(TAG, 'registerPushListener callback end'); 98}