#include "content/public/app/initialize_mojo_core.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/logging.h"
#include "build/build_config.h"
#include "content/common/mojo_core_library_support.h"
#include "content/public/common/content_switches.h"
#include "mojo/core/embedder/configuration.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/features.h"
#include "mojo/public/c/system/functions.h"
#include "mojo/public/c/system/types.h"
#include "mojo/public/cpp/base/shared_memory_utils.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/dynamic_library_support.h"
#include "sandbox/policy/sandbox_type.h"
namespace content {
void InitializeMojoCore() {
if (base::FeatureList::GetInstance()) {
mojo::core::InitFeatures();
} else {
DLOG(FATAL) << "Initializing Mojo without a FeatureList";
}
mojo::core::Configuration config;
config.max_message_num_bytes = 128 * 1024 * 1024;
const auto& command_line = *base::CommandLine::ForCurrentProcess();
const bool is_browser = !command_line.HasSwitch(switches::kProcessType);
if (is_browser) {
config.is_broker_process =
#if BUILDFLAG(IS_CHROMEOS_LACROS)
false
#else
!command_line.HasSwitch(switches::kDisableMojoBroker) &&
!mojo::PlatformChannel::CommandLineHasPassedEndpoint(command_line)
#endif
;
if (!config.is_broker_process)
config.force_direct_shared_memory_allocation = true;
} else {
#if BUILDFLAG(IS_WIN)
config.force_direct_shared_memory_allocation = true;
#elif BUILDFLAG(IS_ANDROID)
config.force_direct_shared_memory_allocation = base::FeatureList::IsEnabled(
mojo::core::kMojoDirectSharedMemoryAndroid);
#endif
}
if (!IsMojoCoreSharedLibraryEnabled()) {
mojo::core::Init(config);
} else if (is_browser) {
MojoInitializeFlags flags = MOJO_INITIALIZE_FLAG_NONE;
if (config.is_broker_process)
flags |= MOJO_INITIALIZE_FLAG_AS_BROKER;
if (config.force_direct_shared_memory_allocation)
flags |= MOJO_INITIALIZE_FLAG_FORCE_DIRECT_SHARED_MEMORY_ALLOCATION;
MojoResult result = mojo::LoadAndInitializeCoreLibrary(
GetMojoCoreSharedLibraryPath(), flags);
CHECK_EQ(MOJO_RESULT_OK, result);
}
#if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_NACL) && !BUILDFLAG(IS_FUCHSIA)
if (sandbox::policy::IsUnsandboxedSandboxType(
sandbox::policy::SandboxTypeFromCommandLine(
*base::CommandLine::ForCurrentProcess()))) {
} else if (config.force_direct_shared_memory_allocation) {
} else {
DCHECK(!config.is_broker_process);
mojo::SharedMemoryUtils::InstallBaseHooks();
}
#endif
}
}