Bug 1320753 - Add nsINamed and have Runnable implement it (r=ehsan)
authorBill McCloskey <billm@mozilla.com>
Tue, 22 Nov 2016 16:18:52 -0800
changeset 324955 97c6218a8654c2a97698ebb13300180ae925fe7f
parent 324954 7940f11b3b42c87281ea606df3aad3c2ebddce1c
child 324956 704915079f6b12990201dc378ee05c8861e98a7c
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewersehsan
bugs1320753
milestone53.0a1
Bug 1320753 - Add nsINamed and have Runnable implement it (r=ehsan) MozReview-Commit-ID: 6Vuw3aJ3860
toolkit/components/downloads/nsDownloadScanner.cpp
xpcom/glue/nsThreadUtils.cpp
xpcom/glue/nsThreadUtils.h
xpcom/threads/moz.build
xpcom/threads/nsINamed.idl
--- a/toolkit/components/downloads/nsDownloadScanner.cpp
+++ b/toolkit/components/downloads/nsDownloadScanner.cpp
@@ -509,17 +509,17 @@ nsDownloadScanner::Scan::DoScan()
 {
   CoInitialize(nullptr);
 
   if (DoScanAES()) {
     // We need to do a few more things on the main thread
     NS_DispatchToMainThread(this);
   } else {
     // We timed out, so just release
-    ReleaseDispatcher* releaser = new ReleaseDispatcher(this);
+    ReleaseDispatcher* releaser = new ReleaseDispatcher(ToSupports(this));
     if(releaser) {
       NS_ADDREF(releaser);
       NS_DispatchToMainThread(releaser);
     }
   }
 
   MOZ_SEH_TRY {
     CoUninitialize();
@@ -693,17 +693,17 @@ nsDownloadScannerWatchdog::WatchdogThrea
       waitTime = static_cast<DWORD>((expectedEndTime - now)/PR_USEC_PER_MSEC);
     }
     HANDLE hThread = waitHandles[2] = scan->GetWaitableThreadHandle();
 
     // Wait for the thread (obj 1) or quit event (obj 0)
     waitStatus = WaitForMultipleObjects(2, (waitHandles+1), FALSE, waitTime);
     CloseHandle(hThread);
 
-    ReleaseDispatcher* releaser = new ReleaseDispatcher(scan);
+    ReleaseDispatcher* releaser = new ReleaseDispatcher(ToSupports(scan));
     if(!releaser)
       continue;
     NS_ADDREF(releaser);
     // Got quit event or error
     if (waitStatus == WAIT_FAILED || waitStatus == WAIT_OBJECT_0) {
       NS_DispatchToMainThread(releaser);
       break;
     // Thread exited normally
--- a/xpcom/glue/nsThreadUtils.cpp
+++ b/xpcom/glue/nsThreadUtils.cpp
@@ -37,25 +37,45 @@ NS_IMPL_ISUPPORTS(IdlePeriod, nsIIdlePer
 
 NS_IMETHODIMP
 IdlePeriod::GetIdlePeriodHint(TimeStamp* aIdleDeadline)
 {
   *aIdleDeadline = TimeStamp();
   return NS_OK;
 }
 
-NS_IMPL_ISUPPORTS(Runnable, nsIRunnable)
+NS_IMPL_ISUPPORTS(Runnable, nsIRunnable, nsINamed)
 
 NS_IMETHODIMP
 Runnable::Run()
 {
   // Do nothing
   return NS_OK;
 }
 
+NS_IMETHODIMP
+Runnable::GetName(nsACString& aName)
+{
+#ifdef RELEASE_OR_BETA
+  aName.Truncate();
+#else
+  aName.AssignASCII(mName);
+#endif
+  return NS_OK;
+}
+
+nsresult
+Runnable::SetName(const char* aName)
+{
+#ifndef RELEASE_OR_BETA
+  mName = aName;
+#endif
+  return NS_OK;
+}
+
 NS_IMPL_ISUPPORTS_INHERITED(CancelableRunnable, Runnable,
                             nsICancelableRunnable)
 
 nsresult
 CancelableRunnable::Cancel()
 {
   // Do nothing
   return NS_OK;
--- a/xpcom/glue/nsThreadUtils.h
+++ b/xpcom/glue/nsThreadUtils.h
@@ -5,22 +5,23 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef nsThreadUtils_h__
 #define nsThreadUtils_h__
 
 #include "prthread.h"
 #include "prinrval.h"
 #include "MainThreadUtils.h"
-#include "nsIThreadManager.h"
-#include "nsIThread.h"
-#include "nsIRunnable.h"
 #include "nsICancelableRunnable.h"
 #include "nsIIdlePeriod.h"
 #include "nsIIncrementalRunnable.h"
+#include "nsINamed.h"
+#include "nsIRunnable.h"
+#include "nsIThreadManager.h"
+#include "nsIThread.h"
 #include "nsStringGlue.h"
 #include "nsCOMPtr.h"
 #include "nsAutoPtr.h"
 #include "mozilla/Atomics.h"
 #include "mozilla/IndexSequence.h"
 #include "mozilla/Likely.h"
 #include "mozilla/Move.h"
 #include "mozilla/TimeStamp.h"
@@ -245,30 +246,35 @@ protected:
   virtual ~IdlePeriod() {}
 private:
   IdlePeriod(const IdlePeriod&) = delete;
   IdlePeriod& operator=(const IdlePeriod&) = delete;
   IdlePeriod& operator=(const IdlePeriod&&) = delete;
 };
 
 // This class is designed to be subclassed.
-class Runnable : public nsIRunnable
+class Runnable : public nsIRunnable, public nsINamed
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
   NS_DECL_NSIRUNNABLE
+  NS_DECL_NSINAMED
 
   Runnable() {}
 
 protected:
   virtual ~Runnable() {}
 private:
   Runnable(const Runnable&) = delete;
   Runnable& operator=(const Runnable&) = delete;
   Runnable& operator=(const Runnable&&) = delete;
+
+#ifndef RELEASE_OR_BETA
+  const char* mName = nullptr;
+#endif
 };
 
 // This class is designed to be subclassed.
 class CancelableRunnable : public Runnable,
                            public nsICancelableRunnable
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
@@ -327,16 +333,22 @@ public:
 private:
   StoredFunction mFunction;
 };
 
 } // namespace detail
 
 } // namespace mozilla
 
+inline nsISupports*
+ToSupports(mozilla::Runnable *p)
+{
+  return static_cast<nsIRunnable*>(p);
+}
+
 template<typename Function>
 already_AddRefed<mozilla::Runnable>
 NS_NewRunnableFunction(Function&& aFunction)
 {
   return do_AddRef(new mozilla::detail::RunnableFunction
                    // Make sure we store a non-reference in nsRunnableFunction.
                    <typename mozilla::RemoveReference<Function>::Type>
                    // But still forward aFunction to move if possible.
--- a/xpcom/threads/moz.build
+++ b/xpcom/threads/moz.build
@@ -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/.
 
 XPIDL_SOURCES += [
     'nsIEnvironment.idl',
     'nsIEventTarget.idl',
     'nsIIdlePeriod.idl',
+    'nsINamed.idl',
     'nsIProcess.idl',
     'nsIRunnable.idl',
     'nsISupportsPriority.idl',
     'nsIThread.idl',
     'nsIThreadInternal.idl',
     'nsIThreadManager.idl',
     'nsIThreadPool.idl',
     'nsITimer.idl',
new file mode 100644
--- /dev/null
+++ b/xpcom/threads/nsINamed.idl
@@ -0,0 +1,24 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* 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"
+
+/**
+ * Represents an object with a name, such as a runnable or a timer.
+ */
+
+[scriptable, uuid(0c5fe7de-7e83-4d0d-a8a6-4a6518b9a7b3)]
+interface nsINamed : nsISupports
+{
+    /*
+     * A string describing the purpose of the runnable/timer/whatever. Useful
+     * for debugging. This attribute is read-only, but you can change it to a
+     * compile-time string literal with setName.
+     */
+    readonly attribute ACString name;
+
+    /* Note: The string you pass in should be a compile-time literal. */
+    [noscript] void setName(in string aName);
+};