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 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsPreloadedStream.h" |
michael@0 | 7 | #include "nsIRunnable.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsThreadUtils.h" |
michael@0 | 10 | #include <algorithm> |
michael@0 | 11 | |
michael@0 | 12 | namespace mozilla { |
michael@0 | 13 | namespace net { |
michael@0 | 14 | |
michael@0 | 15 | NS_IMPL_ISUPPORTS(nsPreloadedStream, |
michael@0 | 16 | nsIInputStream, |
michael@0 | 17 | nsIAsyncInputStream) |
michael@0 | 18 | |
michael@0 | 19 | nsPreloadedStream::nsPreloadedStream(nsIAsyncInputStream *aStream, |
michael@0 | 20 | const char *data, uint32_t datalen) |
michael@0 | 21 | : mStream(aStream), |
michael@0 | 22 | mOffset(0), |
michael@0 | 23 | mLen(datalen) |
michael@0 | 24 | { |
michael@0 | 25 | mBuf = (char *) moz_xmalloc(datalen); |
michael@0 | 26 | memcpy(mBuf, data, datalen); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | nsPreloadedStream::~nsPreloadedStream() |
michael@0 | 30 | { |
michael@0 | 31 | moz_free(mBuf); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | NS_IMETHODIMP |
michael@0 | 35 | nsPreloadedStream::Close() |
michael@0 | 36 | { |
michael@0 | 37 | mLen = 0; |
michael@0 | 38 | return mStream->Close(); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | |
michael@0 | 42 | NS_IMETHODIMP |
michael@0 | 43 | nsPreloadedStream::Available(uint64_t *_retval) |
michael@0 | 44 | { |
michael@0 | 45 | uint64_t avail = 0; |
michael@0 | 46 | |
michael@0 | 47 | nsresult rv = mStream->Available(&avail); |
michael@0 | 48 | if (NS_FAILED(rv)) |
michael@0 | 49 | return rv; |
michael@0 | 50 | *_retval = avail + mLen; |
michael@0 | 51 | return NS_OK; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | NS_IMETHODIMP |
michael@0 | 55 | nsPreloadedStream::Read(char *aBuf, uint32_t aCount, |
michael@0 | 56 | uint32_t *_retval) |
michael@0 | 57 | { |
michael@0 | 58 | if (!mLen) |
michael@0 | 59 | return mStream->Read(aBuf, aCount, _retval); |
michael@0 | 60 | |
michael@0 | 61 | uint32_t toRead = std::min(mLen, aCount); |
michael@0 | 62 | memcpy(aBuf, mBuf + mOffset, toRead); |
michael@0 | 63 | mOffset += toRead; |
michael@0 | 64 | mLen -= toRead; |
michael@0 | 65 | *_retval = toRead; |
michael@0 | 66 | return NS_OK; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | NS_IMETHODIMP |
michael@0 | 70 | nsPreloadedStream::ReadSegments(nsWriteSegmentFun aWriter, |
michael@0 | 71 | void *aClosure, uint32_t aCount, |
michael@0 | 72 | uint32_t *result) |
michael@0 | 73 | { |
michael@0 | 74 | if (!mLen) |
michael@0 | 75 | return mStream->ReadSegments(aWriter, aClosure, aCount, result); |
michael@0 | 76 | |
michael@0 | 77 | *result = 0; |
michael@0 | 78 | while (mLen > 0 && aCount > 0) { |
michael@0 | 79 | uint32_t toRead = std::min(mLen, aCount); |
michael@0 | 80 | uint32_t didRead = 0; |
michael@0 | 81 | nsresult rv; |
michael@0 | 82 | |
michael@0 | 83 | rv = aWriter(this, aClosure, mBuf + mOffset, *result, toRead, &didRead); |
michael@0 | 84 | |
michael@0 | 85 | if (NS_FAILED(rv)) |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | |
michael@0 | 88 | *result += didRead; |
michael@0 | 89 | mOffset += didRead; |
michael@0 | 90 | mLen -= didRead; |
michael@0 | 91 | aCount -= didRead; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | return NS_OK; |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | NS_IMETHODIMP |
michael@0 | 98 | nsPreloadedStream::IsNonBlocking(bool *_retval) |
michael@0 | 99 | { |
michael@0 | 100 | return mStream->IsNonBlocking(_retval); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | NS_IMETHODIMP |
michael@0 | 104 | nsPreloadedStream::CloseWithStatus(nsresult aStatus) |
michael@0 | 105 | { |
michael@0 | 106 | mLen = 0; |
michael@0 | 107 | return mStream->CloseWithStatus(aStatus); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | class RunOnThread : public nsRunnable |
michael@0 | 111 | { |
michael@0 | 112 | public: |
michael@0 | 113 | RunOnThread(nsIAsyncInputStream *aStream, |
michael@0 | 114 | nsIInputStreamCallback *aCallback) |
michael@0 | 115 | : mStream(aStream), |
michael@0 | 116 | mCallback(aCallback) {} |
michael@0 | 117 | |
michael@0 | 118 | virtual ~RunOnThread() {} |
michael@0 | 119 | |
michael@0 | 120 | NS_IMETHOD Run() |
michael@0 | 121 | { |
michael@0 | 122 | mCallback->OnInputStreamReady(mStream); |
michael@0 | 123 | return NS_OK; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | private: |
michael@0 | 127 | nsCOMPtr<nsIAsyncInputStream> mStream; |
michael@0 | 128 | nsCOMPtr<nsIInputStreamCallback> mCallback; |
michael@0 | 129 | }; |
michael@0 | 130 | |
michael@0 | 131 | NS_IMETHODIMP |
michael@0 | 132 | nsPreloadedStream::AsyncWait(nsIInputStreamCallback *aCallback, |
michael@0 | 133 | uint32_t aFlags, |
michael@0 | 134 | uint32_t aRequestedCount, |
michael@0 | 135 | nsIEventTarget *aEventTarget) |
michael@0 | 136 | { |
michael@0 | 137 | if (!mLen) |
michael@0 | 138 | return mStream->AsyncWait(aCallback, aFlags, aRequestedCount, |
michael@0 | 139 | aEventTarget); |
michael@0 | 140 | |
michael@0 | 141 | if (!aCallback) |
michael@0 | 142 | return NS_OK; |
michael@0 | 143 | |
michael@0 | 144 | if (!aEventTarget) |
michael@0 | 145 | return aCallback->OnInputStreamReady(this); |
michael@0 | 146 | |
michael@0 | 147 | nsCOMPtr<nsIRunnable> event = |
michael@0 | 148 | new RunOnThread(this, aCallback); |
michael@0 | 149 | return aEventTarget->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | } // namespace net |
michael@0 | 153 | } // namespace mozilla |