2009-07-13 21:55:04 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
2012-05-21 11:12:37 +00: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 21:55:04 +00:00
|
|
|
|
|
|
|
#include "mozilla/ipc/AsyncChannel.h"
|
2010-02-03 22:17:09 +00:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2009-12-03 08:16:28 +00:00
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
2009-07-13 21:55:04 +00:00
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-11-12 22:46:29 +00:00
|
|
|
#include "nsTraceRefcnt.h"
|
2009-08-25 23:07:22 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2012-07-14 21:21:32 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace std;
|
2009-09-01 16:27:09 +00:00
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::AsyncChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::AsyncChannel::ProcessLink>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::AsyncChannel::ProcessLink* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::AsyncChannel::ProcessLink* obj) { }
|
|
|
|
};
|
|
|
|
|
2010-04-22 23:53:30 +00:00
|
|
|
// We rely on invariants about the lifetime of the transport:
|
|
|
|
//
|
|
|
|
// - outlives this AsyncChannel
|
|
|
|
// - deleted on the IO thread
|
|
|
|
//
|
|
|
|
// These invariants allow us to send messages directly through the
|
|
|
|
// transport without having to worry about orphaned Send() tasks on
|
|
|
|
// the IO thread touching AsyncChannel memory after it's been deleted
|
|
|
|
// on the worker thread. We also don't need to refcount the
|
|
|
|
// Transport, because whatever task triggers its deletion only runs on
|
|
|
|
// the IO thread, and only runs after this AsyncChannel is done with
|
|
|
|
// the Transport.
|
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::AsyncChannel::Transport>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::AsyncChannel::Transport* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::AsyncChannel::Transport* obj) { }
|
|
|
|
};
|
|
|
|
|
2010-01-27 06:41:31 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This is an async message
|
|
|
|
class GoodbyeMessage : public IPC::Message
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum { ID = GOODBYE_MESSAGE_TYPE };
|
|
|
|
GoodbyeMessage() :
|
|
|
|
IPC::Message(MSG_ROUTING_NONE, ID, PRIORITY_NORMAL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
// XXX not much point in implementing this; maybe could help with
|
|
|
|
// debugging?
|
|
|
|
static bool Read(const Message* msg)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void Log(const std::string& aPrefix,
|
|
|
|
FILE* aOutf) const
|
|
|
|
{
|
|
|
|
fputs("(special `Goodbye' message)", aOutf);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace <anon>
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::Link::Link(AsyncChannel *aChan)
|
|
|
|
: mChan(aChan)
|
2009-11-12 22:16:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::Link::~Link()
|
2009-11-12 22:16:54 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan = 0;
|
2009-11-12 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::ProcessLink::ProcessLink(AsyncChannel *aChan)
|
|
|
|
: Link(aChan)
|
|
|
|
, mExistingListener(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncChannel::ProcessLink::~ProcessLink()
|
|
|
|
{
|
|
|
|
mIOLoop = 0;
|
|
|
|
if (mTransport) {
|
|
|
|
mTransport->set_listener(0);
|
|
|
|
|
|
|
|
// we only hold a weak ref to the transport, which is "owned"
|
|
|
|
// by GeckoChildProcess/GeckoThread
|
|
|
|
mTransport = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::Open(mozilla::ipc::Transport* aTransport,
|
|
|
|
MessageLoop *aIOLoop,
|
|
|
|
Side aSide)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aTransport, "need transport layer");
|
|
|
|
|
|
|
|
// FIXME need to check for valid channel
|
|
|
|
|
|
|
|
mTransport = aTransport;
|
|
|
|
|
2009-07-14 05:12:50 +00:00
|
|
|
// FIXME figure out whether we're in parent or child, grab IO loop
|
|
|
|
// appropriately
|
2009-07-13 21:55:04 +00:00
|
|
|
bool needOpen = true;
|
2011-06-03 18:33:55 +00:00
|
|
|
if(aIOLoop) {
|
|
|
|
// We're a child or using the new arguments. Either way, we
|
|
|
|
// need an open.
|
|
|
|
needOpen = true;
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->mChild = (aSide == AsyncChannel::Unknown) || (aSide == AsyncChannel::Child);
|
2011-06-03 18:33:55 +00:00
|
|
|
} else {
|
|
|
|
NS_PRECONDITION(aSide == Unknown, "expected default side arg");
|
|
|
|
|
2009-07-14 05:12:50 +00:00
|
|
|
// parent
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->mChild = false;
|
2009-07-13 21:55:04 +00:00
|
|
|
needOpen = false;
|
2010-04-20 18:43:51 +00:00
|
|
|
aIOLoop = XRE_GetIOMessageLoop();
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mIOLoop = aIOLoop;
|
|
|
|
|
|
|
|
NS_ASSERTION(mIOLoop, "need an IO loop");
|
2011-11-30 16:24:46 +00:00
|
|
|
NS_ASSERTION(mChan->mWorkerLoop, "need a worker loop");
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2012-07-14 21:21:32 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
2009-09-01 16:27:09 +00:00
|
|
|
|
2012-07-14 21:21:32 +00:00
|
|
|
if (needOpen) {
|
|
|
|
// Transport::Connect() has not been called. Call it so
|
|
|
|
// we start polling our pipe and processing outgoing
|
|
|
|
// messages.
|
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &ProcessLink::OnChannelOpened));
|
|
|
|
} else {
|
|
|
|
// Transport::Connect() has already been called. Take
|
|
|
|
// over the channel from the previous listener and process
|
|
|
|
// any queued messages.
|
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &ProcessLink::OnTakeConnectedChannel));
|
|
|
|
}
|
2009-09-01 16:27:09 +00:00
|
|
|
|
|
|
|
// FIXME/cjones: handle errors
|
2012-07-14 21:21:32 +00:00
|
|
|
while (!mChan->Connected()) {
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->mMonitor->Wait();
|
2009-09-01 16:27:09 +00:00
|
|
|
}
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
2011-11-30 16:24:46 +00:00
|
|
|
}
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::EchoMessage(Message *msg)
|
|
|
|
{
|
2011-11-30 16:26:16 +00:00
|
|
|
mChan->AssertWorkerThread();
|
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
// NB: Go through this OnMessageReceived indirection so that
|
|
|
|
// echoing this message does the right thing for SyncChannel
|
|
|
|
// and RPCChannel too
|
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &ProcessLink::OnEchoMessage, msg));
|
|
|
|
// OnEchoMessage takes ownership of |msg|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::SendMessage(Message *msg)
|
|
|
|
{
|
|
|
|
mChan->AssertWorkerThread();
|
2011-11-30 16:26:16 +00:00
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(mTransport, &Transport::Send, msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::SendClose()
|
|
|
|
{
|
|
|
|
mChan->AssertWorkerThread();
|
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE, NewRunnableMethod(this, &ProcessLink::OnCloseChannel));
|
|
|
|
}
|
|
|
|
|
2011-11-30 16:26:16 +00:00
|
|
|
AsyncChannel::ThreadLink::ThreadLink(AsyncChannel *aChan,
|
|
|
|
AsyncChannel *aTargetChan)
|
|
|
|
: Link(aChan),
|
|
|
|
mTargetChan(aTargetChan)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncChannel::ThreadLink::~ThreadLink()
|
|
|
|
{
|
2013-03-15 19:26:59 +00:00
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
// Bug 848949: We need to prevent the other side
|
|
|
|
// from sending us any more messages to avoid Use-After-Free.
|
|
|
|
// The setup here is as shown:
|
|
|
|
//
|
|
|
|
// (Us) (Them)
|
|
|
|
// AsyncChannel AsyncChannel
|
|
|
|
// | ^ \ / ^ |
|
|
|
|
// | | X | |
|
|
|
|
// v | / \ | v
|
|
|
|
// ThreadLink ThreadLink
|
|
|
|
//
|
|
|
|
// We want to null out the diagonal link from their ThreadLink
|
|
|
|
// to our AsyncChannel. Note that we must hold the monitor so
|
|
|
|
// that we do this atomically with respect to them trying to send
|
|
|
|
// us a message.
|
|
|
|
if (mTargetChan) {
|
|
|
|
static_cast<ThreadLink*>(mTargetChan->mLink)->mTargetChan = 0;
|
|
|
|
}
|
2011-11-30 16:26:16 +00:00
|
|
|
mTargetChan = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ThreadLink::EchoMessage(Message *msg)
|
|
|
|
{
|
|
|
|
mChan->AssertWorkerThread();
|
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
mChan->OnMessageReceivedFromLink(*msg);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ThreadLink::SendMessage(Message *msg)
|
|
|
|
{
|
|
|
|
mChan->AssertWorkerThread();
|
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
2013-03-15 19:26:59 +00:00
|
|
|
if (mTargetChan)
|
|
|
|
mTargetChan->OnMessageReceivedFromLink(*msg);
|
2011-11-30 16:26:16 +00:00
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ThreadLink::SendClose()
|
|
|
|
{
|
|
|
|
mChan->AssertWorkerThread();
|
|
|
|
mChan->mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
mChan->mChannelState = ChannelClosed;
|
|
|
|
|
|
|
|
// In a ProcessLink, we would close our half the channel. This
|
|
|
|
// would show up on the other side as an error on the I/O thread.
|
|
|
|
// The I/O thread would then invoke OnChannelErrorFromLink().
|
|
|
|
// As usual, we skip that process and just invoke the
|
|
|
|
// OnChannelErrorFromLink() method directly.
|
2013-03-15 19:26:59 +00:00
|
|
|
if (mTargetChan)
|
|
|
|
mTargetChan->OnChannelErrorFromLink();
|
2011-11-30 16:26:16 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::AsyncChannel(AsyncListener* aListener)
|
2013-04-03 13:54:00 +00:00
|
|
|
: mListener(aListener->asWeakPtr()),
|
2011-11-30 16:24:46 +00:00
|
|
|
mChannelState(ChannelClosed),
|
|
|
|
mWorkerLoop(),
|
|
|
|
mChild(false),
|
|
|
|
mChannelErrorTask(NULL),
|
2013-04-03 13:54:00 +00:00
|
|
|
mLink(NULL),
|
|
|
|
mWorkerLoopID(-1)
|
2011-11-30 16:24:46 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(AsyncChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncChannel::~AsyncChannel()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(AsyncChannel);
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AsyncChannel::Open(Transport* aTransport,
|
|
|
|
MessageLoop* aIOLoop,
|
|
|
|
AsyncChannel::Side aSide)
|
|
|
|
{
|
|
|
|
ProcessLink *link;
|
|
|
|
NS_PRECONDITION(!mLink, "Open() called > once");
|
|
|
|
mMonitor = new RefCountedMonitor();
|
|
|
|
mWorkerLoop = MessageLoop::current();
|
2013-04-03 13:54:00 +00:00
|
|
|
mWorkerLoopID = mWorkerLoop->id();
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink = link = new ProcessLink(this);
|
|
|
|
link->Open(aTransport, aIOLoop, aSide); // n.b.: sets mChild
|
2009-07-13 21:55:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-30 16:26:16 +00:00
|
|
|
/* Opens a connection to another thread in the same process.
|
|
|
|
|
|
|
|
This handshake proceeds as follows:
|
|
|
|
- Let A be the thread initiating the process (either child or parent)
|
|
|
|
and B be the other thread.
|
|
|
|
- A spawns thread for B, obtaining B's message loop
|
|
|
|
- A creates ProtocolChild and ProtocolParent instances.
|
|
|
|
Let PA be the one appropriate to A and PB the side for B.
|
|
|
|
- A invokes PA->Open(PB, ...):
|
|
|
|
- set state to mChannelOpening
|
|
|
|
- this will place a work item in B's worker loop (see next bullet)
|
|
|
|
and then spins until PB->mChannelState becomes mChannelConnected
|
|
|
|
- meanwhile, on PB's worker loop, the work item is removed and:
|
|
|
|
- invokes PB->SlaveOpen(PA, ...):
|
|
|
|
- sets its state and that of PA to Connected
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
AsyncChannel::Open(AsyncChannel *aTargetChan,
|
|
|
|
MessageLoop *aTargetLoop,
|
|
|
|
AsyncChannel::Side aSide)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aTargetChan, "Need a target channel");
|
|
|
|
NS_PRECONDITION(ChannelClosed == mChannelState, "Not currently closed");
|
|
|
|
|
|
|
|
CommonThreadOpenInit(aTargetChan, aSide);
|
|
|
|
|
|
|
|
Side oppSide = Unknown;
|
|
|
|
switch(aSide) {
|
|
|
|
case Child: oppSide = Parent; break;
|
|
|
|
case Parent: oppSide = Child; break;
|
|
|
|
case Unknown: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMonitor = new RefCountedMonitor();
|
|
|
|
|
2012-04-21 21:35:43 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
|
|
|
mChannelState = ChannelOpening;
|
2011-11-30 16:26:16 +00:00
|
|
|
aTargetLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(aTargetChan, &AsyncChannel::OnOpenAsSlave,
|
|
|
|
this, oppSide));
|
|
|
|
|
|
|
|
while (ChannelOpening == mChannelState)
|
|
|
|
mMonitor->Wait();
|
|
|
|
NS_ASSERTION(ChannelConnected == mChannelState, "not connected when awoken");
|
|
|
|
return (ChannelConnected == mChannelState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::CommonThreadOpenInit(AsyncChannel *aTargetChan, Side aSide)
|
|
|
|
{
|
|
|
|
mWorkerLoop = MessageLoop::current();
|
2013-04-03 13:54:00 +00:00
|
|
|
mWorkerLoopID = mWorkerLoop->id();
|
2011-11-30 16:26:16 +00:00
|
|
|
mLink = new ThreadLink(this, aTargetChan);
|
|
|
|
mChild = (aSide == Child);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoked when the other side has begun the open.
|
|
|
|
void
|
|
|
|
AsyncChannel::OnOpenAsSlave(AsyncChannel *aTargetChan, Side aSide)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(ChannelClosed == mChannelState,
|
|
|
|
"Not currently closed");
|
|
|
|
NS_PRECONDITION(ChannelOpening == aTargetChan->mChannelState,
|
|
|
|
"Target channel not in the process of opening");
|
|
|
|
|
|
|
|
CommonThreadOpenInit(aTargetChan, aSide);
|
|
|
|
mMonitor = aTargetChan->mMonitor;
|
|
|
|
|
|
|
|
MonitorAutoLock lock(*mMonitor);
|
|
|
|
NS_ASSERTION(ChannelOpening == aTargetChan->mChannelState,
|
|
|
|
"Target channel not in the process of opening");
|
|
|
|
mChannelState = ChannelConnected;
|
|
|
|
aTargetChan->mChannelState = ChannelConnected;
|
|
|
|
aTargetChan->mMonitor->Notify();
|
|
|
|
}
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::Close()
|
|
|
|
{
|
2010-02-10 00:02:54 +00:00
|
|
|
AssertWorkerThread();
|
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
{
|
2013-04-03 13:54:00 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2009-12-03 08:16:28 +00:00
|
|
|
|
2010-02-10 00:02:54 +00:00
|
|
|
if (ChannelError == mChannelState ||
|
|
|
|
ChannelTimeout == mChannelState) {
|
2010-01-12 06:14:32 +00:00
|
|
|
// See bug 538586: if the listener gets deleted while the
|
|
|
|
// IO thread's NotifyChannelError event is still enqueued
|
|
|
|
// and subsequently deletes us, then the error event will
|
|
|
|
// also be deleted and the listener will never be notified
|
|
|
|
// of the channel error.
|
|
|
|
if (mListener) {
|
2013-04-03 13:54:00 +00:00
|
|
|
MonitorAutoUnlock unlock(*mMonitor);
|
2010-01-12 06:14:32 +00:00
|
|
|
NotifyMaybeChannelError();
|
|
|
|
}
|
2009-12-18 00:12:03 +00:00
|
|
|
return;
|
2010-01-12 06:14:32 +00:00
|
|
|
}
|
2009-12-18 00:12:03 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
if (ChannelConnected != mChannelState)
|
|
|
|
// XXX be strict about this until there's a compelling reason
|
|
|
|
// to relax
|
|
|
|
NS_RUNTIMEABORT("Close() called on closed channel!");
|
2009-11-09 22:56:55 +00:00
|
|
|
|
2009-11-11 08:34:08 +00:00
|
|
|
AssertWorkerThread();
|
2009-11-09 22:56:55 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
// notify the other side that we're about to close our socket
|
2010-01-27 06:41:31 +00:00
|
|
|
SendSpecialMessage(new GoodbyeMessage());
|
2009-12-03 08:16:28 +00:00
|
|
|
|
2010-02-10 00:02:54 +00:00
|
|
|
SynchronouslyClose();
|
|
|
|
}
|
2009-12-03 08:16:28 +00:00
|
|
|
|
2010-02-18 22:21:15 +00:00
|
|
|
NotifyChannelClosed();
|
2010-02-10 00:02:54 +00:00
|
|
|
}
|
2009-11-11 08:34:08 +00:00
|
|
|
|
2010-02-10 00:02:54 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::SynchronouslyClose()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
|
|
|
mLink->SendClose();
|
2010-02-10 00:02:54 +00:00
|
|
|
while (ChannelClosed != mChannelState)
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->Wait();
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-11-30 13:19:49 +00:00
|
|
|
AsyncChannel::Send(Message* _msg)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2011-11-30 13:19:49 +00:00
|
|
|
nsAutoPtr<Message> msg(_msg);
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2009-09-22 02:02:15 +00:00
|
|
|
NS_ABORT_IF_FALSE(MSG_ROUTING_NONE != msg->routing_id(), "need a route");
|
|
|
|
|
2009-10-09 06:21:39 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2009-10-09 06:21:39 +00:00
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("AsyncChannel");
|
2009-10-09 06:21:39 +00:00
|
|
|
return false;
|
2009-10-27 21:32:55 +00:00
|
|
|
}
|
2009-10-09 06:21:39 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink->SendMessage(msg.forget());
|
2009-10-09 06:21:39 +00:00
|
|
|
}
|
2009-08-19 15:44:56 +00:00
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
bool
|
2011-11-30 13:19:49 +00:00
|
|
|
AsyncChannel::Echo(Message* _msg)
|
2011-06-03 18:33:56 +00:00
|
|
|
{
|
2011-11-30 13:19:49 +00:00
|
|
|
nsAutoPtr<Message> msg(_msg);
|
2011-06-03 18:33:56 +00:00
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2011-06-03 18:33:56 +00:00
|
|
|
NS_ABORT_IF_FALSE(MSG_ROUTING_NONE != msg->routing_id(), "need a route");
|
|
|
|
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2011-06-03 18:33:56 +00:00
|
|
|
|
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("AsyncChannel");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink->EchoMessage(msg.forget());
|
2011-06-03 18:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::OnDispatchMessage(const Message& msg)
|
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertWorkerThread();
|
2009-07-13 21:55:04 +00:00
|
|
|
NS_ASSERTION(!msg.is_reply(), "can't process replies here");
|
|
|
|
NS_ASSERTION(!(msg.is_sync() || msg.is_rpc()), "async dispatch only");
|
|
|
|
|
2010-01-27 06:41:31 +00:00
|
|
|
if (MSG_ROUTING_NONE == msg.routing_id()) {
|
|
|
|
if (!OnSpecialMessage(msg.type(), msg))
|
|
|
|
// XXX real error handling
|
|
|
|
NS_RUNTIMEABORT("unhandled special message!");
|
2009-12-03 08:16:28 +00:00
|
|
|
return;
|
2010-01-27 06:41:31 +00:00
|
|
|
}
|
2009-12-03 08:16:28 +00:00
|
|
|
|
|
|
|
// it's OK to dispatch messages if the channel is closed/error'd,
|
|
|
|
// since we don't have a reply to send back
|
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
(void)MaybeHandleError(mListener->OnMessageReceived(msg), "AsyncChannel");
|
|
|
|
}
|
|
|
|
|
2010-01-27 06:41:31 +00:00
|
|
|
bool
|
2012-09-17 08:37:20 +00:00
|
|
|
AsyncChannel::OnSpecialMessage(uint16_t id, const Message& msg)
|
2009-12-03 08:16:28 +00:00
|
|
|
{
|
2010-03-11 07:35:26 +00:00
|
|
|
return false;
|
2010-01-27 06:41:31 +00:00
|
|
|
}
|
2009-12-03 08:16:28 +00:00
|
|
|
|
|
|
|
void
|
2010-04-27 05:42:59 +00:00
|
|
|
AsyncChannel::SendSpecialMessage(Message* msg) const
|
2010-04-22 23:53:30 +00:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mLink->SendMessage(msg);
|
2009-12-03 08:16:28 +00:00
|
|
|
}
|
|
|
|
|
2010-03-12 05:21:58 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::OnNotifyMaybeChannelError()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2010-03-12 05:21:58 +00:00
|
|
|
|
2012-07-05 18:48:40 +00:00
|
|
|
mChannelErrorTask = NULL;
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
// OnChannelError holds mMonitor when it posts this task and this
|
2010-03-12 05:21:58 +00:00
|
|
|
// task cannot be allowed to run until OnChannelError has
|
|
|
|
// exited. We enforce that order by grabbing the mutex here which
|
|
|
|
// should only continue once OnChannelError has completed.
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mMonitor);
|
2010-03-12 05:21:58 +00:00
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldDeferNotifyMaybeError()) {
|
|
|
|
mChannelErrorTask =
|
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnNotifyMaybeChannelError);
|
|
|
|
// 10 ms delay is completely arbitrary
|
|
|
|
mWorkerLoop->PostDelayedTask(FROM_HERE, mChannelErrorTask, 10);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotifyMaybeChannelError();
|
|
|
|
}
|
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::NotifyChannelClosed()
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2010-02-18 22:21:15 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
if (ChannelClosed != mChannelState)
|
|
|
|
NS_RUNTIMEABORT("channel should have been closed!");
|
|
|
|
|
|
|
|
// OK, the IO thread just closed the channel normally. Let the
|
|
|
|
// listener know about it.
|
|
|
|
mListener->OnChannelClose();
|
2010-02-18 22:21:15 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::NotifyMaybeChannelError()
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertNotCurrentThreadOwns();
|
2010-02-18 22:21:15 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
// TODO sort out Close() on this side racing with Close() on the
|
|
|
|
// other side
|
|
|
|
if (ChannelClosing == mChannelState) {
|
|
|
|
// the channel closed, but we received a "Goodbye" message
|
|
|
|
// warning us about it. no worries
|
|
|
|
mChannelState = ChannelClosed;
|
2010-02-18 22:21:15 +00:00
|
|
|
NotifyChannelClosed();
|
|
|
|
return;
|
2009-12-03 08:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Oops, error! Let the listener know about it.
|
|
|
|
mChannelState = ChannelError;
|
|
|
|
mListener->OnChannelError();
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::Clear()
|
|
|
|
{
|
2013-04-03 13:54:00 +00:00
|
|
|
// Don't clear mWorkerLoopID; we use it in AssertLinkThread() and
|
|
|
|
// AssertWorkerThread().
|
|
|
|
//
|
|
|
|
// Also don't clear mListener. If we clear it, then sending a message
|
|
|
|
// through this channel after it's Clear()'ed can cause this process to
|
|
|
|
// crash.
|
|
|
|
//
|
|
|
|
// In practice, mListener owns the channel, so the channel gets deleted
|
|
|
|
// before mListener. But just to be safe, mListener is a weak pointer.
|
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
mWorkerLoop = 0;
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
delete mLink;
|
|
|
|
mLink = 0;
|
2009-12-03 08:16:28 +00:00
|
|
|
|
2009-12-18 00:12:03 +00:00
|
|
|
if (mChannelErrorTask) {
|
|
|
|
mChannelErrorTask->Cancel();
|
|
|
|
mChannelErrorTask = NULL;
|
|
|
|
}
|
2009-12-03 08:16:28 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 17:45:09 +00:00
|
|
|
static void
|
|
|
|
PrintErrorMessage(bool isChild, const char* channelName, const char* msg)
|
|
|
|
{
|
2013-01-04 21:28:37 +00:00
|
|
|
printf_stderr("\n###!!! [%s][%s] Error: %s\n\n",
|
|
|
|
isChild ? "Child" : "Parent", channelName, msg);
|
2011-02-23 17:45:09 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
bool
|
|
|
|
AsyncChannel::MaybeHandleError(Result code, const char* channelName)
|
|
|
|
{
|
|
|
|
if (MsgProcessed == code)
|
|
|
|
return true;
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2012-10-13 16:16:22 +00:00
|
|
|
const char* errorMsg = nullptr;
|
2009-10-27 21:32:55 +00:00
|
|
|
switch (code) {
|
2009-07-15 21:38:55 +00:00
|
|
|
case MsgNotKnown:
|
2009-10-27 21:32:55 +00:00
|
|
|
errorMsg = "Unknown message: not processed";
|
|
|
|
break;
|
2009-07-15 21:38:55 +00:00
|
|
|
case MsgNotAllowed:
|
2009-10-27 21:32:55 +00:00
|
|
|
errorMsg = "Message not allowed: cannot be sent/recvd in this state";
|
|
|
|
break;
|
2009-07-15 21:38:55 +00:00
|
|
|
case MsgPayloadError:
|
2009-10-27 21:32:55 +00:00
|
|
|
errorMsg = "Payload error: message could not be deserialized";
|
|
|
|
break;
|
2010-05-22 19:35:29 +00:00
|
|
|
case MsgProcessingError:
|
|
|
|
errorMsg = "Processing error: message was deserialized, but the handler returned false (indicating failure)";
|
2010-07-02 14:33:19 +00:00
|
|
|
break;
|
2009-07-15 21:38:55 +00:00
|
|
|
case MsgRouteError:
|
2009-10-27 21:32:55 +00:00
|
|
|
errorMsg = "Route error: message sent to unknown actor ID";
|
|
|
|
break;
|
2009-07-15 21:38:55 +00:00
|
|
|
case MsgValueError:
|
2009-10-27 21:32:55 +00:00
|
|
|
errorMsg = "Value error: message was deserialized, but contained an illegal value";
|
|
|
|
break;
|
2009-07-13 21:55:04 +00:00
|
|
|
|
|
|
|
default:
|
2009-12-03 08:16:28 +00:00
|
|
|
NS_RUNTIMEABORT("unknown Result code");
|
2009-10-27 21:32:55 +00:00
|
|
|
return false;
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
2009-10-27 21:32:55 +00:00
|
|
|
|
2011-02-23 17:45:09 +00:00
|
|
|
PrintErrorMessage(mChild, channelName, errorMsg);
|
2010-08-20 23:24:40 +00:00
|
|
|
|
|
|
|
mListener->OnProcessingError(code);
|
|
|
|
|
2009-10-27 21:32:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-27 05:42:59 +00:00
|
|
|
AsyncChannel::ReportConnectionError(const char* channelName) const
|
2009-10-27 21:32:55 +00:00
|
|
|
{
|
2012-10-13 16:16:22 +00:00
|
|
|
const char* errorMsg = nullptr;
|
2009-10-27 21:32:55 +00:00
|
|
|
switch (mChannelState) {
|
|
|
|
case ChannelClosed:
|
|
|
|
errorMsg = "Closed channel: cannot send/recv";
|
|
|
|
break;
|
|
|
|
case ChannelOpening:
|
|
|
|
errorMsg = "Opening channel: not yet ready for send/recv";
|
|
|
|
break;
|
2010-02-10 00:02:54 +00:00
|
|
|
case ChannelTimeout:
|
|
|
|
errorMsg = "Channel timeout: cannot send/recv";
|
2010-07-02 14:36:15 +00:00
|
|
|
break;
|
2010-05-15 02:05:34 +00:00
|
|
|
case ChannelClosing:
|
|
|
|
errorMsg = "Channel closing: too late to send/recv, messages will be lost";
|
2010-07-02 14:36:15 +00:00
|
|
|
break;
|
2009-10-27 21:32:55 +00:00
|
|
|
case ChannelError:
|
|
|
|
errorMsg = "Channel error: cannot send/recv";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-05-15 02:05:34 +00:00
|
|
|
NS_RUNTIMEABORT("unreached");
|
2009-10-27 21:32:55 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 17:45:09 +00:00
|
|
|
PrintErrorMessage(mChild, channelName, errorMsg);
|
2010-08-20 23:24:40 +00:00
|
|
|
|
|
|
|
mListener->OnProcessingError(MsgDropped);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
void
|
2012-09-17 08:37:20 +00:00
|
|
|
AsyncChannel::DispatchOnChannelConnected(int32_t peer_pid)
|
2011-11-30 16:24:46 +00:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
if (mListener)
|
|
|
|
mListener->OnChannelConnected(peer_pid);
|
|
|
|
}
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
//
|
2009-12-03 08:16:28 +00:00
|
|
|
// The methods below run in the context of the IO thread
|
2009-07-13 21:55:04 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::ProcessLink::OnMessageReceived(const Message& msg)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertIOThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
NS_ASSERTION(mChan->mChannelState != ChannelError, "Shouldn't get here!");
|
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
mChan->OnMessageReceivedFromLink(msg);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 18:33:56 +00:00
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::ProcessLink::OnEchoMessage(Message* msg)
|
2011-06-03 18:33:56 +00:00
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
OnMessageReceived(*msg);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::ProcessLink::OnChannelOpened()
|
2009-12-03 08:16:28 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->AssertLinkThread();
|
2011-04-29 00:15:03 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
2012-07-14 21:21:32 +00:00
|
|
|
|
|
|
|
mExistingListener = mTransport->set_listener(this);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mExistingListener) {
|
|
|
|
queue<Message> pending;
|
|
|
|
mExistingListener->GetQueuedMessages(pending);
|
|
|
|
MOZ_ASSERT(pending.empty());
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->mChannelState = ChannelOpening;
|
2012-07-14 21:21:32 +00:00
|
|
|
lock.Notify();
|
2011-04-29 00:15:03 +00:00
|
|
|
}
|
2009-12-03 08:16:28 +00:00
|
|
|
/*assert*/mTransport->Connect();
|
|
|
|
}
|
|
|
|
|
2012-07-14 21:21:32 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::OnTakeConnectedChannel()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
queue<Message> pending;
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
|
|
|
|
mChan->mChannelState = ChannelConnected;
|
|
|
|
|
|
|
|
mExistingListener = mTransport->set_listener(this);
|
|
|
|
if (mExistingListener) {
|
|
|
|
mExistingListener->GetQueuedMessages(pending);
|
|
|
|
}
|
|
|
|
lock.Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch whatever messages the previous listener had queued up.
|
|
|
|
while (!pending.empty()) {
|
|
|
|
OnMessageReceived(pending.front());
|
|
|
|
pending.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-08 23:24:36 +00:00
|
|
|
void
|
2012-09-17 08:37:20 +00:00
|
|
|
AsyncChannel::ProcessLink::OnChannelConnected(int32_t peer_pid)
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertIOThread();
|
2009-09-01 16:27:09 +00:00
|
|
|
|
2010-04-12 00:13:00 +00:00
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
mChan->mChannelState = ChannelConnected;
|
|
|
|
mChan->mMonitor->Notify();
|
2010-04-12 00:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mExistingListener)
|
|
|
|
mExistingListener->OnChannelConnected(peer_pid);
|
2010-10-08 23:24:36 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
mChan->mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(mChan,
|
|
|
|
&AsyncChannel::DispatchOnChannelConnected,
|
|
|
|
peer_pid));
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-11-30 16:24:46 +00:00
|
|
|
AsyncChannel::ProcessLink::OnChannelError()
|
2009-07-13 21:55:04 +00:00
|
|
|
{
|
2009-10-08 19:11:13 +00:00
|
|
|
AssertIOThread();
|
2011-11-30 16:24:46 +00:00
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
mChan->OnChannelErrorFromLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessLink::OnCloseChannel()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
mTransport->Close();
|
|
|
|
|
|
|
|
MonitorAutoLock lock(*mChan->mMonitor);
|
|
|
|
mChan->mChannelState = ChannelClosed;
|
|
|
|
mChan->mMonitor->Notify();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// The methods below run in the context of the link thread
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnMessageReceivedFromLink(const Message& msg)
|
|
|
|
{
|
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
|
|
|
|
|
|
|
if (!MaybeInterceptSpecialIOMessage(msg))
|
|
|
|
// wake up the worker, there's work to do
|
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnDispatchMessage, msg));
|
|
|
|
}
|
2009-10-09 06:21:39 +00:00
|
|
|
|
2011-11-30 16:24:46 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelErrorFromLink()
|
|
|
|
{
|
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2009-12-03 08:16:28 +00:00
|
|
|
if (ChannelClosing != mChannelState)
|
|
|
|
mChannelState = ChannelError;
|
2009-07-13 21:55:04 +00:00
|
|
|
|
2010-03-18 22:52:28 +00:00
|
|
|
PostErrorNotifyTask();
|
|
|
|
}
|
|
|
|
|
2012-07-05 18:48:40 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::CloseWithError()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
MonitorAutoLock lock(*mMonitor);
|
|
|
|
if (ChannelConnected != mChannelState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SynchronouslyClose();
|
|
|
|
mChannelState = ChannelError;
|
|
|
|
PostErrorNotifyTask();
|
|
|
|
}
|
|
|
|
|
2010-03-18 22:52:28 +00:00
|
|
|
void
|
|
|
|
AsyncChannel::PostErrorNotifyTask()
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-03-18 22:52:28 +00:00
|
|
|
|
2012-07-05 18:48:40 +00:00
|
|
|
if (mChannelErrorTask)
|
|
|
|
return;
|
2009-12-18 00:12:03 +00:00
|
|
|
|
2010-02-19 20:39:38 +00:00
|
|
|
// This must be the last code that runs on this thread!
|
2009-12-18 00:12:03 +00:00
|
|
|
mChannelErrorTask =
|
2010-03-12 05:21:58 +00:00
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnNotifyMaybeChannelError);
|
2009-12-18 00:12:03 +00:00
|
|
|
mWorkerLoop->PostTask(FROM_HERE, mChannelErrorTask);
|
2009-07-13 21:55:04 +00:00
|
|
|
}
|
|
|
|
|
2010-03-11 07:35:26 +00:00
|
|
|
bool
|
|
|
|
AsyncChannel::MaybeInterceptSpecialIOMessage(const Message& msg)
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-03-11 07:35:26 +00:00
|
|
|
|
|
|
|
if (MSG_ROUTING_NONE == msg.routing_id()
|
|
|
|
&& GOODBYE_MESSAGE_TYPE == msg.type()) {
|
|
|
|
ProcessGoodbyeMessage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessGoodbyeMessage()
|
|
|
|
{
|
2011-11-30 16:24:46 +00:00
|
|
|
AssertLinkThread();
|
|
|
|
mMonitor->AssertCurrentThreadOwns();
|
2010-03-11 07:35:26 +00:00
|
|
|
|
|
|
|
// TODO sort out Close() on this side racing with Close() on the
|
|
|
|
// other side
|
|
|
|
mChannelState = ChannelClosing;
|
|
|
|
|
|
|
|
printf("NOTE: %s process received `Goodbye', closing down\n",
|
|
|
|
mChild ? "child" : "parent");
|
|
|
|
}
|
|
|
|
|
2009-07-13 21:55:04 +00:00
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|