1 // Copyright 2020 The Chromium Embedded Framework Authors. 2 // Portions copyright 2014 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 #ifndef CEF_LIBCEF_BROWSER_MAIN_RUNNER_H_ 7 #define CEF_LIBCEF_BROWSER_MAIN_RUNNER_H_ 8 #pragma once 9 10 #include "include/cef_app.h" 11 #include "libcef/common/main_runner_delegate.h" 12 #include "libcef/common/main_runner_handler.h" 13 14 #include "base/callback.h" 15 #include "content/public/browser/browser_main_runner.h" 16 17 namespace base { 18 class WaitableEvent; 19 } 20 21 namespace content { 22 class ContentMainRunner; 23 } // namespace content 24 25 class CefUIThread; 26 27 // Manages the main process lifespan and related objects. 28 class CefMainRunner : public CefMainRunnerHandler { 29 public: 30 CefMainRunner(bool multi_threaded_message_loop, bool external_message_pump); 31 32 CefMainRunner(const CefMainRunner&) = delete; 33 CefMainRunner& operator=(const CefMainRunner&) = delete; 34 35 ~CefMainRunner(); 36 37 // Called from CefContext::Initialize. 38 bool Initialize(CefSettings* settings, 39 CefRefPtr<CefApp> application, 40 const CefMainArgs& args, 41 void* windows_sandbox_info, 42 bool* initialized, 43 base::OnceClosure context_initialized); 44 45 // Called from CefContext::Shutdown. 46 void Shutdown(base::OnceClosure shutdown_on_ui_thread, 47 base::OnceClosure finalize_shutdown); 48 49 void RunMessageLoop(); 50 void QuitMessageLoop(); 51 52 // Called from CefExecuteProcess. 53 static int RunAsHelperProcess(const CefMainArgs& args, 54 CefRefPtr<CefApp> application, 55 void* windows_sandbox_info); 56 57 private: 58 // Called from Initialize(). 59 int ContentMainInitialize(const CefMainArgs& args, 60 void* windows_sandbox_info, 61 int* no_sandbox); 62 bool ContentMainRun(bool* initialized, base::OnceClosure context_initialized); 63 64 // CefMainRunnerHandler methods: 65 void PreBrowserMain() override; 66 int RunMainProcess(content::MainFunctionParams main_function_params) override; 67 68 // Create the UI thread when running with multi-threaded message loop mode. 69 bool CreateUIThread(base::OnceClosure setup_callback); 70 71 // Called on the UI thread after the context is initialized. 72 void OnContextInitialized(base::OnceClosure context_initialized); 73 74 // Performs shutdown actions that need to occur on the UI thread before any 75 // threads are destroyed. 76 void FinishShutdownOnUIThread(base::OnceClosure shutdown_on_ui_thread, 77 base::WaitableEvent* uithread_shutdown_event); 78 79 // Destroys the runtime and related objects. 80 void FinalizeShutdown(base::OnceClosure finalize_shutdown); 81 82 const bool multi_threaded_message_loop_; 83 const bool external_message_pump_; 84 85 std::unique_ptr<CefMainRunnerDelegate> main_delegate_; 86 std::unique_ptr<content::ContentMainRunner> main_runner_; 87 88 std::unique_ptr<content::BrowserMainRunner> browser_runner_; 89 std::unique_ptr<CefUIThread> ui_thread_; 90 91 // Used to quit the current base::RunLoop. 92 base::OnceClosure quit_when_idle_callback_; 93 }; 94 95 #endif // CEF_LIBCEF_BROWSER_MAIN_RUNNER_H_ 96