Skip to content

Commit

Permalink
refactor: use separate ipc-renderer-internal / ipc-main-internal APIs…
Browse files Browse the repository at this point in the history
… for Electron internals (electron#13940)
  • Loading branch information
miniak authored and MarshallOfSound committed Oct 6, 2018
1 parent f712261 commit b50f86e
Show file tree
Hide file tree
Showing 49 changed files with 322 additions and 133 deletions.
17 changes: 11 additions & 6 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1578,20 +1578,23 @@ void WebContents::TabTraverse(bool reverse) {
web_contents()->FocusThroughTabTraversal(reverse);
}

bool WebContents::SendIPCMessage(bool all_frames,
bool WebContents::SendIPCMessage(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args) {
return SendIPCMessageWithSender(all_frames, channel, args);
return SendIPCMessageWithSender(internal, send_to_all, channel, args);
}

bool WebContents::SendIPCMessageWithSender(bool all_frames,
bool WebContents::SendIPCMessageWithSender(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id) {
auto* frame_host = web_contents()->GetMainFrame();
if (frame_host) {
return frame_host->Send(new AtomFrameMsg_Message(
frame_host->GetRoutingID(), all_frames, channel, args, sender_id));
return frame_host->Send(new AtomFrameMsg_Message(frame_host->GetRoutingID(),
internal, send_to_all,
channel, args, sender_id));
}
return false;
}
Expand Down Expand Up @@ -2107,6 +2110,7 @@ void WebContents::OnRendererMessageSync(content::RenderFrameHost* frame_host,
}

void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
bool internal,
bool send_to_all,
int32_t web_contents_id,
const std::string& channel,
Expand All @@ -2115,7 +2119,8 @@ void WebContents::OnRendererMessageTo(content::RenderFrameHost* frame_host,
isolate(), web_contents_id);

if (web_contents) {
web_contents->SendIPCMessageWithSender(send_to_all, channel, args, ID());
web_contents->SendIPCMessageWithSender(internal, send_to_all, channel, args,
ID());
}
}

Expand Down
7 changes: 5 additions & 2 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ class WebContents : public mate::TrackableObject<WebContents>,
void TabTraverse(bool reverse);

// Send messages to browser.
bool SendIPCMessage(bool all_frames,
bool SendIPCMessage(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args);

bool SendIPCMessageWithSender(bool all_frames,
bool SendIPCMessageWithSender(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id = 0);
Expand Down Expand Up @@ -442,6 +444,7 @@ class WebContents : public mate::TrackableObject<WebContents>,

// Called when received a message from renderer to be forwarded.
void OnRendererMessageTo(content::RenderFrameHost* frame_host,
bool internal,
bool send_to_all,
int32_t web_contents_id,
const std::string& channel,
Expand Down
6 changes: 4 additions & 2 deletions atom/common/api/api_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomFrameHostMsg_Message_Sync,
base::ListValue /* arguments */,
base::ListValue /* result */)

IPC_MESSAGE_ROUTED4(AtomFrameHostMsg_Message_To,
IPC_MESSAGE_ROUTED5(AtomFrameHostMsg_Message_To,
bool /* internal */,
bool /* send_to_all */,
int32_t /* web_contents_id */,
std::string /* channel */,
base::ListValue /* arguments */)

IPC_MESSAGE_ROUTED4(AtomFrameMsg_Message,
IPC_MESSAGE_ROUTED5(AtomFrameMsg_Message,
bool /* internal */,
bool /* send_to_all */,
std::string /* channel */,
base::ListValue /* arguments */,
Expand Down
4 changes: 2 additions & 2 deletions atom/common/api/remote_callback_freer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void RemoteCallbackFreer::RunDestructor() {
args.AppendInteger(object_id_);
auto* frame_host = web_contents()->GetMainFrame();
if (frame_host) {
frame_host->Send(new AtomFrameMsg_Message(frame_host->GetRoutingID(), false,
channel, args, sender_id));
frame_host->Send(new AtomFrameMsg_Message(frame_host->GetRoutingID(), true,
false, channel, args, sender_id));
}

Observe(nullptr);
Expand Down
2 changes: 1 addition & 1 deletion atom/common/api/remote_object_freer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void RemoteObjectFreer::RunDestructor() {
if (!render_frame)
return;

auto* channel = "ipc-message";
auto* channel = "ipc-internal-message";
base::ListValue args;
args.AppendString("ELECTRON_BROWSER_DEREFERENCE");
args.AppendString(context_id_);
Expand Down
7 changes: 4 additions & 3 deletions atom/renderer/api/atom_api_renderer_ipc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ base::ListValue SendSync(mate::Arguments* args,
}

void SendTo(mate::Arguments* args,
bool internal,
bool send_to_all,
int32_t web_contents_id,
const std::string& channel,
Expand All @@ -69,9 +70,9 @@ void SendTo(mate::Arguments* args,
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));
bool success = render_frame->Send(new AtomFrameHostMsg_Message_To(
render_frame->GetRoutingID(), internal, send_to_all, web_contents_id,
channel, arguments));

if (!success)
args->ThrowError("Unable to send AtomFrameHostMsg_Message_To");
Expand Down
1 change: 1 addition & 0 deletions atom/renderer/api/atom_api_renderer_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ base::ListValue SendSync(mate::Arguments* args,
const base::ListValue& arguments);

void SendTo(mate::Arguments* args,
bool internal,
bool send_to_all,
int32_t web_contents_id,
const std::string& channel,
Expand Down
15 changes: 10 additions & 5 deletions atom/renderer/atom_render_frame_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ namespace {

bool GetIPCObject(v8::Isolate* isolate,
v8::Local<v8::Context> context,
bool internal,
v8::Local<v8::Object>* ipc) {
v8::Local<v8::String> key = mate::StringToV8(isolate, "ipc");
v8::Local<v8::String> key =
mate::StringToV8(isolate, internal ? "ipc-internal" : "ipc");
v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
v8::Local<v8::Object> global_object = context->Global();
v8::Local<v8::Value> value;
Expand Down Expand Up @@ -170,7 +172,8 @@ bool AtomRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
return handled;
}

void AtomRenderFrameObserver::OnBrowserMessage(bool send_to_all,
void AtomRenderFrameObserver::OnBrowserMessage(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id) {
Expand All @@ -186,14 +189,15 @@ void AtomRenderFrameObserver::OnBrowserMessage(bool send_to_all,
if (!frame || !render_frame_->IsMainFrame())
return;

EmitIPCEvent(frame, channel, args, sender_id);
EmitIPCEvent(frame, internal, channel, args, sender_id);

// Also send the message to all sub-frames.
if (send_to_all) {
for (blink::WebFrame* child = frame->FirstChild(); child;
child = child->NextSibling())
if (child->IsWebLocalFrame()) {
EmitIPCEvent(child->ToWebLocalFrame(), channel, args, sender_id);
EmitIPCEvent(child->ToWebLocalFrame(), internal, channel, args,
sender_id);
}
}
}
Expand All @@ -215,6 +219,7 @@ void AtomRenderFrameObserver::OnTakeHeapSnapshot(
}

void AtomRenderFrameObserver::EmitIPCEvent(blink::WebLocalFrame* frame,
bool internal,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id) {
Expand All @@ -233,7 +238,7 @@ void AtomRenderFrameObserver::EmitIPCEvent(blink::WebLocalFrame* frame,
return;

v8::Local<v8::Object> ipc;
if (GetIPCObject(isolate, context, &ipc)) {
if (GetIPCObject(isolate, context, internal, &ipc)) {
TRACE_EVENT0("devtools.timeline", "FunctionCall");
auto args_vector = ListValueToVector(isolate, args);
// Insert the Event object, event.sender is ipc.
Expand Down
4 changes: 3 additions & 1 deletion atom/renderer/atom_render_frame_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver {

protected:
virtual void EmitIPCEvent(blink::WebLocalFrame* frame,
bool internal,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id);
Expand All @@ -54,7 +55,8 @@ class AtomRenderFrameObserver : public content::RenderFrameObserver {
void CreateIsolatedWorldContext();
bool IsMainWorld(int world_id);
bool IsIsolatedWorld(int world_id);
void OnBrowserMessage(bool send_to_all,
void OnBrowserMessage(bool internal,
bool send_to_all,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id);
Expand Down
3 changes: 2 additions & 1 deletion atom/renderer/atom_sandboxed_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {

protected:
void EmitIPCEvent(blink::WebLocalFrame* frame,
bool internal,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id) override {
Expand All @@ -117,7 +118,7 @@ class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
mate::ConvertToV8(isolate, args),
mate::ConvertToV8(isolate, sender_id)};
renderer_client_->InvokeIpcCallback(
context, "onMessage",
context, internal ? "onInternalMessage" : "onMessage",
std::vector<v8::Local<v8::Value>>(argv, argv + node::arraysize(argv)));
}

Expand Down
6 changes: 5 additions & 1 deletion filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ filenames = {
"lib/browser/api/browser-view.js",
"lib/browser/api/browser-window.js",
"lib/browser/api/content-tracing.js",
"lib/browser/api/crash-reporter.js",
"lib/browser/api/dialog.js",
"lib/browser/api/exports/electron.js",
"lib/browser/api/global-shortcut.js",
Expand Down Expand Up @@ -43,10 +44,10 @@ filenames = {
"lib/browser/guest-view-manager.js",
"lib/browser/guest-window-manager.js",
"lib/browser/init.js",
"lib/browser/ipc-main-internal.js",
"lib/browser/objects-registry.js",
"lib/browser/rpc-server.js",
"lib/common/api/clipboard.js",
"lib/common/api/crash-reporter.js",
"lib/common/api/deprecate.js",
"lib/common/api/deprecations.js",
"lib/common/api/is-promise.js",
Expand All @@ -56,6 +57,7 @@ filenames = {
"lib/common/api/shell.js",
"lib/common/atom-binding-setup.js",
"lib/common/buffer-utils.js",
"lib/common/crash-reporter.js",
"lib/common/error-utils.js",
"lib/common/init.js",
"lib/common/parse-features-string.js",
Expand All @@ -65,6 +67,7 @@ filenames = {
"lib/renderer/content-scripts-injector.js",
"lib/renderer/init.js",
"lib/renderer/inspector.js",
"lib/renderer/ipc-renderer-internal.js",
"lib/renderer/override.js",
"lib/renderer/security-warnings.js",
"lib/renderer/web-frame-init.js",
Expand All @@ -74,6 +77,7 @@ filenames = {
"lib/renderer/web-view/web-view-attributes.js",
"lib/renderer/web-view/web-view-constants.js",
"lib/renderer/api/exports/electron.js",
"lib/renderer/api/crash-reporter.js",
"lib/renderer/api/ipc-renderer.js",
"lib/renderer/api/module-list.js",
"lib/renderer/api/remote.js",
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/api/browser-window.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict'

const electron = require('electron')
const { ipcMain, WebContentsView, TopLevelWindow } = electron
const { WebContentsView, TopLevelWindow } = electron
const { BrowserWindow } = process.atomBinding('window')
const v8Util = process.atomBinding('v8_util')
const ipcMain = require('@electron/internal/browser/ipc-main-internal')

Object.setPrototypeOf(BrowserWindow.prototype, TopLevelWindow.prototype)

Expand Down
14 changes: 14 additions & 0 deletions lib/browser/api/crash-reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict'

const CrashReporter = require('@electron/internal/common/crash-reporter')
const ipcMain = require('@electron/internal/browser/ipc-main-internal')

class CrashReporterMain extends CrashReporter {
sendSync (channel, ...args) {
const event = {}
ipcMain.emit(channel, event, ...args)
return event.returnValue
}
}

module.exports = new CrashReporterMain()
10 changes: 1 addition & 9 deletions lib/browser/api/ipc-main.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
'use strict'

const EventEmitter = require('events').EventEmitter
const { EventEmitter } = require('events')

const emitter = new EventEmitter()

const removeAllListeners = emitter.removeAllListeners.bind(emitter)
emitter.removeAllListeners = function (...args) {
if (args.length === 0) {
throw new Error('Removing all listeners from ipcMain will make Electron internals stop working. Please specify a event name')
}
removeAllListeners(...args)
}

// Do not throw exception when channel name is "error".
emitter.on('error', () => {})

Expand Down
1 change: 1 addition & 0 deletions lib/browser/api/module-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = [
{ name: 'BrowserView', file: 'browser-view' },
{ name: 'BrowserWindow', file: 'browser-window' },
{ name: 'contentTracing', file: 'content-tracing' },
{ name: 'crashReporter', file: 'crash-reporter' },
{ name: 'dialog', file: 'dialog' },
{ name: 'globalShortcut', file: 'global-shortcut' },
{ name: 'ipcMain', file: 'ipc-main' },
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/api/navigation-controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { ipcMain } = require('electron')
const ipcMain = require('@electron/internal/browser/ipc-main-internal')

// The history operation in renderer is redirected to browser.
ipcMain.on('ELECTRON_NAVIGATION_CONTROLLER', function (event, method, ...args) {
Expand Down
Loading

0 comments on commit b50f86e

Please sign in to comment.