1/* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import {HttpResponse} from 'common/http_request'; 18import {PersistentStore} from 'common/store/persistent_store'; 19import {AdbConnectionType} from 'trace_collection/adb_connection_type'; 20import {ConnectionState} from 'trace_collection/connection_state'; 21import {ConnectionStateListener} from 'trace_collection/connection_state_listener'; 22import {AdbDeviceConnection} from './adb_device_connection'; 23 24export abstract class AdbHostConnection< 25 D extends AdbDeviceConnection = AdbDeviceConnection, 26> { 27 protected devices: D[] = []; 28 protected readonly store = new PersistentStore(); 29 abstract readonly connectionType: AdbConnectionType; 30 31 constructor(protected listener: ConnectionStateListener) { 32 this.initializeExtraParameters(); 33 } 34 35 async restart(): Promise<void> { 36 this.onDestroy(); 37 await this.setState(ConnectionState.CONNECTING); 38 } 39 40 getDevices(): D[] { 41 return this.devices; 42 } 43 44 protected async setState(newState: ConnectionState, errorText = '') { 45 if (newState === ConnectionState.ERROR) { 46 await this.listener.onError(errorText); 47 } else { 48 await this.listener.onConnectionStateChange(newState); 49 } 50 } 51 52 onDestroy() { 53 this.destroyHost(); 54 this.devices.forEach((device) => device.onDestroy()); 55 } 56 57 protected abstract initializeExtraParameters(): void; 58 protected abstract destroyHost(): void; 59 abstract setSecurityToken(token: string): void; 60 abstract requestDevices(): Promise<void>; 61 abstract cancelDeviceRequests(): void; 62} 63 64export interface AdbResponse { 65 errorState: ConnectionState; 66 errorMsg: string | undefined; 67} 68 69/** 70 * Type for the callback function that is called when a request is successful. 71 */ 72export type OnRequestSuccessCallback = ( 73 resp: HttpResponse, 74) => void | Promise<void> | Promise<Uint8Array>; 75