2015-11-30 17:54:11 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "nsIncrementalStreamLoader.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "GeckoProfiler.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
nsIncrementalStreamLoader::nsIncrementalStreamLoader()
|
2015-12-01 17:00:58 +03:00
|
|
|
: mData(), mBytesConsumed(0) {}
|
2015-11-30 17:54:11 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::Init(nsIIncrementalStreamLoaderObserver *observer) {
|
|
|
|
NS_ENSURE_ARG_POINTER(observer);
|
|
|
|
mObserver = observer;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIncrementalStreamLoader::Create(nsISupports *aOuter, REFNSIID aIID,
|
|
|
|
void **aResult) {
|
|
|
|
if (aOuter) return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsIncrementalStreamLoader *it = new nsIncrementalStreamLoader();
|
|
|
|
if (it == nullptr) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(it);
|
|
|
|
nsresult rv = it->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(it);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsIncrementalStreamLoader, nsIIncrementalStreamLoader,
|
|
|
|
nsIRequestObserver, nsIStreamListener,
|
|
|
|
nsIThreadRetargetableStreamListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::GetNumBytesRead(uint32_t *aNumBytes) {
|
2015-12-01 17:00:58 +03:00
|
|
|
*aNumBytes = mBytesConsumed + mData.length();
|
2015-11-30 17:54:11 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* readonly attribute nsIRequest request; */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::GetRequest(nsIRequest **aRequest) {
|
|
|
|
NS_IF_ADDREF(*aRequest = mRequest);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::OnStartRequest(nsIRequest *request,
|
|
|
|
nsISupports *ctxt) {
|
|
|
|
nsCOMPtr<nsIChannel> chan(do_QueryInterface(request));
|
|
|
|
if (chan) {
|
|
|
|
int64_t contentLength = -1;
|
|
|
|
chan->GetContentLength(&contentLength);
|
|
|
|
if (contentLength >= 0) {
|
2017-10-20 15:46:28 +03:00
|
|
|
// On 64bit platforms size of uint64_t coincides with the size of size_t,
|
2017-10-21 15:07:49 +03:00
|
|
|
// so we want to compare with the minimum from size_t and int64_t.
|
2017-10-20 15:46:28 +03:00
|
|
|
if (static_cast<uint64_t>(contentLength) >
|
|
|
|
std::min(std::numeric_limits<size_t>::max(),
|
|
|
|
static_cast<size_t>(std::numeric_limits<int64_t>::max()))) {
|
2015-11-30 17:54:11 +03:00
|
|
|
// Too big to fit into size_t, so let's bail.
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2017-10-20 15:46:28 +03:00
|
|
|
|
2015-11-30 17:54:11 +03:00
|
|
|
// preallocate buffer
|
|
|
|
if (!mData.initCapacity(contentLength)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mContext = ctxt;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::OnStopRequest(nsIRequest *request, nsISupports *ctxt,
|
|
|
|
nsresult aStatus) {
|
Bug 1375392 - Tweak the PROFILER_LABEL* macros. r=mstange.
This patch makes the following changes to the macros.
- Removes PROFILER_LABEL_FUNC. It's only suitable for use in functions outside
classes, due to PROFILER_FUNCTION_NAME not getting class names, and it was
mostly misused.
- Removes PROFILER_FUNCTION_NAME. It's no longer used, and __func__ is
universally available now anyway.
- Combines the first two string literal arguments of PROFILER_LABEL and
PROFILER_LABEL_DYNAMIC into a single argument. There was no good reason for
them to be separate, and it forced a '::' in the label, which isn't always
appropriate. Also, the meaning of the "name_space" argument was interpreted
in an interesting variety of ways.
- Adds an "AUTO_" prefix to PROFILER_LABEL and PROFILER_LABEL_DYNAMIC, to make
it clearer they construct RAII objects rather than just being function calls.
(I myself have screwed up the scoping because of this in the past.)
- Fills in the 'js::ProfileEntry::Category::' qualifier within the macro, so
the caller doesn't need to. This makes a *lot* more of the uses fit onto a
single line.
The patch also makes the following changes to the macro uses (beyond those
required by the changes described above).
- Fixes a bunch of labels that had gotten out of sync with the name of the
class and/or function that encloses them.
- Removes a useless PROFILER_LABEL use within a trivial scope in
EventStateManager::DispatchMouseOrPointerEvent(). It clearly wasn't serving
any useful purpose. It also serves as extra evidence that the AUTO_ prefix is
a good idea.
- Tweaks DecodePool::SyncRunIf{Preferred,Possible} so that the labelling is
done within them, instead of at their callsites, because that's a more
standard way of doing things.
--HG--
extra : rebase_source : 318d1bc6fc1425a94aacbf489dd46e4f83211de4
2017-06-22 10:08:53 +03:00
|
|
|
AUTO_PROFILER_LABEL("nsIncrementalStreamLoader::OnStopRequest", NETWORK);
|
2015-11-30 17:54:11 +03:00
|
|
|
|
|
|
|
if (mObserver) {
|
|
|
|
// provide nsIIncrementalStreamLoader::request during call to
|
|
|
|
// OnStreamComplete
|
|
|
|
mRequest = request;
|
|
|
|
size_t length = mData.length();
|
2016-04-25 15:41:25 +03:00
|
|
|
uint8_t *elems = mData.extractOrCopyRawBuffer();
|
2015-11-30 17:54:11 +03:00
|
|
|
nsresult rv =
|
|
|
|
mObserver->OnStreamComplete(this, mContext, aStatus, 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);
|
|
|
|
}
|
|
|
|
// done.. cleanup
|
|
|
|
ReleaseData();
|
2016-11-10 06:11:27 +03:00
|
|
|
mRequest = nullptr;
|
|
|
|
mObserver = nullptr;
|
|
|
|
mContext = nullptr;
|
2015-11-30 17:54:11 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsIncrementalStreamLoader::WriteSegmentFun(
|
|
|
|
nsIInputStream *inStr, void *closure, const char *fromSegment,
|
|
|
|
uint32_t toOffset, uint32_t count, uint32_t *writeCount) {
|
|
|
|
nsIncrementalStreamLoader *self = (nsIncrementalStreamLoader *)closure;
|
|
|
|
|
2015-12-01 17:00:58 +03:00
|
|
|
const uint8_t *data = reinterpret_cast<const uint8_t *>(fromSegment);
|
|
|
|
uint32_t consumedCount = 0;
|
|
|
|
nsresult rv;
|
|
|
|
if (self->mData.empty()) {
|
|
|
|
// Shortcut when observer wants to keep the listener's buffer empty.
|
|
|
|
rv = self->mObserver->OnIncrementalData(self, self->mContext, count, data,
|
|
|
|
&consumedCount);
|
|
|
|
|
|
|
|
if (rv != NS_OK) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumedCount > count) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumedCount < count) {
|
|
|
|
if (!self->mData.append(fromSegment + consumedCount,
|
|
|
|
count - consumedCount)) {
|
|
|
|
self->mData.clearAndFree();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We have some non-consumed data from previous OnIncrementalData call,
|
|
|
|
// appending new data and reporting combined data.
|
|
|
|
if (!self->mData.append(fromSegment, count)) {
|
|
|
|
self->mData.clearAndFree();
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
size_t length = self->mData.length();
|
|
|
|
uint32_t reportCount = length > UINT32_MAX ? UINT32_MAX : (uint32_t)length;
|
2016-04-25 15:41:25 +03:00
|
|
|
uint8_t *elems = self->mData.extractOrCopyRawBuffer();
|
2015-12-01 17:00:58 +03:00
|
|
|
|
|
|
|
rv = self->mObserver->OnIncrementalData(self, self->mContext, reportCount,
|
|
|
|
elems, &consumedCount);
|
|
|
|
|
|
|
|
// We still own elems, freeing its memory when exiting scope.
|
|
|
|
if (rv != NS_OK) {
|
|
|
|
free(elems);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumedCount > reportCount) {
|
|
|
|
free(elems);
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumedCount == length) {
|
|
|
|
free(elems); // good case -- fully consumed data
|
|
|
|
} else {
|
|
|
|
// Adopting elems back (at least its portion).
|
|
|
|
self->mData.replaceRawBuffer(elems, length);
|
|
|
|
if (consumedCount > 0) {
|
|
|
|
self->mData.erase(self->mData.begin() + consumedCount);
|
|
|
|
}
|
|
|
|
}
|
2015-11-30 17:54:11 +03:00
|
|
|
}
|
|
|
|
|
2015-12-01 17:00:58 +03:00
|
|
|
self->mBytesConsumed += consumedCount;
|
2015-11-30 17:54:11 +03:00
|
|
|
*writeCount = count;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::OnDataAvailable(nsIRequest *request,
|
|
|
|
nsISupports *ctxt,
|
|
|
|
nsIInputStream *inStr,
|
|
|
|
uint64_t sourceOffset,
|
|
|
|
uint32_t count) {
|
2015-12-01 17:00:58 +03:00
|
|
|
if (mObserver) {
|
|
|
|
// provide nsIIncrementalStreamLoader::request during call to
|
|
|
|
// OnStreamComplete
|
|
|
|
mRequest = request;
|
|
|
|
}
|
2015-11-30 17:54:11 +03:00
|
|
|
uint32_t countRead;
|
2015-12-01 17:00:58 +03:00
|
|
|
nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
|
2016-11-10 06:11:27 +03:00
|
|
|
mRequest = nullptr;
|
2015-12-01 17:00:58 +03:00
|
|
|
return rv;
|
2015-11-30 17:54:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsIncrementalStreamLoader::ReleaseData() { mData.clearAndFree(); }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalStreamLoader::CheckListenerChain() { return NS_OK; }
|