1// Copyright (C) 2021 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use size 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 15import m from 'mithril'; 16 17import {exists} from '../base/utils'; 18 19import {SliceDetails} from './globals'; 20 21// To display process or thread, we want to concatenate their name with ID, but 22// either can be undefined and all the cases need to be considered carefully to 23// avoid `undefined undefined` showing up in the UI. This function does such 24// concatenation. 25// 26// Result can be undefined if both name and process are, in this case result is 27// not going to be displayed in the UI. 28function getDisplayName( 29 name: string | undefined, 30 id: number | undefined, 31): string | undefined { 32 if (name === undefined) { 33 return id === undefined ? undefined : `${id}`; 34 } else { 35 return id === undefined ? name : `${name} ${id}`; 36 } 37} 38 39export abstract class SlicePanel implements m.ClassComponent { 40 protected getProcessThreadDetails(sliceInfo: SliceDetails) { 41 return new Map<string, string | undefined>([ 42 ['Thread', getDisplayName(sliceInfo.threadName, sliceInfo.tid)], 43 ['Process', getDisplayName(sliceInfo.processName, sliceInfo.pid)], 44 ['User ID', exists(sliceInfo.uid) ? String(sliceInfo.uid) : undefined], 45 ['Package name', sliceInfo.packageName], 46 /* eslint-disable @typescript-eslint/strict-boolean-expressions */ 47 [ 48 'Version code', 49 sliceInfo.versionCode ? String(sliceInfo.versionCode) : undefined, 50 ], 51 /* eslint-enable */ 52 ]); 53 } 54 55 abstract view(vnode: m.Vnode): void | m.Children; 56} 57