|
@@ -4,11 +4,13 @@
|
|
|
|
|
|
#include "shell/renderer/electron_sandboxed_renderer_client.h"
|
|
|
|
|
|
+#include <iterator>
|
|
|
#include <tuple>
|
|
|
#include <vector>
|
|
|
|
|
|
#include "base/base_paths.h"
|
|
|
#include "base/command_line.h"
|
|
|
+#include "base/containers/contains.h"
|
|
|
#include "base/files/file_path.h"
|
|
|
#include "base/path_service.h"
|
|
|
#include "base/process/process_handle.h"
|
|
@@ -33,7 +35,7 @@ namespace electron {
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
-const char kLifecycleKey[] = "lifecycle";
|
|
|
+const char kEmitProcessEventKey[] = "emit-process-event";
|
|
|
const char kModuleCacheKey[] = "native-module-cache";
|
|
|
|
|
|
v8::Local<v8::Object> GetModuleCache(v8::Isolate* isolate) {
|
|
@@ -94,27 +96,25 @@ double Uptime() {
|
|
|
.InSecondsF();
|
|
|
}
|
|
|
|
|
|
-void InvokeHiddenCallback(v8::Handle<v8::Context> context,
|
|
|
- const std::string& hidden_key,
|
|
|
- const std::string& callback_name) {
|
|
|
+void InvokeEmitProcessEvent(v8::Handle<v8::Context> context,
|
|
|
+ const std::string& event_name) {
|
|
|
auto* isolate = context->GetIsolate();
|
|
|
- auto binding_key =
|
|
|
- gin::ConvertToV8(isolate, hidden_key)->ToString(context).ToLocalChecked();
|
|
|
+ // set by sandboxed_renderer/init.js
|
|
|
+ auto binding_key = gin::ConvertToV8(isolate, kEmitProcessEventKey)
|
|
|
+ ->ToString(context)
|
|
|
+ .ToLocalChecked();
|
|
|
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))
|
|
|
+ v8::Local<v8::Value> callback_value;
|
|
|
+ if (!global_object->GetPrivate(context, private_binding_key)
|
|
|
+ .ToLocal(&callback_value))
|
|
|
return;
|
|
|
- if (value.IsEmpty() || !value->IsObject())
|
|
|
+ if (callback_value.IsEmpty() || !callback_value->IsFunction())
|
|
|
return;
|
|
|
- auto binding = value->ToObject(context).ToLocalChecked();
|
|
|
- auto callback_key = gin::ConvertToV8(isolate, callback_name)
|
|
|
- ->ToString(context)
|
|
|
- .ToLocalChecked();
|
|
|
- auto callback_value = binding->Get(context, callback_key).ToLocalChecked();
|
|
|
- DCHECK(callback_value->IsFunction()); // set by sandboxed_renderer/init.js
|
|
|
auto callback = callback_value.As<v8::Function>();
|
|
|
- std::ignore = callback->Call(context, binding, 0, nullptr);
|
|
|
+ v8::Local<v8::Value> args[] = {gin::ConvertToV8(isolate, event_name)};
|
|
|
+ std::ignore =
|
|
|
+ callback->Call(context, callback, std::size(args), std::data(args));
|
|
|
}
|
|
|
|
|
|
} // namespace
|
|
@@ -158,37 +158,13 @@ void ElectronSandboxedRendererClient::RenderFrameCreated(
|
|
|
void ElectronSandboxedRendererClient::RunScriptsAtDocumentStart(
|
|
|
content::RenderFrame* render_frame) {
|
|
|
RendererClientBase::RunScriptsAtDocumentStart(render_frame);
|
|
|
- if (injected_frames_.find(render_frame) == injected_frames_.end())
|
|
|
- return;
|
|
|
-
|
|
|
- auto* isolate = blink::MainThreadIsolate();
|
|
|
- v8::HandleScope handle_scope(isolate);
|
|
|
- v8::Local<v8::Context> context =
|
|
|
- GetContext(render_frame->GetWebFrame(), isolate);
|
|
|
- gin_helper::MicrotasksScope microtasks_scope(
|
|
|
- isolate, context->GetMicrotaskQueue(),
|
|
|
- v8::MicrotasksScope::kDoNotRunMicrotasks);
|
|
|
- v8::Context::Scope context_scope(context);
|
|
|
-
|
|
|
- InvokeHiddenCallback(context, kLifecycleKey, "onDocumentStart");
|
|
|
+ EmitProcessEvent(render_frame, "document-start");
|
|
|
}
|
|
|
|
|
|
void ElectronSandboxedRendererClient::RunScriptsAtDocumentEnd(
|
|
|
content::RenderFrame* render_frame) {
|
|
|
RendererClientBase::RunScriptsAtDocumentEnd(render_frame);
|
|
|
- if (injected_frames_.find(render_frame) == injected_frames_.end())
|
|
|
- return;
|
|
|
-
|
|
|
- auto* isolate = blink::MainThreadIsolate();
|
|
|
- v8::HandleScope handle_scope(isolate);
|
|
|
- v8::Local<v8::Context> context =
|
|
|
- GetContext(render_frame->GetWebFrame(), isolate);
|
|
|
- gin_helper::MicrotasksScope microtasks_scope(
|
|
|
- isolate, context->GetMicrotaskQueue(),
|
|
|
- v8::MicrotasksScope::kDoNotRunMicrotasks);
|
|
|
- v8::Context::Scope context_scope(context);
|
|
|
-
|
|
|
- InvokeHiddenCallback(context, kLifecycleKey, "onDocumentEnd");
|
|
|
+ EmitProcessEvent(render_frame, "document-end");
|
|
|
}
|
|
|
|
|
|
void ElectronSandboxedRendererClient::DidCreateScriptContext(
|
|
@@ -219,7 +195,7 @@ void ElectronSandboxedRendererClient::DidCreateScriptContext(
|
|
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
- InvokeHiddenCallback(context, kLifecycleKey, "onLoaded");
|
|
|
+ InvokeEmitProcessEvent(context, "loaded");
|
|
|
}
|
|
|
|
|
|
void ElectronSandboxedRendererClient::WillReleaseScriptContext(
|
|
@@ -234,7 +210,25 @@ void ElectronSandboxedRendererClient::WillReleaseScriptContext(
|
|
|
v8::MicrotasksScope::kDoNotRunMicrotasks);
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
- InvokeHiddenCallback(context, kLifecycleKey, "onExit");
|
|
|
+ InvokeEmitProcessEvent(context, "exit");
|
|
|
+}
|
|
|
+
|
|
|
+void ElectronSandboxedRendererClient::EmitProcessEvent(
|
|
|
+ content::RenderFrame* render_frame,
|
|
|
+ const char* event_name) {
|
|
|
+ if (injected_frames_.find(render_frame) == injected_frames_.end())
|
|
|
+ return;
|
|
|
+
|
|
|
+ auto* isolate = blink::MainThreadIsolate();
|
|
|
+ v8::HandleScope handle_scope(isolate);
|
|
|
+ v8::Local<v8::Context> context =
|
|
|
+ GetContext(render_frame->GetWebFrame(), isolate);
|
|
|
+ gin_helper::MicrotasksScope microtasks_scope(
|
|
|
+ isolate, context->GetMicrotaskQueue(),
|
|
|
+ v8::MicrotasksScope::kDoNotRunMicrotasks);
|
|
|
+ v8::Context::Scope context_scope(context);
|
|
|
+
|
|
|
+ InvokeEmitProcessEvent(context, event_name);
|
|
|
}
|
|
|
|
|
|
} // namespace electron
|