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 {ChromeTracedTracingSession} from '../chrome_traced_tracing_session'; 16import { 17 ChromeTargetInfo, 18 OnTargetChangeCallback, 19 RecordingTargetV2, 20 TracingSession, 21 TracingSessionListener, 22} from '../recording_interfaces_v2'; 23 24export class ChromeTarget implements RecordingTargetV2 { 25 onTargetChange?: OnTargetChangeCallback; 26 private chromeCategories?: string[]; 27 28 constructor(private name: string, private targetType: 'CHROME'|'CHROME_OS') {} 29 30 getInfo(): ChromeTargetInfo { 31 return { 32 targetType: this.targetType, 33 name: this.name, 34 dataSources: 35 [{name: 'chromeCategories', descriptor: this.chromeCategories}], 36 }; 37 } 38 39 // Chrome targets are created after we check that the extension is installed, 40 // so they support tracing sessions. 41 canCreateTracingSession(): boolean { 42 return true; 43 } 44 45 async createTracingSession(tracingSessionListener: TracingSessionListener): 46 Promise<TracingSession> { 47 const tracingSession = 48 new ChromeTracedTracingSession(tracingSessionListener); 49 tracingSession.initConnection(); 50 51 if (!this.chromeCategories) { 52 // Fetch chrome categories from the extension. 53 this.chromeCategories = await tracingSession.getCategories(); 54 if (this.onTargetChange) { 55 this.onTargetChange(); 56 } 57 } 58 59 return tracingSession; 60 } 61 62 // Starts a tracing session in order to fetch chrome categories from the 63 // device. Then, it cancels the session. 64 async fetchTargetInfo(tracingSessionListener: TracingSessionListener): 65 Promise<void> { 66 const tracingSession = 67 await this.createTracingSession(tracingSessionListener); 68 tracingSession.cancel(); 69 } 70 71 disconnect(_disconnectMessage?: string): Promise<void> { 72 return Promise.resolve(undefined); 73 } 74 75 // We can connect to the Chrome target without taking the connection away 76 // from another process. 77 async canConnectWithoutContention(): Promise<boolean> { 78 return true; 79 } 80} 81