2020-06-25 04:34:12 +02:00
|
|
|
// Copyright 2020 The Chromium Embedded Framework Authors.
|
|
|
|
// Portions copyright 2012 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/common/chrome/chrome_main_runner_delegate.h"
|
|
|
|
|
2022-03-09 20:45:39 +01:00
|
|
|
#include "libcef/common/app_manager.h"
|
2020-06-25 04:34:12 +02:00
|
|
|
#include "libcef/common/chrome/chrome_main_delegate_cef.h"
|
|
|
|
|
|
|
|
#include "base/command_line.h"
|
|
|
|
#include "base/run_loop.h"
|
|
|
|
#include "chrome/browser/browser_process_impl.h"
|
2022-03-09 20:45:39 +01:00
|
|
|
#include "chrome/browser/chrome_content_browser_client.h"
|
2023-12-11 21:36:56 +01:00
|
|
|
#include "chrome/browser/chrome_process_singleton.h"
|
2020-06-25 04:34:12 +02:00
|
|
|
#include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
|
|
|
|
#include "components/keep_alive_registry/keep_alive_types.h"
|
|
|
|
#include "components/keep_alive_registry/scoped_keep_alive.h"
|
2023-04-04 20:00:13 +02:00
|
|
|
#include "components/metrics/persistent_system_profile.h"
|
2020-06-25 04:34:12 +02:00
|
|
|
|
2020-06-28 23:05:36 +02:00
|
|
|
ChromeMainRunnerDelegate::ChromeMainRunnerDelegate(
|
|
|
|
CefMainRunnerHandler* runner,
|
2020-07-06 20:14:57 +02:00
|
|
|
CefSettings* settings,
|
2020-06-28 23:05:36 +02:00
|
|
|
CefRefPtr<CefApp> application)
|
2020-07-06 20:14:57 +02:00
|
|
|
: runner_(runner), settings_(settings), application_(application) {}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
ChromeMainRunnerDelegate::~ChromeMainRunnerDelegate() = default;
|
|
|
|
|
|
|
|
content::ContentMainDelegate*
|
|
|
|
ChromeMainRunnerDelegate::GetContentMainDelegate() {
|
|
|
|
if (!main_delegate_) {
|
2020-07-06 20:14:57 +02:00
|
|
|
main_delegate_ = std::make_unique<ChromeMainDelegateCef>(runner_, settings_,
|
|
|
|
application_);
|
2020-06-25 04:34:12 +02:00
|
|
|
}
|
|
|
|
return main_delegate_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeMainRunnerDelegate::BeforeMainThreadInitialize(
|
|
|
|
const CefMainArgs& args) {
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2020-06-25 04:34:12 +02:00
|
|
|
base::CommandLine::Init(0, nullptr);
|
|
|
|
#else
|
|
|
|
base::CommandLine::Init(args.argc, args.argv);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-04-04 20:00:13 +02:00
|
|
|
void ChromeMainRunnerDelegate::BeforeMainThreadRun(
|
|
|
|
bool multi_threaded_message_loop) {
|
|
|
|
if (multi_threaded_message_loop) {
|
2023-12-11 21:36:56 +01:00
|
|
|
multi_threaded_message_loop_ = true;
|
|
|
|
|
2023-04-04 20:00:13 +02:00
|
|
|
// Detach from the main thread so that these objects can be attached and
|
|
|
|
// modified from the UI thread going forward.
|
|
|
|
metrics::GlobalPersistentSystemProfile::GetInstance()
|
|
|
|
->DetachFromCurrentThread();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
void ChromeMainRunnerDelegate::BeforeMainMessageLoopRun(
|
|
|
|
base::RunLoop* run_loop) {
|
2024-01-30 22:33:39 +01:00
|
|
|
// May be nullptr if content::ContentMainRun exits early.
|
|
|
|
if (!g_browser_process) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
// The ScopedKeepAlive instance triggers shutdown logic when released on the
|
|
|
|
// UI thread before terminating the message loop (e.g. from CefQuitMessageLoop
|
|
|
|
// or FinishShutdownOnUIThread when running with multi-threaded message loop).
|
|
|
|
keep_alive_ = std::make_unique<ScopedKeepAlive>(
|
|
|
|
KeepAliveOrigin::APP_CONTROLLER, KeepAliveRestartOption::DISABLED);
|
|
|
|
|
2023-07-11 08:41:47 +02:00
|
|
|
// The QuitClosure will be executed from BrowserProcessImpl::Unpin() via
|
2020-06-25 04:34:12 +02:00
|
|
|
// KeepAliveRegistry when the last ScopedKeepAlive is released.
|
|
|
|
// ScopedKeepAlives are also held by Browser objects.
|
|
|
|
static_cast<BrowserProcessImpl*>(g_browser_process)
|
2023-07-11 08:41:47 +02:00
|
|
|
->SetQuitClosure(run_loop->QuitClosure());
|
2020-06-25 04:34:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 04:51:17 +02:00
|
|
|
bool ChromeMainRunnerDelegate::HandleMainMessageLoopQuit() {
|
2024-01-30 22:33:39 +01:00
|
|
|
// May be nullptr if content::ContentMainRun exits early.
|
|
|
|
if (!g_browser_process) {
|
|
|
|
// Proceed with direct execution of the QuitClosure().
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
// May be called multiple times. See comments in RunMainMessageLoopBefore.
|
|
|
|
keep_alive_.reset();
|
2020-07-04 04:51:17 +02:00
|
|
|
|
2023-07-11 08:41:47 +02:00
|
|
|
// Cancel direct execution of the QuitClosure() in
|
2020-07-04 04:51:17 +02:00
|
|
|
// CefMainRunner::QuitMessageLoop. We instead wait for all Chrome browser
|
|
|
|
// windows to exit.
|
|
|
|
return true;
|
2020-06-25 04:34:12 +02:00
|
|
|
}
|
|
|
|
|
2023-03-14 20:12:03 +01:00
|
|
|
void ChromeMainRunnerDelegate::BeforeUIThreadInitialize() {
|
|
|
|
sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
|
|
|
|
}
|
|
|
|
|
2023-12-11 21:36:56 +01:00
|
|
|
void ChromeMainRunnerDelegate::BeforeUIThreadShutdown() {
|
2022-03-09 20:45:39 +01:00
|
|
|
static_cast<ChromeContentBrowserClient*>(
|
|
|
|
CefAppManager::Get()->GetContentClient()->browser())
|
|
|
|
->CleanupOnUIThread();
|
2022-09-29 18:37:59 +02:00
|
|
|
main_delegate_->CleanupOnUIThread();
|
2022-03-09 20:45:39 +01:00
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
sampling_profiler_.reset();
|
|
|
|
}
|
|
|
|
|
2023-12-11 21:36:56 +01:00
|
|
|
void ChromeMainRunnerDelegate::AfterUIThreadShutdown() {
|
|
|
|
if (multi_threaded_message_loop_) {
|
|
|
|
// Don't wait for this to be called in ChromeMainDelegate::ProcessExiting.
|
|
|
|
// It is safe to call multiple times.
|
|
|
|
ChromeProcessSingleton::DeleteInstance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
void ChromeMainRunnerDelegate::BeforeExecuteProcess(const CefMainArgs& args) {
|
|
|
|
BeforeMainThreadInitialize(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChromeMainRunnerDelegate::AfterExecuteProcess() {
|
|
|
|
AfterMainThreadShutdown();
|
|
|
|
}
|