1// Copyright (C) 2023 The Android Open Source Project 2// 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 15import {Trace} from '../../public/trace'; 16import {App} from '../../public/app'; 17import {MetricVisualisation} from '../../public/plugin'; 18import {PerfettoPlugin} from '../../public/plugin'; 19 20// SKELETON: Rename this class to match your plugin. 21export default class implements PerfettoPlugin { 22 // SKELETON: Update pluginId to match the directory of the plugin. 23 static readonly id = 'com.example.Skeleton'; 24 25 /** 26 * This hook is called when the plugin is activated manually, or when the UI 27 * starts up with this plugin enabled. This is typically before a trace has 28 * been loaded, so there is no trace information in the passed plugin context 29 * object. 30 * 31 * This hook should be used for adding commands that don't depend on the 32 * trace. 33 */ 34 static onActivate(app: App): void { 35 console.log('SkeletonPlugin::onActivate()', app.pluginId); 36 } 37 38 /** 39 * This hook is called as the trace is loading. At this point the trace is 40 * loaded into trace processor and it's ready to process queries. This hook 41 * should be used for adding tracks and commands that depend on the trace. 42 * 43 * It should not be used for finding tracks from other plugins as there is no 44 * guarantee those tracks will have been added yet. 45 */ 46 async onTraceLoad(trace: Trace): Promise<void> { 47 console.log('SkeletonPlugin::onTraceLoad()', trace.traceInfo.traceTitle); 48 49 // This is an example of how to access the pluginArgs pushed by the 50 // postMessage when deep-linking to the UI. 51 if (trace.openerPluginArgs !== undefined) { 52 console.log( 53 `Postmessage args for ${trace.pluginId}`, 54 trace.openerPluginArgs, 55 ); 56 } 57 58 /** 59 * The 'traceready' event is fired when the trace has finished loading, and 60 * all plugins have returned from their onTraceLoad calls. The UI can be 61 * considered 'ready' at this point. All tracks and commands should now be 62 * available, and the timeline is ready to use. 63 * 64 * This is where any automations should be done - things that you would 65 * usually do manually after the trace has loaded but you'd like to automate 66 * them. 67 * 68 * Examples of things that could be done here: 69 * - Pinning tracks 70 * - Focusing on a slice 71 * - Adding debug tracks 72 * 73 * Postmessage args might be useful here - e.g. if you would like to pin a 74 * specific track, pass the track details through the postmessage args 75 * interface and react to it here. 76 * 77 * Note: Any tracks registered in this hook will not be displayed in the 78 * timeline, unless they are manually added through the ctx.timeline API. 79 * However this part of the code is in flux at the moment and the semantics 80 * of how this works might change, though it's still good practice to use 81 * the onTraceLoad hook to add tracks as it means that all tracks are 82 * available by the time this hook gets called. 83 * 84 * TODO(stevegolton): Update this comment if the semantics of track adding 85 * changes. 86 */ 87 trace.onTraceReady.addListener(async () => { 88 console.log('SkeletonPlugin::traceready'); 89 }); 90 } 91 92 static metricVisualisations(): MetricVisualisation[] { 93 return []; 94 } 95} 96