1 // Copyright 2020 The Chromium Embedded Framework Authors.
2 // Portions copyright 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5
6 #include "libcef/common/chrome/chrome_main_runner_delegate.h"
7
8 #include "libcef/common/chrome/chrome_main_delegate_cef.h"
9
10 #include "base/command_line.h"
11 #include "base/run_loop.h"
12 #include "chrome/browser/browser_process_impl.h"
13 #include "chrome/common/profiler/main_thread_stack_sampling_profiler.h"
14 #include "components/keep_alive_registry/keep_alive_types.h"
15 #include "components/keep_alive_registry/scoped_keep_alive.h"
16
ChromeMainRunnerDelegate(CefMainRunnerHandler * runner,CefSettings * settings,CefRefPtr<CefApp> application)17 ChromeMainRunnerDelegate::ChromeMainRunnerDelegate(
18 CefMainRunnerHandler* runner,
19 CefSettings* settings,
20 CefRefPtr<CefApp> application)
21 : runner_(runner), settings_(settings), application_(application) {}
22
23 ChromeMainRunnerDelegate::~ChromeMainRunnerDelegate() = default;
24
25 content::ContentMainDelegate*
GetContentMainDelegate()26 ChromeMainRunnerDelegate::GetContentMainDelegate() {
27 if (!main_delegate_) {
28 main_delegate_ = std::make_unique<ChromeMainDelegateCef>(runner_, settings_,
29 application_);
30 }
31 return main_delegate_.get();
32 }
33
BeforeMainThreadInitialize(const CefMainArgs & args)34 void ChromeMainRunnerDelegate::BeforeMainThreadInitialize(
35 const CefMainArgs& args) {
36 #if BUILDFLAG(IS_WIN)
37 base::CommandLine::Init(0, nullptr);
38 #else
39 base::CommandLine::Init(args.argc, args.argv);
40 #endif
41
42 sampling_profiler_ = std::make_unique<MainThreadStackSamplingProfiler>();
43 }
44
BeforeMainMessageLoopRun(base::RunLoop * run_loop)45 void ChromeMainRunnerDelegate::BeforeMainMessageLoopRun(
46 base::RunLoop* run_loop) {
47 // The ScopedKeepAlive instance triggers shutdown logic when released on the
48 // UI thread before terminating the message loop (e.g. from CefQuitMessageLoop
49 // or FinishShutdownOnUIThread when running with multi-threaded message loop).
50 keep_alive_ = std::make_unique<ScopedKeepAlive>(
51 KeepAliveOrigin::APP_CONTROLLER, KeepAliveRestartOption::DISABLED);
52
53 // The idle callback will be executed from BrowserProcessImpl::Unpin() via
54 // KeepAliveRegistry when the last ScopedKeepAlive is released.
55 // ScopedKeepAlives are also held by Browser objects.
56 DCHECK(g_browser_process);
57 static_cast<BrowserProcessImpl*>(g_browser_process)
58 ->SetQuitClosure(run_loop->QuitWhenIdleClosure());
59 }
60
HandleMainMessageLoopQuit()61 bool ChromeMainRunnerDelegate::HandleMainMessageLoopQuit() {
62 // May be called multiple times. See comments in RunMainMessageLoopBefore.
63 keep_alive_.reset();
64
65 // Cancel direct execution of the QuitWhenIdleClosure() in
66 // CefMainRunner::QuitMessageLoop. We instead wait for all Chrome browser
67 // windows to exit.
68 return true;
69 }
70
AfterMainThreadShutdown()71 void ChromeMainRunnerDelegate::AfterMainThreadShutdown() {
72 sampling_profiler_.reset();
73 }
74
BeforeExecuteProcess(const CefMainArgs & args)75 void ChromeMainRunnerDelegate::BeforeExecuteProcess(const CefMainArgs& args) {
76 BeforeMainThreadInitialize(args);
77 }
78
AfterExecuteProcess()79 void ChromeMainRunnerDelegate::AfterExecuteProcess() {
80 AfterMainThreadShutdown();
81 }
82