diff --git a/lib/browser/rpc-server.js b/lib/browser/rpc-server.js index aa4848031f871..7bcbba3ec5f0d 100644 --- a/lib/browser/rpc-server.js +++ b/lib/browser/rpc-server.js @@ -231,7 +231,7 @@ const unwrapArgs = function (sender, frameId, contextId, args) { v8Util.setHiddenValue(callIntoRenderer, 'location', meta.location) Object.defineProperty(callIntoRenderer, 'length', { value: meta.length }) - v8Util.setRemoteCallbackFreer(callIntoRenderer, contextId, meta.id, sender) + v8Util.setRemoteCallbackFreer(callIntoRenderer, frameId, contextId, meta.id, sender) rendererFunctions.set(objectId, callIntoRenderer) return callIntoRenderer } diff --git a/shell/common/api/remote_callback_freer.cc b/shell/common/api/remote_callback_freer.cc index e1057c04e9ac5..387b9e508445f 100644 --- a/shell/common/api/remote_callback_freer.cc +++ b/shell/common/api/remote_callback_freer.cc @@ -16,19 +16,23 @@ namespace electron { // static void RemoteCallbackFreer::BindTo(v8::Isolate* isolate, v8::Local target, + int frame_id, const std::string& context_id, int object_id, content::WebContents* web_contents) { - new RemoteCallbackFreer(isolate, target, context_id, object_id, web_contents); + new RemoteCallbackFreer(isolate, target, frame_id, context_id, object_id, + web_contents); } RemoteCallbackFreer::RemoteCallbackFreer(v8::Isolate* isolate, v8::Local target, + int frame_id, const std::string& context_id, int object_id, content::WebContents* web_contents) : ObjectLifeMonitor(isolate, target), content::WebContentsObserver(web_contents), + frame_id_(frame_id), context_id_(context_id), object_id_(object_id) {} @@ -40,10 +44,15 @@ void RemoteCallbackFreer::RunDestructor() { int32_t sender_id = 0; args.AppendString(context_id_); args.AppendInteger(object_id_); - auto* frame_host = web_contents()->GetMainFrame(); - if (frame_host) { + + auto frames = web_contents()->GetAllFrames(); + auto iter = std::find_if(frames.begin(), frames.end(), [this](auto* f) { + return f->GetRoutingID() == frame_id_; + }); + + if (iter != frames.end() && (*iter)->IsRenderFrameLive()) { mojom::ElectronRendererAssociatedPtr electron_ptr; - frame_host->GetRemoteAssociatedInterfaces()->GetInterface( + (*iter)->GetRemoteAssociatedInterfaces()->GetInterface( mojo::MakeRequest(&electron_ptr)); electron_ptr->Message(true /* internal */, false /* send_to_all */, channel, args.Clone(), sender_id); diff --git a/shell/common/api/remote_callback_freer.h b/shell/common/api/remote_callback_freer.h index aa4278c24ecfc..e6eea15d824a8 100644 --- a/shell/common/api/remote_callback_freer.h +++ b/shell/common/api/remote_callback_freer.h @@ -17,6 +17,7 @@ class RemoteCallbackFreer : public ObjectLifeMonitor, public: static void BindTo(v8::Isolate* isolate, v8::Local target, + int frame_id, const std::string& context_id, int object_id, content::WebContents* web_conents); @@ -24,6 +25,7 @@ class RemoteCallbackFreer : public ObjectLifeMonitor, protected: RemoteCallbackFreer(v8::Isolate* isolate, v8::Local target, + int frame_id, const std::string& context_id, int object_id, content::WebContents* web_conents); @@ -35,6 +37,7 @@ class RemoteCallbackFreer : public ObjectLifeMonitor, void RenderViewDeleted(content::RenderViewHost*) override; private: + int frame_id_; std::string context_id_; int object_id_;