Skip to content

Commit 167912d

Browse files
huntiepull[bot]
authored andcommitted
Simplify debugger launch flow to invoke Chrome directly, drop kill API (#44672)
Summary: Pull Request resolved: #44672 Swaps out and simplifies the internals of the debugger launch flow. We observed that we could achieve better launch/windowing behaviour by passing the `--app` argument directly to the detected Chrome path. This shares the user's default Chrome profile: - Fixes unwanted behaviour such as a separate dock icon on macOS (which, when clicked, would launch an unwanted empty window). - Enables settings persistence. This change also removes the `LaunchedBrowser.kill` API. Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D57726649 fbshipit-source-id: fc3a715dc852a50559048d1d1c378f64aeb2013f
1 parent c4ae96e commit 167912d

File tree

7 files changed

+32
-87
lines changed

7 files changed

+32
-87
lines changed

flow-typed/npm/temp-dir_2.x.x.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/dev-middleware/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
"open": "^7.0.3",
3434
"selfsigned": "^2.4.1",
3535
"serve-static": "^1.13.1",
36-
"temp-dir": "^2.0.0",
3736
"ws": "^6.2.2"
3837
},
3938
"engines": {

packages/dev-middleware/src/index.flow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
export {default as createDevMiddleware} from './createDevMiddleware';
1313

14-
export type {BrowserLauncher, LaunchedBrowser} from './types/BrowserLauncher';
14+
export type {BrowserLauncher} from './types/BrowserLauncher';
1515
export type {EventReporter, ReportableEvent} from './types/EventReporter';
1616
export type {
1717
CustomMessageHandler,

packages/dev-middleware/src/middleware/openDebuggerMiddleware.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111

1212
import type {InspectorProxyQueries} from '../inspector-proxy/InspectorProxy';
13-
import type {BrowserLauncher, LaunchedBrowser} from '../types/BrowserLauncher';
13+
import type {BrowserLauncher} from '../types/BrowserLauncher';
1414
import type {EventReporter} from '../types/EventReporter';
1515
import type {Experiments} from '../types/Experiments';
1616
import type {Logger} from '../types/Logger';
@@ -20,8 +20,6 @@ import type {IncomingMessage, ServerResponse} from 'http';
2020
import getDevToolsFrontendUrl from '../utils/getDevToolsFrontendUrl';
2121
import url from 'url';
2222

23-
const debuggerInstances = new Map<string, ?LaunchedBrowser>();
24-
2523
type Options = $ReadOnly<{
2624
serverBaseUrl: string,
2725
logger?: Logger,
@@ -117,20 +115,12 @@ export default function openDebuggerMiddleware({
117115
try {
118116
switch (launchType) {
119117
case 'launch':
120-
const frontendInstanceId =
121-
device != null
122-
? 'device:' + device
123-
: 'app:' + (appId ?? '<null>');
124-
await debuggerInstances.get(frontendInstanceId)?.kill();
125-
debuggerInstances.set(
126-
frontendInstanceId,
127-
await browserLauncher.launchDebuggerAppWindow(
128-
getDevToolsFrontendUrl(
129-
experiments,
130-
target.webSocketDebuggerUrl,
131-
serverBaseUrl,
132-
{launchId, useFuseboxEntryPoint},
133-
),
118+
await browserLauncher.launchDebuggerAppWindow(
119+
getDevToolsFrontendUrl(
120+
experiments,
121+
target.webSocketDebuggerUrl,
122+
serverBaseUrl,
123+
{launchId, useFuseboxEntryPoint},
134124
),
135125
);
136126
res.end();

packages/dev-middleware/src/types/BrowserLauncher.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
* @oncall react_native
1010
*/
1111

12-
/**
13-
* Represents a launched web browser instance.
14-
*/
15-
export type LaunchedBrowser = {
16-
kill: () => void | Promise<void>,
17-
...
18-
};
19-
2012
/**
2113
* An interface for integrators to provide a custom implementation for
2214
* opening URLs in a web browser.
@@ -27,5 +19,5 @@ export interface BrowserLauncher {
2719
* optionally returning an object to control the launched browser instance.
2820
* The browser used should be capable of running Chrome DevTools.
2921
*/
30-
launchDebuggerAppWindow: (url: string) => Promise<LaunchedBrowser | void>;
22+
launchDebuggerAppWindow: (url: string) => Promise<void>;
3123
}

packages/dev-middleware/src/utils/DefaultBrowserLauncher.js

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
* @oncall react_native
1010
*/
1111

12-
import type {BrowserLauncher, LaunchedBrowser} from '../types/BrowserLauncher';
13-
14-
import {promises as fs} from 'fs';
15-
import path from 'path';
16-
import osTempDir from 'temp-dir';
12+
import type {BrowserLauncher} from '../types/BrowserLauncher';
1713

14+
const {spawn} = require('child_process');
1815
const ChromeLauncher = require('chrome-launcher');
1916
const {Launcher: EdgeLauncher} = require('chromium-edge-launcher');
2017

@@ -27,15 +24,13 @@ const DefaultBrowserLauncher: BrowserLauncher = {
2724
* Attempt to open the debugger frontend in a Google Chrome or Microsoft Edge
2825
* app window.
2926
*/
30-
launchDebuggerAppWindow: async (url: string): Promise<LaunchedBrowser> => {
31-
let browserType = 'chrome';
27+
launchDebuggerAppWindow: async url => {
3228
let chromePath;
3329

3430
try {
3531
// Locate Chrome installation path, will throw if not found
3632
chromePath = ChromeLauncher.getChromePath();
3733
} catch (e) {
38-
browserType = 'edge';
3934
chromePath = EdgeLauncher.getFirstInstallation();
4035

4136
if (chromePath == null) {
@@ -47,40 +42,28 @@ const DefaultBrowserLauncher: BrowserLauncher = {
4742
}
4843
}
4944

50-
const userDataDir = await createTempDir(
51-
`react-native-debugger-frontend-${browserType}`,
52-
);
53-
const launchedChrome = await ChromeLauncher.launch({
54-
ignoreDefaultFlags: true,
55-
chromeFlags: [
56-
...ChromeLauncher.Launcher.defaultFlags().filter(
57-
/**
58-
* This flag controls whether Chrome treats a visually covered (occluded) tab
59-
* as "backgrounded". We launch CDT as a single tab/window via `--app`, so we
60-
* do want Chrome to treat our tab as "backgrounded" when the UI is covered.
61-
* Omitting this flag allows "visibilitychange" events to fire properly.
62-
*/
63-
flag => flag !== '--disable-backgrounding-occluded-windows',
64-
),
65-
`--app=${url}`,
66-
`--user-data-dir=${userDataDir}`,
67-
'--window-size=1200,600',
68-
],
69-
chromePath,
70-
});
45+
const chromeFlags = [`--app=${url}`, '--window-size=1200,600'];
7146

72-
return {
73-
kill: async () => launchedChrome.kill(),
74-
};
47+
return new Promise((resolve, reject) => {
48+
const childProcess = spawn(chromePath, chromeFlags, {
49+
detached: true,
50+
stdio: 'ignore',
51+
});
52+
53+
childProcess.on('data', () => {
54+
resolve();
55+
});
56+
childProcess.on('close', (code: number) => {
57+
if (code !== 0) {
58+
reject(
59+
new Error(
60+
`Failed to launch debugger app window: ${chromePath} exited with code ${code}`,
61+
),
62+
);
63+
}
64+
});
65+
});
7566
},
7667
};
7768

78-
async function createTempDir(dirName: string): Promise<string> {
79-
const tempDir = path.join(osTempDir, dirName);
80-
81-
await fs.mkdir(tempDir, {recursive: true});
82-
83-
return tempDir;
84-
}
85-
8669
export default DefaultBrowserLauncher;

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9567,11 +9567,6 @@ tar@^6.1.11:
95679567
mkdirp "^1.0.3"
95689568
yallist "^4.0.0"
95699569

9570-
temp-dir@^2.0.0:
9571-
version "2.0.0"
9572-
resolved "https://registry.yarnpkg.com/temp-dir/-/temp-dir-2.0.0.tgz#bde92b05bdfeb1516e804c9c00ad45177f31321e"
9573-
integrity sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==
9574-
95759570
temp@^0.8.4:
95769571
version "0.8.4"
95779572
resolved "https://registry.yarnpkg.com/temp/-/temp-0.8.4.tgz#8c97a33a4770072e0a05f919396c7665a7dd59f2"

0 commit comments

Comments
 (0)