#include "content/browser/browser_main_loop.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "base/allocator/partition_alloc_features.h"
#include "base/base_switches.h"
#include "base/callback_list.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/memory_pressure_monitor.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/pending_task.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_source.h"
#include "base/process/process_metrics.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/synchronization/waitable_event.h"
#include "base/system/system_monitor.h"
#include "base/task/current_thread.h"
#include "base/task/deferred_sequenced_task_runner.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool/initialization_util.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "base/timer/hi_res_timer_manager.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/chromecast_buildflags.h"
#include "build/chromeos_buildflags.h"
#include "cc/base/histograms.h"
#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "components/memory_pressure/multi_source_memory_pressure_monitor.h"
#include "components/power_monitor/make_power_monitor_device_source.h"
#include "components/services/storage/dom_storage/storage_area_impl.h"
#include "components/tracing/common/tracing_switches.h"
#include "components/variations/fake_crash.h"
#include "components/viz/host/compositing_mode_reporter_impl.h"
#include "components/viz/host/gpu_host_impl.h"
#include "components/viz/host/host_frame_sink_manager.h"
#include "content/browser/accessibility/browser_accessibility_state_impl.h"
#include "content/browser/browser_thread_impl.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/compositor/viz_process_transport_factory.h"
#include "content/browser/download/save_file_manager.h"
#include "content/browser/field_trial_synchronizer.h"
#include "content/browser/first_party_sets/first_party_set_parser.h"
#include "content/browser/first_party_sets/first_party_sets_handler_impl.h"
#include "content/browser/gpu/browser_gpu_channel_host_factory.h"
#include "content/browser/gpu/browser_gpu_client_delegate.h"
#include "content/browser/gpu/compositor_util.h"
#include "content/browser/gpu/gpu_data_manager_impl.h"
#include "content/browser/gpu/gpu_disk_cache_factory.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/media/media_internals.h"
#include "content/browser/media/media_keys_listener_manager_impl.h"
#include "content/browser/memory_pressure/user_level_memory_pressure_signal_generator.h"
#include "content/browser/metrics/histogram_synchronizer.h"
#include "content/browser/network/browser_online_state_observer.h"
#include "content/browser/network_service_instance_impl.h"
#include "content/browser/renderer_host/media/media_stream_manager.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/scheduler/browser_task_executor.h"
#include "content/browser/scheduler/responsiveness/watcher.h"
#include "content/browser/screenlock_monitor/screenlock_monitor.h"
#include "content/browser/screenlock_monitor/screenlock_monitor_device_source.h"
#include "content/browser/sms/sms_provider.h"
#include "content/browser/speech/speech_recognition_manager_impl.h"
#include "content/browser/speech/tts_controller_impl.h"
#include "content/browser/startup_data_impl.h"
#include "content/browser/startup_task_runner.h"
#include "content/browser/tracing/background_tracing_manager_impl.h"
#include "content/browser/tracing/startup_tracing_controller.h"
#include "content/browser/tracing/tracing_controller_impl.h"
#include "content/browser/utility_process_host.h"
#include "content/browser/webrtc/webrtc_internals.h"
#include "content/browser/webui/content_web_ui_configs.h"
#include "content/browser/webui/url_data_manager.h"
#include "content/common/content_switches_internal.h"
#include "content/common/features.h"
#include "content/common/pseudonymization_salt.h"
#include "content/common/skia_utils.h"
#include "content/common/thread_pool_util.h"
#include "content/public/browser/audio_service.h"
#include "content/public/browser/background_tracing_manager.h"
#include "content/public/browser/browser_main_parts.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/device_service.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/site_isolation_policy.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/main_function_params.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/zygote/zygote_buildflags.h"
#include "crypto/crypto_buildflags.h"
#include "device/fido/hid/fido_hid_discovery.h"
#include "device/gamepad/gamepad_service.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_system.h"
#include "media/audio/audio_thread_impl.h"
#include "media/base/user_input_monitor.h"
#include "media/media_buildflags.h"
#include "media/midi/midi_service.h"
#include "media/mojo/buildflags.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/bindings/interface_endpoint_client.h"
#include "mojo/public/cpp/bindings/mojo_buildflags.h"
#include "mojo/public/cpp/bindings/sync_call_restrictions.h"
#include "net/base/network_change_notifier.h"
#include "net/log/net_log.h"
#include "net/socket/client_socket_factory.h"
#include "net/ssl/ssl_config_service.h"
#include "ppapi/buildflags/buildflags.h"
#include "services/audio/service.h"
#include "services/data_decoder/public/cpp/service_provider.h"
#include "services/data_decoder/public/mojom/data_decoder_service.mojom.h"
#include "services/network/public/cpp/network_switches.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/transitional_url_loader_factory_owner.h"
#include "services/tracing/public/cpp/trace_startup_config.h"
#include "services/video_capture/public/cpp/features.h"
#include "skia/ext/event_tracer_impl.h"
#include "skia/ext/legacy_display_globals.h"
#include "skia/ext/skia_memory_dump_provider.h"
#include "sql/sql_memory_dump_provider.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/display/display_features.h"
#include "ui/gfx/font_render_params.h"
#include "ui/gfx/switches.h"
#if defined(USE_AURA) || BUILDFLAG(IS_MAC)
#include "content/browser/compositor/image_transport_factory.h"
#endif
#if defined(USE_AURA)
#include "content/public/browser/context_factory.h"
#include "ui/aura/env.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_android.h"
#include "base/trace_event/cpufreq_monitor_android.h"
#include "components/tracing/common/graphics_memory_dump_provider_android.h"
#include "content/browser/android/browser_startup_controller.h"
#include "content/browser/android/launcher_thread.h"
#include "content/browser/android/scoped_surface_request_manager.h"
#include "content/browser/android/tracing_controller_android.h"
#include "content/browser/font_unique_name_lookup/font_unique_name_lookup_android.h"
#include "content/browser/screen_orientation/screen_orientation_delegate_android.h"
#include "media/base/android/media_drm_bridge_client.h"
#include "ui/android/screen_android.h"
#include "ui/display/screen.h"
#include "ui/gl/gl_surface.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "base/apple/scoped_nsautorelease_pool.h"
#include "content/browser/renderer_host/browser_compositor_view_mac.h"
#include "content/browser/theme_helper_mac.h"
#include "ui/accelerated_widget_mac/window_resize_helper_mac.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include <commctrl.h>
#include <shellapi.h>
#include "base/threading/platform_thread_win.h"
#include "net/base/winsock_init.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
#endif
#if defined(USE_GLIB)
#include <glib-object.h>
#endif
#if BUILDFLAG(IS_WIN)
#include "media/device_monitors/system_message_window_win.h"
#elif (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && defined(USE_UDEV)
#include "media/device_monitors/device_monitor_udev.h"
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/zx/job.h>
#include "base/fuchsia/default_job.h"
#include "base/fuchsia/fuchsia_logging.h"
#endif
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
#include "content/browser/sandbox_host_linux.h"
#endif
#if BUILDFLAG(ENABLE_PLUGINS)
#include "content/browser/plugin_service_impl.h"
#endif
#if BUILDFLAG(ENABLE_LIBRARY_CDMS) || BUILDFLAG(IS_ANDROID)
#include "content/browser/media/cdm_registry_impl.h"
#endif
#if BUILDFLAG(USE_NSS_CERTS)
#include "crypto/nss_util.h"
#endif
#if defined(ENABLE_IPC_FUZZER) && BUILDFLAG(IS_MAC)
#include "base/apple/foundation_util.h"
#endif
#if BUILDFLAG(MOJO_RANDOM_DELAYS_ENABLED)
#include "mojo/public/cpp/bindings/lib/test_random_mojo_delays.h"
#endif
#ifdef DestroyAll
#undef DestroyAll
#endif
namespace content {
namespace {
#if defined(USE_GLIB)
static void GLibLogHandler(const gchar* log_domain,
GLogLevelFlags log_level,
const gchar* message,
gpointer userdata) { … }
static void SetUpGLibLogHandler() { … }
#endif
NOINLINE void ResetThread_IO(
std::unique_ptr<BrowserProcessIOThread> io_thread) { … }
enum WorkerPoolType : size_t { … };
#if BUILDFLAG(IS_FUCHSIA)
void InitDefaultJob() {
zx::job job;
zx_status_t result = zx::job::create(*zx::job::default_job(), 0, &job);
ZX_CHECK(ZX_OK == result, result) << "zx_job_create";
base::SetDefaultJob(std::move(job));
}
#endif
#if defined(ENABLE_IPC_FUZZER)
bool GetBuildDirectory(base::FilePath* result) {
if (!base::PathService::Get(base::DIR_EXE, result))
return false;
#if BUILDFLAG(IS_MAC)
if (base::apple::AmIBundled()) {
*result = result->DirName().DirName().DirName();
}
#endif
return true;
}
void SetFileUrlPathAliasForIpcFuzzer() {
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kFileUrlPathAlias))
return;
base::FilePath build_directory;
if (!GetBuildDirectory(&build_directory)) {
LOG(ERROR) << "Failed to get build directory for /gen path alias.";
return;
}
const base::CommandLine::StringType alias_switch =
FILE_PATH_LITERAL("/gen=") + build_directory.AppendASCII("gen").value();
base::CommandLine::ForCurrentProcess()->AppendSwitchNative(
switches::kFileUrlPathAlias, alias_switch);
}
#endif
std::unique_ptr<base::MemoryPressureMonitor> CreateMemoryPressureMonitor(
const base::CommandLine& command_line) { … }
#if BUILDFLAG(IS_CHROMEOS_ASH)
mojo::PendingRemote<data_decoder::mojom::BleScanParser> GetBleScanParser() {
static base::NoDestructor<data_decoder::DataDecoder> decoder;
mojo::PendingRemote<data_decoder::mojom::BleScanParser> ble_scan_parser;
decoder->GetService()->BindBleScanParser(
ble_scan_parser.InitWithNewPipeAndPassReceiver());
return ble_scan_parser;
}
#endif
class OopDataDecoder : public data_decoder::ServiceProvider { … };
void BindHidManager(mojo::PendingReceiver<device::mojom::HidManager> receiver) { … }
uint32_t GenerateBrowserSalt() { … }
std::string GetRelatedWebsiteSetSwitch() { … }
}
BrowserMainLoop* g_current_browser_main_loop = …;
#if BUILDFLAG(IS_ANDROID)
namespace {
bool g_post_startup_tasks = true;
}
void BrowserMainLoop::EnableStartupTasks(bool enabled) {
g_post_startup_tasks = enabled;
}
#endif
BrowserMainLoop* BrowserMainLoop::GetInstance() { … }
media::AudioManager* BrowserMainLoop::GetAudioManager() { … }
BrowserMainLoop::BrowserMainLoop(
MainFunctionParams parameters,
std::unique_ptr<base::ThreadPoolInstance::ScopedExecutionFence>
scoped_execution_fence)
: … { … }
BrowserMainLoop::~BrowserMainLoop() { … }
void BrowserMainLoop::Init() { … }
int BrowserMainLoop::EarlyInitialization() { … }
void BrowserMainLoop::PreCreateMainMessageLoop() { … }
void BrowserMainLoop::CreateMainMessageLoop() { … }
void BrowserMainLoop::PostCreateMainMessageLoop() { … }
void BrowserMainLoop::CreateMessageLoopForEarlyShutdown() { … }
int BrowserMainLoop::PreCreateThreads() { … }
void BrowserMainLoop::CreateStartupTasks() { … }
scoped_refptr<base::SingleThreadTaskRunner>
BrowserMainLoop::GetResizeTaskRunner() { … }
gpu::GpuChannelEstablishFactory*
BrowserMainLoop::gpu_channel_establish_factory() const { … }
#if BUILDFLAG(IS_ANDROID)
void BrowserMainLoop::SynchronouslyFlushStartupTasks() {
startup_task_runner_->RunAllTasksNow();
}
#endif
int BrowserMainLoop::CreateThreads() { … }
int BrowserMainLoop::PostCreateThreads() { … }
int BrowserMainLoop::PreMainMessageLoopRun() { … }
BrowserMainLoop::ProceedWithMainMessageLoopRun
BrowserMainLoop::InterceptMainMessageLoopRun() { … }
void BrowserMainLoop::RunMainMessageLoop() { … }
void BrowserMainLoop::PreShutdown() { … }
void BrowserMainLoop::ShutdownThreadsAndCleanUp() { … }
media::AudioManager* BrowserMainLoop::audio_manager() const { … }
void BrowserMainLoop::GetCompositingModeReporter(
mojo::PendingReceiver<viz::mojom::CompositingModeReporter> receiver) { … }
void BrowserMainLoop::PostCreateThreadsImpl() { … }
bool BrowserMainLoop::UsingInProcessGpu() const { … }
void BrowserMainLoop::InitializeMemoryManagementComponent() { … }
bool BrowserMainLoop::InitializeToolkit() { … }
void BrowserMainLoop::InitializeMojo() { … }
void BrowserMainLoop::InitializeAudio() { … }
bool BrowserMainLoop::AudioServiceOutOfProcess() const { … }
SmsProvider* BrowserMainLoop::GetSmsProvider() { … }
void BrowserMainLoop::SetSmsProviderForTesting(
std::unique_ptr<SmsProvider> provider) { … }
base::PlatformThreadId BrowserMainLoop::GetIOThreadId() { … }
}