Bug 856973 - Outparamdel NS_New{In,Out}putStreamReadyEvent; r=ehsan
authorMs2ger <ms2ger@gmail.com>
Thu, 04 Apr 2013 09:05:18 +0200
changeset 138525 677e358170320be6f7330b0f731fd55b3070935e
parent 138524 069d1883bbd58a2b0b5c225fe38348da3dd3a8d7
child 138526 94c8840a989c8fbcbb7234141557e971ada483d6
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs856973
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 856973 - Outparamdel NS_New{In,Out}putStreamReadyEvent; r=ehsan
content/html/content/src/HTMLCanvasElement.cpp
image/encoders/bmp/nsBMPEncoder.cpp
image/encoders/ico/nsICOEncoder.cpp
image/encoders/jpeg/nsJPEGEncoder.cpp
image/encoders/png/nsPNGEncoder.cpp
netwerk/base/src/nsBaseContentStream.cpp
netwerk/base/src/nsSocketTransport2.cpp
netwerk/protocol/device/AndroidCaptureProvider.cpp
netwerk/protocol/device/GonkCaptureProvider.cpp
xpcom/io/nsPipe3.cpp
xpcom/io/nsStreamUtils.cpp
xpcom/io/nsStreamUtils.h
--- a/content/html/content/src/HTMLCanvasElement.cpp
+++ b/content/html/content/src/HTMLCanvasElement.cpp
@@ -390,19 +390,18 @@ HTMLCanvasElement::MozFetchAsStream(nsII
 
   nsCOMPtr<nsIAsyncInputStream> asyncData = do_QueryInterface(inputData, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIThread> mainThread;
   rv = NS_GetMainThread(getter_AddRefs(mainThread));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIInputStreamCallback> asyncCallback;
-  rv = NS_NewInputStreamReadyEvent(getter_AddRefs(asyncCallback), aCallback, mainThread);
-  NS_ENSURE_SUCCESS(rv, rv);
+  nsCOMPtr<nsIInputStreamCallback> asyncCallback =
+    NS_NewInputStreamReadyEvent(aCallback, mainThread);
 
   return asyncCallback->OnInputStreamReady(asyncData);
 }
 
 NS_IMETHODIMP
 HTMLCanvasElement::SetMozPrintCallback(nsIPrintCallback *aCallback)
 {
   mPrintCallback = aCallback;
--- a/image/encoders/bmp/nsBMPEncoder.cpp
+++ b/image/encoders/bmp/nsBMPEncoder.cpp
@@ -444,19 +444,17 @@ nsBMPEncoder::ConvertHostARGBRow(const u
 void
 nsBMPEncoder::NotifyListener()
 {
   if (mCallback &&
       (GetCurrentImageBufferOffset() - mImageBufferReadPoint >= 
        mNotifyThreshold || mFinished)) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback),
-                                  mCallback,
-                                  mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback");
     // Null the callback first because OnInputStreamReady could
     // reenter AsyncWait
     mCallback = nullptr;
--- a/image/encoders/ico/nsICOEncoder.cpp
+++ b/image/encoders/ico/nsICOEncoder.cpp
@@ -425,19 +425,17 @@ NS_IMETHODIMP nsICOEncoder::CloseWithSta
 void
 nsICOEncoder::NotifyListener()
 {
   if (mCallback &&
       (GetCurrentImageBufferOffset() - mImageBufferReadPoint >= mNotifyThreshold ||
        mFinished)) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback),
-                                  mCallback,
-                                  mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback");
     // Null the callback first because OnInputStreamReady could reenter
     // AsyncWait
     mCallback = nullptr;
--- a/image/encoders/jpeg/nsJPEGEncoder.cpp
+++ b/image/encoders/jpeg/nsJPEGEncoder.cpp
@@ -483,19 +483,17 @@ nsJPEGEncoder::NotifyListener()
   // image).
   ReentrantMonitorAutoEnter autoEnter(mReentrantMonitor);
 
   if (mCallback &&
       (mImageBufferUsed - mImageBufferReadPoint >= mNotifyThreshold ||
        mFinished)) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback),
-                                  mCallback,
-                                  mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback");
     // Null the callback first because OnInputStreamReady could reenter
     // AsyncWait
     mCallback = nullptr;
--- a/image/encoders/png/nsPNGEncoder.cpp
+++ b/image/encoders/png/nsPNGEncoder.cpp
@@ -704,19 +704,17 @@ nsPNGEncoder::NotifyListener()
   // image).
   ReentrantMonitorAutoEnter autoEnter(mReentrantMonitor);
 
   if (mCallback &&
       (mImageBufferUsed - mImageBufferReadPoint >= mNotifyThreshold ||
        mFinished)) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback),
-                                  mCallback,
-                                  mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback");
     // Null the callback first because OnInputStreamReady could reenter
     // AsyncWait
     mCallback = nullptr;
--- a/netwerk/base/src/nsBaseContentStream.cpp
+++ b/netwerk/base/src/nsBaseContentStream.cpp
@@ -14,20 +14,17 @@ nsBaseContentStream::DispatchCallback(bo
   if (!mCallback)
     return;
 
   // It's important to clear mCallback and mCallbackTarget up-front because the
   // OnInputStreamReady implementation may call our AsyncWait method.
 
   nsCOMPtr<nsIInputStreamCallback> callback;
   if (async) {
-    NS_NewInputStreamReadyEvent(getter_AddRefs(callback), mCallback,
-                                mCallbackTarget);
-    if (!callback)
-      return;  // out of memory!
+    callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     mCallback = nullptr;
   } else {
     callback.swap(mCallback);
   }
   mCallbackTarget = nullptr;
 
   callback->OnInputStreamReady(this);
 }
--- a/netwerk/base/src/nsSocketTransport2.cpp
+++ b/netwerk/base/src/nsSocketTransport2.cpp
@@ -415,24 +415,17 @@ nsSocketInputStream::AsyncWait(nsIInputS
     nsCOMPtr<nsIInputStreamCallback> directCallback;
     {
         MutexAutoLock lock(mTransport->mLock);
 
         if (callback && target) {
             //
             // build event proxy
             //
-            // failure to create an event proxy (most likely out of memory)
-            // shouldn't alter the state of the transport.
-            //
-            nsCOMPtr<nsIInputStreamCallback> temp;
-            nsresult rv = NS_NewInputStreamReadyEvent(getter_AddRefs(temp),
-                                                      callback, target);
-            if (NS_FAILED(rv)) return rv;
-            mCallback = temp;
+            mCallback = NS_NewInputStreamReadyEvent(callback, target);
         }
         else
             mCallback = callback;
 
         if (NS_FAILED(mCondition))
             directCallback.swap(mCallback);
         else
             mCallbackFlags = flags;
@@ -652,24 +645,17 @@ nsSocketOutputStream::AsyncWait(nsIOutpu
 
     {
         MutexAutoLock lock(mTransport->mLock);
 
         if (callback && target) {
             //
             // build event proxy
             //
-            // failure to create an event proxy (most likely out of memory)
-            // shouldn't alter the state of the transport.
-            //
-            nsCOMPtr<nsIOutputStreamCallback> temp;
-            nsresult rv = NS_NewOutputStreamReadyEvent(getter_AddRefs(temp),
-                                                       callback, target);
-            if (NS_FAILED(rv)) return rv;
-            mCallback = temp;
+            mCallback = NS_NewOutputStreamReadyEvent(callback, target);
         }
         else
             mCallback = callback;
 
         mCallbackFlags = flags;
     }
     mTransport->OnOutputPending();
     return NS_OK;
--- a/netwerk/protocol/device/AndroidCaptureProvider.cpp
+++ b/netwerk/protocol/device/AndroidCaptureProvider.cpp
@@ -208,17 +208,17 @@ void AndroidCameraInputStream::doClose()
 
 
 void AndroidCameraInputStream::NotifyListeners() {
   mozilla::ReentrantMonitorAutoEnter autoMonitor(mMonitor);
   
   if (mCallback && (mAvailable > sizeof(RawVideoHeader))) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback), mCallback, mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback!");
 
     // Null the callback first because OnInputStreamReady may reenter AsyncWait
     mCallback = nullptr;
--- a/netwerk/protocol/device/GonkCaptureProvider.cpp
+++ b/netwerk/protocol/device/GonkCaptureProvider.cpp
@@ -500,17 +500,17 @@ void GonkCameraInputStream::doClose() {
 
 
 void GonkCameraInputStream::NotifyListeners() {
   ReentrantMonitorAutoEnter enter(mMonitor);
   
   if (mCallback && (mAvailable > sizeof(RawVideoHeader))) {
     nsCOMPtr<nsIInputStreamCallback> callback;
     if (mCallbackTarget) {
-      NS_NewInputStreamReadyEvent(getter_AddRefs(callback), mCallback, mCallbackTarget);
+      callback = NS_NewInputStreamReadyEvent(mCallback, mCallbackTarget);
     } else {
       callback = mCallback;
     }
 
     NS_ASSERTION(callback, "Shouldn't fail to make the callback!");
 
     // Null the callback first because OnInputStreamReady may reenter AsyncWait
     mCallback = nullptr;
--- a/xpcom/io/nsPipe3.cpp
+++ b/xpcom/io/nsPipe3.cpp
@@ -826,19 +826,17 @@ nsPipeInputStream::AsyncWait(nsIInputStr
         mCallback = 0;
         mCallbackFlags = 0;
 
         if (!callback)
             return NS_OK;
 
         nsCOMPtr<nsIInputStreamCallback> proxy;
         if (target) {
-            nsresult rv = NS_NewInputStreamReadyEvent(getter_AddRefs(proxy),
-                                                      callback, target);
-            if (NS_FAILED(rv)) return rv;
+            proxy = NS_NewInputStreamReadyEvent(callback, target);
             callback = proxy;
         }
 
         if (NS_FAILED(mPipe->mStatus) ||
                 (mAvailable && !(flags & WAIT_CLOSURE_ONLY))) {
             // stream is already closed or readable; post event.
             pipeEvents.NotifyInputReady(this, callback);
         }
@@ -1207,19 +1205,17 @@ nsPipeOutputStream::AsyncWait(nsIOutputS
         mCallback = 0;
         mCallbackFlags = 0;
 
         if (!callback)
             return NS_OK;
 
         nsCOMPtr<nsIOutputStreamCallback> proxy;
         if (target) {
-            nsresult rv = NS_NewOutputStreamReadyEvent(getter_AddRefs(proxy),
-                                                       callback, target);
-            if (NS_FAILED(rv)) return rv;
+            proxy = NS_NewOutputStreamReadyEvent(callback, target);
             callback = proxy;
         }
 
         if (NS_FAILED(mPipe->mStatus) ||
                 (mWritable && !(flags & WAIT_CLOSURE_ONLY))) {
             // stream is already closed or writable; post event.
             pipeEvents.NotifyOutputReady(this, callback);
         }
--- a/xpcom/io/nsStreamUtils.cpp
+++ b/xpcom/io/nsStreamUtils.cpp
@@ -1,16 +1,17 @@
 /* vim:set ts=4 sw=4 sts=4 et cin: */
 /* 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 "mozilla/Mutex.h"
 #include "mozilla/Attributes.h"
 #include "nsStreamUtils.h"
+#include "nsAutoPtr.h"
 #include "nsCOMPtr.h"
 #include "nsIPipe.h"
 #include "nsIEventTarget.h"
 #include "nsIRunnable.h"
 #include "nsISafeOutputStream.h"
 #include "nsString.h"
 
 using namespace mozilla;
@@ -40,20 +41,19 @@ private:
         // release mCallback on the correct thread.  if mTarget lives on the
         // calling thread, then we are ok.  otherwise, we have to try to 
         // proxy the Release over the right thread.  if that thread is dead,
         // then there's nothing we can do... better to leak than crash.
         //
         bool val;
         nsresult rv = mTarget->IsOnCurrentThread(&val);
         if (NS_FAILED(rv) || !val) {
-            nsCOMPtr<nsIInputStreamCallback> event;
-            NS_NewInputStreamReadyEvent(getter_AddRefs(event), mCallback,
-                                        mTarget);
-            mCallback = 0;
+            nsCOMPtr<nsIInputStreamCallback> event =
+                NS_NewInputStreamReadyEvent(mCallback, mTarget);
+            mCallback = nullptr;
             if (event) {
                 rv = event->OnInputStreamReady(nullptr);
                 if (NS_FAILED(rv)) {
                     NS_NOTREACHED("leaking stream event");
                     nsISupports *sup = event;
                     NS_ADDREF(sup);
                 }
             }
@@ -119,20 +119,19 @@ private:
         // release mCallback on the correct thread.  if mTarget lives on the
         // calling thread, then we are ok.  otherwise, we have to try to 
         // proxy the Release over the right thread.  if that thread is dead,
         // then there's nothing we can do... better to leak than crash.
         //
         bool val;
         nsresult rv = mTarget->IsOnCurrentThread(&val);
         if (NS_FAILED(rv) || !val) {
-            nsCOMPtr<nsIOutputStreamCallback> event;
-            NS_NewOutputStreamReadyEvent(getter_AddRefs(event), mCallback,
-                                         mTarget);
-            mCallback = 0;
+            nsCOMPtr<nsIOutputStreamCallback> event =
+                NS_NewOutputStreamReadyEvent(mCallback, mTarget);
+            mCallback = nullptr;
             if (event) {
                 rv = event->OnOutputStreamReady(nullptr);
                 if (NS_FAILED(rv)) {
                     NS_NOTREACHED("leaking stream event");
                     nsISupports *sup = event;
                     NS_ADDREF(sup);
                 }
             }
@@ -170,42 +169,36 @@ private:
     nsCOMPtr<nsIEventTarget>          mTarget;
 };
 
 NS_IMPL_THREADSAFE_ISUPPORTS2(nsOutputStreamReadyEvent, nsIRunnable,
                               nsIOutputStreamCallback)
 
 //-----------------------------------------------------------------------------
 
-nsresult
-NS_NewInputStreamReadyEvent(nsIInputStreamCallback **event,
-                            nsIInputStreamCallback *callback,
+already_AddRefed<nsIInputStreamCallback>
+NS_NewInputStreamReadyEvent(nsIInputStreamCallback *callback,
                             nsIEventTarget *target)
 {
     NS_ASSERTION(callback, "null callback");
     NS_ASSERTION(target, "null target");
-    nsInputStreamReadyEvent *ev = new nsInputStreamReadyEvent(callback, target);
-    if (!ev)
-        return NS_ERROR_OUT_OF_MEMORY;
-    NS_ADDREF(*event = ev);
-    return NS_OK;
+    nsRefPtr<nsInputStreamReadyEvent> ev =
+        new nsInputStreamReadyEvent(callback, target);
+    return ev.forget();
 }
 
-nsresult
-NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback **event,
-                             nsIOutputStreamCallback *callback,
+already_AddRefed<nsIOutputStreamCallback>
+NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback *callback,
                              nsIEventTarget *target)
 {
     NS_ASSERTION(callback, "null callback");
     NS_ASSERTION(target, "null target");
-    nsOutputStreamReadyEvent *ev = new nsOutputStreamReadyEvent(callback, target);
-    if (!ev)
-        return NS_ERROR_OUT_OF_MEMORY;
-    NS_ADDREF(*event = ev);
-    return NS_OK;
+    nsRefPtr<nsOutputStreamReadyEvent> ev =
+        new nsOutputStreamReadyEvent(callback, target);
+    return ev.forget();
 }
 
 //-----------------------------------------------------------------------------
 // NS_AsyncCopy implementation
 
 // abstract stream copier...
 class nsAStreamCopier : public nsIInputStreamCallback
                       , public nsIOutputStreamCallback
--- a/xpcom/io/nsStreamUtils.h
+++ b/xpcom/io/nsStreamUtils.h
@@ -1,15 +1,16 @@
 /* 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/. */
 
 #ifndef nsStreamUtils_h__
 #define nsStreamUtils_h__
 
+#include "nsCOMPtr.h"
 #include "nsStringFwd.h"
 #include "nsIInputStream.h"
 
 class nsIOutputStream;
 class nsIInputStreamCallback;
 class nsIOutputStreamCallback;
 class nsIEventTarget;
 
@@ -18,34 +19,32 @@ class nsIEventTarget;
  * proxy object's OnInputStreamReady function may only be called once!  The
  * proxy object ensures that the real notify object will be free'd on the
  * thread corresponding to the given event target regardless of what thread
  * the proxy object is destroyed on.
  *
  * This function is designed to be used to implement AsyncWait when the
  * aTarget parameter is non-null.
  */
-extern nsresult
-NS_NewInputStreamReadyEvent(nsIInputStreamCallback **aEvent,
-                            nsIInputStreamCallback  *aNotify,
+extern already_AddRefed<nsIInputStreamCallback>
+NS_NewInputStreamReadyEvent(nsIInputStreamCallback  *aNotify,
                             nsIEventTarget          *aTarget);
 
 /**
  * A "one-shot" proxy of the OnOutputStreamReady callback.  The resulting
  * proxy object's OnOutputStreamReady function may only be called once!  The
  * proxy object ensures that the real notify object will be free'd on the
  * thread corresponding to the given event target regardless of what thread 
  * the proxy object is destroyed on.
  *
  * This function is designed to be used to implement AsyncWait when the
  * aTarget parameter is non-null.
  */
-extern nsresult
-NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback **aEvent,
-                             nsIOutputStreamCallback  *aNotify,
+extern already_AddRefed<nsIOutputStreamCallback>
+NS_NewOutputStreamReadyEvent(nsIOutputStreamCallback  *aNotify,
                              nsIEventTarget           *aTarget);
 
 /* ------------------------------------------------------------------------- */
 
 enum nsAsyncCopyMode {
     NS_ASYNCCOPY_VIA_READSEGMENTS,
     NS_ASYNCCOPY_VIA_WRITESEGMENTS
 };