Reduce use of any/nulls from proxy connection and proxy client.

Bug: 264508345
Test: n/a
Change-Id: I8dfeea001ece9df426d269155d8c5888c4147ef8
diff --git a/tools/winscope/src/trace_collection/on_request_success_callback.ts b/tools/winscope/src/trace_collection/on_request_success_callback.ts
new file mode 100644
index 0000000..db36218
--- /dev/null
+++ b/tools/winscope/src/trace_collection/on_request_success_callback.ts
@@ -0,0 +1,19 @@
+/*
+ * Copyright 2024, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export type OnRequestSuccessCallback = (
+  request: XMLHttpRequest,
+) => void | Promise<void>;
diff --git a/tools/winscope/src/trace_collection/proxy_client.ts b/tools/winscope/src/trace_collection/proxy_client.ts
index 1c97ab3..0c6c44b 100644
--- a/tools/winscope/src/trace_collection/proxy_client.ts
+++ b/tools/winscope/src/trace_collection/proxy_client.ts
@@ -16,6 +16,7 @@
 
 import {OnProgressUpdateType} from 'common/function_utils';
 import {PersistentStore} from 'common/persistent_store';
+import {OnRequestSuccessCallback} from './on_request_success_callback';
 import {ConfigMap} from './trace_collection_utils';
 
 export interface Device {
@@ -61,9 +62,9 @@
   async call(
     method: string,
     path: string,
-    onSuccess: ((request: XMLHttpRequest) => void | Promise<void>) | undefined,
+    onSuccess: OnRequestSuccessCallback | undefined,
     type?: XMLHttpRequest['responseType'],
-    jsonRequest: any = null,
+    jsonRequest?: object,
   ): Promise<void> {
     return new Promise((resolve) => {
       const request = new XMLHttpRequest();
@@ -139,41 +140,47 @@
     );
   }
 
-  async setEnabledConfig(view: any, req: string[]) {
+  async setEnabledConfig(view: ProxyClient, req: string[]) {
     await proxyRequest.call(
       'POST',
-      `${ProxyEndpoint.ENABLE_CONFIG_TRACE}${view.proxy.selectedDevice}/`,
+      `${ProxyEndpoint.ENABLE_CONFIG_TRACE}${view.selectedDevice}/`,
       undefined,
       undefined,
       req,
     );
   }
 
-  async setSelectedConfig(endpoint: ProxyEndpoint, view: any, req: ConfigMap) {
+  async setSelectedConfig(
+    endpoint: ProxyEndpoint,
+    view: ProxyClient,
+    req: ConfigMap,
+  ) {
     await proxyRequest.call(
       'POST',
-      `${endpoint}${view.proxy.selectedDevice}/`,
+      `${endpoint}${view.selectedDevice}/`,
       undefined,
       undefined,
       req,
     );
   }
 
-  async startTrace(view: any, requestedTraces: string[]) {
+  async startTrace(
+    view: ProxyClient,
+    requestedTraces: string[],
+    onSuccessStartTrace: OnRequestSuccessCallback,
+  ) {
     this.tracingTraces = requestedTraces;
     await proxyRequest.call(
       'POST',
-      `${ProxyEndpoint.START_TRACE}${view.proxy.selectedDevice}/`,
-      (request: XMLHttpRequest) => {
-        view.keepAliveTrace(view);
-      },
+      `${ProxyEndpoint.START_TRACE}${view.selectedDevice}/`,
+      onSuccessStartTrace,
       undefined,
       requestedTraces,
     );
   }
 
   async endTrace(
-    view: any,
+    view: ProxyClient,
     progressCallback: OnProgressUpdateType,
   ): Promise<void> {
     const requestedTraces = this.tracingTraces;
@@ -183,7 +190,7 @@
     }
     await proxyRequest.call(
       'POST',
-      `${ProxyEndpoint.END_TRACE}${view.proxy.selectedDevice}/`,
+      `${ProxyEndpoint.END_TRACE}${view.selectedDevice}/`,
       async (request: XMLHttpRequest) => {
         await proxyClient.updateAdbData(
           requestedTraces,
@@ -194,28 +201,25 @@
     );
   }
 
-  async keepTraceAlive(view: any) {
-    await this.call(
+  async keepTraceAlive(
+    view: ProxyClient,
+    onSuccessKeepTraceAlive: OnRequestSuccessCallback,
+  ) {
+    await proxyRequest.call(
       'GET',
-      `${ProxyEndpoint.STATUS}${view.proxy.selectedDevice}/`,
-      (request: XMLHttpRequest) => {
-        if (request.responseText !== 'True') {
-          view.endTrace();
-        } else if (view.keep_alive_worker === null) {
-          view.keep_alive_worker = setInterval(view.keepAliveTrace, 1000, view);
-        }
-      },
+      `${ProxyEndpoint.STATUS}${view.selectedDevice}/`,
+      onSuccessKeepTraceAlive,
     );
   }
 
   async dumpState(
-    view: any,
+    view: ProxyClient,
     requestedDumps: string[],
     progressCallback: OnProgressUpdateType,
   ) {
     await proxyRequest.call(
       'POST',
-      `${ProxyEndpoint.DUMP}${view.proxy.selectedDevice}/`,
+      `${ProxyEndpoint.DUMP}${view.selectedDevice}/`,
       async (request: XMLHttpRequest) => {
         await proxyClient.updateAdbData(
           requestedDumps,
@@ -228,7 +232,46 @@
     );
   }
 
-  onSuccessGetDevices = async (request: XMLHttpRequest) => {
+  async fetchFiles(dev: string, adbParams: AdbParams): Promise<void> {
+    const files = adbParams.files;
+    const idx = adbParams.idx;
+
+    await proxyRequest.call(
+      'GET',
+      `${ProxyEndpoint.FETCH}${dev}/${files[idx]}/`,
+      this.onSuccessFetchFiles,
+      'arraybuffer',
+    );
+  }
+
+  private onSuccessFetchFiles: OnRequestSuccessCallback = async (
+    request: XMLHttpRequest,
+  ) => {
+    try {
+      const enc = new TextDecoder('utf-8');
+      const resp = enc.decode(request.response);
+      const filesByType = JSON.parse(resp);
+
+      for (const filetype of Object.keys(filesByType)) {
+        const files = filesByType[filetype];
+        for (const encodedFileBuffer of files) {
+          const buffer = Uint8Array.from(atob(encodedFileBuffer), (c) =>
+            c.charCodeAt(0),
+          );
+          const blob = new Blob([buffer]);
+          const newFile = new File([blob], filetype);
+          proxyClient.adbData.push(newFile);
+        }
+      }
+    } catch (error) {
+      proxyClient.setState(ProxyState.ERROR, request.responseText);
+      throw error;
+    }
+  };
+
+  private onSuccessGetDevices: OnRequestSuccessCallback = async (
+    request: XMLHttpRequest,
+  ) => {
     const client = proxyClient;
     try {
       client.devices = JSON.parse(request.responseText);
@@ -247,39 +290,6 @@
       client.setState(ProxyState.ERROR, client.errorText);
     }
   };
-
-  async fetchFiles(dev: string, adbParams: AdbParams): Promise<void> {
-    const files = adbParams.files;
-    const idx = adbParams.idx;
-
-    await proxyRequest.call(
-      'GET',
-      `${ProxyEndpoint.FETCH}${dev}/${files[idx]}/`,
-      async (request: XMLHttpRequest) => {
-        try {
-          const enc = new TextDecoder('utf-8');
-          const resp = enc.decode(request.response);
-          const filesByType = JSON.parse(resp);
-
-          for (const filetype of Object.keys(filesByType)) {
-            const files = filesByType[filetype];
-            for (const encodedFileBuffer of files) {
-              const buffer = Uint8Array.from(atob(encodedFileBuffer), (c) =>
-                c.charCodeAt(0),
-              );
-              const blob = new Blob([buffer]);
-              const newFile = new File([blob], filetype);
-              proxyClient.adbData.push(newFile);
-            }
-          }
-        } catch (error) {
-          proxyClient.setState(ProxyState.ERROR, request.responseText);
-          throw error;
-        }
-      },
-      'arraybuffer',
-    );
-  }
 }
 export const proxyRequest = new ProxyRequest();
 
diff --git a/tools/winscope/src/trace_collection/proxy_connection.ts b/tools/winscope/src/trace_collection/proxy_connection.ts
index 9c8980f..d01b758 100644
--- a/tools/winscope/src/trace_collection/proxy_connection.ts
+++ b/tools/winscope/src/trace_collection/proxy_connection.ts
@@ -32,7 +32,7 @@
 
 export class ProxyConnection implements Connection {
   proxy = proxyClient;
-  keep_alive_worker: any = null;
+  keep_alive_worker: NodeJS.Timer | undefined;
   notConnected = [
     ProxyState.NO_PROXY,
     ProxyState.UNAUTH,
@@ -136,10 +136,16 @@
   keepAliveTrace(view: ProxyConnection) {
     if (!view.isEndTraceState()) {
       clearInterval(view.keep_alive_worker);
-      view.keep_alive_worker = null;
+      view.keep_alive_worker = undefined;
       return;
     }
-    proxyRequest.keepTraceAlive(view);
+    proxyRequest.keepTraceAlive(view.proxy, (request: XMLHttpRequest) => {
+      if (request.responseText !== 'True') {
+        view.endTrace();
+      } else if (view.keep_alive_worker === undefined) {
+        view.keep_alive_worker = setInterval(view.keepAliveTrace, 1000, view);
+      }
+    });
   }
 
   async startTrace(
@@ -149,31 +155,35 @@
     reqSelectedWmConfig?: ConfigMap,
   ) {
     if (reqEnableConfig) {
-      proxyRequest.setEnabledConfig(this, reqEnableConfig);
+      proxyRequest.setEnabledConfig(this.proxy, reqEnableConfig);
     }
     if (reqSelectedSfConfig) {
       proxyRequest.setSelectedConfig(
         ProxyEndpoint.SELECTED_SF_CONFIG_TRACE,
-        this,
+        this.proxy,
         reqSelectedSfConfig,
       );
     }
     if (reqSelectedWmConfig) {
       proxyRequest.setSelectedConfig(
         ProxyEndpoint.SELECTED_WM_CONFIG_TRACE,
-        this,
+        this.proxy,
         reqSelectedWmConfig,
       );
     }
     await proxyClient.setState(ProxyState.STARTING_TRACE);
-    await proxyRequest.startTrace(this, requestedTraces);
+    await proxyRequest.startTrace(
+      this.proxy,
+      requestedTraces,
+      (request: XMLHttpRequest) => this.keepAliveTrace(this),
+    );
     proxyClient.setState(ProxyState.END_TRACE);
   }
 
   async endTrace() {
     this.progressCallback(0);
     await this.proxy.setState(ProxyState.LOAD_DATA);
-    await proxyRequest.endTrace(this, this.progressCallback);
+    await proxyRequest.endTrace(this.proxy, this.progressCallback);
   }
 
   async dumpState(requestedDumps: string[]): Promise<boolean> {
@@ -184,7 +194,11 @@
       return false;
     }
     await this.proxy.setState(ProxyState.LOAD_DATA);
-    await proxyRequest.dumpState(this, requestedDumps, this.progressCallback);
+    await proxyRequest.dumpState(
+      this.proxy,
+      requestedDumps,
+      this.progressCallback,
+    );
     return true;
   }