Skip to content
This repository has been archived by the owner on Jan 4, 2019. It is now read-only.

Commit

Permalink
add ipcRenderer.sendShared
Browse files Browse the repository at this point in the history
auditors @bbondy
  • Loading branch information
bridiver committed Aug 3, 2017
1 parent e0748be commit 57ecc85
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
12 changes: 12 additions & 0 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<v8::Local<v8::Value>> 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> WebContents::FromTabID(v8::Isolate* isolate,
int tab_id) {
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AtomAutofillClient;

namespace base {
class SharedMemory;
class SharedMemoryHandle;
}

namespace blink {
Expand Down Expand Up @@ -516,6 +517,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
const base::ListValue& args,
IPC::Message* message);

void OnRendererMessageShared(const base::string16& channel,
const base::SharedMemoryHandle& shared_memory);

v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_;
v8::Global<v8::Value> debugger_;
Expand Down
4 changes: 4 additions & 0 deletions atom/common/api/api_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */,
Expand Down
5 changes: 5 additions & 0 deletions atom/common/api/resources/ipc_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) : []
Expand Down Expand Up @@ -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))

21 changes: 21 additions & 0 deletions atom/common/javascript_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down
9 changes: 8 additions & 1 deletion atom/common/javascript_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -27,6 +31,9 @@ class JavascriptBindings : public content::RenderViewObserver,
void GetBinding(const v8::FunctionCallbackInfo<v8::Value>& 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);
Expand Down

0 comments on commit 57ecc85

Please sign in to comment.