Skip to content

Commit

Permalink
Expose a chrome_renderer service.
Browse files Browse the repository at this point in the history
The idea is that the content embedder should define its own services that run
in each process type, rather than merging stuff onto content's own per-process
service type manifests. So now in addition to content_browser, content_renderer
etc, we have:

chrome
chrome_renderer

etc.

This CL implements this for chrome_renderer (chrome was done prior), does
the initialization to make sure both services are started, and migrates more
SpellCheck interfaces onto this. One trick here is that in this model embedder
code has to use its own service connector to connect to its other process
services, rather than the connector(s) exposed by content.

This is logically consistent with a future in which content_browser morphs into
a "navigation" service, content_renderer morphs into a "blink" service,
content_utility morphs into many individual services, content_gpu morphs into
viz, etc.

There are a bunch of tweaks here to support tests with this new model.

Bug: 
Change-Id: Ib1b30054a63ed30501c88c3fe5c471682f5a03ae
Reviewed-on: https://chromium-review.googlesource.com/742344
Commit-Queue: Ben Goodger <ben@chromium.org>
Reviewed-by: Ken Rockot <rockot@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#531093}
  • Loading branch information
Ben Goodger authored and Commit Bot committed Jan 23, 2018
1 parent 080ccb3 commit 1633130
Show file tree
Hide file tree
Showing 51 changed files with 442 additions and 203 deletions.
3 changes: 2 additions & 1 deletion android_webview/browser/aw_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ AwContentBrowserClient::GetWebContentsViewDelegate(
}

void AwContentBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) {
// Grant content: scheme access to the whole renderer process, since we impose
// per-view access checks, and access is granted by default (see
// AwSettings.mAllowContentUrlAccess).
Expand Down
4 changes: 3 additions & 1 deletion android_webview/browser/aw_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class AwContentBrowserClient : public content::ContentBrowserClient {
const content::MainFunctionParams& parameters) override;
content::WebContentsViewDelegate* GetWebContentsViewDelegate(
content::WebContents* web_contents) override;
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
void RenderProcessWillLaunch(
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) override;
bool IsHandledURL(const GURL& url) override;
void AppendExtraCommandLineSwitches(base::CommandLine* command_line,
int child_process_id) override;
Expand Down
6 changes: 2 additions & 4 deletions android_webview/renderer/aw_content_renderer_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,8 @@ void AwContentRendererClient::RenderThreadStarted() {
std::move(registry)));

#if BUILDFLAG(ENABLE_SPELLCHECK)
if (!spellcheck_) {
spellcheck_ = std::make_unique<SpellCheck>(this);
thread->AddObserver(spellcheck_.get());
}
if (!spellcheck_)
spellcheck_ = std::make_unique<SpellCheck>(nullptr, this);
#endif
}

Expand Down
5 changes: 5 additions & 0 deletions chrome/app/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ service_manifest("chrome_manifest") {
source = "chrome_manifest.json"
}

service_manifest("chrome_renderer_manifest") {
source = "chrome_renderer_manifest.json"
}

service_manifest("chrome_content_packaged_services_manifest_overlay") {
source =
"//chrome/browser/chrome_content_packaged_services_manifest_overlay.json"
Expand Down Expand Up @@ -542,6 +546,7 @@ if (use_aura) {
":chrome_content_plugin_manifest",
":chrome_content_renderer_manifest",
":chrome_content_utility_manifest",
":chrome_renderer_manifest",
]

catalog("catalog") {
Expand Down
3 changes: 3 additions & 0 deletions chrome/app/OWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ per-file chrome_exe_main_mac.cc=mark@chromium.org

per-file chrome_manifest.json=set noparent
per-file chrome_manifest.json=file://ipc/SECURITY_OWNERS

per-file chrome_renderer_manifest.json=set noparent
per-file chrome_renderer_manifest.json=file://ipc/SECURITY_OWNERS
5 changes: 4 additions & 1 deletion chrome/app/chrome_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
]
},
"requires": {
"chrome_renderer": [ "browser" ],
"service_manager": [
"service_manager:all_users"
"service_manager:all_users",
"service_manager:client_process",
"service_manager:instance_name"
]
}
}
Expand Down
16 changes: 16 additions & 0 deletions chrome/app/chrome_renderer_manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "chrome_renderer",
"display_name": "Chrome Renderer",
"interface_provider_specs": {
"service_manager:connector": {
"provides": {
"browser": [
"spellcheck::mojom::SpellChecker"
]
},
"requires": {
"chrome": [ "renderer" ]
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ class BrowserClientForTextInputClientMac : public ChromeContentBrowserClient {

// ContentBrowserClient overrides.
void RenderProcessWillLaunch(
content::RenderProcessHost* process_host) override {
ChromeContentBrowserClient::RenderProcessWillLaunch(process_host);
content::RenderProcessHost* process_host,
service_manager::mojom::ServiceRequest* service_request) override {
ChromeContentBrowserClient::RenderProcessWillLaunch(process_host,
service_request);
filters_.push_back(
new content::TestTextInputClientMessageFilter(process_host));
}
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/browser_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<include name="IDR_CLOUDPRINT_MANIFEST" file="resources\cloud_print_app\manifest.json" type="BINDATA" />
<include name="IDR_PDF_COMPOSITOR_MANIFEST" file="..\..\components\printing\service\pdf_compositor_manifest.json" type="BINDATA" />
</if>
<include name="IDR_CHROME_RENDERER_SERVICE_MANIFEST" file="..\app\chrome_renderer_manifest.json" type="BINDATA" />
<include name="IDR_DEVTOOLS_DISCOVERY_PAGE_HTML" file="devtools\frontend\devtools_discovery_page.html" type="BINDATA"/>
<include name="IDR_DOMAIN_RELIABILITY_INTERNALS_HTML" file="resources\domain_reliability_internals.html" compress="gzip" type="BINDATA" />
<include name="IDR_DOMAIN_RELIABILITY_INTERNALS_CSS" file="resources\domain_reliability_internals.css" compress="gzip" type="BINDATA" />
Expand Down
20 changes: 18 additions & 2 deletions chrome/browser/chrome_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
#include "services/preferences/public/cpp/in_process_service_factory.h"
#include "services/preferences/public/interfaces/preferences.mojom.h"
#include "services/proxy_resolver/public/interfaces/proxy_resolver.mojom.h"
#include "services/service_manager/public/interfaces/connector.mojom.h"
#include "services/service_manager/sandbox/sandbox_type.h"
#include "storage/browser/fileapi/external_mount_points.h"
#include "third_party/WebKit/common/page/page_visibility_state.mojom.h"
Expand Down Expand Up @@ -929,6 +930,8 @@ content::BrowserMainParts* ChromeContentBrowserClient::CreateBrowserMainParts(

chrome::AddMetricsExtraParts(main_parts);

main_parts->AddParts(ChromeService::GetInstance()->CreateExtraParts());

return main_parts;
}

Expand Down Expand Up @@ -1047,7 +1050,8 @@ bool ChromeContentBrowserClient::AllowGpuLaunchRetryOnIOThread() {
}

void ChromeContentBrowserClient::RenderProcessWillLaunch(
content::RenderProcessHost* host) {
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) {
int id = host->GetID();
Profile* profile = Profile::FromBrowserContext(host->GetBrowserContext());
host->AddFilter(new ChromeRenderMessageFilter(id, profile));
Expand Down Expand Up @@ -1118,6 +1122,16 @@ void ChromeContentBrowserClient::RenderProcessWillLaunch(
HostContentSettingsMapFactory::GetForProfile(profile), &rules);
}
rc_interface->SetContentSettingRules(rules);

service_manager::mojom::ServicePtr service;
*service_request = mojo::MakeRequest(&service);
service_manager::mojom::PIDReceiverPtr pid_receiver;
service_manager::Identity renderer_identity = host->GetChildIdentity();
ChromeService::GetInstance()->connector()->StartService(
service_manager::Identity(chrome::mojom::kRendererServiceName,
renderer_identity.user_id(),
renderer_identity.instance()),
std::move(service), mojo::MakeRequest(&pid_receiver));
}

GURL ChromeContentBrowserClient::GetEffectiveURL(
Expand Down Expand Up @@ -3142,7 +3156,7 @@ void ChromeContentBrowserClient::RegisterInProcessServices(
StaticServiceMap* services) {
{
service_manager::EmbeddedServiceInfo info;
info.factory = base::Bind(&ChromeService::Create);
info.factory = ChromeService::GetInstance()->CreateChromeServiceFactory();
services->insert(std::make_pair(chrome::mojom::kServiceName, info));
}
if (g_browser_process->pref_service_factory()) {
Expand Down Expand Up @@ -3273,6 +3287,8 @@ ChromeContentBrowserClient::GetExtraServiceManifests() {
#if BUILDFLAG(ENABLE_PRINTING)
{printing::mojom::kServiceName, IDR_PDF_COMPOSITOR_MANIFEST},
#endif
{chrome::mojom::kRendererServiceName,
IDR_CHROME_RENDERER_SERVICE_MANIFEST},
});
}

Expand Down
4 changes: 3 additions & 1 deletion chrome/browser/chrome_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ class ChromeContentBrowserClient : public content::ContentBrowserClient {
bool* in_memory) override;
content::WebContentsViewDelegate* GetWebContentsViewDelegate(
content::WebContents* web_contents) override;
void RenderProcessWillLaunch(
content::RenderProcessHost* host,
service_manager::mojom::ServiceRequest* service_request) override;
bool AllowGpuLaunchRetryOnIOThread() override;
void RenderProcessWillLaunch(content::RenderProcessHost* host) override;
GURL GetEffectiveURL(content::BrowserContext* browser_context,
const GURL& url) override;
bool ShouldUseProcessPerSite(content::BrowserContext* browser_context,
Expand Down
4 changes: 0 additions & 4 deletions chrome/browser/chrome_content_renderer_manifest_overlay.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
"browser": [
"chrome::mojom::ResourceUsageReporter",
"chrome::mojom::SearchBouncer",
"spellcheck::mojom::SpellChecker",
"profiling::mojom::ProfilingClient"
]
},
"requires": {
"chrome": [ "renderer" ]
}
},
"navigation:frame": {
Expand Down
148 changes: 121 additions & 27 deletions chrome/browser/chrome_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,152 @@

#include "chrome/browser/chrome_service.h"

#include "chrome/browser/chrome_browser_main_extra_parts.h"
#include "chrome/common/constants.mojom.h"
#include "components/spellcheck/spellcheck_build_features.h"
#include "components/startup_metric_utils/browser/startup_metric_host_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/common/service_manager_connection.h"
#include "services/service_manager/public/cpp/binder_registry.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/service_manager/public/cpp/service.h"
#include "services/service_manager/public/cpp/service_context.h"

#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/launchable.h"
#if defined(USE_OZONE)
#include "services/ui/public/cpp/input_devices/input_device_controller.h"
#endif
#endif
#if BUILDFLAG(ENABLE_SPELLCHECK)
#include "chrome/browser/spellchecker/spell_check_host_chrome_impl.h"
#if BUILDFLAG(HAS_SPELLCHECK_PANEL)
#include "chrome/browser/spellchecker/spell_check_panel_host_impl.h"
#endif
#endif

// static
std::unique_ptr<service_manager::Service> ChromeService::Create() {
return base::MakeUnique<ChromeService>();
}
namespace {

ChromeService* g_instance = nullptr;

} // namespace

class ChromeService::IOThreadContext : public service_manager::Service {
public:
IOThreadContext() {
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner =
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI);

ChromeService::ChromeService() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
#if defined(OS_CHROMEOS)
#if defined(USE_OZONE)
input_device_controller_.AddInterface(&registry_);
input_device_controller_.AddInterface(&registry_, ui_task_runner);
#endif
registry_.AddInterface(
base::Bind(&chromeos::Launchable::Bind, base::Unretained(&launchable_)));
registry_.AddInterface(base::BindRepeating(&chromeos::Launchable::Bind,
base::Unretained(&launchable_)),
ui_task_runner);
#endif
registry_.AddInterface(
base::Bind(&startup_metric_utils::StartupMetricHostImpl::Create));
registry_.AddInterface(base::BindRepeating(
&startup_metric_utils::StartupMetricHostImpl::Create));
#if BUILDFLAG(ENABLE_SPELLCHECK)
registry_with_source_info_.AddInterface(
base::BindRepeating(&SpellCheckHostChromeImpl::Create),
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI));
registry_with_source_info_.AddInterface(
base::BindRepeating(&SpellCheckHostChromeImpl::Create), ui_task_runner);
#if BUILDFLAG(HAS_SPELLCHECK_PANEL)
registry_.AddInterface(base::Bind(&SpellCheckPanelHostImpl::Create),
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::UI));
registry_.AddInterface(
base::BindRepeating(&SpellCheckPanelHostImpl::Create), ui_task_runner);
#endif
#endif
}
~IOThreadContext() override = default;

void Init(service_manager::mojom::ConnectorRequest connector_request) {
DCHECK(!connector_request_.is_pending());
connector_request_ = std::move(connector_request);
}

private:
// service_manager::Service:
void OnStart() override {
context()->connector()->BindConnectorRequest(std::move(connector_request_));
}
void OnBindInterface(const service_manager::BindSourceInfo& remote_info,
const std::string& name,
mojo::ScopedMessagePipeHandle handle) override {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
content::OverrideOnBindInterface(remote_info, name, &handle);
if (!handle.is_valid())
return;

if (!registry_.TryBindInterface(name, &handle))
registry_with_source_info_.TryBindInterface(name, &handle, remote_info);
}

service_manager::mojom::ConnectorRequest connector_request_;

service_manager::BinderRegistry registry_;
service_manager::BinderRegistryWithArgs<
const service_manager::BindSourceInfo&>
registry_with_source_info_;

#if defined(OS_CHROMEOS)
chromeos::Launchable launchable_;
#if defined(USE_OZONE)
ui::InputDeviceController input_device_controller_;
#endif
#endif

DISALLOW_COPY_AND_ASSIGN(IOThreadContext);
};

class ChromeService::ExtraParts : public ChromeBrowserMainExtraParts {
public:
explicit ExtraParts(ChromeService* service) : service_(service) {}
~ExtraParts() override = default;

private:
void ServiceManagerConnectionStarted(
content::ServiceManagerConnection* connection) override {
connection->GetConnector()->StartService(
service_manager::Identity(chrome::mojom::kServiceName));
service_->InitConnector();
}

ChromeService* service_;

DISALLOW_COPY_AND_ASSIGN(ExtraParts);
};

// static
ChromeService* ChromeService::GetInstance() {
if (!g_instance)
g_instance = new ChromeService;
return g_instance;
}

ChromeBrowserMainExtraParts* ChromeService::CreateExtraParts() {
return new ExtraParts(this);
}

service_manager::EmbeddedServiceInfo::ServiceFactory
ChromeService::CreateChromeServiceFactory() {
return base::BindRepeating(&ChromeService::CreateChromeServiceWrapper,
base::Unretained(this));
}

ChromeService::ChromeService()
: io_thread_context_(std::make_unique<IOThreadContext>()) {}
ChromeService::~ChromeService() {}

void ChromeService::OnBindInterface(
const service_manager::BindSourceInfo& remote_info,
const std::string& name,
mojo::ScopedMessagePipeHandle handle) {
content::OverrideOnBindInterface(remote_info, name, &handle);
if (!handle.is_valid())
return;
void ChromeService::InitConnector() {
service_manager::mojom::ConnectorRequest request;
connector_ = service_manager::Connector::Create(&request);
io_thread_context_->Init(std::move(request));
}

if (!registry_.TryBindInterface(name, &handle))
registry_with_source_info_.TryBindInterface(name, &handle, remote_info);
std::unique_ptr<service_manager::Service>
ChromeService::CreateChromeServiceWrapper() {
return std::make_unique<service_manager::ForwardingService>(
io_thread_context_.get());
}
Loading

0 comments on commit 1633130

Please sign in to comment.