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