refactor: expose process.contextId directly (#29236)
This commit is contained in:
parent
12aa6d7343
commit
8fc86517fa
6 changed files with 5 additions and 23 deletions
|
@ -45,10 +45,6 @@ require('@electron/internal/common/init');
|
||||||
// The global variable will be used by ipc for event dispatching
|
// The global variable will be used by ipc for event dispatching
|
||||||
const v8Util = process._linkedBinding('electron_common_v8_util');
|
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 { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') as typeof ipcRendererInternalModule;
|
||||||
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
|
const ipcRenderer = require('@electron/internal/renderer/api/ipc-renderer').default;
|
||||||
|
|
||||||
|
|
|
@ -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('loaded', () => (preloadProcess as events.EventEmitter).emit('loaded'));
|
||||||
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
|
process.on('exit', () => (preloadProcess as events.EventEmitter).emit('exit'));
|
||||||
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));
|
(process as events.EventEmitter).on('document-start', () => (preloadProcess as events.EventEmitter).emit('document-start'));
|
||||||
|
|
|
@ -74,8 +74,6 @@ void ElectronRendererClient::RunScriptsAtDocumentEnd(
|
||||||
void ElectronRendererClient::DidCreateScriptContext(
|
void ElectronRendererClient::DidCreateScriptContext(
|
||||||
v8::Handle<v8::Context> renderer_context,
|
v8::Handle<v8::Context> renderer_context,
|
||||||
content::RenderFrame* render_frame) {
|
content::RenderFrame* render_frame) {
|
||||||
RendererClientBase::DidCreateScriptContext(renderer_context, render_frame);
|
|
||||||
|
|
||||||
// TODO(zcbenz): Do not create Node environment if node integration is not
|
// TODO(zcbenz): Do not create Node environment if node integration is not
|
||||||
// enabled.
|
// enabled.
|
||||||
|
|
||||||
|
|
|
@ -200,8 +200,6 @@ void ElectronSandboxedRendererClient::RunScriptsAtDocumentEnd(
|
||||||
void ElectronSandboxedRendererClient::DidCreateScriptContext(
|
void ElectronSandboxedRendererClient::DidCreateScriptContext(
|
||||||
v8::Handle<v8::Context> context,
|
v8::Handle<v8::Context> context,
|
||||||
content::RenderFrame* render_frame) {
|
content::RenderFrame* render_frame) {
|
||||||
RendererClientBase::DidCreateScriptContext(context, render_frame);
|
|
||||||
|
|
||||||
// Only allow preload for the main frame or
|
// Only allow preload for the main frame or
|
||||||
// For devtools we still want to run the preload_bundle script
|
// For devtools we still want to run the preload_bundle script
|
||||||
// Or when nodeSupport is explicitly enabled in sub frames
|
// Or when nodeSupport is explicitly enabled in sub frames
|
||||||
|
|
|
@ -146,16 +146,6 @@ RendererClientBase::~RendererClientBase() {
|
||||||
g_renderer_client_base = nullptr;
|
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
|
// static
|
||||||
RendererClientBase* RendererClientBase::Get() {
|
RendererClientBase* RendererClientBase::Get() {
|
||||||
DCHECK(g_renderer_client_base);
|
DCHECK(g_renderer_client_base);
|
||||||
|
@ -165,9 +155,13 @@ RendererClientBase* RendererClientBase::Get() {
|
||||||
void RendererClientBase::BindProcess(v8::Isolate* isolate,
|
void RendererClientBase::BindProcess(v8::Isolate* isolate,
|
||||||
gin_helper::Dictionary* process,
|
gin_helper::Dictionary* process,
|
||||||
content::RenderFrame* render_frame) {
|
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("isMainFrame", render_frame->IsMainFrame());
|
||||||
process->SetReadOnly("contextIsolated",
|
process->SetReadOnly("contextIsolated",
|
||||||
render_frame->GetBlinkPreferences().context_isolation);
|
render_frame->GetBlinkPreferences().context_isolation);
|
||||||
|
process->SetReadOnly("contextId", context_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererClientBase::RenderThreadStarted() {
|
void RendererClientBase::RenderThreadStarted() {
|
||||||
|
|
|
@ -69,7 +69,7 @@ class RendererClientBase : public content::ContentRendererClient
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
virtual void DidCreateScriptContext(v8::Handle<v8::Context> context,
|
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,
|
virtual void WillReleaseScriptContext(v8::Handle<v8::Context> context,
|
||||||
content::RenderFrame* render_frame) = 0;
|
content::RenderFrame* render_frame) = 0;
|
||||||
virtual void DidClearWindowObject(content::RenderFrame* render_frame);
|
virtual void DidClearWindowObject(content::RenderFrame* render_frame);
|
||||||
|
|
Loading…
Reference in a new issue