Make some browser interfaces accessible to all child processes
The discardable shared memory manager and resource_coordinator's
memory instrumentation interfaces may be usable by any child process,
and memory instrumentation is in fact already in use by GPU and
renderers.
This moves these interface binders to a shared ConnectionFilter attached
to all browser-side ServiceManagerConnection instances, ensuring that
all content child processes can bind the interfaces.
BUG=718559
Change-Id: I8a1a1ff5b749365d020f72480e25ba2d88a025b3
Reviewed-on: https://chromium-review.googlesource.com/496607
Commit-Queue: Ken Rockot <[email protected]>
Reviewed-by: Ben Goodger <[email protected]>
Cr-Commit-Position: refs/heads/master@{#469593}
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index 16803c32..47c55207 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -1293,6 +1293,8 @@
"resource_context_impl.h",
"screen_orientation/screen_orientation_provider.cc",
"screen_orientation/screen_orientation_provider.h",
+ "service_manager/common_browser_interfaces.cc",
+ "service_manager/common_browser_interfaces.h",
"service_manager/merge_dictionary.cc",
"service_manager/merge_dictionary.h",
"service_manager/service_manager_context.cc",
diff --git a/content/browser/browser_context.cc b/content/browser/browser_context.cc
index 509baef..3f68fd7d 100644
--- a/content/browser/browser_context.cc
+++ b/content/browser/browser_context.cc
@@ -29,6 +29,7 @@
#include "content/browser/indexed_db/indexed_db_context_impl.h"
#include "content/browser/loader/resource_dispatcher_host_impl.h"
#include "content/browser/push_messaging/push_messaging_router.h"
+#include "content/browser/service_manager/common_browser_interfaces.h"
#include "content/browser/storage_partition_impl_map.h"
#include "content/common/child_process_host_impl.h"
#include "content/public/browser/blob_handle.h"
@@ -492,6 +493,8 @@
for (const auto& entry : services) {
connection->AddEmbeddedService(entry.first, entry.second);
}
+
+ RegisterCommonBrowserInterfaces(connection);
connection->Start();
}
}
diff --git a/content/browser/gpu/gpu_process_host.cc b/content/browser/gpu/gpu_process_host.cc
index 4977be2..da5a242b 100644
--- a/content/browser/gpu/gpu_process_host.cc
+++ b/content/browser/gpu/gpu_process_host.cc
@@ -346,12 +346,6 @@
auto task_runner = BrowserThread::GetTaskRunnerForThread(BrowserThread::UI);
registry_.AddInterface(base::Bind(&FieldTrialRecorder::Create),
task_runner);
- registry_.AddInterface(
- base::Bind(
- &memory_instrumentation::CoordinatorImpl::BindCoordinatorRequest,
- base::Unretained(
- memory_instrumentation::CoordinatorImpl::GetInstance())),
- task_runner);
#if defined(OS_ANDROID)
registry_.AddInterface(
base::Bind(&BindJavaInterface<media::mojom::AndroidOverlayProvider>),
diff --git a/content/browser/renderer_host/render_process_host_impl.cc b/content/browser/renderer_host/render_process_host_impl.cc
index 3121685..5731df87c 100644
--- a/content/browser/renderer_host/render_process_host_impl.cc
+++ b/content/browser/renderer_host/render_process_host_impl.cc
@@ -52,7 +52,6 @@
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "cc/output/buffer_to_texture_target_map.h"
-#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "components/metrics/single_sample_metrics.h"
#include "components/tracing/common/tracing_switches.h"
#include "content/browser/appcache/appcache_dispatcher_host.h"
@@ -183,7 +182,6 @@
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "net/url_request/url_request_context_getter.h"
#include "ppapi/features/features.h"
-#include "services/resource_coordinator/memory/coordinator/coordinator_impl.h"
#include "services/service_manager/embedder/switches.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "services/service_manager/public/cpp/connector.h"
@@ -1341,25 +1339,8 @@
base::Bind(&WebSocketManager::CreateWebSocket, GetID(),
MSG_ROUTING_NONE));
- // Chrome browser process only provides DiscardableSharedMemory service when
- // Chrome is not running in mus+ash.
- if (!service_manager::ServiceManagerIsRemote()) {
- discardable_memory::DiscardableSharedMemoryManager* manager =
- BrowserMainLoop::GetInstance()->discardable_shared_memory_manager();
- registry->AddInterface(
- base::Bind(&discardable_memory::DiscardableSharedMemoryManager::Bind,
- base::Unretained(manager)));
- }
-
AddUIThreadInterface(registry.get(), base::Bind(&FieldTrialRecorder::Create));
- AddUIThreadInterface(
- registry.get(),
- base::Bind(
- &memory_instrumentation::CoordinatorImpl::BindCoordinatorRequest,
- base::Unretained(
- memory_instrumentation::CoordinatorImpl::GetInstance())));
-
associated_interfaces_.reset(new AssociatedInterfaceRegistryImpl());
GetContentClient()->browser()->ExposeInterfacesToRenderer(
registry.get(), associated_interfaces_.get(), this);
diff --git a/content/browser/service_manager/common_browser_interfaces.cc b/content/browser/service_manager/common_browser_interfaces.cc
new file mode 100644
index 0000000..afc81bb
--- /dev/null
+++ b/content/browser/service_manager/common_browser_interfaces.cc
@@ -0,0 +1,98 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/service_manager/common_browser_interfaces.h"
+
+#include <memory>
+#include <utility>
+
+#include "base/callback.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/task_runner.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
+#include "content/browser/browser_main_loop.h"
+#include "content/public/common/connection_filter.h"
+#include "content/public/common/service_manager_connection.h"
+#include "mojo/public/cpp/bindings/interface_request.h"
+#include "services/resource_coordinator/memory/coordinator/coordinator_impl.h"
+#include "services/service_manager/public/cpp/binder_registry.h"
+
+namespace content {
+
+namespace {
+
+void BindMemoryCoordinatorRequest(
+ const service_manager::BindSourceInfo& source_info,
+ memory_instrumentation::mojom::CoordinatorRequest request) {
+ auto* coordinator = memory_instrumentation::CoordinatorImpl::GetInstance();
+ if (coordinator)
+ coordinator->BindCoordinatorRequest(source_info, std::move(request));
+}
+
+class ConnectionFilterImpl : public ConnectionFilter {
+ public:
+ ConnectionFilterImpl()
+ : main_thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {
+ RegisterMainThreadInterface(base::Bind(&BindMemoryCoordinatorRequest));
+
+ auto* browser_main_loop = BrowserMainLoop::GetInstance();
+ if (browser_main_loop) {
+ auto* manager = browser_main_loop->discardable_shared_memory_manager();
+ if (manager) {
+ registry_.AddInterface(base::Bind(
+ &discardable_memory::DiscardableSharedMemoryManager::Bind,
+ base::Unretained(manager)));
+ }
+ }
+ }
+
+ ~ConnectionFilterImpl() override {}
+
+ private:
+ template <typename Interface>
+ using InterfaceBinder =
+ base::Callback<void(const service_manager::BindSourceInfo&,
+ mojo::InterfaceRequest<Interface>)>;
+
+ // ConnectionFilter:
+ void OnBindInterface(const service_manager::BindSourceInfo& source_info,
+ const std::string& interface_name,
+ mojo::ScopedMessagePipeHandle* interface_pipe,
+ service_manager::Connector* connector) override {
+ if (registry_.CanBindInterface(interface_name)) {
+ registry_.BindInterface(source_info, interface_name,
+ std::move(*interface_pipe));
+ }
+ }
+
+ template <typename Interface>
+ static void BindOnTaskRunner(
+ const scoped_refptr<base::TaskRunner>& task_runner,
+ const InterfaceBinder<Interface>& binder,
+ const service_manager::BindSourceInfo& source_info,
+ mojo::InterfaceRequest<Interface> request) {
+ task_runner->PostTask(
+ FROM_HERE, base::BindOnce(binder, source_info, std::move(request)));
+ }
+
+ template <typename Interface>
+ void RegisterMainThreadInterface(const InterfaceBinder<Interface>& binder) {
+ registry_.AddInterface(base::Bind(&BindOnTaskRunner<Interface>,
+ main_thread_task_runner_, binder));
+ }
+
+ const scoped_refptr<base::TaskRunner> main_thread_task_runner_;
+ service_manager::BinderRegistry registry_;
+
+ DISALLOW_COPY_AND_ASSIGN(ConnectionFilterImpl);
+};
+
+} // namespace
+
+void RegisterCommonBrowserInterfaces(ServiceManagerConnection* connection) {
+ connection->AddConnectionFilter(base::MakeUnique<ConnectionFilterImpl>());
+}
+
+} // namespace content
diff --git a/content/browser/service_manager/common_browser_interfaces.h b/content/browser/service_manager/common_browser_interfaces.h
new file mode 100644
index 0000000..0e826dec
--- /dev/null
+++ b/content/browser/service_manager/common_browser_interfaces.h
@@ -0,0 +1,18 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_BROWSER_SERVICE_MANAGER_COMMON_BROWSER_INTERFACES_H_
+#define CONTENT_BROWSER_SERVICE_MANAGER_COMMON_BROWSER_INTERFACES_H_
+
+namespace content {
+
+class ServiceManagerConnection;
+
+// Registers interface binders for browser-side interfaces that are common to
+// all child process types.
+void RegisterCommonBrowserInterfaces(ServiceManagerConnection* connection);
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_SERVICE_MANAGER_COMMON_BROWSER_INTERFACES_H_
diff --git a/content/browser/service_manager/service_manager_context.cc b/content/browser/service_manager/service_manager_context.cc
index 98372cb..eeb81396 100644
--- a/content/browser/service_manager/service_manager_context.cc
+++ b/content/browser/service_manager/service_manager_context.cc
@@ -19,6 +19,7 @@
#include "base/strings/utf_string_conversions.h"
#include "content/browser/child_process_launcher.h"
#include "content/browser/gpu/gpu_process_host.h"
+#include "content/browser/service_manager/common_browser_interfaces.h"
#include "content/browser/service_manager/merge_dictionary.h"
#include "content/browser/wake_lock/wake_lock_context_host.h"
#include "content/common/service_manager/service_manager_connection_impl.h"
@@ -275,6 +276,7 @@
ServiceManagerConnection::SetForProcess(ServiceManagerConnection::Create(
mojo::MakeRequest(&root_browser_service),
BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
+ auto* browser_connection = ServiceManagerConnection::GetForProcess();
service_manager::mojom::PIDReceiverPtr pid_receiver;
packaged_services_connection_->GetConnector()->StartService(
@@ -313,8 +315,7 @@
// This is safe to assign directly from any thread, because
// ServiceManagerContext must be constructed before anyone can call
// GetConnectorForIOThread().
- g_io_thread_connector.Get() =
- ServiceManagerConnection::GetForProcess()->GetConnector()->Clone();
+ g_io_thread_connector.Get() = browser_connection->GetConnector()->Clone();
ContentBrowserClient::OutOfProcessServiceMap sandboxed_services;
GetContentClient()
@@ -360,12 +361,14 @@
shape_detection::mojom::kServiceName));
packaged_services_connection_->Start();
- ServiceManagerConnection::GetForProcess()->Start();
+
+ RegisterCommonBrowserInterfaces(browser_connection);
+ browser_connection->Start();
if (network_service_enabled) {
// Start the network service process as soon as possible, since it is
// critical to start up performance.
- ServiceManagerConnection::GetForProcess()->GetConnector()->StartService(
+ browser_connection->GetConnector()->StartService(
mojom::kNetworkServiceName);
}
}