53f6cbccbf
* refactor: use mojo for electron internal IPC * add sender_id, drop MessageSync * remove usages of AtomFrameMsg_Message * iwyu * first draft of renderer->browser direction * refactor to reuse a single ipc interface * implement TakeHeapSnapshot through mojo * the rest of the owl^WtakeHeapSnapshot mojofication * remove no-op overrides in AtomRendererClient * delete renderer-side ElectronApiServiceImpl when its pipe is destroyed * looks like we don't need to overlay the renderer manifest after all * don't try to send 2 replies to a sync rpc * undo changes to manifests.cc * unify sandboxed + unsandboxed ipc events * lint * register ElectronBrowser mojo service on devtools WebContents * fix takeHeapSnapshopt failure paths * {electron_api => atom}::mojom * add send_to_all to ElectronRenderer::Message * keep interface alive until callback is called * review comments * use GetContext from RendererClientBase * robustify a test that uses window.open * MessageSync posts a task to put sync messages in the same queue as async ones * add v8::MicrotasksScope and node::CallbackScope * iwyu * use weakptr to api::WebContents instead of Unretained * make MessageSync an asynchronous message & use non-associated interface * iwyu + comments * remove unused WeakPtrFactory * inline OnRendererMessage[Sync] * cleanups & comments * use helper methods instead of inline lambdas * remove unneeded async in test * add mojo to manifests deps * add gn check for //electron/manifests and mojo * don't register renderer side service until preload has been run * update gn check targets list * move interface registration back to RenderFrameCreated
166 lines
5.9 KiB
C++
166 lines
5.9 KiB
C++
// Copyright (c) 2013 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <string>
|
|
|
|
#include "atom/common/api/api_messages.h"
|
|
#include "atom/common/native_mate_converters/value_converter.h"
|
|
#include "atom/common/node_bindings.h"
|
|
#include "atom/common/node_includes.h"
|
|
#include "base/task/post_task.h"
|
|
#include "base/values.h"
|
|
#include "content/public/renderer/render_frame.h"
|
|
#include "electron/atom/common/api/api.mojom.h"
|
|
#include "native_mate/arguments.h"
|
|
#include "native_mate/dictionary.h"
|
|
#include "native_mate/handle.h"
|
|
#include "native_mate/object_template_builder.h"
|
|
#include "native_mate/wrappable.h"
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
|
|
using blink::WebLocalFrame;
|
|
using content::RenderFrame;
|
|
|
|
namespace {
|
|
|
|
RenderFrame* GetCurrentRenderFrame() {
|
|
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
|
|
if (!frame)
|
|
return nullptr;
|
|
|
|
return RenderFrame::FromWebFrame(frame);
|
|
}
|
|
|
|
class IPCRenderer : public mate::Wrappable<IPCRenderer> {
|
|
public:
|
|
explicit IPCRenderer(v8::Isolate* isolate) {
|
|
Init(isolate);
|
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
DCHECK(render_frame);
|
|
render_frame->GetRemoteInterfaces()->GetInterface(
|
|
mojo::MakeRequest(&electron_browser_ptr_));
|
|
}
|
|
static void BuildPrototype(v8::Isolate* isolate,
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
|
prototype->SetClassName(mate::StringToV8(isolate, "IPCRenderer"));
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
|
.SetMethod("send", &IPCRenderer::Send)
|
|
.SetMethod("sendSync", &IPCRenderer::SendSync);
|
|
}
|
|
static mate::Handle<IPCRenderer> Create(v8::Isolate* isolate) {
|
|
return mate::CreateHandle(isolate, new IPCRenderer(isolate));
|
|
}
|
|
|
|
void Send(mate::Arguments* args,
|
|
bool internal,
|
|
const std::string& channel,
|
|
const base::ListValue& arguments) {
|
|
electron_browser_ptr_->Message(internal, channel, arguments.Clone());
|
|
}
|
|
|
|
base::Value SendSync(mate::Arguments* args,
|
|
bool internal,
|
|
const std::string& channel,
|
|
const base::ListValue& arguments) {
|
|
base::Value result;
|
|
|
|
// A task is posted to a separate thread to execute the request so that
|
|
// this thread may block on a waitable event. It is safe to pass raw
|
|
// pointers to |result| and |event| as this stack frame will survive until
|
|
// the request is complete.
|
|
scoped_refptr<base::SingleThreadTaskRunner> task_runner =
|
|
base::CreateSingleThreadTaskRunnerWithTraits({});
|
|
|
|
base::WaitableEvent response_received_event;
|
|
|
|
// We unbind the interface from this thread to pass it over to the worker
|
|
// thread temporarily. This requires that no callbacks be pending for this
|
|
// interface.
|
|
auto interface_info = electron_browser_ptr_.PassInterface();
|
|
task_runner->PostTask(
|
|
FROM_HERE, base::BindOnce(&IPCRenderer::SendMessageSyncOnWorkerThread,
|
|
base::Unretained(&interface_info),
|
|
base::Unretained(&response_received_event),
|
|
base::Unretained(&result), internal, channel,
|
|
base::Unretained(&arguments)));
|
|
response_received_event.Wait();
|
|
electron_browser_ptr_.Bind(std::move(interface_info));
|
|
return result;
|
|
}
|
|
|
|
private:
|
|
static void SendMessageSyncOnWorkerThread(
|
|
atom::mojom::ElectronBrowserPtrInfo* interface_info,
|
|
base::WaitableEvent* event,
|
|
base::Value* result,
|
|
bool internal,
|
|
const std::string& channel,
|
|
const base::ListValue* arguments) {
|
|
atom::mojom::ElectronBrowserPtr browser_ptr(std::move(*interface_info));
|
|
browser_ptr->MessageSync(
|
|
internal, channel, arguments->Clone(),
|
|
base::BindOnce(&IPCRenderer::ReturnSyncResponseToMainThread,
|
|
std::move(browser_ptr), base::Unretained(interface_info),
|
|
base::Unretained(event), base::Unretained(result)));
|
|
}
|
|
static void ReturnSyncResponseToMainThread(
|
|
atom::mojom::ElectronBrowserPtr ptr,
|
|
atom::mojom::ElectronBrowserPtrInfo* interface_info,
|
|
base::WaitableEvent* event,
|
|
base::Value* result,
|
|
base::Value response) {
|
|
*result = std::move(response);
|
|
*interface_info = ptr.PassInterface();
|
|
event->Signal();
|
|
}
|
|
|
|
atom::mojom::ElectronBrowserPtr electron_browser_ptr_;
|
|
};
|
|
|
|
void SendTo(mate::Arguments* args,
|
|
bool internal,
|
|
bool send_to_all,
|
|
int32_t web_contents_id,
|
|
const std::string& channel,
|
|
const base::ListValue& arguments) {
|
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
if (render_frame == nullptr)
|
|
return;
|
|
|
|
bool success = render_frame->Send(new AtomFrameHostMsg_Message_To(
|
|
render_frame->GetRoutingID(), internal, send_to_all, web_contents_id,
|
|
channel, arguments));
|
|
|
|
if (!success)
|
|
args->ThrowError("Unable to send AtomFrameHostMsg_Message_To");
|
|
}
|
|
|
|
void SendToHost(mate::Arguments* args,
|
|
const std::string& channel,
|
|
const base::ListValue& arguments) {
|
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
if (render_frame == nullptr)
|
|
return;
|
|
|
|
bool success = render_frame->Send(new AtomFrameHostMsg_Message_Host(
|
|
render_frame->GetRoutingID(), channel, arguments));
|
|
|
|
if (!success)
|
|
args->ThrowError("Unable to send AtomFrameHostMsg_Message_Host");
|
|
}
|
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
v8::Local<v8::Value> unused,
|
|
v8::Local<v8::Context> context,
|
|
void* priv) {
|
|
mate::Dictionary dict(context->GetIsolate(), exports);
|
|
dict.Set("ipc", IPCRenderer::Create(context->GetIsolate()));
|
|
dict.SetMethod("sendTo", &SendTo);
|
|
dict.SetMethod("sendToHost", &SendToHost);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_renderer_ipc, Initialize)
|