2016-05-28 00:54:31 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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 22:38:29 +04:00
|
|
|
|
|
|
|
#include "MessagePump.h"
|
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsITimer.h"
|
2014-07-10 01:39:18 +04:00
|
|
|
#include "nsICancelableRunnable.h"
|
2013-10-23 12:28:24 +04: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-02 01:19:32 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2013-10-23 12:28:24 +04:00
|
|
|
#include "nsDebug.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-09-23 21:25:00 +04:00
|
|
|
#include "nsString.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2013-12-09 23:57:29 +04:00
|
|
|
#include "nsTimerImpl.h"
|
2009-08-28 23:46:21 +04:00
|
|
|
#include "nsXULAppAPI.h"
|
2009-06-29 22:38:29 +04:00
|
|
|
#include "prthread.h"
|
|
|
|
|
2013-02-22 06:10:59 +04:00
|
|
|
using base::TimeTicks;
|
2013-10-23 12:28:24 +04:00
|
|
|
using namespace mozilla::ipc;
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2013-12-09 23:57:29 +04:00
|
|
|
NS_DEFINE_NAMED_CID(NS_TIMER_CID);
|
|
|
|
|
2016-02-26 18:52:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
static MessagePump::Delegate* gFirstDelegate;
|
|
|
|
#endif
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace ipc {
|
2009-08-26 03:07:22 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
class DoWorkRunnable final : public CancelableRunnable,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsITimerCallback
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
2013-10-23 12:28:24 +04:00
|
|
|
public:
|
2014-09-01 05:12:55 +04:00
|
|
|
explicit DoWorkRunnable(MessagePump* aPump)
|
2017-06-12 22:34:10 +03:00
|
|
|
: CancelableRunnable("ipc::DoWorkRunnable")
|
|
|
|
, mPump(aPump)
|
2013-10-23 12:28:24 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPump);
|
2009-11-24 00:01:12 +03:00
|
|
|
}
|
2013-10-23 12:28:24 +04:00
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2013-10-23 12:28:24 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult Cancel() override;
|
2013-10-23 12:28:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~DoWorkRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
MessagePump* mPump;
|
2014-07-10 01:39:18 +04:00
|
|
|
// DoWorkRunnable is designed as a stateless singleton. Do not add stateful
|
|
|
|
// members here!
|
2013-10-23 12:28:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace ipc */
|
|
|
|
} /* namespace mozilla */
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2017-08-11 02:00:32 +03:00
|
|
|
MessagePump::MessagePump(nsIEventTarget* aEventTarget)
|
|
|
|
: mEventTarget(aEventTarget)
|
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
|
|
|
}
|
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
MessagePump::~MessagePump()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
void
|
|
|
|
MessagePump::Run(MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
2013-10-23 12:28:24 +04:00
|
|
|
MOZ_ASSERT(keep_running_);
|
2016-03-30 22:20:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(NS_IsMainThread(),
|
2016-05-28 00:54:30 +03:00
|
|
|
"Use mozilla::ipc::MessagePumpForNonMainThreads instead!");
|
2017-08-11 02:00:32 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!mEventTarget);
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
nsIThread* thisThread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(thisThread);
|
2009-06-29 22:38:29 +04:00
|
|
|
|
2017-09-25 05:57:48 +03:00
|
|
|
mDelayedWorkTimer = NS_NewTimer();
|
2013-10-23 12:28:24 +04:00
|
|
|
MOZ_ASSERT(mDelayedWorkTimer);
|
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
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
bool did_work = NS_ProcessNextEvent(thisThread, false) ? true : false;
|
2009-06-29 22:38:29 +04:00
|
|
|
if (!keep_running_)
|
|
|
|
break;
|
|
|
|
|
2012-08-25 12:25:08 +04: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 22:38:29 +04:00
|
|
|
|
|
|
|
did_work |= aDelegate->DoDelayedWork(&delayed_work_time_);
|
2009-11-24 00:01:12 +03:00
|
|
|
|
2016-08-02 15:54:00 +03:00
|
|
|
if (did_work && delayed_work_time_.is_null())
|
2009-11-24 00:01:12 +03:00
|
|
|
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;
|
|
|
|
|
2012-06-21 18:45:00 +04:00
|
|
|
if (did_work)
|
|
|
|
continue;
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
// This will either sleep or process an event.
|
2016-03-30 22:20:20 +03:00
|
|
|
NS_ProcessNextEvent(thisThread, true);
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
2013-06-03 14:14:44 +04: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.
|
2017-08-11 02:00:32 +03:00
|
|
|
if (mEventTarget) {
|
|
|
|
mEventTarget->Dispatch(mDoWorkEvent, NS_DISPATCH_NORMAL);
|
2016-03-30 22:20:20 +03:00
|
|
|
} else {
|
2009-09-02 02:17:24 +04:00
|
|
|
// 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 23:53:17 +04:00
|
|
|
NS_DispatchToMainThread(mDoWorkEvent);
|
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
|
2013-02-22 06:10:59 +04:00
|
|
|
MessagePump::ScheduleDelayedWork(const base::TimeTicks& aDelayedTime)
|
2009-11-24 00:01:12 +03:00
|
|
|
{
|
2016-03-30 22:20:20 +03:00
|
|
|
// To avoid racing on mDelayedWorkTimer, we need to be on the same thread as
|
|
|
|
// ::Run().
|
2017-08-11 02:00:32 +03:00
|
|
|
MOZ_RELEASE_ASSERT((!mEventTarget && NS_IsMainThread())
|
|
|
|
|| mEventTarget->IsOnCurrentThread());
|
2016-03-30 22:20:20 +03:00
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
if (!mDelayedWorkTimer) {
|
2017-09-25 05:57:48 +03:00
|
|
|
mDelayedWorkTimer = NS_NewTimer();
|
2009-11-24 00:01:12 +03: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 20:39:09 +04:00
|
|
|
// TimeDelta's constructor initializes to 0
|
|
|
|
base::TimeDelta delay;
|
|
|
|
if (aDelayedTime > base::TimeTicks::Now())
|
|
|
|
delay = aDelayedTime - base::TimeTicks::Now();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t delayMS = uint32_t(delay.InMilliseconds());
|
2009-11-24 00:01:12 +03:00
|
|
|
mDelayedWorkTimer->InitWithCallback(mDoWorkEvent, delayMS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
nsIEventTarget*
|
|
|
|
MessagePump::GetXPCOMThread()
|
|
|
|
{
|
2017-08-11 02:00:32 +03:00
|
|
|
if (mEventTarget) {
|
|
|
|
return mEventTarget;
|
2016-05-13 01:15:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main thread
|
2017-08-11 02:00:32 +03:00
|
|
|
return GetMainThreadEventTarget();
|
2016-05-13 01:15:43 +03:00
|
|
|
}
|
|
|
|
|
2009-11-24 00:01:12 +03:00
|
|
|
void
|
|
|
|
MessagePump::DoDelayedWork(base::MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
aDelegate->DoDelayedWork(&delayed_work_time_);
|
|
|
|
if (!delayed_work_time_.is_null()) {
|
|
|
|
ScheduleDelayedWork(delayed_work_time_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(DoWorkRunnable, CancelableRunnable,
|
2016-05-28 00:54:30 +03:00
|
|
|
nsITimerCallback)
|
2013-10-23 12:28:24 +04: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);
|
|
|
|
|
2016-04-01 15:25:40 +03:00
|
|
|
bool nestableTasksAllowed = loop->NestableTasksAllowed();
|
|
|
|
loop->SetNestableTasksAllowed(true);
|
2013-10-23 12:28:24 +04:00
|
|
|
mPump->DoDelayedWork(loop);
|
2016-04-01 15:25:40 +03:00
|
|
|
loop->SetNestableTasksAllowed(nestableTasksAllowed);
|
2013-10-23 12:28:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2009-06-29 22:38:29 +04:00
|
|
|
}
|
|
|
|
|
2016-04-11 21:40:06 +03:00
|
|
|
nsresult
|
2014-07-10 01:39:18 +04:00
|
|
|
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.
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(Run());
|
2014-07-10 01:39:18 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
void
|
2013-10-23 12:28:24 +04:00
|
|
|
MessagePumpForChildProcess::Run(base::MessagePump::Delegate* aDelegate)
|
2009-06-29 22:38:29 +04:00
|
|
|
{
|
|
|
|
if (mFirstRun) {
|
2013-10-23 12:28:24 +04:00
|
|
|
MOZ_ASSERT(aDelegate && !gFirstDelegate);
|
2016-02-26 18:52:07 +03:00
|
|
|
#ifdef DEBUG
|
2009-06-29 22:38:29 +04:00
|
|
|
gFirstDelegate = aDelegate;
|
2016-02-26 18:52:07 +03:00
|
|
|
#endif
|
2013-10-23 12:28:24 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
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?!");
|
|
|
|
}
|
2013-10-23 12:28:24 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate);
|
2016-02-26 18:52:07 +03:00
|
|
|
#ifdef DEBUG
|
2012-07-30 18:20:58 +04:00
|
|
|
gFirstDelegate = nullptr;
|
2016-02-26 18:52:07 +03:00
|
|
|
#endif
|
2013-10-23 12:28:24 +04:00
|
|
|
|
2009-06-29 22:38:29 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-07-02 01:19:32 +04:00
|
|
|
|
2013-10-23 12:28:24 +04:00
|
|
|
MOZ_ASSERT(aDelegate && aDelegate == gFirstDelegate);
|
2012-08-25 12:25:08 +04: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-02 01:19:32 +04:00
|
|
|
// Really run.
|
2009-06-29 22:38:29 +04:00
|
|
|
mozilla::ipc::MessagePump::Run(aDelegate);
|
|
|
|
}
|
2013-10-23 16:01:24 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForNonMainThreads::Run(base::MessagePump::Delegate* aDelegate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(keep_running_);
|
2016-03-30 22:20:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!NS_IsMainThread(), "Use mozilla::ipc::MessagePump instead!");
|
2013-10-23 16:01:24 +04:00
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
2017-08-11 02:00:32 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mEventTarget->IsOnCurrentThread());
|
2013-10-23 16:01:24 +04:00
|
|
|
|
2017-09-25 05:57:48 +03:00
|
|
|
mDelayedWorkTimer = NS_NewTimer(mEventTarget);
|
2013-10-23 16:01:24 +04:00
|
|
|
MOZ_ASSERT(mDelayedWorkTimer);
|
|
|
|
|
2014-09-12 18:49:38 +04:00
|
|
|
// Chromium event notifications to be processed will be received by this
|
|
|
|
// event loop as a DoWorkRunnables via ScheduleWork. Chromium events that
|
2016-03-30 22:20:20 +03:00
|
|
|
// were received before our thread is valid, however, will not generate
|
2014-09-12 18:49:38 +04:00
|
|
|
// runnable wrappers. We must process any of these before we enter this
|
|
|
|
// loop, or we will forever have unprocessed chromium messages in our queue.
|
|
|
|
//
|
|
|
|
// Note we would like to request a flush of the chromium event queue
|
|
|
|
// using a runnable on the xpcom side, but some thread implementations
|
|
|
|
// (dom workers) get cranky if we call ScheduleWork here (ScheduleWork
|
2017-08-11 02:00:32 +03:00
|
|
|
// calls dispatch on mEventTarget) before the thread processes an event. As
|
2014-09-12 18:49:38 +04:00
|
|
|
// such, clear the queue manually.
|
|
|
|
while (aDelegate->DoWork()) {
|
|
|
|
}
|
2014-03-26 00:27:12 +04:00
|
|
|
|
2014-09-12 18:49:38 +04:00
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
2013-10-23 16:01:24 +04:00
|
|
|
for (;;) {
|
2014-03-26 00:27:12 +04:00
|
|
|
autoReleasePool.Recycle();
|
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
bool didWork = NS_ProcessNextEvent(thread, false) ? true : false;
|
2013-10-23 16:01:24 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
DebugOnly<bool> didIdleWork = aDelegate->DoIdleWork();
|
|
|
|
MOZ_ASSERT(!didIdleWork);
|
2013-10-23 16:01:24 +04:00
|
|
|
if (!keep_running_) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will either sleep or process an event.
|
2016-03-30 22:20:20 +03:00
|
|
|
NS_ProcessNextEvent(thread, true);
|
2013-10-23 16:01:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mDelayedWorkTimer->Cancel();
|
|
|
|
|
|
|
|
keep_running_ = true;
|
|
|
|
}
|
2014-09-12 18:49:38 +04:00
|
|
|
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
|
|
|
|
NS_IMPL_QUERY_INTERFACE(MessagePumpForNonMainUIThreads, nsIThreadObserver)
|
|
|
|
|
|
|
|
#define CHECK_QUIT_STATE { if (state_->should_quit) { break; } }
|
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
void
|
|
|
|
MessagePumpForNonMainUIThreads::DoRunLoop()
|
2014-09-12 18:49:38 +04:00
|
|
|
{
|
2016-03-30 22:20:20 +03:00
|
|
|
MOZ_RELEASE_ASSERT(!NS_IsMainThread(), "Use mozilla::ipc::MessagePump instead!");
|
|
|
|
|
2014-09-12 18:49:38 +04:00
|
|
|
// If this is a chromium thread and no nsThread is associated
|
|
|
|
// with it, this call will create a new nsThread.
|
2016-03-30 22:20:20 +03:00
|
|
|
nsIThread* thread = NS_GetCurrentThread();
|
|
|
|
MOZ_ASSERT(thread);
|
2014-09-12 18:49:38 +04:00
|
|
|
|
|
|
|
// Set the main thread observer so we can wake up when
|
|
|
|
// xpcom events need to get processed.
|
2016-03-30 22:20:20 +03:00
|
|
|
nsCOMPtr<nsIThreadInternal> ti(do_QueryInterface(thread));
|
2014-09-12 18:49:38 +04:00
|
|
|
MOZ_ASSERT(ti);
|
|
|
|
ti->SetObserver(this);
|
|
|
|
|
|
|
|
base::ScopedNSAutoreleasePool autoReleasePool;
|
|
|
|
for (;;) {
|
|
|
|
autoReleasePool.Recycle();
|
|
|
|
|
2016-03-30 22:20:20 +03:00
|
|
|
bool didWork = NS_ProcessNextEvent(thread, false);
|
2014-09-12 18:49:38 +04:00
|
|
|
|
|
|
|
didWork |= ProcessNextWindowsMessage();
|
|
|
|
CHECK_QUIT_STATE
|
|
|
|
|
|
|
|
didWork |= state_->delegate->DoWork();
|
|
|
|
CHECK_QUIT_STATE
|
|
|
|
|
|
|
|
didWork |= state_->delegate->DoDelayedWork(&delayed_work_time_);
|
|
|
|
if (didWork && delayed_work_time_.is_null()) {
|
|
|
|
KillTimer(message_hwnd_, reinterpret_cast<UINT_PTR>(this));
|
|
|
|
}
|
|
|
|
CHECK_QUIT_STATE
|
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-13 01:15:43 +03:00
|
|
|
DebugOnly<bool> didIdleWork = state_->delegate->DoIdleWork();
|
|
|
|
MOZ_ASSERT(!didIdleWork);
|
2014-09-12 18:49:38 +04:00
|
|
|
CHECK_QUIT_STATE
|
|
|
|
|
|
|
|
SetInWait();
|
2016-03-30 22:20:20 +03:00
|
|
|
bool hasWork = NS_HasPendingEvents(thread);
|
2014-09-12 18:49:38 +04:00
|
|
|
if (didWork || hasWork) {
|
|
|
|
ClearInWait();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
WaitForWork(); // Calls MsgWaitForMultipleObjectsEx(QS_ALLINPUT)
|
|
|
|
ClearInWait();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearInWait();
|
|
|
|
|
|
|
|
ti->SetObserver(nullptr);
|
|
|
|
}
|
2016-01-26 19:30:03 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2017-07-07 02:05:28 +03:00
|
|
|
MessagePumpForNonMainUIThreads::OnDispatchedEvent()
|
2016-01-26 19:30:03 +03:00
|
|
|
{
|
|
|
|
// If our thread is sleeping in DoRunLoop's call to WaitForWork() and an
|
|
|
|
// event posts to the nsIThread event queue - break our thread out of
|
|
|
|
// chromium's WaitForWork.
|
|
|
|
if (GetInWait()) {
|
|
|
|
ScheduleWork();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MessagePumpForNonMainUIThreads::OnProcessNextEvent(nsIThreadInternal *thread,
|
|
|
|
bool mayWait)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MessagePumpForNonMainUIThreads::AfterProcessNextEvent(nsIThreadInternal *thread,
|
|
|
|
bool eventWasProcessed)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-09-12 18:49:38 +04:00
|
|
|
|
|
|
|
#endif // XP_WIN
|
2016-12-01 01:56:53 +03:00
|
|
|
|
|
|
|
#if defined(MOZ_WIDGET_ANDROID)
|
|
|
|
void
|
|
|
|
MessagePumpForAndroidUI::Run(Delegate* delegate)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("MessagePumpForAndroidUI should never be Run.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForAndroidUI::Quit()
|
|
|
|
{
|
|
|
|
MOZ_CRASH("MessagePumpForAndroidUI should never be Quit.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForAndroidUI::ScheduleWork()
|
|
|
|
{
|
|
|
|
MOZ_CRASH("MessagePumpForAndroidUI should never ScheduleWork");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePumpForAndroidUI::ScheduleDelayedWork(const TimeTicks& delayed_work_time)
|
|
|
|
{
|
|
|
|
MOZ_CRASH("MessagePumpForAndroidUI should never ScheduleDelayedWork");
|
|
|
|
}
|
|
|
|
#endif // defined(MOZ_WIDGET_ANDROID)
|