#include "content/child/child_thread_impl.h"
#include <signal.h>
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include "base/base_switches.h"
#include "base/clang_profiling_buildflags.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/alias.h"
#include "base/debug/leak_annotations.h"
#include "base/debug/profiler.h"
#include "base/files/file.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_pump.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_macros.h"
#include "base/power_monitor/power_monitor.h"
#include "base/process/process.h"
#include "base/process/process_handle.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/timer/elapsed_timer.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "content/child/browser_exposed_child_interfaces.h"
#include "content/child/child_process.h"
#include "content/child/child_process_synthetic_trial_syncer.h"
#include "content/common/child_process.mojom.h"
#include "content/common/content_constants_internal.h"
#include "content/common/features.h"
#include "content/common/field_trial_recorder.mojom.h"
#include "content/common/in_process_child_thread_params.h"
#include "content/common/pseudonymization_salt.h"
#include "content/public/child/child_thread.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "ipc/ipc_channel_mojo.h"
#include "ipc/ipc_logging.h"
#include "ipc/ipc_platform_file.h"
#include "ipc/ipc_sync_channel.h"
#include "ipc/ipc_sync_message_filter.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/platform/named_platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/platform/platform_channel_endpoint.h"
#include "mojo/public/cpp/platform/platform_handle.h"
#include "mojo/public/cpp/system/buffer.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "sandbox/policy/sandbox_type.h"
#include "services/device/public/cpp/power_monitor/power_monitor_broadcast_source.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/client_process_impl.h"
#include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
#include "services/tracing/public/cpp/background_tracing/background_tracing_agent_impl.h"
#include "services/tracing/public/cpp/background_tracing/background_tracing_agent_provider_impl.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"
#if BUILDFLAG(IS_POSIX)
#include "base/posix/global_descriptors.h"
#include "content/public/common/content_descriptors.h"
#if !BUILDFLAG(IS_ANDROID)
#include "services/tracing/public/cpp/system_tracing_service.h"
#include "services/tracing/public/cpp/traced_process.h"
#endif
#endif
#if BUILDFLAG(IS_APPLE)
#include "base/mac/mach_port_rendezvous.h"
#endif
#if BUILDFLAG(CLANG_PROFILING_INSIDE_SANDBOX)
#include <stdio.h>
#include "base/test/clang_profiling.h"
#include "build/config/compiler/compiler_buildflags.h"
#if BUILDFLAG(IS_POSIX)
#include "base/files/scoped_file.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <io.h>
#endif
extern "C" void __llvm_profile_set_file_object(FILE* File, int EnableMerge);
#endif
namespace content {
namespace {
const int kConnectionTimeoutS = …;
ABSL_CONST_INIT thread_local ChildThreadImpl* child_thread_impl = …;
#if BUILDFLAG(IS_POSIX)
#if defined(ADDRESS_SANITIZER) || defined(LEAK_SANITIZER) || \
defined(MEMORY_SANITIZER) || defined(THREAD_SANITIZER) || \
defined(UNDEFINED_SANITIZER)
class WaitAndExitDelegate : public base::PlatformThread::Delegate {
public:
explicit WaitAndExitDelegate(base::TimeDelta duration)
: duration_(duration) {}
WaitAndExitDelegate(const WaitAndExitDelegate&) = delete;
WaitAndExitDelegate& operator=(const WaitAndExitDelegate&) = delete;
void ThreadMain() override {
base::PlatformThread::Sleep(duration_);
base::Process::TerminateCurrentProcessImmediately(0);
}
private:
const base::TimeDelta duration_;
};
bool CreateWaitAndExitThread(base::TimeDelta duration) {
std::unique_ptr<WaitAndExitDelegate> delegate(
new WaitAndExitDelegate(duration));
const bool thread_created =
base::PlatformThread::CreateNonJoinable(0, delegate.get());
if (!thread_created)
return false;
WaitAndExitDelegate* leaking_delegate = delegate.release();
ANNOTATE_LEAKING_OBJECT_PTR(leaking_delegate);
std::ignore = leaking_delegate;
return true;
}
#endif
void TerminateSelfOnDisconnect() { … }
class SuicideOnChannelErrorFilter : public IPC::MessageFilter { … };
#endif
mojo::IncomingInvitation InitializeMojoIPCChannel() { … }
void FieldTrialActivatedCallback(
mojo::SharedRemote<mojom::FieldTrialRecorder> recorder,
const std::string& trial_name) { … }
}
class ChildThreadImpl::IOThreadState
: public base::RefCountedThreadSafe<IOThreadState>,
public mojom::ChildProcess { … };
ChildThread* ChildThread::Get() { … }
ChildThreadImpl::Options::Options() = default;
ChildThreadImpl::Options::Options(const Options& other) = default;
ChildThreadImpl::Options::~Options() = default;
ChildThreadImpl::Options::Builder::Builder() = default;
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::InBrowserProcess(
const InProcessChildThreadParams& params) { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::WithLegacyIPCChannel(bool with_channel) { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::ConnectToBrowser(
bool connect_to_browser_parms) { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::IPCTaskRunner(
scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_parms) { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::ServiceBinder(
ChildThreadImpl::Options::ServiceBinder binder) { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::ExposesInterfacesToBrowser() { … }
ChildThreadImpl::Options::Builder&
ChildThreadImpl::Options::Builder::SetUrgentMessageObserver(
IPC::UrgentMessageObserver* observer) { … }
ChildThreadImpl::Options ChildThreadImpl::Options::Builder::Build() { … }
#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
ChildThreadImpl::ChildThreadMessageRouter::ChildThreadMessageRouter(
IPC::Sender* sender)
: sender_(sender) {}
bool ChildThreadImpl::ChildThreadMessageRouter::Send(IPC::Message* msg) {
return sender_->Send(msg);
}
bool ChildThreadImpl::ChildThreadMessageRouter::RouteMessage(
const IPC::Message& msg) {
bool handled = IPC::MessageRouter::RouteMessage(msg);
#if BUILDFLAG(IS_ANDROID)
if (!handled && msg.is_sync()) {
IPC::Message* reply = IPC::SyncMessage::GenerateReply(&msg);
reply->set_reply_error();
Send(reply);
}
#endif
return handled;
}
#endif
ChildThreadImpl::ChildThreadImpl(base::RepeatingClosure quit_closure)
: … { … }
ChildThreadImpl::ChildThreadImpl(base::RepeatingClosure quit_closure,
const Options& options)
: … { … }
scoped_refptr<base::SingleThreadTaskRunner> ChildThreadImpl::GetIOTaskRunner() { … }
void ChildThreadImpl::SetFieldTrialGroup(const std::string& trial_name,
const std::string& group_name) { … }
void ChildThreadImpl::Init(const Options& options) { … }
ChildThreadImpl::~ChildThreadImpl() { … }
void ChildThreadImpl::Shutdown() { … }
bool ChildThreadImpl::ShouldBeDestroyed() { … }
void ChildThreadImpl::OnChannelConnected(int32_t peer_pid) { … }
void ChildThreadImpl::OnChannelError() { … }
#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
bool ChildThreadImpl::Send(IPC::Message* msg) {
DCHECK(main_thread_runner_->BelongsToCurrentThread());
if (!channel_) {
delete msg;
return false;
}
return channel_->Send(msg);
}
#endif
#if BUILDFLAG(IS_WIN)
void ChildThreadImpl::PreCacheFont(const LOGFONT& log_font) {
GetFontCacheWin()->PreCacheFont(log_font);
}
void ChildThreadImpl::ReleaseCachedFonts() {
GetFontCacheWin()->ReleaseCachedFonts();
}
const mojo::Remote<mojom::FontCacheWin>& ChildThreadImpl::GetFontCacheWin() {
if (!font_cache_win_)
BindHostReceiver(font_cache_win_.BindNewPipeAndPassReceiver());
return font_cache_win_;
}
#endif
void ChildThreadImpl::RecordAction(const base::UserMetricsAction& action) { … }
void ChildThreadImpl::RecordComputedAction(const std::string& action) { … }
void ChildThreadImpl::BindHostReceiver(mojo::GenericPendingReceiver receiver) { … }
#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
IPC::MessageRouter* ChildThreadImpl::GetRouter() {
DCHECK(main_thread_runner_->BelongsToCurrentThread());
return &router_;
}
#endif
bool ChildThreadImpl::OnMessageReceived(const IPC::Message& msg) { … }
void ChildThreadImpl::OnAssociatedInterfaceRequest(
const std::string& interface_name,
mojo::ScopedInterfaceEndpointHandle handle) { … }
void ChildThreadImpl::ExposeInterfacesToBrowser(mojo::BinderMap binders) { … }
#if BUILDFLAG(CONTENT_ENABLE_LEGACY_IPC)
bool ChildThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
return false;
}
#endif
void ChildThreadImpl::GetBackgroundTracingAgentProvider(
mojo::PendingReceiver<tracing::mojom::BackgroundTracingAgentProvider>
receiver) { … }
void ChildThreadImpl::DisconnectChildProcessHost() { … }
void ChildThreadImpl::BindServiceInterface(
mojo::GenericPendingReceiver receiver) { … }
void ChildThreadImpl::OnBindReceiver(mojo::GenericPendingReceiver receiver) { … }
ChildThreadImpl* ChildThreadImpl::current() { … }
void ChildThreadImpl::OnProcessFinalRelease() { … }
void ChildThreadImpl::SetBatterySaverMode(bool battery_saver_mode_enabled) { … }
void ChildThreadImpl::EnsureConnected() { … }
bool ChildThreadImpl::IsInBrowserProcess() const { … }
#if BUILDFLAG(IS_ANDROID)
void ChildThreadImpl::OnMemoryPressureFromBrowserReceived(
base::MemoryPressureListener::MemoryPressureLevel level) {
if (IsInBrowserProcess()) {
return;
}
base::MemoryPressureListener::NotifyMemoryPressure(level);
}
#endif
}