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 :
|
|
|
|
*/
|
|
|
|
/* ***** 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"
|
|
|
|
#include "mozilla/ipc/GeckoThread.h"
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
2009-08-25 16:07:22 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-07-13 14:55:04 -07:00
|
|
|
|
2009-09-01 09:27:09 -07:00
|
|
|
using mozilla::MutexAutoLock;
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
template<>
|
|
|
|
struct RunnableMethodTraits<mozilla::ipc::AsyncChannel>
|
|
|
|
{
|
|
|
|
static void RetainCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
static void ReleaseCallee(mozilla::ipc::AsyncChannel* obj) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
|
|
|
|
|
|
|
bool
|
|
|
|
AsyncChannel::Open(Transport* aTransport, MessageLoop* aIOLoop)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(!mTransport, "Open() called > once");
|
|
|
|
NS_PRECONDITION(aTransport, "need transport layer");
|
|
|
|
|
|
|
|
// FIXME need to check for valid channel
|
|
|
|
|
|
|
|
mTransport = aTransport;
|
|
|
|
mTransport->set_listener(this);
|
|
|
|
|
2009-07-13 22:12:50 -07:00
|
|
|
// FIXME figure out whether we're in parent or child, grab IO loop
|
|
|
|
// appropriately
|
2009-07-13 14:55:04 -07:00
|
|
|
bool needOpen = true;
|
|
|
|
if(!aIOLoop) {
|
2009-07-13 22:12:50 -07:00
|
|
|
// parent
|
2009-07-13 14:55:04 -07:00
|
|
|
needOpen = false;
|
|
|
|
aIOLoop = BrowserProcessSubThread
|
|
|
|
::GetMessageLoop(BrowserProcessSubThread::IO);
|
2009-07-13 22:12:50 -07:00
|
|
|
// FIXME assuming that the parent waits for the OnConnected event.
|
|
|
|
// FIXME see GeckoChildProcessHost.cpp. bad assumption!
|
2009-08-19 08:44:56 -07:00
|
|
|
mChannelState = ChannelConnected;
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
2009-10-08 14:44:43 -07:00
|
|
|
mChild = needOpen;
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
mIOLoop = aIOLoop;
|
|
|
|
mWorkerLoop = MessageLoop::current();
|
|
|
|
|
|
|
|
NS_ASSERTION(mIOLoop, "need an IO loop");
|
|
|
|
NS_ASSERTION(mWorkerLoop, "need a worker loop");
|
|
|
|
|
2009-09-01 09:27:09 -07:00
|
|
|
if (needOpen) { // child process
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
mIOLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this,
|
|
|
|
&AsyncChannel::OnChannelOpened));
|
2009-09-01 09:27:09 -07:00
|
|
|
|
|
|
|
// FIXME/cjones: handle errors
|
|
|
|
while (mChannelState != ChannelConnected) {
|
|
|
|
mCvar.Wait();
|
|
|
|
}
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::Close()
|
|
|
|
{
|
|
|
|
// FIXME impl
|
|
|
|
|
|
|
|
mChannelState = ChannelClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AsyncChannel::Send(Message* msg)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertWorkerThread();
|
2009-10-08 23:21:39 -07:00
|
|
|
mMutex.AssertNotCurrentThreadOwns();
|
2009-09-21 19:02:15 -07:00
|
|
|
NS_ABORT_IF_FALSE(MSG_ROUTING_NONE != msg->routing_id(), "need a route");
|
|
|
|
|
2009-10-08 23:21:39 -07:00
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
|
|
|
|
if (!Connected())
|
|
|
|
// trying to Send() to a closed or error'd channel
|
|
|
|
return false;
|
|
|
|
|
|
|
|
mIOLoop->PostTask(FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnSend, msg));
|
|
|
|
}
|
2009-08-19 08:44:56 -07:00
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnDispatchMessage(const Message& msg)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertWorkerThread();
|
2009-07-13 14:55:04 -07:00
|
|
|
NS_ASSERTION(!msg.is_reply(), "can't process replies here");
|
|
|
|
NS_ASSERTION(!(msg.is_sync() || msg.is_rpc()), "async dispatch only");
|
|
|
|
|
|
|
|
switch (mListener->OnMessageReceived(msg)) {
|
2009-07-15 14:38:55 -07:00
|
|
|
case MsgProcessed:
|
2009-07-13 14:55:04 -07:00
|
|
|
return;
|
|
|
|
|
2009-07-15 14:38:55 -07:00
|
|
|
case MsgNotKnown:
|
|
|
|
case MsgNotAllowed:
|
|
|
|
case MsgPayloadError:
|
|
|
|
case MsgRouteError:
|
|
|
|
case MsgValueError:
|
2009-07-13 14:55:04 -07:00
|
|
|
// FIXME/cjones: error handling; OnError()?
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
NOTREACHED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// The methods below run in the context of the IO thread, and can proxy
|
|
|
|
// back to the methods above
|
|
|
|
//
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnMessageReceived(const Message& msg)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertIOThread();
|
2009-08-25 16:07:22 -07:00
|
|
|
NS_ASSERTION(mChannelState != ChannelError, "Shouldn't get here!");
|
|
|
|
|
2009-07-13 14:55:04 -07:00
|
|
|
// wake up the worker, there's work to do
|
2009-10-08 23:21:39 -07:00
|
|
|
mWorkerLoop->PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
NewRunnableMethod(this, &AsyncChannel::OnDispatchMessage, msg));
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelConnected(int32 peer_pid)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertIOThread();
|
2009-09-01 09:27:09 -07:00
|
|
|
|
2009-10-08 23:21:39 -07:00
|
|
|
MutexAutoLock lock(mMutex);
|
2009-08-19 08:44:56 -07:00
|
|
|
mChannelState = ChannelConnected;
|
2009-09-01 09:27:09 -07:00
|
|
|
mCvar.Notify();
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelError()
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertIOThread();
|
2009-10-08 23:21:39 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mChannelState = ChannelError;
|
|
|
|
}
|
2009-08-25 16:07:22 -07:00
|
|
|
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
// Parent process, one of our children died. Notify?
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Child process, initiate quit sequence.
|
|
|
|
#ifdef DEBUG
|
|
|
|
// XXXbent this is totally out of place, but works for now.
|
2009-08-28 12:46:21 -07:00
|
|
|
XRE_ShutdownChildProcess(mWorkerLoop);
|
2009-08-25 16:07:22 -07:00
|
|
|
|
|
|
|
// Must exit the IO loop, which will then join with the UI loop.
|
|
|
|
MessageLoop::current()->Quit();
|
|
|
|
#else
|
|
|
|
// Go ahead and abort here.
|
|
|
|
NS_DebugBreak(NS_DEBUG_ABORT, nsnull, nsnull, nsnull, 0);
|
|
|
|
#endif
|
|
|
|
}
|
2009-07-13 14:55:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnChannelOpened()
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertIOThread();
|
2009-07-13 14:55:04 -07:00
|
|
|
mChannelState = ChannelOpening;
|
|
|
|
/*assert*/mTransport->Connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AsyncChannel::OnSend(Message* aMsg)
|
|
|
|
{
|
2009-10-08 12:11:13 -07:00
|
|
|
AssertIOThread();
|
2009-07-13 14:55:04 -07:00
|
|
|
mTransport->Send(aMsg);
|
|
|
|
// mTransport deletes aMsg
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace ipc
|
|
|
|
} // namespace mozilla
|