2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-04-23 04:18:07 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-01-22 06:31:37 +00:00
|
|
|
#include <string>
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "base/task/post_task.h"
|
2019-01-22 06:31:37 +00:00
|
|
|
#include "base/values.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "gin/dictionary.h"
|
|
|
|
#include "gin/handle.h"
|
|
|
|
#include "gin/object_template_builder.h"
|
|
|
|
#include "gin/wrappable.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "services/service_manager/public/cpp/interface_provider.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/api/api.mojom.h"
|
2019-10-31 07:56:00 +00:00
|
|
|
#include "shell/common/gin_converters/blink_converter.h"
|
|
|
|
#include "shell/common/gin_converters/value_converter.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_bindings.h"
|
|
|
|
#include "shell/common/node_includes.h"
|
|
|
|
#include "shell/common/promise_util.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-07-28 07:29:51 +00:00
|
|
|
using blink::WebLocalFrame;
|
2018-04-18 01:55:30 +00:00
|
|
|
using content::RenderFrame;
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2019-01-22 06:31:37 +00:00
|
|
|
namespace {
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
RenderFrame* GetCurrentRenderFrame() {
|
2017-06-16 20:42:33 +00:00
|
|
|
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
|
2013-04-23 04:18:07 +00:00
|
|
|
if (!frame)
|
2016-07-10 09:52:28 +00:00
|
|
|
return nullptr;
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
return RenderFrame::FromWebFrame(frame);
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
class IPCRenderer : public gin::Wrappable<IPCRenderer> {
|
2019-04-02 22:38:16 +00:00
|
|
|
public:
|
2019-09-19 15:09:15 +00:00
|
|
|
static gin::WrapperInfo kWrapperInfo;
|
|
|
|
|
|
|
|
static gin::Handle<IPCRenderer> Create(v8::Isolate* isolate) {
|
|
|
|
return gin::CreateHandle(isolate, new IPCRenderer(isolate));
|
|
|
|
}
|
|
|
|
|
2019-06-05 00:10:31 +00:00
|
|
|
explicit IPCRenderer(v8::Isolate* isolate)
|
2019-09-18 19:58:00 +00:00
|
|
|
: task_runner_(base::CreateSingleThreadTaskRunner({base::ThreadPool()})) {
|
2019-04-02 22:38:16 +00:00
|
|
|
RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
|
|
DCHECK(render_frame);
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
// Bind the interface on the background runner. All accesses will be via
|
|
|
|
// the thread-safe pointer. This is to support our "fake-sync"
|
|
|
|
// MessageSync() hack; see the comment in IPCRenderer::SendSync.
|
2019-06-19 21:23:04 +00:00
|
|
|
electron::mojom::ElectronBrowserPtrInfo info;
|
2019-06-05 00:10:31 +00:00
|
|
|
render_frame->GetRemoteInterfaces()->GetInterface(mojo::MakeRequest(&info));
|
2019-06-19 21:23:04 +00:00
|
|
|
electron_browser_ptr_ =
|
|
|
|
electron::mojom::ThreadSafeElectronBrowserPtr::Create(std::move(info),
|
|
|
|
task_runner_);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
// gin::Wrappable:
|
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) override {
|
|
|
|
return gin::Wrappable<IPCRenderer>::GetObjectTemplateBuilder(isolate)
|
2019-04-02 22:38:16 +00:00
|
|
|
.SetMethod("send", &IPCRenderer::Send)
|
2019-04-03 21:22:23 +00:00
|
|
|
.SetMethod("sendSync", &IPCRenderer::SendSync)
|
|
|
|
.SetMethod("sendTo", &IPCRenderer::SendTo)
|
2019-05-31 17:25:19 +00:00
|
|
|
.SetMethod("sendToHost", &IPCRenderer::SendToHost)
|
|
|
|
.SetMethod("invoke", &IPCRenderer::Invoke);
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
2019-06-05 00:10:31 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
const char* GetTypeName() override { return "IPCRenderer"; }
|
2019-04-02 22:38:16 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
private:
|
2019-10-09 17:59:08 +00:00
|
|
|
void Send(v8::Isolate* isolate,
|
|
|
|
bool internal,
|
2019-04-02 22:38:16 +00:00
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
v8::Local<v8::Value> arguments) {
|
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
electron_browser_ptr_->get()->Message(internal, channel,
|
|
|
|
std::move(message));
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
v8::Local<v8::Promise> Invoke(v8::Isolate* isolate,
|
2019-08-23 22:45:50 +00:00
|
|
|
bool internal,
|
2019-05-31 17:25:19 +00:00
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
v8::Local<v8::Value> arguments) {
|
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return v8::Local<v8::Promise>();
|
|
|
|
}
|
|
|
|
electron::util::Promise<blink::CloneableMessage> p(isolate);
|
2019-05-31 17:25:19 +00:00
|
|
|
auto handle = p.GetHandle();
|
2019-06-05 00:10:31 +00:00
|
|
|
|
|
|
|
electron_browser_ptr_->get()->Invoke(
|
2019-10-09 17:59:08 +00:00
|
|
|
internal, channel, std::move(message),
|
|
|
|
base::BindOnce(
|
|
|
|
[](electron::util::Promise<blink::CloneableMessage> p,
|
|
|
|
blink::CloneableMessage result) { p.ResolveWithGin(result); },
|
|
|
|
std::move(p)));
|
2019-05-31 17:25:19 +00:00
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
void SendTo(v8::Isolate* isolate,
|
|
|
|
bool internal,
|
2019-04-03 21:22:23 +00:00
|
|
|
bool send_to_all,
|
|
|
|
int32_t web_contents_id,
|
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
v8::Local<v8::Value> arguments) {
|
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-06-05 00:10:31 +00:00
|
|
|
electron_browser_ptr_->get()->MessageTo(
|
2019-10-09 17:59:08 +00:00
|
|
|
internal, send_to_all, web_contents_id, channel, std::move(message));
|
2019-04-03 21:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
void SendToHost(v8::Isolate* isolate,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> arguments) {
|
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
electron_browser_ptr_->get()->MessageHost(channel, std::move(message));
|
2019-04-03 21:22:23 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage SendSync(v8::Isolate* isolate,
|
|
|
|
bool internal,
|
|
|
|
const std::string& channel,
|
|
|
|
v8::Local<v8::Value> arguments) {
|
|
|
|
blink::CloneableMessage message;
|
2019-10-31 07:56:00 +00:00
|
|
|
if (!gin::ConvertFromV8(isolate, arguments, &message)) {
|
2019-10-09 17:59:08 +00:00
|
|
|
return blink::CloneableMessage();
|
|
|
|
}
|
2019-05-31 17:25:19 +00:00
|
|
|
// We aren't using a true synchronous mojo call here. We're calling an
|
|
|
|
// asynchronous method and blocking on the result. The reason we're doing
|
|
|
|
// this is a little complicated, so buckle up.
|
|
|
|
//
|
|
|
|
// Mojo has a concept of synchronous calls. However, synchronous calls are
|
|
|
|
// dangerous. In particular, it's quite possible for two processes to call
|
|
|
|
// synchronous methods on each other and cause a deadlock. Mojo has a
|
|
|
|
// mechanism to avoid this kind of deadlock: if a process is waiting on the
|
|
|
|
// result of a synchronous call, and it receives an incoming call for a
|
|
|
|
// synchronous method, it will process that request immediately, even
|
|
|
|
// though it's currently blocking. However, if it receives an incoming
|
|
|
|
// request for an _asynchronous_ method, that can't cause a deadlock, so it
|
|
|
|
// stashes the request on a queue to be processed once the synchronous
|
|
|
|
// thing it's waiting on returns.
|
|
|
|
//
|
|
|
|
// This behavior is useful for preventing deadlocks, but it is inconvenient
|
|
|
|
// here because it can result in messages being reordered. If the main
|
|
|
|
// process is awaiting the result of a synchronous call (which it does only
|
|
|
|
// very rarely, since it's bad to block the main process), and we send
|
|
|
|
// first an asynchronous message to the main process, followed by a
|
|
|
|
// synchronous message, then the main process will process the synchronous
|
|
|
|
// one first.
|
|
|
|
//
|
|
|
|
// It turns out, Electron has some dependency on message ordering,
|
|
|
|
// especially during window shutdown, and getting messages out of order can
|
|
|
|
// result in, for example, remote objects disappearing unexpectedly. To
|
|
|
|
// avoid these issues and guarantee consistent message ordering, we send
|
|
|
|
// all messages to the main process as asynchronous messages. This causes
|
|
|
|
// them to always be queued and processed in the same order they were
|
|
|
|
// received, even if they were received while the main process was waiting
|
|
|
|
// on a synchronous call.
|
|
|
|
//
|
|
|
|
// However, in the calling process, we still need to block on the result,
|
|
|
|
// because the caller is expecting a result synchronously. So we do a bit
|
2019-06-05 00:10:31 +00:00
|
|
|
// of a trick: we pass the Mojo handle over to a worker thread, send the
|
2019-05-31 17:25:19 +00:00
|
|
|
// asynchronous message from that thread, and then block on the result.
|
2019-06-05 00:10:31 +00:00
|
|
|
// It's important that we pass the handle over to the worker thread,
|
|
|
|
// because that allows Mojo to process incoming messages (most importantly,
|
|
|
|
// the response to our request) on that thread. If we didn't pass it to a
|
|
|
|
// worker thread, and instead sent the call from the main thread, we would
|
2019-05-31 17:25:19 +00:00
|
|
|
// never receive a response because Mojo wouldn't be able to run its
|
|
|
|
// message handling code, because the main thread would be tied up blocking
|
|
|
|
// on the WaitableEvent.
|
|
|
|
//
|
|
|
|
// Phew. If you got this far, here's a gold star: ⭐️
|
|
|
|
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage result;
|
2019-04-02 22:38:16 +00:00
|
|
|
|
2019-06-05 00:10:31 +00:00
|
|
|
// A task is posted to a worker thread to execute the request so that
|
2019-04-02 22:38:16 +00:00
|
|
|
// this thread may block on a waitable event. It is safe to pass raw
|
2019-06-05 00:10:31 +00:00
|
|
|
// pointers to |result| and |response_received_event| as this stack frame
|
|
|
|
// will survive until the request is complete.
|
2019-04-02 22:38:16 +00:00
|
|
|
|
|
|
|
base::WaitableEvent response_received_event;
|
2019-06-05 00:10:31 +00:00
|
|
|
task_runner_->PostTask(
|
2019-04-02 22:38:16 +00:00
|
|
|
FROM_HERE, base::BindOnce(&IPCRenderer::SendMessageSyncOnWorkerThread,
|
2019-06-05 00:10:31 +00:00
|
|
|
base::Unretained(this),
|
2019-04-02 22:38:16 +00:00
|
|
|
base::Unretained(&response_received_event),
|
|
|
|
base::Unretained(&result), internal, channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
std::move(message)));
|
2019-04-02 22:38:16 +00:00
|
|
|
response_received_event.Wait();
|
2018-06-13 07:38:31 +00:00
|
|
|
return result;
|
2019-04-02 22:38:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 00:10:31 +00:00
|
|
|
void SendMessageSyncOnWorkerThread(base::WaitableEvent* event,
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage* result,
|
2019-06-05 00:10:31 +00:00
|
|
|
bool internal,
|
|
|
|
const std::string& channel,
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage arguments) {
|
2019-06-05 00:10:31 +00:00
|
|
|
electron_browser_ptr_->get()->MessageSync(
|
|
|
|
internal, channel, std::move(arguments),
|
2019-04-02 22:38:16 +00:00
|
|
|
base::BindOnce(&IPCRenderer::ReturnSyncResponseToMainThread,
|
|
|
|
base::Unretained(event), base::Unretained(result)));
|
|
|
|
}
|
2019-09-19 15:09:15 +00:00
|
|
|
|
2019-06-05 00:10:31 +00:00
|
|
|
static void ReturnSyncResponseToMainThread(base::WaitableEvent* event,
|
2019-10-09 17:59:08 +00:00
|
|
|
blink::CloneableMessage* result,
|
|
|
|
blink::CloneableMessage response) {
|
2019-04-02 22:38:16 +00:00
|
|
|
*result = std::move(response);
|
|
|
|
event->Signal();
|
|
|
|
}
|
|
|
|
|
2019-06-05 00:10:31 +00:00
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner_;
|
2019-06-19 21:23:04 +00:00
|
|
|
scoped_refptr<electron::mojom::ThreadSafeElectronBrowserPtr>
|
2019-06-05 00:10:31 +00:00
|
|
|
electron_browser_ptr_;
|
2019-04-02 22:38:16 +00:00
|
|
|
};
|
2013-04-23 13:52:19 +00:00
|
|
|
|
2019-09-19 15:09:15 +00:00
|
|
|
gin::WrapperInfo IPCRenderer::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2019-09-19 15:09:15 +00:00
|
|
|
gin::Dictionary dict(context->GetIsolate(), exports);
|
2019-04-02 22:38:16 +00:00
|
|
|
dict.Set("ipc", IPCRenderer::Create(context->GetIsolate()));
|
2013-04-23 04:18:07 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 16:01:29 +00:00
|
|
|
} // namespace
|
2013-04-23 04:18:07 +00:00
|
|
|
|
2019-03-08 18:29:52 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(atom_renderer_ipc, Initialize)
|