2014-06-30 19:39:45 +04: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-10-25 19:36:24 +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/. */
|
|
|
|
|
|
|
|
#include "nsMessageLoop.h"
|
|
|
|
#include "mozilla/WeakPtr.h"
|
|
|
|
#include "base/message_loop.h"
|
|
|
|
#include "base/task.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2012-10-25 19:36:24 +04:00
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Task runs its nsIRunnable when Run() is called, or after
|
|
|
|
* aEnsureRunsAfterMS milliseconds have elapsed since the object was
|
|
|
|
* constructed.
|
|
|
|
*
|
|
|
|
* Note that the MessageLoop owns this object and will delete it after it calls
|
|
|
|
* Run(). Tread lightly.
|
|
|
|
*/
|
|
|
|
class MessageLoopIdleTask
|
2016-04-28 03:06:05 +03:00
|
|
|
: public Runnable
|
2012-10-25 19:36:24 +04:00
|
|
|
, public SupportsWeakPtr<MessageLoopIdleTask>
|
|
|
|
{
|
|
|
|
public:
|
2015-04-24 19:43:01 +03:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MessageLoopIdleTask)
|
2012-10-25 19:36:24 +04:00
|
|
|
MessageLoopIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS);
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2012-10-25 19:36:24 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult Init(uint32_t aEnsureRunsAfterMS);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> mTask;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2014-07-01 02:11:53 +04:00
|
|
|
|
|
|
|
virtual ~MessageLoopIdleTask() {}
|
2012-10-25 19:36:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This timer callback calls MessageLoopIdleTask::Run() when its timer fires.
|
|
|
|
* (The timer can't call back into MessageLoopIdleTask directly since that's
|
|
|
|
* not a refcounted object; it's owned by the MessageLoop.)
|
|
|
|
*
|
|
|
|
* We keep a weak reference to the MessageLoopIdleTask, although a raw pointer
|
|
|
|
* should in theory suffice: When the MessageLoopIdleTask runs (right before
|
|
|
|
* the MessageLoop deletes it), it cancels its timer. But the weak pointer
|
|
|
|
* saves us from worrying about an edge case somehow messing us up here.
|
|
|
|
*/
|
|
|
|
class MessageLoopTimerCallback
|
2017-07-26 21:18:20 +03:00
|
|
|
: public nsITimerCallback, public nsINamed
|
2012-10-25 19:36:24 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-08-05 17:36:32 +04:00
|
|
|
explicit MessageLoopTimerCallback(MessageLoopIdleTask* aTask);
|
2012-10-25 19:36:24 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("MessageLoopTimerCallback");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-25 19:36:24 +04:00
|
|
|
private:
|
|
|
|
WeakPtr<MessageLoopIdleTask> mTask;
|
2014-07-01 02:11:53 +04:00
|
|
|
|
|
|
|
virtual ~MessageLoopTimerCallback() {}
|
2012-10-25 19:36:24 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
MessageLoopIdleTask::MessageLoopIdleTask(nsIRunnable* aTask,
|
|
|
|
uint32_t aEnsureRunsAfterMS)
|
2017-06-12 22:34:10 +03:00
|
|
|
: mozilla::Runnable("MessageLoopIdleTask")
|
|
|
|
, mTask(aTask)
|
2012-10-25 19:36:24 +04:00
|
|
|
{
|
|
|
|
// Init() really shouldn't fail, but if it does, we schedule our runnable
|
|
|
|
// immediately, because it's more important to guarantee that we run the task
|
|
|
|
// eventually than it is to run the task when we're idle.
|
|
|
|
nsresult rv = Init(aEnsureRunsAfterMS);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Running idle task early because we couldn't initialize our timer.");
|
|
|
|
NS_DispatchToCurrentThread(mTask);
|
|
|
|
|
|
|
|
mTask = nullptr;
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MessageLoopIdleTask::Init(uint32_t aEnsureRunsAfterMS)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageLoopTimerCallback> callback =
|
2012-10-25 19:36:24 +04:00
|
|
|
new MessageLoopTimerCallback(this);
|
2017-10-16 09:11:22 +03:00
|
|
|
return NS_NewTimerWithCallback(getter_AddRefs(mTimer),
|
|
|
|
callback, aEnsureRunsAfterMS,
|
|
|
|
nsITimer::TYPE_ONE_SHOT);
|
2012-10-25 19:36:24 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 03:06:05 +03:00
|
|
|
NS_IMETHODIMP
|
2012-10-25 19:36:24 +04:00
|
|
|
MessageLoopIdleTask::Run()
|
|
|
|
{
|
|
|
|
// Null out our pointers because if Run() was called by the timer, this
|
|
|
|
// object will be kept alive by the MessageLoop until the MessageLoop calls
|
|
|
|
// Run().
|
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTask) {
|
|
|
|
mTask->Run();
|
|
|
|
mTask = nullptr;
|
|
|
|
}
|
2016-04-28 03:06:05 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2012-10-25 19:36:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageLoopTimerCallback::MessageLoopTimerCallback(MessageLoopIdleTask* aTask)
|
2014-07-30 23:52:05 +04:00
|
|
|
: mTask(aTask)
|
2014-05-13 21:41:38 +04:00
|
|
|
{
|
|
|
|
}
|
2012-10-25 19:36:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MessageLoopTimerCallback::Notify(nsITimer* aTimer)
|
|
|
|
{
|
|
|
|
// We don't expect to hit the case when the timer fires but mTask has been
|
|
|
|
// deleted, because mTask should cancel the timer before the mTask is
|
|
|
|
// deleted. But you never know...
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(mTask, "This timer shouldn't have fired.");
|
2012-10-25 19:36:24 +04:00
|
|
|
|
|
|
|
if (mTask) {
|
|
|
|
mTask->Run();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS(MessageLoopTimerCallback, nsITimerCallback, nsINamed)
|
2012-10-25 19:36:24 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-10-25 19:36:24 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMessageLoop, nsIMessageLoop)
|
2012-10-25 19:36:24 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMessageLoop::PostIdleTask(nsIRunnable* aTask, uint32_t aEnsureRunsAfterMS)
|
|
|
|
{
|
|
|
|
// The message loop owns MessageLoopIdleTask and deletes it after calling
|
|
|
|
// Run(). Be careful...
|
2016-04-28 03:06:05 +03:00
|
|
|
RefPtr<MessageLoopIdleTask> idle =
|
|
|
|
new MessageLoopIdleTask(aTask, aEnsureRunsAfterMS);
|
|
|
|
MessageLoop::current()->PostIdleTask(idle.forget());
|
|
|
|
|
2012-10-25 19:36:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsMessageLoopConstructor(nsISupports* aOuter,
|
|
|
|
const nsIID& aIID,
|
|
|
|
void** aInstancePtr)
|
|
|
|
{
|
2014-05-13 21:41:38 +04:00
|
|
|
if (NS_WARN_IF(aOuter)) {
|
2013-11-20 01:27:37 +04:00
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
2014-05-13 21:41:38 +04:00
|
|
|
}
|
2012-10-25 19:36:24 +04:00
|
|
|
nsISupports* messageLoop = new nsMessageLoop();
|
|
|
|
return messageLoop->QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|