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 {TRACE_SUFFIX} from '../common/constants'; 16import {ConsumerPortResponse} from './consumer_port_types'; 17 18export type ConsumerPortCallback = (_: ConsumerPortResponse) => void; 19export type ErrorCallback = (_: string) => void; 20export type StatusCallback = (_: string) => void; 21 22export abstract class RpcConsumerPort { 23 // The responses of the call invocations should be sent through this listener. 24 // This is done by the 3 "send" methods in this abstract class. 25 private consumerPortListener: Consumer; 26 27 constructor(consumerPortListener: Consumer) { 28 this.consumerPortListener = consumerPortListener; 29 } 30 31 // RequestData is the proto representing the arguments of the function call. 32 abstract handleCommand(methodName: string, requestData: Uint8Array): void; 33 34 sendMessage(data: ConsumerPortResponse) { 35 this.consumerPortListener.onConsumerPortResponse(data); 36 } 37 38 sendErrorMessage(message: string) { 39 this.consumerPortListener.onError(message); 40 } 41 42 sendStatus(status: string) { 43 this.consumerPortListener.onStatus(status); 44 } 45 46 // Allows the recording controller to customise the suffix added to recorded 47 // traces when they are downloaded. In the general case this will be 48 // .perfetto-trace however if the trace is recorded compressed if could be 49 // .perfetto-trace.gz etc. 50 getRecordedTraceSuffix(): string { 51 return TRACE_SUFFIX; 52 } 53} 54 55export interface Consumer { 56 onConsumerPortResponse(data: ConsumerPortResponse): void; 57 onError: ErrorCallback; 58 onStatus: StatusCallback; 59} 60