refactor: implement ipcRenderer.sendTo in native code for better performance (#14285)
This commit is contained in:
parent
e6e3ccfc50
commit
c23e7fa101
7 changed files with 54 additions and 25 deletions
|
@ -1019,6 +1019,7 @@ bool WebContents::OnMessageReceived(const IPC::Message& message,
|
||||||
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_Message, OnRendererMessage)
|
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_Message, OnRendererMessage)
|
||||||
IPC_MESSAGE_FORWARD_DELAY_REPLY(AtomFrameHostMsg_Message_Sync, &helper,
|
IPC_MESSAGE_FORWARD_DELAY_REPLY(AtomFrameHostMsg_Message_Sync, &helper,
|
||||||
FrameDispatchHelper::OnRendererMessageSync)
|
FrameDispatchHelper::OnRendererMessageSync)
|
||||||
|
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_Message_To, OnRendererMessageTo)
|
||||||
IPC_MESSAGE_FORWARD_DELAY_REPLY(
|
IPC_MESSAGE_FORWARD_DELAY_REPLY(
|
||||||
AtomFrameHostMsg_SetTemporaryZoomLevel, &helper,
|
AtomFrameHostMsg_SetTemporaryZoomLevel, &helper,
|
||||||
FrameDispatchHelper::OnSetTemporaryZoomLevel)
|
FrameDispatchHelper::OnSetTemporaryZoomLevel)
|
||||||
|
@ -2077,6 +2078,19 @@ void WebContents::OnRendererMessageSync(content::RenderFrameHost* frame_host,
|
||||||
EmitWithSender(channel, frame_host, message, args);
|
EmitWithSender(channel, frame_host, message, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
|
||||||
|
bool send_to_all,
|
||||||
|
int32_t web_contents_id,
|
||||||
|
const base::string16& channel,
|
||||||
|
const base::ListValue& args) {
|
||||||
|
auto* web_contents = mate::TrackableObject<WebContents>::FromWeakMapID(
|
||||||
|
isolate(), web_contents_id);
|
||||||
|
|
||||||
|
if (web_contents) {
|
||||||
|
web_contents->SendIPCMessage(send_to_all, channel, args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// static
|
// static
|
||||||
mate::Handle<WebContents> WebContents::CreateFrom(
|
mate::Handle<WebContents> WebContents::CreateFrom(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
|
|
|
@ -419,6 +419,13 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
const base::ListValue& args,
|
const base::ListValue& args,
|
||||||
IPC::Message* message);
|
IPC::Message* message);
|
||||||
|
|
||||||
|
// Called when received a message from renderer to be forwarded.
|
||||||
|
void OnRendererMessageTo(content::RenderFrameHost* frame_host,
|
||||||
|
bool send_to_all,
|
||||||
|
int32_t web_contents_id,
|
||||||
|
const base::string16& channel,
|
||||||
|
const base::ListValue& args);
|
||||||
|
|
||||||
// Called when received a synchronous message from renderer to
|
// Called when received a synchronous message from renderer to
|
||||||
// set temporary zoom level.
|
// set temporary zoom level.
|
||||||
void OnSetTemporaryZoomLevel(content::RenderFrameHost* frame_host,
|
void OnSetTemporaryZoomLevel(content::RenderFrameHost* frame_host,
|
||||||
|
|
|
@ -33,6 +33,12 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomFrameHostMsg_Message_Sync,
|
||||||
base::ListValue /* arguments */,
|
base::ListValue /* arguments */,
|
||||||
base::ListValue /* result */)
|
base::ListValue /* result */)
|
||||||
|
|
||||||
|
IPC_MESSAGE_ROUTED4(AtomFrameHostMsg_Message_To,
|
||||||
|
bool /* send_to_all */,
|
||||||
|
int32_t /* web_contents_id */,
|
||||||
|
base::string16 /* channel */,
|
||||||
|
base::ListValue /* arguments */)
|
||||||
|
|
||||||
IPC_MESSAGE_ROUTED3(AtomFrameMsg_Message,
|
IPC_MESSAGE_ROUTED3(AtomFrameMsg_Message,
|
||||||
bool /* send_to_all */,
|
bool /* send_to_all */,
|
||||||
std::string /* channel */,
|
std::string /* channel */,
|
||||||
|
|
|
@ -60,6 +60,23 @@ base::ListValue SendSync(mate::Arguments* args,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendTo(mate::Arguments* args,
|
||||||
|
bool send_to_all,
|
||||||
|
int32_t web_contents_id,
|
||||||
|
const base::string16& channel,
|
||||||
|
const base::ListValue& arguments) {
|
||||||
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
||||||
|
if (render_frame == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
|
bool success = render_frame->Send(
|
||||||
|
new AtomFrameHostMsg_Message_To(render_frame->GetRoutingID(), send_to_all,
|
||||||
|
web_contents_id, channel, arguments));
|
||||||
|
|
||||||
|
if (!success)
|
||||||
|
args->ThrowError("Unable to send AtomFrameHostMsg_Message_To");
|
||||||
|
}
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
v8::Local<v8::Value> unused,
|
v8::Local<v8::Value> unused,
|
||||||
v8::Local<v8::Context> context,
|
v8::Local<v8::Context> context,
|
||||||
|
@ -67,6 +84,7 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||||
mate::Dictionary dict(context->GetIsolate(), exports);
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
||||||
dict.SetMethod("send", &Send);
|
dict.SetMethod("send", &Send);
|
||||||
dict.SetMethod("sendSync", &SendSync);
|
dict.SetMethod("sendSync", &SendSync);
|
||||||
|
dict.SetMethod("sendTo", &SendTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
|
@ -22,6 +22,12 @@ base::ListValue SendSync(mate::Arguments* args,
|
||||||
const std::string& channel,
|
const std::string& channel,
|
||||||
const base::ListValue& arguments);
|
const base::ListValue& arguments);
|
||||||
|
|
||||||
|
void SendTo(mate::Arguments* args,
|
||||||
|
bool send_to_all,
|
||||||
|
int32_t web_contents_id,
|
||||||
|
const base::string16& channel,
|
||||||
|
const base::ListValue& arguments);
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
v8::Local<v8::Value> unused,
|
v8::Local<v8::Value> unused,
|
||||||
v8::Local<v8::Context> context,
|
v8::Local<v8::Context> context,
|
||||||
|
|
|
@ -5,7 +5,7 @@ const {EventEmitter} = require('events')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const v8Util = process.atomBinding('v8_util')
|
const v8Util = process.atomBinding('v8_util')
|
||||||
|
|
||||||
const {ipcMain, isPromise, webContents} = electron
|
const {ipcMain, isPromise} = electron
|
||||||
|
|
||||||
const objectsRegistry = require('./objects-registry')
|
const objectsRegistry = require('./objects-registry')
|
||||||
const bufferUtils = require('../common/buffer-utils')
|
const bufferUtils = require('../common/buffer-utils')
|
||||||
|
@ -417,20 +417,6 @@ ipcMain.on('ELECTRON_BROWSER_ASYNC_CALL_TO_GUEST_VIEW', function (event, context
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
ipcMain.on('ELECTRON_BROWSER_SEND_TO', function (event, sendToAll, webContentsId, channel, ...args) {
|
|
||||||
let contents = webContents.fromId(webContentsId)
|
|
||||||
if (!contents) {
|
|
||||||
console.error(`Sending message to WebContents with unknown ID ${webContentsId}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sendToAll) {
|
|
||||||
contents.sendToAll(channel, ...args)
|
|
||||||
} else {
|
|
||||||
contents.send(channel, ...args)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// Implements window.close()
|
// Implements window.close()
|
||||||
ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
ipcMain.on('ELECTRON_BROWSER_WINDOW_CLOSE', function (event) {
|
||||||
const window = event.sender.getOwnerBrowserWindow()
|
const window = event.sender.getOwnerBrowserWindow()
|
||||||
|
|
|
@ -19,19 +19,11 @@ ipcRenderer.sendToHost = function (...args) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
|
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {
|
||||||
if (typeof webContentsId !== 'number') {
|
return binding.sendTo(false, webContentsId, channel, args)
|
||||||
throw new TypeError('First argument has to be webContentsId')
|
|
||||||
}
|
|
||||||
|
|
||||||
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', false, webContentsId, channel, ...args)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
|
ipcRenderer.sendToAll = function (webContentsId, channel, ...args) {
|
||||||
if (typeof webContentsId !== 'number') {
|
return binding.sendTo(true, webContentsId, channel, args)
|
||||||
throw new TypeError('First argument has to be webContentsId')
|
|
||||||
}
|
|
||||||
|
|
||||||
ipcRenderer.send('ELECTRON_BROWSER_SEND_TO', true, webContentsId, channel, ...args)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeAllListeners = ipcRenderer.removeAllListeners.bind(ipcRenderer)
|
const removeAllListeners = ipcRenderer.removeAllListeners.bind(ipcRenderer)
|
||||||
|
|
Loading…
Reference in a new issue