track async api requests
This commit is contained in:
parent
2b547bd44a
commit
a734326907
8 changed files with 39 additions and 39 deletions
|
@ -11,6 +11,8 @@ const debuggerBinding = process.atomBinding('debugger');
|
||||||
|
|
||||||
let slice = [].slice;
|
let slice = [].slice;
|
||||||
let nextId = 0;
|
let nextId = 0;
|
||||||
|
|
||||||
|
// Map of requestId and response callback.
|
||||||
let responseCallback = {};
|
let responseCallback = {};
|
||||||
|
|
||||||
let getNextId = function() {
|
let getNextId = function() {
|
||||||
|
@ -59,13 +61,16 @@ let PDFPageSize = {
|
||||||
|
|
||||||
// Following methods are mapped to webFrame.
|
// Following methods are mapped to webFrame.
|
||||||
const webFrameMethods = [
|
const webFrameMethods = [
|
||||||
'executeJavaScript',
|
|
||||||
'insertText',
|
'insertText',
|
||||||
'setZoomFactor',
|
'setZoomFactor',
|
||||||
'setZoomLevel',
|
'setZoomLevel',
|
||||||
'setZoomLevelLimits'
|
'setZoomLevelLimits'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const asyncWebFrameMethods = [
|
||||||
|
'executeJavaScript',
|
||||||
|
];
|
||||||
|
|
||||||
let wrapWebContents = function(webContents) {
|
let wrapWebContents = function(webContents) {
|
||||||
// webContents is an EventEmitter.
|
// webContents is an EventEmitter.
|
||||||
var controller, method, name, ref1;
|
var controller, method, name, ref1;
|
||||||
|
@ -107,25 +112,35 @@ let wrapWebContents = function(webContents) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (let method of asyncWebFrameMethods) {
|
||||||
|
webContents[method] = function() {
|
||||||
|
let args = Array.prototype.slice.call(arguments);
|
||||||
|
this.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', method, args);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure webContents.executeJavaScript would run the code only when the
|
// Make sure webContents.executeJavaScript would run the code only when the
|
||||||
// webContents has been loaded.
|
// webContents has been loaded.
|
||||||
const executeJavaScript = webContents.executeJavaScript;
|
const executeJavaScript = webContents.executeJavaScript;
|
||||||
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
|
webContents.executeJavaScript = function(code, hasUserGesture, callback) {
|
||||||
|
let requestId = getNextId();
|
||||||
if (typeof hasUserGesture === "function") {
|
if (typeof hasUserGesture === "function") {
|
||||||
callback = hasUserGesture;
|
callback = hasUserGesture;
|
||||||
hasUserGesture = false;
|
hasUserGesture = false;
|
||||||
}
|
}
|
||||||
if (callback !== null)
|
if (callback !== null)
|
||||||
responseCallback["executeJavaScript"] = callback;
|
responseCallback[requestId] = callback;
|
||||||
if (this.getURL() && !this.isLoading())
|
if (this.getURL() && !this.isLoading())
|
||||||
return executeJavaScript.call(this, code, hasUserGesture);
|
return executeJavaScript.call(this, requestId, code, hasUserGesture);
|
||||||
else
|
else
|
||||||
return this.once('did-finish-load', executeJavaScript.bind(this, code, hasUserGesture));
|
return this.once('did-finish-load', executeJavaScript.bind(this, requestId, code, hasUserGesture));
|
||||||
};
|
};
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_RESPONSE', function(event, method, result) {
|
ipcMain.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', function(event, id, result) {
|
||||||
if (responseCallback[method])
|
if (responseCallback[id]) {
|
||||||
responseCallback[method].apply(null, [result]);
|
responseCallback[id].apply(null, [result]);
|
||||||
|
delete responseCallback[id];
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Dispatch IPC messages to the ipc module.
|
// Dispatch IPC messages to the ipc module.
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
|
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
|
||||||
|
|
||||||
#include "native_mate/converter.h"
|
#include "native_mate/converter.h"
|
||||||
#include "third_party/WebKit/public/platform/WebVector.h"
|
|
||||||
|
|
||||||
namespace blink {
|
namespace blink {
|
||||||
class WebInputEvent;
|
class WebInputEvent;
|
||||||
|
@ -88,19 +87,6 @@ struct Converter<blink::WebFindOptions> {
|
||||||
blink::WebFindOptions* out);
|
blink::WebFindOptions* out);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
struct Converter<blink::WebVector<T> > {
|
|
||||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
|
||||||
const blink::WebVector<T>& val) {
|
|
||||||
v8::Local<v8::Array> result(
|
|
||||||
MATE_ARRAY_NEW(isolate, static_cast<int>(val.size())));
|
|
||||||
for (size_t i = 0; i < val.size(); ++i) {
|
|
||||||
result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace mate
|
} // namespace mate
|
||||||
|
|
||||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
|
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_BLINK_CONVERTER_H_
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include "atom/common/native_mate_converters/callback.h"
|
#include "atom/common/native_mate_converters/callback.h"
|
||||||
#include "atom/common/native_mate_converters/gfx_converter.h"
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
#include "atom/common/native_mate_converters/blink_converter.h"
|
|
||||||
#include "atom/renderer/api/atom_api_spell_check_client.h"
|
#include "atom/renderer/api/atom_api_spell_check_client.h"
|
||||||
#include "content/public/renderer/render_frame.h"
|
#include "content/public/renderer/render_frame.h"
|
||||||
#include "content/public/renderer/render_view.h"
|
#include "content/public/renderer/render_view.h"
|
||||||
|
@ -33,7 +32,7 @@ class ScriptExecutionCallback : public blink::WebScriptExecutionCallback {
|
||||||
public:
|
public:
|
||||||
using CompletionCallback =
|
using CompletionCallback =
|
||||||
base::Callback<void(
|
base::Callback<void(
|
||||||
const blink::WebVector<v8::Local<v8::Value>>& result)>;
|
const v8::Local<v8::Value>& result)>;
|
||||||
|
|
||||||
explicit ScriptExecutionCallback(const CompletionCallback& callback)
|
explicit ScriptExecutionCallback(const CompletionCallback& callback)
|
||||||
: callback_(callback) {}
|
: callback_(callback) {}
|
||||||
|
@ -42,7 +41,8 @@ class ScriptExecutionCallback : public blink::WebScriptExecutionCallback {
|
||||||
void completed(
|
void completed(
|
||||||
const blink::WebVector<v8::Local<v8::Value>>& result) override {
|
const blink::WebVector<v8::Local<v8::Value>>& result) override {
|
||||||
if (!callback_.is_null())
|
if (!callback_.is_null())
|
||||||
callback_.Run(result);
|
// Right now only single results per frame is supported.
|
||||||
|
callback_.Run(result[0]);
|
||||||
delete this;
|
delete this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,17 +32,16 @@ v8Util.setHiddenValue(global, 'ipc', new events.EventEmitter);
|
||||||
const electron = require('electron');
|
const electron = require('electron');
|
||||||
|
|
||||||
// Call webFrame method.
|
// Call webFrame method.
|
||||||
const asyncWebFrameMethods = [
|
|
||||||
'executeJavaScript'
|
|
||||||
];
|
|
||||||
|
|
||||||
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
|
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_METHOD', (event, method, args) => {
|
||||||
if (asyncWebFrameMethods.includes(method)) {
|
electron.webFrame[method].apply(electron.webFrame, args);
|
||||||
|
});
|
||||||
|
|
||||||
|
electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (event, method, args) => {
|
||||||
|
let requestId = args.shift();
|
||||||
const responseCallback = function(result) {
|
const responseCallback = function(result) {
|
||||||
event.sender.send('ELECTRON_INTERNAL_RENDERER_WEB_FRAME_RESPONSE', method, result);
|
event.sender.send('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_RESPONSE', requestId, result);
|
||||||
};
|
};
|
||||||
args.push(responseCallback);
|
args.push(responseCallback);
|
||||||
}
|
|
||||||
electron.webFrame[method].apply(electron.webFrame, args);
|
electron.webFrame[method].apply(electron.webFrame, args);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -393,14 +393,14 @@ var registerWebViewElement = function() {
|
||||||
nonblockMethods = [
|
nonblockMethods = [
|
||||||
'insertCSS',
|
'insertCSS',
|
||||||
'send',
|
'send',
|
||||||
'sendInputEvent'
|
'sendInputEvent',
|
||||||
];
|
];
|
||||||
webFrameMethods = [
|
webFrameMethods = [
|
||||||
'executeJavaScript',
|
'executeJavaScript',
|
||||||
'insertText',
|
'insertText',
|
||||||
'setZoomFactor',
|
'setZoomFactor',
|
||||||
'setZoomLevel',
|
'setZoomLevel',
|
||||||
'setZoomLevelLimits'
|
'setZoomLevelLimits',
|
||||||
];
|
];
|
||||||
|
|
||||||
// Forward proto.foo* method calls to WebViewImpl.foo*.
|
// Forward proto.foo* method calls to WebViewImpl.foo*.
|
||||||
|
|
|
@ -430,7 +430,7 @@ Injects CSS into the current web page.
|
||||||
* `code` String
|
* `code` String
|
||||||
* `userGesture` Boolean (optional)
|
* `userGesture` Boolean (optional)
|
||||||
* `callback` Function (optional) - Called after script has been executed.
|
* `callback` Function (optional) - Called after script has been executed.
|
||||||
* `result` Array
|
* `result`
|
||||||
|
|
||||||
Evaluates `code` in page.
|
Evaluates `code` in page.
|
||||||
|
|
||||||
|
|
|
@ -284,7 +284,7 @@ Injects CSS into the guest page.
|
||||||
* `code` String
|
* `code` String
|
||||||
* `userGesture` Boolean - Default `false`.
|
* `userGesture` Boolean - Default `false`.
|
||||||
* `callback` Function (optional) - Called after script has been executed.
|
* `callback` Function (optional) - Called after script has been executed.
|
||||||
* `result` Array
|
* `result`
|
||||||
|
|
||||||
Evaluates `code` in page. If `userGesture` is set, it will create the user
|
Evaluates `code` in page. If `userGesture` is set, it will create the user
|
||||||
gesture context in the page. HTML APIs like `requestFullScreen`, which require
|
gesture context in the page. HTML APIs like `requestFullScreen`, which require
|
||||||
|
|
|
@ -572,7 +572,7 @@ describe('<webview> tag', function() {
|
||||||
var listener = function() {
|
var listener = function() {
|
||||||
var jsScript = "'4'+2";
|
var jsScript = "'4'+2";
|
||||||
webview.executeJavaScript(jsScript, false, function(result) {
|
webview.executeJavaScript(jsScript, false, function(result) {
|
||||||
assert.equal(result[0], '42');
|
assert.equal(result, '42');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
webview.removeEventListener('did-finish-load', listener);
|
webview.removeEventListener('did-finish-load', listener);
|
||||||
|
|
Loading…
Add table
Reference in a new issue