Skip to content

Commit

Permalink
fix: UtilityProcess.fork before app ready
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 22, 2024
1 parent 707b9a5 commit 63411ec
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 7 deletions.
4 changes: 2 additions & 2 deletions shell/browser/browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ BuildState* BrowserProcessImpl::GetBuildState() {
return nullptr;
}

void BrowserProcessImpl::PostEarlyInitialization() {
void BrowserProcessImpl::PreEarlyInitialization() {
PrefServiceFactory prefs_factory;
auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get());
Expand Down Expand Up @@ -126,7 +126,7 @@ void BrowserProcessImpl::PreCreateThreads() {
// Must be created before the IOThread.
// Once IOThread class is no longer needed,
// this can be created on first use.
if (!SystemNetworkContextManager::GetInstance())
if (!SystemNetworkContextManager::HasInstance())
SystemNetworkContextManager::CreateInstance(local_state_.get());
}

Expand Down
2 changes: 1 addition & 1 deletion shell/browser/browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BrowserProcessImpl : public BrowserProcess {
static void ApplyProxyModeFromCommandLine(ValueMapPrefStore* pref_store);

BuildState* GetBuildState() override;
void PostEarlyInitialization();
void PreEarlyInitialization();
void PreCreateThreads();
void PreMainMessageLoopRun();
void PostDestroyThreads() {}
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/electron_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,10 @@ void ElectronBrowserClient::OnNetworkServiceCreated(
if (!g_browser_process)
return;

if (!SystemNetworkContextManager::HasInstance())
SystemNetworkContextManager::CreateInstance(
g_browser_process->local_state());

g_browser_process->system_network_context_manager()->OnNetworkServiceCreated(
network_service);
}
Expand Down
6 changes: 3 additions & 3 deletions shell/browser/electron_browser_main_parts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ int ElectronBrowserMainParts::PreEarlyInitialization() {
ui::ColorProviderManager::Get().AppendColorProviderInitializer(
base::BindRepeating(AddChromeColorMixers));

// Initialize after user script environment creation.
fake_browser_process_->PreEarlyInitialization();

return GetExitCode();
}

Expand Down Expand Up @@ -276,9 +279,6 @@ void ElectronBrowserMainParts::PostEarlyInitialization() {
// and/or user data directory.
logging::InitElectronLogging(*base::CommandLine::ForCurrentProcess(),
/* is_preinit = */ false);

// Initialize after user script environment creation.
fake_browser_process_->PostEarlyInitialization();
}

int ElectronBrowserMainParts::PreCreateThreads() {
Expand Down
13 changes: 13 additions & 0 deletions shell/browser/net/system_network_context_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,28 @@ SystemNetworkContextManager* SystemNetworkContextManager::CreateInstance(
return g_system_network_context_manager;
}

// static
bool SystemNetworkContextManager::HasInstance() {
return !!g_system_network_context_manager;
}

// static
SystemNetworkContextManager* SystemNetworkContextManager::GetInstance() {
if (!g_system_network_context_manager) {
// Initialize the network service, which will trigger
// ElectronBrowserClient::OnNetworkServiceCreated(), which calls
// CreateInstance() to initialize |g_system_network_context_manager|.
content::GetNetworkService();
}

return g_system_network_context_manager;
}

// static
void SystemNetworkContextManager::DeleteInstance() {
DCHECK(g_system_network_context_manager);
delete g_system_network_context_manager;
g_system_network_context_manager = nullptr;
}

// c.f.
Expand Down
7 changes: 6 additions & 1 deletion shell/browser/net/system_network_context_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ class SystemNetworkContextManager {
SystemNetworkContextManager& operator=(const SystemNetworkContextManager&) =
delete;

// Checks if the global SystemNetworkContextManager has been created.
static bool HasInstance();

// Creates the global instance of SystemNetworkContextManager. If an
// instance already exists, this will cause a DCHECK failure.
static SystemNetworkContextManager* CreateInstance(PrefService* pref_service);

// Gets the global SystemNetworkContextManager instance.
// Gets the global SystemNetworkContextManager instance. If it has not been
// created yet, NetworkService is called, which will cause the
// SystemNetworkContextManager to be created.
static SystemNetworkContextManager* GetInstance();

// Destroys the global SystemNetworkContextManager instance.
Expand Down

0 comments on commit 63411ec

Please sign in to comment.