Add webContents.sendToAll to send message to all frames
This commit is contained in:
parent
c1facec5a6
commit
9ab76fb884
7 changed files with 58 additions and 18 deletions
|
@ -1090,7 +1090,12 @@ void WebContents::TabTraverse(bool reverse) {
|
|||
|
||||
bool WebContents::SendIPCMessage(const base::string16& channel,
|
||||
const base::ListValue& args) {
|
||||
return Send(new AtomViewMsg_Message(routing_id(), channel, args));
|
||||
return Send(new AtomViewMsg_Message(routing_id(), false, channel, args));
|
||||
}
|
||||
|
||||
bool WebContents::SendIPCMessageToAll(const base::string16& channel,
|
||||
const base::ListValue& args) {
|
||||
return Send(new AtomViewMsg_Message(routing_id(), true, channel, args));
|
||||
}
|
||||
|
||||
void WebContents::SendInputEvent(v8::Isolate* isolate,
|
||||
|
@ -1258,6 +1263,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
|
|||
.SetMethod("focus", &WebContents::Focus)
|
||||
.SetMethod("tabTraverse", &WebContents::TabTraverse)
|
||||
.SetMethod("_send", &WebContents::SendIPCMessage)
|
||||
.SetMethod("_sendToAll", &WebContents::SendIPCMessageToAll)
|
||||
.SetMethod("sendInputEvent", &WebContents::SendInputEvent)
|
||||
.SetMethod("beginFrameSubscription",
|
||||
&WebContents::BeginFrameSubscription)
|
||||
|
|
|
@ -124,6 +124,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
// Send messages to browser.
|
||||
bool SendIPCMessage(const base::string16& channel,
|
||||
const base::ListValue& args);
|
||||
bool SendIPCMessageToAll(const base::string16& channel,
|
||||
const base::ListValue& args);
|
||||
|
||||
// Send WebInputEvent to the page.
|
||||
void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);
|
||||
|
|
|
@ -30,7 +30,8 @@ IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync,
|
|||
base::ListValue /* arguments */,
|
||||
base::string16 /* result (in JSON) */)
|
||||
|
||||
IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
|
||||
IPC_MESSAGE_ROUTED3(AtomViewMsg_Message,
|
||||
bool /* send_to_all */,
|
||||
base::string16 /* channel */,
|
||||
base::ListValue /* arguments */)
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@ void RemoteCallbackFreer::RunDestructor() {
|
|||
base::ASCIIToUTF16("ELECTRON_RENDERER_RELEASE_CALLBACK");
|
||||
base::ListValue args;
|
||||
args.AppendInteger(object_id_);
|
||||
Send(new AtomViewMsg_Message(routing_id(), channel, args));
|
||||
Send(new AtomViewMsg_Message(routing_id(), false, channel, args));
|
||||
|
||||
Observe(nullptr);
|
||||
}
|
||||
|
|
|
@ -59,6 +59,34 @@ std::vector<v8::Local<v8::Value>> ListValueToVector(
|
|||
return result;
|
||||
}
|
||||
|
||||
void EmitIPCEvent(blink::WebFrame* frame,
|
||||
const base::string16& channel,
|
||||
const base::ListValue& args) {
|
||||
if (!frame || frame->isWebRemoteFrame())
|
||||
return;
|
||||
|
||||
v8::Isolate* isolate = blink::mainThreadIsolate();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
|
||||
v8::Local<v8::Context> context = frame->mainWorldScriptContext();
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
// Only emit IPC event for context with node integration.
|
||||
node::Environment* env = node::Environment::GetCurrent(context);
|
||||
if (!env)
|
||||
return;
|
||||
|
||||
v8::Local<v8::Object> ipc;
|
||||
if (GetIPCObject(isolate, context, &ipc)) {
|
||||
auto args_vector = ListValueToVector(isolate, args);
|
||||
// Insert the Event object, event.sender is ipc.
|
||||
mate::Dictionary event = mate::Dictionary::CreateEmpty(isolate);
|
||||
event.Set("sender", ipc);
|
||||
args_vector.insert(args_vector.begin(), event.GetHandle());
|
||||
mate::EmitEvent(isolate, ipc, channel, args_vector);
|
||||
}
|
||||
}
|
||||
|
||||
base::StringPiece NetResourceProvider(int key) {
|
||||
if (key == IDR_DIR_HEADER_HTML) {
|
||||
base::StringPiece html_data =
|
||||
|
@ -123,7 +151,8 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
|
|||
return handled;
|
||||
}
|
||||
|
||||
void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
|
||||
void AtomRenderViewObserver::OnBrowserMessage(bool send_to_all,
|
||||
const base::string16& channel,
|
||||
const base::ListValue& args) {
|
||||
if (!document_created_)
|
||||
return;
|
||||
|
@ -135,20 +164,13 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
|
|||
if (!frame || frame->isWebRemoteFrame())
|
||||
return;
|
||||
|
||||
v8::Isolate* isolate = blink::mainThreadIsolate();
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
EmitIPCEvent(frame, channel, args);
|
||||
|
||||
v8::Local<v8::Context> context = frame->mainWorldScriptContext();
|
||||
v8::Context::Scope context_scope(context);
|
||||
|
||||
v8::Local<v8::Object> ipc;
|
||||
if (GetIPCObject(isolate, context, &ipc)) {
|
||||
auto args_vector = ListValueToVector(isolate, args);
|
||||
// Insert the Event object, event.sender is ipc.
|
||||
mate::Dictionary event = mate::Dictionary::CreateEmpty(isolate);
|
||||
event.Set("sender", ipc);
|
||||
args_vector.insert(args_vector.begin(), event.GetHandle());
|
||||
mate::EmitEvent(isolate, ipc, channel, args_vector);
|
||||
// Also send the message to all sub-frames.
|
||||
if (send_to_all) {
|
||||
for (blink::WebFrame* child = frame->firstChild(); child;
|
||||
child = child->nextSibling())
|
||||
EmitIPCEvent(child, channel, args);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -30,7 +30,8 @@ class AtomRenderViewObserver : public content::RenderViewObserver {
|
|||
void DraggableRegionsChanged(blink::WebFrame* frame) override;
|
||||
bool OnMessageReceived(const IPC::Message& message) override;
|
||||
|
||||
void OnBrowserMessage(const base::string16& channel,
|
||||
void OnBrowserMessage(bool send_to_all,
|
||||
const base::string16& channel,
|
||||
const base::ListValue& args);
|
||||
|
||||
// Weak reference to renderer client.
|
||||
|
|
|
@ -78,6 +78,14 @@ let wrapWebContents = function (webContents) {
|
|||
return this._send(channel, args)
|
||||
}
|
||||
|
||||
// WebContents::sendToAll(channel, args..)
|
||||
webContents.sendToAll = function (channel, ...args) {
|
||||
if (channel == null) {
|
||||
throw new Error('Missing required channel argument')
|
||||
}
|
||||
return this._sendToAll(channel, args)
|
||||
}
|
||||
|
||||
// The navigation controller.
|
||||
controller = new NavigationController(webContents)
|
||||
ref1 = NavigationController.prototype
|
||||
|
|
Loading…
Reference in a new issue