1// Copyright (C) 2019 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 {perfetto} from '../gen/protos'; 16 17export interface Typed { 18 type: string; 19} 20 21// A type guard that can be used in order to be able to access the property of 22// an object in a checked manner. 23export function hasProperty<T extends object, P extends string>( 24 obj: T, prop: P): obj is T&{[prop in P]: unknown} { 25 return obj.hasOwnProperty(prop); 26} 27 28export function isTyped(obj: object): obj is Typed { 29 return obj.hasOwnProperty('type'); 30} 31 32export interface ReadBuffersResponse extends 33 Typed, perfetto.protos.IReadBuffersResponse {} 34export interface EnableTracingResponse extends 35 Typed, perfetto.protos.IEnableTracingResponse {} 36export interface GetTraceStatsResponse extends 37 Typed, perfetto.protos.IGetTraceStatsResponse {} 38export interface FreeBuffersResponse extends 39 Typed, perfetto.protos.IFreeBuffersResponse {} 40export interface GetCategoriesResponse extends Typed {} 41export interface DisableTracingResponse extends 42 Typed, perfetto.protos.IDisableTracingResponse {} 43 44export type ConsumerPortResponse = 45 EnableTracingResponse|ReadBuffersResponse|GetTraceStatsResponse| 46 GetCategoriesResponse|FreeBuffersResponse|DisableTracingResponse; 47 48export function isReadBuffersResponse(obj: Typed): obj is ReadBuffersResponse { 49 return obj.type === 'ReadBuffersResponse'; 50} 51 52export function isEnableTracingResponse(obj: Typed): 53 obj is EnableTracingResponse { 54 return obj.type === 'EnableTracingResponse'; 55} 56 57export function isGetTraceStatsResponse(obj: Typed): 58 obj is GetTraceStatsResponse { 59 return obj.type === 'GetTraceStatsResponse'; 60} 61 62export function isFreeBuffersResponse(obj: Typed): obj is FreeBuffersResponse { 63 return obj.type === 'FreeBuffersResponse'; 64} 65 66export function isDisableTracingResponse(obj: Typed): 67 obj is DisableTracingResponse { 68 return obj.type === 'DisableTracingResponse'; 69} 70