Skip to content

Commit ccff140

Browse files
miniaknornagon
authored andcommitted
feat: add --enable-api-filtering-logging commandline switch (electron#20335)
1 parent e1eb951 commit ccff140

15 files changed

Lines changed: 85 additions & 29 deletions

File tree

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ These individual tutorials expand on topics discussed in the guide above.
108108

109109
* [Synopsis](api/synopsis.md)
110110
* [Process Object](api/process.md)
111-
* [Supported Chrome Command Line Switches](api/chrome-command-line-switches.md)
111+
* [Supported Command Line Switches](api/command-line-switches.md)
112112
* [Environment Variables](api/environment-variables.md)
113113
* [Breaking API Changes](api/breaking-changes.md)
114114

docs/api/app.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ Overrides the current application's name.
683683

684684
Returns `String` - The current application locale. Possible return values are documented [here](locales.md).
685685

686-
To set the locale, you'll want to use a command line switch at app startup, which may be found [here](https://github.com/electron/electron/blob/master/docs/api/chrome-command-line-switches.md).
686+
To set the locale, you'll want to use a command line switch at app startup, which may be found [here](https://github.com/electron/electron/blob/master/docs/api/command-line-switches.md).
687687

688688
**Note:** When distributing your packaged app, you have to also ship the
689689
`locales` folder.
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Supported Chrome Command Line Switches
1+
# Supported Command Line Switches
22

33
> Command line switches supported by Electron.
44
@@ -181,6 +181,17 @@ logging level for all code in the source files under a `foo/bar` directory.
181181

182182
This switch only works when `--enable-logging` is also passed.
183183

184+
## --enable-api-filtering-logging
185+
186+
Enables caller stack logging for the following APIs (filtering events):
187+
- `desktopCapturer.getSources()` / `desktop-capturer-get-sources`
188+
- `remote.require()` / `remote-require`
189+
- `remote.getGlobal()` / `remote-get-builtin`
190+
- `remote.getBuiltin()` / `remote-get-global`
191+
- `remote.getCurrentWindow()` / `remote-get-current-window`
192+
- `remote.getCurrentWebContents()` / `remote-get-current-web-contents`
193+
- `remote.getGuestWebContents()` / `remote-get-guest-web-contents`
194+
184195
## --no-sandbox
185196

186197
Disables Chromium sandbox, which is now enabled by default.

docs/api/command-line.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ app.commandLine.hasSwitch('disable-gpu')
1212
```
1313

1414
For more information on what kinds of flags and switches you can use, check
15-
out the [Chrome Command Line Switches](./chrome-command-line-switches.md)
15+
out the [Command Line Switches](./command-line-switches.md)
1616
document.
1717

1818
### Instance Methods

docs/api/net-log.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ app.on('ready', async () => {
1515
})
1616
```
1717

18-
See [`--log-net-log`](chrome-command-line-switches.md#--log-net-logpath) to log network events throughout the app's lifecycle.
18+
See [`--log-net-log`](command-line-switches.md#--log-net-logpath) to log network events throughout the app's lifecycle.
1919

2020
**Note:** All methods unless specified can only be used after the `ready` event
2121
of the `app` module gets emitted.

filenames.auto.gni

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ auto_filenames = {
99
"docs/api/browser-view.md",
1010
"docs/api/browser-window-proxy.md",
1111
"docs/api/browser-window.md",
12-
"docs/api/chrome-command-line-switches.md",
1312
"docs/api/client-request.md",
1413
"docs/api/clipboard.md",
14+
"docs/api/command-line-switches.md",
1515
"docs/api/command-line.md",
1616
"docs/api/content-tracing.md",
1717
"docs/api/cookies.md",

lib/browser/remote/server.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,12 @@ const emitCustomEvent = function (contents: electron.WebContents, eventName: str
383383
return event
384384
}
385385

386+
const logStack = function (contents: electron.WebContents, code: string, stack: string | undefined) {
387+
if (stack) {
388+
console.warn(`WebContents (${contents.id}): ${code}`, stack)
389+
}
390+
}
391+
386392
handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) {
387393
const objectId = [passedContextId, id]
388394
if (!rendererFunctions.has(objectId)) {
@@ -392,7 +398,8 @@ handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, con
392398
removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId))
393399
})
394400

395-
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName) {
401+
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) {
402+
logStack(event.sender, `remote.require('${moduleName}')`, stack)
396403
const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName)
397404

398405
if (customEvent.returnValue === undefined) {
@@ -406,7 +413,8 @@ handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, modu
406413
return valueToMeta(event.sender, contextId, customEvent.returnValue)
407414
})
408415

409-
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) {
416+
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName, stack) {
417+
logStack(event.sender, `remote.getBuiltin('${moduleName}')`, stack)
410418
const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName)
411419

412420
if (customEvent.returnValue === undefined) {
@@ -420,7 +428,8 @@ handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId,
420428
return valueToMeta(event.sender, contextId, customEvent.returnValue)
421429
})
422430

423-
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) {
431+
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName, stack) {
432+
logStack(event.sender, `remote.getGlobal('${globalName}')`, stack)
424433
const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName)
425434

426435
if (customEvent.returnValue === undefined) {
@@ -434,7 +443,8 @@ handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globa
434443
return valueToMeta(event.sender, contextId, customEvent.returnValue)
435444
})
436445

437-
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
446+
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId, stack) {
447+
logStack(event.sender, 'remote.getCurrentWindow()', stack)
438448
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window')
439449

440450
if (customEvent.returnValue === undefined) {
@@ -448,7 +458,8 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextI
448458
return valueToMeta(event.sender, contextId, customEvent.returnValue)
449459
})
450460

451-
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
461+
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId, stack) {
462+
logStack(event.sender, 'remote.getCurrentWebContents()', stack)
452463
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents')
453464

454465
if (customEvent.returnValue === undefined) {
@@ -549,14 +560,15 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
549560
return null
550561
})
551562

552-
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
563+
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId, stack) {
564+
logStack(event.sender, 'remote.getGuestWebContents()', stack)
553565
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender)
554566

555567
const customEvent = emitCustomEvent(event.sender, 'remote-get-guest-web-contents', guest)
556568

557569
if (customEvent.returnValue === undefined) {
558570
if (customEvent.defaultPrevented) {
559-
throw new Error(`Blocked remote.getGuestForWebContents()`)
571+
throw new Error(`Blocked remote.getGuestWebContents()`)
560572
} else {
561573
customEvent.returnValue = guest
562574
}

lib/browser/rpc-server.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ const emitCustomEvent = function (contents, eventName, ...args) {
2323
return event
2424
}
2525

26+
const logStack = function (contents, code, stack) {
27+
if (stack) {
28+
console.warn(`WebContents (${contents.id}): ${code}`, stack)
29+
}
30+
}
31+
2632
// Implements window.close()
2733
ipcMainInternal.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
2834
const window = event.sender.getOwnerBrowserWindow()
@@ -63,15 +69,16 @@ ipcMainUtils.handleSync('ELECTRON_BROWSER_CLIPBOARD', function (event, method, .
6369
if (features.isDesktopCapturerEnabled()) {
6470
const desktopCapturer = require('@electron/internal/browser/desktop-capturer')
6571

66-
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, ...args) {
72+
ipcMainInternal.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, options, stack) {
73+
logStack(event.sender, 'desktopCapturer.getSources()', stack)
6774
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources')
6875

6976
if (customEvent.defaultPrevented) {
7077
console.error('Blocked desktopCapturer.getSources()')
7178
return []
7279
}
7380

74-
return desktopCapturer.getSources(event, ...args)
81+
return desktopCapturer.getSources(event, options)
7582
})
7683
}
7784

lib/renderer/api/desktop-capturer.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import { nativeImage } from 'electron'
22
import { ipcRendererInternal } from '@electron/internal/renderer/ipc-renderer-internal'
33

4+
const { hasSwitch } = process.electronBinding('command_line')
5+
46
// |options.types| can't be empty and must be an array
57
function isValid (options: Electron.SourcesOptions) {
68
const types = options ? options.types : undefined
79
return Array.isArray(types)
810
}
911

12+
const enableStacks = hasSwitch('enable-api-filtering-logging')
13+
14+
function getCurrentStack () {
15+
const target = {}
16+
if (enableStacks) {
17+
Error.captureStackTrace(target, getCurrentStack)
18+
}
19+
return (target as any).stack
20+
}
21+
1022
export async function getSources (options: Electron.SourcesOptions) {
1123
if (!isValid(options)) throw new Error('Invalid options')
1224

@@ -21,7 +33,7 @@ export async function getSources (options: Electron.SourcesOptions) {
2133
captureScreen,
2234
thumbnailSize,
2335
fetchWindowIcons
24-
} as ElectronInternal.GetSourcesOptions)
36+
} as ElectronInternal.GetSourcesOptions, getCurrentStack())
2537

2638
return sources.map(source => ({
2739
id: source.id,

lib/renderer/api/remote.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const v8Util = process.electronBinding('v8_util')
4+
const { hasSwitch } = process.electronBinding('command_line')
45

56
const { CallbacksRegistry } = require('@electron/internal/renderer/remote/callbacks-registry')
67
const bufferUtils = require('@electron/internal/common/remote/buffer-utils')
@@ -281,6 +282,16 @@ function handleMessage (channel, handler) {
281282
})
282283
}
283284

285+
const enableStacks = hasSwitch('enable-api-filtering-logging')
286+
287+
function getCurrentStack () {
288+
const target = {}
289+
if (enableStacks) {
290+
Error.captureStackTrace(target, getCurrentStack)
291+
}
292+
return target.stack
293+
}
294+
284295
// Browser calls a callback in renderer.
285296
handleMessage('ELECTRON_RENDERER_CALLBACK', (id, args) => {
286297
callbacksRegistry.apply(id, metaToValue(args))
@@ -293,34 +304,34 @@ handleMessage('ELECTRON_RENDERER_RELEASE_CALLBACK', (id) => {
293304

294305
exports.require = (module) => {
295306
const command = 'ELECTRON_BROWSER_REQUIRE'
296-
const meta = ipcRendererInternal.sendSync(command, contextId, module)
307+
const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack())
297308
return metaToValue(meta)
298309
}
299310

300311
// Alias to remote.require('electron').xxx.
301312
exports.getBuiltin = (module) => {
302313
const command = 'ELECTRON_BROWSER_GET_BUILTIN'
303-
const meta = ipcRendererInternal.sendSync(command, contextId, module)
314+
const meta = ipcRendererInternal.sendSync(command, contextId, module, getCurrentStack())
304315
return metaToValue(meta)
305316
}
306317

307318
exports.getCurrentWindow = () => {
308319
const command = 'ELECTRON_BROWSER_CURRENT_WINDOW'
309-
const meta = ipcRendererInternal.sendSync(command, contextId)
320+
const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack())
310321
return metaToValue(meta)
311322
}
312323

313324
// Get current WebContents object.
314325
exports.getCurrentWebContents = () => {
315326
const command = 'ELECTRON_BROWSER_CURRENT_WEB_CONTENTS'
316-
const meta = ipcRendererInternal.sendSync(command, contextId)
327+
const meta = ipcRendererInternal.sendSync(command, contextId, getCurrentStack())
317328
return metaToValue(meta)
318329
}
319330

320331
// Get a global object in browser.
321332
exports.getGlobal = (name) => {
322333
const command = 'ELECTRON_BROWSER_GLOBAL'
323-
const meta = ipcRendererInternal.sendSync(command, contextId, name)
334+
const meta = ipcRendererInternal.sendSync(command, contextId, name, getCurrentStack())
324335
return metaToValue(meta)
325336
}
326337

@@ -339,7 +350,7 @@ exports.createFunctionWithReturnValue = (returnValue) => {
339350
// Get the guest WebContents from guestInstanceId.
340351
exports.getGuestWebContents = (guestInstanceId) => {
341352
const command = 'ELECTRON_BROWSER_GUEST_WEB_CONTENTS'
342-
const meta = ipcRendererInternal.sendSync(command, contextId, guestInstanceId)
353+
const meta = ipcRendererInternal.sendSync(command, contextId, guestInstanceId, getCurrentStack())
343354
return metaToValue(meta)
344355
}
345356

0 commit comments

Comments
 (0)