namespace IPC {
+class ChannelFactory;
+class MessageFilter;
+class MessageFilterRouter;
class SendCallbackHelper;
//-----------------------------------------------------------------------------
//
class IPC_EXPORT ChannelProxy : public Sender, public base::NonThreadSafe {
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 IPC_EXPORT MessageFilter
- : public base::RefCountedThreadSafe<MessageFilter> {
- public:
- 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 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);
-
- protected:
- virtual ~MessageFilter();
-
- private:
- friend class base::RefCountedThreadSafe<MessageFilter>;
- };
-
// Initializes a channel proxy. The channel_handle 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
// on the background thread. Any message not handled by the filter will be
// dispatched to the listener. The given task runner correspond to a thread
// on which IPC::Channel is created and used (e.g. IO thread).
- ChannelProxy(const IPC::ChannelHandle& channel_handle,
- Channel::Mode mode,
- Listener* listener,
- base::SingleThreadTaskRunner* ipc_task_runner);
+ static scoped_ptr<ChannelProxy> Create(
+ const IPC::ChannelHandle& channel_handle,
+ Channel::Mode mode,
+ Listener* listener,
+ const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
- virtual ~ChannelProxy();
+ static scoped_ptr<ChannelProxy> Create(
+ scoped_ptr<ChannelFactory> factory,
+ Listener* listener,
+ const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
+
+ ~ChannelProxy() override;
// Initializes the channel proxy. Only call this once to initialize a channel
// proxy that was not initialized in its constructor. If create_pipe_now is
// thread.
void Init(const IPC::ChannelHandle& channel_handle, Channel::Mode mode,
bool create_pipe_now);
+ void Init(scoped_ptr<ChannelFactory> factory, bool create_pipe_now);
// 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
+ // WARNING: MessageFilter objects 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.
// 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) OVERRIDE;
+ bool Send(Message* message) override;
// Used to intercept messages as they are received on the background thread.
//
// Get the process ID for the connected peer.
// Returns base::kNullProcessId if the peer is not connected yet.
- base::ProcessId peer_pid() const { return context_->peer_pid_; }
+ base::ProcessId GetPeerPID() const { return context_->peer_pid_; }
-#if defined(OS_POSIX) && !defined(OS_NACL)
+#if defined(OS_POSIX) && !defined(OS_NACL_SFI)
// Calls through to the underlying channel's methods.
int GetClientFileDescriptor();
- int TakeClientFileDescriptor();
- bool GetPeerEuid(uid_t* peer_euid) const;
-#endif // defined(OS_POSIX)
+ base::ScopedFD TakeClientFileDescriptor();
+#endif
protected:
class Context;
// to the internal state.
ChannelProxy(Context* context);
+ ChannelProxy(
+ Listener* listener,
+ const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner);
+
// Used internally to hold state that is referenced on the IPC thread.
class Context : public base::RefCountedThreadSafe<Context>,
public Listener {
public:
- Context(Listener* listener, base::SingleThreadTaskRunner* ipc_thread);
+ Context(Listener* listener,
+ const scoped_refptr<base::SingleThreadTaskRunner>& ipc_thread);
void ClearIPCTaskRunner();
base::SingleThreadTaskRunner* ipc_task_runner() const {
return ipc_task_runner_.get();
protected:
friend class base::RefCountedThreadSafe<Context>;
- virtual ~Context();
+ ~Context() override;
// IPC::Listener methods:
- virtual bool OnMessageReceived(const Message& message) OVERRIDE;
- virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
- virtual void OnChannelError() OVERRIDE;
+ bool OnMessageReceived(const Message& message) override;
+ void OnChannelConnected(int32 peer_pid) override;
+ void OnChannelError() override;
// Like OnMessageReceived but doesn't try the filters.
bool OnMessageReceivedNoFilter(const Message& message);
friend class SendCallbackHelper;
// Create the Channel
- void CreateChannel(const IPC::ChannelHandle& channel_handle,
- const Channel::Mode& mode);
+ void CreateChannel(scoped_ptr<ChannelFactory> factory);
// Methods called on the IO thread.
void OnSendMessage(scoped_ptr<Message> message_ptr);
void AddFilter(MessageFilter* filter);
void OnDispatchConnected();
void OnDispatchError();
+ void OnDispatchBadMessage(const Message& message);
scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
Listener* listener_;
// List of filters. This is only accessed on the IPC thread.
std::vector<scoped_refptr<MessageFilter> > filters_;
scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
+
+ // Note, channel_ may be set on the Listener thread or the IPC thread.
+ // But once it has been set, it must only be read or cleared on the IPC
+ // thread.
scoped_ptr<Channel> channel_;
std::string channel_id_;
bool channel_connected_called_;
+ // Routes a given message to a proper subset of |filters_|, depending
+ // on which message classes a filter might support.
+ scoped_ptr<MessageFilterRouter> message_filter_router_;
+
// Holds filters between the AddFilter call on the listerner thread and the
// IPC thread when they're added to filters_.
std::vector<scoped_refptr<MessageFilter> > pending_filters_;