mirror of
https://github.com/electron/electron.git
synced 2026-05-02 03:00:22 -04:00
* feat: capture JS stack trace on renderer OOM
When a renderer process approaches its V8 heap limit, capture the
JavaScript stack trace and write it to both a Crashpad crash key
("js-oom-stack") and stderr.
The stack trace is captured via RequestInterrupt rather than directly
inside the NearHeapLimitCallback because CurrentStackTrace is unsafe
to call during OOM — V8 FATALs on optimized (TurboFan) frames that
have had their deoptimization data garbage-collected. RequestInterrupt
defers the capture to the next V8 safe point, where all frames are
guaranteed to have deopt data available. This matches Node.js's
approach of never capturing JS stacks inside the heap limit callback.
The callback is registered once per isolate via an atomic guard in
RendererClientBase::DidCreateScriptContext, preventing the CHECK
failure V8 raises on duplicate AddNearHeapLimitCallback registrations
(which would otherwise occur on page navigations or multiple contexts).
Refs: #46078
Made-with: Cursor
* Update shell/renderer/oom_stack_trace.cc
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
* Update shell/renderer/oom_stack_trace.cc
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
* test: add crash reporter test for OOM JS stack trace
Add a test that verifies the `electron.v8-oom.stack` crash key contains
the JS stack trace (including function names) when a renderer process
runs out of memory. Also deduplicate the heap info formatting in
oom_stack_trace.cc.
Refs: #46078
Made-with: Cursor
* fix: lint formatting in oom_stack_trace.cc
Made-with: Cursor
* fix: use proper logger API instead of cstdio
* fix: check heap headroom before capturing OOM stack trace
deepak1556: "Should there be check for available heap size [for]
CurrentStackTrace and formatting"
CurrentStackTrace allocates StackTraceInfo + StackFrameInfo on the V8
heap. If the 20 MB bump is partially consumed by the time the interrupt
fires, these allocations trigger a secondary OOM. Guard with a 2 MB
headroom check.
Made-with: Cursor
* fix: handle V8 cage limit when bumping heap for OOM stack capture
deepak1556: "Does this bumping work when we are at the cage limit of
4GB"
V8's pointer compression cage caps the heap at ~4 GB. When
current_heap_limit is already near the ceiling, our 20 MB bump gets
clamped to zero and the interrupt never fires. Detect this and record
heap info as the final crash key instead of waiting for a stack trace
that won't arrive.
Made-with: Cursor
* feat: add V8 heap statistics as OOM crash keys
deepak1556: "V8 seems to capture heap stats as crash keys but it gets
missed today due to the OOM callback override... wonder if we can
include that to get some more heuristics in the dump."
Record heap used/total/limit/available, per-space stats for old_space
and large_object_space, native/detached context counts, and utilization
percentage as crash keys. Also add heap stats in the V8OOMErrorCallback
in node_bindings.cc for the final OOM crash report.
Made-with: Cursor
* feat: support worker thread isolates for OOM stack trace
deepak1556: "You need a separate registration for worker threads via
WorkerScriptReadyForEvaluationOnWorkerThread but that also means the
process global g_registered_isolate would break."
Chromium has one V8 isolate per thread (main + one per web worker), so
thread_local is equivalent to per-isolate storage. Replace the global
atomic + mutex/set with a constinit thread_local OomState* that holds
the isolate pointer and per-isolate is_in_oom flag. The void* data
parameter on AddNearHeapLimitCallback delivers OomState* directly into
callbacks, so the hot path needs no TLS lookup.
Add WorkerScriptReadyForEvaluationOnWorkerThread and
WillDestroyWorkerContextOnWorkerThread overrides to RendererClientBase
so both ElectronRendererClient and ElectronSandboxedRendererClient get
worker OOM registration. Update ElectronRendererClient to call the base
class in both worker lifecycle methods.
Add a web worker OOM test that spawns a dedicated Worker with a memory
leak and verifies the stack trace captures the worker function name.
Made-with: Cursor
* fix: register OOM callback for all script contexts
When context isolation is enabled, ShouldNotifyClient skips
DidCreateScriptContext for the main world, but user JS still runs there
and can OOM. Register in DidInstallConditionalFeatures which fires for
every script context. The TLS dedup guard prevents double-registration
on the same isolate.
Made-with: Cursor
* fix: guard against division by zero and cage size changes in OOM handler
Add a zero-guard on heap_size_limit before computing utilization
percentage — maximizes robustness in an OOM code path.
Add static_assert on kPtrComprCageReservationSize to catch any
upstream V8 change to the cage size at compile time.
Made-with: Cursor
* fix: address review feedback on OOM stack trace PR
- Remove redundant RegisterOomStackTraceCallback from
electron_render_frame_observer.cc; DidCreateScriptContext is sufficient
since main world and isolated world share the same isolate
- Replace thread_local OomState* with base::ThreadLocalOwnedPointer
wrapped in base::NoDestructor per Chromium style for non-trivially
destructible types
- Change heap-headroom and cage-limit logs from ERROR to INFO since
users cannot act on these diagnostics
- Add comment explaining why base class is called last in
WillDestroyWorkerContextOnWorkerThread (OOM deregistration ordering)
Made-with: Cursor
* fix: skip OOM stack trace registration for worklet contexts
Worklets can share a thread and isolate via WorkletThreadHolder's
per-process singleton pattern. With per-thread OOM state, the first
worklet to be destroyed would prematurely remove the callback for
any remaining worklets on the same thread. Skip worklets entirely
to avoid this; can be revisited with ref-counting if needed.
Made-with: Cursor
* fix: prevent dangling raw_ptr<v8::Isolate> in OOM state
The OomState held a raw_ptr<v8::Isolate> that outlived the isolate on
the main thread: gin::IsolateHolder destroyed the isolate during
shutdown, but the OomState (stored in thread-local storage) was only
released later in JavascriptEnvironment::~JavascriptEnvironment. This
triggers a dangling pointer check when building with
enable_dangling_raw_ptr_checks.
Register OomState as a gin::PerIsolateData::DisposeObserver so it
clears the raw_ptr and removes the NearHeapLimitCallback before the
isolate is destroyed, regardless of destructor ordering.
Suggested-by: Deepak Mohan
Made-with: Cursor
* test: verify OOM crash keys end-to-end via crash reporter
Replace stderr-based OOM tests with end-to-end crash dump validation.
Instead of parsing log output, start a crash reporter server, trigger
renderer OOM, and verify the uploaded crash dump contains the expected
`electron.v8-oom.*` annotations — the same code path production crash
reports take.
Consolidate all OOM test scenarios (basic heap leak, JSON.stringify,
web worker) into a single `describe('OOM crash keys')` block inside
api-crash-reporter-spec using the existing crash fixture app with new
renderer-oom-json and renderer-oom-worker crash types.
The web worker test verifies that OOM crash keys are present but does
not assert on the JS function name: the 20 MB heap bump may be
exhausted before V8 reaches a safe point to fire the stack-capture
interrupt, leaving the crash key at "(stack pending)". Increasing the
bump or switching to a synchronous capture strategy would fix this but
is left for a follow-up.
Remove the standalone oom-stack-trace-spec.ts and its fixture app.
Made-with: Cursor
---------
Co-authored-by: Niklas Wenzel <dev@nikwen.de>
667 lines
26 KiB
C++
667 lines
26 KiB
C++
// Copyright (c) 2017 GitHub, Inc.
|
|
// Use of this source code is governed by the MIT license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include "shell/renderer/renderer_client_base.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "base/command_line.h"
|
|
#include "base/functional/callback_helpers.h"
|
|
#include "base/strings/string_split.h"
|
|
#include "components/network_hints/renderer/web_prescient_networking_impl.h"
|
|
#include "content/common/buildflags.h"
|
|
#include "content/public/common/content_constants.h"
|
|
#include "content/public/common/content_switches.h"
|
|
#include "content/public/renderer/render_frame.h"
|
|
#include "content/public/renderer/render_thread.h"
|
|
#include "electron/buildflags/buildflags.h"
|
|
#include "electron/fuses.h"
|
|
#include "printing/buildflags/buildflags.h"
|
|
#include "shell/browser/api/electron_api_protocol.h"
|
|
#include "shell/common/api/electron_api_native_image.h"
|
|
#include "shell/common/color_util.h"
|
|
#include "shell/common/gin_helper/dictionary.h"
|
|
#include "shell/common/node_includes.h"
|
|
#include "shell/common/node_util.h"
|
|
#include "shell/common/options_switches.h"
|
|
#include "shell/common/plugin.mojom.h"
|
|
#include "shell/common/world_ids.h"
|
|
#include "shell/renderer/api/context_bridge/object_cache.h"
|
|
#include "shell/renderer/api/electron_api_context_bridge.h"
|
|
#include "shell/renderer/browser_exposed_renderer_interfaces.h"
|
|
#include "shell/renderer/content_settings_observer.h"
|
|
#include "shell/renderer/electron_api_service_impl.h"
|
|
#include "shell/renderer/electron_autofill_agent.h"
|
|
#include "shell/renderer/oom_stack_trace.h"
|
|
#include "third_party/abseil-cpp/absl/strings/str_format.h"
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
|
|
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
|
|
#include "third_party/blink/public/platform/web_runtime_features.h"
|
|
#include "third_party/blink/public/web/blink.h"
|
|
#include "third_party/blink/public/web/web_custom_element.h" // NOLINT(build/include_alpha)
|
|
#include "third_party/blink/public/web/web_frame_widget.h"
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
|
#include "third_party/blink/public/web/web_plugin_params.h"
|
|
#include "third_party/blink/public/web/web_script_source.h"
|
|
#include "third_party/blink/public/web/web_security_policy.h"
|
|
#include "third_party/blink/public/web/web_view.h"
|
|
#include "third_party/blink/renderer/core/execution_context/execution_context.h" // nogncheck
|
|
#include "third_party/blink/renderer/platform/media/multi_buffer_data_source.h" // nogncheck
|
|
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h" // nogncheck
|
|
#include "third_party/widevine/cdm/buildflags.h"
|
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
#include "components/pdf/renderer/internal_plugin_renderer_helpers.h"
|
|
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
#include <shlobj.h>
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_WIDEVINE)
|
|
#include "chrome/renderer/media/chrome_key_systems.h" // nogncheck
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
#include "components/spellcheck/renderer/spellcheck.h"
|
|
#include "components/spellcheck/renderer/spellcheck_provider.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
#include "components/pdf/common/constants.h" // nogncheck
|
|
#include "components/pdf/common/pdf_util.h" // nogncheck
|
|
#include "shell/common/electron_constants.h"
|
|
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
#if BUILDFLAG(ENABLE_PLUGINS)
|
|
#include "shell/common/plugin_info.h"
|
|
#endif // BUILDFLAG(ENABLE_PLUGINS)
|
|
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
#include "components/printing/renderer/print_render_frame_helper.h"
|
|
#include "printing/metafile_agent.h" // nogncheck
|
|
#include "shell/renderer/printing/print_render_frame_helper_delegate.h"
|
|
#endif // BUILDFLAG(ENABLE_PRINTING)
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
#include "base/strings/utf_string_conversions.h"
|
|
#include "content/public/common/webplugininfo.h"
|
|
#include "extensions/common/constants.h"
|
|
#include "extensions/common/extensions_client.h"
|
|
#include "extensions/renderer/api/core_extensions_renderer_api_provider.h"
|
|
#include "extensions/renderer/dispatcher.h"
|
|
#include "extensions/renderer/extension_frame_helper.h"
|
|
#include "extensions/renderer/extension_web_view_helper.h"
|
|
#include "extensions/renderer/guest_view/mime_handler_view/mime_handler_view_container_manager.h"
|
|
#include "shell/common/extensions/electron_extensions_client.h"
|
|
#include "shell/renderer/extensions/electron_extensions_renderer_api_provider.h"
|
|
#include "shell/renderer/extensions/electron_extensions_renderer_client.h"
|
|
#endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
|
|
namespace electron {
|
|
|
|
content::RenderFrame* GetRenderFrame(v8::Isolate* const isolate,
|
|
v8::Local<v8::Object> value);
|
|
|
|
namespace {
|
|
|
|
void SetIsWebView(v8::Isolate* isolate, v8::Local<v8::Object> object) {
|
|
gin_helper::Dictionary dict(isolate, object);
|
|
dict.SetHidden("isWebView", true);
|
|
}
|
|
|
|
std::vector<std::string> ParseSchemesCLISwitch(
|
|
base::CommandLine* command_line,
|
|
const std::string_view switch_name) {
|
|
std::string custom_schemes = command_line->GetSwitchValueASCII(switch_name);
|
|
return base::SplitString(custom_schemes, ",", base::TRIM_WHITESPACE,
|
|
base::SPLIT_WANT_NONEMPTY);
|
|
}
|
|
|
|
// static
|
|
RendererClientBase* g_renderer_client_base = nullptr;
|
|
|
|
bool IsDevTools(content::RenderFrame* render_frame) {
|
|
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
|
|
"devtools");
|
|
}
|
|
|
|
bool IsDevToolsExtension(content::RenderFrame* render_frame) {
|
|
return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
|
|
"chrome-extension");
|
|
}
|
|
|
|
} // namespace
|
|
|
|
RendererClientBase::RendererClientBase() {
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
|
// Parse --service-worker-schemes=scheme1,scheme2
|
|
std::vector<std::string> service_worker_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kServiceWorkerSchemes);
|
|
for (const std::string& scheme : service_worker_schemes_list)
|
|
electron::api::AddServiceWorkerScheme(scheme);
|
|
// Parse --standard-schemes=scheme1,scheme2
|
|
std::vector<std::string> standard_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kStandardSchemes);
|
|
for (const std::string& scheme : standard_schemes_list)
|
|
url::AddStandardScheme(scheme.c_str(), url::SCHEME_WITH_HOST);
|
|
// Parse --cors-schemes=scheme1,scheme2
|
|
std::vector<std::string> cors_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kCORSSchemes);
|
|
for (const std::string& scheme : cors_schemes_list)
|
|
url::AddCorsEnabledScheme(scheme.c_str());
|
|
// Parse --streaming-schemes=scheme1,scheme2
|
|
std::vector<std::string> streaming_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kStreamingSchemes);
|
|
for (const std::string& scheme : streaming_schemes_list)
|
|
blink::AddStreamingScheme(scheme.c_str());
|
|
// Parse --secure-schemes=scheme1,scheme2
|
|
std::vector<std::string> secure_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kSecureSchemes);
|
|
for (const std::string& scheme : secure_schemes_list)
|
|
url::AddSecureScheme(scheme.data());
|
|
// Parse --extension-schemes=scheme1,scheme2
|
|
std::vector<std::string> extension_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kExtensionSchemes);
|
|
for (const std::string& scheme : extension_schemes_list)
|
|
url::AddExtensionScheme(scheme.c_str());
|
|
// We rely on the unique process host id which is notified to the
|
|
// renderer process via command line switch from the content layer,
|
|
// if this switch is removed from the content layer for some reason,
|
|
// we should define our own.
|
|
DCHECK(command_line->HasSwitch(::switches::kRendererClientId));
|
|
renderer_client_id_ =
|
|
command_line->GetSwitchValueASCII(::switches::kRendererClientId);
|
|
|
|
g_renderer_client_base = this;
|
|
}
|
|
|
|
RendererClientBase::~RendererClientBase() {
|
|
g_renderer_client_base = nullptr;
|
|
}
|
|
|
|
// static
|
|
RendererClientBase* RendererClientBase::Get() {
|
|
DCHECK(g_renderer_client_base);
|
|
return g_renderer_client_base;
|
|
}
|
|
|
|
void RendererClientBase::BindProcess(v8::Isolate* isolate,
|
|
gin_helper::Dictionary* process,
|
|
content::RenderFrame* render_frame) {
|
|
auto context_id =
|
|
absl::StrFormat("%s-%" PRId64, renderer_client_id_, ++next_context_id_);
|
|
|
|
process->SetReadOnly("isMainFrame", render_frame->IsMainFrame());
|
|
process->SetReadOnly("contextIsolated",
|
|
render_frame->GetBlinkPreferences().context_isolation);
|
|
process->SetReadOnly("contextId", context_id);
|
|
}
|
|
|
|
bool RendererClientBase::ShouldLoadPreload(
|
|
v8::Isolate* const isolate,
|
|
v8::Local<v8::Context> context,
|
|
content::RenderFrame* render_frame) const {
|
|
auto prefs = render_frame->GetBlinkPreferences();
|
|
bool is_main_frame = render_frame->IsMainFrame();
|
|
bool is_devtools =
|
|
IsDevTools(render_frame) || IsDevToolsExtension(render_frame);
|
|
bool allow_node_in_sub_frames = prefs.node_integration_in_sub_frames;
|
|
|
|
return (is_main_frame || is_devtools || allow_node_in_sub_frames) &&
|
|
!IsWebViewFrame(isolate, context, render_frame);
|
|
}
|
|
|
|
void RendererClientBase::RenderThreadStarted() {
|
|
auto* command_line = base::CommandLine::ForCurrentProcess();
|
|
|
|
// Enable MessagePort close event by default.
|
|
// The feature got reverted from stable to test in
|
|
// https://chromium-review.googlesource.com/c/chromium/src/+/5276821
|
|
// We had the event supported through patch before upstream support,
|
|
// this is an alternative option than restoring our patch.
|
|
blink::WebRuntimeFeatures::EnableFeatureFromString("MessagePortCloseEvent",
|
|
true);
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_client_ = std::make_unique<ElectronExtensionsClient>();
|
|
extensions::ExtensionsClient::Set(extensions_client_.get());
|
|
|
|
extensions_renderer_client_ =
|
|
std::make_unique<ElectronExtensionsRendererClient>();
|
|
extensions_renderer_client_->AddAPIProvider(
|
|
std::make_unique<extensions::CoreExtensionsRendererAPIProvider>());
|
|
extensions_renderer_client_->AddAPIProvider(
|
|
std::make_unique<ElectronExtensionsRendererAPIProvider>());
|
|
extensions::ExtensionsRendererClient::Set(extensions_renderer_client_.get());
|
|
extensions_renderer_client_->RenderThreadStarted();
|
|
|
|
blink::String extension_scheme(extensions::kExtensionScheme);
|
|
// Extension resources are HTTP-like and safe to expose to the fetch API. The
|
|
// rules for the fetch API are consistent with XHR.
|
|
blink::SchemeRegistry::RegisterURLSchemeAsSupportingFetchAPI(
|
|
extension_scheme);
|
|
// Extension resources, when loaded as the top-level document, should bypass
|
|
// Blink's strict first-party origin checks.
|
|
blink::SchemeRegistry::RegisterURLSchemeAsFirstPartyWhenTopLevel(
|
|
extension_scheme);
|
|
blink::SchemeRegistry::RegisterURLSchemeAsBypassingContentSecurityPolicy(
|
|
extension_scheme);
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
spellcheck_ = std::make_unique<SpellCheck>(this);
|
|
#endif
|
|
|
|
blink::WebCustomElement::AddEmbedderCustomElementName("webview");
|
|
blink::WebCustomElement::AddEmbedderCustomElementName("browserplugin");
|
|
|
|
std::vector<std::string> fetch_enabled_schemes =
|
|
ParseSchemesCLISwitch(command_line, switches::kFetchSchemes);
|
|
for (const std::string& scheme : fetch_enabled_schemes) {
|
|
blink::WebSecurityPolicy::RegisterURLSchemeAsSupportingFetchAPI(
|
|
blink::WebString::FromUTF8(scheme));
|
|
}
|
|
|
|
std::vector<std::string> service_worker_schemes =
|
|
ParseSchemesCLISwitch(command_line, switches::kServiceWorkerSchemes);
|
|
for (const std::string& scheme : service_worker_schemes)
|
|
blink::WebSecurityPolicy::RegisterURLSchemeAsAllowingServiceWorkers(
|
|
blink::WebString::FromUTF8(scheme));
|
|
|
|
std::vector<std::string> csp_bypassing_schemes =
|
|
ParseSchemesCLISwitch(command_line, switches::kBypassCSPSchemes);
|
|
for (const std::string& scheme : csp_bypassing_schemes)
|
|
blink::SchemeRegistry::RegisterURLSchemeAsBypassingContentSecurityPolicy(
|
|
blink::String(scheme));
|
|
|
|
std::vector<std::string> code_cache_schemes_list =
|
|
ParseSchemesCLISwitch(command_line, switches::kCodeCacheSchemes);
|
|
for (const auto& scheme : code_cache_schemes_list) {
|
|
blink::WebSecurityPolicy::RegisterURLSchemeAsCodeCacheWithHashing(
|
|
blink::WebString::FromUTF8(scheme));
|
|
}
|
|
|
|
// Allow file scheme to handle service worker by default.
|
|
// FIXME(zcbenz): Can this be moved elsewhere?
|
|
if (electron::fuses::IsGrantFileProtocolExtraPrivilegesEnabled()) {
|
|
blink::WebSecurityPolicy::RegisterURLSchemeAsAllowingServiceWorkers("file");
|
|
blink::SchemeRegistry::RegisterURLSchemeAsSupportingFetchAPI("file");
|
|
}
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
// Set ApplicationUserModelID in renderer process.
|
|
std::wstring app_id =
|
|
command_line->GetSwitchValueNative(switches::kAppUserModelId);
|
|
if (!app_id.empty()) {
|
|
SetCurrentProcessExplicitAppUserModelID(app_id.c_str());
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::ExposeInterfacesToBrowser(mojo::BinderMap* binders) {
|
|
// NOTE: Do not add binders directly within this method. Instead, modify the
|
|
// definition of |ExposeElectronRendererInterfacesToBrowser()| to ensure
|
|
// security review coverage.
|
|
ExposeElectronRendererInterfacesToBrowser(this, binders);
|
|
}
|
|
|
|
void RendererClientBase::RenderFrameCreated(
|
|
content::RenderFrame* render_frame) {
|
|
#if defined(TOOLKIT_VIEWS)
|
|
new AutofillAgent(render_frame,
|
|
render_frame->GetAssociatedInterfaceRegistry());
|
|
#endif
|
|
new ContentSettingsObserver(render_frame);
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
new printing::PrintRenderFrameHelper(
|
|
render_frame,
|
|
std::make_unique<electron::PrintRenderFrameHelperDelegate>());
|
|
#endif
|
|
|
|
// Note: ElectronApiServiceImpl has to be created now to capture the
|
|
// DidCreateDocumentElement event.
|
|
new ElectronApiServiceImpl(render_frame, this);
|
|
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
auto* dispatcher = extensions_renderer_client_->dispatcher();
|
|
// ExtensionFrameHelper destroys itself when the RenderFrame is destroyed.
|
|
new extensions::ExtensionFrameHelper(render_frame, dispatcher);
|
|
|
|
dispatcher->OnRenderFrameCreated(render_frame);
|
|
|
|
render_frame->GetAssociatedInterfaceRegistry()
|
|
->AddInterface<extensions::mojom::MimeHandlerViewContainerManager>(
|
|
base::BindRepeating(
|
|
&extensions::MimeHandlerViewContainerManager::BindReceiver,
|
|
base::Unretained(render_frame)));
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
if (render_frame->GetBlinkPreferences().enable_spellcheck) {
|
|
new SpellCheckProvider(render_frame, spellcheck_.get());
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
|
|
void RendererClientBase::GetInterface(
|
|
const std::string& interface_name,
|
|
mojo::ScopedMessagePipeHandle interface_pipe) {
|
|
// TODO(crbug.com/977637): Get rid of the use of this implementation of
|
|
// |service_manager::LocalInterfaceProvider|. This was done only to avoid
|
|
// churning spellcheck code while eliminating the "chrome" and
|
|
// "chrome_renderer" services. Spellcheck is (and should remain) the only
|
|
// consumer of this implementation.
|
|
content::RenderThread::Get()->BindHostReceiver(
|
|
mojo::GenericPendingReceiver(interface_name, std::move(interface_pipe)));
|
|
}
|
|
#endif
|
|
|
|
void RendererClientBase::DidCreateScriptContext(
|
|
v8::Isolate* isolate,
|
|
v8::Local<v8::Context> context,
|
|
content::RenderFrame* render_frame) {
|
|
RegisterOomStackTraceCallback(isolate);
|
|
}
|
|
|
|
void RendererClientBase::DidClearWindowObject(
|
|
content::RenderFrame* render_frame) {
|
|
// Make sure every page will get a script context created.
|
|
render_frame->GetWebFrame()->ExecuteScript(blink::WebScriptSource("void 0"));
|
|
}
|
|
|
|
bool RendererClientBase::OverrideCreatePlugin(
|
|
content::RenderFrame* render_frame,
|
|
const blink::WebPluginParams& params,
|
|
blink::WebPlugin** plugin) {
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
if (params.mime_type.Utf8() == pdf::kInternalPluginMimeType) {
|
|
*plugin = pdf::CreateInternalPlugin(std::move(params), render_frame, {});
|
|
return true;
|
|
}
|
|
#endif // BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
|
|
if (params.mime_type.Utf8() == content::kBrowserPluginMimeType ||
|
|
render_frame->GetBlinkPreferences().enable_plugins)
|
|
return false;
|
|
|
|
*plugin = nullptr;
|
|
return true;
|
|
}
|
|
|
|
void RendererClientBase::DidSetUserAgent(const std::string& user_agent) {
|
|
#if BUILDFLAG(ENABLE_PRINTING)
|
|
printing::SetAgent(user_agent);
|
|
#endif
|
|
}
|
|
|
|
bool RendererClientBase::IsPluginHandledExternally(
|
|
content::RenderFrame* render_frame,
|
|
const blink::WebElement& plugin_element,
|
|
const GURL& original_url,
|
|
const std::string& mime_type) {
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
|
DCHECK(plugin_element.HasHTMLTagName("object") ||
|
|
plugin_element.HasHTMLTagName("embed"));
|
|
|
|
mojo::AssociatedRemote<mojom::ElectronPluginInfoHost> plugin_info_host;
|
|
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
|
|
&plugin_info_host);
|
|
mojom::PluginInfoPtr plugin_info = mojom::PluginInfo::New();
|
|
plugin_info_host->GetPluginInfo(
|
|
original_url, render_frame->GetWebFrame()->Top()->GetSecurityOrigin(),
|
|
mime_type, &plugin_info);
|
|
|
|
if (plugin_info->actual_mime_type == pdf::kInternalPluginMimeType) {
|
|
if (IsPdfInternalPluginAllowedOrigin(
|
|
render_frame->GetWebFrame()->GetSecurityOrigin(), {})) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return extensions::MimeHandlerViewContainerManager::Get(
|
|
content::RenderFrame::FromWebFrame(
|
|
plugin_element.GetDocument().GetFrame()),
|
|
true /* create_if_does_not_exist */)
|
|
->CreateFrameContainer(plugin_element, original_url,
|
|
plugin_info->actual_mime_type,
|
|
plugin_info->plugin);
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
v8::Local<v8::Object> RendererClientBase::GetScriptableObject(
|
|
const blink::WebElement& plugin_element,
|
|
v8::Isolate* isolate) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
// If there is a MimeHandlerView that can provide the scriptable object then
|
|
// MaybeCreateMimeHandlerView must have been called before and a container
|
|
// manager should exist.
|
|
auto* container_manager = extensions::MimeHandlerViewContainerManager::Get(
|
|
content::RenderFrame::FromWebFrame(
|
|
plugin_element.GetDocument().GetFrame()),
|
|
false /* create_if_does_not_exist */);
|
|
if (container_manager)
|
|
return container_manager->GetScriptableObject(plugin_element, isolate);
|
|
#endif
|
|
return {};
|
|
}
|
|
|
|
std::unique_ptr<blink::WebPrescientNetworking>
|
|
RendererClientBase::CreatePrescientNetworking(
|
|
content::RenderFrame* render_frame) {
|
|
return std::make_unique<network_hints::WebPrescientNetworkingImpl>(
|
|
render_frame);
|
|
}
|
|
|
|
void RendererClientBase::RunScriptsAtDocumentStart(
|
|
content::RenderFrame* render_frame) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_.get()->RunScriptsAtDocumentStart(render_frame);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::RunScriptsAtDocumentIdle(
|
|
content::RenderFrame* render_frame) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_.get()->RunScriptsAtDocumentIdle(render_frame);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::RunScriptsAtDocumentEnd(
|
|
content::RenderFrame* render_frame) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_.get()->RunScriptsAtDocumentEnd(render_frame);
|
|
#endif
|
|
}
|
|
|
|
bool RendererClientBase::AllowScriptExtensionForServiceWorker(
|
|
const url::Origin& script_origin) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
return script_origin.scheme() == extensions::kExtensionScheme;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::DidInitializeServiceWorkerContextOnWorkerThread(
|
|
blink::WebServiceWorkerContextProxy* context_proxy,
|
|
const GURL& service_worker_scope,
|
|
const GURL& script_url) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_->dispatcher()
|
|
->DidInitializeServiceWorkerContextOnWorkerThread(
|
|
context_proxy, service_worker_scope, script_url);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::WillEvaluateServiceWorkerOnWorkerThread(
|
|
blink::WebServiceWorkerContextProxy* context_proxy,
|
|
v8::Isolate* const v8_isolate,
|
|
v8::Local<v8::Context> v8_context,
|
|
int64_t service_worker_version_id,
|
|
const GURL& service_worker_scope,
|
|
const GURL& script_url,
|
|
const blink::ServiceWorkerToken& service_worker_token) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_->dispatcher()
|
|
->WillEvaluateServiceWorkerOnWorkerThread(
|
|
context_proxy, v8_isolate, v8_context, service_worker_version_id,
|
|
service_worker_scope, script_url, service_worker_token);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::DidStartServiceWorkerContextOnWorkerThread(
|
|
int64_t service_worker_version_id,
|
|
const GURL& service_worker_scope,
|
|
const GURL& script_url,
|
|
const blink::ServiceWorkerToken& service_worker_token) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_->dispatcher()
|
|
->DidStartServiceWorkerContextOnWorkerThread(
|
|
service_worker_version_id, service_worker_scope, script_url,
|
|
service_worker_token);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::WillDestroyServiceWorkerContextOnWorkerThread(
|
|
v8::Local<v8::Context> context,
|
|
int64_t service_worker_version_id,
|
|
const GURL& service_worker_scope,
|
|
const GURL& script_url,
|
|
const blink::ServiceWorkerToken& service_worker_token) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
extensions_renderer_client_->dispatcher()
|
|
->WillDestroyServiceWorkerContextOnWorkerThread(
|
|
context, service_worker_version_id, service_worker_scope, script_url,
|
|
service_worker_token);
|
|
#endif
|
|
}
|
|
|
|
void RendererClientBase::WorkerScriptReadyForEvaluationOnWorkerThread(
|
|
v8::Local<v8::Context> context) {
|
|
// Worklets can share a thread and isolate (via WorkletThreadHolder), so the
|
|
// per-thread OOM state would be prematurely removed when the first worklet
|
|
// is destroyed. Skip worklets for now; can be revisited with ref-counting.
|
|
if (blink::ExecutionContext::From(context)->IsWorkletGlobalScope())
|
|
return;
|
|
RegisterOomStackTraceCallback(v8::Isolate::GetCurrent());
|
|
}
|
|
|
|
void RendererClientBase::WillDestroyWorkerContextOnWorkerThread(
|
|
v8::Local<v8::Context> context) {
|
|
if (blink::ExecutionContext::From(context)->IsWorkletGlobalScope())
|
|
return;
|
|
UnregisterOomStackTraceCallback(v8::Isolate::GetCurrent());
|
|
}
|
|
|
|
void RendererClientBase::WebViewCreated(blink::WebView* web_view,
|
|
bool was_created_by_renderer,
|
|
const url::Origin* outermost_origin) {
|
|
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
|
|
new extensions::ExtensionWebViewHelper(web_view, outermost_origin);
|
|
#endif
|
|
}
|
|
|
|
v8::Local<v8::Context> RendererClientBase::GetContext(
|
|
blink::WebLocalFrame* frame,
|
|
v8::Isolate* isolate) const {
|
|
auto* render_frame = content::RenderFrame::FromWebFrame(frame);
|
|
DCHECK(render_frame);
|
|
if (render_frame && render_frame->GetBlinkPreferences().context_isolation)
|
|
return frame->GetScriptContextFromWorldId(isolate,
|
|
WorldIDs::ISOLATED_WORLD_ID);
|
|
else
|
|
return frame->MainWorldScriptContext();
|
|
}
|
|
|
|
bool RendererClientBase::IsWebViewFrame(
|
|
v8::Isolate* const isolate,
|
|
v8::Local<v8::Context> context,
|
|
content::RenderFrame* render_frame) const {
|
|
if (render_frame->IsMainFrame())
|
|
return false;
|
|
|
|
gin::Dictionary window_dict(
|
|
isolate, GetContext(render_frame->GetWebFrame(), isolate)->Global());
|
|
|
|
v8::Local<v8::Object> frame_element;
|
|
if (!window_dict.Get("frameElement", &frame_element))
|
|
return false;
|
|
|
|
gin_helper::Dictionary frame_element_dict(isolate, frame_element);
|
|
|
|
bool is_webview = false;
|
|
return frame_element_dict.GetHidden("isWebView", &is_webview) && is_webview;
|
|
}
|
|
|
|
void RendererClientBase::SetupMainWorldOverrides(
|
|
v8::Isolate* const isolate,
|
|
v8::Local<v8::Context> context,
|
|
content::RenderFrame* render_frame) {
|
|
auto prefs = render_frame->GetBlinkPreferences();
|
|
// We only need to run the isolated bundle if webview is enabled
|
|
if (!prefs.webview_tag)
|
|
return;
|
|
|
|
// Setup window overrides in the main world context
|
|
// Wrap the bundle into a function that receives the isolatedApi as
|
|
// an argument.
|
|
v8::HandleScope handle_scope{isolate};
|
|
v8::Context::Scope context_scope{context};
|
|
|
|
auto isolated_api = gin_helper::Dictionary::CreateEmpty(isolate);
|
|
isolated_api.SetMethod("allowGuestViewElementDefinition",
|
|
&AllowGuestViewElementDefinition);
|
|
isolated_api.SetMethod("setIsWebView", &SetIsWebView);
|
|
|
|
auto source_context = GetContext(render_frame->GetWebFrame(), isolate);
|
|
gin_helper::Dictionary global(isolate, source_context->Global());
|
|
|
|
v8::Local<v8::Value> guest_view_internal;
|
|
if (global.GetHidden("guestViewInternal", &guest_view_internal)) {
|
|
auto result = api::PassValueToOtherContext(
|
|
isolate, source_context, isolate, context, guest_view_internal,
|
|
source_context->Global(), false, api::BridgeErrorTarget::kSource);
|
|
if (!result.IsEmpty()) {
|
|
isolated_api.Set("guestViewInternal", result.ToLocalChecked());
|
|
}
|
|
}
|
|
|
|
v8::LocalVector<v8::String> isolated_bundle_params(
|
|
isolate, {node::FIXED_ONE_BYTE_STRING(isolate, "isolatedApi")});
|
|
|
|
v8::LocalVector<v8::Value> isolated_bundle_args(isolate,
|
|
{isolated_api.GetHandle()});
|
|
|
|
util::CompileAndCall(isolate, context, "electron/js2c/isolated_bundle",
|
|
&isolated_bundle_params, &isolated_bundle_args);
|
|
}
|
|
|
|
// static
|
|
void RendererClientBase::AllowGuestViewElementDefinition(
|
|
v8::Isolate* isolate,
|
|
v8::Local<v8::Object> context,
|
|
v8::Local<v8::Function> register_cb) {
|
|
v8::HandleScope handle_scope(isolate);
|
|
v8::Context::Scope context_scope(context->GetCreationContextChecked(isolate));
|
|
blink::WebCustomElement::EmbedderNamesAllowedScope embedder_names_scope;
|
|
|
|
content::RenderFrame* render_frame = GetRenderFrame(isolate, context);
|
|
if (!render_frame)
|
|
return;
|
|
|
|
render_frame->GetWebFrame()->RequestExecuteV8Function(
|
|
context->GetCreationContextChecked(isolate), register_cb,
|
|
v8::Null(isolate), 0, nullptr, base::NullCallback());
|
|
}
|
|
|
|
} // namespace electron
|