2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2000-02-02 23:04:47 +00:00
|
|
|
|
|
|
|
#include "nsStreamLoader.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIChannel.h"
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2013-03-18 14:25:50 +00:00
|
|
|
#include "GeckoProfiler.h"
|
2009-09-17 10:14:25 +00:00
|
|
|
|
2014-10-31 02:48:30 +00:00
|
|
|
#include <limits>
|
|
|
|
|
2016-05-19 02:02:57 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2009-09-17 10:14:25 +00:00
|
|
|
nsStreamLoader::nsStreamLoader()
|
2014-10-31 02:48:30 +00:00
|
|
|
: mData()
|
2009-09-17 10:14:25 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsStreamLoader::~nsStreamLoader()
|
|
|
|
{
|
|
|
|
}
|
2000-02-02 23:04:47 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-05-04 14:17:47 +00:00
|
|
|
nsStreamLoader::Init(nsIStreamLoaderObserver* aStreamObserver,
|
|
|
|
nsIRequestObserver* aRequestObserver)
|
2000-02-02 23:04:47 +00:00
|
|
|
{
|
2015-05-04 14:17:47 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aStreamObserver);
|
|
|
|
mObserver = aStreamObserver;
|
|
|
|
mRequestObserver = aRequestObserver;
|
2006-10-24 21:47:40 +00:00
|
|
|
return NS_OK;
|
2000-02-02 23:04:47 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:40 +00:00
|
|
|
nsresult
|
2000-02-02 23:04:47 +00:00
|
|
|
nsStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult)
|
|
|
|
{
|
|
|
|
if (aOuter) return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsStreamLoader* it = new nsStreamLoader();
|
2012-07-30 14:20:58 +00:00
|
|
|
if (it == nullptr)
|
2000-02-02 23:04:47 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(it);
|
|
|
|
nsresult rv = it->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(it);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsStreamLoader, nsIStreamLoader,
|
2014-11-24 18:28:54 +00:00
|
|
|
nsIRequestObserver, nsIStreamListener,
|
|
|
|
nsIThreadRetargetableStreamListener)
|
2000-02-02 23:04:47 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
2000-02-02 23:04:47 +00:00
|
|
|
{
|
2014-10-31 02:48:30 +00:00
|
|
|
*aNumBytes = mData.length();
|
2000-02-02 23:04:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-14 02:46:28 +00:00
|
|
|
NS_IMETHODIMP
|
2001-10-18 10:37:04 +00:00
|
|
|
nsStreamLoader::GetRequest(nsIRequest **aRequest)
|
2000-04-14 02:46:28 +00:00
|
|
|
{
|
2006-10-24 21:47:40 +00:00
|
|
|
NS_IF_ADDREF(*aRequest = mRequest);
|
|
|
|
return NS_OK;
|
2000-04-14 02:46:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-31 02:48:30 +00:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsStreamLoader::OnStartRequest(nsIRequest* request, nsISupports *ctxt)
|
2000-02-02 23:04:47 +00:00
|
|
|
{
|
2001-12-14 22:54:56 +00:00
|
|
|
nsCOMPtr<nsIChannel> chan( do_QueryInterface(request) );
|
|
|
|
if (chan) {
|
2012-10-22 17:51:07 +00:00
|
|
|
int64_t contentLength = -1;
|
2006-10-24 21:47:40 +00:00
|
|
|
chan->GetContentLength(&contentLength);
|
|
|
|
if (contentLength >= 0) {
|
2014-10-31 02:48:30 +00:00
|
|
|
if (uint64_t(contentLength) > std::numeric_limits<size_t>::max()) {
|
|
|
|
// Too big to fit into size_t, so let's bail.
|
2012-10-22 17:51:07 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-10-24 21:47:40 +00:00
|
|
|
// preallocate buffer
|
2014-10-31 02:48:30 +00:00
|
|
|
if (!mData.initCapacity(contentLength)) {
|
2009-09-17 10:14:25 +00:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2006-10-24 21:47:40 +00:00
|
|
|
}
|
2001-12-14 22:54:56 +00:00
|
|
|
}
|
2006-10-24 21:47:40 +00:00
|
|
|
mContext = ctxt;
|
2015-05-04 14:17:47 +00:00
|
|
|
if (mRequestObserver) {
|
|
|
|
mRequestObserver->OnStartRequest(request, ctxt);
|
|
|
|
}
|
2000-02-02 23:04:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-31 02:48:30 +00:00
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsStreamLoader::OnStopRequest(nsIRequest* request, nsISupports *ctxt,
|
2001-04-10 06:01:08 +00:00
|
|
|
nsresult aStatus)
|
2000-02-02 23:04:47 +00:00
|
|
|
{
|
2014-05-23 21:12:29 +00:00
|
|
|
PROFILER_LABEL("nsStreamLoader", "OnStopRequest",
|
|
|
|
js::ProfileEntry::Category::NETWORK);
|
|
|
|
|
2001-11-06 23:09:44 +00:00
|
|
|
if (mObserver) {
|
2001-10-18 10:37:04 +00:00
|
|
|
// provide nsIStreamLoader::request during call to OnStreamComplete
|
|
|
|
mRequest = request;
|
2014-10-31 02:48:30 +00:00
|
|
|
size_t length = mData.length();
|
2016-04-25 12:41:25 +00:00
|
|
|
uint8_t* elems = mData.extractOrCopyRawBuffer();
|
2009-09-17 10:14:25 +00:00
|
|
|
nsresult rv = mObserver->OnStreamComplete(this, mContext, aStatus,
|
2014-10-31 02:48:30 +00:00
|
|
|
length, elems);
|
|
|
|
if (rv != NS_SUCCESS_ADOPTED_DATA) {
|
|
|
|
// The observer didn't take ownership of the extracted data buffer, so
|
|
|
|
// put it back into mData.
|
|
|
|
mData.replaceRawBuffer(elems, length);
|
2009-09-17 10:14:25 +00:00
|
|
|
}
|
2001-10-18 10:37:04 +00:00
|
|
|
// done.. cleanup
|
2014-04-17 14:59:54 +00:00
|
|
|
ReleaseData();
|
2001-10-18 10:37:04 +00:00
|
|
|
mRequest = 0;
|
|
|
|
mObserver = 0;
|
|
|
|
mContext = 0;
|
2001-07-17 18:57:42 +00:00
|
|
|
}
|
2015-05-04 14:17:47 +00:00
|
|
|
|
|
|
|
if (mRequestObserver) {
|
|
|
|
mRequestObserver->OnStopRequest(request, ctxt, aStatus);
|
|
|
|
mRequestObserver = nullptr;
|
|
|
|
}
|
|
|
|
|
2001-10-18 10:37:04 +00:00
|
|
|
return NS_OK;
|
2000-02-02 23:04:47 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 22:54:56 +00:00
|
|
|
NS_METHOD
|
|
|
|
nsStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
|
|
|
|
void *closure,
|
|
|
|
const char *fromSegment,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount)
|
2001-12-14 22:54:56 +00:00
|
|
|
{
|
|
|
|
nsStreamLoader *self = (nsStreamLoader *) closure;
|
|
|
|
|
2014-10-31 02:48:30 +00:00
|
|
|
if (!self->mData.append(fromSegment, count)) {
|
|
|
|
self->mData.clearAndFree();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-09-17 10:14:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 22:54:56 +00:00
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-02-02 23:04:47 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 20:38:08 +00:00
|
|
|
nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
|
2000-02-02 23:04:47 +00:00
|
|
|
nsIInputStream *inStr,
|
2012-09-06 02:41:02 +00:00
|
|
|
uint64_t sourceOffset, uint32_t count)
|
2000-02-02 23:04:47 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t countRead;
|
2001-12-14 22:54:56 +00:00
|
|
|
return inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
|
2000-02-02 23:04:47 +00:00
|
|
|
}
|
2014-04-17 14:59:54 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsStreamLoader::ReleaseData()
|
|
|
|
{
|
2014-10-31 02:48:30 +00:00
|
|
|
mData.clearAndFree();
|
2014-04-17 14:59:54 +00:00
|
|
|
}
|
2014-11-24 18:28:54 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsStreamLoader::CheckListenerChain()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-05-19 02:02:57 +00:00
|
|
|
|
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|