2009-07-13 14:55:04 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-07-13 14:55:04 -07:00
|
|
|
|
|
|
|
#ifndef ipc_glue_AsyncChannel_h
|
|
|
|
#define ipc_glue_AsyncChannel_h 1
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
|
2011-04-29 12:21:57 -07:00
|
|
|
#include "mozilla/Monitor.h"
|
2011-06-03 11:33:56 -07:00
|
|
|
#include "mozilla/ipc/Transport.h"
|
2009-07-15 14:38:55 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2009-10-27 20:31:04 -07:00
|
|
|
struct HasResultCodes
|
|
|
|
{
|
|
|
|
enum Result {
|
|
|
|
MsgProcessed,
|
2010-08-20 16:24:40 -07:00
|
|
|
MsgDropped,
|
2009-10-27 20:31:04 -07:00
|
|
|
MsgNotKnown,
|
|
|
|
MsgNotAllowed,
|
|
|
|
MsgPayloadError,
|
2010-05-22 12:35:29 -07:00
|
|
|
MsgProcessingError,
|
2009-10-27 20:31:04 -07:00
|
|
|
MsgRouteError,
|
2012-01-12 03:07:50 -08:00
|
|
|
MsgValueError
|
2009-10-27 20:31:04 -07:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
|
|
|
|
class RefCountedMonitor : public Monitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefCountedMonitor()
|
|
|
|
: Monitor("mozilla.ipc.AsyncChannel.mMonitor")
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(RefCountedMonitor)
|
|
|
|
};
|
|
|
|
|
|
|
|
class AsyncChannel : protected HasResultCodes
|
2009-07-13 14:55:04 -07:00
|
|
|
{
|
|
|
|
protected:
|
2011-04-29 12:21:57 -07:00
|
|
|
typedef mozilla::Monitor Monitor;
|
2009-09-01 09:27:09 -07:00
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
enum ChannelState {
|
|
|
|
ChannelClosed,
|
|
|
|
ChannelOpening,
|
2009-08-19 08:44:56 -07:00
|
|
|
ChannelConnected,
|
2010-02-09 16:02:54 -08:00
|
|
|
ChannelTimeout,
|
2009-12-03 00:16:28 -08:00
|
|
|
ChannelClosing,
|
2009-07-13 14:55:04 -07:00
|
|
|
ChannelError
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef IPC::Message Message;
|
2011-06-03 11:33:56 -07:00
|
|
|
typedef mozilla::ipc::Transport Transport;
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2009-10-27 20:31:04 -07:00
|
|
|
class /*NS_INTERFACE_CLASS*/ AsyncListener: protected HasResultCodes
|
2009-07-13 14:55:04 -07:00
|
|
|
{
|
|
|
|
public:
|
2009-07-15 15:06:30 -07:00
|
|
|
virtual ~AsyncListener() { }
|
2009-12-03 00:16:28 -08:00
|
|
|
|
|
|
|
virtual void OnChannelClose() = 0;
|
|
|
|
virtual void OnChannelError() = 0;
|
2009-07-13 14:55:04 -07:00
|
|
|
virtual Result OnMessageReceived(const Message& aMessage) = 0;
|
2010-08-20 16:24:40 -07:00
|
|
|
virtual void OnProcessingError(Result aError) = 0;
|
2012-09-17 01:37:20 -07:00
|
|
|
virtual void OnChannelConnected(int32_t peer_pid) {};
|
2009-07-13 14:55:04 -07:00
|
|
|
};
|
|
|
|
|
2011-06-03 11:33:55 -07:00
|
|
|
enum Side { Parent, Child, Unknown };
|
|
|
|
|
2009-10-27 20:31:04 -07:00
|
|
|
public:
|
2009-12-03 00:16:28 -08:00
|
|
|
//
|
|
|
|
// These methods are called on the "worker" thread
|
|
|
|
//
|
2009-11-12 14:16:54 -08:00
|
|
|
AsyncChannel(AsyncListener* aListener);
|
|
|
|
virtual ~AsyncChannel();
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2010-01-12 00:25:18 -08:00
|
|
|
// "Open" from the perspective of the transport layer; the underlying
|
2009-09-01 09:27:09 -07:00
|
|
|
// socketpair/pipe should already be created.
|
|
|
|
//
|
|
|
|
// Returns true iff the transport layer was successfully connected,
|
|
|
|
// i.e., mChannelState == ChannelConnected.
|
2011-06-03 11:33:55 -07:00
|
|
|
bool Open(Transport* aTransport, MessageLoop* aIOLoop=0, Side aSide=Unknown);
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2011-11-30 08:26:16 -08:00
|
|
|
// "Open" a connection to another thread in the same process.
|
|
|
|
//
|
|
|
|
// Returns true iff the transport layer was successfully connected,
|
|
|
|
// i.e., mChannelState == ChannelConnected.
|
|
|
|
//
|
|
|
|
// For more details on the process of opening a channel between
|
|
|
|
// threads, see the extended comment on this function
|
|
|
|
// in AsyncChannel.cpp.
|
|
|
|
bool Open(AsyncChannel *aTargetChan, MessageLoop *aTargetLoop, Side aSide);
|
|
|
|
|
2009-11-03 13:37:07 -08:00
|
|
|
// Close the underlying transport channel.
|
2009-07-13 14:55:04 -07:00
|
|
|
void Close();
|
|
|
|
|
2012-07-05 11:48:40 -07:00
|
|
|
// Force the channel to behave as if a channel error occurred. Valid
|
|
|
|
// for process links only, not thread links.
|
|
|
|
void CloseWithError();
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
// Asynchronously send a message to the other side of the channel
|
2010-02-16 10:44:21 -08:00
|
|
|
virtual bool Send(Message* msg);
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2011-06-03 11:33:56 -07:00
|
|
|
// Asynchronously deliver a message back to this side of the
|
|
|
|
// channel
|
|
|
|
virtual bool Echo(Message* msg);
|
|
|
|
|
2010-10-08 16:24:36 -07:00
|
|
|
// Send OnChannelConnected notification to listeners.
|
2012-09-17 01:37:20 -07:00
|
|
|
void DispatchOnChannelConnected(int32_t peer_pid);
|
2010-10-08 16:24:36 -07:00
|
|
|
|
2009-12-03 00:16:28 -08:00
|
|
|
//
|
2011-11-30 08:24:46 -08:00
|
|
|
// Each AsyncChannel is associated with either a ProcessLink or a
|
|
|
|
// ThreadLink via the field mLink. The type of link is determined
|
|
|
|
// by whether this AsyncChannel is communicating with another
|
|
|
|
// process or another thread. In the former case, file
|
|
|
|
// descriptors or a socket are used via the I/O queue. In the
|
|
|
|
// latter case, messages are enqueued directly onto the target
|
|
|
|
// thread's work queue.
|
2009-12-03 00:16:28 -08:00
|
|
|
//
|
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
class Link {
|
|
|
|
protected:
|
|
|
|
AsyncChannel *mChan;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Link(AsyncChannel *aChan);
|
|
|
|
virtual ~Link();
|
|
|
|
|
|
|
|
// n.b.: These methods all require that the channel monitor is
|
|
|
|
// held when they are invoked.
|
|
|
|
virtual void EchoMessage(Message *msg) = 0;
|
|
|
|
virtual void SendMessage(Message *msg) = 0;
|
|
|
|
virtual void SendClose() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcessLink : public Link, public Transport::Listener {
|
|
|
|
protected:
|
|
|
|
Transport* mTransport;
|
|
|
|
MessageLoop* mIOLoop; // thread where IO happens
|
|
|
|
Transport::Listener* mExistingListener; // channel's previous listener
|
|
|
|
|
|
|
|
void OnCloseChannel();
|
|
|
|
void OnChannelOpened();
|
2012-07-14 14:21:32 -07:00
|
|
|
void OnTakeConnectedChannel();
|
2011-11-30 08:24:46 -08:00
|
|
|
void OnEchoMessage(Message* msg);
|
|
|
|
|
|
|
|
void AssertIOThread() const
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mIOLoop == MessageLoop::current(),
|
|
|
|
"not on I/O thread!");
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ProcessLink(AsyncChannel *chan);
|
|
|
|
virtual ~ProcessLink();
|
|
|
|
void Open(Transport* aTransport, MessageLoop *aIOLoop, Side aSide);
|
|
|
|
|
|
|
|
// Run on the I/O thread, only when using inter-process link.
|
|
|
|
// These methods acquire the monitor and forward to the
|
|
|
|
// similarly named methods in AsyncChannel below
|
|
|
|
// (OnMessageReceivedFromLink(), etc)
|
2012-07-06 11:15:45 -07:00
|
|
|
virtual void OnMessageReceived(const Message& msg) MOZ_OVERRIDE;
|
2012-09-17 01:37:20 -07:00
|
|
|
virtual void OnChannelConnected(int32_t peer_pid) MOZ_OVERRIDE;
|
2012-07-06 11:15:45 -07:00
|
|
|
virtual void OnChannelError() MOZ_OVERRIDE;
|
2011-11-30 08:24:46 -08:00
|
|
|
|
2012-07-06 11:15:45 -07:00
|
|
|
virtual void EchoMessage(Message *msg) MOZ_OVERRIDE;
|
|
|
|
virtual void SendMessage(Message *msg) MOZ_OVERRIDE;
|
|
|
|
virtual void SendClose() MOZ_OVERRIDE;
|
2011-11-30 08:26:16 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ThreadLink : public Link {
|
|
|
|
protected:
|
|
|
|
AsyncChannel* mTargetChan;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ThreadLink(AsyncChannel *aChan, AsyncChannel *aTargetChan);
|
|
|
|
virtual ~ThreadLink();
|
|
|
|
|
2012-07-06 11:15:45 -07:00
|
|
|
virtual void EchoMessage(Message *msg) MOZ_OVERRIDE;
|
|
|
|
virtual void SendMessage(Message *msg) MOZ_OVERRIDE;
|
|
|
|
virtual void SendClose() MOZ_OVERRIDE;
|
2011-11-30 08:24:46 -08:00
|
|
|
};
|
2009-07-13 14:55:04 -07:00
|
|
|
|
|
|
|
protected:
|
2011-11-30 08:24:46 -08:00
|
|
|
// The "link" thread is either the I/O thread (ProcessLink) or the
|
|
|
|
// other actor's work thread (ThreadLink). In either case, it is
|
|
|
|
// NOT our worker thread.
|
|
|
|
void AssertLinkThread() const
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mWorkerLoop != MessageLoop::current(),
|
|
|
|
"on worker thread but should not be!");
|
|
|
|
}
|
|
|
|
|
2009-10-08 12:11:13 -07:00
|
|
|
// Can be run on either thread
|
2010-04-26 22:42:59 -07:00
|
|
|
void AssertWorkerThread() const
|
2009-10-08 12:11:13 -07:00
|
|
|
{
|
2009-10-08 23:21:39 -07:00
|
|
|
NS_ABORT_IF_FALSE(mWorkerLoop == MessageLoop::current(),
|
|
|
|
"not on worker thread!");
|
2009-10-08 12:11:13 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 22:42:59 -07:00
|
|
|
bool Connected() const {
|
2011-11-30 08:24:46 -08:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2012-07-14 14:21:32 -07:00
|
|
|
// The transport layer allows us to send messages before
|
|
|
|
// receiving the "connected" ack from the remote side.
|
|
|
|
return (ChannelOpening == mChannelState ||
|
|
|
|
ChannelConnected == mChannelState);
|
2009-09-21 19:02:15 -07:00
|
|
|
}
|
|
|
|
|
2011-11-30 08:24:46 -08:00
|
|
|
// Return true if |msg| is a special message targeted at the IO
|
|
|
|
// thread, in which case it shouldn't be delivered to the worker.
|
|
|
|
virtual bool MaybeInterceptSpecialIOMessage(const Message& msg);
|
|
|
|
void ProcessGoodbyeMessage();
|
|
|
|
|
|
|
|
// Runs on the link thread. Invoked either from the I/O thread methods above
|
|
|
|
// or directly from the other actor if using a thread-based link.
|
|
|
|
//
|
|
|
|
// n.b.: mMonitor is always held when these methods are invoked.
|
|
|
|
// In the case of a ProcessLink, it is acquired by the ProcessLink.
|
|
|
|
// In the case of a ThreadLink, it is acquired by the other actor,
|
|
|
|
// which then invokes these methods directly.
|
|
|
|
virtual void OnMessageReceivedFromLink(const Message& msg);
|
|
|
|
virtual void OnChannelErrorFromLink();
|
|
|
|
void PostErrorNotifyTask();
|
|
|
|
|
2009-10-08 23:21:39 -07:00
|
|
|
// Run on the worker thread
|
2009-07-13 14:55:04 -07:00
|
|
|
void OnDispatchMessage(const Message& aMsg);
|
2012-09-17 01:37:20 -07:00
|
|
|
virtual bool OnSpecialMessage(uint16_t id, const Message& msg);
|
2010-04-26 22:42:59 -07:00
|
|
|
void SendSpecialMessage(Message* msg) const;
|
2010-01-26 22:41:31 -08:00
|
|
|
|
2010-02-09 16:02:54 -08:00
|
|
|
// Tell the IO thread to close the channel and wait for it to ACK.
|
|
|
|
void SynchronouslyClose();
|
|
|
|
|
2009-10-27 14:32:55 -07:00
|
|
|
bool MaybeHandleError(Result code, const char* channelName);
|
2010-04-26 22:42:59 -07:00
|
|
|
void ReportConnectionError(const char* channelName) const;
|
2009-10-27 14:32:55 -07:00
|
|
|
|
2009-12-03 00:16:28 -08:00
|
|
|
// Run on the worker thread
|
|
|
|
|
2010-03-11 21:21:58 -08:00
|
|
|
void OnNotifyMaybeChannelError();
|
2010-04-26 22:42:59 -07:00
|
|
|
virtual bool ShouldDeferNotifyMaybeError() const {
|
2010-03-11 21:21:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
2009-12-03 00:16:28 -08:00
|
|
|
void NotifyChannelClosed();
|
|
|
|
void NotifyMaybeChannelError();
|
2011-11-30 08:26:16 -08:00
|
|
|
void OnOpenAsSlave(AsyncChannel *aTargetChan, Side aSide);
|
|
|
|
void CommonThreadOpenInit(AsyncChannel *aTargetChan, Side aSide);
|
2009-12-03 00:16:28 -08:00
|
|
|
|
2010-02-14 23:47:00 -08:00
|
|
|
virtual void Clear();
|
2009-12-03 00:16:28 -08:00
|
|
|
|
2009-07-20 09:37:18 -07:00
|
|
|
AsyncListener* mListener;
|
2009-07-13 14:55:04 -07:00
|
|
|
ChannelState mChannelState;
|
2011-11-30 08:24:46 -08:00
|
|
|
nsRefPtr<RefCountedMonitor> mMonitor;
|
2009-07-13 14:55:04 -07:00
|
|
|
MessageLoop* mWorkerLoop; // thread where work is done
|
2009-10-08 14:44:43 -07:00
|
|
|
bool mChild; // am I the child or parent?
|
2009-12-17 16:12:03 -08:00
|
|
|
CancelableTask* mChannelErrorTask; // NotifyMaybeChannelError runnable
|
2011-11-30 08:24:46 -08:00
|
|
|
Link *mLink; // link to other thread/process
|
2009-07-13 14:55:04 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|
|
|
|
#endif // ifndef ipc_glue_AsyncChannel_h
|