1e75ebbc8Sopenharmony_ci// @ts-nocheck 2e75ebbc8Sopenharmony_ci/* 3e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 4e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 5e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License. 6e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at 7e75ebbc8Sopenharmony_ci * 8e75ebbc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 9e75ebbc8Sopenharmony_ci * 10e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 11e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 12e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and 14e75ebbc8Sopenharmony_ci * limitations under the License. 15e75ebbc8Sopenharmony_ci */ 16e75ebbc8Sopenharmony_ci 17e75ebbc8Sopenharmony_ciimport Log from '../default/Log'; 18e75ebbc8Sopenharmony_ciimport SourceLoaderFactory from './sourceloader/SourceLoaderFactory'; 19e75ebbc8Sopenharmony_ciimport SourceLoader from './sourceloader/SourceLoader'; 20e75ebbc8Sopenharmony_ciimport PluginSourceLoader from './sourceloader/PluginSourceLoader'; 21e75ebbc8Sopenharmony_ciimport { FilterData, ItemComponentData, RootConfigInfo } from './common/Constants'; 22e75ebbc8Sopenharmony_ciimport { 23e75ebbc8Sopenharmony_ci AbilityInfoWithId, 24e75ebbc8Sopenharmony_ci BundleEventType, 25e75ebbc8Sopenharmony_ci ListenerHandle, 26e75ebbc8Sopenharmony_ci queryAbility, 27e75ebbc8Sopenharmony_ci registerBundleListener, 28e75ebbc8Sopenharmony_ci} from './common/BundleParseUtil'; 29e75ebbc8Sopenharmony_ciimport { AbilityInfo } from 'bundle/abilityInfo'; 30e75ebbc8Sopenharmony_ciimport { ExtensionAbilityInfo } from 'bundleManager/ExtensionAbilityInfo'; 31e75ebbc8Sopenharmony_ci 32e75ebbc8Sopenharmony_ciexport type PluginListener = { 33e75ebbc8Sopenharmony_ci onItemAdd: (itemData: ItemComponentData) => void; 34e75ebbc8Sopenharmony_ci onItemRemove: (itemData: ItemComponentData) => void; 35e75ebbc8Sopenharmony_ci onLoadPluginComponentData: (itemData: ItemComponentData) => void; 36e75ebbc8Sopenharmony_ci}; 37e75ebbc8Sopenharmony_ci 38e75ebbc8Sopenharmony_ciconst TAG = 'PluginDataSourceManager'; 39e75ebbc8Sopenharmony_ciconst INVALID_USERID = -1; 40e75ebbc8Sopenharmony_ci 41e75ebbc8Sopenharmony_ciexport default class PluginDataSourceManager { 42e75ebbc8Sopenharmony_ci mAction = ''; 43e75ebbc8Sopenharmony_ci mUserId: number = INVALID_USERID; 44e75ebbc8Sopenharmony_ci mListenerHandle: ListenerHandle | undefined = undefined; 45e75ebbc8Sopenharmony_ci mFilterDatas: Map<string, string> = new Map(); 46e75ebbc8Sopenharmony_ci mLoaders: Map<string, SourceLoader> = new Map(); 47e75ebbc8Sopenharmony_ci mFactory: SourceLoaderFactory; 48e75ebbc8Sopenharmony_ci 49e75ebbc8Sopenharmony_ci constructor(listener: PluginListener) { 50e75ebbc8Sopenharmony_ci this.mFactory = new SourceLoaderFactory({ 51e75ebbc8Sopenharmony_ci add: listener.onItemAdd, 52e75ebbc8Sopenharmony_ci remove: listener.onItemRemove, 53e75ebbc8Sopenharmony_ci onLoadPluginComponentData: listener.onLoadPluginComponentData, 54e75ebbc8Sopenharmony_ci }); 55e75ebbc8Sopenharmony_ci } 56e75ebbc8Sopenharmony_ci 57e75ebbc8Sopenharmony_ci initDataSource(configs: RootConfigInfo): void { 58e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `initDataSource, configs: ${JSON.stringify(configs)}`); 59e75ebbc8Sopenharmony_ci this.mAction = configs.action; 60e75ebbc8Sopenharmony_ci configs.filterDatas.forEach((data: FilterData) => this.mFilterDatas.set(data.abilityName, data.id)); 61e75ebbc8Sopenharmony_ci for (let pluginType in configs.loaderConfig) { 62e75ebbc8Sopenharmony_ci const sourceLoader = this.mFactory.getSourceLoader(pluginType, configs.loaderConfig[pluginType]); 63e75ebbc8Sopenharmony_ci if (sourceLoader instanceof SourceLoader) { 64e75ebbc8Sopenharmony_ci this.mLoaders.set(pluginType, sourceLoader); 65e75ebbc8Sopenharmony_ci Log.showInfo(TAG, `getSourceLoader plugin: ${pluginType}, loader${this.mLoaders.get(pluginType)}`); 66e75ebbc8Sopenharmony_ci } 67e75ebbc8Sopenharmony_ci } 68e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `action:${this.mAction}, filterData: ${JSON.stringify(this.mFilterDatas)}`); 69e75ebbc8Sopenharmony_ci registerBundleListener(this, (handle) => { 70e75ebbc8Sopenharmony_ci this.mListenerHandle = handle; 71e75ebbc8Sopenharmony_ci }); 72e75ebbc8Sopenharmony_ci } 73e75ebbc8Sopenharmony_ci 74e75ebbc8Sopenharmony_ci async onBundleNotify(bundleName: string, event: BundleEventType): Promise<void> { 75e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `onBundleNotify, bundleName: ${bundleName}, event: ${event}`); 76e75ebbc8Sopenharmony_ci if (event == BundleEventType.BUNDLE_CHANGE || event == BundleEventType.BUNDLE_REMOVE) { 77e75ebbc8Sopenharmony_ci this.mLoaders.forEach((loader) => loader.onBundleRemove(bundleName)); 78e75ebbc8Sopenharmony_ci } 79e75ebbc8Sopenharmony_ci if (event == BundleEventType.BUNDLE_CHANGE || event == BundleEventType.BUNDLE_ADD) { 80e75ebbc8Sopenharmony_ci let abilityInfos = await queryAbility(this.mAction, this.mUserId, bundleName); 81e75ebbc8Sopenharmony_ci Log.showInfo(TAG, `abilityInfos: ${JSON.stringify(abilityInfos)}`); 82e75ebbc8Sopenharmony_ci abilityInfos.forEach((info) => this.notifyAbilityAdd(info)); 83e75ebbc8Sopenharmony_ci } 84e75ebbc8Sopenharmony_ci } 85e75ebbc8Sopenharmony_ci 86e75ebbc8Sopenharmony_ci clearAll(): void { 87e75ebbc8Sopenharmony_ci Log.showDebug(TAG, 'clearAll'); 88e75ebbc8Sopenharmony_ci this.unregisterListener(); 89e75ebbc8Sopenharmony_ci this.mLoaders.forEach((sourceLoader) => sourceLoader.clearData()); 90e75ebbc8Sopenharmony_ci } 91e75ebbc8Sopenharmony_ci 92e75ebbc8Sopenharmony_ci async loadData(userId: number): Promise<void> { 93e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `loadData, userId: ${userId}, this.mUserId: ${this.mUserId}`); 94e75ebbc8Sopenharmony_ci if (this.mUserId != userId) { 95e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `loadData, queryAbility`); 96e75ebbc8Sopenharmony_ci this.mUserId = userId; 97e75ebbc8Sopenharmony_ci this.mLoaders.forEach((sourceLoader) => sourceLoader.clearData()); 98e75ebbc8Sopenharmony_ci let abilityInfos = await queryAbility(this.mAction, this.mUserId); 99e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `loadData, abilityInfos: ${JSON.stringify(abilityInfos)}`); 100e75ebbc8Sopenharmony_ci abilityInfos.forEach((info) => this.notifyAbilityAdd(info)); 101e75ebbc8Sopenharmony_ci } 102e75ebbc8Sopenharmony_ci this.mLoaders.forEach((sourceLoader) => sourceLoader.reloadData(this.mUserId)); 103e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `loadData, end`); 104e75ebbc8Sopenharmony_ci } 105e75ebbc8Sopenharmony_ci 106e75ebbc8Sopenharmony_ci private notifyAbilityAdd(info: AbilityInfo | ExtensionAbilityInfo): void { 107e75ebbc8Sopenharmony_ci Log.showDebug(TAG, `notifyAbilityAdd, info: ${JSON.stringify(info)}`); 108e75ebbc8Sopenharmony_ci let itemId = this.mFilterDatas.get(info.name); 109e75ebbc8Sopenharmony_ci if (!itemId) { 110e75ebbc8Sopenharmony_ci Log.showError(TAG, `notifyAbilityAdd, can't find itemId, ability:${info.name}`); 111e75ebbc8Sopenharmony_ci return; 112e75ebbc8Sopenharmony_ci } 113e75ebbc8Sopenharmony_ci let abilityInfo: AbilityInfoWithId = { 114e75ebbc8Sopenharmony_ci ...info, 115e75ebbc8Sopenharmony_ci itemId: itemId, 116e75ebbc8Sopenharmony_ci }; 117e75ebbc8Sopenharmony_ci if ((!abilityInfo.metaData || !abilityInfo.metaData.length) && (!abilityInfo.metadata || !abilityInfo.metadata.length)) { 118e75ebbc8Sopenharmony_ci Log.showError(TAG, `Can't find metadata, abilityId: ${abilityInfo.name}`); 119e75ebbc8Sopenharmony_ci return; 120e75ebbc8Sopenharmony_ci } 121e75ebbc8Sopenharmony_ci this.mLoaders.forEach((loader) => loader.onAbilityAdd(abilityInfo)); 122e75ebbc8Sopenharmony_ci } 123e75ebbc8Sopenharmony_ci 124e75ebbc8Sopenharmony_ci private unregisterListener(): void { 125e75ebbc8Sopenharmony_ci this.mListenerHandle?.unRegister(); 126e75ebbc8Sopenharmony_ci this.mListenerHandle = undefined; 127e75ebbc8Sopenharmony_ci } 128e75ebbc8Sopenharmony_ci 129e75ebbc8Sopenharmony_ci async updatePluginComponentData(pluginComponentData: ItemComponentData): Promise<void> { 130e75ebbc8Sopenharmony_ci Log.showInfo(TAG, 'updatePluginComponentData'); 131e75ebbc8Sopenharmony_ci this.mLoaders.forEach((loader) => { 132e75ebbc8Sopenharmony_ci if (loader instanceof PluginSourceLoader) { 133e75ebbc8Sopenharmony_ci (loader as PluginSourceLoader).onUpdatePluginComponentData(pluginComponentData); 134e75ebbc8Sopenharmony_ci } 135e75ebbc8Sopenharmony_ci }); 136e75ebbc8Sopenharmony_ci } 137e75ebbc8Sopenharmony_ci}