Liu Song’s Projects


~/Projects/chrome-devtools-frontend

git clone https://code.lsong.org/chrome-devtools-frontend

Commit

Commit
6fcf49a45be08180797543afb1c33ef9bd321364
Author
Jack Franklin <[email protected]>
Date
2022-12-07 16:15:22 +0000 +0000
Diffstat
 front_end/core/host/InspectorFrontendHost.ts | 18 ++++++++++++++----

Make InspectorFrontendHost "worker-proof"

If a worker happens to import code from SDK, this file will also end up
in the worker context. This is mostly OK - most of the `window`
references are in called functions, but there is a reference to `window`
in the top level which errors.

Similarly, the `constructor()` runs and therefore we need to add the
check for `document` to avoid it.

In an ideal world no worker should ever load this file, even by
accident, but if they do, let's make sure that it doesn't immediately
error.

Bug: none
Change-Id: I5d7540927c8c1eac3c64e623e254f9f5c21c94bb
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4085768
Commit-Queue: Jack Franklin <[email protected]>
Reviewed-by: Paul Irish <[email protected]>
Reviewed-by: Andres Olivares <[email protected]>


diff --git a/front_end/core/host/InspectorFrontendHost.ts b/front_end/core/host/InspectorFrontendHost.ts
index a50c22b250fd0aa6073fa8c06ccb19235b1e5aaa..fd6aaadccb7f646628d8a0e5ac6b1a0c968307a2 100644
--- a/front_end/core/host/InspectorFrontendHost.ts
+++ b/front_end/core/host/InspectorFrontendHost.ts
@@ -73,6 +73,15 @@   recordedEnumeratedHistograms: {actionName: EnumeratedHistogram, actionCode: number}[] = [];
   recordedPerformanceHistograms: {histogramName: string, duration: number}[] = [];
 
   constructor() {
+    this.#urlsBeingSaved = new Map();
+
+    // Guard against errors should this file ever be imported at the top level
+    // within a worker - in which case this constructor is run. If there's no
+    // document, we can early exit.
+    if (typeof document === 'undefined') {
+      return;
+    }
+
     function stopEventPropagation(this: InspectorFrontendHostAPI, event: KeyboardEvent): void {
       // Let browser handle Ctrl+/Ctrl- shortcuts in hosted mode.
       const zoomModifier = this.platform() === 'mac' ? event.metaKey : event.ctrlKey;
@@ -80,10 +89,10 @@       if (zoomModifier && (event.key === '+' || event.key === '-')) {
         event.stopPropagation();
       }
     }
+
     document.addEventListener('keydown', event => {
       stopEventPropagation.call(this, (event as KeyboardEvent));
     }, true);
-    this.#urlsBeingSaved = new Map();
   }
 
   platform(): string {
@@ -404,8 +413,8 @@ }
 
 // @ts-ignore Global injected by devtools-compatibility.js
 // eslint-disable-next-line @typescript-eslint/naming-convention
+ *
  * Copyright (C) 2009 Google Inc. All rights reserved.
-const OVERRIDES_FILE_SYSTEM_PATH = '/overrides' as Platform.DevToolsPath.RawPathString;
 
 class InspectorFrontendAPIImpl {
   constructor() {
@@ -452,8 +460,9 @@   let proto;
   if (!InspectorFrontendHostInstance) {
     // Instantiate stub for web-hosted mode if necessary.
     // @ts-ignore Global injected by devtools-compatibility.js
+ *
  * Copyright (C) 2009 Google Inc. All rights reserved.
-  loadCompleted(): void {
+/*
   } else {
     // Otherwise add stubs for missing methods that are declared in the interface.
     proto = InspectorFrontendHostStub.prototype;
@@ -481,7 +490,7 @@ // so the host instance should not be initialized there.
 initializeInspectorFrontendHost();
 // @ts-ignore Global injected by devtools-compatibility.js
  *
- * Redistribution and use in source and binary forms, with or without
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 })();
 
 export function isUnderTest(prefs?: {