Skip to content

Commit

Permalink
fix: set up performance_manager in electron shell
Browse files Browse the repository at this point in the history
This allows usage of performance.measureUserAgentSpecificMemory.

Fixes #37147
  • Loading branch information
bouk committed May 24, 2024
1 parent 7ec8137 commit d867d9d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ source_set("electron_lib") {
"//components/os_crypt/async/browser",
"//components/os_crypt/async/browser:key_provider_interface",
"//components/os_crypt/sync",
"//components/performance_manager",
"//components/pref_registry",
"//components/prefs",
"//components/security_state/content",
Expand Down
Empty file added patches/blink/.patches
Empty file.
1 change: 1 addition & 0 deletions patches/chromium/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ refactor_expose_file_system_access_blocklist.patch
partially_revert_is_newly_created_to_allow_for_browser_initiated.patch
fix_use_app_launch_prefetch_namespace_for_subprocesstype.patch
feat_add_support_for_missing_dialog_features_to_shell_dialogs.patch
remove_v8_worker_memory_single_check.patch
22 changes: 22 additions & 0 deletions patches/chromium/remove_v8_worker_memory_single_check.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bouke van der Bijl <i@bou.ke>
Date: Fri, 24 May 2024 10:32:10 +0200
Subject: Remove check that there's only one V8 context in a worker

Electron has multiple because of the Node.js isolate.

diff --git a/third_party/blink/renderer/controller/performance_manager/v8_worker_memory_reporter.cc b/third_party/blink/renderer/controller/performance_manager/v8_worker_memory_reporter.cc
index 0cad8137efb2a8f2ae7e60bc1c0422ba00bcf547..abde7739103cc92eb9b9c8e82bb4b48e57e2dbaf 100644
--- a/third_party/blink/renderer/controller/performance_manager/v8_worker_memory_reporter.cc
+++ b/third_party/blink/renderer/controller/performance_manager/v8_worker_memory_reporter.cc
@@ -94,8 +94,8 @@ void WorkerMeasurementDelegate::MeasurementComplete(
DCHECK(worker_thread_->IsCurrentThread());
WorkerOrWorkletGlobalScope* global_scope = worker_thread_->GlobalScope();
DCHECK(global_scope);
- DCHECK_LE(result.contexts.size(), 1u);
- DCHECK_LE(result.sizes_in_bytes.size(), 1u);
+ // DCHECK_LE(result.contexts.size(), 1u);
+ // DCHECK_LE(result.sizes_in_bytes.size(), 1u);
size_t bytes = result.unattributed_size_in_bytes;
for (size_t size : result.sizes_in_bytes) {
bytes += size;
16 changes: 16 additions & 0 deletions shell/browser/electron_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "components/embedder_support/user_agent_utils.h"
#include "components/net_log/chrome_net_log.h"
#include "components/network_hints/common/network_hints.mojom.h"
#include "components/performance_manager/embedder/performance_manager_registry.h"
#include "content/browser/keyboard_lock/keyboard_lock_service_impl.h" // nogncheck
#include "content/browser/site_instance_impl.h" // nogncheck
#include "content/public/browser/browser_main_runner.h"
Expand All @@ -49,6 +50,7 @@
#include "content/public/browser/tts_platform.h"
#include "content/public/browser/url_loader_request_interceptor.h"
#include "content/public/browser/weak_document_ptr.h"
#include "content/public/browser/web_contents_view_delegate.h"
#include "content/public/common/content_descriptors.h"
#include "content/public/common/content_paths.h"
#include "content/public/common/content_switches.h"
Expand Down Expand Up @@ -1563,6 +1565,9 @@ void ElectronBrowserClient::ExposeInterfacesToRenderer(
service_manager::BinderRegistry* registry,
blink::AssociatedInterfaceRegistry* associated_registry,
content::RenderProcessHost* render_process_host) {
performance_manager::PerformanceManagerRegistry::GetInstance()
->CreateProcessNodeAndExposeInterfacesToRendererProcess(
registry, render_process_host);
#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
associated_registry->AddInterface<extensions::mojom::RendererHost>(
base::BindRepeating(&extensions::RendererStartupHelper::BindForRenderer,
Expand All @@ -1573,6 +1578,8 @@ void ElectronBrowserClient::ExposeInterfacesToRenderer(
void ElectronBrowserClient::RegisterBrowserInterfaceBindersForFrame(
content::RenderFrameHost* render_frame_host,
mojo::BinderMapWithContext<content::RenderFrameHost*>* map) {
performance_manager::PerformanceManagerRegistry::GetInstance()
->ExposeInterfacesToRenderFrame(map);
map->Add<network_hints::mojom::NetworkHintsHandler>(
base::BindRepeating(&BindNetworkHintsHandler));
map->Add<blink::mojom::BadgeService>(
Expand Down Expand Up @@ -1719,6 +1726,15 @@ void ElectronBrowserClient::RegisterBrowserInterfaceBindersForServiceWorker(
base::BindRepeating(&BindBadgeServiceForServiceWorker));
}

std::unique_ptr<content::WebContentsViewDelegate>
ElectronBrowserClient::GetWebContentsViewDelegate(
content::WebContents* web_contents) {
performance_manager::PerformanceManagerRegistry::GetInstance()
->MaybeCreatePageNodeForWebContents(web_contents);
return content::ContentBrowserClient::GetWebContentsViewDelegate(
web_contents);
}

#if BUILDFLAG(IS_MAC)
device::GeolocationSystemPermissionManager*
ElectronBrowserClient::GetGeolocationSystemPermissionManager() {
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/electron_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class ElectronBrowserClient : public content::ContentBrowserClient,
const content::ServiceWorkerVersionBaseInfo& service_worker_version_info,
mojo::BinderMapWithContext<const content::ServiceWorkerVersionBaseInfo&>*
map) override;
std::unique_ptr<content::WebContentsViewDelegate> GetWebContentsViewDelegate(
content::WebContents* web_contents) override;
#if BUILDFLAG(IS_LINUX)
void GetAdditionalMappedFilesForChildProcess(
const base::CommandLine& command_line,
Expand Down
7 changes: 7 additions & 0 deletions shell/browser/electron_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "components/os_crypt/sync/key_storage_util_linux.h"
#include "components/os_crypt/sync/os_crypt.h"
#include "components/password_manager/core/browser/password_manager_switches.h" // nogncheck
#include "components/performance_manager/embedder/graph_features.h"
#include "components/performance_manager/embedder/performance_manager_lifetime.h"
#include "content/browser/browser_main_loop.h" // nogncheck
#include "content/public/browser/browser_child_process_host_delegate.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
Expand Down Expand Up @@ -364,6 +366,9 @@ int ElectronBrowserMainParts::PreCreateThreads() {
}

void ElectronBrowserMainParts::PostCreateThreads() {
performance_manager_lifetime_ =
std::make_unique<performance_manager::PerformanceManagerLifetime>(
performance_manager::GraphFeatures::WithMinimal(), base::DoNothing());
content::GetIOThreadTaskRunner({})->PostTask(
FROM_HERE,
base::BindOnce(&tracing::TracingSamplerProfiler::CreateOnChildThread));
Expand Down Expand Up @@ -610,6 +615,8 @@ void ElectronBrowserMainParts::PostMainMessageLoopRun() {
#if BUILDFLAG(IS_LINUX)
ui::OzonePlatform::GetInstance()->PostMainMessageLoopRun();
#endif

performance_manager_lifetime_.reset();
}

#if !BUILDFLAG(IS_MAC)
Expand Down
7 changes: 7 additions & 0 deletions shell/browser/electron_browser_main_parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "ui/display/screen.h"
#include "ui/views/layout/layout_provider.h"

namespace performance_manager {
class PerformanceManagerLifetime;
} // namespace performance_manager

class BrowserProcessImpl;
class IconManager;

Expand Down Expand Up @@ -172,6 +176,9 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {
std::unique_ptr<IconManager> icon_manager_;
std::unique_ptr<base::FieldTrialList> field_trial_list_;

std::unique_ptr<performance_manager::PerformanceManagerLifetime>
performance_manager_lifetime_;

#if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
std::unique_ptr<ElectronExtensionsClient> extensions_client_;
std::unique_ptr<ElectronExtensionsBrowserClient> extensions_browser_client_;
Expand Down

0 comments on commit d867d9d

Please sign in to comment.