From: Mike Hommey <glandium@debian.org>
Date: Wed, 29 Oct 2008 21:18:39 +0100
Subject: Better fix to avoid crashes with NS_HasPendingEvents

---
 xpcom/glue/nsThreadUtils.cpp |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/xpcom/glue/nsThreadUtils.cpp b/xpcom/glue/nsThreadUtils.cpp
index 9619be6..4b1ac7c 100644
--- a/xpcom/glue/nsThreadUtils.cpp
+++ b/xpcom/glue/nsThreadUtils.cpp
@@ -207,24 +207,27 @@ NS_ProcessPendingEvents(nsIThread *thread, PRIntervalTime timeout)
 }
 #endif // XPCOM_GLUE_AVOID_NSPR
 
+inline PRBool
+hasPendingEvents(nsIThread *thread)
+{
+  PRBool val;
+  return NS_SUCCEEDED(thread->HasPendingEvents(&val)) && val;
+}
+
 PRBool
 NS_HasPendingEvents(nsIThread *thread)
 {
-#ifdef MOZILLA_INTERNAL_API
   if (!thread) {
+#ifndef MOZILLA_INTERNAL_API
+    nsCOMPtr<nsIThread> current;
+    NS_GetCurrentThread(getter_AddRefs(current));
+    return hasPendingEvents(current);
+#else
     thread = NS_GetCurrentThread();
     NS_ENSURE_TRUE(thread, PR_FALSE);
-  }
-#else
-  nsCOMPtr<nsIThread> current;
-  if (!thread) {
-    NS_GetCurrentThread(getter_AddRefs(current));
-    NS_ENSURE_TRUE(current, PR_FALSE);
-    thread = current.get();
-  }
 #endif
-  PRBool val;
-  return NS_SUCCEEDED(thread->HasPendingEvents(&val)) && val;
+  }
+  return hasPendingEvents(thread);
 }
 
 PRBool
