Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsStreamListenerTee.h" |
michael@0 | 6 | #include "nsProxyRelease.h" |
michael@0 | 7 | |
michael@0 | 8 | NS_IMPL_ISUPPORTS(nsStreamListenerTee, |
michael@0 | 9 | nsIStreamListener, |
michael@0 | 10 | nsIRequestObserver, |
michael@0 | 11 | nsIStreamListenerTee, |
michael@0 | 12 | nsIThreadRetargetableStreamListener) |
michael@0 | 13 | |
michael@0 | 14 | NS_IMETHODIMP |
michael@0 | 15 | nsStreamListenerTee::OnStartRequest(nsIRequest *request, |
michael@0 | 16 | nsISupports *context) |
michael@0 | 17 | { |
michael@0 | 18 | NS_ENSURE_TRUE(mListener, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 19 | nsresult rv1 = mListener->OnStartRequest(request, context); |
michael@0 | 20 | nsresult rv2 = NS_OK; |
michael@0 | 21 | if (mObserver) |
michael@0 | 22 | rv2 = mObserver->OnStartRequest(request, context); |
michael@0 | 23 | |
michael@0 | 24 | // Preserve NS_SUCCESS_XXX in rv1 in case mObserver didn't throw |
michael@0 | 25 | return (NS_FAILED(rv2) && NS_SUCCEEDED(rv1)) ? rv2 : rv1; |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | NS_IMETHODIMP |
michael@0 | 29 | nsStreamListenerTee::OnStopRequest(nsIRequest *request, |
michael@0 | 30 | nsISupports *context, |
michael@0 | 31 | nsresult status) |
michael@0 | 32 | { |
michael@0 | 33 | NS_ENSURE_TRUE(mListener, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 34 | // it is critical that we close out the input stream tee |
michael@0 | 35 | if (mInputTee) { |
michael@0 | 36 | mInputTee->SetSink(nullptr); |
michael@0 | 37 | mInputTee = 0; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | // release sink on the same thread where the data was written (bug 716293) |
michael@0 | 41 | if (mEventTarget) { |
michael@0 | 42 | nsIOutputStream *sink = nullptr; |
michael@0 | 43 | mSink.swap(sink); |
michael@0 | 44 | if (NS_FAILED(NS_ProxyRelease(mEventTarget, sink))) { |
michael@0 | 45 | NS_WARNING("Releasing sink on the current thread!"); |
michael@0 | 46 | NS_RELEASE(sink); |
michael@0 | 47 | } |
michael@0 | 48 | } |
michael@0 | 49 | else { |
michael@0 | 50 | mSink = 0; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | nsresult rv = mListener->OnStopRequest(request, context, status); |
michael@0 | 54 | if (mObserver) |
michael@0 | 55 | mObserver->OnStopRequest(request, context, status); |
michael@0 | 56 | mObserver = 0; |
michael@0 | 57 | return rv; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | NS_IMETHODIMP |
michael@0 | 61 | nsStreamListenerTee::OnDataAvailable(nsIRequest *request, |
michael@0 | 62 | nsISupports *context, |
michael@0 | 63 | nsIInputStream *input, |
michael@0 | 64 | uint64_t offset, |
michael@0 | 65 | uint32_t count) |
michael@0 | 66 | { |
michael@0 | 67 | NS_ENSURE_TRUE(mListener, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 68 | NS_ENSURE_TRUE(mSink, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 69 | |
michael@0 | 70 | nsCOMPtr<nsIInputStream> tee; |
michael@0 | 71 | nsresult rv; |
michael@0 | 72 | |
michael@0 | 73 | if (!mInputTee) { |
michael@0 | 74 | if (mEventTarget) |
michael@0 | 75 | rv = NS_NewInputStreamTeeAsync(getter_AddRefs(tee), input, |
michael@0 | 76 | mSink, mEventTarget); |
michael@0 | 77 | else |
michael@0 | 78 | rv = NS_NewInputStreamTee(getter_AddRefs(tee), input, mSink); |
michael@0 | 79 | if (NS_FAILED(rv)) return rv; |
michael@0 | 80 | |
michael@0 | 81 | mInputTee = do_QueryInterface(tee, &rv); |
michael@0 | 82 | if (NS_FAILED(rv)) return rv; |
michael@0 | 83 | } |
michael@0 | 84 | else { |
michael@0 | 85 | // re-initialize the input tee since the input stream may have changed. |
michael@0 | 86 | rv = mInputTee->SetSource(input); |
michael@0 | 87 | if (NS_FAILED(rv)) return rv; |
michael@0 | 88 | |
michael@0 | 89 | tee = do_QueryInterface(mInputTee, &rv); |
michael@0 | 90 | if (NS_FAILED(rv)) return rv; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | return mListener->OnDataAvailable(request, context, tee, offset, count); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | NS_IMETHODIMP |
michael@0 | 97 | nsStreamListenerTee::CheckListenerChain() |
michael@0 | 98 | { |
michael@0 | 99 | NS_ASSERTION(NS_IsMainThread(), "Should be on main thread!"); |
michael@0 | 100 | nsresult rv = NS_OK; |
michael@0 | 101 | nsCOMPtr<nsIThreadRetargetableStreamListener> retargetableListener = |
michael@0 | 102 | do_QueryInterface(mListener, &rv); |
michael@0 | 103 | if (retargetableListener) { |
michael@0 | 104 | rv = retargetableListener->CheckListenerChain(); |
michael@0 | 105 | } |
michael@0 | 106 | if (NS_FAILED(rv)) { |
michael@0 | 107 | return rv; |
michael@0 | 108 | } |
michael@0 | 109 | if (!mObserver) { |
michael@0 | 110 | return rv; |
michael@0 | 111 | } |
michael@0 | 112 | retargetableListener = do_QueryInterface(mObserver, &rv); |
michael@0 | 113 | if (retargetableListener) { |
michael@0 | 114 | rv = retargetableListener->CheckListenerChain(); |
michael@0 | 115 | } |
michael@0 | 116 | return rv; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | NS_IMETHODIMP |
michael@0 | 120 | nsStreamListenerTee::Init(nsIStreamListener *listener, |
michael@0 | 121 | nsIOutputStream *sink, |
michael@0 | 122 | nsIRequestObserver *requestObserver) |
michael@0 | 123 | { |
michael@0 | 124 | NS_ENSURE_ARG_POINTER(listener); |
michael@0 | 125 | NS_ENSURE_ARG_POINTER(sink); |
michael@0 | 126 | mListener = listener; |
michael@0 | 127 | mSink = sink; |
michael@0 | 128 | mObserver = requestObserver; |
michael@0 | 129 | return NS_OK; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | NS_IMETHODIMP |
michael@0 | 133 | nsStreamListenerTee::InitAsync(nsIStreamListener *listener, |
michael@0 | 134 | nsIEventTarget *eventTarget, |
michael@0 | 135 | nsIOutputStream *sink, |
michael@0 | 136 | nsIRequestObserver *requestObserver) |
michael@0 | 137 | { |
michael@0 | 138 | NS_ENSURE_ARG_POINTER(eventTarget); |
michael@0 | 139 | mEventTarget = eventTarget; |
michael@0 | 140 | return Init(listener, sink, requestObserver); |
michael@0 | 141 | } |