Skip to content
This repository has been archived by the owner on May 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #492 from HarsimranSingh/task
Browse files Browse the repository at this point in the history
Merge 9.61.32/staging into 9.61.33/staging
  • Loading branch information
HarsimranSingh authored Jul 24, 2018
2 parents d9b14d1 + 18ae6bc commit 0dbc966
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 108 deletions.
5 changes: 4 additions & 1 deletion src/browser/api/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,10 @@ Application.getInfo = function(identity, callback) {
launchMode,
manifestUrl: url,
manifest,
parentUuid
parentUuid,
runtime: {
version: System.getRuntimeInfo(identity).version
}
};

callback(response);
Expand Down
138 changes: 79 additions & 59 deletions src/browser/api/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ let browserWindowEventMap = {
},
'unmaximize': {
topic: 'restored'
},
'will-close': {
topic: 'closing'
}
// 'move': {
// topic: 'bounds-changing'
Expand Down Expand Up @@ -489,7 +486,7 @@ Window.create = function(id, opts) {
_externalWindowEventAdapter = new ExternalWindowEventAdapter(browserWindow);
}

let windowTeardown = createWindowTearDown(identity, id);
let windowTeardown = createWindowTearDown(identity, id, browserWindow, _boundsChangedHandler);

// once the window is closed, be sure to close all the children
// it may have and remove it from the
Expand Down Expand Up @@ -521,23 +518,19 @@ Window.create = function(id, opts) {
ofEvents.removeAllListeners(closeEventString);
});

browserWindow.on('closed', () => {
if (browserWindow._options.saveWindowState) {
let cachedBounds = _boundsChangedHandler.getCachedBounds();
saveBoundsToDisk(identity, cachedBounds, err => {
if (err) {
log.writeToLog('info', err);
}
windowTeardown();
// These were causing an exception on close if the window was reloaded
_boundsChangedHandler.teardown();
browserWindow.removeAllListeners();
browserWindow.once('will-close', () => {
const type = 'closing';
windowTeardown()
.then(() => log.writeToLog('info', `Window tear down complete ${uuid} ${name}`))
.catch(err => {
log.writeToLog('info', `Error while tearing down ${uuid} ${name}`);
log.writeToLog('info', err);
});
} else {
windowTeardown();
_boundsChangedHandler.teardown();
browserWindow.removeAllListeners();
}
ofEvents.emit(route.window(type, uuid, name), { topic: 'window', type: type, uuid, name });
});

webContents.once('close', () => {
webContents.removeAllListeners();
});

const isMainWindow = (uuid === name);
Expand All @@ -551,10 +544,6 @@ Window.create = function(id, opts) {
}
};

webContents.once('close', () => {
webContents.removeAllListeners();
});

webContents.on('crashed', (event, killed, terminationStatus) => {
emitToAppIfMainWin('crashed', {
reason: terminationStatus
Expand Down Expand Up @@ -1840,56 +1829,87 @@ function emitReloadedEvent(identity, url) {
});
}

function createWindowTearDown(identity, id) {
function createWindowTearDown(identity, id, browserWindow, _boundsChangedHandler) {
const promises = [];

//we want to treat the close events as a step in the teardown, wrapping it in a promise.
promises.push(new Promise(resolve => {
browserWindow.once('closed', resolve);
}));

//wrap the operation of closing a child window in a promise.
function closeChildWin(childId) {
return new Promise((resolve, reject) => {
const child = coreState.getWinObjById(childId);

// TODO right now this is forceable to handle the event that there was a close
// requested on a child window and the main window closes. This needs
// looking into
if (child) {
let childIdentity = {
name: child.name,
uuid: child.uuid
};

Window.close(childIdentity, true, () => {
resolve();
});
} else {
resolve();
}
});
}

//Even if disk operations fail we need to resolve this promise to avoid zombie processes.
function handleSaveStateAlwaysResolve() {
return new Promise((resolve, reject) => {
if (browserWindow._options.saveWindowState) {
const cachedBounds = _boundsChangedHandler.getCachedBounds();
saveBoundsToDisk(identity, cachedBounds, err => {
if (err) {
log.writeToLog('info', err);
}
// These were causing an exception on close if the window was reloaded
_boundsChangedHandler.teardown();
resolve();
});
} else {
_boundsChangedHandler.teardown();
resolve();
}
});
}

//Window tear down will:
// Update core state by removing the window.
// Save the window state to disk
// Close all child windows
// Wait for the close event.
return function() {
let ofWindow = Window.wrap(identity.uuid, identity.name);
let childWindows = coreState.getChildrenByWinId(id);

let childWindows = coreState.getChildrenByWinId(id) || [];
// remove from core state earlier rather than later
coreState.removeChildById(id);

// remove window from any groups it belongs to
WindowGroups.leaveGroup(ofWindow);

if (childWindows && childWindows.length > 0) {
let closedChildren = 0;
promises.push(handleSaveStateAlwaysResolve());

childWindows.forEach(childId => {
let child = coreState.getWinObjById(childId);

// TODO right now this is forceable to handle the event that there was a close
// requested on a child window and the main window closes. This needs
// looking into
if (child) {
let childIdentity = {
name: child.name,
uuid: child.uuid
};
childWindows.forEach(childId => {
promises.push(closeChildWin(childId));
});

Window.close(childIdentity, true, () => {
closedChildren++;
if (closedChildren === childWindows.length) {
emitCloseEvents(identity);
coreState.removeChildById(id);
}
});
} else {
closedChildren++;
if (closedChildren === childWindows.length) {
emitCloseEvents(identity);
coreState.removeChildById(id);
}
}
});
} else {
return Promise.all(promises).then(() => {
emitCloseEvents(identity);
}
browserWindow.removeAllListeners();
});
};
}

function saveBoundsToDisk(identity, bounds, callback) {
let cacheFile = getBoundsCacheSafeFileName(identity);
let data = {
const cacheFile = getBoundsCacheSafeFileName(identity);
const data = {
'active': 'true',
'height': bounds.height,
'width': bounds.width,
Expand Down
14 changes: 13 additions & 1 deletion src/browser/api_protocol/api_handlers/mesh_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as log from '../../log';
import { default as connectionManager } from '../../connection_manager';
import ofEvents from '../../of_events';
import { isLocalUuid } from '../../core_state';
import { IdentityAddress } from '../../runtime_p2p/peer_connection_manager';

const SUBSCRIBE_ACTION = 'subscribe';
const PUBLISH_ACTION = 'publish-message';
Expand Down Expand Up @@ -122,8 +123,19 @@ function ferryActionMiddleware(msg: MessagePackage, next: () => void) {
if (isValidUuid && isForwardAction && isValidIdentity && isRemoteEntity && isLocalAction) {
try {
connectionManager.resolveIdentity({uuid})
.then((id: any) => {
.then((id: IdentityAddress) => {
id.runtime.fin.System.executeOnRemote(identity, data)
.then(res => {
switch (action) {
case 'get-info':
if (res && res.data && !res.data.runtime) {
Object.assign(res.data, {runtime: {version: id.runtime.portInfo.version}});
}
return res;
default:
return res;
}
})
.then(ack)
.catch(nack);
}).catch((e: Error) => {
Expand Down
94 changes: 51 additions & 43 deletions src/browser/core_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,19 +729,23 @@ export function getInfoByUuidFrame(targetIdentity: Shapes.Identity): Shapes.Fram
}

for (const { openfinWindow } of app.children) {
const { name } = openfinWindow;

if (name === frame) {
const parent = getParentIdentity({uuid, name});

return {
name,
uuid,
parent,
entityType: 'window'
};
} else if (openfinWindow.frames.get(frame)) {
return openfinWindow.frames.get(frame);
if (openfinWindow) {
const { name } = openfinWindow;

if (name === frame) {
const parent = getParentIdentity({uuid, name});

return {
name,
uuid,
parent,
entityType: 'window'
};
} else if (openfinWindow.frames.get(frame)) {
return openfinWindow.frames.get(frame);
}
} else {
writeToLog(1, `unable to find openfinWindow of child of ${app.uuid}`, true);
}
}
}
Expand All @@ -754,38 +758,42 @@ export function getRoutingInfoByUuidFrame(uuid: string, frame: string) {
}

for (const { openfinWindow } of app.children) {
const { uuid, name } = openfinWindow;
let browserWindow: Shapes.BrowserWindow;
browserWindow = openfinWindow.browserWindow;

if (!openfinWindow.mainFrameRoutingId) {
// save bit time here by not calling webContents.mainFrameRoutingId every time
// mainFrameRoutingId is wrong during setWindowObj
if (!browserWindow.isDestroyed()) {
openfinWindow.mainFrameRoutingId = browserWindow.webContents.mainFrameRoutingId;
writeToLog(1, `set mainFrameRoutingId ${uuid} ${name} ${openfinWindow.mainFrameRoutingId}`, true);
} else {
writeToLog(1, `unable to set mainFrameRoutingId ${uuid} ${name}`, true);
if (openfinWindow) {
const { uuid, name } = openfinWindow;
let browserWindow: Shapes.BrowserWindow;
browserWindow = openfinWindow.browserWindow;

if (!openfinWindow.mainFrameRoutingId) {
// save bit time here by not calling webContents.mainFrameRoutingId every time
// mainFrameRoutingId is wrong during setWindowObj
if (!browserWindow.isDestroyed()) {
openfinWindow.mainFrameRoutingId = browserWindow.webContents.mainFrameRoutingId;
writeToLog(1, `set mainFrameRoutingId ${uuid} ${name} ${openfinWindow.mainFrameRoutingId}`, true);
} else {
writeToLog(1, `unable to set mainFrameRoutingId ${uuid} ${name}`, true);
}
}
}

if (name === frame) {
return {
name,
browserWindow,
frameRoutingId: openfinWindow.mainFrameRoutingId,
mainFrameRoutingId: openfinWindow.mainFrameRoutingId,
frameName: name
};
} else if (openfinWindow.frames.get(frame)) {
const {name, frameRoutingId} = openfinWindow.frames.get(frame);
return {
name,
browserWindow,
frameRoutingId,
mainFrameRoutingId: openfinWindow.mainFrameRoutingId,
frameName: name
};
if (name === frame) {
return {
name,
browserWindow,
frameRoutingId: openfinWindow.mainFrameRoutingId,
mainFrameRoutingId: openfinWindow.mainFrameRoutingId,
frameName: name
};
} else if (openfinWindow.frames.get(frame)) {
const {name, frameRoutingId} = openfinWindow.frames.get(frame);
return {
name,
browserWindow,
frameRoutingId,
mainFrameRoutingId: openfinWindow.mainFrameRoutingId,
frameName: name
};
}
} else {
writeToLog(1, `unable to find openfinWindow of child of ${app.uuid}`, true);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/browser/subscription_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default class SubscriptionManager {
constructor() {
this.subscriptionList = new Map();

ofEvents.on(route.window('closed', '*'), (identity: Identity) => {
ofEvents.on(route.window('closed', '*'), (event: any) => {
const identity: Identity = event.data[0];
this.removeAllSubscriptions(identity);
});

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/api-decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@

// main window
if (uuid === name) {
eventMap.push([`application/initialized/${uuid}`, undefined]);
eventMap.push([`application/initialized/${uuid}`, { uuid }]);
}

eventMap.push([`window/dom-content-loaded/${uuid}-${name}`, winIdentity]);
Expand Down
4 changes: 2 additions & 2 deletions test/global_hotkey.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('GlobalHotkey', () => {

GlobalHotkey.register(identity, hotkey, spy);
//we simulate a window close.
ofEvents.emit(route.window('closed', '*'), identity);
ofEvents.emit(route.window('closed', identity.uuid, identity.name), identity);
const isRegistered = GlobalHotkey.isRegistered(hotkey);
assert.deepStrictEqual(isRegistered, false, 'Expected hotkey to not be registered');
});
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('GlobalHotkey', () => {
GlobalHotkey.register(identity2, hotkey, spy2);

//we simulate a window close.
ofEvents.emit(route.window('closed', '*'), identity2);
ofEvents.emit(route.window('closed', identity2.uuid, identity2.name), identity2);

mockElectron.globalShortcut.mockRaiseEvent(hotkey);
assert.ok(spy.calledOnce, 'Expected the global shortcut to be called');
Expand Down

0 comments on commit 0dbc966

Please sign in to comment.