1// Copyright (C) 2024 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 {assetSrc} from '../../base/assets'; 16import {assertExists} from '../../base/logging'; 17import {PerfettoPlugin} from '../../public/plugin'; 18import {Trace} from '../../public/trace'; 19import {SqlModules} from './sql_modules'; 20import {SQL_MODULES_DOCS_SCHEMA, SqlModulesImpl} from './sql_modules_impl'; 21import {extensions} from '../../components/extensions'; 22 23export default class implements PerfettoPlugin { 24 static readonly id = 'dev.perfetto.SqlModules'; 25 private sqlModules?: SqlModules; 26 private tables?: string[]; 27 28 async onTraceLoad(ctx: Trace) { 29 this.loadJson(ctx); 30 } 31 32 private async loadJson(ctx: Trace) { 33 const x = await fetch(assetSrc('stdlib_docs.json')); 34 const json = await x.json(); 35 const docs = SQL_MODULES_DOCS_SCHEMA.parse(json); 36 const sqlModules = new SqlModulesImpl(docs); 37 38 this.sqlModules = sqlModules; 39 this.tables = sqlModules.listTablesNames(); 40 41 ctx.commands.registerCommand({ 42 id: 'perfetto.OpenSqlModulesTable', 43 name: 'Open table...', 44 callback: async () => { 45 const chosenTable = await ctx.omnibox.prompt( 46 'Choose a table...', 47 this.tables, 48 ); 49 if (chosenTable === undefined) { 50 return; 51 } 52 const module = sqlModules.getModuleForTable(chosenTable); 53 if (module === undefined) { 54 return; 55 } 56 const sqlTable = module.getSqlTableDescription(chosenTable); 57 sqlTable && 58 extensions.addLegacySqlTableTab(ctx, { 59 table: sqlTable, 60 }); 61 }, 62 }); 63 } 64 65 getSqlModules(): SqlModules { 66 return assertExists(this.sqlModules); 67 } 68 69 getSqlTables(): string[] { 70 return assertExists(this.tables); 71 } 72} 73