netwerk/base/src/nsStreamLoader.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "nsStreamLoader.h"
michael@0 7 #include "nsIInputStream.h"
michael@0 8 #include "nsIChannel.h"
michael@0 9 #include "nsError.h"
michael@0 10 #include "GeckoProfiler.h"
michael@0 11
michael@0 12 nsStreamLoader::nsStreamLoader()
michael@0 13 : mData(nullptr),
michael@0 14 mAllocated(0),
michael@0 15 mLength(0)
michael@0 16 {
michael@0 17 }
michael@0 18
michael@0 19 nsStreamLoader::~nsStreamLoader()
michael@0 20 {
michael@0 21 ReleaseData();
michael@0 22 }
michael@0 23
michael@0 24 NS_IMETHODIMP
michael@0 25 nsStreamLoader::Init(nsIStreamLoaderObserver* observer)
michael@0 26 {
michael@0 27 NS_ENSURE_ARG_POINTER(observer);
michael@0 28 mObserver = observer;
michael@0 29 return NS_OK;
michael@0 30 }
michael@0 31
michael@0 32 nsresult
michael@0 33 nsStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
michael@0 34 {
michael@0 35 if (aOuter) return NS_ERROR_NO_AGGREGATION;
michael@0 36
michael@0 37 nsStreamLoader* it = new nsStreamLoader();
michael@0 38 if (it == nullptr)
michael@0 39 return NS_ERROR_OUT_OF_MEMORY;
michael@0 40 NS_ADDREF(it);
michael@0 41 nsresult rv = it->QueryInterface(aIID, aResult);
michael@0 42 NS_RELEASE(it);
michael@0 43 return rv;
michael@0 44 }
michael@0 45
michael@0 46 NS_IMPL_ISUPPORTS(nsStreamLoader, nsIStreamLoader,
michael@0 47 nsIRequestObserver, nsIStreamListener)
michael@0 48
michael@0 49 NS_IMETHODIMP
michael@0 50 nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
michael@0 51 {
michael@0 52 *aNumBytes = mLength;
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 /* readonly attribute nsIRequest request; */
michael@0 57 NS_IMETHODIMP
michael@0 58 nsStreamLoader::GetRequest(nsIRequest **aRequest)
michael@0 59 {
michael@0 60 NS_IF_ADDREF(*aRequest = mRequest);
michael@0 61 return NS_OK;
michael@0 62 }
michael@0 63
michael@0 64 NS_IMETHODIMP
michael@0 65 nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
michael@0 66 {
michael@0 67 nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
michael@0 68 if (chan) {
michael@0 69 int64_t contentLength = -1;
michael@0 70 chan->GetContentLength(&contentLength);
michael@0 71 if (contentLength >= 0) {
michael@0 72 if (contentLength > UINT32_MAX) {
michael@0 73 // Too big to fit into uint32, so let's bail.
michael@0 74 // XXX we should really make mAllocated and mLength 64-bit instead.
michael@0 75 return NS_ERROR_OUT_OF_MEMORY;
michael@0 76 }
michael@0 77 uint32_t contentLength32 = uint32_t(contentLength);
michael@0 78 // preallocate buffer
michael@0 79 mData = static_cast<uint8_t*>(moz_malloc(contentLength32));
michael@0 80 if (!mData) {
michael@0 81 return NS_ERROR_OUT_OF_MEMORY;
michael@0 82 }
michael@0 83 mAllocated = contentLength32;
michael@0 84 }
michael@0 85 }
michael@0 86 mContext = ctxt;
michael@0 87 return NS_OK;
michael@0 88 }
michael@0 89
michael@0 90 NS_IMETHODIMP
michael@0 91 nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
michael@0 92 nsresult aStatus)
michael@0 93 {
michael@0 94 PROFILER_LABEL("network", "nsStreamLoader::OnStopRequest");
michael@0 95 if (mObserver) {
michael@0 96 // provide nsIStreamLoader::request during call to OnStreamComplete
michael@0 97 mRequest = request;
michael@0 98 nsresult rv = mObserver->OnStreamComplete(this, mContext, aStatus,
michael@0 99 mLength, mData);
michael@0 100 if (rv == NS_SUCCESS_ADOPTED_DATA) {
michael@0 101 // the observer now owns the data buffer, and the loader must
michael@0 102 // not deallocate it
michael@0 103 mData = nullptr;
michael@0 104 }
michael@0 105 // done.. cleanup
michael@0 106 ReleaseData();
michael@0 107 mRequest = 0;
michael@0 108 mObserver = 0;
michael@0 109 mContext = 0;
michael@0 110 }
michael@0 111 return NS_OK;
michael@0 112 }
michael@0 113
michael@0 114 NS_METHOD
michael@0 115 nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
michael@0 116 void *closure,
michael@0 117 const char *fromSegment,
michael@0 118 uint32_t toOffset,
michael@0 119 uint32_t count,
michael@0 120 uint32_t *writeCount)
michael@0 121 {
michael@0 122 nsStreamLoader *self = (nsStreamLoader *) closure;
michael@0 123
michael@0 124 if (count > UINT32_MAX - self->mLength) {
michael@0 125 return NS_ERROR_ILLEGAL_VALUE; // is there a better error to use here?
michael@0 126 }
michael@0 127
michael@0 128 if (self->mLength + count > self->mAllocated) {
michael@0 129 self->mData = static_cast<uint8_t*>(NS_Realloc(self->mData,
michael@0 130 self->mLength + count));
michael@0 131 if (!self->mData) {
michael@0 132 self->ReleaseData();
michael@0 133 return NS_ERROR_OUT_OF_MEMORY;
michael@0 134 }
michael@0 135 self->mAllocated = self->mLength + count;
michael@0 136 }
michael@0 137
michael@0 138 ::memcpy(self->mData + self->mLength, fromSegment, count);
michael@0 139 self->mLength += count;
michael@0 140
michael@0 141 *writeCount = count;
michael@0 142
michael@0 143 return NS_OK;
michael@0 144 }
michael@0 145
michael@0 146 NS_IMETHODIMP
michael@0 147 nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
michael@0 148 nsIInputStream *inStr,
michael@0 149 uint64_t sourceOffset, uint32_t count)
michael@0 150 {
michael@0 151 uint32_t countRead;
michael@0 152 return inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
michael@0 153 }
michael@0 154
michael@0 155 void
michael@0 156 nsStreamLoader::ReleaseData()
michael@0 157 {
michael@0 158 if (mData) {
michael@0 159 NS_Free(mData);
michael@0 160 mData = nullptr;
michael@0 161 }
michael@0 162 mLength = 0;
michael@0 163 mAllocated = 0;
michael@0 164 }

mercurial