Merge pull request #4522 from deepak1556/async_execute_javascript_patch

webContents: provide responses for executeJavscript method
This commit is contained in:
Cheng Zhao 2016-02-26 21:17:47 +08:00
commit ebfc127628
10 changed files with 137 additions and 16 deletions

View file

@ -58,7 +58,6 @@ let PDFPageSize = {
// Following methods are mapped to webFrame.
const webFrameMethods = [
'executeJavaScript',
'insertText',
'setZoomFactor',
'setZoomLevel',
@ -106,14 +105,26 @@ let wrapWebContents = function(webContents) {
};
}
const asyncWebFrameMethods = function(requestId, method, callback, ...args) {
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', requestId, method, args);
ipcMain.once(`ELECTRON_INTERNAL_BROWSER_ASYNC_WEB_FRAME_RESPONSE_${requestId}`, function(event, result) {
if (callback)
callback(result);
});
};
// Make sure webContents.executeJavaScript would run the code only when the
// webContents has been loaded.
const executeJavaScript = webContents.executeJavaScript;
webContents.executeJavaScript = function(code, hasUserGesture) {
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
let requestId = getNextId();
if (typeof hasUserGesture === "function") {
callback = hasUserGesture;
hasUserGesture = false;
}
if (this.getURL() && !this.isLoading())
return executeJavaScript.call(this, code, hasUserGesture);
return asyncWebFrameMethods.call(this, requestId, "executeJavaScript", callback, code, hasUserGesture);
else
return this.once('did-finish-load', executeJavaScript.bind(this, code, hasUserGesture));
return this.once('did-finish-load', asyncWebFrameMethods.bind(this, requestId, "executeJavaScript", callback, code, hasUserGesture));
};
// Dispatch IPC messages to the ipc module.

View file

@ -354,11 +354,17 @@ ipcMain.on('ATOM_BROWSER_GUEST_WEB_CONTENTS', function(event, guestInstanceId) {
}
});
ipcMain.on('ATOM_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function(event, guestInstanceId, method, ...args) {
ipcMain.on('ATOM_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function(event, requestId, guestInstanceId, method, ...args) {
try {
let guestViewManager = require('./guest-view-manager');
let guest = guestViewManager.getGuest(guestInstanceId);
return guest[method].apply(guest, args);
if (requestId) {
const responseCallback = function(result) {
event.sender.send(`ATOM_RENDERER_ASYNC_CALL_TO_GUEST_VIEW_RESPONSE_${requestId}`, result);
};
args.push(responseCallback);
}
guest[method].apply(guest, args);
} catch (error) {
return event.returnValue = exceptionToMeta(error);
}