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 worker from '@ohos.worker'; 17import Log from '../../../../../../../common/src/main/ets/default/Log'; 18import PluginDataSourceManager from '../../../../../../../common/src/main/ets/plugindatasource/PluginDataSourceManager'; 19import Constants, { obtainMsg } from '../../../../../../../common/src/main/ets/plugindatasource/common/Constants'; 20import {writeFaultLog, FaultID} from '../../../../../../../common/src/main/ets/default/SysFaultLogger'; 21 22const parentPort = worker.parentPort; 23const TAG = `${parentPort.name} Worker`; 24Log.showInfo(TAG, 'Start.'); 25 26var sManager; 27 28parentPort.onmessage = (msg) => { 29 Log.showInfo(TAG, `onMessage, msg = ${JSON.stringify(msg)}`); 30 let data = msg.data; 31 switch (data.action) { 32 case Constants.INIT_CONFIG: 33 initConfig(data.data); 34 break; 35 case Constants.CLEAR_ALL: 36 clearAll(); 37 break; 38 case Constants.LOAD_DATA: 39 loadData(data.data); 40 break; 41 case Constants.UPDATE_PLUGIN_COMPONENT_DATA: 42 updatePluginComponentData(data.data); 43 break; 44 default: 45 Log.showError(TAG, 'onMessage, unknown action type.'); 46 } 47}; 48 49function initConfig(config) { 50 Log.showDebug(TAG, `initConfig, config = ${JSON.stringify(config)}`); 51 sManager = new PluginDataSourceManager({ 52 onItemAdd: (itemData) => { 53 Log.showInfo(TAG, `sManager.onItemAdd, itemData = ${JSON.stringify(itemData)}`); 54 itemData.label && (itemData.label = encodeURIComponent(itemData.label)); 55 parentPort.postMessage(obtainMsg(Constants.ADD_ITEM, itemData)); 56 }, 57 onItemRemove: (itemData) => { 58 Log.showInfo(TAG, `sManager.onItemRemove, itemData = ${JSON.stringify(itemData)}`); 59 parentPort.postMessage(obtainMsg(Constants.REMOVE_ITEM, itemData)); 60 }, 61 onLoadPluginComponentData: (itemData) => { 62 Log.showInfo(TAG, `sManager.onLoadPluginComponentData, itemData = ${JSON.stringify(itemData)}`); 63 parentPort.postMessage(obtainMsg(Constants.LOAD_PLUGIN_COMPONENT_DATA, itemData)); 64 }, 65 }); 66 sManager.initDataSource(config); 67 parentPort.postMessage(obtainMsg(Constants.INIT_FINISH, {})); 68} 69 70function clearAll() { 71 Log.showDebug(TAG, 'clearAll '); 72 sManager?.clearAll(); 73} 74 75function loadData(userId) { 76 Log.showDebug(TAG, 'loadData '); 77 sManager?.loadData(userId); 78} 79 80function updatePluginComponentData(pluginComponentData) { 81 Log.showDebug(TAG, 'updatePluginComponentData '); 82 sManager?.updatePluginComponentData(pluginComponentData); 83} 84 85parentPort.onclose = function () { 86 Log.showDebug(TAG, 'onclose'); 87}; 88 89parentPort.onmessageerror = function () { 90 Log.showError(TAG, 'onmessageerror'); 91}; 92 93parentPort.onerror = function (data) { 94 Log.showError( 95 TAG, `onerror, lineno = ${data.lineno}, msg = ${data.message}, filename = ${data.filename}, col = ${data.colno}` 96 ); 97 writeFaultLog({CORE_SYSTEM: "com.ohos.systemui", TARGET_API: "systemui", FAULT_ID: FaultID.WORKER_ERROR 98 , MSG: "Abnormal occurrence"}); 99}; 100