Commit 56de0e5c authored by amistry's avatar amistry Committed by Commit bot

Add a ServiceRegistry::ConnectToRemoteService that takes an InterfaceRequest<>.

And change the task manager to use it.

Review URL: https://codereview.chromium.org/1137803005

Cr-Commit-Position: refs/heads/master@{#330881}
parent 5364de18
......@@ -22,13 +22,31 @@
// about://memory-internals.
//
// To create:
// 1. Obtain a ResourceUsageReporter connection using the child process's
// service registry. i.e:
// ResourceUsageReporterPtr service;
// process->GetServiceRegistry()->ConnectToRemoteService(&service);
// 2. If needed, the connection can be passed to another thread using
// ResourceUsageReporterPtr::PassInterface().
// 3. Pass the service to the constructor.
// 1. Create a ResourceUsageReporterPtr and obtain an InterfaceRequest<> using
// mojo::GetProxy.
// 2. Use the child process's service registry to connect to the service using
// the InterfaceRequest<>. Note, ServiceRegistry is thread hostile and
// must always be accessed from the same thread. However, InterfaceRequest<>
// can be passed safely between threads, and therefore a task can be posted
// to the ServiceRegistry thread to connect to the remote service.
// 3. Pass the ResourceUsageReporterPtr to the constructor.
//
// Example:
// void Foo::ConnectToService(
// mojo::InterfaceRequest<ResourceUsageReporter> req) {
// content::ServiceRegistry* registry = host_->GetServiceRegistry();
// registry->ConnectToRemoteService(req.Pass());
// }
//
// ...
// ResourceUsageReporterPtr service;
// mojo::InterfaceRequest<ResourceUsageReporter> request =
// mojo::GetProxy(&service);
// content::BrowserThread::PostTask(
// content::BrowserThread::IO, FROM_HERE,
// base::Bind(&Foo::ConnectToService, this, base::Passed(&request)));
// resource_usage_.reset(new ProcessResourceUsage(service.Pass()));
// ...
//
// Note: ProcessResourceUsage is thread-hostile and must live on a single
// thread.
......
......@@ -60,11 +60,8 @@ class ChildProcessResource : public Resource {
// process would be "Plugin: Flash" when name is "Flash".
base::string16 GetLocalizedTitle() const;
static mojo::InterfacePtrInfo<ResourceUsageReporter>
GetProcessUsageOnIOThread(int id);
void OnGetProcessUsageDone(
mojo::InterfacePtrInfo<ResourceUsageReporter> info);
static void ConnectResourceReporterOnIOThread(
int id, mojo::InterfaceRequest<ResourceUsageReporter> req);
int process_type_;
base::string16 name_;
......@@ -88,21 +85,19 @@ class ChildProcessResource : public Resource {
gfx::ImageSkia* ChildProcessResource::default_icon_ = NULL;
// static
mojo::InterfacePtrInfo<ResourceUsageReporter>
ChildProcessResource::GetProcessUsageOnIOThread(int id) {
void ChildProcessResource::ConnectResourceReporterOnIOThread(
int id, mojo::InterfaceRequest<ResourceUsageReporter> req) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
content::BrowserChildProcessHost* host =
content::BrowserChildProcessHost::FromID(id);
if (!host)
return mojo::InterfacePtrInfo<ResourceUsageReporter>();
return;
content::ServiceRegistry* registry = host->GetServiceRegistry();
if (!registry)
return mojo::InterfacePtrInfo<ResourceUsageReporter>();
return;
ResourceUsageReporterPtr service;
registry->ConnectToRemoteService(&service);
return service.PassInterface().Pass();
registry->ConnectToRemoteService(req.Pass());
}
ChildProcessResource::ChildProcessResource(int process_type,
......@@ -123,27 +118,19 @@ ChildProcessResource::ChildProcessResource(int process_type,
default_icon_ = rb.GetImageSkiaNamed(IDR_PLUGINS_FAVICON);
// TODO(jabdelmalek): use different icon for web workers.
}
BrowserThread::PostTaskAndReplyWithResult(
ResourceUsageReporterPtr service;
mojo::InterfaceRequest<ResourceUsageReporter> request =
mojo::GetProxy(&service);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&ChildProcessResource::GetProcessUsageOnIOThread,
unique_process_id),
base::Bind(&ChildProcessResource::OnGetProcessUsageDone,
weak_factory_.GetWeakPtr()));
base::Bind(&ChildProcessResource::ConnectResourceReporterOnIOThread,
unique_process_id, base::Passed(&request)));
resource_usage_.reset(new ProcessResourceUsage(service.Pass()));
}
ChildProcessResource::~ChildProcessResource() {
}
void ChildProcessResource::OnGetProcessUsageDone(
mojo::InterfacePtrInfo<ResourceUsageReporter> info) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (info.is_valid()) {
ResourceUsageReporterPtr service;
service.Bind(info.Pass());
resource_usage_.reset(new ProcessResourceUsage(service.Pass()));
}
}
// Resource methods:
base::string16 ChildProcessResource::GetTitle() const {
if (title_.empty())
......
......@@ -54,8 +54,11 @@ class CONTENT_EXPORT ServiceRegistry {
// Connect to an interface provided by the remote service provider.
template <typename Interface>
void ConnectToRemoteService(mojo::InterfacePtr<Interface>* ptr) {
ConnectToRemoteService(Interface::Name_,
mojo::GetProxy(ptr).PassMessagePipe());
ConnectToRemoteService(mojo::GetProxy(ptr));
}
template <typename Interface>
void ConnectToRemoteService(mojo::InterfaceRequest<Interface> ptr) {
ConnectToRemoteService(Interface::Name_, ptr.PassMessagePipe());
}
virtual void ConnectToRemoteService(const base::StringPiece& name,
mojo::ScopedMessagePipeHandle handle) = 0;
......
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