Bug 1355161 - provide a scriptable equivalent of NS_DispatchToMainThread, r=froydnj.
--- a/xpcom/threads/nsIThreadManager.idl
+++ b/xpcom/threads/nsIThreadManager.idl
@@ -3,16 +3,17 @@
/* 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 "nsISupports.idl"
[ptr] native PRThread(PRThread);
+interface nsIRunnable;
interface nsIThread;
/**
* An interface for creating and locating nsIThread instances.
*/
[scriptable, uuid(1be89eca-e2f7-453b-8d38-c11ba247f6f3)]
interface nsIThreadManager : nsISupports
{
@@ -74,9 +75,19 @@ interface nsIThreadManager : nsISupports
*/
readonly attribute nsIThread currentThread;
/**
* This attribute is true if the calling thread is the main thread of the
* application process.
*/
readonly attribute boolean isMainThread;
+
+ /**
+ * This queues a runnable to the main thread. It's a shortcut for JS callers
+ * to be used instead of
+ * .mainThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
+ * or
+ * .currentThread.dispatch(runnable, Ci.nsIEventTarget.DISPATCH_NORMAL);
+ * C++ callers should instead use NS_DispatchToMainThread.
+ */
+ void dispatchToMainThread(in nsIRunnable event);
};
--- a/xpcom/threads/nsThreadManager.cpp
+++ b/xpcom/threads/nsThreadManager.cpp
@@ -343,8 +343,22 @@ nsThreadManager::GetIsMainThread(bool* a
}
uint32_t
nsThreadManager::GetHighestNumberOfThreads()
{
OffTheBooksMutexAutoLock lock(mLock);
return mHighestNumberOfThreads;
}
+
+NS_IMETHODIMP
+nsThreadManager::DispatchToMainThread(nsIRunnable *aEvent)
+{
+ // Note: C++ callers should instead use NS_DispatchToMainThread.
+ MOZ_ASSERT(NS_IsMainThread());
+
+ // Keep this functioning during Shutdown
+ if (NS_WARN_IF(!mMainThread)) {
+ return NS_ERROR_NOT_INITIALIZED;
+ }
+
+ return mMainThread->DispatchFromScript(aEvent, 0);
+}