refactor: use mojo for electron internal IPC (#17406)

* 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
This commit is contained in:
Jeremy Apthorp 2019-04-02 15:38:16 -07:00 committed by GitHub
parent 5b696491db
commit 53f6cbccbf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 620 additions and 401 deletions

View file

@ -30,7 +30,7 @@ namespace atom {
namespace {
const char kIpcKey[] = "ipcNative";
const char kLifecycleKey[] = "lifecycle";
const char kModuleCacheKey[] = "native-module-cache";
bool IsDevTools(content::RenderFrame* render_frame) {
@ -91,41 +91,26 @@ v8::Local<v8::Value> CreatePreloadScript(v8::Isolate* isolate,
preloadSrc);
}
class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
public:
AtomSandboxedRenderFrameObserver(content::RenderFrame* render_frame,
AtomSandboxedRendererClient* renderer_client)
: AtomRenderFrameObserver(render_frame, renderer_client),
renderer_client_(renderer_client) {}
protected:
void EmitIPCEvent(blink::WebLocalFrame* frame,
bool internal,
const std::string& channel,
const base::ListValue& args,
int32_t sender_id) override {
if (!frame)
return;
auto* isolate = blink::MainThreadIsolate();
v8::HandleScope handle_scope(isolate);
auto context = renderer_client_->GetContext(frame, isolate);
v8::Context::Scope context_scope(context);
v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, internal),
mate::ConvertToV8(isolate, channel),
mate::ConvertToV8(isolate, args),
mate::ConvertToV8(isolate, sender_id)};
renderer_client_->InvokeIpcCallback(
context, "onMessage",
std::vector<v8::Local<v8::Value>>(argv, argv + node::arraysize(argv)));
}
private:
AtomSandboxedRendererClient* renderer_client_;
DISALLOW_COPY_AND_ASSIGN(AtomSandboxedRenderFrameObserver);
};
void InvokeHiddenCallback(v8::Handle<v8::Context> context,
const std::string& hidden_key,
const std::string& callback_name) {
auto* isolate = context->GetIsolate();
auto binding_key = mate::ConvertToV8(isolate, hidden_key)->ToString(isolate);
auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
auto global_object = context->Global();
v8::Local<v8::Value> value;
if (!global_object->GetPrivate(context, private_binding_key).ToLocal(&value))
return;
if (value.IsEmpty() || !value->IsObject())
return;
auto binding = value->ToObject(isolate);
auto callback_key =
mate::ConvertToV8(isolate, callback_name)->ToString(isolate);
auto callback_value = binding->Get(callback_key);
DCHECK(callback_value->IsFunction()); // set by sandboxed_renderer/init.js
auto callback = v8::Handle<v8::Function>::Cast(callback_value);
ignore_result(callback->Call(context, binding, 0, nullptr));
}
} // namespace
@ -166,7 +151,7 @@ void AtomSandboxedRendererClient::InitializeBindings(
void AtomSandboxedRendererClient::RenderFrameCreated(
content::RenderFrame* render_frame) {
new AtomSandboxedRenderFrameObserver(render_frame, this);
new AtomRenderFrameObserver(render_frame, this);
RendererClientBase::RenderFrameCreated(render_frame);
}
@ -187,8 +172,7 @@ void AtomSandboxedRendererClient::RunScriptsAtDocumentStart(
GetContext(render_frame->GetWebFrame(), isolate);
v8::Context::Scope context_scope(context);
InvokeIpcCallback(context, "onDocumentStart",
std::vector<v8::Local<v8::Value>>());
InvokeHiddenCallback(context, kLifecycleKey, "onDocumentStart");
}
void AtomSandboxedRendererClient::RunScriptsAtDocumentEnd(
@ -203,8 +187,7 @@ void AtomSandboxedRendererClient::RunScriptsAtDocumentEnd(
GetContext(render_frame->GetWebFrame(), isolate);
v8::Context::Scope context_scope(context);
InvokeIpcCallback(context, "onDocumentEnd",
std::vector<v8::Local<v8::Value>>());
InvokeHiddenCallback(context, kLifecycleKey, "onDocumentEnd");
}
void AtomSandboxedRendererClient::DidCreateScriptContext(
@ -303,29 +286,7 @@ void AtomSandboxedRendererClient::WillReleaseScriptContext(
auto* isolate = context->GetIsolate();
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
InvokeIpcCallback(context, "onExit", std::vector<v8::Local<v8::Value>>());
}
void AtomSandboxedRendererClient::InvokeIpcCallback(
v8::Handle<v8::Context> context,
const std::string& callback_name,
std::vector<v8::Handle<v8::Value>> args) {
auto* isolate = context->GetIsolate();
auto binding_key = mate::ConvertToV8(isolate, kIpcKey)->ToString(isolate);
auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
auto global_object = context->Global();
v8::Local<v8::Value> value;
if (!global_object->GetPrivate(context, private_binding_key).ToLocal(&value))
return;
if (value.IsEmpty() || !value->IsObject())
return;
auto binding = value->ToObject(isolate);
auto callback_key =
mate::ConvertToV8(isolate, callback_name)->ToString(isolate);
auto callback_value = binding->Get(callback_key);
DCHECK(callback_value->IsFunction()); // set by sandboxed_renderer/init.js
auto callback = v8::Handle<v8::Function>::Cast(callback_value);
ignore_result(callback->Call(context, binding, args.size(), args.data()));
InvokeHiddenCallback(context, kLifecycleKey, "onExit");
}
} // namespace atom