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