chore: make rpc-server reply to sender frame instead of the main frame (#15973)

* chore: make rpc-server reply to frame

* fix: check IsRenderFrameLive
This commit is contained in:
Cheng Zhao 2018-12-10 09:37:42 +09:00 committed by GitHub
parent eb8fcf833c
commit db2fda1b6f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 10 deletions

View file

@ -1674,6 +1674,23 @@ bool WebContents::SendIPCMessageWithSender(bool internal,
return false;
}
bool WebContents::SendIPCMessageToFrame(bool internal,
bool send_to_all,
int32_t frame_id,
const std::string& channel,
const base::ListValue& args) {
auto frames = web_contents()->GetAllFrames();
auto iter = std::find_if(frames.begin(), frames.end(), [frame_id](auto* f) {
return f->GetRoutingID() == frame_id;
});
if (iter == frames.end())
return false;
if (!(*iter)->IsRenderFrameLive())
return false;
return (*iter)->Send(new AtomFrameMsg_Message(
frame_id, internal, send_to_all, channel, args, 0 /* sender_id */));
}
void WebContents::SendInputEvent(v8::Isolate* isolate,
v8::Local<v8::Value> input_event) {
content::RenderWidgetHostView* view =
@ -2119,6 +2136,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("isFocused", &WebContents::IsFocused)
.SetMethod("tabTraverse", &WebContents::TabTraverse)
.SetMethod("_send", &WebContents::SendIPCMessage)
.SetMethod("_sendToFrame", &WebContents::SendIPCMessageToFrame)
.SetMethod("sendInputEvent", &WebContents::SendInputEvent)
.SetMethod("beginFrameSubscription", &WebContents::BeginFrameSubscription)
.SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
@ -2183,7 +2201,7 @@ void WebContents::OnRendererMessage(content::RenderFrameHost* frame_host,
const std::string& channel,
const base::ListValue& args) {
// webContents.emit(channel, new Event(), args...);
Emit(channel, args);
EmitWithSender(channel, frame_host, nullptr, args);
}
void WebContents::OnRendererMessageSync(content::RenderFrameHost* frame_host,

View file

@ -218,6 +218,12 @@ class WebContents : public mate::TrackableObject<WebContents>,
const base::ListValue& args,
int32_t sender_id = 0);
bool SendIPCMessageToFrame(bool internal,
bool send_to_all,
int32_t frame_id,
const std::string& channel,
const base::ListValue& args);
// Send WebInputEvent to the page.
void SendInputEvent(v8::Isolate* isolate, v8::Local<v8::Value> input_event);

View file

@ -5,6 +5,7 @@
#include "atom/browser/api/event_emitter.h"
#include "atom/browser/api/event.h"
#include "content/public/browser/render_frame_host.h"
#include "native_mate/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
@ -56,6 +57,8 @@ v8::Local<v8::Object> CreateJSEvent(v8::Isolate* isolate,
event = CreateEventObject(isolate);
}
mate::Dictionary(isolate, event).Set("sender", object);
if (sender)
mate::Dictionary(isolate, event).Set("frameId", sender->GetRoutingID());
return event;
}