2009-07-14 01:55:04 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Plugin App.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Chris Jones <jones.chris.g@gmail.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2009
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "mozilla/ipc/AsyncChannel.h"
|
2010-02-04 01:17:09 +03:00
|
|
|
#include "mozilla/ipc/BrowserProcessSubThread.h"
|
2009-12-03 11:16:28 +03:00
|
|
|
#include "mozilla/ipc/ProtocolUtils.h"
|
2009-07-14 01:55:04 +04:00
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-11-13 01:46:29 +03:00
|
|
|
#include "nsTraceRefcnt.h"
|
2009-08-26 03:07:22 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
using mozilla::MonitorAutoLock;
|
2009-09-01 20:27:09 +04:00
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::AsyncChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
2010-04-23 03:53:30 +04: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 09:41:31 +03: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-14 01:55:04 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
2009-11-13 01:16:54 +03:00
|
|
|
AsyncChannel::AsyncChannel(AsyncListener* aListener)
|
|
|
|
: mTransport(0),
|
|
|
|
mListener(aListener),
|
|
|
|
mChannelState(ChannelClosed),
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor("mozilla.ipc.AsyncChannel.mMonitor"),
|
2009-11-13 01:16:54 +03:00
|
|
|
mIOLoop(),
|
2009-12-18 03:12:03 +03:00
|
|
|
mWorkerLoop(),
|
2010-01-27 11:17:17 +03:00
|
|
|
mChild(false),
|
2010-04-12 04:13:00 +04:00
|
|
|
mChannelErrorTask(NULL),
|
|
|
|
mExistingListener(NULL)
|
2009-11-13 01:16:54 +03:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(AsyncChannel);
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncChannel::~AsyncChannel()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(AsyncChannel);
|
2009-12-03 11:16:28 +03:00
|
|
|
Clear();
|
2009-11-13 01:16:54 +03:00
|
|
|
}
|
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
bool
|
2011-06-03 22:33:55 +04:00
|
|
|
AsyncChannel::Open(Transport* aTransport, MessageLoop* aIOLoop, Side aSide)
|
2009-07-14 01:55:04 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!mTransport, "Open() called > once");
|
|
|
|
NS_PRECONDITION(aTransport, "need transport layer");
|
|
|
|
|
|
|
|
// FIXME need to check for valid channel
|
|
|
|
|
|
|
|
mTransport = aTransport;
|
2010-04-12 04:13:00 +04:00
|
|
|
mExistingListener = mTransport->set_listener(this);
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2009-07-14 09:12:50 +04:00
|
|
|
// FIXME figure out whether we're in parent or child, grab IO loop
|
|
|
|
// appropriately
|
2009-07-14 01:55:04 +04:00
|
|
|
bool needOpen = true;
|
2011-06-03 22:33:55 +04:00
|
|
|
if(aIOLoop) {
|
|
|
|
// We're a child or using the new arguments. Either way, we
|
|
|
|
// need an open.
|
|
|
|
needOpen = true;
|
|
|
|
mChild = (aSide == Unknown) || (aSide == Child);
|
|
|
|
} else {
|
|
|
|
NS_PRECONDITION(aSide == Unknown, "expected default side arg");
|
|
|
|
|
2009-07-14 09:12:50 +04:00
|
|
|
// parent
|
2011-06-03 22:33:55 +04:00
|
|
|
mChild = false;
|
2009-07-14 01:55:04 +04:00
|
|
|
needOpen = false;
|
2010-04-20 22:43:51 +04:00
|
|
|
aIOLoop = XRE_GetIOMessageLoop();
|
2009-07-14 09:12:50 +04:00
|
|
|
// FIXME assuming that the parent waits for the OnConnected event.
|
|
|
|
// FIXME see GeckoChildProcessHost.cpp. bad assumption!
|
2009-08-19 19:44:56 +04:00
|
|
|
mChannelState = ChannelConnected;
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mIOLoop = aIOLoop;
|
|
|
|
mWorkerLoop = MessageLoop::current();
|
|
|
|
|
|
|
|
NS_ASSERTION(mIOLoop, "need an IO loop");
|
|
|
|
NS_ASSERTION(mWorkerLoop, "need a worker loop");
|
|
|
|
|
2009-09-01 20:27:09 +04:00
|
|
|
if (needOpen) { // child process
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2009-09-01 20:27:09 +04:00
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
mIOLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
|
|
|
&AsyncChannel::OnChannelOpened));
|
2009-09-01 20:27:09 +04:00
|
|
|
|
|
|
|
// FIXME/cjones: handle errors
|
|
|
|
while (mChannelState != ChannelConnected) {
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.Wait();
|
2009-09-01 20:27:09 +04:00
|
|
|
}
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::Close()
|
|
|
|
{
|
2010-02-10 03:02:54 +03:00
|
|
|
AssertWorkerThread();
|
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2010-02-10 03:02:54 +03:00
|
|
|
if (ChannelError == mChannelState ||
|
|
|
|
ChannelTimeout == mChannelState) {
|
2010-01-12 09:14:32 +03: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) {
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoUnlock unlock(mMonitor);
|
2010-01-12 09:14:32 +03:00
|
|
|
NotifyMaybeChannelError();
|
|
|
|
}
|
2009-12-18 03:12:03 +03:00
|
|
|
return;
|
2010-01-12 09:14:32 +03:00
|
|
|
}
|
2009-12-18 03:12:03 +03:00
|
|
|
|
2009-12-03 11:16:28 +03: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-10 01:56:55 +03:00
|
|
|
|
2009-11-11 11:34:08 +03:00
|
|
|
AssertWorkerThread();
|
2009-11-10 01:56:55 +03:00
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
// notify the other side that we're about to close our socket
|
2010-01-27 09:41:31 +03:00
|
|
|
SendSpecialMessage(new GoodbyeMessage());
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2010-02-10 03:02:54 +03:00
|
|
|
SynchronouslyClose();
|
|
|
|
}
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2010-02-19 01:21:15 +03:00
|
|
|
NotifyChannelClosed();
|
2010-02-10 03:02:54 +03:00
|
|
|
}
|
2009-11-11 11:34:08 +03:00
|
|
|
|
2010-02-10 03:02:54 +03:00
|
|
|
void
|
|
|
|
AsyncChannel::SynchronouslyClose()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2009-12-03 11:16:28 +03:00
|
|
|
|
2010-02-10 03:02:54 +03:00
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE, NewRunnableMethod(this, &AsyncChannel::OnCloseChannel));
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2010-02-10 03:02:54 +03:00
|
|
|
while (ChannelClosed != mChannelState)
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.Wait();
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AsyncChannel::Send(Message* msg)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertWorkerThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertNotCurrentThreadOwns();
|
2009-09-22 06:02:15 +04:00
|
|
|
NS_ABORT_IF_FALSE(MSG_ROUTING_NONE != msg->routing_id(), "need a route");
|
|
|
|
|
2009-10-09 10:21:39 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2009-10-09 10:21:39 +04:00
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("AsyncChannel");
|
2009-10-09 10:21:39 +04:00
|
|
|
return false;
|
2009-10-28 00:32:55 +03:00
|
|
|
}
|
2009-10-09 10:21:39 +04:00
|
|
|
|
2010-04-23 03:53:30 +04:00
|
|
|
SendThroughTransport(msg);
|
2009-10-09 10:21:39 +04:00
|
|
|
}
|
2009-08-19 19:44:56 +04:00
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
bool
|
|
|
|
AsyncChannel::Echo(Message* msg)
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
mMonitor.AssertNotCurrentThreadOwns();
|
|
|
|
NS_ABORT_IF_FALSE(MSG_ROUTING_NONE != msg->routing_id(), "need a route");
|
|
|
|
|
|
|
|
{
|
|
|
|
MonitorAutoLock lock(mMonitor);
|
|
|
|
|
|
|
|
if (!Connected()) {
|
|
|
|
ReportConnectionError("AsyncChannel");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, &AsyncChannel::OnEchoMessage, msg));
|
|
|
|
// OnEchoMessage takes ownership of |msg|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
void
|
|
|
|
AsyncChannel::OnDispatchMessage(const Message& msg)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertWorkerThread();
|
2009-07-14 01:55:04 +04: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 09:41:31 +03: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 11:16:28 +03:00
|
|
|
return;
|
2010-01-27 09:41:31 +03:00
|
|
|
}
|
2009-12-03 11:16:28 +03: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-28 00:32:55 +03:00
|
|
|
(void)MaybeHandleError(mListener->OnMessageReceived(msg), "AsyncChannel");
|
|
|
|
}
|
|
|
|
|
2010-01-27 09:41:31 +03:00
|
|
|
bool
|
|
|
|
AsyncChannel::OnSpecialMessage(uint16 id, const Message& msg)
|
2009-12-03 11:16:28 +03:00
|
|
|
{
|
2010-03-11 10:35:26 +03:00
|
|
|
return false;
|
2010-01-27 09:41:31 +03:00
|
|
|
}
|
2009-12-03 11:16:28 +03:00
|
|
|
|
|
|
|
void
|
2010-04-27 09:42:59 +04:00
|
|
|
AsyncChannel::SendSpecialMessage(Message* msg) const
|
2010-04-23 03:53:30 +04:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
SendThroughTransport(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-27 09:42:59 +04:00
|
|
|
AsyncChannel::SendThroughTransport(Message* msg) const
|
2009-12-03 11:16:28 +03:00
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
|
|
|
|
mIOLoop->PostTask(
|
|
|
|
FROM_HERE,
|
2010-04-23 03:53:30 +04:00
|
|
|
NewRunnableMethod(mTransport, &Transport::Send, msg));
|
2009-12-03 11:16:28 +03:00
|
|
|
}
|
|
|
|
|
2010-03-12 08:21:58 +03:00
|
|
|
void
|
|
|
|
AsyncChannel::OnNotifyMaybeChannelError()
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertNotCurrentThreadOwns();
|
2010-03-12 08:21:58 +03:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
// OnChannelError holds mMonitor when it posts this task and this
|
2010-03-12 08:21:58 +03: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-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-03-12 08:21:58 +03: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 11:16:28 +03:00
|
|
|
void
|
|
|
|
AsyncChannel::NotifyChannelClosed()
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertNotCurrentThreadOwns();
|
2010-02-19 01:21:15 +03:00
|
|
|
|
2009-12-03 11:16:28 +03: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-19 01:21:15 +03:00
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::NotifyMaybeChannelError()
|
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertNotCurrentThreadOwns();
|
2010-02-19 01:21:15 +03:00
|
|
|
|
2009-12-03 11:16:28 +03: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-19 01:21:15 +03:00
|
|
|
NotifyChannelClosed();
|
|
|
|
return;
|
2009-12-03 11:16:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Oops, error! Let the listener know about it.
|
|
|
|
mChannelState = ChannelError;
|
|
|
|
mListener->OnChannelError();
|
|
|
|
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::Clear()
|
|
|
|
{
|
|
|
|
mListener = 0;
|
|
|
|
mIOLoop = 0;
|
|
|
|
mWorkerLoop = 0;
|
|
|
|
|
|
|
|
if (mTransport) {
|
|
|
|
mTransport->set_listener(0);
|
|
|
|
|
|
|
|
// we only hold a weak ref to the transport, which is "owned"
|
|
|
|
// by GeckoChildProcess/GeckoThread
|
|
|
|
mTransport = 0;
|
|
|
|
}
|
2009-12-18 03:12:03 +03:00
|
|
|
if (mChannelErrorTask) {
|
|
|
|
mChannelErrorTask->Cancel();
|
|
|
|
mChannelErrorTask = NULL;
|
|
|
|
}
|
2009-12-03 11:16:28 +03:00
|
|
|
}
|
|
|
|
|
2011-02-23 20:45:09 +03:00
|
|
|
static void
|
|
|
|
PrintErrorMessage(bool isChild, const char* channelName, const char* msg)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\n###!!! [%s][%s] Error: %s\n\n",
|
|
|
|
isChild ? "Child" : "Parent", channelName, msg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
bool
|
|
|
|
AsyncChannel::MaybeHandleError(Result code, const char* channelName)
|
|
|
|
{
|
|
|
|
if (MsgProcessed == code)
|
|
|
|
return true;
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
const char* errorMsg;
|
|
|
|
switch (code) {
|
2009-07-16 01:38:55 +04:00
|
|
|
case MsgNotKnown:
|
2009-10-28 00:32:55 +03:00
|
|
|
errorMsg = "Unknown message: not processed";
|
|
|
|
break;
|
2009-07-16 01:38:55 +04:00
|
|
|
case MsgNotAllowed:
|
2009-10-28 00:32:55 +03:00
|
|
|
errorMsg = "Message not allowed: cannot be sent/recvd in this state";
|
|
|
|
break;
|
2009-07-16 01:38:55 +04:00
|
|
|
case MsgPayloadError:
|
2009-10-28 00:32:55 +03:00
|
|
|
errorMsg = "Payload error: message could not be deserialized";
|
|
|
|
break;
|
2010-05-22 23:35:29 +04:00
|
|
|
case MsgProcessingError:
|
|
|
|
errorMsg = "Processing error: message was deserialized, but the handler returned false (indicating failure)";
|
2010-07-02 18:33:19 +04:00
|
|
|
break;
|
2009-07-16 01:38:55 +04:00
|
|
|
case MsgRouteError:
|
2009-10-28 00:32:55 +03:00
|
|
|
errorMsg = "Route error: message sent to unknown actor ID";
|
|
|
|
break;
|
2009-07-16 01:38:55 +04:00
|
|
|
case MsgValueError:
|
2009-10-28 00:32:55 +03:00
|
|
|
errorMsg = "Value error: message was deserialized, but contained an illegal value";
|
|
|
|
break;
|
2009-07-14 01:55:04 +04:00
|
|
|
|
|
|
|
default:
|
2009-12-03 11:16:28 +03:00
|
|
|
NS_RUNTIMEABORT("unknown Result code");
|
2009-10-28 00:32:55 +03:00
|
|
|
return false;
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
2009-10-28 00:32:55 +03:00
|
|
|
|
2011-02-23 20:45:09 +03:00
|
|
|
PrintErrorMessage(mChild, channelName, errorMsg);
|
2010-08-21 03:24:40 +04:00
|
|
|
|
|
|
|
mListener->OnProcessingError(code);
|
|
|
|
|
2009-10-28 00:32:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-27 09:42:59 +04:00
|
|
|
AsyncChannel::ReportConnectionError(const char* channelName) const
|
2009-10-28 00:32:55 +03:00
|
|
|
{
|
|
|
|
const char* errorMsg;
|
|
|
|
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 03:02:54 +03:00
|
|
|
case ChannelTimeout:
|
|
|
|
errorMsg = "Channel timeout: cannot send/recv";
|
2010-07-02 18:36:15 +04:00
|
|
|
break;
|
2010-05-15 06:05:34 +04:00
|
|
|
case ChannelClosing:
|
|
|
|
errorMsg = "Channel closing: too late to send/recv, messages will be lost";
|
2010-07-02 18:36:15 +04:00
|
|
|
break;
|
2009-10-28 00:32:55 +03:00
|
|
|
case ChannelError:
|
|
|
|
errorMsg = "Channel error: cannot send/recv";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-05-15 06:05:34 +04:00
|
|
|
NS_RUNTIMEABORT("unreached");
|
2009-10-28 00:32:55 +03:00
|
|
|
}
|
|
|
|
|
2011-02-23 20:45:09 +03:00
|
|
|
PrintErrorMessage(mChild, channelName, errorMsg);
|
2010-08-21 03:24:40 +04:00
|
|
|
|
|
|
|
mListener->OnProcessingError(MsgDropped);
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2009-12-03 11:16:28 +03:00
|
|
|
// The methods below run in the context of the IO thread
|
2009-07-14 01:55:04 +04:00
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnMessageReceived(const Message& msg)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertIOThread();
|
2009-08-26 03:07:22 +04:00
|
|
|
NS_ASSERTION(mChannelState != ChannelError, "Shouldn't get here!");
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-03-11 10:35:26 +03:00
|
|
|
|
|
|
|
if (!MaybeInterceptSpecialIOMessage(msg))
|
|
|
|
// wake up the worker, there's work to do
|
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnDispatchMessage, msg));
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
2011-06-03 22:33:56 +04:00
|
|
|
void
|
|
|
|
AsyncChannel::OnEchoMessage(Message* msg)
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
OnMessageReceived(*msg);
|
|
|
|
delete msg;
|
|
|
|
}
|
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelOpened()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
2011-04-29 04:15:03 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2011-04-29 04:15:03 +04:00
|
|
|
mChannelState = ChannelOpening;
|
|
|
|
}
|
2009-12-03 11:16:28 +03:00
|
|
|
/*assert*/mTransport->Connect();
|
|
|
|
}
|
|
|
|
|
2010-10-09 03:24:36 +04:00
|
|
|
void
|
|
|
|
AsyncChannel::DispatchOnChannelConnected(int32 peer_pid)
|
|
|
|
{
|
|
|
|
AssertWorkerThread();
|
|
|
|
if (mListener)
|
|
|
|
mListener->OnChannelConnected(peer_pid);
|
|
|
|
}
|
|
|
|
|
2009-07-14 01:55:04 +04:00
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelConnected(int32 peer_pid)
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertIOThread();
|
2009-09-01 20:27:09 +04:00
|
|
|
|
2010-04-12 04:13:00 +04:00
|
|
|
{
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2010-04-12 04:13:00 +04:00
|
|
|
mChannelState = ChannelConnected;
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.Notify();
|
2010-04-12 04:13:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(mExistingListener)
|
|
|
|
mExistingListener->OnChannelConnected(peer_pid);
|
2010-10-09 03:24:36 +04:00
|
|
|
|
|
|
|
mWorkerLoop->PostTask(FROM_HERE, NewRunnableMethod(this,
|
|
|
|
&AsyncChannel::DispatchOnChannelConnected,
|
|
|
|
peer_pid));
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelError()
|
|
|
|
{
|
2009-10-08 23:11:13 +04:00
|
|
|
AssertIOThread();
|
2009-10-09 10:21:39 +04:00
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2009-12-03 11:16:28 +03:00
|
|
|
if (ChannelClosing != mChannelState)
|
|
|
|
mChannelState = ChannelError;
|
2009-07-14 01:55:04 +04:00
|
|
|
|
2010-03-19 01:52:28 +03:00
|
|
|
PostErrorNotifyTask();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::PostErrorNotifyTask()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2010-03-19 01:52:28 +03:00
|
|
|
|
2009-12-18 03:12:03 +03:00
|
|
|
NS_ASSERTION(!mChannelErrorTask, "OnChannelError called twice?");
|
|
|
|
|
2010-02-19 23:39:38 +03:00
|
|
|
// This must be the last code that runs on this thread!
|
2009-12-18 03:12:03 +03:00
|
|
|
mChannelErrorTask =
|
2010-03-12 08:21:58 +03:00
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnNotifyMaybeChannelError);
|
2009-12-18 03:12:03 +03:00
|
|
|
mWorkerLoop->PostTask(FROM_HERE, mChannelErrorTask);
|
2009-07-14 01:55:04 +04:00
|
|
|
}
|
|
|
|
|
2009-11-10 01:56:55 +03:00
|
|
|
void
|
2009-12-03 11:16:28 +03:00
|
|
|
AsyncChannel::OnCloseChannel()
|
2009-11-10 01:56:55 +03:00
|
|
|
{
|
|
|
|
AssertIOThread();
|
|
|
|
|
|
|
|
mTransport->Close();
|
|
|
|
|
2011-04-29 23:21:57 +04:00
|
|
|
MonitorAutoLock lock(mMonitor);
|
2009-12-03 11:16:28 +03:00
|
|
|
mChannelState = ChannelClosed;
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.Notify();
|
2009-11-10 01:56:55 +03:00
|
|
|
}
|
|
|
|
|
2010-03-11 10:35:26 +03:00
|
|
|
bool
|
|
|
|
AsyncChannel::MaybeInterceptSpecialIOMessage(const Message& msg)
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2010-03-11 10:35:26 +03:00
|
|
|
|
|
|
|
if (MSG_ROUTING_NONE == msg.routing_id()
|
|
|
|
&& GOODBYE_MESSAGE_TYPE == msg.type()) {
|
|
|
|
ProcessGoodbyeMessage();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::ProcessGoodbyeMessage()
|
|
|
|
{
|
|
|
|
AssertIOThread();
|
2011-04-29 23:21:57 +04:00
|
|
|
mMonitor.AssertCurrentThreadOwns();
|
2010-03-11 10:35:26 +03: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-14 01:55:04 +04:00
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|