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-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
#include "MessagePump.h"
|
|
|
|
|
2013-10-23 08:28:24 +00:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsITimer.h"
|
2014-07-09 21:39:18 +00:00
|
|
|
#include "nsICancelableRunnable.h"
|
2013-10-23 08:28:24 +00:00
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/scoped_nsautorelease_pool.h"
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2009-07-01 21:19:32 +00:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2013-10-23 08:28:24 +00:00
|
|
|
#include "nsDebug.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2013-12-09 19:57:29 +00:00
|
|
|
#include "nsTimerImpl.h"
|
2009-08-28 19:46:21 +00:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "prthread.h"
|
|
|
|
|
2012-05-03 14:53:31 +00:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-03 10:14:44 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
#include "ipc/Nuwa.h"
|
|
|
|
#endif
|
|
|
|
|
2013-02-22 02:10:59 +00:00
|
|
|
using base::TimeTicks;
|
2013-10-23 08:28:24 +00:00
|
|
|
using namespace mozilla::ipc;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2013-12-09 19:57:29 +00:00
|
|
|
NS_DEFINE_NAMED_CID(NS_TIMER_CID);
|
|
|
|
|
2013-10-23 08:28:24 +00:00
|
|
|
static mozilla::DebugOnly<MessagePump::Delegate*> gFirstDelegate;
|
2009-11-23 21:01:12 +00:00
|
|
|
|
2013-10-23 08:28:24 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
2009-08-25 23:07:22 +00:00
|
|
|
|
2014-07-09 21:39:18 +00:00
|
|
|
class DoWorkRunnable MOZ_FINAL : public nsICancelableRunnable,
|
2013-10-23 08:28:24 +00:00
|
|
|
public nsITimerCallback
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2013-10-23 08:28:24 +00:00
|
|
|
public:
|
|
|
|
DoWorkRunnable(MessagePump* aPump)
|
|
|
|
: mPump(aPump)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPump);
|
2009-11-23 21:01:12 +00:00
|
|
|
}
|
2013-10-23 08:28:24 +00:00
|
|
|
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2014-07-09 21:39:18 +00:00
|
|
|
NS_DECL_NSICANCELABLERUNNABLE
|
2013-10-23 08:28:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
~DoWorkRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
MessagePump* mPump;
|
2014-07-09 21:39:18 +00:00
|
|
|
// DoWorkRunnable is designed as a stateless singleton. Do not add stateful
|
|
|
|
// members here!
|
2013-10-23 08:28:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace ipc */
|
|
|
|
} /* namespace mozilla */
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-11-23 21:01:12 +00:00
|
|
|
MessagePump::MessagePump()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mThread(nullptr)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2009-11-23 21:01:12 +00:00
|
|
|
mDoWorkEvent = new DoWorkRunnable(this);
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-10-23 08:28:24 +00:00
|
|
|
MessagePump::~MessagePump()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
void
|
|
|
|
MessagePump::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
2013-10-23 08:28:24 +00:00
|
|
|
MOZ_ASSERT(keep_running_);
|
2013-10-23 12:01:24 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"Use mozilla::ipc::MessagePumpForNonMainThreads instead!");
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-07-01 21:19:32 +00:00
|
|
|
mThread = NS_GetCurrentThread();
|
2013-10-23 08:28:24 +00:00
|
|
|
MOZ_ASSERT(mThread);
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2013-12-09 19:57:29 +00:00
|
|
|
mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
|
2013-10-23 08:28:24 +00:00
|
|
|
MOZ_ASSERT(mDelayedWorkTimer);
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2009-07-01 21:19:32 +00:00
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2009-07-01 21:19:32 +00:00
|
|
|
autoReleasePool.Recycle();
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
bool did_work = NS_ProcessNextEvent(mThread, false) ? true : false;
|
2009-06-29 18:38:29 +00:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2012-08-25 08:25:08 +00: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 18:38:29 +00:00
|
|
|
|
2012-05-03 14:53:31 +00: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
|
2014-01-18 05:32:24 +00:00
|
|
|
if (MOZ_LIKELY(AndroidBridge::HasEnv())) {
|
2014-04-01 12:29:25 +00:00
|
|
|
did_work |= mozilla::widget::android::GeckoAppShell::PumpMessageLoop();
|
2014-01-18 05:32:24 +00:00
|
|
|
}
|
2012-05-03 14:53:31 +00:00
|
|
|
#endif
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
did_work |= aDelegate->DoDelayedWork(&delayed_work_time_);
|
2009-11-23 21:01:12 +00:00
|
|
|
|
2013-06-03 10:14:44 +00:00
|
|
|
if (did_work && delayed_work_time_.is_null()
|
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
&& (!IsNuwaReady() || !IsNuwaProcess())
|
|
|
|
#endif
|
|
|
|
)
|
2009-11-23 21:01:12 +00:00
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
did_work = aDelegate->DoIdleWork();
|
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2012-06-21 14:45:00 +00:00
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
2009-11-23 21:01:12 +00:00
|
|
|
// This will either sleep or process an event.
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ProcessNextEvent(mThread, true);
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2013-06-03 10:14:44 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
if (!IsNuwaReady() || !IsNuwaProcess())
|
|
|
|
#endif
|
|
|
|
mDelayedWorkTimer->Cancel();
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
keep_running_ = true;
|
|
|
|
}
|
|
|
|
|
2009-07-01 21:19:32 +00:00
|
|
|
void
|
|
|
|
MessagePump::ScheduleWork()
|
|
|
|
{
|
|
|
|
// Make sure the event loop wakes up.
|
|
|
|
if (mThread) {
|
2009-11-23 21:01:12 +00:00
|
|
|
mThread->Dispatch(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2009-07-01 21:19:32 +00:00
|
|
|
}
|
2009-09-01 22:17:24 +00: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.
|
2014-05-23 19:53:17 +00:00
|
|
|
NS_DispatchToMainThread(mDoWorkEvent);
|
2009-09-01 22:17:24 +00:00
|
|
|
}
|
|
|
|
event_.Signal();
|
2009-07-01 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
2010-08-19 20:31:47 +00: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 21:01:12 +00:00
|
|
|
void
|
2013-02-22 02:10:59 +00:00
|
|
|
MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime)
|
2009-11-23 21:01:12 +00:00
|
|
|
{
|
2013-06-03 10:14:44 +00:00
|
|
|
#ifdef MOZ_NUWA_PROCESS
|
|
|
|
if (IsNuwaReady() && IsNuwaProcess())
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2009-11-23 21:01:12 +00:00
|
|
|
if (!mDelayedWorkTimer) {
|
2013-12-09 19:57:29 +00:00
|
|
|
mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
|
2009-11-23 21:01:12 +00:00
|
|
|
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-07-10 16:39:09 +00:00
|
|
|
// TimeDelta's constructor initializes to 0
|
|
|
|
base::TimeDelta delay;
|
|
|
|
if (aDelayedTime > base::TimeTicks::Now())
|
|
|
|
delay = aDelayedTime - base::TimeTicks::Now();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t delayMS = uint32_t(delay.InMilliseconds());
|
2009-11-23 21:01:12 +00: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_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 21:39:18 +00:00
|
|
|
NS_IMPL_ISUPPORTS(DoWorkRunnable, nsIRunnable, nsITimerCallback,
|
|
|
|
nsICancelableRunnable)
|
2013-10-23 08:28:24 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Run()
|
|
|
|
{
|
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
MOZ_ASSERT(loop);
|
|
|
|
|
|
|
|
bool nestableTasksAllowed = loop->NestableTasksAllowed();
|
|
|
|
|
|
|
|
// MessageLoop::RunTask() disallows nesting, but our Frankenventloop will
|
|
|
|
// always dispatch DoWork() below from what looks to MessageLoop like a nested
|
|
|
|
// context. So we unconditionally allow nesting here.
|
|
|
|
loop->SetNestableTasksAllowed(true);
|
|
|
|
loop->DoWork();
|
|
|
|
loop->SetNestableTasksAllowed(nestableTasksAllowed);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
MessageLoop* loop = MessageLoop::current();
|
|
|
|
MOZ_ASSERT(loop);
|
|
|
|
|
|
|
|
mPump->DoDelayedWork(loop);
|
|
|
|
|
|
|
|
return NS_OK;
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2014-07-09 21:39:18 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DoWorkRunnable::Cancel()
|
|
|
|
{
|
|
|
|
// Workers require cancelable runnables, but we can't really cancel cleanly
|
|
|
|
// here. If we don't process this runnable then we will leave something
|
|
|
|
// unprocessed in the message_loop. Therefore, eagerly complete our work
|
|
|
|
// instead by immediately calling Run(). Run() should be called separately
|
|
|
|
// after this. Unfortunately we cannot use flags to verify this because
|
|
|
|
// DoWorkRunnable is a stateless singleton that can be in the event queue
|
|
|
|
// multiple times simultaneously.
|
|
|
|
MOZ_ALWAYS_TRUE(NS_SUCCEEDED(Run()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
void
|
2013-10-23 08:28:24 +00:00
|
|
|
MessagePumpForChildProcess::Run(base::MessagePump::Delegate* aDelegate)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
|
|
|
if (mFirstRun) {
|
2013-10-23 08:28:24 +00:00
|
|
|
MOZ_ASSERT(aDelegate && !gFirstDelegate);
|
2009-06-29 18:38:29 +00:00
|
|
|
gFirstDelegate = aDelegate;
|
2013-10-23 08:28:24 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
mFirstRun = false;
|
2009-08-28 19:46:21 +00:00
|
|
|
if (NS_FAILED(XRE_RunAppShell())) {
|
2009-06-29 18:38:29 +00:00
|
|
|
NS_WARNING("Failed to run app shell?!");
|
|
|
|
}
|
2013-10-23 08:28:24 +00:00
|
|
|
|
|
|
|
MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate);
|
2012-07-30 14:20:58 +00:00
|
|
|
gFirstDelegate = nullptr;
|
2013-10-23 08:28:24 +00:00
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-07-01 21:19:32 +00:00
|
|
|
|
2013-10-23 08:28:24 +00:00
|
|
|
MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate);
|
2012-08-25 08:25:08 +00: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 21:19:32 +00:00
|
|
|
// Really run.
|
2009-06-29 18:38:29 +00:00
|
|
|
mozilla::ipc::MessagePump::Run(aDelegate);
|
|
|
|
}
|
2013-10-23 12:01:24 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(keep_running_);
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread(), "Use mozilla::ipc::MessagePump instead!");
|
|
|
|
|
|
|
|
mThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(mThread);
|
|
|
|
|
2013-12-09 19:57:29 +00:00
|
|
|
mDelayedWorkTimer = do_CreateInstance(kNS_TIMER_CID);
|
2013-10-23 12:01:24 +00:00
|
|
|
MOZ_ASSERT(mDelayedWorkTimer);
|
|
|
|
|
|
|
|
if (NS_FAILED(mDelayedWorkTimer->SetTarget(mThread))) {
|
|
|
|
MOZ_CRASH("Failed to set timer target!");
|
|
|
|
}
|
|
|
|
|
2014-03-25 20:27:12 +00:00
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
|
|
|
|
2013-10-23 12:01:24 +00:00
|
|
|
for (;;) {
|
2014-03-25 20:27:12 +00:00
|
|
|
autoReleasePool.Recycle();
|
|
|
|
|
2013-10-23 12:01:24 +00:00
|
|
|
bool didWork = NS_ProcessNextEvent(mThread, false) ? true : false;
|
|
|
|
if (!keep_running_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
didWork |= aDelegate->DoDelayedWork(&delayed_work_time_);
|
|
|
|
|
|
|
|
if (didWork && delayed_work_time_.is_null()) {
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!keep_running_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
didWork = aDelegate->DoIdleWork();
|
|
|
|
if (!keep_running_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will either sleep or process an event.
|
|
|
|
NS_ProcessNextEvent(mThread, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
|
|
|
|
keep_running_ = true;
|
|
|
|
}
|