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 "base/macros.h" 16 #include "content/public/browser/browser_main_runner.h" 17 18 namespace base { 19 class WaitableEvent; 20 } 21 22 namespace content { 23 class ContentMainRunner; 24 struct ContentMainParams; 25 } // namespace content 26 27 class CefUIThread; 28 29 // Manages the main process lifespan and related objects. 30 class CefMainRunner : public CefMainRunnerHandler { 31 public: 32 CefMainRunner(bool multi_threaded_message_loop, bool external_message_pump); 33 ~CefMainRunner(); 34 35 // Called from CefContext::Initialize. 36 bool Initialize(CefSettings* settings, 37 CefRefPtr<CefApp> application, 38 const CefMainArgs& args, 39 void* windows_sandbox_info, 40 bool* initialized, 41 base::OnceClosure context_initialized); 42 43 // Called from CefContext::Shutdown. 44 void Shutdown(base::OnceClosure shutdown_on_ui_thread, 45 base::OnceClosure finalize_shutdown); 46 47 void RunMessageLoop(); 48 void QuitMessageLoop(); 49 50 // Called from CefExecuteProcess. 51 static int RunAsHelperProcess(const CefMainArgs& args, 52 CefRefPtr<CefApp> application, 53 void* windows_sandbox_info); 54 55 private: 56 // Called from Initialize(). 57 int ContentMainInitialize(const CefMainArgs& args, 58 void* windows_sandbox_info, 59 int* no_sandbox); 60 bool ContentMainRun(bool* initialized, base::OnceClosure context_initialized); 61 62 // CefMainRunnerHandler methods: 63 void PreCreateMainMessageLoop() override; 64 int RunMainProcess( 65 const content::MainFunctionParams& main_function_params) override; 66 67 // Create the UI thread when running with multi-threaded message loop mode. 68 bool CreateUIThread(base::OnceClosure setup_callback); 69 70 // Called on the UI thread after the context is initialized. 71 void OnContextInitialized(base::OnceClosure context_initialized); 72 73 // Performs shutdown actions that need to occur on the UI thread before any 74 // threads are destroyed. 75 void FinishShutdownOnUIThread(base::OnceClosure shutdown_on_ui_thread, 76 base::WaitableEvent* uithread_shutdown_event); 77 78 // Destroys the runtime and related objects. 79 void FinalizeShutdown(base::OnceClosure finalize_shutdown); 80 81 const bool multi_threaded_message_loop_; 82 const bool external_message_pump_; 83 84 std::unique_ptr<CefMainRunnerDelegate> main_delegate_; 85 std::unique_ptr<content::ContentMainRunner> main_runner_; 86 std::unique_ptr<content::ContentMainParams> main_params_; 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 DISALLOW_COPY_AND_ASSIGN(CefMainRunner); 95 }; 96 97 #endif // CEF_LIBCEF_BROWSER_MAIN_RUNNER_H_ 98