refactor: pass internal flag via IPC message struct for consistency (#16490)

This commit is contained in:
Milan Burda 2019-01-23 17:24:57 +01:00 committed by Shelley Vohr
parent 0a5adfe365
commit cc90919384
12 changed files with 103 additions and 65 deletions

View file

@ -280,10 +280,12 @@ struct WebContents::FrameDispatchHelper {
api_web_contents->OnGetZoomLevel(rfh, reply_msg);
}
void OnRendererMessageSync(const std::string& channel,
void OnRendererMessageSync(bool internal,
const std::string& channel,
const base::ListValue& args,
IPC::Message* message) {
api_web_contents->OnRendererMessageSync(rfh, channel, args, message);
api_web_contents->OnRendererMessageSync(rfh, internal, channel, args,
message);
}
};
@ -1070,6 +1072,7 @@ bool WebContents::OnMessageReceived(const IPC::Message& message,
IPC_MESSAGE_FORWARD_DELAY_REPLY(AtomFrameHostMsg_Message_Sync, &helper,
FrameDispatchHelper::OnRendererMessageSync)
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_Message_To, OnRendererMessageTo)
IPC_MESSAGE_HANDLER(AtomFrameHostMsg_Message_Host, OnRendererMessageHost)
IPC_MESSAGE_FORWARD_DELAY_REPLY(
AtomFrameHostMsg_SetTemporaryZoomLevel, &helper,
FrameDispatchHelper::OnSetTemporaryZoomLevel)
@ -2206,18 +2209,22 @@ AtomBrowserContext* WebContents::GetBrowserContext() const {
}
void WebContents::OnRendererMessage(content::RenderFrameHost* frame_host,
bool internal,
const std::string& channel,
const base::ListValue& args) {
// webContents.emit(channel, new Event(), args...);
EmitWithSender(channel, frame_host, nullptr, args);
// webContents.emit('-ipc-message', new Event(), internal, channel, args);
EmitWithSender("-ipc-message", frame_host, nullptr, internal, channel, args);
}
void WebContents::OnRendererMessageSync(content::RenderFrameHost* frame_host,
bool internal,
const std::string& channel,
const base::ListValue& args,
IPC::Message* message) {
// webContents.emit(channel, new Event(sender, message), args...);
EmitWithSender(channel, frame_host, message, args);
// webContents.emit('-ipc-message-sync', new Event(sender, message), internal,
// channel, args);
EmitWithSender("-ipc-message-sync", frame_host, message, internal, channel,
args);
}
void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
@ -2235,6 +2242,13 @@ void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
}
}
void WebContents::OnRendererMessageHost(content::RenderFrameHost* frame_host,
const std::string& channel,
const base::ListValue& args) {
// webContents.emit('ipc-message-host', new Event(), channel, args);
EmitWithSender("ipc-message-host", frame_host, nullptr, channel, args);
}
// static
mate::Handle<WebContents> WebContents::Create(v8::Isolate* isolate,
const mate::Dictionary& options) {

View file

@ -480,11 +480,13 @@ class WebContents : public mate::TrackableObject<WebContents>,
// Called when received a message from renderer.
void OnRendererMessage(content::RenderFrameHost* frame_host,
bool internal,
const std::string& channel,
const base::ListValue& args);
// Called when received a synchronous message from renderer.
void OnRendererMessageSync(content::RenderFrameHost* frame_host,
bool internal,
const std::string& channel,
const base::ListValue& args,
IPC::Message* message);
@ -497,6 +499,11 @@ class WebContents : public mate::TrackableObject<WebContents>,
const std::string& channel,
const base::ListValue& args);
// Called when received a message from renderer to host.
void OnRendererMessageHost(content::RenderFrameHost* frame_host,
const std::string& channel,
const base::ListValue& args);
// Called when received a synchronous message from renderer to
// set temporary zoom level.
void OnSetTemporaryZoomLevel(content::RenderFrameHost* frame_host,

View file

@ -25,11 +25,13 @@ IPC_STRUCT_TRAITS_BEGIN(atom::DraggableRegion)
IPC_STRUCT_TRAITS_MEMBER(bounds)
IPC_STRUCT_TRAITS_END()
IPC_MESSAGE_ROUTED2(AtomFrameHostMsg_Message,
IPC_MESSAGE_ROUTED3(AtomFrameHostMsg_Message,
bool /* internal */,
std::string /* channel */,
base::ListValue /* arguments */)
IPC_SYNC_MESSAGE_ROUTED2_1(AtomFrameHostMsg_Message_Sync,
IPC_SYNC_MESSAGE_ROUTED3_1(AtomFrameHostMsg_Message_Sync,
bool /* internal */,
std::string /* channel */,
base::ListValue /* arguments */,
base::ListValue /* result */)
@ -41,6 +43,10 @@ IPC_MESSAGE_ROUTED5(AtomFrameHostMsg_Message_To,
std::string /* channel */,
base::ListValue /* arguments */)
IPC_MESSAGE_ROUTED2(AtomFrameHostMsg_Message_Host,
std::string /* channel */,
base::ListValue /* arguments */)
IPC_MESSAGE_ROUTED5(AtomFrameMsg_Message,
bool /* internal */,
bool /* send_to_all */,

View file

@ -56,13 +56,12 @@ void RemoteObjectFreer::RunDestructor() {
if (!render_frame)
return;
auto* channel = "ipc-internal-message";
auto* channel = "ELECTRON_BROWSER_DEREFERENCE";
base::ListValue args;
args.AppendString("ELECTRON_BROWSER_DEREFERENCE");
args.AppendString(context_id_);
args.AppendInteger(object_id_);
render_frame->Send(new AtomFrameHostMsg_Message(render_frame->GetRoutingID(),
channel, args));
true, channel, args));
}
} // namespace atom

View file

@ -5,7 +5,6 @@
#include <string>
#include "atom/common/api/api_messages.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_bindings.h"
#include "atom/common/node_includes.h"
@ -29,6 +28,7 @@ RenderFrame* GetCurrentRenderFrame() {
}
void Send(mate::Arguments* args,
bool internal,
const std::string& channel,
const base::ListValue& arguments) {
RenderFrame* render_frame = GetCurrentRenderFrame();
@ -36,13 +36,14 @@ void Send(mate::Arguments* args,
return;
bool success = render_frame->Send(new AtomFrameHostMsg_Message(
render_frame->GetRoutingID(), channel, arguments));
render_frame->GetRoutingID(), internal, channel, arguments));
if (!success)
args->ThrowError("Unable to send AtomFrameHostMsg_Message");
}
base::ListValue SendSync(mate::Arguments* args,
bool internal,
const std::string& channel,
const base::ListValue& arguments) {
base::ListValue result;
@ -52,7 +53,7 @@ base::ListValue SendSync(mate::Arguments* args,
return result;
IPC::SyncMessage* message = new AtomFrameHostMsg_Message_Sync(
render_frame->GetRoutingID(), channel, arguments, &result);
render_frame->GetRoutingID(), internal, channel, arguments, &result);
bool success = render_frame->Send(message);
if (!success)
@ -79,6 +80,20 @@ void SendTo(mate::Arguments* args,
args->ThrowError("Unable to send AtomFrameHostMsg_Message_To");
}
void SendToHost(mate::Arguments* args,
const std::string& channel,
const base::ListValue& arguments) {
RenderFrame* render_frame = GetCurrentRenderFrame();
if (render_frame == nullptr)
return;
bool success = render_frame->Send(new AtomFrameHostMsg_Message_Host(
render_frame->GetRoutingID(), channel, arguments));
if (!success)
args->ThrowError("Unable to send AtomFrameHostMsg_Message_Host");
}
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
@ -87,6 +102,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("send", &Send);
dict.SetMethod("sendSync", &SendSync);
dict.SetMethod("sendTo", &SendTo);
dict.SetMethod("sendToHost", &SendToHost);
}
} // namespace

View file

@ -212,11 +212,10 @@ void AtomRenderFrameObserver::OnTakeHeapSnapshot(
bool success = TakeHeapSnapshot(blink::MainThreadIsolate(), &file);
base::ListValue args;
args.AppendString(channel);
args.AppendBoolean(success);
render_frame_->Send(new AtomFrameHostMsg_Message(
render_frame_->GetRoutingID(), "ipc-internal-message", args));
render_frame_->GetRoutingID(), true, channel, args));
}
void AtomRenderFrameObserver::EmitIPCEvent(blink::WebLocalFrame* frame,

View file

@ -114,11 +114,12 @@ class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
auto context = renderer_client_->GetContext(frame, isolate);
v8::Context::Scope context_scope(context);
v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, channel),
v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, internal),
mate::ConvertToV8(isolate, channel),
mate::ConvertToV8(isolate, args),
mate::ConvertToV8(isolate, sender_id)};
renderer_client_->InvokeIpcCallback(
context, internal ? "onInternalMessage" : "onMessage",
context, "onMessage",
std::vector<v8::Local<v8::Value>>(argv, argv + node::arraysize(argv)));
}

View file

@ -358,6 +358,13 @@ const addReplyInternalToEvent = (event) => {
})
}
const addReturnValueToEvent = (event) => {
Object.defineProperty(event, 'returnValue', {
set: (value) => event.sendReply([value]),
get: () => {}
})
}
// Add JavaScript wrappers for WebContents class.
WebContents.prototype._init = function () {
// The navigation controller.
@ -370,38 +377,27 @@ WebContents.prototype._init = function () {
this.capturePage = deprecate.promisify(this.capturePage, 2)
// Dispatch IPC messages to the ipc module.
this.on('-ipc-message', function (event, [channel, ...args]) {
addReplyToEvent(event)
this.emit('ipc-message', event, channel, ...args)
ipcMain.emit(channel, event, ...args)
this.on('-ipc-message', function (event, internal, channel, args) {
if (internal) {
addReplyInternalToEvent(event)
ipcMainInternal.emit(channel, event, ...args)
} else {
addReplyToEvent(event)
this.emit('ipc-message', event, channel, ...args)
ipcMain.emit(channel, event, ...args)
}
})
this.on('-ipc-message-sync', function (event, [channel, ...args]) {
Object.defineProperty(event, 'returnValue', {
set: function (value) {
return event.sendReply([value])
},
get: function () {}
})
addReplyToEvent(event)
this.emit('ipc-message-sync', event, channel, ...args)
ipcMain.emit(channel, event, ...args)
})
this.on('ipc-internal-message', function (event, [channel, ...args]) {
addReplyInternalToEvent(event)
ipcMainInternal.emit(channel, event, ...args)
})
this.on('ipc-internal-message-sync', function (event, [channel, ...args]) {
Object.defineProperty(event, 'returnValue', {
set: function (value) {
return event.sendReply([value])
},
get: function () {}
})
addReplyInternalToEvent(event)
ipcMainInternal.emit(channel, event, ...args)
this.on('-ipc-message-sync', function (event, internal, channel, args) {
addReturnValueToEvent(event)
if (internal) {
addReplyInternalToEvent(event)
ipcMainInternal.emit(channel, event, ...args)
} else {
addReplyToEvent(event)
this.emit('ipc-message-sync', event, channel, ...args)
ipcMain.emit(channel, event, ...args)
}
})
// Handle context menu action request from pepper plugin.

View file

@ -140,7 +140,7 @@ const createGuest = function (embedder, params) {
}
// Dispatch guest's IPC messages to embedder.
guest.on('ipc-message-host', function (_, [channel, ...args]) {
guest.on('ipc-message-host', function (_, channel, args) {
sendToEmbedder('ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE', channel, ...args)
})

View file

@ -7,16 +7,16 @@ const v8Util = process.atomBinding('v8_util')
const ipcRenderer = v8Util.getHiddenValue(global, 'ipc')
const internal = false
ipcRenderer.send = function (...args) {
return binding.send('-ipc-message', args)
ipcRenderer.send = function (channel, ...args) {
return binding.send(internal, channel, args)
}
ipcRenderer.sendSync = function (...args) {
return binding.sendSync('-ipc-message-sync', args)[0]
ipcRenderer.sendSync = function (channel, ...args) {
return binding.sendSync(internal, channel, args)[0]
}
ipcRenderer.sendToHost = function (...args) {
return binding.send('ipc-message-host', args)
ipcRenderer.sendToHost = function (channel, ...args) {
return binding.sendToHost(channel, args)
}
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {

View file

@ -7,12 +7,12 @@ const v8Util = process.atomBinding('v8_util')
const ipcRenderer = v8Util.getHiddenValue(global, 'ipc-internal')
const internal = true
ipcRenderer.send = function (...args) {
return binding.send('ipc-internal-message', args)
ipcRenderer.send = function (channel, ...args) {
return binding.send(internal, channel, args)
}
ipcRenderer.sendSync = function (...args) {
return binding.sendSync('ipc-internal-message-sync', args)[0]
ipcRenderer.sendSync = function (channel, ...args) {
return binding.sendSync(internal, channel, args)[0]
}
ipcRenderer.sendTo = function (webContentsId, channel, ...args) {

View file

@ -56,12 +56,12 @@ const loadedModules = new Map([
const ipcNative = process.atomBinding('ipc')
v8Util.setHiddenValue(global, 'ipcNative', ipcNative)
ipcNative.onInternalMessage = function (channel, args, senderId) {
ipcRenderer.emit(channel, { sender: ipcRenderer, senderId }, ...args)
}
ipcNative.onMessage = function (channel, args, senderId) {
electron.ipcRenderer.emit(channel, { sender: electron.ipcRenderer, senderId }, ...args)
ipcNative.onMessage = function (internal, channel, args, senderId) {
if (internal) {
ipcRenderer.emit(channel, { sender: ipcRenderer, senderId }, ...args)
} else {
electron.ipcRenderer.emit(channel, { sender: electron.ipcRenderer, senderId }, ...args)
}
}
ipcNative.onExit = function () {