Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.

Commit ec6af16

Browse files
committed
Frame manager (fixes #19 and #20)
1 parent c6806b7 commit ec6af16

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

Diff for: src/Server/Preload.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
setExecutionError = remote.getGlobal('setExecutionError'),
44
setWindowUnloading = remote.getGlobal('setWindowUnloading'),
55
setWindowIdName = remote.getGlobal('setWindowIdName'),
6+
getWindowNameFromId = remote.getGlobal('getWindowNameFromId'),
67
setFileFromScript = remote.getGlobal('setFileFromScript'),
78
DELAY_SCRIPT_RESPONSE = remote.getGlobal('DELAY_SCRIPT_RESPONSE'),
89
electronWindow = remote.getCurrentWindow();
@@ -21,15 +22,13 @@
2122
setWindowIdName(electronWindow.id, null, location.href);
2223
});
2324

24-
let oldWndName = window.name || remote.getGlobal('newWindowName');
25+
setWindowIdName(electronWindow.id, (window.name || remote.getGlobal('newWindowName')), location.href);
2526
window.__defineSetter__("name", function (name) {
26-
oldWndName = name;
2727
setWindowIdName(electronWindow.id, name, location.href);
2828
});
2929
window.__defineGetter__("name", function () {
30-
return oldWndName;
30+
return getWindowNameFromId(electronWindow.id);
3131
});
32-
setWindowIdName(electronWindow.id, oldWndName, location.href);
3332

3433
window.Electron = {
3534
'syn': require('syn'),

Diff for: src/Server/Server.js

+41-11
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Electron.app.on('ready', function() {
112112
cookieResponse = null,
113113
screenshotResponse = null,
114114
windowWillUnload = false,
115+
/** @type {Object.<string, string>} */
115116
windowIdNameMap = {},
116117
captureResponse = false,
117118
bindServerOnce;
@@ -125,6 +126,7 @@ Electron.app.on('ready', function() {
125126
*/
126127
global.setExecutionError = function (error) {
127128
Logger.error('Script evaluation failed internally: %s', errorToString(error));
129+
128130
executeResponse = {'error': errorToString(error)};
129131
};
130132

@@ -147,26 +149,36 @@ Electron.app.on('ready', function() {
147149

148150
/**
149151
*
150-
* @param {integer} id
151-
* @param {string} name
152-
* @param {string} url
152+
* @param {Number} id
153+
* @param {String} name
154+
* @param {String} url
153155
*/
154156
global.setWindowIdName = function (id, name, url) {
155157
const sId = id === null ? "" : id.toString();
156158

157159
if (name === '') {
158-
name = 'frame-' + sId;
160+
name = 'electron_window_' + sId;
159161
}
160162

161163
if (name === null) {
162-
Logger.info('Unlinked window named "%s" from id "%s" for %s.', name, sId, url);
164+
Logger.info('Unlinked window named %j from id %j for %j.', name, sId, url);
163165
if (windowIdNameMap[sId]) delete windowIdNameMap[sId];
164166
} else {
165-
Logger.info('Linked window named "%s" with id "%s" for %s.', name, sId, url);
167+
Logger.info('Linked window named %j with id %j for %j.', name, sId, url);
166168
windowIdNameMap[sId] = name;
167169
}
168170
};
169171

172+
/**
173+
*
174+
* @param {Number} id
175+
*/
176+
global.getWindowNameFromId = function (id) {
177+
const sId = id === null ? "" : id.toString();
178+
179+
return windowIdNameMap[sId];
180+
};
181+
170182
/**
171183
* Finds window by its window name. Note that this depends on the windows successfully registering it's id and name
172184
* when created. Since we keep these details in a hash map, we need to be careful about keeping it up to date.
@@ -181,7 +193,7 @@ Electron.app.on('ready', function() {
181193
}
182194

183195
for (let id in windowIdNameMap) {
184-
if (windowIdNameMap[id] === name) {
196+
if (windowIdNameMap.hasOwnProperty(id) && windowIdNameMap[id] === name) {
185197
const wnd = BrowserWindow.fromId(parseInt(id));
186198
if (wnd) result.push(wnd);
187199
}
@@ -339,6 +351,25 @@ Electron.app.on('ready', function() {
339351
* @param {Electron.BrowserWindow} window
340352
*/
341353
function (event, window) {
354+
const windowId = window.id;
355+
356+
Logger.debug('Browser window created with id %j.', windowId);
357+
358+
window
359+
.on('closed', function () { // important: we can't use window anymore in here!
360+
if (windowId) {
361+
Logger.info('Window "%s" (id %d) has been closed.', windowIdNameMap[windowId.toString()] || '', windowId);
362+
363+
pageVisited = true;
364+
captureResponse = false;
365+
delete windowIdNameMap[windowId.toString()];
366+
ResponseManager.remove(windowId);
367+
} else {
368+
Logger.warn('Browser window with id %j was closed.', windowId);
369+
}
370+
})
371+
;
372+
342373
window.webContents
343374
.on('login', function (event, request, authInfo, callback) {
344375
if (auth.user !== false) {
@@ -352,10 +383,9 @@ Electron.app.on('ready', function() {
352383
global.newWindowName = frameName;
353384
setupWindowOptions(options);
354385
})
355-
.on('closed', function () {
356-
Logger.info('Window "%s" (id %d) has been closed.', windowIdNameMap[window.id] || '', window.id);
357-
delete windowIdNameMap[window.id];
358-
ResponseManager.remove(window.id);
386+
.on('will-navigate', function (event, url) {
387+
Logger.debug('Event "will-navigate" triggered for url %j.', url);
388+
global.setWindowUnloading(true);
359389
})
360390
.on('did-finish-load', function () {
361391
if (bindServerOnce) {

Diff for: tests/ElectronConfig.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class ElectronConfig extends AbstractConfig
1919

2020
protected function __construct()
2121
{
22-
$this->logger = new ElectronFileLogger(__DIR__ . '/../tmp/output.log');
22+
$this->logger = new ElectronFileLogger(__DIR__ . '/../tmp/output.log', false);
2323
}
2424

2525
/**

Diff for: tests/WebDriverTest.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,22 @@ public function testWindowMaximize()
233233
);
234234
}
235235

236-
public function testOpeningNewWindow()
236+
public function testNewWindow()
237237
{
238-
$this->assertEquals(['frame-1'], $this->driver->getWindowNames());
238+
$this->assertEquals(['electron_window_1'], $this->driver->getWindowNames());
239239

240240
$this->assertSame(9, $this->driver->evaluateScript('4 + 5'));
241241

242242
$this->driver->executeScript('window.open();');
243-
$this->assertEquals(['frame-1', 'frame-2'], $this->driver->getWindowNames());
243+
$this->assertEquals(['electron_window_1', 'electron_window_2'], $this->driver->getWindowNames());
244+
245+
$this->driver->switchToWindow('electron_window_2');
246+
$this->assertEquals('electron_window_2', $this->driver->getWindowName());
247+
248+
$this->driver->visit('http://google.com');
249+
$this->assertEquals('electron_window_2', $this->driver->getWindowName());
250+
251+
$this->driver->executeScript('window.close();');
252+
$this->assertEquals(['electron_window_1'], $this->driver->getWindowNames());
244253
}
245254
}

0 commit comments

Comments
 (0)