2009-06-29 22:38:29 +04:00
|
|
|
/* ***** 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 IPC.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Ben Turner <bent.mozilla@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 "MessagePump.h"
|
|
|
|
|
2009-07-02 01:19:32 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2009-08-28 23:46:21 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-07-02 01:19:32 +04:00
|
|
|
#include "pratom.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "prthread.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/scoped_nsautorelease_pool.h"
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
using mozilla::ipc::DoWorkRunnable;
|
2009-06-29 22:38:29 +04:00
|
|
|
using mozilla::ipc::MessagePump;
|
|
|
|
using mozilla::ipc::MessagePumpForChildProcess;
|
2009-11-24 00:01:12 +03:00
|
|
|
using base::Time;
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(DoWorkRunnable, nsIRunnable, nsITimerCallback)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Run()
|
2009-08-26 03:07:22 +04:00
|
|
|
{
|
2009-11-24 00:01:12 +03:00
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
NS_ASSERTION(loop, "Shouldn't be null!");
|
|
|
|
if (loop) {
|
|
|
|
bool nestableTasksAllowed = loop->NestableTasksAllowed();
|
|
|
|
|
2010-08-20 04:05:51 +04:00
|
|
|
// MessageLoop::RunTask() disallows nesting, but our Frankenventloop
|
2010-08-20 00:31:47 +04:00
|
|
|
// will always dispatch DoWork() below from what looks to
|
|
|
|
// MessageLoop like a nested context. So we unconditionally allow
|
|
|
|
// nesting here.
|
2009-11-24 00:01:12 +03:00
|
|
|
loop->SetNestableTasksAllowed(true);
|
|
|
|
loop->DoWork();
|
|
|
|
loop->SetNestableTasksAllowed(nestableTasksAllowed);
|
2009-08-26 03:07:22 +04:00
|
|
|
}
|
2009-11-24 00:01:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Notify(nsITimer* aTimer)
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
2009-11-24 00:01:12 +03:00
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
NS_ASSERTION(loop, "Shouldn't be null!");
|
|
|
|
if (loop) {
|
|
|
|
mPump->DoDelayedWork(loop);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
MessagePump::MessagePump()
|
|
|
|
: mThread(nsnull)
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
2009-11-24 00:01:12 +03:00
|
|
|
mDoWorkEvent = new DoWorkRunnable(this);
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePump::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(keep_running_, "Quit must have been called outside of Run!");
|
2009-11-24 00:01:12 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Called Run on the wrong thread!");
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-07-02 01:19:32 +04:00
|
|
|
mThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(mThread, "This should never be null!");
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
mDelayedWorkTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
NS_ASSERTION(mDelayedWorkTimer, "Failed to create timer!");
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-07-02 01:19:32 +04:00
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
for (;;) {
|
2009-07-02 01:19:32 +04:00
|
|
|
autoReleasePool.Recycle();
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2009-07-02 01:19:32 +04:00
|
|
|
bool did_work = NS_ProcessNextEvent(mThread, PR_FALSE) ? true : false;
|
2009-06-29 22:38:29 +04:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
did_work |= aDelegate->DoWork();
|
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
did_work |= aDelegate->DoDelayedWork(&delayed_work_time_);
|
2009-11-24 00:01:12 +03:00
|
|
|
|
|
|
|
if (did_work && delayed_work_time_.is_null())
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
did_work = aDelegate->DoIdleWork();
|
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
// This will either sleep or process an event.
|
|
|
|
NS_ProcessNextEvent(mThread, PR_TRUE);
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
mDelayedWorkTimer->Cancel();
|
2009-06-29 22:38:29 +04:00
|
|
|
|
|
|
|
keep_running_ = true;
|
|
|
|
}
|
|
|
|
|
2009-07-02 01:19:32 +04:00
|
|
|
void
|
|
|
|
MessagePump::ScheduleWork()
|
|
|
|
{
|
|
|
|
// Make sure the event loop wakes up.
|
|
|
|
if (mThread) {
|
2009-11-24 00:01:12 +03:00
|
|
|
mThread->Dispatch(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2009-07-02 01:19:32 +04:00
|
|
|
}
|
2009-09-02 02:17:24 +04:00
|
|
|
else {
|
|
|
|
// Some things (like xpcshell) don't use the app shell and so Run hasn't
|
|
|
|
// been called. We still need to wake up the main thread.
|
2009-11-24 00:01:12 +03:00
|
|
|
NS_DispatchToMainThread(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2009-09-02 02:17:24 +04:00
|
|
|
}
|
|
|
|
event_.Signal();
|
2009-07-02 01:19:32 +04:00
|
|
|
}
|
|
|
|
|
2010-08-20 00:31:47 +04:00
|
|
|
void
|
|
|
|
MessagePump::ScheduleWorkForNestedLoop()
|
|
|
|
{
|
|
|
|
// This method is called when our MessageLoop has just allowed
|
|
|
|
// nested tasks. In our setup, whenever that happens we know that
|
|
|
|
// DoWork() will be called "soon", so there's no need to pay the
|
|
|
|
// cost of what will be a no-op nsThread::Dispatch(mDoWorkEvent).
|
|
|
|
}
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
void
|
|
|
|
MessagePump::ScheduleDelayedWork(const base::Time& aDelayedTime)
|
|
|
|
{
|
|
|
|
if (!mDelayedWorkTimer) {
|
|
|
|
mDelayedWorkTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
if (!mDelayedWorkTimer) {
|
|
|
|
// Called before XPCOM has started up? We can't do this correctly.
|
|
|
|
NS_WARNING("Delayed task might not run!");
|
|
|
|
delayed_work_time_ = aDelayedTime;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!delayed_work_time_.is_null()) {
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
delayed_work_time_ = aDelayedTime;
|
|
|
|
|
|
|
|
base::TimeDelta delay = aDelayedTime - base::Time::Now();
|
|
|
|
PRUint32 delayMS = PRUint32(delay.InMilliseconds());
|
|
|
|
mDelayedWorkTimer->InitWithCallback(mDoWorkEvent, delayMS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePump::DoDelayedWork(base::MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
aDelegate->DoDelayedWork(&delayed_work_time_);
|
|
|
|
if (!delayed_work_time_.is_null()) {
|
|
|
|
ScheduleDelayedWork(delayed_work_time_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
namespace {
|
|
|
|
MessagePump::Delegate* gFirstDelegate = nsnull;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForChildProcess::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
if (mFirstRun) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(aDelegate && gFirstDelegate == nsnull, "Huh?!");
|
|
|
|
gFirstDelegate = aDelegate;
|
|
|
|
#endif
|
|
|
|
mFirstRun = false;
|
2009-08-28 23:46:21 +04:00
|
|
|
if (NS_FAILED(XRE_RunAppShell())) {
|
2009-06-29 22:38:29 +04:00
|
|
|
NS_WARNING("Failed to run app shell?!");
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(aDelegate && aDelegate == gFirstDelegate, "Huh?!");
|
|
|
|
gFirstDelegate = nsnull;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2009-07-02 01:19:32 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(aDelegate && aDelegate == gFirstDelegate, "Huh?!");
|
|
|
|
#endif
|
2009-07-02 01:19:32 +04:00
|
|
|
// Really run.
|
2009-06-29 22:38:29 +04:00
|
|
|
mozilla::ipc::MessagePump::Run(aDelegate);
|
|
|
|
}
|