#include "libcef/common/chrome/chrome_main_runner_delegate.h"
#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"
#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"
ChromeMainRunnerDelegate::ChromeMainRunnerDelegate(
CefMainRunnerHandler* runner,
CefSettings* settings,
CefRefPtr<CefApp> application)
: runner_(runner), settings_(settings), application_(application) {}
ChromeMainRunnerDelegate::~ChromeMainRunnerDelegate() = default;
content::ContentMainDelegate*
ChromeMainRunnerDelegate::GetContentMainDelegate() {
if (!main_delegate_) {
main_delegate_ = std::make_unique<ChromeMainDelegateCef>(runner_, settings_,
application_);
}
return main_delegate_.get();
}
void ChromeMainRunnerDelegate::BeforeMainThreadInitialize(
const CefMainArgs& args) {
#if BUILDFLAG(IS_WIN)
base::CommandLine::Init(0, nullptr);
#else
base::CommandLine::Init(args.argc, args.argv);
#endif
sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
}
void ChromeMainRunnerDelegate::BeforeMainMessageLoopRun(
base::RunLoop* run_loop) {
keep_alive_ = std::make_unique<ScopedKeepAlive>(
KeepAliveOrigin::APP_CONTROLLER, KeepAliveRestartOption::DISABLED);
DCHECK(g_browser_process);
static_cast<BrowserProcessImpl*>(g_browser_process)
->SetQuitClosure(run_loop->QuitWhenIdleClosure());
}
bool ChromeMainRunnerDelegate::HandleMainMessageLoopQuit() {
keep_alive_.reset();
return true;
}
void ChromeMainRunnerDelegate::AfterMainThreadShutdown() {
sampling_profiler_.reset();
}
void ChromeMainRunnerDelegate::BeforeExecuteProcess(const CefMainArgs& args) {
BeforeMainThreadInitialize(args);
}
void ChromeMainRunnerDelegate::AfterExecuteProcess() {
AfterMainThreadShutdown();
}