refactor: expose process.contextId directly (#29236)

This commit is contained in:
Milan Burda 2021-05-24 09:51:54 +02:00 committed by GitHub
parent 12aa6d7343
commit 8fc86517fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 5 additions and 23 deletions

View file

@ -45,10 +45,6 @@ require('@electron/internal/common/init');
// The global variable will be used by ipc for event dispatching
const v8Util = process._linkedBinding('electron_common_v8_util');
// Expose process.contextId
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
Object.defineProperty(process, 'contextId', { enumerable: true, value: contextId });
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;

View file

@ -96,10 +96,6 @@ Object.defineProperty(preloadProcess, 'noDeprecation', {
}
});
// Expose process.contextId
const contextId = v8Util.getHiddenValue<string>(global, 'contextId');
Object.defineProperty(preloadProcess, 'contextId', { enumerable: true, value: contextId });
process.on('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded'));
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));

View file

@ -74,8 +74,6 @@ void ElectronRendererClient::RunScriptsAtDocumentEnd(
void ElectronRendererClient::DidCreateScriptContext(
v8::Handle<v8::Context> renderer_context,
content::RenderFrame* render_frame) {
RendererClientBase::DidCreateScriptContext(renderer_context, render_frame);
// TODO(zcbenz): Do not create Node environment if node integration is not
// enabled.

View file

@ -200,8 +200,6 @@ void ElectronSandboxedRendererClient::RunScriptsAtDocumentEnd(
void ElectronSandboxedRendererClient::DidCreateScriptContext(
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) {
RendererClientBase::DidCreateScriptContext(context, render_frame);
// Only allow preload for the main frame or
// For devtools we still want to run the preload_bundle script
// Or when nodeSupport is explicitly enabled in sub frames

View file

@ -146,16 +146,6 @@ RendererClientBase::~RendererClientBase() {
g_renderer_client_base = nullptr;
}
void RendererClientBase::DidCreateScriptContext(
v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) {
// global.setHidden("contextId", `${processHostId}-${++next_context_id_}`)
auto context_id = base::StringPrintf(
"%s-%" PRId64, renderer_client_id_.c_str(), ++next_context_id_);
gin_helper::Dictionary global(context->GetIsolate(), context->Global());
global.SetHidden("contextId", context_id);
}
// static
RendererClientBase* RendererClientBase::Get() {
DCHECK(g_renderer_client_base);
@ -165,9 +155,13 @@ RendererClientBase* RendererClientBase::Get() {
void RendererClientBase::BindProcess(v8::Isolate* isolate,
gin_helper::Dictionary* process,
content::RenderFrame* render_frame) {
auto context_id = base::StringPrintf(
"%s-%" PRId64, renderer_client_id_.c_str(), ++next_context_id_);
process->SetReadOnly("isMainFrame", render_frame->IsMainFrame());
process->SetReadOnly("contextIsolated",
render_frame->GetBlinkPreferences().context_isolation);
process->SetReadOnly("contextId", context_id);
}
void RendererClientBase::RenderThreadStarted() {

View file

@ -69,7 +69,7 @@ class RendererClientBase : public content::ContentRendererClient
#endif
virtual void DidCreateScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame);
content::RenderFrame* render_frame) = 0;
virtual void WillReleaseScriptContext(v8::Handle<v8::Context> context,
content::RenderFrame* render_frame) = 0;
virtual void DidClearWindowObject(content::RenderFrame* render_frame);