Commit 37e07d29 authored by Ken Rockot's avatar Ken Rockot Committed by Commit Bot

Add MojoDedicatedThread feature

This introduces a new MojoDedicatedThread feature to Content which, when
enabled, changes Mojo to use a dedicated background thread for receiving
incoming IPCs rather than using Content's IO thread.

The feature is disabled by default.

Bug: 1082761
Change-Id: Ia68f288309a316be14b5254e9c4f006b6f8c74cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2202143Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Commit-Queue: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#768903}
parent 59218269
......@@ -919,7 +919,7 @@ int ContentMainRunnerImpl::RunServiceManager(MainFunctionParams& main_params,
service_manager_environment_ = std::make_unique<ServiceManagerEnvironment>(
BrowserTaskExecutor::CreateIOThread());
download::SetIOTaskRunner(
service_manager_environment_->ipc_thread()->task_runner());
service_manager_environment_->io_thread()->task_runner());
InitializeBrowserMemoryInstrumentationClient();
......
......@@ -10,6 +10,7 @@
#include "content/browser/browser_process_sub_thread.h"
#include "content/browser/service_manager/service_manager_context.h"
#include "content/browser/startup_data_impl.h"
#include "content/public/common/content_features.h"
#include "content/public/common/service_manager_connection.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
......@@ -17,13 +18,19 @@
namespace content {
ServiceManagerEnvironment::ServiceManagerEnvironment(
std::unique_ptr<BrowserProcessSubThread> ipc_thread)
: ipc_thread_(std::move(ipc_thread)),
mojo_ipc_support_(std::make_unique<mojo::core::ScopedIPCSupport>(
ipc_thread_->task_runner(),
mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST)),
service_manager_context_(
std::make_unique<ServiceManagerContext>(ipc_thread_->task_runner())) {
std::unique_ptr<BrowserProcessSubThread> io_thread)
: io_thread_(std::move(io_thread)) {
scoped_refptr<base::SingleThreadTaskRunner> mojo_ipc_task_runner =
io_thread_->task_runner();
if (base::FeatureList::IsEnabled(features::kMojoDedicatedThread)) {
mojo_ipc_thread_.StartWithOptions(
base::Thread::Options(base::MessagePumpType::IO, 0));
mojo_ipc_task_runner = mojo_ipc_thread_.task_runner();
}
mojo_ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
mojo_ipc_task_runner, mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
service_manager_context_ =
std::make_unique<ServiceManagerContext>(io_thread_->task_runner());
ServiceManagerConnection::GetForProcess()->Start();
}
......@@ -32,7 +39,7 @@ ServiceManagerEnvironment::~ServiceManagerEnvironment() = default;
std::unique_ptr<StartupDataImpl>
ServiceManagerEnvironment::CreateBrowserStartupData() {
auto startup_data = std::make_unique<StartupDataImpl>();
startup_data->ipc_thread = std::move(ipc_thread_);
startup_data->io_thread = std::move(io_thread_);
startup_data->mojo_ipc_support = std::move(mojo_ipc_support_);
startup_data->service_manager_shutdown_closure =
base::BindOnce(&ServiceManagerContext::ShutDown,
......
......@@ -8,6 +8,7 @@
#include <memory>
#include "base/macros.h"
#include "base/threading/thread.h"
#include "content/common/content_export.h"
namespace mojo {
......@@ -27,22 +28,23 @@ struct StartupDataImpl;
class CONTENT_EXPORT ServiceManagerEnvironment {
public:
explicit ServiceManagerEnvironment(
std::unique_ptr<BrowserProcessSubThread> ipc_thread);
std::unique_ptr<BrowserProcessSubThread> io_thread);
~ServiceManagerEnvironment();
BrowserProcessSubThread* ipc_thread() { return ipc_thread_.get(); }
BrowserProcessSubThread* io_thread() { return io_thread_.get(); }
// Returns a new StartupDataImpl which captures and/or reflects the partial
// state of this ServiceManagerEnvironment. This must be called and the
// result passed to BrowserMain if the browser is going to be started within
// Service Manager's process.
//
// After this call, the ServiceManagerEnvironment no longer owns the IPC
// thread and |ipc_thread()| returns null.
// After this call, the ServiceManagerEnvironment no longer owns the IO
// thread and |io_thread()| returns null.
std::unique_ptr<StartupDataImpl> CreateBrowserStartupData();
private:
std::unique_ptr<BrowserProcessSubThread> ipc_thread_;
std::unique_ptr<BrowserProcessSubThread> io_thread_;
base::Thread mojo_ipc_thread_{"Mojo IPC"};
std::unique_ptr<mojo::core::ScopedIPCSupport> mojo_ipc_support_;
std::unique_ptr<ServiceManagerContext> service_manager_context_;
......
......@@ -589,7 +589,7 @@ void BrowserMainLoop::Init() {
static_cast<StartupDataImpl*>(parameters_.startup_data);
// This is always invoked before |io_thread_| is initialized (i.e. never
// resets it).
io_thread_ = std::move(startup_data->ipc_thread);
io_thread_ = std::move(startup_data->io_thread);
mojo_ipc_support_ = std::move(startup_data->mojo_ipc_support);
service_manager_shutdown_closure_ =
std::move(startup_data->service_manager_shutdown_closure);
......
......@@ -60,7 +60,7 @@ TEST_F(BrowserMainLoopTest, CreateThreadsInSingleProcess) {
MainFunctionParams main_function_params(GetProcessCommandLine());
StartupDataImpl startup_data;
startup_data.ipc_thread = BrowserTaskExecutor::CreateIOThread();
startup_data.io_thread = BrowserTaskExecutor::CreateIOThread();
main_function_params.startup_data = &startup_data;
BrowserMainLoop browser_main_loop(
......@@ -82,7 +82,7 @@ TEST_F(BrowserMainLoopTest,
MainFunctionParams main_function_params(GetProcessCommandLine());
StartupDataImpl startup_data;
startup_data.ipc_thread = BrowserTaskExecutor::CreateIOThread();
startup_data.io_thread = BrowserTaskExecutor::CreateIOThread();
main_function_params.startup_data = &startup_data;
BrowserMainLoop browser_main_loop(
......
......@@ -20,7 +20,7 @@ struct CONTENT_EXPORT StartupDataImpl : public StartupData {
StartupDataImpl();
~StartupDataImpl() override;
std::unique_ptr<BrowserProcessSubThread> ipc_thread;
std::unique_ptr<BrowserProcessSubThread> io_thread;
std::unique_ptr<mojo::core::ScopedIPCSupport> mojo_ipc_support;
base::OnceClosure service_manager_shutdown_closure;
};
......
......@@ -570,8 +570,16 @@ void ChildThreadImpl::Init(const Options& options) {
// IPC mode.
mojo::ScopedMessagePipeHandle child_process_pipe;
if (!IsInBrowserProcess()) {
scoped_refptr<base::SingleThreadTaskRunner> mojo_ipc_task_runner =
GetIOTaskRunner();
if (base::FeatureList::IsEnabled(features::kMojoDedicatedThread)) {
mojo_ipc_thread_.StartWithOptions(
base::Thread::Options(base::MessagePumpType::IO, 0));
mojo_ipc_task_runner = mojo_ipc_thread_.task_runner();
}
mojo_ipc_support_.reset(new mojo::core::ScopedIPCSupport(
GetIOTaskRunner(), mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST));
mojo_ipc_task_runner,
mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST));
mojo::IncomingInvitation invitation = InitializeMojoIPCChannel();
child_process_pipe = invitation.ExtractMessagePipe(0);
} else {
......
......@@ -16,6 +16,7 @@
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "build/build_config.h"
#include "components/variations/child_process_field_trial_syncer.h"
#include "content/common/associated_interfaces.mojom.h"
......@@ -211,6 +212,7 @@ class CONTENT_EXPORT ChildThreadImpl
const mojo::Remote<mojom::FontCacheWin>& GetFontCacheWin();
#endif
base::Thread mojo_ipc_thread_{"Mojo IPC"};
std::unique_ptr<mojo::core::ScopedIPCSupport> mojo_ipc_support_;
mojo::AssociatedReceiver<mojom::RouteProvider> route_provider_receiver_{this};
......
......@@ -343,6 +343,11 @@ const base::Feature kMediaDevicesSystemMonitorCache {
#endif
};
// If enabled Mojo uses a dedicated background thread to listen for incoming
// IPCs. Otherwise it's configured to use Content's IO thread for that purpose.
const base::Feature kMojoDedicatedThread{"MojoDedicatedThread",
base::FEATURE_DISABLED_BY_DEFAULT};
// Enables/disables the video capture service.
const base::Feature kMojoVideoCapture{"MojoVideoCapture",
base::FEATURE_ENABLED_BY_DEFAULT};
......
......@@ -81,6 +81,7 @@ CONTENT_EXPORT extern const base::Feature kLegacyWindowsDWriteFontFallback;
CONTENT_EXPORT extern const base::Feature kLogJsConsoleMessages;
CONTENT_EXPORT extern const base::Feature kLowPriorityIframes;
CONTENT_EXPORT extern const base::Feature kMediaDevicesSystemMonitorCache;
CONTENT_EXPORT extern const base::Feature kMojoDedicatedThread;
CONTENT_EXPORT extern const base::Feature kMojoVideoCapture;
CONTENT_EXPORT extern const base::Feature kMojoVideoCaptureSecondary;
CONTENT_EXPORT extern const base::Feature kMouseSubframeNoImplicitCapture;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment