1/* 2 * Copyright (c) 2021-2022 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 { Want } from 'ability/want'; 17import Log from '../../default/Log'; 18import { ItemComponentData } from '../common/Constants'; 19import { ListenerHandle, registerPushListener, requestFunction, PluginComponentInfo 20} from '../common/PluginComponentManagerUtil'; 21 22const TAG = 'PluginSourceLoaderPatch'; 23 24export interface UpdatePluginComponentDataListener { 25 onUpdatePluginComponentData(pluginComponentData: ItemComponentData): Promise<void>; 26} 27 28export default class PluginSourceLoaderPatch { 29 mOwnerWant: Want; 30 mListener: UpdatePluginComponentDataListener; 31 mListenerHandle: ListenerHandle; 32 33 constructor(ownerWant: Want, listener: UpdatePluginComponentDataListener) { 34 Log.showInfo(TAG, `constructor, ownerWant: ${JSON.stringify(ownerWant)}`); 35 this.mOwnerWant = ownerWant; 36 this.mListener = listener; 37 registerPushListener(ownerWant, this, (handle) => { 38 this.mListenerHandle = handle; 39 }); 40 } 41 42 async onPushPluginComponentData(pluginComponentInfo: PluginComponentInfo): Promise<void> { 43 Log.showInfo(TAG, `onPushPluginComponentData, pluginComponentInfo: ${JSON.stringify(pluginComponentInfo)}`); 44 let pluginComponentData = this.pluginComponentInfoToItemComponentData(pluginComponentInfo); 45 this.mListener.onUpdatePluginComponentData(pluginComponentData).then(() => { 46 }).catch(err => { 47 }); 48 } 49 50 async requestPluginComponentData(itemData: ItemComponentData): Promise<ItemComponentData> { 51 Log.showInfo(TAG, `requestPluginComponentData, itemData: ${JSON.stringify(itemData)}`); 52 let ret = await requestFunction(this.mOwnerWant, itemData); 53 return this.pluginComponentInfoToItemComponentData(ret); 54 } 55 56 pluginComponentInfoToItemComponentData(pluginComponentInfo: PluginComponentInfo): ItemComponentData{ 57 let pluginComponentData: ItemComponentData = { 58 id: null, 59 pluginType: 3, 60 deviceId: null, 61 bundleName: pluginComponentInfo.bundleName, 62 moduleName: pluginComponentInfo.moduleName, 63 abilityName: pluginComponentInfo.abilityName, 64 abilityLabelId: null, 65 abilityIconId: null, 66 template: pluginComponentInfo.template, 67 actionData: { 68 pluginData: { 69 template: pluginComponentInfo.componentTemplate, 70 data: pluginComponentInfo.data 71 } 72 } 73 }; 74 return pluginComponentData; 75 } 76 77 clearAll(): void { 78 Log.showInfo(TAG, 'clearAll'); 79 this.mListenerHandle?.unRegister(); 80 this.mListenerHandle = undefined; 81 } 82}