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