1// Copyright (C) 2022 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 { 16 ADB_ENDPOINT, 17 DEFAULT_WEBSOCKET_URL, 18 TRACED_ENDPOINT, 19} from '../../frontend/recording/recording_ui_utils'; 20 21import {TargetFactory} from './recording_interfaces_v2'; 22import { 23 ANDROID_WEBSOCKET_TARGET_FACTORY, 24 AndroidWebsocketTargetFactory, 25} from './target_factories/android_websocket_target_factory'; 26import { 27 HOST_OS_TARGET_FACTORY, 28 HostOsTargetFactory, 29} from './target_factories/host_os_target_factory'; 30import {targetFactoryRegistry} from './target_factory_registry'; 31 32// The WebsocketMenuController will handle paths for all factories which 33// connect over websocket. At present, these are: 34// - adb websocket factory 35// - host OS websocket factory 36export class WebsocketMenuController { 37 private path: string = DEFAULT_WEBSOCKET_URL; 38 39 getPath(): string { 40 return this.path; 41 } 42 43 setPath(path: string): void { 44 this.path = path; 45 } 46 47 onPathChange(): void { 48 if (targetFactoryRegistry.has(ANDROID_WEBSOCKET_TARGET_FACTORY)) { 49 const androidTargetFactory = 50 targetFactoryRegistry.get(ANDROID_WEBSOCKET_TARGET_FACTORY) as 51 AndroidWebsocketTargetFactory; 52 androidTargetFactory.tryEstablishWebsocket(this.path + ADB_ENDPOINT); 53 } 54 55 if (targetFactoryRegistry.has(HOST_OS_TARGET_FACTORY)) { 56 const hostTargetFactory = 57 targetFactoryRegistry.get(HOST_OS_TARGET_FACTORY) as 58 HostOsTargetFactory; 59 hostTargetFactory.tryEstablishWebsocket(this.path + TRACED_ENDPOINT); 60 } 61 } 62 63 getTargetFactories(): TargetFactory[] { 64 const targetFactories = []; 65 if (targetFactoryRegistry.has(ANDROID_WEBSOCKET_TARGET_FACTORY)) { 66 targetFactories.push( 67 targetFactoryRegistry.get(ANDROID_WEBSOCKET_TARGET_FACTORY)); 68 } 69 if (targetFactoryRegistry.has(HOST_OS_TARGET_FACTORY)) { 70 targetFactories.push(targetFactoryRegistry.get(HOST_OS_TARGET_FACTORY)); 71 } 72 return targetFactories; 73 } 74} 75