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-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
#include "MessagePump.h"
|
|
|
|
|
2009-07-01 14:19:32 -07:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2009-06-29 11:38:29 -07:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2009-08-28 12:46:21 -07:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-07-01 14:19:32 -07:00
|
|
|
#include "pratom.h"
|
2009-06-29 11:38:29 -07:00
|
|
|
#include "prthread.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/scoped_nsautorelease_pool.h"
|
|
|
|
|
2012-05-03 07:53:31 -07:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
using mozilla::ipc::DoWorkRunnable;
|
2009-06-29 11:38:29 -07:00
|
|
|
using mozilla::ipc::MessagePump;
|
|
|
|
using mozilla::ipc::MessagePumpForChildProcess;
|
2013-02-21 18:10:59 -08:00
|
|
|
using base::TimeTicks;
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(DoWorkRunnable, nsIRunnable, nsITimerCallback)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Run()
|
2009-08-25 16:07:22 -07:00
|
|
|
{
|
2009-11-23 13:01:12 -08:00
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
NS_ASSERTION(loop, "Shouldn't be null!");
|
|
|
|
if (loop) {
|
|
|
|
bool nestableTasksAllowed = loop->NestableTasksAllowed();
|
|
|
|
|
2010-08-19 17:05:51 -07:00
|
|
|
// MessageLoop::RunTask() disallows nesting, but our Frankenventloop
|
2010-08-19 13:31:47 -07:00
|
|
|
// will always dispatch DoWork() below from what looks to
|
|
|
|
// MessageLoop like a nested context. So we unconditionally allow
|
|
|
|
// nesting here.
|
2009-11-23 13:01:12 -08:00
|
|
|
loop->SetNestableTasksAllowed(true);
|
|
|
|
loop->DoWork();
|
|
|
|
loop->SetNestableTasksAllowed(nestableTasksAllowed);
|
2009-08-25 16:07:22 -07:00
|
|
|
}
|
2009-11-23 13:01:12 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-08-25 16:07:22 -07:00
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Notify(nsITimer* aTimer)
|
2009-06-29 11:38:29 -07:00
|
|
|
{
|
2009-11-23 13:01:12 -08:00
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
NS_ASSERTION(loop, "Shouldn't be null!");
|
|
|
|
if (loop) {
|
|
|
|
mPump->DoDelayedWork(loop);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
MessagePump::MessagePump()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mThread(nullptr)
|
2009-06-29 11:38:29 -07:00
|
|
|
{
|
2009-11-23 13:01:12 -08:00
|
|
|
mDoWorkEvent = new DoWorkRunnable(this);
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePump::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(keep_running_, "Quit must have been called outside of Run!");
|
2009-11-23 13:01:12 -08:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Called Run on the wrong thread!");
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2009-07-01 14:19:32 -07:00
|
|
|
mThread = NS_GetCurrentThread();
|
|
|
|
NS_ASSERTION(mThread, "This should never be null!");
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
mDelayedWorkTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
|
|
|
|
NS_ASSERTION(mDelayedWorkTimer, "Failed to create timer!");
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2009-07-01 14:19:32 -07:00
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
for (;;) {
|
2009-07-01 14:19:32 -07:00
|
|
|
autoReleasePool.Recycle();
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
bool did_work = NS_ProcessNextEvent(mThread, false) ? true : false;
|
2009-06-29 11:38:29 -07:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2012-08-25 01:25:08 -07:00
|
|
|
// NB: it is crucial *not* to directly call |aDelegate->DoWork()|
|
|
|
|
// here. To ensure that MessageLoop tasks and XPCOM events have
|
|
|
|
// equal priority, we sensitively rely on processing exactly one
|
|
|
|
// Task per DoWorkRunnable XPCOM event.
|
2009-06-29 11:38:29 -07:00
|
|
|
|
2012-05-03 07:53:31 -07:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
// This processes messages in the Android Looper. Note that we only
|
|
|
|
// get here if the normal Gecko event loop has been awoken above.
|
|
|
|
// Bug 750713
|
2012-06-04 22:14:12 -07:00
|
|
|
did_work |= AndroidBridge::Bridge()->PumpMessageLoop();
|
2012-05-03 07:53:31 -07:00
|
|
|
#endif
|
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
did_work |= aDelegate->DoDelayedWork(&delayed_work_time_);
|
2009-11-23 13:01:12 -08:00
|
|
|
|
|
|
|
if (did_work && delayed_work_time_.is_null())
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
did_work = aDelegate->DoIdleWork();
|
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2012-06-21 07:45:00 -07:00
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
// This will either sleep or process an event.
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ProcessNextEvent(mThread, true);
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
|
2009-11-23 13:01:12 -08:00
|
|
|
mDelayedWorkTimer->Cancel();
|
2009-06-29 11:38:29 -07:00
|
|
|
|
|
|
|
keep_running_ = true;
|
|
|
|
}
|
|
|
|
|
2009-07-01 14:19:32 -07:00
|
|
|
void
|
|
|
|
MessagePump::ScheduleWork()
|
|
|
|
{
|
|
|
|
// Make sure the event loop wakes up.
|
|
|
|
if (mThread) {
|
2009-11-23 13:01:12 -08:00
|
|
|
mThread->Dispatch(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2009-07-01 14:19:32 -07:00
|
|
|
}
|
2009-09-01 15:17:24 -07: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-23 13:01:12 -08:00
|
|
|
NS_DispatchToMainThread(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2009-09-01 15:17:24 -07:00
|
|
|
}
|
|
|
|
event_.Signal();
|
2009-07-01 14:19:32 -07:00
|
|
|
}
|
|
|
|
|
2010-08-19 13:31:47 -07: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-23 13:01:12 -08:00
|
|
|
void
|
2013-02-21 18:10:59 -08:00
|
|
|
MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime)
|
2009-11-23 13:01:12 -08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-02-21 18:10:59 -08:00
|
|
|
base::TimeDelta delay = aDelayedTime - base::TimeTicks::Now();
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t delayMS = uint32_t(delay.InMilliseconds());
|
2009-11-23 13:01:12 -08:00
|
|
|
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 11:38:29 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
namespace {
|
2012-07-30 07:20:58 -07:00
|
|
|
MessagePump::Delegate* gFirstDelegate = nullptr;
|
2009-06-29 11:38:29 -07:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForChildProcess::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
if (mFirstRun) {
|
|
|
|
#ifdef DEBUG
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_ASSERTION(aDelegate && gFirstDelegate == nullptr, "Huh?!");
|
2009-06-29 11:38:29 -07:00
|
|
|
gFirstDelegate = aDelegate;
|
|
|
|
#endif
|
|
|
|
mFirstRun = false;
|
2009-08-28 12:46:21 -07:00
|
|
|
if (NS_FAILED(XRE_RunAppShell())) {
|
2009-06-29 11:38:29 -07:00
|
|
|
NS_WARNING("Failed to run app shell?!");
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(aDelegate && aDelegate == gFirstDelegate, "Huh?!");
|
2012-07-30 07:20:58 -07:00
|
|
|
gFirstDelegate = nullptr;
|
2009-06-29 11:38:29 -07:00
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
2009-07-01 14:19:32 -07:00
|
|
|
|
2009-06-29 11:38:29 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
NS_ASSERTION(aDelegate && aDelegate == gFirstDelegate, "Huh?!");
|
|
|
|
#endif
|
2012-08-25 01:25:08 -07:00
|
|
|
|
|
|
|
// We can get to this point in startup with Tasks in our loop's
|
|
|
|
// incoming_queue_ or pending_queue_, but without a matching
|
|
|
|
// DoWorkRunnable(). In MessagePump::Run() above, we sensitively
|
|
|
|
// depend on *not* directly calling delegate->DoWork(), because that
|
|
|
|
// prioritizes Tasks above XPCOM events. However, from this point
|
|
|
|
// forward, any Task posted to our loop is guaranteed to have a
|
|
|
|
// DoWorkRunnable enqueued for it.
|
|
|
|
//
|
|
|
|
// So we just flush the pending work here and move on.
|
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
bool nestableTasksAllowed = loop->NestableTasksAllowed();
|
|
|
|
loop->SetNestableTasksAllowed(true);
|
|
|
|
|
|
|
|
while (aDelegate->DoWork());
|
|
|
|
|
|
|
|
loop->SetNestableTasksAllowed(nestableTasksAllowed);
|
|
|
|
|
|
|
|
|
2009-07-01 14:19:32 -07:00
|
|
|
// Really run.
|
2009-06-29 11:38:29 -07:00
|
|
|
mozilla::ipc::MessagePump::Run(aDelegate);
|
|
|
|
}
|