зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1253123 - Remove ipc_channel_proxy (r=jld) a=kwierso
MozReview-Commit-ID: CwO2Ufog5qX --HG-- extra : source : d345149b216921e65dfe6c1ed86cc7c61941bb78
This commit is contained in:
Родитель
bdd97ae771
Коммит
698aacacb8
|
@ -34,7 +34,6 @@ UNIFIED_SOURCES += [
|
|||
'src/chrome/common/child_thread.cc',
|
||||
'src/chrome/common/chrome_switches.cc',
|
||||
'src/chrome/common/ipc_channel.cc',
|
||||
'src/chrome/common/ipc_channel_proxy.cc',
|
||||
'src/chrome/common/ipc_message.cc',
|
||||
'src/chrome/common/message_router.cc',
|
||||
'src/chrome/common/notification_service.cc',
|
||||
|
|
|
@ -1,275 +0,0 @@
|
|||
// Copyright (c) 2006-2008 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 "base/message_loop.h"
|
||||
#include "base/thread.h"
|
||||
#include "chrome/common/ipc_channel_proxy.h"
|
||||
#include "chrome/common/ipc_message_utils.h"
|
||||
|
||||
namespace IPC {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ChannelProxy::Context::Context(Channel::Listener* listener,
|
||||
MessageFilter* filter,
|
||||
MessageLoop* ipc_message_loop)
|
||||
: listener_message_loop_(MessageLoop::current()),
|
||||
listener_(listener),
|
||||
ipc_message_loop_(ipc_message_loop),
|
||||
channel_(NULL),
|
||||
peer_pid_(0),
|
||||
channel_connected_called_(false) {
|
||||
if (filter)
|
||||
filters_.push_back(filter);
|
||||
}
|
||||
|
||||
void ChannelProxy::Context::CreateChannel(const std::wstring& id,
|
||||
const Channel::Mode& mode) {
|
||||
DCHECK(channel_ == NULL);
|
||||
channel_id_ = id;
|
||||
channel_ = new Channel(id, mode, this);
|
||||
}
|
||||
|
||||
bool ChannelProxy::Context::TryFilters(const Message& message) {
|
||||
|
||||
for (size_t i = 0; i < filters_.size(); ++i) {
|
||||
if (filters_[i]->OnMessageReceived(message)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnMessageReceived(const Message& message) {
|
||||
// First give a chance to the filters to process this message.
|
||||
if (!TryFilters(message))
|
||||
OnMessageReceivedNoFilter(message);
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) {
|
||||
// NOTE: This code relies on the listener's message loop not going away while
|
||||
// this thread is active. That should be a reasonable assumption, but it
|
||||
// feels risky. We may want to invent some more indirect way of referring to
|
||||
// a MessageLoop if this becomes a problem.
|
||||
listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
this, &Context::OnDispatchMessage, message));
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnChannelConnected(int32_t peer_pid) {
|
||||
peer_pid_ = peer_pid;
|
||||
for (size_t i = 0; i < filters_.size(); ++i)
|
||||
filters_[i]->OnChannelConnected(peer_pid);
|
||||
|
||||
// See above comment about using listener_message_loop_ here.
|
||||
listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
this, &Context::OnDispatchConnected));
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnChannelError() {
|
||||
for (size_t i = 0; i < filters_.size(); ++i)
|
||||
filters_[i]->OnChannelError();
|
||||
|
||||
// See above comment about using listener_message_loop_ here.
|
||||
listener_message_loop_->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
this, &Context::OnDispatchError));
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnChannelOpened() {
|
||||
DCHECK(channel_ != NULL);
|
||||
|
||||
// Assume a reference to ourselves on behalf of this thread. This reference
|
||||
// will be released when we are closed.
|
||||
AddRef();
|
||||
|
||||
if (!channel_->Connect()) {
|
||||
OnChannelError();
|
||||
return;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < filters_.size(); ++i)
|
||||
filters_[i]->OnFilterAdded(channel_);
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnChannelClosed() {
|
||||
// It's okay for IPC::ChannelProxy::Close to be called more than once, which
|
||||
// would result in this branch being taken.
|
||||
if (!channel_)
|
||||
return;
|
||||
|
||||
for (size_t i = 0; i < filters_.size(); ++i) {
|
||||
filters_[i]->OnChannelClosing();
|
||||
filters_[i]->OnFilterRemoved();
|
||||
}
|
||||
|
||||
// We don't need the filters anymore.
|
||||
filters_.clear();
|
||||
|
||||
delete channel_;
|
||||
channel_ = NULL;
|
||||
|
||||
// Balance with the reference taken during startup. This may result in
|
||||
// self-destruction.
|
||||
Release();
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnSendMessage(Message* message) {
|
||||
if (!channel_->Send(message))
|
||||
OnChannelError();
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnAddFilter(MessageFilter* filter) {
|
||||
filters_.push_back(filter);
|
||||
|
||||
// If the channel has already been created, then we need to send this message
|
||||
// so that the filter gets access to the Channel.
|
||||
if (channel_)
|
||||
filter->OnFilterAdded(channel_);
|
||||
|
||||
// Balances the AddRef in ChannelProxy::AddFilter.
|
||||
filter->Release();
|
||||
}
|
||||
|
||||
// Called on the IPC::Channel thread
|
||||
void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) {
|
||||
for (size_t i = 0; i < filters_.size(); ++i) {
|
||||
if (filters_[i].get() == filter) {
|
||||
filter->OnFilterRemoved();
|
||||
filters_.erase(filters_.begin() + i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
NOTREACHED() << "filter to be removed not found";
|
||||
}
|
||||
|
||||
// Called on the listener's thread
|
||||
void ChannelProxy::Context::OnDispatchMessage(const Message& message) {
|
||||
if (!listener_)
|
||||
return;
|
||||
|
||||
OnDispatchConnected();
|
||||
|
||||
|
||||
listener_->OnMessageReceived(message);
|
||||
|
||||
}
|
||||
|
||||
// Called on the listener's thread
|
||||
void ChannelProxy::Context::OnDispatchConnected() {
|
||||
if (channel_connected_called_)
|
||||
return;
|
||||
|
||||
channel_connected_called_ = true;
|
||||
if (listener_)
|
||||
listener_->OnChannelConnected(peer_pid_);
|
||||
}
|
||||
|
||||
// Called on the listener's thread
|
||||
void ChannelProxy::Context::OnDispatchError() {
|
||||
if (listener_)
|
||||
listener_->OnChannelError();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
ChannelProxy::ChannelProxy(const std::wstring& channel_id, Channel::Mode mode,
|
||||
Channel::Listener* listener, MessageFilter* filter,
|
||||
MessageLoop* ipc_thread)
|
||||
: context_(new Context(listener, filter, ipc_thread)) {
|
||||
Init(channel_id, mode, ipc_thread, true);
|
||||
}
|
||||
|
||||
ChannelProxy::ChannelProxy(const std::wstring& channel_id, Channel::Mode mode,
|
||||
MessageLoop* ipc_thread, Context* context,
|
||||
bool create_pipe_now)
|
||||
: context_(context) {
|
||||
Init(channel_id, mode, ipc_thread, create_pipe_now);
|
||||
}
|
||||
|
||||
void ChannelProxy::Init(const std::wstring& channel_id, Channel::Mode mode,
|
||||
MessageLoop* ipc_thread_loop, bool create_pipe_now) {
|
||||
if (create_pipe_now) {
|
||||
// Create the channel immediately. This effectively sets up the
|
||||
// low-level pipe so that the client can connect. Without creating
|
||||
// the pipe immediately, it is possible for a listener to attempt
|
||||
// to connect and get an error since the pipe doesn't exist yet.
|
||||
context_->CreateChannel(channel_id, mode);
|
||||
} else {
|
||||
#if defined(OS_POSIX)
|
||||
// TODO(playmobil): On POSIX, IPC::Channel uses a socketpair(), one side of
|
||||
// which needs to be mapped into the child process' address space.
|
||||
// To know the value of the client side FD we need to have already
|
||||
// created a socketpair which currently occurs in IPC::Channel's
|
||||
// constructor.
|
||||
// If we lazilly construct the IPC::Channel then the caller has no way
|
||||
// of knowing the FD #.
|
||||
//
|
||||
// We can solve this either by having the Channel's creation launch the
|
||||
// subprocess itself or by creating the socketpair() externally.
|
||||
NOTIMPLEMENTED();
|
||||
#endif // defined(OS_POSIX)
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::CreateChannel, channel_id, mode));
|
||||
}
|
||||
|
||||
// complete initialization on the background thread
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::OnChannelOpened));
|
||||
}
|
||||
|
||||
void ChannelProxy::Close() {
|
||||
// Clear the backpointer to the listener so that any pending calls to
|
||||
// Context::OnDispatchMessage or OnDispatchError will be ignored. It is
|
||||
// possible that the channel could be closed while it is receiving messages!
|
||||
context_->Clear();
|
||||
|
||||
if (context_->ipc_message_loop()) {
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::OnChannelClosed));
|
||||
}
|
||||
}
|
||||
|
||||
bool ChannelProxy::Send(Message* message) {
|
||||
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::OnSendMessage, message));
|
||||
return true;
|
||||
}
|
||||
|
||||
void ChannelProxy::AddFilter(MessageFilter* filter) {
|
||||
// We want to addref the filter to prevent it from
|
||||
// being destroyed before the OnAddFilter call is invoked.
|
||||
filter->AddRef();
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::OnAddFilter, filter));
|
||||
}
|
||||
|
||||
void ChannelProxy::RemoveFilter(MessageFilter* filter) {
|
||||
context_->ipc_message_loop()->PostTask(FROM_HERE, NewRunnableMethod(
|
||||
context_.get(), &Context::OnRemoveFilter, filter));
|
||||
}
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
// See the TODO regarding lazy initialization of the channel in
|
||||
// ChannelProxy::Init().
|
||||
// We assume that IPC::Channel::GetClientFileDescriptorMapping() is thread-safe.
|
||||
void ChannelProxy::GetClientFileDescriptorMapping(int *src_fd,
|
||||
int *dest_fd) const {
|
||||
Channel *channel = context_.get()->channel_;
|
||||
DCHECK(channel); // Channel must have been created first.
|
||||
channel->GetClientFileDescriptorMapping(src_fd, dest_fd);
|
||||
}
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
} // namespace IPC
|
|
@ -1,213 +0,0 @@
|
|||
// Copyright (c) 2006-2008 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 CHROME_COMMON_IPC_CHANNEL_PROXY_H__
|
||||
#define CHROME_COMMON_IPC_CHANNEL_PROXY_H__
|
||||
|
||||
#include <vector>
|
||||
#include "base/lock.h"
|
||||
#include "chrome/common/ipc_channel.h"
|
||||
#include "nsISupportsImpl.h"
|
||||
#include "nsAutoPtr.h"
|
||||
|
||||
class MessageLoop;
|
||||
|
||||
namespace IPC {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// IPC::ChannelProxy
|
||||
//
|
||||
// This class is a helper class that is useful when you wish to run an IPC
|
||||
// channel on a background thread. It provides you with the option of either
|
||||
// handling IPC messages on that background thread or having them dispatched to
|
||||
// your main thread (the thread on which the IPC::ChannelProxy is created).
|
||||
//
|
||||
// The API for an IPC::ChannelProxy is very similar to that of an IPC::Channel.
|
||||
// When you send a message to an IPC::ChannelProxy, the message is routed to
|
||||
// the background thread, where it is then passed to the IPC::Channel's Send
|
||||
// method. This means that you can send a message from your thread and your
|
||||
// message will be sent over the IPC channel when possible instead of being
|
||||
// delayed until your thread returns to its message loop. (Often IPC messages
|
||||
// will queue up on the IPC::Channel when there is a lot of traffic, and the
|
||||
// channel will not get cycles to flush its message queue until the thread, on
|
||||
// which it is running, returns to its message loop.)
|
||||
//
|
||||
// An IPC::ChannelProxy can have a MessageFilter associated with it, which will
|
||||
// be notified of incoming messages on the IPC::Channel's thread. This gives
|
||||
// the consumer of IPC::ChannelProxy the ability to respond to incoming
|
||||
// messages on this background thread instead of on their own thread, which may
|
||||
// be bogged down with other processing. The result can be greatly improved
|
||||
// latency for messages that can be handled on a background thread.
|
||||
//
|
||||
// The consumer of IPC::ChannelProxy is responsible for allocating the Thread
|
||||
// instance where the IPC::Channel will be created and operated.
|
||||
//
|
||||
class ChannelProxy : public Message::Sender {
|
||||
public:
|
||||
// A class that receives messages on the thread where the IPC channel is
|
||||
// running. It can choose to prevent the default action for an IPC message.
|
||||
class MessageFilter {
|
||||
public:
|
||||
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MessageFilter)
|
||||
|
||||
// Called on the background thread to provide the filter with access to the
|
||||
// channel. Called when the IPC channel is initialized or when AddFilter
|
||||
// is called if the channel is already initialized.
|
||||
virtual void OnFilterAdded(Channel* channel) {}
|
||||
|
||||
// Called on the background thread when the filter has been removed from
|
||||
// the ChannelProxy and when the Channel is closing. After a filter is
|
||||
// removed, it will not be called again.
|
||||
virtual void OnFilterRemoved() {}
|
||||
|
||||
// Called to inform the filter that the IPC channel is connected and we
|
||||
// have received the internal Hello message from the peer.
|
||||
virtual void OnChannelConnected(int32_t peer_pid) {}
|
||||
|
||||
// Called when there is an error on the channel, typically that the channel
|
||||
// has been closed.
|
||||
virtual void OnChannelError() {}
|
||||
|
||||
// Called to inform the filter that the IPC channel will be destroyed.
|
||||
// OnFilterRemoved is called immediately after this.
|
||||
virtual void OnChannelClosing() {}
|
||||
|
||||
// Return true to indicate that the message was handled, or false to let
|
||||
// the message be handled in the default way.
|
||||
virtual bool OnMessageReceived(const Message& message) {
|
||||
return false;
|
||||
}
|
||||
protected:
|
||||
virtual ~MessageFilter() {}
|
||||
};
|
||||
|
||||
// Initializes a channel proxy. The channel_id and mode parameters are
|
||||
// passed directly to the underlying IPC::Channel. The listener is called on
|
||||
// the thread that creates the ChannelProxy. The filter's OnMessageReceived
|
||||
// method is called on the thread where the IPC::Channel is running. The
|
||||
// filter may be null if the consumer is not interested in handling messages
|
||||
// on the background thread. Any message not handled by the filter will be
|
||||
// dispatched to the listener. The given message loop indicates where the
|
||||
// IPC::Channel should be created.
|
||||
ChannelProxy(const std::wstring& channel_id, Channel::Mode mode,
|
||||
Channel::Listener* listener, MessageFilter* filter,
|
||||
MessageLoop* ipc_thread_loop);
|
||||
|
||||
~ChannelProxy() {
|
||||
Close();
|
||||
}
|
||||
|
||||
// Close the IPC::Channel. This operation completes asynchronously, once the
|
||||
// background thread processes the command to close the channel. It is ok to
|
||||
// call this method multiple times. Redundant calls are ignored.
|
||||
//
|
||||
// WARNING: The MessageFilter object held by the ChannelProxy is also
|
||||
// released asynchronously, and it may in fact have its final reference
|
||||
// released on the background thread. The caller should be careful to deal
|
||||
// with / allow for this possibility.
|
||||
void Close();
|
||||
|
||||
// Send a message asynchronously. The message is routed to the background
|
||||
// thread where it is passed to the IPC::Channel's Send method.
|
||||
virtual bool Send(Message* message);
|
||||
|
||||
// Used to intercept messages as they are received on the background thread.
|
||||
//
|
||||
// Ordinarily, messages sent to the ChannelProxy are routed to the matching
|
||||
// listener on the worker thread. This API allows code to intercept messages
|
||||
// before they are sent to the worker thread.
|
||||
void AddFilter(MessageFilter* filter);
|
||||
void RemoveFilter(MessageFilter* filter);
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
// Calls through to the underlying channel's methods.
|
||||
// TODO(playmobil): For now this is only implemented in the case of
|
||||
// create_pipe_now = true, we need to figure this out for the latter case.
|
||||
void GetClientFileDescriptorMapping(int *src_fd, int *dest_fd) const;
|
||||
#endif // defined(OS_POSIX)
|
||||
|
||||
protected:
|
||||
class Context;
|
||||
// A subclass uses this constructor if it needs to add more information
|
||||
// to the internal state. If create_pipe_now is true, the pipe is created
|
||||
// immediately. Otherwise it's created on the IO thread.
|
||||
ChannelProxy(const std::wstring& channel_id, Channel::Mode mode,
|
||||
MessageLoop* ipc_thread_loop, Context* context,
|
||||
bool create_pipe_now);
|
||||
|
||||
// Used internally to hold state that is referenced on the IPC thread.
|
||||
class Context : public Channel::Listener {
|
||||
public:
|
||||
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Context)
|
||||
Context(Channel::Listener* listener, MessageFilter* filter,
|
||||
MessageLoop* ipc_thread);
|
||||
MessageLoop* ipc_message_loop() const { return ipc_message_loop_; }
|
||||
const std::wstring& channel_id() const { return channel_id_; }
|
||||
|
||||
// Dispatches a message on the listener thread.
|
||||
void OnDispatchMessage(const Message& message);
|
||||
|
||||
protected:
|
||||
virtual ~Context() {}
|
||||
|
||||
// IPC::Channel::Listener methods:
|
||||
virtual void OnMessageReceived(const Message& message);
|
||||
virtual void OnChannelConnected(int32_t peer_pid);
|
||||
virtual void OnChannelError();
|
||||
|
||||
// Like OnMessageReceived but doesn't try the filters.
|
||||
void OnMessageReceivedNoFilter(const Message& message);
|
||||
|
||||
// Gives the filters a chance at processing |message|.
|
||||
// Returns true if the message was processed, false otherwise.
|
||||
bool TryFilters(const Message& message);
|
||||
|
||||
// Like Open and Close, but called on the IPC thread.
|
||||
virtual void OnChannelOpened();
|
||||
virtual void OnChannelClosed();
|
||||
|
||||
// Called on the consumers thread when the ChannelProxy is closed. At that
|
||||
// point the consumer is telling us that they don't want to receive any
|
||||
// more messages, so we honor that wish by forgetting them!
|
||||
virtual void Clear() { listener_ = NULL; }
|
||||
|
||||
private:
|
||||
friend class ChannelProxy;
|
||||
// Create the Channel
|
||||
void CreateChannel(const std::wstring& id, const Channel::Mode& mode);
|
||||
|
||||
// Methods called via InvokeLater:
|
||||
void OnSendMessage(Message* message_ptr);
|
||||
void OnAddFilter(MessageFilter* filter);
|
||||
void OnRemoveFilter(MessageFilter* filter);
|
||||
void OnDispatchConnected();
|
||||
void OnDispatchError();
|
||||
|
||||
MessageLoop* listener_message_loop_;
|
||||
Channel::Listener* listener_;
|
||||
|
||||
// List of filters. This is only accessed on the IPC thread.
|
||||
std::vector<RefPtr<MessageFilter> > filters_;
|
||||
MessageLoop* ipc_message_loop_;
|
||||
Channel* channel_;
|
||||
std::wstring channel_id_;
|
||||
int peer_pid_;
|
||||
bool channel_connected_called_;
|
||||
};
|
||||
|
||||
Context* context() { return context_; }
|
||||
|
||||
private:
|
||||
void Init(const std::wstring& channel_id, Channel::Mode mode,
|
||||
MessageLoop* ipc_thread_loop, bool create_pipe_now);
|
||||
|
||||
// By maintaining this indirection (ref-counted) to our internal state, we
|
||||
// can safely be destroyed while the background thread continues to do stuff
|
||||
// that involves this data.
|
||||
RefPtr<Context> context_;
|
||||
};
|
||||
|
||||
} // namespace IPC
|
||||
|
||||
#endif // CHROME_COMMON_IPC_CHANNEL_PROXY_H__
|
Загрузка…
Ссылка в новой задаче