Skip to content

Commit 9ef83cd

Browse files
jeremyspiegelcodebytere
authored andcommitted
fix: properly pass openExternal activate option (electron#18722)
A reference to an OpenExternalOptions structure was being captured by an Objective-C block that outlived the object that was being referenced.
1 parent f33517d commit 9ef83cd

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

atom/common/platform_util_mac.mm

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,15 @@ void OpenExternal(const GURL& url,
108108
return;
109109
}
110110

111+
bool activate = options.activate;
111112
__block OpenExternalCallback c = std::move(callback);
112-
dispatch_async(
113-
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
114-
__block std::string error = OpenURL(ns_url, options.activate);
115-
dispatch_async(dispatch_get_main_queue(), ^{
116-
std::move(c).Run(error);
117-
});
118-
});
113+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
114+
^{
115+
__block std::string error = OpenURL(ns_url, activate);
116+
dispatch_async(dispatch_get_main_queue(), ^{
117+
std::move(c).Run(error);
118+
});
119+
});
119120
}
120121

121122
bool MoveItemToTrash(const base::FilePath& full_path) {

spec/api-shell-spec.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ const assert = require('assert')
22
const fs = require('fs')
33
const path = require('path')
44
const os = require('os')
5-
const { shell } = require('electron')
5+
const { shell, remote } = require('electron')
6+
const { BrowserWindow } = remote
7+
8+
const { closeWindow } = require('./window-helpers')
69

710
describe('shell module', () => {
811
const fixtures = path.resolve(__dirname, 'fixtures')
@@ -18,6 +21,7 @@ describe('shell module', () => {
1821

1922
describe('shell.openExternal()', () => {
2023
let envVars = {}
24+
let w
2125

2226
beforeEach(function () {
2327
envVars = {
@@ -27,7 +31,9 @@ describe('shell module', () => {
2731
}
2832
})
2933

30-
afterEach(() => {
34+
afterEach(async () => {
35+
await closeWindow(w)
36+
w = null
3137
// reset env vars to prevent side effects
3238
if (process.platform === 'linux') {
3339
process.env.DE = envVars.de
@@ -44,7 +50,24 @@ describe('shell module', () => {
4450
process.env.DISPLAY = ''
4551
}
4652

47-
shell.openExternal(url).then(() => done())
53+
// Ensure an external window is activated via a new window's blur event
54+
w = new BrowserWindow()
55+
let promiseResolved = false
56+
let blurEventEmitted = false
57+
58+
w.on('blur', () => {
59+
blurEventEmitted = true
60+
if (promiseResolved) {
61+
done()
62+
}
63+
})
64+
65+
shell.openExternal(url).then(() => {
66+
promiseResolved = true
67+
if (blurEventEmitted || process.platform === 'linux') {
68+
done()
69+
}
70+
})
4871
})
4972

5073
it('opens an external link synchronously', () => {

0 commit comments

Comments
 (0)