Compare commits

...

24 Commits

Author SHA1 Message Date
trop[bot]
7acdcae772 fix: menu bar visibility when exiting full screen (#38683)
* fix:visibility of menu bar when exiting full screen

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* format code

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* format code

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* Modify comments

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* add menu bar visibility test

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* format code

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* change code

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

* platform related in test

Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: wugaosheng <wugaosheng@kylinos.cn>
2023-06-09 13:59:15 +02:00
Erick Zhao
b8d9a80dbb docs: use local img for contents.adjustSelection api (#38697)
docs: use local img for contents.adjustSelection api (#38655)

* docs: use local img for contents.adjustSelection api

* fixup
2023-06-08 13:42:43 -07:00
trop[bot]
67f9b1f88d fix: dangling pointer warning when updating menus (#38690)
fix: dangling raw_ptr warning when updating menus

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-08 16:40:52 -04:00
trop[bot]
fc241977fe fix: reparenting UAF crash on macOS (#38679)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-08 13:29:17 -04:00
trop[bot]
01faeef36a build: use upstream's presubmit cpplint filters. (#38686)
* build: use upstream's presubmit cpplint filters.

Our list of filters has fallen out-of-sync with upstream tests and so
our checks don't match those in the Google + Chromium guides.

The checks in this PR come from depot_tools and are copied manually
since the depot_tools code is python but our linter is js. 🤷
Perhaps we should rewrite in python?

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* fixup! build: use upstream's presubmit cpplint filters.

chore: make linter happy with my linter changes

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-08 13:28:37 -04:00
trop[bot]
9d3c1d993a chore: clean up ElectronPermissionManager (#38669)
chore: clean up ElectronPermissionManager

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-08 12:19:01 +02:00
trop[bot]
95f8fa8732 fix: bad error passing webContents.print(null) (#38641)
fix: bad error passing webContents.print(null)

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-07 23:53:02 -07:00
trop[bot]
53b398d7c3 refactor: remove unused OffScreenRenderWidgetHostView fields (#38649)
* refactor: remove unused field OffScreenRenderWidgetHostView::last_time_

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: remove unused field OffScreenRenderWidgetHostView::last_scroll_offset_

Unused since 1a9e253

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: remove unused field OffScreenRenderWidgetHostView::paint_callback_running_

assigned to, but unread, since 81bf158

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-07 23:51:07 -07:00
trop[bot]
4d4191ed1a build: improve error output in release.js (#38660)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
2023-06-07 23:47:55 -07:00
trop[bot]
1facc6d304 fix: validate response in protocol.handle() (#38635)
fix: validate response in protocol.handle()

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-07 10:44:54 -05:00
trop[bot]
455458ace8 fix: sync api::Screen wrapper method sigs to upstream (#38633)
refactor: sync api::Screen getter sigs to upstream

ui::Display GetAllDisplays(), GetPrimaryDisplay(), GetDisplayMatching(),
and GetDisplayNearestPoint() methods are all const, so make our wrappers
const too.

ui::Display GetAllDisplays() returns a const reference, so make our
wrapper return a const reference too. This avoids creating a new
std::vector<display::Display> each time it's called.

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-07 12:09:23 +02:00
trop[bot]
0e2fd41733 refactor: remove redundant resizing strategy equality check (#38617)
* refactor: remove redundant resizing strategy equality check

Upstream already checks this.

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: remove devtools_settings.h from iwc header

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: remove unused <vectro> from iwc header

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-07 09:28:42 +02:00
trop[bot]
dbc4e2d8d7 docs: removed outdated Appveyor doc (#38626)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
2023-06-06 19:57:16 -07:00
trop[bot]
3f3435bea7 fix: account for BrowserView bounds in setting autofill popup bounds (#38610)
fix: account for BrowserView bounds in setting autofill popup bounds

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-06 21:50:52 +02:00
trop[bot]
d142292485 refactor: use process_util.h helpers (#38606)
refactor: use process_util.h helpers

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Milan Burda <milan.burda@gmail.com>
2023-06-06 15:48:33 -04:00
trop[bot]
42a5affba8 fix: file selection when disallowed on macOS (#38590)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-05 16:21:41 +02:00
trop[bot]
f89974fe4b refactor: use direct aggregation in NativeWindowViews (#38576)
* refactor: in NativeWindowViews, aggregate root_view_ directly

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: in NativeWindowViews, aggregate keyboard_event_handler_ directly

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: make NativeWindowClientView::window_ a raw_ref

Xref: https://chromium.googlesource.com/chromium/src/+/main/styleguide/c++/c++.md\#non_owning-pointers-in-class-fields

Prefer const raw_ref<T> whenever the held pointer will never be null

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* chore: make lint happy

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-05 15:42:25 +02:00
trop[bot]
c62a32df72 chore: type check JS in docs (#38584)
* build(deps): update @electron/lint-roller

* chore: type check JS in docs

* docs: add @ts-check and @ts-expect-error to code blocks

* chore: fix type check errors in docs

* chore: add ts-type to blocks

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
2023-06-05 14:08:01 +02:00
trop[bot]
75981c1e9a docs: clarify which electron modules are exposed in sandboxed renderers (#38579)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Milan Burda <milan.burda@gmail.com>
2023-06-05 11:56:16 +02:00
trop[bot]
d9935276dd docs: fix SerialPort typing (#38583)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
2023-06-05 11:33:46 +02:00
trop[bot]
b9453eb6d8 chore: remove unused electron::api::View code (#38569)
chore: remove unused electron::api::View methods

Remove code that was added in 2c8dc9e never got used.

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2023-06-03 16:18:27 -05:00
trop[bot]
e12b30b6ce perf: avoid unnecessary base value clone (#38564) 2023-06-02 17:04:19 -05:00
trop[bot]
9bbd85c2ba build: fix doc-only early exit on Appveyor (#38552)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
2023-06-01 15:44:52 -04:00
trop[bot]
665823596c refactor: remove unused switches (#38530) 2023-06-01 14:17:29 +02:00
94 changed files with 712 additions and 583 deletions

View File

@@ -68,9 +68,11 @@ for:
node script/yarn.js install --frozen-lockfile
node script/doc-only-change.js --prNumber=$env:APPVEYOR_PULL_REQUEST_NUMBER
if ($LASTEXITCODE -eq 0) {
Write-warning "Skipping build for doc only change"; Exit-AppveyorBuild
Write-warning "Skipping build for doc only change"
Exit-AppveyorBuild
} else {
$global:LASTEXITCODE = 0
}
$global:LASTEXITCODE = 0
- cd ..
- ps: Write-Host "Building $env:GN_CONFIG build"
- git config --global core.longpaths true
@@ -222,9 +224,11 @@ for:
node script/yarn.js install --frozen-lockfile
node script/doc-only-change.js --prNumber=$env:APPVEYOR_PULL_REQUEST_NUMBER
if ($LASTEXITCODE -eq 0) {
Write-warning "Skipping build for doc only change"; Exit-AppveyorBuild
Write-warning "Skipping build for doc only change"
Exit-AppveyorBuild
} else {
$global:LASTEXITCODE = 0
}
$global:LASTEXITCODE = 0
- cd ..
- mkdir out\Default
- cd ..

View File

@@ -66,9 +66,11 @@ for:
node script/yarn.js install --frozen-lockfile
node script/doc-only-change.js --prNumber=$env:APPVEYOR_PULL_REQUEST_NUMBER
if ($LASTEXITCODE -eq 0) {
Write-warning "Skipping build for doc only change"; Exit-AppveyorBuild
Write-warning "Skipping build for doc only change"
Exit-AppveyorBuild
} else {
$global:LASTEXITCODE = 0
}
$global:LASTEXITCODE = 0
- cd ..
- ps: Write-Host "Building $env:GN_CONFIG build"
- git config --global core.longpaths true
@@ -218,9 +220,11 @@ for:
node script/yarn.js install --frozen-lockfile
node script/doc-only-change.js --prNumber=$env:APPVEYOR_PULL_REQUEST_NUMBER
if ($LASTEXITCODE -eq 0) {
Write-warning "Skipping build for doc only change"; Exit-AppveyorBuild
Write-warning "Skipping build for doc only change"
Exit-AppveyorBuild
} else {
$global:LASTEXITCODE = 0
}
$global:LASTEXITCODE = 0
- cd ..
- mkdir out\Default
- cd ..

View File

@@ -971,7 +971,7 @@ app.setJumpList([
title: 'Tool A',
program: process.execPath,
args: '--run-tool-a',
icon: process.execPath,
iconPath: process.execPath,
iconIndex: 0,
description: 'Runs Tool A'
},
@@ -980,7 +980,7 @@ app.setJumpList([
title: 'Tool B',
program: process.execPath,
args: '--run-tool-b',
icon: process.execPath,
iconPath: process.execPath,
iconIndex: 0,
description: 'Runs Tool B'
}
@@ -1418,8 +1418,8 @@ const fs = require('fs')
let filepath
let bookmark
dialog.showOpenDialog(null, { securityScopedBookmarks: true }, (filepaths, bookmarks) => {
filepath = filepaths[0]
dialog.showOpenDialog(null, { securityScopedBookmarks: true }).then(({ filePaths, bookmarks }) => {
filepath = filePaths[0]
bookmark = bookmarks[0]
fs.readFileSync(filepath)
})

View File

@@ -104,6 +104,7 @@ window, you have to set both `parent` and `modal` options:
```javascript
const { BrowserWindow } = require('electron')
const top = new BrowserWindow()
const child = new BrowserWindow({ parent: top, modal: true, show: false })
child.loadURL('https://github.com')
child.once('ready-to-show', () => {
@@ -597,7 +598,7 @@ On Linux the setter is a no-op, although the getter returns `true`.
A `boolean` property that determines whether the window is excluded from the applications Windows menu. `false` by default.
```js
```js @ts-expect-error=[11]
const win = new BrowserWindow({ height: 600, width: 600 })
const template = [
@@ -1200,6 +1201,9 @@ Node's [`url.format`](https://nodejs.org/api/url.html#url_url_format_urlobject)
method:
```javascript
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
const url = require('url').format({
protocol: 'file',
slashes: true,
@@ -1213,6 +1217,9 @@ You can load a URL using a `POST` request with URL-encoded data by doing
the following:
```javascript
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
win.loadURL('http://localhost:8000/post', {
postData: [{
type: 'rawData',

View File

@@ -104,7 +104,7 @@ The `callback` function is expected to be called back with user credentials:
* `username` string
* `password` string
```javascript
```javascript @ts-type={request:Electron.ClientRequest}
request.on('login', (authInfo, callback) => {
callback('username', 'password')
})
@@ -113,7 +113,7 @@ request.on('login', (authInfo, callback) => {
Providing empty credentials will cancel the request and report an authentication
error on the response object:
```javascript
```javascript @ts-type={request:Electron.ClientRequest}
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
response.on('error', (error) => {

View File

@@ -148,10 +148,7 @@ clipboard.
```js
const { clipboard } = require('electron')
clipboard.writeBookmark({
text: 'https://electronjs.org',
bookmark: 'Electron Homepage'
})
clipboard.writeBookmark('Electron Homepage', 'https://electronjs.org')
```
### `clipboard.readFindText()` _macOS_

View File

@@ -18,7 +18,7 @@ contextBridge.exposeInMainWorld(
)
```
```javascript
```javascript @ts-nocheck
// Renderer (Main World)
window.electron.doThing()
@@ -104,7 +104,7 @@ contextBridge.exposeInIsolatedWorld(
)
```
```javascript
```javascript @ts-nocheck
// Renderer (In isolated world id1004)
window.electron.doThing()

View File

@@ -10,7 +10,9 @@ title is `Electron`:
```javascript
// In the main process.
const { desktopCapturer } = require('electron')
const { BrowserWindow, desktopCapturer } = require('electron')
const mainWindow = new BrowserWindow()
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
for (const source of sources) {
@@ -22,7 +24,7 @@ desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources =
})
```
```javascript
```javascript @ts-nocheck
// In the preload script.
const { ipcRenderer } = require('electron')

View File

@@ -72,7 +72,7 @@ and a directory selector, so if you set `properties` to
`['openFile', 'openDirectory']` on these platforms, a directory selector will be
shown.
```js
```js @ts-type={mainWindow:Electron.BrowserWindow}
dialog.showOpenDialogSync(mainWindow, {
properties: ['openFile', 'openDirectory']
})
@@ -139,7 +139,7 @@ and a directory selector, so if you set `properties` to
`['openFile', 'openDirectory']` on these platforms, a directory selector will be
shown.
```js
```js @ts-type={mainWindow:Electron.BrowserWindow}
dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'openDirectory']
}).then(result => {

View File

@@ -89,7 +89,7 @@ tuples. So, the even-numbered offsets are key values, and the odd-numbered
offsets are the associated values. Header names are not lowercased, and
duplicates are not merged.
```javascript
```javascript @ts-type={response:Electron.IncomingMessage}
// Prints something like:
//
// [ 'user-agent',
@@ -100,5 +100,5 @@ duplicates are not merged.
// '127.0.0.1:8000',
// 'ACCEPT',
// '*/*' ]
console.log(request.rawHeaders)
console.log(response.rawHeaders)
```

View File

@@ -83,14 +83,14 @@ If `listener` returns a Promise, the eventual result of the promise will be
returned as a reply to the remote caller. Otherwise, the return value of the
listener will be used as the value of the reply.
```js title='Main Process'
```js title='Main Process' @ts-type={somePromise:(...args:unknown[])=>Promise<unknown>}
ipcMain.handle('my-invokable-ipc', async (event, ...args) => {
const result = await somePromise(...args)
return result
})
```
```js title='Renderer Process'
```js title='Renderer Process' @ts-type={arg1:unknown} @ts-type={arg2:unknown}
async () => {
const result = await ipcRenderer.invoke('my-invokable-ipc', arg1, arg2)
// ...

View File

@@ -101,7 +101,7 @@ The main process should listen for `channel` with
For example:
```javascript
```javascript @ts-type={someArgument:unknown} @ts-type={doSomeWork:(arg:unknown)=>Promise<unknown>}
// Renderer process
ipcRenderer.invoke('some-name', someArgument).then((result) => {
// ...

View File

@@ -147,7 +147,7 @@ can have a submenu.
An example of creating the application menu with the simple template API:
```javascript
```javascript @ts-expect-error=[107]
const { app, Menu } = require('electron')
const isMac = process.platform === 'darwin'
@@ -267,7 +267,7 @@ menu on behalf of the renderer.
Below is an example of showing a menu when the user right clicks the page:
```js
```js @ts-expect-error=[21]
// renderer
window.addEventListener('contextmenu', (e) => {
e.preventDefault()
@@ -289,7 +289,7 @@ ipcMain.on('show-context-menu', (event) => {
{ label: 'Menu Item 2', type: 'checkbox', checked: true }
]
const menu = Menu.buildFromTemplate(template)
menu.popup(BrowserWindow.fromWebContents(event.sender))
menu.popup({ window: BrowserWindow.fromWebContents(event.sender) })
})
```

View File

@@ -17,7 +17,8 @@ Example:
```js
// Main process
const { MessageChannelMain } = require('electron')
const { BrowserWindow, MessageChannelMain } = require('electron')
const w = new BrowserWindow()
const { port1, port2 } = new MessageChannelMain()
w.webContents.postMessage('port', null, [port2])
port1.postMessage({ some: 'message' })
@@ -26,9 +27,9 @@ port1.postMessage({ some: 'message' })
const { ipcRenderer } = require('electron')
ipcRenderer.on('port', (e) => {
// e.ports is a list of ports sent along with this message
e.ports[0].on('message', (messageEvent) => {
e.ports[0].onmessage = (messageEvent) => {
console.log(messageEvent.data)
})
}
})
```

View File

@@ -5,7 +5,7 @@
Process: [Main](../glossary.md#main-process)
```javascript
const { netLog } = require('electron')
const { app, netLog } = require('electron')
app.whenReady().then(async () => {
await netLog.startLogging('/path/to/net-log')

View File

@@ -32,7 +32,7 @@ To have your custom protocol work in combination with a custom session, you need
to register it to that session explicitly.
```javascript
const { session, app, protocol } = require('electron')
const { app, BrowserWindow, net, protocol, session } = require('electron')
const path = require('path')
const url = require('url')
@@ -41,11 +41,11 @@ app.whenReady().then(() => {
const ses = session.fromPartition(partition)
ses.protocol.handle('atom', (request) => {
const path = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.join(__dirname, path)))
const filePath = request.url.slice('atom://'.length)
return net.fetch(url.pathToFileURL(path.join(__dirname, filePath)).toString())
})
mainWindow = new BrowserWindow({ webPreferences: { partition } })
const mainWindow = new BrowserWindow({ webPreferences: { partition } })
})
```
@@ -121,9 +121,9 @@ Either a `Response` or a `Promise<Response>` can be returned.
Example:
```js
import { app, protocol } from 'electron'
import { join } from 'path'
import { pathToFileURL } from 'url'
const { app, net, protocol } = require('electron')
const { join } = require('path')
const { pathToFileURL } = require('url')
protocol.registerSchemesAsPrivileged([
{
@@ -131,7 +131,7 @@ protocol.registerSchemesAsPrivileged([
privileges: {
standard: true,
secure: true,
supportsFetchAPI: true
supportFetchAPI: true
}
}
])
@@ -147,7 +147,7 @@ app.whenReady().then(() => {
}
// NB, this does not check for paths that escape the bundle, e.g.
// app://bundle/../../secret_file.txt
return net.fetch(pathToFileURL(join(__dirname, pathname)))
return net.fetch(pathToFileURL(join(__dirname, pathname)).toString())
} else if (host === 'api') {
return net.fetch('https://api.my-server.com/' + pathname, {
method: req.method,

View File

@@ -98,7 +98,7 @@ Emitted when Electron is about to download `item` in `webContents`.
Calling `event.preventDefault()` will cancel the download and `item` will not be
available from next tick of the process.
```javascript
```javascript @ts-expect-error=[4]
const { session } = require('electron')
session.defaultSession.on('will-download', (event, item, webContents) => {
event.preventDefault()
@@ -214,7 +214,7 @@ cancel the request. Additionally, permissioning on `navigator.hid` can
be further managed by using [`ses.setPermissionCheckHandler(handler)`](#sessetpermissioncheckhandlerhandler)
and [`ses.setDevicePermissionHandler(handler)`](#sessetdevicepermissionhandlerhandler).
```javascript
```javascript @ts-type={fetchGrantedDevices:()=>(Array<Electron.DevicePermissionHandlerHandlerDetails['device']>)}
const { app, BrowserWindow } = require('electron')
let win = null
@@ -253,7 +253,7 @@ app.whenReady().then(() => {
win.webContents.session.on('select-hid-device', (event, details, callback) => {
event.preventDefault()
const selectedDevice = details.deviceList.find((device) => {
return device.vendorId === '9025' && device.productId === '67'
return device.vendorId === 9025 && device.productId === 67
})
callback(selectedDevice?.deviceId)
})
@@ -320,7 +320,7 @@ cancel the request. Additionally, permissioning on `navigator.serial` can
be managed by using [ses.setPermissionCheckHandler(handler)](#sessetpermissioncheckhandlerhandler)
with the `serial` permission.
```javascript
```javascript @ts-type={fetchGrantedDevices:()=>(Array<Electron.DevicePermissionHandlerHandlerDetails['device']>)}
const { app, BrowserWindow } = require('electron')
let win = null
@@ -463,7 +463,7 @@ cancel the request. Additionally, permissioning on `navigator.usb` can
be further managed by using [`ses.setPermissionCheckHandler(handler)`](#sessetpermissioncheckhandlerhandler)
and [`ses.setDevicePermissionHandler(handler)`](#sessetdevicepermissionhandlerhandler).
```javascript
```javascript @ts-type={fetchGrantedDevices:()=>(Array<Electron.DevicePermissionHandlerHandlerDetails['device']>)} @ts-type={updateGrantedDevices:(devices:Array<Electron.DevicePermissionHandlerHandlerDetails['device']>)=>void}
const { app, BrowserWindow } = require('electron')
let win = null
@@ -502,7 +502,7 @@ app.whenReady().then(() => {
win.webContents.session.on('select-usb-device', (event, details, callback) => {
event.preventDefault()
const selectedDevice = details.deviceList.find((device) => {
return device.vendorId === '9025' && device.productId === '67'
return device.vendorId === 9025 && device.productId === 67
})
if (selectedDevice) {
// Optionally, add this to the persisted devices (updateGrantedDevices needs to be implemented by developer to persist permissions)
@@ -755,15 +755,17 @@ Sets download saving directory. By default, the download directory will be the
Emulates network with the given configuration for the `session`.
```javascript
const win = new BrowserWindow()
// To emulate a GPRS connection with 50kbps throughput and 500 ms latency.
window.webContents.session.enableNetworkEmulation({
win.webContents.session.enableNetworkEmulation({
latency: 500,
downloadThroughput: 6400,
uploadThroughput: 6400
})
// To emulate a network outage.
window.webContents.session.enableNetworkEmulation({ offline: true })
win.webContents.session.enableNetworkEmulation({ offline: true })
```
#### `ses.preconnect(options)`
@@ -1036,7 +1038,7 @@ Additionally, the default behavior of Electron is to store granted device permis
If longer term storage is needed, a developer can store granted device
permissions (eg when handling the `select-hid-device` event) and then read from that storage with `setDevicePermissionHandler`.
```javascript
```javascript @ts-type={fetchGrantedDevices:()=>(Array<Electron.DevicePermissionHandlerHandlerDetails['device']>)}
const { app, BrowserWindow } = require('electron')
let win = null
@@ -1084,9 +1086,9 @@ app.whenReady().then(() => {
win.webContents.session.on('select-hid-device', (event, details, callback) => {
event.preventDefault()
const selectedDevice = details.deviceList.find((device) => {
return device.vendorId === '9025' && device.productId === '67'
return device.vendorId === 9025 && device.productId === 67
})
callback(selectedPort?.deviceId)
callback(selectedDevice?.deviceId)
})
})
```
@@ -1174,32 +1176,32 @@ macOS does not require a handler because macOS handles the pairing
automatically. To clear the handler, call `setBluetoothPairingHandler(null)`.
```javascript
const { app, BrowserWindow, ipcMain, session } = require('electron')
let bluetoothPinCallback = null
const { app, BrowserWindow, session } = require('electron')
const path = require('path')
function createWindow () {
let bluetoothPinCallback = null
const mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.webContents.session.setBluetoothPairingHandler((details, callback) => {
bluetoothPinCallback = callback
// Send a IPC message to the renderer to prompt the user to confirm the pairing.
// Note that this will require logic in the renderer to handle this message and
// display a prompt to the user.
mainWindow.webContents.send('bluetooth-pairing-request', details)
})
// Listen for an IPC message from the renderer to get the response for the Bluetooth pairing.
mainWindow.webContents.ipc.on('bluetooth-pairing-response', (event, response) => {
bluetoothPinCallback(response)
})
}
// Listen for an IPC message from the renderer to get the response for the Bluetooth pairing.
ipcMain.on('bluetooth-pairing-response', (event, response) => {
bluetoothPinCallback(response)
})
mainWindow.webContents.session.setBluetoothPairingHandler((details, callback) => {
bluetoothPinCallback = callback
// Send a IPC message to the renderer to prompt the user to confirm the pairing.
// Note that this will require logic in the renderer to handle this message and
// display a prompt to the user.
mainWindow.webContents.send('bluetooth-pairing-request', details)
})
app.whenReady().then(() => {
createWindow()
})

View File

@@ -2,9 +2,9 @@
* `portId` string - Unique identifier for the port.
* `portName` string - Name of the port.
* `displayName` string - A string suitable for display to the user for describing this device.
* `vendorId` string - Optional USB vendor ID.
* `productId` string - Optional USB product ID.
* `serialNumber` string - The USB device serial number.
* `usbDriverName` string (optional) - Represents a single serial port on macOS can be enumerated by multiple drivers.
* `deviceInstanceId` string (optional) - A stable identifier on Windows that can be used for device permissions.
* `displayName` string (optional) - A string suitable for display to the user for describing this device.
* `vendorId` string (optional) - The USB vendor ID.
* `productId` string (optional) - The USB product ID.
* `serialNumber` string (optional) - The USB device serial number.
* `usbDriverName` string (optional) _macOS_ - Represents a single serial port on macOS can be enumerated by multiple drivers.
* `deviceInstanceId` string (optional) _Windows_ - A stable identifier on Windows that can be used for device permissions.

View File

@@ -87,12 +87,12 @@ const { TouchBarLabel, TouchBarButton, TouchBarSpacer } = TouchBar
let spinning = false
// Reel labels
const reel1 = new TouchBarLabel()
const reel2 = new TouchBarLabel()
const reel3 = new TouchBarLabel()
const reel1 = new TouchBarLabel({ label: '' })
const reel2 = new TouchBarLabel({ label: '' })
const reel3 = new TouchBarLabel({ label: '' })
// Spin result label
const result = new TouchBarLabel()
const result = new TouchBarLabel({ label: '' })
// Spin button
const spin = new TouchBarButton({

View File

@@ -98,7 +98,7 @@ async function lookupTargetId (browserWindow) {
await wc.debugger.attach('1.3')
const { targetInfo } = await wc.debugger.sendCommand('Target.getTargetInfo')
const { targetId } = targetInfo
const targetWebContents = await webContents.fromDevToolsTargetId(targetId)
const targetWebContents = await wc.fromDevToolsTargetId(targetId)
}
```
@@ -1020,9 +1020,9 @@ e.g. the `http://` or `file://`. If the load should bypass http cache then
use the `pragma` header to achieve it.
```javascript
const { webContents } = require('electron')
const win = new BrowserWindow()
const options = { extraHeaders: 'pragma: no-cache\n' }
webContents.loadURL('https://github.com', options)
win.webContents.loadURL('https://github.com', options)
```
#### `contents.loadFile(filePath[, options])`
@@ -1052,6 +1052,7 @@ an app structure like this:
Would require code like this
```js
const win = new BrowserWindow()
win.loadFile('src/index.html')
```
@@ -1188,7 +1189,9 @@ when this process is unstable or unusable, for instance in order to recover
from the `unresponsive` event.
```js
contents.on('unresponsive', async () => {
const win = new BrowserWindow()
win.webContents.on('unresponsive', async () => {
const { response } = await dialog.showMessageBox({
message: 'App X has become unresponsive',
title: 'Do you want to try forcefully reloading the app?',
@@ -1196,8 +1199,8 @@ contents.on('unresponsive', async () => {
cancelId: 1
})
if (response === 0) {
contents.forcefullyCrashRenderer()
contents.reload()
win.webContents.forcefullyCrashRenderer()
win.webContents.reload()
}
})
```
@@ -1224,8 +1227,9 @@ Injects CSS into the current web page and returns a unique key for the inserted
stylesheet.
```js
contents.on('did-finish-load', () => {
contents.insertCSS('html, body { background-color: #f00; }')
const win = new BrowserWindow()
win.webContents.on('did-finish-load', () => {
win.webContents.insertCSS('html, body { background-color: #f00; }')
})
```
@@ -1239,9 +1243,11 @@ Removes the inserted CSS from the current web page. The stylesheet is identified
by its key, which is returned from `contents.insertCSS(css)`.
```js
contents.on('did-finish-load', async () => {
const key = await contents.insertCSS('html, body { background-color: #f00; }')
contents.removeInsertedCSS(key)
const win = new BrowserWindow()
win.webContents.on('did-finish-load', async () => {
const key = await win.webContents.insertCSS('html, body { background-color: #f00; }')
win.webContents.removeInsertedCSS(key)
})
```
@@ -1262,7 +1268,9 @@ this limitation.
Code execution will be suspended until web page stop loading.
```js
contents.executeJavaScript('fetch("https://jsonplaceholder.typicode.com/users/1").then(resp => resp.json())', true)
const win = new BrowserWindow()
win.webContents.executeJavaScript('fetch("https://jsonplaceholder.typicode.com/users/1").then(resp => resp.json())', true)
.then((result) => {
console.log(result) // Will be the JSON object from the fetch call
})
@@ -1373,7 +1381,8 @@ Sets the maximum and minimum pinch-to-zoom level.
> **NOTE**: Visual zoom is disabled by default in Electron. To re-enable it, call:
>
> ```js
> contents.setVisualZoomLevelLimits(1, 3)
> const win = new BrowserWindow()
> win.webContents.setVisualZoomLevelLimits(1, 3)
> ```
#### `contents.undo()`
@@ -1457,11 +1466,11 @@ For a call of `win.webContents.adjustSelection({ start: 1, end: 5 })`
Before:
<img width="487" alt="Image Before Text Selection Adjustment" src="https://user-images.githubusercontent.com/2036040/231761306-cd4e7b15-c2ed-46cf-8e80-10811f6de83e.png">
<img width="487" alt="Image Before Text Selection Adjustment" src="../images/web-contents-text-selection-before.png"/>
After:
<img width="487" alt="Image After Text Selection Adjustment" src="https://user-images.githubusercontent.com/2036040/231761169-887eb8ef-06fb-46e4-9efa-898bcb0d6a2b.png">
<img width="487" alt="Image After Text Selection Adjustment" src="../images/web-contents-text-selection-after.png"/>
#### `contents.replace(text)`
@@ -1508,12 +1517,12 @@ can be obtained by subscribing to [`found-in-page`](web-contents.md#event-found-
Stops any `findInPage` request for the `webContents` with the provided `action`.
```javascript
const { webContents } = require('electron')
webContents.on('found-in-page', (event, result) => {
if (result.finalUpdate) webContents.stopFindInPage('clearSelection')
const win = new BrowserWindow()
win.webContents.on('found-in-page', (event, result) => {
if (result.finalUpdate) win.webContents.stopFindInPage('clearSelection')
})
const requestId = webContents.findInPage('api')
const requestId = win.webContents.findInPage('api')
console.log(requestId)
```
@@ -1593,6 +1602,7 @@ Use `page-break-before: always;` CSS style to force to print to a new page.
Example usage:
```js
const win = new BrowserWindow()
const options = {
silent: true,
deviceName: 'My-Printer',
@@ -1889,8 +1899,9 @@ For example:
```js
// Main process
const win = new BrowserWindow()
const { port1, port2 } = new MessageChannelMain()
webContents.postMessage('port', { message: 'hello' }, [port1])
win.webContents.postMessage('port', { message: 'hello' }, [port1])
// Renderer process
ipcRenderer.on('port', (e, msg) => {

View File

@@ -128,8 +128,9 @@ For example:
```js
// Main process
const win = new BrowserWindow()
const { port1, port2 } = new MessageChannelMain()
webContents.mainFrame.postMessage('port', { message: 'hello' }, [port1])
win.webContents.mainFrame.postMessage('port', { message: 'hello' }, [port1])
// Renderer process
ipcRenderer.on('port', (e, msg) => {

View File

@@ -96,13 +96,12 @@ with an array of misspelt words when complete.
An example of using [node-spellchecker][spellchecker] as provider:
```javascript
```javascript @ts-expect-error=[2,6]
const { webFrame } = require('electron')
const spellChecker = require('spellchecker')
webFrame.setSpellCheckProvider('en-US', {
spellCheck (words, callback) {
setTimeout(() => {
const spellchecker = require('spellchecker')
const misspelled = words.filter(x => spellchecker.isMisspelled(x))
callback(misspelled)
}, 0)

View File

@@ -255,7 +255,7 @@ The `webview` tag has the following methods:
**Example**
```javascript
```javascript @ts-expect-error=[3]
const webview = document.querySelector('webview')
webview.addEventListener('dom-ready', () => {
webview.openDevTools()
@@ -799,7 +799,7 @@ Fired when the guest window logs a console message.
The following example code forwards all log messages to the embedder's console
without regard for log level or other properties.
```javascript
```javascript @ts-expect-error=[3]
const webview = document.querySelector('webview')
webview.addEventListener('console-message', (e) => {
console.log('Guest page logged a message:', e.message)
@@ -820,7 +820,7 @@ Returns:
Fired when a result is available for
[`webview.findInPage`](#webviewfindinpagetext-options) request.
```javascript
```javascript @ts-expect-error=[3,6]
const webview = document.querySelector('webview')
webview.addEventListener('found-in-page', (e) => {
webview.stopFindInPage('keepSelection')
@@ -945,7 +945,7 @@ Fired when the guest page attempts to close itself.
The following example code navigates the `webview` to `about:blank` when the
guest attempts to close itself.
```javascript
```javascript @ts-expect-error=[3]
const webview = document.querySelector('webview')
webview.addEventListener('close', () => {
webview.src = 'about:blank'
@@ -965,7 +965,7 @@ Fired when the guest page has sent an asynchronous message to embedder page.
With `sendToHost` method and `ipc-message` event you can communicate
between guest page and embedder page:
```javascript
```javascript @ts-expect-error=[4,7]
// In embedder page.
const webview = document.querySelector('webview')
webview.addEventListener('ipc-message', (event) => {

View File

@@ -1,62 +0,0 @@
# Updating an Appveyor Azure Image
Electron CI on Windows uses AppVeyor, which in turn uses Azure VM images to run. Occasionally, these VM images need to be updated due to changes in Chromium requirements. In order to update you will need [PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell?view=powershell-7.3&viewFallbackFrom=powershell-6) and the [Azure PowerShell module](https://learn.microsoft.com/en-us/powershell/azure/install-az-ps?view=azps-9.5.0&viewFallbackFrom=azps-1.8.0).
Occasionally we need to update these images owing to changes in Chromium or other miscellaneous build requirement changes.
Example Use Case:
* We need `VS15.9` and we have `VS15.7` installed; this would require us to update an Azure image.
1. Identify the image you wish to modify.
* In [appveyor.yml](https://github.com/electron/electron/blob/main/appveyor.yml), the image is identified by the property _image_.
* The names used correspond to the _"images"_ defined for a build cloud, eg the [libcc-20 cloud](https://windows-ci.electronjs.org/build-clouds/8).
* Find the image you wish to modify in the build cloud and make note of the **VHD Blob Path** for that image, which is the value for that corresponding key.
* You will need this URI path to copy into a new image.
* You will also need the storage account name which is labeled in AppVeyor as the **Disk Storage Account Name**
2. Get the Azure storage account key
* Log into Azure using credentials stored in LastPass (under Azure Enterprise) and then find the storage account corresponding to the name found in AppVeyor.
* Example, for `appveyorlibccbuilds` **Disk Storage Account Name** you'd look for `appveyorlibccbuilds` in the list of storage accounts @ Home < Storage Accounts
* Click into it and look for `Access Keys`, and then you can use any of the keys present in the list.
3. Get the full virtual machine image URI from Azure
* Navigate to Home < Storage Accounts < `$ACCT_NAME` < Blobs < Images
* In the following list, look for the VHD path name you got from Appveyor and then click on it.
* Copy the whole URL from the top of the subsequent window.
4. Copy the image using the [Copy Master Image PowerShell script](https://github.com/appveyor/ci/blob/master/scripts/enterprise/copy-master-image-azure.ps1).
* It is essential to copy the VM because if you spin up a VM against an image that image cannot at the same time be used by AppVeyor.
* Use the storage account name, key, and URI obtained from Azure to run this script.
* See Step 3 for URI & when prompted, press enter to use same storage account as destination.
* Use default destination container name `(images)`
* Also, when naming the copy, use a name that indicates what the new image will contain (if that has changed) and date stamp.
* Ex. `libcc-20core-vs2017-15.9-2019-04-15.vhd`
* Go into Azure and get the URI for the newly created image as described in a previous step
5. Spin up a new VM using the [Create Master VM from VHD PowerShell](https://github.com/appveyor/ci/blob/master/scripts/enterprise/create_master_vm_from_vhd.ps1).
* From PowerShell, execute `ps1` file with `./create_master_vm_from_vhd.ps1`
* You will need the credential information available in the AppVeyor build cloud definition.
* This includes:
* Client ID
* Client Secret
* Tenant ID
* Subscription ID
* Resource Group
* Virtual Network
* You will also need to specify
* Master VM name - just a unique name to identify the temporary VM
* Master VM size - use `Standard_F32s_v2`
* Master VHD URI - use URI obtained @ end of previous step
* Location use `East US`
6. Log back into Azure and find the VM you just created in Home < Virtual Machines < `$YOUR_NEW_VM`
* You can download a RDP (Remote Desktop) file to access the VM.
7. Using Microsoft Remote Desktop, click `Connect` to connect to the VM.
* Credentials for logging into the VM are found in LastPass under the `AppVeyor Enterprise master VM` credentials.
8. Modify the VM as required.
9. Shut down the VM and then delete it in Azure.
10. Add the new image to the Appveyor Cloud settings or modify an existing image to point to the new VHD.

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -55,7 +55,7 @@ fs.readdirSync('/path/to/example.asar')
Use a module from the archive:
```javascript
```javascript @ts-nocheck
require('./path/to/example.asar/dir/module.js')
```

View File

@@ -40,7 +40,7 @@ Valid `algorithm` values are currently `SHA256` only. The `hash` is a hash of t
ASAR integrity checking is currently disabled by default and can be enabled by toggling a fuse. See [Electron Fuses](fuses.md) for more information on what Electron Fuses are and how they work. When enabling this fuse you typically also want to enable the `onlyLoadAppFromAsar` fuse otherwise the validity checking can be bypassed via the Electron app code search path.
```js
```js @ts-nocheck
require('@electron/fuses').flipFuses(
// E.g. /a/b/Foo.app
pathToPackagedApp,

View File

@@ -90,7 +90,7 @@ Usage of `selenium-webdriver` with Electron is the same as with
normal websites, except that you have to manually specify how to connect
ChromeDriver and where to find the binary of your Electron app:
```js title='test.js'
```js title='test.js' @ts-expect-error=[1]
const webdriver = require('selenium-webdriver')
const driver = new webdriver.Builder()
// The "9515" is the port opened by ChromeDriver.
@@ -155,7 +155,7 @@ Playwright launches your app in development mode through the `_electron.launch`
To point this API to your Electron app, you can pass the path to your main process
entry point (here, it is `main.js`).
```js {5}
```js {5} @ts-nocheck
const { _electron: electron } = require('playwright')
const { test } = require('@playwright/test')
@@ -169,7 +169,7 @@ test('launch app', async () => {
After that, you will access to an instance of Playwright's `ElectronApp` class. This
is a powerful class that has access to main process modules for example:
```js {6-11}
```js {6-11} @ts-nocheck
const { _electron: electron } = require('playwright')
const { test } = require('@playwright/test')
@@ -189,7 +189,7 @@ test('get isPackaged', async () => {
It can also create individual [Page][playwright-page] objects from Electron BrowserWindow instances.
For example, to grab the first BrowserWindow and save a screenshot:
```js {6-7}
```js {6-7} @ts-nocheck
const { _electron: electron } = require('playwright')
const { test } = require('@playwright/test')
@@ -205,7 +205,7 @@ test('save screenshot', async () => {
Putting all this together using the PlayWright Test runner, let's create a `example.spec.js`
test file with a single test and assertion:
```js title='example.spec.js'
```js title='example.spec.js' @ts-nocheck
const { _electron: electron } = require('playwright')
const { test, expect } = require('@playwright/test')
@@ -259,7 +259,7 @@ expose custom methods to your test suite.
To create a custom driver, we'll use Node.js' [`child_process`](https://nodejs.org/api/child_process.html) API.
The test suite will spawn the Electron process, then establish a simple messaging protocol:
```js title='testDriver.js'
```js title='testDriver.js' @ts-nocheck
const childProcess = require('child_process')
const electronPath = require('electron')
@@ -296,7 +296,7 @@ For convenience, you may want to wrap `appProcess` in a driver object that provi
high-level functions. Here is an example of how you can do this. Let's start by creating
a `TestDriver` class:
```js title='testDriver.js'
```js title='testDriver.js' @ts-nocheck
class TestDriver {
constructor ({ path, args, env }) {
this.rpcCalls = []
@@ -378,7 +378,7 @@ framework of your choosing. The following example uses
[`ava`](https://www.npmjs.com/package/ava), but other popular choices like Jest
or Mocha would work as well:
```js title='test.js'
```js title='test.js' @ts-nocheck
const test = require('ava')
const electronPath = require('electron')
const { TestDriver } = require('./testDriver')

View File

@@ -67,7 +67,7 @@ are likely using [`electron-packager`][], which includes [`@electron/osx-sign`][
If you're using Packager's API, you can pass [in configuration that both signs
and notarizes your application](https://electron.github.io/electron-packager/main/interfaces/electronpackager.options.html).
```js
```js @ts-nocheck
const packager = require('electron-packager')
packager({
@@ -116,7 +116,7 @@ Electron app. This is the tool used under the hood by Electron Forge's
`electron-winstaller` directly, use the `certificateFile` and `certificatePassword` configuration
options when creating your installer.
```js {10-11}
```js {10-11} @ts-nocheck
const electronInstaller = require('electron-winstaller')
// NB: Use this syntax within an async function, Node does not have support for
// top-level await as of Node 12.
@@ -146,7 +146,7 @@ If you're not using Electron Forge and want to use `electron-wix-msi` directly,
`certificateFile` and `certificatePassword` configuration options
or pass in parameters directly to [SignTool.exe][] with the `signWithParams` option.
```js {12-13}
```js {12-13} @ts-nocheck
import { MSICreator } from 'electron-wix-msi'
// Step 1: Instantiate the MSICreator

View File

@@ -16,7 +16,7 @@ Context isolation has been enabled by default since Electron 12, and it is a rec
Exposing APIs from your preload script to a loaded website in the renderer process is a common use-case. With context isolation disabled, your preload script would share a common global `window` object with the renderer. You could then attach arbitrary properties to a preload script:
```javascript title='preload.js'
```javascript title='preload.js' @ts-nocheck
// preload with contextIsolation disabled
window.myAPI = {
doAThing: () => {}
@@ -25,7 +25,7 @@ window.myAPI = {
The `doAThing()` function could then be used directly in the renderer process:
```javascript title='renderer.js'
```javascript title='renderer.js' @ts-nocheck
// use the exposed API in the renderer
window.myAPI.doAThing()
```
@@ -43,7 +43,7 @@ contextBridge.exposeInMainWorld('myAPI', {
})
```
```javascript title='renderer.js'
```javascript title='renderer.js' @ts-nocheck
// use the exposed API in the renderer
window.myAPI.doAThing()
```
@@ -98,7 +98,7 @@ declare global {
Doing so will ensure that the TypeScript compiler will know about the `electronAPI` property on your global `window` object when writing scripts in your renderer process:
```typescript title='renderer.ts'
```typescript title='renderer.ts' @ts-nocheck
window.electronAPI.loadPreferences()
```

View File

@@ -116,7 +116,7 @@ Now the renderer process can communicate with the main process securely and perf
The `renderer.js` file is responsible for controlling the `<button>` functionality.
```js title='renderer.js'
```js title='renderer.js' @ts-expect-error=[2,7]
document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
const isDarkMode = await window.darkMode.toggle()
document.getElementById('theme-source').innerHTML = isDarkMode ? 'Dark' : 'Light'

View File

@@ -67,7 +67,7 @@ The loadBrowserProcessSpecificV8Snapshot fuse changes which V8 snapshot file is
We've made a handy module, [`@electron/fuses`](https://npmjs.com/package/@electron/fuses), to make flipping these fuses easy. Check out the README of that module for more details on usage and potential error cases.
```js
```js @ts-nocheck
require('@electron/fuses').flipFuses(
// Path to electron
require('electron'),

View File

@@ -66,7 +66,7 @@ You can use environment variables to override the base URL, the path at which to
look for Electron binaries, and the binary filename. The URL used by `@electron/get`
is composed as follows:
```javascript
```javascript @ts-nocheck
url = ELECTRON_MIRROR + ELECTRON_CUSTOM_DIR + '/' + ELECTRON_CUSTOM_FILENAME
```

View File

@@ -138,7 +138,7 @@ To make these elements interactive, we'll be adding a few lines of code in the i
`renderer.js` file that leverages the `window.electronAPI` functionality exposed from the preload
script:
```javascript title='renderer.js (Renderer Process)'
```javascript title='renderer.js (Renderer Process)' @ts-expect-error=[4,5]
const setButton = document.getElementById('btn')
const titleInput = document.getElementById('title')
setButton.addEventListener('click', () => {
@@ -182,13 +182,13 @@ provided to the renderer process. Please refer to
:::
```javascript {6-13,25} title='main.js (Main Process)'
const { BrowserWindow, dialog, ipcMain } = require('electron')
const { app, BrowserWindow, dialog, ipcMain } = require('electron')
const path = require('path')
// ...
async function handleFileOpen () {
const { canceled, filePaths } = await dialog.showOpenDialog()
const { canceled, filePaths } = await dialog.showOpenDialog({})
if (!canceled) {
return filePaths[0]
}
@@ -203,7 +203,7 @@ function createWindow () {
mainWindow.loadFile('index.html')
}
app.whenReady(() => {
app.whenReady().then(() => {
ipcMain.handle('dialog:openFile', handleFileOpen)
createWindow()
})
@@ -263,7 +263,7 @@ The UI consists of a single `#btn` button element that will be used to trigger o
a `#filePath` element that will be used to display the path of the selected file. Making these
pieces work will take a few lines of code in the renderer process script:
```javascript title='renderer.js (Renderer Process)'
```javascript title='renderer.js (Renderer Process)' @ts-expect-error=[5]
const btn = document.getElementById('btn')
const filePathElement = document.getElementById('filePath')
@@ -412,7 +412,7 @@ function createWindow () {
For the purposes of the tutorial, it's important to note that the `click` handler
sends a message (either `1` or `-1`) to the renderer process through the `update-counter` channel.
```javascript
```javascript @ts-nocheck
click: () => mainWindow.webContents.send('update-counter', -1)
```
@@ -486,7 +486,7 @@ To tie it all together, we'll create an interface in the loaded HTML file that c
Finally, to make the values update in the HTML document, we'll add a few lines of DOM manipulation
so that the value of the `#counter` element is updated whenever we fire an `update-counter` event.
```javascript title='renderer.js (Renderer Process)'
```javascript title='renderer.js (Renderer Process)' @ts-nocheck
const counter = document.getElementById('counter')
window.electronAPI.onUpdateCounter((_event, value) => {
@@ -509,7 +509,7 @@ We can demonstrate this with slight modifications to the code from the previous
renderer process, use the `event` parameter to send a reply back to the main process through the
`counter-value` channel.
```javascript title='renderer.js (Renderer Process)'
```javascript title='renderer.js (Renderer Process)' @ts-nocheck
const counter = document.getElementById('counter')
window.electronAPI.onUpdateCounter((event, value) => {

View File

@@ -56,7 +56,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the
following lines:
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/global'
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/global' @ts-type={createWindow:()=>void}
const { app, globalShortcut } = require('electron')
app.whenReady().then(() => {
@@ -131,7 +131,7 @@ If you don't want to do manual shortcut parsing, there are libraries that do
advanced key detection, such as [mousetrap][]. Below are examples of usage of the
`mousetrap` running in the Renderer process:
```js
```js @ts-nocheck
Mousetrap.bind('4', () => { console.log('4') })
Mousetrap.bind('?', () => { console.log('show shortcuts!') })
Mousetrap.bind('esc', () => { console.log('escape') }, 'keyup')

View File

@@ -45,6 +45,8 @@ if (process.defaultApp) {
We will now define the function in charge of creating our browser window and load our application's `index.html` file.
```javascript
let mainWindow
const createWindow = () => {
// Create the browser window.
mainWindow = new BrowserWindow({
@@ -65,7 +67,7 @@ This code will be different in Windows compared to MacOS and Linux. This is due
#### Windows code:
```javascript
```javascript @ts-type={mainWindow:Electron.BrowserWindow} @ts-type={createWindow:()=>void}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
@@ -91,7 +93,7 @@ if (!gotTheLock) {
#### MacOS and Linux code:
```javascript
```javascript @ts-type={createWindow:()=>void}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
@@ -166,7 +168,7 @@ If you're using Electron Packager's API, adding support for protocol handlers is
Electron Forge is handled, except
`protocols` is part of the Packager options passed to the `packager` function.
```javascript
```javascript @ts-nocheck
const packager = require('electron-packager')
packager({

View File

@@ -126,7 +126,7 @@ app.whenReady().then(async () => {
Then, in your preload scripts you receive the port through IPC and set up the
listeners.
```js title='preloadMain.js and preloadSecondary.js (Preload scripts)'
```js title='preloadMain.js and preloadSecondary.js (Preload scripts)' @ts-nocheck
const { ipcRenderer } = require('electron')
ipcRenderer.on('port', e => {
@@ -148,7 +148,7 @@ That means window.electronMessagePort is globally available and you can call
`postMessage` on it from anywhere in your app to send a message to the other
renderer.
```js title='renderer.js (Renderer Process)'
```js title='renderer.js (Renderer Process)' @ts-nocheck
// elsewhere in your code to send a message to the other renderers message handler
window.electronMessagePort.postmessage('ping')
```
@@ -181,7 +181,7 @@ app.whenReady().then(async () => {
// We can't use ipcMain.handle() here, because the reply needs to transfer a
// MessagePort.
// Listen for message sent from the top-level frame
mainWindow.webContents.mainFrame.on('request-worker-channel', (event) => {
mainWindow.webContents.mainFrame.ipc.on('request-worker-channel', (event) => {
// Create a new channel ...
const { port1, port2 } = new MessageChannelMain()
// ... send one end to the worker ...
@@ -245,7 +245,7 @@ Electron's built-in IPC methods only support two modes: fire-and-forget
can implement a "response stream", where a single request responds with a
stream of data.
```js title='renderer.js (Renderer Process)'
```js title='renderer.js (Renderer Process)' @ts-expect-error=[18]
const makeStreamingRequest = (element, callback) => {
// MessageChannels are lightweight--it's cheap to create a new one for each
// request.

View File

@@ -42,7 +42,7 @@ safe.
The only way to load a native module safely for now, is to make sure the app
loads no native modules after the Web Workers get started.
```javascript
```javascript @ts-expect-error=[1]
process.dlopen = () => {
throw new Error('Load native module is not safe')
}

View File

@@ -22,6 +22,7 @@ In `preload.js` use the [`contextBridge`][] to inject a method `window.electron.
```js
const { contextBridge, ipcRenderer } = require('electron')
const path = require('path')
contextBridge.exposeInMainWorld('electron', {
startDrag: (fileName) => {
@@ -43,7 +44,7 @@ Add a draggable element to `index.html`, and reference your renderer script:
In `renderer.js` set up the renderer process to handle drag events by calling the method you added via the [`contextBridge`][] above.
```javascript
```javascript @ts-expect-error=[3]
document.getElementById('drag').ondragstart = (event) => {
event.preventDefault()
window.electron.startDrag('drag-and-drop.md')

View File

@@ -172,7 +172,7 @@ in the fictitious `.foo` format. In order to do that, it relies on the
equally fictitious `foo-parser` module. In traditional Node.js development,
you might write code that eagerly loads dependencies:
```js title='parser.js'
```js title='parser.js' @ts-expect-error=[2]
const fs = require('fs')
const fooParser = require('foo-parser')
@@ -195,7 +195,7 @@ In the above example, we're doing a lot of work that's being executed as soon
as the file is loaded. Do we need to get parsed files right away? Could we
do this work a little later, when `getParsedFiles()` is actually called?
```js title='parser.js'
```js title='parser.js' @ts-expect-error=[20]
// "fs" is likely already being loaded, so the `require()` call is cheap
const fs = require('fs')
@@ -204,7 +204,7 @@ class Parser {
// Touch the disk as soon as `getFiles` is called, not sooner.
// Also, ensure that we're not blocking other operations by using
// the asynchronous version.
this.files = this.files || await fs.readdir('.')
this.files = this.files || await fs.promises.readdir('.')
return this.files
}

View File

@@ -175,13 +175,13 @@ Although preload scripts share a `window` global with the renderer they're attac
you cannot directly attach any variables from the preload script to `window` because of
the [`contextIsolation`][context-isolation] default.
```js title='preload.js'
```js title='preload.js' @ts-nocheck
window.myAPI = {
desktop: true
}
```
```js title='renderer.js'
```js title='renderer.js' @ts-nocheck
console.log(window.myAPI)
// => undefined
```
@@ -200,7 +200,7 @@ contextBridge.exposeInMainWorld('myAPI', {
})
```
```js title='renderer.js'
```js title='renderer.js' @ts-nocheck
console.log(window.myAPI)
// => { desktop: true }
```

View File

@@ -182,7 +182,7 @@ In Electron, browser windows can only be created after the `app` module's
[`app.whenReady()`][app-when-ready] API. Call `createWindow()` after `whenReady()`
resolves its Promise.
```js
```js @ts-type={createWindow:()=>void}
app.whenReady().then(() => {
createWindow()
})
@@ -239,7 +239,7 @@ from within your existing `whenReady()` callback.
[activate]: ../api/app.md#event-activate-macos
```js
```js @ts-type={createWindow:()=>void}
app.whenReady().then(() => {
createWindow()
@@ -290,6 +290,7 @@ To attach this script to your renderer process, pass in the path to your preload
to the `webPreferences.preload` option in your existing `BrowserWindow` constructor.
```js
const { app, BrowserWindow } = require('electron')
// include the Node.js 'path' module at the top of your file
const path = require('path')

View File

@@ -46,7 +46,7 @@ scripts attached to sandboxed renderers will still have a polyfilled subset of N
APIs available. A `require` function similar to Node's `require` module is exposed,
but can only import a subset of Electron and Node's built-in modules:
* `electron` (only renderer process modules)
* `electron` (following renderer process modules: `contextBridge`, `crashReporter`, `ipcRenderer`, `nativeImage`, `webFrame`)
* [`events`](https://nodejs.org/api/events.html)
* [`timers`](https://nodejs.org/api/timers.html)
* [`url`](https://nodejs.org/api/url.html)

View File

@@ -141,7 +141,7 @@ like `HTTP`. Similarly, we recommend the use of `WSS` over `WS`, `FTPS` over
#### How?
```js title='main.js (Main Process)'
```js title='main.js (Main Process)' @ts-type={browserWindow:Electron.BrowserWindow}
// Bad
browserWindow.loadURL('http://example.com')
@@ -278,7 +278,7 @@ security-conscious developers might want to assume the very opposite.
```js title='main.js (Main Process)'
const { session } = require('electron')
const URL = require('url').URL
const { URL } = require('url')
session
.fromPartition('some-partition')
@@ -608,7 +608,8 @@ sometimes be fooled - a `startsWith('https://example.com')` test would let
`https://example.com.attacker.com` through.
```js title='main.js (Main Process)'
const URL = require('url').URL
const { URL } = require('url')
const { app } = require('electron')
app.on('web-contents-created', (event, contents) => {
contents.on('will-navigate', (event, navigationUrl) => {
@@ -647,8 +648,8 @@ receive, amongst other parameters, the `url` the window was requested to open
and the options used to create it. We recommend that you register a handler to
monitor the creation of windows, and deny any unexpected window creation.
```js title='main.js (Main Process)'
const { shell } = require('electron')
```js title='main.js (Main Process)' @ts-type={isSafeForExternalOpen:(url:string)=>boolean}
const { app, shell } = require('electron')
app.on('web-contents-created', (event, contents) => {
contents.setWindowOpenHandler(({ url }) => {
@@ -683,7 +684,7 @@ leveraged to execute arbitrary commands.
#### How?
```js title='main.js (Main Process)'
```js title='main.js (Main Process)' @ts-type={USER_CONTROLLED_DATA_HERE:string}
// Bad
const { shell } = require('electron')
shell.openExternal(USER_CONTROLLED_DATA_HERE)
@@ -739,7 +740,7 @@ You should be validating the `sender` of **all** IPC messages by default.
#### How?
```js title='main.js (Main Process)'
```js title='main.js (Main Process)' @ts-type={getSecrets:()=>unknown}
// Bad
ipcMain.handle('get-secrets', () => {
return getSecrets()

View File

@@ -72,7 +72,7 @@ npx electron-installer-snap --src=out/myappname-linux-x64
If you have an existing build pipeline, you can use `electron-installer-snap`
programmatically. For more information, see the [Snapcraft API docs][snapcraft-syntax].
```js
```js @ts-nocheck
const snap = require('electron-installer-snap')
snap(options)

View File

@@ -20,12 +20,12 @@ On macOS as we use the native APIs there is no way to set the language that the
For Windows and Linux there are a few Electron APIs you should use to set the languages for the spellchecker.
```js
```js @ts-type={myWindow:Electron.BrowserWindow}
// Sets the spellchecker to check English US and French
myWindow.session.setSpellCheckerLanguages(['en-US', 'fr'])
myWindow.webContents.session.setSpellCheckerLanguages(['en-US', 'fr'])
// An array of all available language codes
const possibleLanguages = myWindow.session.availableSpellCheckerLanguages
const possibleLanguages = myWindow.webContents.session.availableSpellCheckerLanguages
```
By default the spellchecker will enable the language matching the current OS locale.
@@ -35,7 +35,7 @@ By default the spellchecker will enable the language matching the current OS loc
All the required information to generate a context menu is provided in the [`context-menu`](../api/web-contents.md#event-context-menu) event on each `webContents` instance. A small example
of how to make a context menu with this information is provided below.
```js
```js @ts-type={myWindow:Electron.BrowserWindow}
const { Menu, MenuItem } = require('electron')
myWindow.webContents.on('context-menu', (event, params) => {
@@ -45,7 +45,7 @@ myWindow.webContents.on('context-menu', (event, params) => {
for (const suggestion of params.dictionarySuggestions) {
menu.append(new MenuItem({
label: suggestion,
click: () => mainWindow.webContents.replaceMisspelling(suggestion)
click: () => myWindow.webContents.replaceMisspelling(suggestion)
}))
}
@@ -54,7 +54,7 @@ myWindow.webContents.on('context-menu', (event, params) => {
menu.append(
new MenuItem({
label: 'Add to dictionary',
click: () => mainWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
click: () => myWindow.webContents.session.addWordToSpellCheckerDictionary(params.misspelledWord)
})
)
}
@@ -67,8 +67,8 @@ myWindow.webContents.on('context-menu', (event, params) => {
Although the spellchecker itself does not send any typings, words or user input to Google services the hunspell dictionary files are downloaded from a Google CDN by default. If you want to avoid this you can provide an alternative URL to download the dictionaries from.
```js
myWindow.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
```js @ts-type={myWindow:Electron.BrowserWindow}
myWindow.webContents.session.setSpellCheckerDictionaryDownloadURL('https://example.com/dictionaries/')
```
Check out the docs for [`session.setSpellCheckerDictionaryDownloadURL`](../api/session.md#sessetspellcheckerdictionarydownloadurlurl) for more information on where to get the dictionary files from and how you need to host them.

View File

@@ -51,7 +51,7 @@ app.whenReady().then(() => {
Great! Now we can start attaching a context menu to our Tray, like so:
```js
```js @ts-expect-error=[8]
const contextMenu = Menu.buildFromTemplate([
{ label: 'Item1', type: 'radio' },
{ label: 'Item2', type: 'radio' },
@@ -68,7 +68,7 @@ To read more about constructing native menus, click
Finally, let's give our tray a tooltip and a title.
```js
```js @ts-type={tray:Electron.Tray}
tray.setToolTip('This is my application')
tray.setTitle('This is my title')
```

View File

@@ -256,7 +256,7 @@ const createWindow = () => {
### Calling your function when the app is ready
```js title='main.js (Lines 12-14)'
```js title='main.js (Lines 12-14)' @ts-type={createWindow:()=>void}
app.whenReady().then(() => {
createWindow()
})
@@ -336,7 +336,7 @@ Because windows cannot be created before the `ready` event, you should only list
`activate` events after your app is initialized. Do this by only listening for activate
events inside your existing `whenReady()` callback.
```js
```js @ts-type={createWindow:()=>void}
app.whenReady().then(() => {
createWindow()

View File

@@ -118,7 +118,7 @@ information in the window. This variable can be accessed via `window.versions` o
`versions`. Create a `renderer.js` script that uses the [`document.getElementById`][]
DOM API to replace the displayed text for the HTML element with `info` as its `id` property.
```js title="renderer.js"
```js title="renderer.js" @ts-nocheck
const information = document.getElementById('info')
information.innerText = `This app is using Chrome (v${versions.chrome()}), Node.js (v${versions.node()}), and Electron (v${versions.electron()})`
```
@@ -225,7 +225,7 @@ app.whenReady().then(() => {
Once you have the sender and receiver set up, you can now send messages from the renderer
to the main process through the `'ping'` channel you just defined.
```js title='renderer.js'
```js title='renderer.js' @ts-expect-error=[2]
const func = async () => {
const response = await window.versions.ping()
console.log(response) // prints out 'pong'

View File

@@ -188,7 +188,7 @@ npm install update-electron-app
Then, import the module and call it immediately in the main process.
```js title='main.js'
```js title='main.js' @ts-nocheck
require('update-electron-app')()
```

View File

@@ -32,7 +32,7 @@ npm install update-electron-app
Then, invoke the updater from your app's main process file:
```js title="main.js"
```js title="main.js" @ts-nocheck
require('update-electron-app')()
```
@@ -113,7 +113,7 @@ Now that you've configured the basic update mechanism for your application, you
need to ensure that the user will get notified when there's an update. This
can be achieved using the [autoUpdater API events](../api/auto-updater.md#events):
```javascript title="main.js"
```javascript title="main.js" @ts-expect-error=[11]
autoUpdater.on('update-downloaded', (event, releaseNotes, releaseName) => {
const dialogOpts = {
type: 'info',

View File

@@ -196,9 +196,9 @@ const win = new BrowserWindow({
}
})
ipcMain.on('set-ignore-mouse-events', (event, ...args) => {
ipcMain.on('set-ignore-mouse-events', (event, ignore, options) => {
const win = BrowserWindow.fromWebContents(event.sender)
win.setIgnoreMouseEvents(...args)
win.setIgnoreMouseEvents(ignore, options)
})
```

View File

@@ -125,7 +125,7 @@ Starting with a working application from the
following lines:
```javascript
const { BrowserWindow } = require('electron')
const { BrowserWindow, nativeImage } = require('electron')
const path = require('path')
const win = new BrowserWindow()
@@ -133,11 +133,11 @@ const win = new BrowserWindow()
win.setThumbarButtons([
{
tooltip: 'button1',
icon: path.join(__dirname, 'button1.png'),
icon: nativeImage.createFromPath(path.join(__dirname, 'button1.png')),
click () { console.log('button1 clicked') }
}, {
tooltip: 'button2',
icon: path.join(__dirname, 'button2.png'),
icon: nativeImage.createFromPath(path.join(__dirname, 'button2.png')),
flags: ['enabled', 'dismissonclick'],
click () { console.log('button2 clicked.') }
}
@@ -189,11 +189,11 @@ Starting with a working application from the
following lines:
```javascript
const { BrowserWindow } = require('electron')
const { BrowserWindow, nativeImage } = require('electron')
const win = new BrowserWindow()
win.setOverlayIcon('path/to/overlay.png', 'Description for overlay')
win.setOverlayIcon(nativeImage.createFromPath('path/to/overlay.png'), 'Description for overlay')
```
[msdn-icon-overlay]: https://learn.microsoft.com/en-us/windows/win32/shell/taskbar-extensions#icon-overlays

View File

@@ -60,6 +60,26 @@ function convertToRequestBody (uploadData: ProtocolRequest['uploadData']): Reque
}) as RequestInit['body'];
}
// TODO(codebytere): Use Object.hasOwn() once we update to ECMAScript 2022.
function validateResponse (res: Response) {
if (!res || typeof res !== 'object') return false;
if (res.type === 'error') return true;
const exists = (key: string) => Object.prototype.hasOwnProperty.call(res, key);
if (exists('status') && typeof res.status !== 'number') return false;
if (exists('statusText') && typeof res.statusText !== 'string') return false;
if (exists('headers') && typeof res.headers !== 'object') return false;
if (exists('body')) {
if (typeof res.body !== 'object') return false;
if (res.body !== null && !(res.body instanceof ReadableStream)) return false;
}
return true;
}
Protocol.prototype.handle = function (this: Electron.Protocol, scheme: string, handler: (req: Request) => Response | Promise<Response>) {
const register = isBuiltInScheme(scheme) ? this.interceptProtocol : this.registerProtocol;
const success = register.call(this, scheme, async (preq: ProtocolRequest, cb: any) => {
@@ -73,13 +93,14 @@ Protocol.prototype.handle = function (this: Electron.Protocol, scheme: string, h
duplex: body instanceof ReadableStream ? 'half' : undefined
} as any);
const res = await handler(req);
if (!res || typeof res !== 'object') {
if (!validateResponse(res)) {
return cb({ error: ERR_UNEXPECTED });
}
if (res.type === 'error') { cb({ error: ERR_FAILED }); } else {
} else if (res.type === 'error') {
cb({ error: ERR_FAILED });
} else {
cb({
data: res.body ? Readable.fromWeb(res.body as ReadableStream<ArrayBufferView>) : null,
headers: Object.fromEntries(res.headers),
headers: res.headers ? Object.fromEntries(res.headers) : {},
statusCode: res.status,
statusText: res.statusText,
mimeType: (res as any).__original_resp?._responseHead?.mimeType

View File

@@ -335,36 +335,34 @@ WebContents.prototype.printToPDF = async function (options) {
}
};
WebContents.prototype.print = function (options: ElectronInternal.WebContentsPrintOptions = {}, callback) {
// TODO(codebytere): deduplicate argument sanitization by moving rest of
// print param logic into new file shared between printToPDF and print
if (typeof options === 'object') {
// Optionally set size for PDF.
if (options.pageSize !== undefined) {
const pageSize = options.pageSize;
if (typeof pageSize === 'object') {
if (!pageSize.height || !pageSize.width) {
throw new Error('height and width properties are required for pageSize');
}
// Dimensions in Microns - 1 meter = 10^6 microns
const height = Math.ceil(pageSize.height);
const width = Math.ceil(pageSize.width);
if (!isValidCustomPageSize(width, height)) {
throw new Error('height and width properties must be minimum 352 microns.');
}
options.mediaSize = {
name: 'CUSTOM',
custom_display_name: 'Custom',
height_microns: height,
width_microns: width
};
} else if (PDFPageSizes[pageSize]) {
options.mediaSize = PDFPageSizes[pageSize];
} else {
throw new Error(`Unsupported pageSize: ${pageSize}`);
// TODO(codebytere): deduplicate argument sanitization by moving rest of
// print param logic into new file shared between printToPDF and print
WebContents.prototype.print = function (printOptions: ElectronInternal.WebContentsPrintOptions, callback) {
const options = printOptions ?? {};
if (options.pageSize) {
const pageSize = options.pageSize;
if (typeof pageSize === 'object') {
if (!pageSize.height || !pageSize.width) {
throw new Error('height and width properties are required for pageSize');
}
// Dimensions in Microns - 1 meter = 10^6 microns
const height = Math.ceil(pageSize.height);
const width = Math.ceil(pageSize.width);
if (!isValidCustomPageSize(width, height)) {
throw new Error('height and width properties must be minimum 352 microns.');
}
options.mediaSize = {
name: 'CUSTOM',
custom_display_name: 'Custom',
height_microns: height,
width_microns: width
};
} else if (PDFPageSizes[pageSize]) {
options.mediaSize = PDFPageSizes[pageSize];
} else {
throw new Error(`Unsupported pageSize: ${pageSize}`);
}
}

View File

@@ -9,7 +9,7 @@
"@electron/docs-parser": "^1.1.0",
"@electron/fiddle-core": "^1.0.4",
"@electron/github-app-auth": "^2.0.0",
"@electron/lint-roller": "^1.2.1",
"@electron/lint-roller": "^1.5.0",
"@electron/typescript-definitions": "^8.14.0",
"@octokit/rest": "^19.0.7",
"@primer/octicons": "^10.0.0",
@@ -30,6 +30,7 @@
"@types/stream-json": "^1.5.1",
"@types/temp": "^0.8.34",
"@types/uuid": "^3.4.6",
"@types/w3c-web-serial": "^1.0.3",
"@types/webpack": "^5.28.0",
"@types/webpack-env": "^1.17.0",
"@typescript-eslint/eslint-plugin": "^5.59.7",
@@ -87,10 +88,11 @@
"lint:objc": "node ./script/lint.js --objc",
"lint:py": "node ./script/lint.js --py",
"lint:gn": "node ./script/lint.js --gn",
"lint:docs": "remark docs -qf && npm run lint:js-in-markdown && npm run create-typescript-definitions && npm run lint:docs-fiddles && npm run lint:docs-relative-links && npm run lint:markdownlint",
"lint:docs": "remark docs -qf && npm run lint:js-in-markdown && npm run create-typescript-definitions && npm run lint:ts-check-js-in-markdown && npm run lint:docs-fiddles && npm run lint:docs-relative-links && npm run lint:markdownlint",
"lint:docs-fiddles": "standard \"docs/fiddles/**/*.js\"",
"lint:docs-relative-links": "electron-lint-markdown-links --root docs \"**/*.md\"",
"lint:markdownlint": "electron-markdownlint \"*.md\" \"docs/**/*.md\"",
"lint:ts-check-js-in-markdown": "electron-lint-markdown-ts-check --root docs \"**/*.md\" --ignore \"breaking-changes.md\"",
"lint:js-in-markdown": "electron-lint-markdown-standard --root docs \"**/*.md\"",
"create-api-json": "node script/create-api-json.js",
"create-typescript-definitions": "npm run create-api-json && electron-typescript-definitions --api=electron-api.json && node spec/ts-smoke/runner.js",

View File

@@ -27,6 +27,32 @@ const IGNORELIST = new Set([
const IS_WINDOWS = process.platform === 'win32';
const CPPLINT_FILTERS = [
// from presubmit_canned_checks.py OFF_BY_DEFAULT_LINT_FILTERS
'-build/include',
'-build/include_order',
'-build/namespaces',
'-readability/casting',
'-runtime/int',
'-whitespace/braces',
// from presubmit_canned_checks.py OFF_UNLESS_MANUALLY_ENABLED_LINT_FILTERS
'-build/c++11',
'-build/header_guard',
'-readability/todo',
'-runtime/references',
'-whitespace/braces',
'-whitespace/comma',
'-whitespace/end_of_line',
'-whitespace/forcolon',
'-whitespace/indent',
'-whitespace/line_length',
'-whitespace/newline',
'-whitespace/operators',
'-whitespace/parens',
'-whitespace/semicolon',
'-whitespace/tab'
];
function spawnAndCheckExitCode (cmd, args, opts) {
opts = { stdio: 'inherit', ...opts };
const { error, status, signal } = childProcess.spawnSync(cmd, args, opts);
@@ -75,7 +101,7 @@ const LINTERS = [{
const clangFormatFlags = opts.fix ? ['--fix'] : [];
for (const chunk of chunkFilenames(filenames)) {
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', ...clangFormatFlags, ...chunk]);
cpplint(chunk);
cpplint([`--filter=${CPPLINT_FILTERS.join(',')}`, ...chunk]);
}
}
}, {
@@ -85,13 +111,7 @@ const LINTERS = [{
run: (opts, filenames) => {
const clangFormatFlags = opts.fix ? ['--fix'] : [];
spawnAndCheckExitCode('python3', ['script/run-clang-format.py', '-r', ...clangFormatFlags, ...filenames]);
const filter = [
'-readability/braces',
'-readability/casting',
'-whitespace/braces',
'-whitespace/indent',
'-whitespace/parens'
];
const filter = [...CPPLINT_FILTERS, '-readability/braces'];
cpplint(['--extensions=mm,h', `--filter=${filter.join(',')}`, ...filenames]);
}
}, {

View File

@@ -77,7 +77,7 @@ async function validateReleaseAssets (release, validatingRelease) {
} else {
await verifyShasumsForRemoteFiles(downloadUrls)
.catch(err => {
console.log(`${fail} error verifyingShasums`, err);
console.error(`${fail} error verifyingShasums`, err);
});
}
const azRemoteFiles = azRemoteFilesForVersion(release.tag_name);
@@ -90,7 +90,7 @@ function check (condition, statement, exitIfFail = false) {
console.log(`${pass} ${statement}`);
} else {
failureCount++;
console.log(`${fail} ${statement}`);
console.error(`${fail} ${statement}`);
if (exitIfFail) process.exit(1);
}
}
@@ -212,7 +212,7 @@ function runScript (scriptName, scriptArgs, cwd) {
try {
return execSync(scriptCommand, scriptOptions);
} catch (err) {
console.log(`${fail} Error running ${scriptName}`, err);
console.error(`${fail} Error running ${scriptName}`, err);
process.exit(1);
}
}
@@ -266,7 +266,8 @@ async function createReleaseShasums (release) {
repo: targetRepo,
asset_id: existingAssets[0].id
}).catch(err => {
console.log(`${fail} Error deleting ${fileName} on GitHub:`, err);
console.error(`${fail} Error deleting ${fileName} on GitHub:`, err);
process.exit(1);
});
}
console.log(`Creating and uploading the release ${fileName}.`);
@@ -292,7 +293,7 @@ async function uploadShasumFile (filePath, fileName, releaseId) {
data: fs.createReadStream(filePath),
name: fileName
}).catch(err => {
console.log(`${fail} Error uploading ${filePath} to GitHub:`, err);
console.error(`${fail} Error uploading ${filePath} to GitHub:`, err);
process.exit(1);
});
}
@@ -301,13 +302,13 @@ function saveShaSumFile (checksums, fileName) {
return new Promise((resolve, reject) => {
temp.open(fileName, (err, info) => {
if (err) {
console.log(`${fail} Could not create ${fileName} file`);
console.error(`${fail} Could not create ${fileName} file`);
process.exit(1);
} else {
fs.writeFileSync(info.fd, checksums);
fs.close(info.fd, (err) => {
if (err) {
console.log(`${fail} Could close ${fileName} file`);
console.error(`${fail} Could close ${fileName} file`);
process.exit(1);
}
resolve(info.path);
@@ -336,7 +337,7 @@ async function publishRelease (release) {
draft: false,
make_latest: makeLatest ? 'true' : 'false'
}).catch(err => {
console.log(`${fail} Error publishing release:`, err);
console.error(`${fail} Error publishing release:`, err);
process.exit(1);
});
}
@@ -406,7 +407,7 @@ async function verifyDraftGitHubReleaseAssets (release) {
return { url: response.headers.location, file: asset.name };
})).catch(err => {
console.log(`${fail} Error downloading files from GitHub`, err);
console.error(`${fail} Error downloading files from GitHub`, err);
process.exit(1);
});

View File

@@ -22,6 +22,7 @@
#include "ppapi/buildflags/buildflags.h"
#include "shell/common/electron_paths.h"
#include "shell/common/options_switches.h"
#include "shell/common/process_util.h"
#include "third_party/widevine/cdm/buildflags.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
@@ -148,16 +149,13 @@ base::RefCountedMemory* ElectronContentClient::GetDataResourceBytes(
}
void ElectronContentClient::AddAdditionalSchemes(Schemes* schemes) {
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(::switches::kProcessType);
// Browser Process registration happens in
// `api::Protocol::RegisterSchemesAsPrivileged`
//
// Renderer Process registration happens in `RendererClientBase`
//
// We use this for registration to network utility process
if (process_type == ::switches::kUtilityProcess) {
if (IsUtilityProcess()) {
AppendDelimitedSwitchToVector(switches::kServiceWorkerSchemes,
&schemes->service_worker_schemes);
AppendDelimitedSwitchToVector(switches::kStandardSchemes,

View File

@@ -316,9 +316,7 @@ absl::optional<int> ElectronMainDelegate::BasicStartupComplete() {
void ElectronMainDelegate::PreSandboxStartup() {
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(::switches::kProcessType);
std::string process_type = GetProcessType();
base::FilePath user_data_dir =
command_line->GetSwitchValuePath(::switches::kUserDataDir);
@@ -335,9 +333,9 @@ void ElectronMainDelegate::PreSandboxStartup() {
// know the correct user-data directory. (And, further, accessing the
// application name on Linux can cause glib calls that end up spawning
// threads, which if done before the zygote is booted, causes a CHECK().)
logging::InitElectronLogging(*command_line,
/* is_preinit = */ process_type.empty() ||
process_type == ::switches::kZygoteProcess);
logging::InitElectronLogging(
*command_line,
/* is_preinit = */ IsBrowserProcess() || IsZygoteProcess());
#endif
#if !IS_MAS_BUILD()
@@ -356,7 +354,7 @@ void ElectronMainDelegate::PreSandboxStartup() {
// In the main process, we wait for JS to call crashReporter.start() before
// initializing crashpad. If we're in the renderer, we want to initialize it
// immediately at boot.
if (!process_type.empty()) {
if (!IsBrowserProcess()) {
ElectronCrashReporterClient::Create();
crash_reporter::InitializeCrashpad(false, process_type);
}
@@ -364,7 +362,7 @@ void ElectronMainDelegate::PreSandboxStartup() {
#if BUILDFLAG(IS_LINUX)
// Zygote needs to call InitCrashReporter() in RunZygote().
if (process_type != ::switches::kZygoteProcess && !process_type.empty()) {
if (!IsZygoteProcess() && !IsBrowserProcess()) {
ElectronCrashReporterClient::Create();
if (command_line->HasSwitch(
crash_reporter::switches::kCrashpadHandlerPid)) {
@@ -414,12 +412,8 @@ absl::optional<int> ElectronMainDelegate::PreBrowserMain() {
}
base::StringPiece ElectronMainDelegate::GetBrowserV8SnapshotFilename() {
const base::CommandLine* command_line =
base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(::switches::kProcessType);
bool load_browser_process_specific_v8_snapshot =
process_type.empty() &&
IsBrowserProcess() &&
electron::fuses::IsLoadBrowserProcessSpecificV8SnapshotEnabled();
if (load_browser_process_specific_v8_snapshot) {
return "browser_v8_context_snapshot.bin";

View File

@@ -706,8 +706,6 @@ void BaseWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
v8::Local<v8::Object> object;
if (value->IsObject() && value->ToObject(context).ToLocal(&object) &&
gin::ConvertFromV8(isolate, value, &menu) && !menu.IsEmpty()) {
menu_.Reset(isolate, menu.ToV8());
// We only want to update the menu if the menu has a non-zero item count,
// or we risk crashes.
if (menu->model()->GetItemCount() == 0) {
@@ -715,6 +713,8 @@ void BaseWindow::SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> value) {
} else {
window_->SetMenu(menu->model());
}
menu_.Reset(isolate, menu.ToV8());
} else if (value->IsNull()) {
RemoveMenu();
} else {

View File

@@ -28,6 +28,7 @@
#include "shell/common/gin_converters/time_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "shell/common/process_util.h"
#include "shell/common/thread_restrictions.h"
#if !IS_MAS_BUILD()
@@ -142,17 +143,13 @@ void Start(const std::string& submit_url,
ElectronCrashReporterClient::Get()->SetShouldRateLimit(rate_limit);
ElectronCrashReporterClient::Get()->SetShouldCompressUploads(compress);
ElectronCrashReporterClient::Get()->SetGlobalAnnotations(global_extra);
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
is_node_process
? "node"
: command_line->GetSwitchValueASCII(::switches::kProcessType);
std::string process_type = is_node_process ? "node" : GetProcessType();
#if BUILDFLAG(IS_LINUX)
for (const auto& pair : extra)
electron::crash_keys::SetCrashKey(pair.first, pair.second);
{
electron::ScopedAllowBlockingForElectron allow_blocking;
::crash_reporter::InitializeCrashpad(process_type.empty(), process_type);
::crash_reporter::InitializeCrashpad(IsBrowserProcess(), process_type);
}
if (ignore_system_crash_handler) {
crashpad::CrashpadInfo::GetCrashpadInfo()
@@ -161,7 +158,7 @@ void Start(const std::string& submit_url,
#elif BUILDFLAG(IS_MAC)
for (const auto& pair : extra)
electron::crash_keys::SetCrashKey(pair.first, pair.second);
::crash_reporter::InitializeCrashpad(process_type.empty(), process_type);
::crash_reporter::InitializeCrashpad(IsBrowserProcess(), process_type);
if (ignore_system_crash_handler) {
crashpad::CrashpadInfo::GetCrashpadInfo()
->set_system_crash_reporter_forwarding(crashpad::TriState::kDisabled);
@@ -172,8 +169,8 @@ void Start(const std::string& submit_url,
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
::crash_reporter::InitializeCrashpadWithEmbeddedHandler(
process_type.empty(), process_type,
base::WideToUTF8(user_data_dir.value()), base::FilePath());
IsBrowserProcess(), process_type, base::WideToUTF8(user_data_dir.value()),
base::FilePath());
#endif
#endif
}

View File

@@ -87,22 +87,6 @@ gfx::Point Screen::GetCursorScreenPoint(v8::Isolate* isolate) {
return screen_->GetCursorScreenPoint();
}
display::Display Screen::GetPrimaryDisplay() {
return screen_->GetPrimaryDisplay();
}
std::vector<display::Display> Screen::GetAllDisplays() {
return screen_->GetAllDisplays();
}
display::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) {
return screen_->GetDisplayNearestPoint(point);
}
display::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
return screen_->GetDisplayMatching(match_rect);
}
#if BUILDFLAG(IS_WIN)
static gfx::Rect ScreenToDIPRect(electron::NativeWindow* window,

View File

@@ -42,10 +42,18 @@ class Screen : public gin::Wrappable<Screen>,
~Screen() override;
gfx::Point GetCursorScreenPoint(v8::Isolate* isolate);
display::Display GetPrimaryDisplay();
std::vector<display::Display> GetAllDisplays();
display::Display GetDisplayNearestPoint(const gfx::Point& point);
display::Display GetDisplayMatching(const gfx::Rect& match_rect);
display::Display GetPrimaryDisplay() const {
return screen_->GetPrimaryDisplay();
}
const std::vector<display::Display>& GetAllDisplays() const {
return screen_->GetAllDisplays();
}
display::Display GetDisplayNearestPoint(const gfx::Point& point) const {
return screen_->GetDisplayNearestPoint(point);
}
display::Display GetDisplayMatching(const gfx::Rect& match_rect) const {
return screen_->GetDisplayMatching(match_rect);
}
// display::DisplayObserver:
void OnDisplayAdded(const display::Display& new_display) override;

View File

@@ -21,20 +21,6 @@ View::~View() {
delete view_;
}
#if BUILDFLAG(ENABLE_VIEWS_API)
void View::AddChildView(gin::Handle<View> child) {
AddChildViewAt(child, child_views_.size());
}
void View::AddChildViewAt(gin::Handle<View> child, size_t index) {
if (index > child_views_.size())
return;
child_views_.emplace(child_views_.begin() + index, // index
isolate(), child->GetWrapper()); // v8::Global(args...)
view()->AddChildViewAt(child->view(), index);
}
#endif
// static
gin_helper::WrappableBase* View::New(gin::Arguments* args) {
auto* view = new View();
@@ -46,11 +32,6 @@ gin_helper::WrappableBase* View::New(gin::Arguments* args) {
void View::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "View"));
#if BUILDFLAG(ENABLE_VIEWS_API)
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("addChildView", &View::AddChildView)
.SetMethod("addChildViewAt", &View::AddChildViewAt);
#endif
}
} // namespace electron::api

View File

@@ -5,10 +5,7 @@
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_VIEW_H_
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_VIEW_H_
#include <vector>
#include "base/memory/raw_ptr.h"
#include "electron/buildflags/buildflags.h"
#include "gin/handle.h"
#include "shell/common/gin_helper/wrappable.h"
#include "ui/views/view.h"
@@ -22,11 +19,6 @@ class View : public gin_helper::Wrappable<View> {
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
#if BUILDFLAG(ENABLE_VIEWS_API)
void AddChildView(gin::Handle<View> child);
void AddChildViewAt(gin::Handle<View> child, size_t index);
#endif
views::View* view() const { return view_; }
// disable copy
@@ -42,8 +34,6 @@ class View : public gin_helper::Wrappable<View> {
void set_delete_view(bool should) { delete_view_ = should; }
private:
std::vector<v8::Global<v8::Object>> child_views_;
bool delete_view_ = true;
raw_ptr<views::View> view_ = nullptr;
};

View File

@@ -11,6 +11,7 @@
#include "content/public/browser/render_widget_host_view.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "shell/browser/javascript_environment.h"
#include "shell/browser/native_browser_view.h"
#include "shell/browser/native_window.h"
namespace electron {
@@ -36,7 +37,11 @@ void AutofillDriver::ShowAutofillPopup(
v8::HandleScope scope(isolate);
auto* web_contents = api::WebContents::From(
content::WebContents::FromRenderFrameHost(render_frame_host_));
if (!web_contents || !web_contents->owner_window())
if (!web_contents)
return;
auto* owner_window = web_contents->owner_window();
if (!owner_window)
return;
auto* embedder = web_contents->embedder();
@@ -55,9 +60,23 @@ void AutofillDriver::ShowAutofillPopup(
embedder_frame_host = embedder->web_contents()->GetPrimaryMainFrame();
}
// Ensure that if the WebContents belongs to a BrowserView,
// the popup is positioned relative to the BrowserView's bounds.
for (NativeBrowserView* bv : owner_window->browser_views()) {
auto* iwc = bv->GetInspectableWebContents();
if (!iwc)
continue;
auto* awc = api::WebContents::From(iwc->GetWebContents());
if (awc == web_contents) {
auto bv_origin = bv->GetBounds().origin();
popup_bounds.Offset(gfx::Vector2dF(bv_origin.x(), bv_origin.y()));
break;
}
}
autofill_popup_->CreateView(render_frame_host_, embedder_frame_host, osr,
web_contents->owner_window()->content_view(),
popup_bounds);
owner_window->content_view(), popup_bounds);
autofill_popup_->SetItems(values, labels);
}

View File

@@ -329,10 +329,8 @@ void ElectronBrowserContext::InitPrefs() {
#endif
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
base::Value::List current_dictionaries =
prefs()->GetList(spellcheck::prefs::kSpellCheckDictionaries).Clone();
// No configured dictionaries, the default will be en-US
if (current_dictionaries.empty()) {
if (prefs()->GetList(spellcheck::prefs::kSpellCheckDictionaries).empty()) {
std::string default_code = spellcheck::GetCorrespondingSpellCheckLanguage(
base::i18n::GetConfiguredLocale());
if (!default_code.empty()) {

View File

@@ -146,9 +146,14 @@ void ElectronPermissionManager::RequestPermission(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
StatusCallback response_callback) {
StatusCallback callback) {
if (render_frame_host->IsNestedWithinFencedFrame()) {
std::move(callback).Run(blink::mojom::PermissionStatus::DENIED);
return;
}
RequestPermissionWithDetails(permission, render_frame_host, requesting_origin,
user_gesture, {}, std::move(response_callback));
user_gesture, {}, std::move(callback));
}
void ElectronPermissionManager::RequestPermissionWithDetails(
@@ -170,9 +175,15 @@ void ElectronPermissionManager::RequestPermissions(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
bool user_gesture,
StatusesCallback response_callback) {
StatusesCallback callback) {
if (render_frame_host->IsNestedWithinFencedFrame()) {
std::move(callback).Run(std::vector<blink::mojom::PermissionStatus>(
permissions.size(), blink::mojom::PermissionStatus::DENIED));
return;
}
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
{}, std::move(response_callback));
{}, std::move(callback));
}
void ElectronPermissionManager::RequestPermissionsWithDetails(
@@ -248,6 +259,12 @@ void ElectronPermissionManager::RequestPermissionsFromCurrentDocument(
bool user_gesture,
base::OnceCallback<void(const std::vector<blink::mojom::PermissionStatus>&)>
callback) {
if (render_frame_host->IsNestedWithinFencedFrame()) {
std::move(callback).Run(std::vector<blink::mojom::PermissionStatus>(
permissions.size(), blink::mojom::PermissionStatus::DENIED));
return;
}
RequestPermissionsWithDetails(permissions, render_frame_host, user_gesture,
{}, std::move(callback));
}
@@ -274,19 +291,6 @@ ElectronPermissionManager::GetPermissionResultForOriginWithoutContext(
status, content::PermissionStatusSource::UNSPECIFIED);
}
ElectronPermissionManager::SubscriptionId
ElectronPermissionManager::SubscribePermissionStatusChange(
blink::PermissionType permission,
content::RenderProcessHost* render_process_host,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) {
return SubscriptionId(-1);
}
void ElectronPermissionManager::UnsubscribePermissionStatusChange(
SubscriptionId id) {}
void ElectronPermissionManager::CheckBluetoothDevicePair(
gin_helper::Dictionary details,
PairCallback pair_callback) const {
@@ -304,9 +308,9 @@ bool ElectronPermissionManager::CheckPermissionWithDetails(
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
base::Value::Dict details) const {
if (check_handler_.is_null()) {
if (check_handler_.is_null())
return true;
}
auto* web_contents =
render_frame_host
? content::WebContents::FromRenderFrameHost(render_frame_host)
@@ -336,18 +340,17 @@ bool ElectronPermissionManager::CheckDevicePermission(
const url::Origin& origin,
const base::Value& device,
ElectronBrowserContext* browser_context) const {
if (device_permission_handler_.is_null()) {
if (device_permission_handler_.is_null())
return browser_context->CheckDevicePermission(origin, device, permission);
} else {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
.Set("deviceType", permission)
.Set("origin", origin.Serialize())
.Set("device", device.Clone())
.Build();
return device_permission_handler_.Run(details);
}
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
.Set("deviceType", permission)
.Set("origin", origin.Serialize())
.Set("device", device.Clone())
.Build();
return device_permission_handler_.Run(details);
}
void ElectronPermissionManager::GrantDevicePermission(
@@ -371,22 +374,23 @@ void ElectronPermissionManager::RevokeDevicePermission(
ElectronPermissionManager::USBProtectedClasses
ElectronPermissionManager::CheckProtectedUSBClasses(
const USBProtectedClasses& classes) const {
if (protected_usb_handler_.is_null()) {
if (protected_usb_handler_.is_null())
return classes;
} else {
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
v8::Local<v8::Object> details = gin::DataObjectBuilder(isolate)
.Set("protectedClasses", classes)
.Build();
return protected_usb_handler_.Run(details);
}
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope scope(isolate);
v8::Local<v8::Object> details =
gin::DataObjectBuilder(isolate).Set("protectedClasses", classes).Build();
return protected_usb_handler_.Run(details);
}
blink::mojom::PermissionStatus
ElectronPermissionManager::GetPermissionStatusForCurrentDocument(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host) {
if (render_frame_host->IsNestedWithinFencedFrame())
return blink::mojom::PermissionStatus::DENIED;
base::Value::Dict details;
details.Set("embeddingOrigin",
content::PermissionUtil::GetLastCommittedOriginAsURL(
@@ -412,12 +416,25 @@ ElectronPermissionManager::GetPermissionStatusForEmbeddedRequester(
blink::PermissionType permission,
content::RenderFrameHost* render_frame_host,
const url::Origin& overridden_origin) {
if (render_frame_host->IsNestedWithinFencedFrame()) {
if (render_frame_host->IsNestedWithinFencedFrame())
return blink::mojom::PermissionStatus::DENIED;
}
return GetPermissionStatus(
permission, overridden_origin.GetURL(),
render_frame_host->GetLastCommittedOrigin().GetURL());
}
ElectronPermissionManager::SubscriptionId
ElectronPermissionManager::SubscribePermissionStatusChange(
blink::PermissionType permission,
content::RenderProcessHost* render_process_host,
content::RenderFrameHost* render_frame_host,
const GURL& requesting_origin,
base::RepeatingCallback<void(blink::mojom::PermissionStatus)> callback) {
return SubscriptionId();
}
void ElectronPermissionManager::UnsubscribePermissionStatusChange(
SubscriptionId id) {}
} // namespace electron

View File

@@ -14,7 +14,7 @@ namespace electron {
NSArray* ListValueToNSArray(const base::Value::List& value) {
std::string json;
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json))
if (!base::JSONWriter::Write(base::ValueView{value}, &json))
return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData
@@ -57,7 +57,7 @@ base::Value::List NSArrayToValue(NSArray* arr) {
NSDictionary* DictionaryValueToNSDictionary(const base::Value::Dict& value) {
std::string json;
if (!base::JSONWriter::Write(base::Value(value.Clone()), &json))
if (!base::JSONWriter::Write(base::ValueView{value}, &json))
return nil;
NSData* jsonData = [NSData dataWithBytes:json.c_str() length:json.length()];
id obj = [NSJSONSerialization JSONObjectWithData:jsonData

View File

@@ -157,6 +157,7 @@ class NativeWindowMac : public NativeWindow,
bool IsActive() const override;
// Remove the specified child window without closing it.
void RemoveChildWindow(NativeWindow* child) override;
void RemoveChildFromParentWindow(NativeWindow* child);
// Attach child windows, if the window is visible.
void AttachChildren() override;
// Detach window from parent without destroying it.

View File

@@ -388,6 +388,9 @@ void NativeWindowMac::Close() {
return;
}
// Ensure we're detached from the parent window before closing.
RemoveChildFromParentWindow(this);
// If a sheet is attached to the window when we call
// [window_ performClose:nil], the window won't close properly
// even after the user has ended the sheet.
@@ -405,6 +408,8 @@ void NativeWindowMac::Close() {
}
void NativeWindowMac::CloseImmediately() {
RemoveChildFromParentWindow(this);
// Retain the child window before closing it. If the last reference to the
// NSWindow goes away inside -[NSWindow close], then bad stuff can happen.
// See e.g. http://crbug.com/616701.
@@ -598,6 +603,11 @@ void NativeWindowMac::RemoveChildWindow(NativeWindow* child) {
[window_ removeChildWindow:child->GetNativeWindow().GetNativeNSWindow()];
}
void NativeWindowMac::RemoveChildFromParentWindow(NativeWindow* child) {
if (parent())
parent()->RemoveChildWindow(child);
}
void NativeWindowMac::AttachChildren() {
for (auto* child : child_windows_) {
auto* child_nswindow = child->GetNativeWindow().GetNativeNSWindow();
@@ -1781,27 +1791,27 @@ void NativeWindowMac::InternalSetWindowButtonVisibility(bool visible) {
[[window_ standardWindowButton:NSWindowZoomButton] setHidden:!visible];
}
void NativeWindowMac::InternalSetParentWindow(NativeWindow* parent,
void NativeWindowMac::InternalSetParentWindow(NativeWindow* new_parent,
bool attach) {
if (is_modal())
return;
NativeWindow::SetParentWindow(parent);
// Do not remove/add if we are already properly attached.
if (attach && parent &&
[window_ parentWindow] == parent->GetNativeWindow().GetNativeNSWindow())
if (attach && new_parent &&
[window_ parentWindow] ==
new_parent->GetNativeWindow().GetNativeNSWindow())
return;
// Remove current parent window.
if ([window_ parentWindow])
parent->RemoveChildWindow(this);
RemoveChildFromParentWindow(this);
// Set new parent window.
if (parent && attach) {
parent->add_child_window(this);
parent->AttachChildren();
if (new_parent && attach) {
new_parent->add_child_window(this);
new_parent->AttachChildren();
}
NativeWindow::SetParentWindow(new_parent);
}
void NativeWindowMac::SetForwardMouseMessages(bool forward) {

View File

@@ -36,7 +36,6 @@
#include "ui/gfx/image/image.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/background.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/widget.h"
@@ -166,7 +165,8 @@ class NativeWindowClientView : public views::ClientView {
NativeWindowClientView(views::Widget* widget,
views::View* root_view,
NativeWindowViews* window)
: views::ClientView(widget, root_view), window_(window) {}
: views::ClientView{widget, root_view},
window_{raw_ref<NativeWindowViews>::from_ptr(window)} {}
~NativeWindowClientView() override = default;
// disable copy
@@ -179,22 +179,19 @@ class NativeWindowClientView : public views::ClientView {
}
private:
raw_ptr<NativeWindowViews> window_;
const raw_ref<NativeWindowViews> window_;
};
} // namespace
NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
NativeWindow* parent)
: NativeWindow(options, parent),
root_view_(std::make_unique<RootView>(this)),
keyboard_event_handler_(
std::make_unique<views::UnhandledKeyboardEventHandler>()) {
: NativeWindow(options, parent) {
options.Get(options::kTitle, &title_);
bool menu_bar_autohide;
if (options.Get(options::kAutoHideMenuBar, &menu_bar_autohide))
root_view_->SetAutoHideMenuBar(menu_bar_autohide);
root_view_.SetAutoHideMenuBar(menu_bar_autohide);
#if BUILDFLAG(IS_WIN)
// On Windows we rely on the CanResize() to indicate whether window can be
@@ -455,12 +452,12 @@ void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) {
void NativeWindowViews::SetContentView(views::View* view) {
if (content_view()) {
root_view_->RemoveChildView(content_view());
root_view_.RemoveChildView(content_view());
}
set_content_view(view);
focused_view_ = view;
root_view_->AddChildView(content_view());
root_view_->Layout();
root_view_.AddChildView(content_view());
root_view_.Layout();
}
void NativeWindowViews::Close() {
@@ -734,10 +731,14 @@ void NativeWindowViews::SetFullScreen(bool fullscreen) {
gfx::Rect());
// Auto-hide menubar when in fullscreen.
if (fullscreen)
if (fullscreen) {
menu_bar_visible_before_fullscreen_ = IsMenuBarVisible();
SetMenuBarVisibility(false);
else
SetMenuBarVisibility(!IsMenuBarAutoHide());
} else {
SetMenuBarVisibility(!IsMenuBarAutoHide() &&
menu_bar_visible_before_fullscreen_);
menu_bar_visible_before_fullscreen_ = false;
}
#endif
}
@@ -1097,7 +1098,7 @@ bool NativeWindowViews::IsTabletMode() const {
}
SkColor NativeWindowViews::GetBackgroundColor() {
auto* background = root_view_->background();
auto* background = root_view_.background();
if (!background)
return SK_ColorTRANSPARENT;
return background->get_color();
@@ -1105,7 +1106,7 @@ SkColor NativeWindowViews::GetBackgroundColor() {
void NativeWindowViews::SetBackgroundColor(SkColor background_color) {
// web views' background color.
root_view_->SetBackground(views::CreateSolidBackground(background_color));
root_view_.SetBackground(views::CreateSolidBackground(background_color));
#if BUILDFLAG(IS_WIN)
// Set the background color of native window.
@@ -1240,7 +1241,7 @@ void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) {
// Remove global menu bar.
if (global_menu_bar_ && menu_model == nullptr) {
global_menu_bar_.reset();
root_view_->UnregisterAcceleratorsWithFocusManager();
root_view_.UnregisterAcceleratorsWithFocusManager();
return;
}
@@ -1249,7 +1250,7 @@ void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) {
if (!global_menu_bar_)
global_menu_bar_ = std::make_unique<GlobalMenuBarX11>(this);
if (global_menu_bar_->IsServerStarted()) {
root_view_->RegisterAcceleratorsWithFocusManager(menu_model);
root_view_.RegisterAcceleratorsWithFocusManager(menu_model);
global_menu_bar_->SetMenu(menu_model);
return;
}
@@ -1260,13 +1261,13 @@ void NativeWindowViews::SetMenu(ElectronMenuModel* menu_model) {
gfx::Size content_size = GetContentSize();
bool should_reset_size = use_content_size_ && has_frame() &&
!IsMenuBarAutoHide() &&
((!!menu_model) != root_view_->HasMenu());
((!!menu_model) != root_view_.HasMenu());
root_view_->SetMenu(menu_model);
root_view_.SetMenu(menu_model);
if (should_reset_size) {
// Enlarge the size constraints for the menu.
int menu_bar_height = root_view_->GetMenuBarHeight();
int menu_bar_height = root_view_.GetMenuBarHeight();
extensions::SizeConstraints constraints = GetContentSizeConstraints();
if (constraints.HasMinimumSize()) {
gfx::Size min_size = constraints.GetMinimumSize();
@@ -1393,19 +1394,19 @@ void NativeWindowViews::SetOverlayIcon(const gfx::Image& overlay,
}
void NativeWindowViews::SetAutoHideMenuBar(bool auto_hide) {
root_view_->SetAutoHideMenuBar(auto_hide);
root_view_.SetAutoHideMenuBar(auto_hide);
}
bool NativeWindowViews::IsMenuBarAutoHide() {
return root_view_->IsMenuBarAutoHide();
return root_view_.IsMenuBarAutoHide();
}
void NativeWindowViews::SetMenuBarVisibility(bool visible) {
root_view_->SetMenuBarVisibility(visible);
root_view_.SetMenuBarVisibility(visible);
}
bool NativeWindowViews::IsMenuBarVisible() {
return root_view_->IsMenuBarVisible();
return root_view_.IsMenuBarVisible();
}
void NativeWindowViews::SetBackgroundMaterial(const std::string& material) {
@@ -1503,8 +1504,8 @@ gfx::Rect NativeWindowViews::ContentBoundsToWindowBounds(
}
#endif
if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
int menu_bar_height = root_view_->GetMenuBarHeight();
if (root_view_.HasMenu() && root_view_.IsMenuBarVisible()) {
int menu_bar_height = root_view_.GetMenuBarHeight();
window_bounds.set_y(window_bounds.y() - menu_bar_height);
window_bounds.set_height(window_bounds.height() + menu_bar_height);
}
@@ -1530,8 +1531,8 @@ gfx::Rect NativeWindowViews::WindowBoundsToContentBounds(
content_bounds.set_size(ScreenToDIPRect(hwnd, content_bounds).size());
#endif
if (root_view_->HasMenu() && root_view_->IsMenuBarVisible()) {
int menu_bar_height = root_view_->GetMenuBarHeight();
if (root_view_.HasMenu() && root_view_.IsMenuBarVisible()) {
int menu_bar_height = root_view_.GetMenuBarHeight();
content_bounds.set_y(content_bounds.y() + menu_bar_height);
content_bounds.set_height(content_bounds.height() - menu_bar_height);
}
@@ -1574,7 +1575,7 @@ void NativeWindowViews::OnWidgetActivationChanged(views::Widget* changed_widget,
if (!active && IsMenuBarAutoHide() && IsMenuBarVisible())
SetMenuBarVisibility(false);
root_view_->ResetAltState();
root_view_.ResetAltState();
}
void NativeWindowViews::OnWidgetBoundsChanged(views::Widget* changed_widget,
@@ -1630,7 +1631,7 @@ std::u16string NativeWindowViews::GetWindowTitle() const {
}
views::View* NativeWindowViews::GetContentsView() {
return root_view_.get();
return &root_view_;
}
bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
@@ -1640,7 +1641,7 @@ bool NativeWindowViews::ShouldDescendIntoChildForEventHandling(
}
views::ClientView* NativeWindowViews::CreateClientView(views::Widget* widget) {
return new NativeWindowClientView(widget, root_view_.get(), this);
return new NativeWindowClientView{widget, GetContentsView(), this};
}
std::unique_ptr<views::NonClientFrameView>
@@ -1679,9 +1680,9 @@ void NativeWindowViews::HandleKeyboardEvent(
NotifyWindowExecuteAppCommand(kBrowserForward);
#endif
keyboard_event_handler_->HandleKeyboardEvent(event,
root_view_->GetFocusManager());
root_view_->HandleKeyEvent(event);
keyboard_event_handler_.HandleKeyboardEvent(event,
root_view_.GetFocusManager());
root_view_.HandleKeyEvent(event);
}
void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
@@ -1689,7 +1690,7 @@ void NativeWindowViews::OnMouseEvent(ui::MouseEvent* event) {
return;
// Alt+Click should not toggle menu bar.
root_view_->ResetAltState();
root_view_.ResetAltState();
#if BUILDFLAG(IS_LINUX)
if (event->changed_button_flags() == ui::EF_BACK_MOUSE_BUTTON)

View File

@@ -13,6 +13,8 @@
#include <vector>
#include "base/memory/raw_ptr.h"
#include "shell/browser/ui/views/root_view.h"
#include "ui/views/controls/webview/unhandled_keyboard_event_handler.h"
#include "ui/views/widget/widget_observer.h"
#if defined(USE_OZONE)
@@ -28,14 +30,9 @@
#endif
namespace views {
class UnhandledKeyboardEventHandler;
}
namespace electron {
class GlobalMenuBarX11;
class RootView;
class WindowStateWatcher;
#if defined(USE_OZONE_PLATFORM_X11)
@@ -251,7 +248,7 @@ class NativeWindowViews : public NativeWindow,
// Maintain window placement.
void MoveBehindTaskBarIfNeeded();
std::unique_ptr<RootView> root_view_;
RootView root_view_{this};
// The view should be focused by default.
raw_ptr<views::View> focused_view_ = nullptr;
@@ -322,7 +319,10 @@ class NativeWindowViews : public NativeWindow,
#endif
// Handles unhandled keyboard messages coming back from the renderer process.
std::unique_ptr<views::UnhandledKeyboardEventHandler> keyboard_event_handler_;
views::UnhandledKeyboardEventHandler keyboard_event_handler_;
// Whether the menubar is visible before the window enters fullscreen
bool menu_bar_visible_before_fullscreen_ = false;
// Whether the window should be enabled based on user calls to SetEnabled()
bool is_enabled_ = true;

View File

@@ -726,10 +726,8 @@ void OffScreenRenderWidgetHostView::CompositeFrame(
}
}
paint_callback_running_ = true;
callback_.Run(gfx::IntersectRects(gfx::Rect(size_in_pixels), damage_rect),
frame);
paint_callback_running_ = false;
ReleaseResize();
}

View File

@@ -19,7 +19,6 @@
#include "base/memory/raw_ptr.h"
#include "base/process/kill.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "components/viz/common/quads/compositor_frame.h"
#include "components/viz/common/surfaces/parent_local_surface_id_allocator.h"
#include "content/browser/renderer_host/delegated_frame_host.h" // nogncheck
@@ -259,9 +258,6 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,
int frame_rate_ = 0;
int frame_rate_threshold_us_ = 0;
base::Time last_time_ = base::Time::Now();
gfx::Vector2dF last_scroll_offset_;
gfx::Size size_;
bool painting_;
@@ -272,8 +268,6 @@ class OffScreenRenderWidgetHostView : public content::RenderWidgetHostViewBase,
bool hold_resize_ = false;
bool pending_resize_ = false;
bool paint_callback_running_ = false;
viz::LocalSurfaceId delegated_frame_host_surface_id_;
viz::ParentLocalSurfaceIdAllocator delegated_frame_host_allocator_;

View File

@@ -281,11 +281,29 @@ void ReadDialogPathsWithBookmarks(NSOpenPanel* dialog,
std::vector<base::FilePath>* paths,
std::vector<std::string>* bookmarks) {
NSArray* urls = [dialog URLs];
for (NSURL* url in urls)
if ([url isFileURL]) {
paths->emplace_back(base::SysNSStringToUTF8([url path]));
bookmarks->push_back(GetBookmarkDataFromNSURL(url));
for (NSURL* url in urls) {
if (![url isFileURL])
continue;
NSString* path = [url path];
// There's a bug in macOS where despite a request to disallow file
// selection, files/packages can be selected. If file selection
// was disallowed, drop any files selected. See crbug.com/1357523.
if (![dialog canChooseFiles]) {
BOOL is_directory;
BOOL exists =
[[NSFileManager defaultManager] fileExistsAtPath:path
isDirectory:&is_directory];
BOOL is_package =
[[NSWorkspace sharedWorkspace] isFilePackageAtPath:path];
if (!exists || !is_directory || is_package)
continue;
}
paths->emplace_back(base::SysNSStringToUTF8(path));
bookmarks->push_back(GetBookmarkDataFromNSURL(url));
}
}
void ReadDialogPaths(NSOpenPanel* dialog, std::vector<base::FilePath>* paths) {

View File

@@ -21,6 +21,7 @@
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/devtools/devtools_contents_resizing_strategy.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
@@ -624,13 +625,8 @@ void InspectableWebContents::AddDevToolsExtensionsToClient() {
#endif
void InspectableWebContents::SetInspectedPageBounds(const gfx::Rect& rect) {
DevToolsContentsResizingStrategy strategy(rect);
if (contents_resizing_strategy_.Equals(strategy))
return;
contents_resizing_strategy_.CopyFrom(strategy);
if (managed_devtools_web_contents_)
view_->SetContentsResizingStrategy(contents_resizing_strategy_);
view_->SetContentsResizingStrategy(DevToolsContentsResizingStrategy{rect});
}
void InspectableWebContents::InspectElementCompleted() {}

View File

@@ -10,15 +10,12 @@
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/containers/span.h"
#include "base/containers/unique_ptr_adapters.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/devtools/devtools_contents_resizing_strategy.h"
#include "chrome/browser/devtools/devtools_embedder_message_dispatcher.h"
#include "chrome/browser/devtools/devtools_settings.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_frontend_host.h"
#include "content/public/browser/web_contents_delegate.h"
@@ -202,7 +199,6 @@ class InspectableWebContents
void AddDevToolsExtensionsToClient();
#endif
DevToolsContentsResizingStrategy contents_resizing_strategy_;
gfx::Rect devtools_bounds_;
bool can_dock_ = true;
std::string dock_state_;

View File

@@ -206,7 +206,7 @@ void UsbChooserContext::RevokeObjectPermissionInternal(
v8::HandleScope scope(isolate);
gin_helper::Dictionary details =
gin_helper::Dictionary::CreateEmpty(isolate);
details.Set("device", object.Clone());
details.Set("device", object);
details.Set("origin", origin.Serialize());
session->Emit("usb-device-revoked", details);
}

View File

@@ -9,22 +9,6 @@ namespace electron {
const char kBrowserForward[] = "browser-forward";
const char kBrowserBackward[] = "browser-backward";
const char kSHA1Certificate[] = "SHA-1 Certificate";
const char kSHA1MajorDescription[] =
"The certificate for this site expires in 2017 or later, "
"and the certificate chain contains a certificate signed using SHA-1.";
const char kSHA1MinorDescription[] =
"The certificate for this site expires in 2016, "
"and the certificate chain contains a certificate signed using SHA-1.";
const char kCertificateError[] = "Certificate Error";
const char kValidCertificate[] = "Valid Certificate";
const char kValidCertificateDescription[] =
"The connection to this site is using a valid, trusted server certificate.";
const char kSecureProtocol[] = "Secure TLS connection";
const char kSecureProtocolDescription[] =
"The connection to this site is using a strong protocol version "
"and cipher suite.";
const char kDeviceVendorIdKey[] = "vendorId";
const char kDeviceProductIdKey[] = "productId";
const char kDeviceSerialNumberKey[] = "serialNumber";

View File

@@ -15,16 +15,6 @@ namespace electron {
extern const char kBrowserForward[];
extern const char kBrowserBackward[];
// Strings describing Chrome security policy for DevTools security panel.
extern const char kSHA1Certificate[];
extern const char kSHA1MajorDescription[];
extern const char kSHA1MinorDescription[];
extern const char kCertificateError[];
extern const char kValidCertificate[];
extern const char kValidCertificateDescription[];
extern const char kSecureProtocol[];
extern const char kSecureProtocolDescription[];
// Keys for Device APIs
extern const char kDeviceVendorIdKey[];
extern const char kDeviceProductIdKey[];

View File

@@ -22,7 +22,7 @@ v8::Local<v8::Value> Converter<const extensions::Extension*>::ToV8(
dict.Set("path", extension->path());
dict.Set("url", extension->url());
dict.Set("version", extension->VersionString());
dict.Set("manifest", extension->manifest()->value()->Clone());
dict.Set("manifest", *extension->manifest()->value());
return gin::ConvertToV8(isolate, dict);
}

View File

@@ -25,14 +25,6 @@ bool Converter<base::Value::Dict>::FromV8(v8::Isolate* isolate,
}
}
v8::Local<v8::Value> Converter<base::Value::Dict>::ToV8(
v8::Isolate* isolate,
const base::Value::Dict& val) {
base::Value value(val.Clone());
return content::V8ValueConverter::Create()->ToV8Value(
value, isolate->GetCurrentContext());
}
bool Converter<base::Value>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value* out) {
@@ -47,8 +39,9 @@ bool Converter<base::Value>::FromV8(v8::Isolate* isolate,
}
}
v8::Local<v8::Value> Converter<base::Value>::ToV8(v8::Isolate* isolate,
const base::Value& val) {
v8::Local<v8::Value> Converter<base::ValueView>::ToV8(
v8::Isolate* isolate,
const base::ValueView val) {
return content::V8ValueConverter::Create()->ToV8Value(
val, isolate->GetCurrentContext());
}
@@ -67,12 +60,4 @@ bool Converter<base::Value::List>::FromV8(v8::Isolate* isolate,
}
}
v8::Local<v8::Value> Converter<base::Value::List>::ToV8(
v8::Isolate* isolate,
const base::Value::List& val) {
base::Value value(val.Clone());
return content::V8ValueConverter::Create()->ToV8Value(
value, isolate->GetCurrentContext());
}
} // namespace gin

View File

@@ -10,13 +10,21 @@
namespace gin {
template <>
struct Converter<base::ValueView> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::ValueView val);
};
template <>
struct Converter<base::Value::Dict> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::Value::Dict* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value::Dict& val);
const base::Value::Dict& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
};
template <>
@@ -25,7 +33,9 @@ struct Converter<base::Value> {
v8::Local<v8::Value> val,
base::Value* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value& val);
const base::Value& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
};
template <>
@@ -34,7 +44,9 @@ struct Converter<base::Value::List> {
v8::Local<v8::Value> val,
base::Value::List* out);
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::Value::List& val);
const base::Value::List& val) {
return gin::ConvertToV8(isolate, base::ValueView{val});
}
};
} // namespace gin

View File

@@ -23,7 +23,6 @@
#include "base/trace_event/trace_event.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
#include "electron/buildflags/buildflags.h"
#include "electron/fuses.h"
#include "shell/browser/api/electron_api_app.h"
@@ -350,20 +349,17 @@ NodeBindings::~NodeBindings() {
void NodeBindings::RegisterBuiltinBindings() {
#define V(modname) _register_##modname();
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(::switches::kProcessType);
if (process_type.empty()) {
if (IsBrowserProcess()) {
ELECTRON_BROWSER_BINDINGS(V)
#if BUILDFLAG(ENABLE_VIEWS_API)
ELECTRON_VIEWS_BINDINGS(V)
#endif
}
ELECTRON_COMMON_BINDINGS(V)
if (process_type == ::switches::kRendererProcess) {
if (IsRendererProcess()) {
ELECTRON_RENDERER_BINDINGS(V)
}
if (process_type == ::switches::kUtilityProcess) {
if (IsUtilityProcess()) {
ELECTRON_UTILITY_BINDINGS(V)
}
#if DCHECK_IS_ON()

View File

@@ -123,8 +123,6 @@ const char kZoomFactor[] = "zoomFactor";
// Script that will be loaded by guest WebContents before other scripts.
const char kPreloadScript[] = "preload";
const char kPreloadScripts[] = "preloadScripts";
// Enable the node integration.
const char kNodeIntegration[] = "nodeIntegration";
@@ -173,9 +171,6 @@ const char kJavaScript[] = "javascript";
// Enables image support.
const char kImages[] = "images";
// Image animation policy.
const char kImageAnimationPolicy[] = "imageAnimationPolicy";
// Make TextArea elements resizable.
const char kTextAreasAreResizable[] = "textAreasAreResizable";

View File

@@ -67,7 +67,6 @@ extern const char kOverlayHeight[];
// WebPreferences.
extern const char kZoomFactor[];
extern const char kPreloadScript[];
extern const char kPreloadScripts[];
extern const char kNodeIntegration[];
extern const char kContextIsolation[];
extern const char kExperimentalFeatures[];
@@ -86,7 +85,6 @@ extern const char kNodeIntegrationInSubFrames[];
extern const char kDisableHtmlFullscreenWindowResize[];
extern const char kJavaScript[];
extern const char kImages[];
extern const char kImageAnimationPolicy[];
extern const char kTextAreasAreResizable[];
extern const char kWebGL[];
extern const char kNavigateOnDragDrop[];

View File

@@ -26,18 +26,29 @@ void EmitWarning(node::Environment* env,
emit_warning.Run(warning_msg, warning_type, "");
}
bool IsBrowserProcess() {
std::string GetProcessType() {
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(switches::kProcessType);
return process_type.empty();
return command_line->GetSwitchValueASCII(switches::kProcessType);
}
bool IsBrowserProcess() {
static bool result = GetProcessType().empty();
return result;
}
bool IsRendererProcess() {
auto* command_line = base::CommandLine::ForCurrentProcess();
std::string process_type =
command_line->GetSwitchValueASCII(switches::kProcessType);
return process_type == switches::kRendererProcess;
static bool result = GetProcessType() == switches::kRendererProcess;
return result;
}
bool IsUtilityProcess() {
static bool result = GetProcessType() == switches::kUtilityProcess;
return result;
}
bool IsZygoteProcess() {
static bool result = GetProcessType() == switches::kZygoteProcess;
return result;
}
} // namespace electron

View File

@@ -17,8 +17,12 @@ void EmitWarning(node::Environment* env,
const std::string& warning_msg,
const std::string& warning_type);
std::string GetProcessType();
bool IsBrowserProcess();
bool IsRendererProcess();
bool IsUtilityProcess();
bool IsZygoteProcess();
} // namespace electron

View File

@@ -4677,6 +4677,21 @@ describe('BrowserWindow module', () => {
expect(w.getChildWindows().length).to.equal(0);
});
it('can handle child window close and reparent multiple times', async () => {
const w = new BrowserWindow({ show: false });
let c: BrowserWindow | null;
for (let i = 0; i < 5; i++) {
c = new BrowserWindow({ show: false, parent: w });
const closed = once(c, 'closed');
c.close();
await closed;
}
await setTimeout();
expect(w.getChildWindows().length).to.equal(0);
});
ifit(process.platform === 'darwin')('child window matches visibility when visibility changes', async () => {
const w = new BrowserWindow({ show: false });
const c = new BrowserWindow({ show: false, parent: w });
@@ -5338,6 +5353,48 @@ describe('BrowserWindow module', () => {
});
});
ifdescribe(process.platform !== 'darwin')('when fullscreen state is changed', () => {
it('correctly remembers state prior to fullscreen change', async () => {
const w = new BrowserWindow({ show: false });
expect(w.isMenuBarVisible()).to.be.true('isMenuBarVisible');
w.setMenuBarVisibility(false);
expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
const enterFS = once(w, 'enter-full-screen');
w.setFullScreen(true);
await enterFS;
expect(w.fullScreen).to.be.true('not fullscreen');
const exitFS = once(w, 'leave-full-screen');
w.setFullScreen(false);
await exitFS;
expect(w.fullScreen).to.be.false('not fullscreen');
expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
});
it('correctly remembers state prior to fullscreen change with autoHide', async () => {
const w = new BrowserWindow({ show: false });
expect(w.autoHideMenuBar).to.be.false('autoHideMenuBar');
w.autoHideMenuBar = true;
expect(w.autoHideMenuBar).to.be.true('autoHideMenuBar');
w.setMenuBarVisibility(false);
expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
const enterFS = once(w, 'enter-full-screen');
w.setFullScreen(true);
await enterFS;
expect(w.fullScreen).to.be.true('not fullscreen');
const exitFS = once(w, 'leave-full-screen');
w.setFullScreen(false);
await exitFS;
expect(w.fullScreen).to.be.false('not fullscreen');
expect(w.isMenuBarVisible()).to.be.false('isMenuBarVisible');
});
});
ifdescribe(process.platform === 'darwin')('fullscreenable state', () => {
it('with functions', () => {
it('can be set with fullscreenable constructor option', () => {

View File

@@ -1211,6 +1211,42 @@ describe('protocol module', () => {
await expect(net.fetch('test-scheme://foo')).to.eventually.be.rejectedWith('net::ERR_FAILED');
});
it('handles invalid protocol response status', async () => {
protocol.handle('test-scheme', () => {
return { status: [] } as any;
});
defer(() => { protocol.unhandle('test-scheme'); });
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
});
it('handles invalid protocol response statusText', async () => {
protocol.handle('test-scheme', () => {
return { statusText: false } as any;
});
defer(() => { protocol.unhandle('test-scheme'); });
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
});
it('handles invalid protocol response header parameters', async () => {
protocol.handle('test-scheme', () => {
return { headers: false } as any;
});
defer(() => { protocol.unhandle('test-scheme'); });
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
});
it('handles invalid protocol response body parameters', async () => {
protocol.handle('test-scheme', () => {
return { body: false } as any;
});
defer(() => { protocol.unhandle('test-scheme'); });
await expect(net.fetch('test-scheme://foo')).to.be.rejectedWith('net::ERR_UNEXPECTED');
});
it('handles a synchronous error in the handler', async () => {
protocol.handle('test-scheme', () => { throw new Error('test'); });
defer(() => { protocol.unhandle('test-scheme'); });

View File

@@ -194,10 +194,10 @@
"@octokit/auth-app" "^4.0.13"
"@octokit/rest" "^19.0.11"
"@electron/lint-roller@^1.2.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@electron/lint-roller/-/lint-roller-1.2.1.tgz#9f9d99b0a8975646e0a0131ab1b21a2ec62e80d8"
integrity sha512-w9PelpTBX8ClAv2iVa8fYqK77dnN0zWiHW98Utf8D83nmxkCMQNrKdRupSyiuIttbic1Nao8FhTScppmzOz0gw==
"@electron/lint-roller@^1.5.0":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@electron/lint-roller/-/lint-roller-1.5.0.tgz#9b743979e1b03327e475fa696bb781eb2ea05ef2"
integrity sha512-205UxwJEx8zv5wLwPq4wMA0OYrJ7d1GuqOhPav0Uy2HWe4K+DZbSP50safCvZCSpI6Op3DMo79tp5i8VppuPWA==
dependencies:
"@dsanders11/vscode-markdown-languageservice" "^0.3.0"
glob "^8.1.0"
@@ -206,6 +206,7 @@
mdast-util-from-markdown "^1.3.0"
minimist "^1.2.8"
node-fetch "^2.6.9"
rimraf "^4.4.1"
standard "^17.0.0"
unist-util-visit "^4.1.2"
vscode-languageserver "^8.1.0"
@@ -1066,6 +1067,11 @@
dependencies:
"@types/node" "*"
"@types/w3c-web-serial@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@types/w3c-web-serial/-/w3c-web-serial-1.0.3.tgz#9fd5e8542f74e464bb1715b384b5c0dcbf2fb2c3"
integrity sha512-R4J/OjqKAUFQoXVIkaUTfzb/sl6hLh/ZhDTfowJTRMa7LhgEmI/jXV4zsL1u8HpNa853BxwNmDIr0pauizzwSQ==
"@types/webpack-env@^1.17.0":
version "1.17.0"
resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.17.0.tgz#f99ce359f1bfd87da90cc4a57cab0a18f34a48d0"
@@ -3156,6 +3162,16 @@ glob@^8.1.0:
minimatch "^5.0.1"
once "^1.3.0"
glob@^9.2.0:
version "9.3.5"
resolved "https://registry.yarnpkg.com/glob/-/glob-9.3.5.tgz#ca2ed8ca452781a3009685607fdf025a899dfe21"
integrity sha512-e1LleDykUz2Iu+MTYdkSsuWX8lvAjAcs0Xef0lNIu0S2wOAzuTxCJtcd9S3cijlwYF18EsU3rzb8jPVobxDh9Q==
dependencies:
fs.realpath "^1.0.0"
minimatch "^8.0.2"
minipass "^4.2.4"
path-scurry "^1.6.1"
glob@~8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/glob/-/glob-8.0.3.tgz#415c6eb2deed9e502c68fa44a272e6da6eeca42e"
@@ -4078,7 +4094,7 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"
lru-cache@^9.0.0:
lru-cache@^9.0.0, lru-cache@^9.1.1:
version "9.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-9.1.1.tgz#c58a93de58630b688de39ad04ef02ef26f1902f1"
integrity sha512-65/Jky17UwSb0BuB9V+MyDpsOtXKmYwzhyl+cOa9XUiI4uV2Ouy/2voFP3+al0BjZbJgMBD8FojMpAf+Z+qn4A==
@@ -4514,6 +4530,13 @@ minimatch@^5.0.1:
dependencies:
brace-expansion "^2.0.1"
minimatch@^8.0.2:
version "8.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-8.0.4.tgz#847c1b25c014d4e9a7f68aaf63dedd668a626229"
integrity sha512-W0Wvr9HyFXZRGIDgCicunpQ299OKXs9RgZfaukz4qAW/pJhcpUfupc9c+OObPOFueNy8VSrZgEmDtk6Kh4WzDA==
dependencies:
brace-expansion "^2.0.1"
minimatch@~5.1.2:
version "5.1.2"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-5.1.2.tgz#0939d7d6f0898acbd1508abe534d1929368a8fff"
@@ -4543,6 +4566,16 @@ minipass@^4.0.0:
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.0.1.tgz#2b9408c6e81bb8b338d600fb3685e375a370a057"
integrity sha512-V9esFpNbK0arbN3fm2sxDKqMYgIp7XtVdE4Esj+PE4Qaaxdg1wIw48ITQIOn1sc8xXSmUviVL3cyjMqPlrVkiA==
minipass@^4.2.4:
version "4.2.8"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-4.2.8.tgz#f0010f64393ecfc1d1ccb5f582bcaf45f48e1a3a"
integrity sha512-fNzuVyifolSLFL4NzpF+wEF4qrgqaaKX0haXPQEdQ7NKAN+WecoKMHV09YcuL/DHxrUsYQOK3MiuDf7Ip2OXfQ==
"minipass@^5.0.0 || ^6.0.2":
version "6.0.2"
resolved "https://registry.yarnpkg.com/minipass/-/minipass-6.0.2.tgz#542844b6c4ce95b202c0995b0a471f1229de4c81"
integrity sha512-MzWSV5nYVT7mVyWCwn2o7JH13w2TBRmmSqSRCKzTw+lmft9X4z+3wjvs06Tzijo5z4W/kahUCDpRXTF+ZrmF/w==
minizlib@^2.1.1:
version "2.1.2"
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-2.1.2.tgz#e90d3466ba209b932451508a11ce3d3632145931"
@@ -4933,6 +4966,14 @@ path-parse@^1.0.6, path-parse@^1.0.7:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
path-scurry@^1.6.1:
version "1.9.2"
resolved "https://registry.yarnpkg.com/path-scurry/-/path-scurry-1.9.2.tgz#90f9d296ac5e37e608028e28a447b11d385b3f63"
integrity sha512-qSDLy2aGFPm8i4rsbHd4MNyTcrzHFsLQykrtbuGRknZZCBBVXSv2tSCDN2Cg6Rt/GFRw8GoW9y9Ecw5rIPG1sg==
dependencies:
lru-cache "^9.1.1"
minipass "^5.0.0 || ^6.0.2"
path-to-regexp@0.1.7:
version "0.1.7"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"
@@ -5830,6 +5871,13 @@ rimraf@^3.0.2:
dependencies:
glob "^7.1.3"
rimraf@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-4.4.1.tgz#bd33364f67021c5b79e93d7f4fa0568c7c21b755"
integrity sha512-Gk8NlF062+T9CqNGn6h4tls3k6T1+/nXdOcSZVikNVtlRdYpA7wRJJMoXmuvOnLW844rPjdQ7JgXCYM6PPC/og==
dependencies:
glob "^9.2.0"
rimraf@~2.2.6:
version "2.2.8"
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.2.8.tgz#e439be2aaee327321952730f99a8929e4fc50582"