Bug 1355161 - provide a scriptable equivalent of NS_DispatchToMainThread, r=froydnj.
authorFlorian Queze <florian@queze.net>
Fri, 14 Apr 2017 18:27:32 +0200
changeset 563291 d2a26a27c192d43ffec287c618a9926ba0a75d67
parent 563290 83e7c9bfbd76715d6e6a73f0ac45bb3cd4305d7e
child 563292 cd981920d0ef8adf66e504b718a4208dc03c7a4c
push id54258
push usercpeterson@mozilla.com
push dateSun, 16 Apr 2017 05:52:14 +0000
reviewersfroydnj
bugs1355161
milestone55.0a1
Bug 1355161 - provide a scriptable equivalent of NS_DispatchToMainThread, r=froydnj.
xpcom/threads/nsIThreadManager.idl
xpcom/threads/nsThreadManager.cpp
--- 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);
+}