Skip to content
Permalink
Browse files

feat: make `ipc-message` and `ipc-message-sync` events public

  • Loading branch information...
miniak committed Jan 20, 2019
1 parent dacf7f8 commit 0c43c6ee87280497932674fc9d5e117614a0034e
@@ -279,10 +279,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)
@@ -2204,18 +2207,23 @@ 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(event_name, new Event(), channel, args);
auto* event_name = internal ? "ipc-internal-message" : "ipc-message";
EmitWithSender(event_name, frame_host, nullptr, 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(event_name, new Event(sender, message), channel, args);
auto* event_name =
internal ? "ipc-internal-message-sync" : "ipc-message-sync";
EmitWithSender(event_name, frame_host, message, channel, args);
}

void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
@@ -2233,6 +2241,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) {
@@ -479,11 +479,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);
@@ -496,6 +498,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,
@@ -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 */,
@@ -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
@@ -2,22 +2,22 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "atom/renderer/api/atom_api_renderer_ipc.h"
#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"
#include "base/values.h"
#include "content/public/renderer/render_frame.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
#include "third_party/blink/public/web/web_local_frame.h"

using blink::WebLocalFrame;
using content::RenderFrame;

namespace atom {

namespace api {
namespace {

RenderFrame* GetCurrentRenderFrame() {
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
@@ -28,20 +28,22 @@ RenderFrame* GetCurrentRenderFrame() {
}

void Send(mate::Arguments* args,
bool internal,
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(
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;
@@ -51,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)
@@ -78,20 +80,29 @@ void SendTo(mate::Arguments* args,
args->ThrowError("Unable to send AtomFrameHostMsg_Message_To");
}

} // namespace api
void SendToHost(mate::Arguments* args,
const std::string& channel,
const base::ListValue& arguments) {
RenderFrame* render_frame = GetCurrentRenderFrame();
if (render_frame == nullptr)
return;

} // namespace atom
bool success = render_frame->Send(new AtomFrameHostMsg_Message_Host(
render_frame->GetRoutingID(), channel, arguments));

namespace {
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,
void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("send", &atom::api::Send);
dict.SetMethod("sendSync", &atom::api::SendSync);
dict.SetMethod("sendTo", &atom::api::SendTo);
dict.SetMethod("send", &Send);
dict.SetMethod("sendSync", &SendSync);
dict.SetMethod("sendTo", &SendTo);
dict.SetMethod("sendToHost", &SendToHost);
}

} // namespace

This file was deleted.

@@ -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-message", args));
render_frame_->GetRoutingID(), true, channel, args));
}

void AtomRenderFrameObserver::EmitIPCEvent(blink::WebLocalFrame* frame,
@@ -11,7 +11,6 @@
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_bindings.h"
#include "atom/common/options_switches.h"
#include "atom/renderer/api/atom_api_renderer_ipc.h"
#include "atom/renderer/atom_render_frame_observer.h"
#include "base/base_paths.h"
#include "base/command_line.h"
@@ -673,6 +673,26 @@ Returns:

Emitted when the preload script `preloadPath` throws an unhandled exception `error`.

#### Event: 'ipc-message'

Returns:

* `event` Event
* `channel` String
* `args` Array

Emitted when `ipcRenderer.send(channel, ...args)` is called in the renderer process.

#### Event: 'ipc-message-sync'

Returns:

* `event` Event
* `channel` String
* `args` Array

Emitted when `ipcRenderer.sendSync(channel, ...args)` is called in the renderer process.

#### Event: 'desktop-capturer-get-sources'

Returns:
@@ -629,7 +629,6 @@ filenames = {
"atom/common/platform_util_win.cc",
"atom/common/promise_util.h",
"atom/common/promise_util.cc",
"atom/renderer/api/atom_api_renderer_ipc.h",
"atom/renderer/api/atom_api_renderer_ipc.cc",
"atom/renderer/api/atom_api_spell_check_client.cc",
"atom/renderer/api/atom_api_spell_check_client.h",
@@ -212,15 +212,15 @@ WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callba
WebContents.prototype.takeHeapSnapshot = function (filePath) {
return new Promise((resolve, reject) => {
const channel = `ELECTRON_TAKE_HEAP_SNAPSHOT_RESULT_${getNextId()}`
ipcMain.once(channel, (event, success) => {
ipcMainInternal.once(channel, (event, success) => {
if (success) {
resolve()
} else {
reject(new Error('takeHeapSnapshot failed'))
}
})
if (!this._takeHeapSnapshot(filePath, channel)) {
ipcMain.emit(channel, false)
ipcMainInternal.emit(channel, false)
}
})
}
@@ -337,11 +337,11 @@ 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]) {
this.on('ipc-message', function (event, channel, args) {
ipcMain.emit(channel, event, ...args)
})

this.on('ipc-message-sync', function (event, [channel, ...args]) {
this.on('ipc-message-sync', function (event, channel, args) {
Object.defineProperty(event, 'returnValue', {
set: function (value) {
return event.sendReply([value])
@@ -351,11 +351,11 @@ WebContents.prototype._init = function () {
ipcMain.emit(channel, event, ...args)
})

this.on('ipc-internal-message', function (event, [channel, ...args]) {
this.on('ipc-internal-message', function (event, channel, args) {
ipcMainInternal.emit(channel, event, ...args)
})

this.on('ipc-internal-message-sync', function (event, [channel, ...args]) {
this.on('ipc-internal-message-sync', function (event, channel, args) {
Object.defineProperty(event, 'returnValue', {
set: function (value) {
return event.sendReply([value])
@@ -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)
})

0 comments on commit 0c43c6e

Please sign in to comment.
You can’t perform that action at this time.