From 57ecc85913296dc29d6bce708aba77018884e9bc Mon Sep 17 00:00:00 2001 From: bridiver Date: Thu, 3 Aug 2017 11:47:26 -0700 Subject: [PATCH] add ipcRenderer.sendShared auditors @bbondy --- atom/browser/api/atom_api_web_contents.cc | 12 ++++++++++++ atom/browser/api/atom_api_web_contents.h | 4 ++++ atom/common/api/api_messages.h | 4 ++++ atom/common/api/resources/ipc_utils.js | 5 +++++ atom/common/javascript_bindings.cc | 21 +++++++++++++++++++++ atom/common/javascript_bindings.h | 9 ++++++++- 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 89538564f3..937fd24220 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1323,6 +1323,7 @@ bool WebContents::OnMessageReceived(const IPC::Message& message) { IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message, OnRendererMessage) IPC_MESSAGE_HANDLER_DELAY_REPLY(AtomViewHostMsg_Message_Sync, OnRendererMessageSync) + IPC_MESSAGE_HANDLER(AtomViewHostMsg_Message_Shared, OnRendererMessageShared) IPC_MESSAGE_HANDLER_CODE(ViewHostMsg_SetCursor, OnCursorChange, handled = false) IPC_MESSAGE_UNHANDLED(handled = false) @@ -2472,6 +2473,17 @@ void WebContents::OnRendererMessageSync(const base::string16& channel, EmitWithSender(base::UTF16ToUTF8(channel), web_contents(), message, args); } +void WebContents::OnRendererMessageShared(const base::string16& channel, + const base::SharedMemoryHandle& handle) { + std::vector> args = { + mate::StringToV8(isolate(), channel), + brave::SharedMemoryWrapper::CreateFrom(isolate(), handle).ToV8(), + }; + + // webContents.emit(channel, new Event(), args...); + Emit("ipc-message", args); +} + // static mate::Handle WebContents::FromTabID(v8::Isolate* isolate, int tab_id) { diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index bc662971f2..ac881b6058 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -33,6 +33,7 @@ class AtomAutofillClient; namespace base { class SharedMemory; +class SharedMemoryHandle; } namespace blink { @@ -516,6 +517,9 @@ class WebContents : public mate::TrackableObject, const base::ListValue& args, IPC::Message* message); + void OnRendererMessageShared(const base::string16& channel, + const base::SharedMemoryHandle& shared_memory); + v8::Global session_; v8::Global devtools_web_contents_; v8::Global debugger_; diff --git a/atom/common/api/api_messages.h b/atom/common/api/api_messages.h index 315a8094ff..82f4af2d11 100644 --- a/atom/common/api/api_messages.h +++ b/atom/common/api/api_messages.h @@ -25,6 +25,10 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync, base::ListValue /* arguments */, base::string16 /* result (in JSON) */) +IPC_MESSAGE_ROUTED2(AtomViewHostMsg_Message_Shared, + base::string16 /* channel */, + base::SharedMemoryHandle /* arguments */) + IPC_MESSAGE_ROUTED3(AtomViewMsg_Message, bool /* send_to_all */, base::string16 /* channel */, diff --git a/atom/common/api/resources/ipc_utils.js b/atom/common/api/resources/ipc_utils.js index fdfcfd907c..1e281dbc75 100644 --- a/atom/common/api/resources/ipc_utils.js +++ b/atom/common/api/resources/ipc_utils.js @@ -12,6 +12,10 @@ if (!ipcRenderer) { return ipc.send('ipc-message', $Array.slice(args)) } + ipcRenderer.sendShared = function (channel, shared) { + return ipc.sendShared(channel, shared) + } + ipcRenderer.sendSync = function () { var args args = 1 <= arguments.length ? $Array.slice(arguments, 0) : [] @@ -51,6 +55,7 @@ exports.$set('on', ipcRenderer.on.bind(ipcRenderer)) exports.$set('once', ipcRenderer.once.bind(ipcRenderer)) exports.$set('send', ipcRenderer.send.bind(ipcRenderer)) exports.$set('sendSync', ipcRenderer.sendSync.bind(ipcRenderer)) +exports.$set('sendShared', ipcRenderer.sendShared.bind(ipcRenderer)) exports.$set('sendToHost', ipcRenderer.sendToHost.bind(ipcRenderer)) exports.$set('emit', ipcRenderer.emit.bind(ipcRenderer)) diff --git a/atom/common/javascript_bindings.cc b/atom/common/javascript_bindings.cc index 10ec048f1a..55caa54253 100644 --- a/atom/common/javascript_bindings.cc +++ b/atom/common/javascript_bindings.cc @@ -12,6 +12,8 @@ #include "atom/common/native_mate_converters/content_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" +#include "base/memory/shared_memory.h" +#include "base/memory/shared_memory_handle.h" #include "brave/common/extensions/shared_memory_bindings.h" #include "content/public/renderer/render_frame.h" #include "content/public/renderer/render_view.h" @@ -138,6 +140,23 @@ void JavascriptBindings::IPCSend(mate::Arguments* args, args->ThrowError("Unable to send AtomViewHostMsg_Message"); } +void JavascriptBindings::IPCSendShared(mate::Arguments* args, + const base::string16& channel, + base::SharedMemory* shared_memory) { + if (!is_valid() || !render_view()) + return; + + base::SharedMemoryHandle memory_handle = shared_memory->handle().Duplicate(); + if (!memory_handle.IsValid()) + return false; + + bool success = Send(new AtomViewHostMsg_Message_Shared( + render_view()->GetRoutingID(), channel, memory_handle)); + + if (!success) + args->ThrowError("Unable to send AtomViewHostMsg_Message_Shared"); +} + base::string16 JavascriptBindings::IPCSendSync(mate::Arguments* args, const base::string16& channel, const base::ListValue& arguments) { @@ -170,6 +189,8 @@ void JavascriptBindings::GetBinding( base::Unretained(this))); ipc.SetMethod("sendSync", base::Bind(&JavascriptBindings::IPCSendSync, base::Unretained(this))); + ipc.SetMethod("sendShared", base::Bind(&JavascriptBindings::IPCSendShared, + base::Unretained(this))); binding.Set("ipc", ipc.GetHandle()); mate::Dictionary v8(isolate, v8::Object::New(isolate)); diff --git a/atom/common/javascript_bindings.h b/atom/common/javascript_bindings.h index 39b76b552d..b04de51119 100644 --- a/atom/common/javascript_bindings.h +++ b/atom/common/javascript_bindings.h @@ -5,12 +5,16 @@ #ifndef ATOM_COMMON_JAVASCRIPT_BINDINGS_H_ #define ATOM_COMMON_JAVASCRIPT_BINDINGS_H_ -#include "base/memory/shared_memory_handle.h" #include "content/public/renderer/render_view_observer.h" #include "extensions/renderer/object_backed_native_handler.h" #include "extensions/renderer/script_context.h" #include "v8/include/v8.h" +namespace base { +class SharedMemory; +class SharedMemoryHandle; +} + namespace mate { class Arguments; } @@ -27,6 +31,9 @@ class JavascriptBindings : public content::RenderViewObserver, void GetBinding(const v8::FunctionCallbackInfo& args); private: + void IPCSendShared(mate::Arguments* args, + const base::string16& channel, + base::SharedMemory* shared_memory); base::string16 IPCSendSync(mate::Arguments* args, const base::string16& channel, const base::ListValue& arguments);