2005-06-01 18:05:36 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
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/. */
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2011-12-18 03:47:45 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-11-09 02:28:05 +00:00
|
|
|
#include "mozilla/UniquePtrExtensions.h"
|
2016-01-08 15:20:00 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2011-12-18 03:47:45 +00:00
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
#include "nsIIncrementalDownload.h"
|
|
|
|
#include "nsIRequestObserver.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
2005-10-10 23:44:47 +00:00
|
|
|
#include "nsIChannelEventSink.h"
|
2010-08-05 02:15:55 +00:00
|
|
|
#include "nsIAsyncVerifyRedirectCallback.h"
|
2005-10-10 23:44:47 +00:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2005-06-01 18:05:36 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIObserver.h"
|
2015-07-31 20:48:26 +00:00
|
|
|
#include "nsIStreamListener.h"
|
2012-06-06 02:08:30 +00:00
|
|
|
#include "nsIFile.h"
|
2005-06-01 18:05:36 +00:00
|
|
|
#include "nsITimer.h"
|
2015-07-31 20:48:26 +00:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIInputStream.h"
|
2005-06-01 18:05:36 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "prio.h"
|
|
|
|
#include "prprf.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2014-09-21 16:40:12 +00:00
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsContentUtils.h"
|
2016-01-09 01:20:50 +00:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
// Default values used to initialize a nsIncrementalDownload object.
|
|
|
|
#define DEFAULT_CHUNK_SIZE (4096 * 16) // bytes
|
|
|
|
#define DEFAULT_INTERVAL 60 // seconds
|
|
|
|
|
2009-06-16 08:46:40 +00:00
|
|
|
#define UPDATE_PROGRESS_INTERVAL PRTime(500 * PR_USEC_PER_MSEC) // 500ms
|
|
|
|
|
2005-10-20 21:45:13 +00:00
|
|
|
// Number of times to retry a failed byte-range request.
|
|
|
|
#define MAX_RETRY_COUNT 20
|
|
|
|
|
2016-01-09 01:20:50 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
static nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
WriteToFile(nsIFile *lf, const char *data, uint32_t len, int32_t flags)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
PRFileDesc *fd;
|
2013-01-10 13:46:39 +00:00
|
|
|
int32_t mode = 0600;
|
|
|
|
nsresult rv;
|
|
|
|
#if defined(MOZ_WIDGET_GONK)
|
|
|
|
// The sdcard on a B2G phone looks like:
|
|
|
|
// d---rwx--- system sdcard_rw 1970-01-01 01:00:00 sdcard
|
|
|
|
// On the emulator, xpcshell fails when using 0600 mode to open the file,
|
|
|
|
// and 0660 works.
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
rv = lf->GetParent(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
uint32_t parentPerm;
|
|
|
|
rv = parent->GetPermissions(&parentPerm);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if ((parentPerm & 0700) == 0) {
|
|
|
|
// Parent directory has no owner-write, so try to use group permissions
|
|
|
|
// instead of owner permissions.
|
|
|
|
mode = 0660;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
rv = lf->OpenNSPRFileDesc(flags, mode, &fd);
|
2005-06-01 18:05:36 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2005-10-20 21:45:13 +00:00
|
|
|
if (len)
|
2012-08-22 15:56:38 +00:00
|
|
|
rv = PR_Write(fd, data, len) == int32_t(len) ? NS_OK : NS_ERROR_FAILURE;
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
PR_Close(fd);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
AppendToFile(nsIFile *lf, const char *data, uint32_t len)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t flags = PR_WRONLY | PR_CREATE_FILE | PR_APPEND;
|
2005-06-01 18:05:36 +00:00
|
|
|
return WriteToFile(lf, data, len, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxSize may be -1 if unknown
|
|
|
|
static void
|
2012-08-22 15:56:38 +00:00
|
|
|
MakeRangeSpec(const int64_t &size, const int64_t &maxSize, int32_t chunkSize,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool fetchRemaining, nsCString &rangeSpec)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
rangeSpec.AssignLiteral("bytes=");
|
2012-08-22 15:56:38 +00:00
|
|
|
rangeSpec.AppendInt(int64_t(size));
|
2005-06-01 18:05:36 +00:00
|
|
|
rangeSpec.Append('-');
|
|
|
|
|
|
|
|
if (fetchRemaining)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t end = size + int64_t(chunkSize);
|
|
|
|
if (maxSize != int64_t(-1) && end > maxSize)
|
2005-06-01 18:05:36 +00:00
|
|
|
end = maxSize;
|
|
|
|
end -= 1;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
rangeSpec.AppendInt(int64_t(end));
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsIncrementalDownload final
|
2011-12-18 03:47:45 +00:00
|
|
|
: public nsIIncrementalDownload
|
|
|
|
, public nsIStreamListener
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
, public nsIAsyncVerifyRedirectCallback
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIREQUEST
|
|
|
|
NS_DECL_NSIINCREMENTALDOWNLOAD
|
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSIOBSERVER
|
2005-10-10 23:44:47 +00:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
2010-08-05 02:15:55 +00:00
|
|
|
NS_DECL_NSIASYNCVERIFYREDIRECTCALLBACK
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
nsIncrementalDownload();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~nsIncrementalDownload() {}
|
|
|
|
nsresult FlushChunk();
|
2009-06-16 08:46:40 +00:00
|
|
|
void UpdateProgress();
|
2005-06-01 18:05:36 +00:00
|
|
|
nsresult CallOnStartRequest();
|
|
|
|
void CallOnStopRequest();
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult StartTimer(int32_t interval);
|
2005-06-01 18:05:36 +00:00
|
|
|
nsresult ProcessTimeout();
|
|
|
|
nsresult ReadCurrentSize();
|
2006-12-29 01:19:32 +00:00
|
|
|
nsresult ClearRequestHeader(nsIHttpChannel *channel);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
nsCOMPtr<nsIRequestObserver> mObserver;
|
|
|
|
nsCOMPtr<nsISupports> mObserverContext;
|
|
|
|
nsCOMPtr<nsIProgressEventSink> mProgressSink;
|
|
|
|
nsCOMPtr<nsIURI> mURI;
|
|
|
|
nsCOMPtr<nsIURI> mFinalURI;
|
2012-06-06 02:08:30 +00:00
|
|
|
nsCOMPtr<nsIFile> mDest;
|
2010-08-05 02:15:55 +00:00
|
|
|
nsCOMPtr<nsIChannel> mChannel;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2016-01-08 15:20:00 +00:00
|
|
|
mozilla::UniquePtr<char[]> mChunk;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mChunkLen;
|
|
|
|
int32_t mChunkSize;
|
|
|
|
int32_t mInterval;
|
|
|
|
int64_t mTotalSize;
|
|
|
|
int64_t mCurrentSize;
|
|
|
|
uint32_t mLoadFlags;
|
|
|
|
int32_t mNonPartialCount;
|
2010-08-05 02:15:55 +00:00
|
|
|
nsresult mStatus;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mIsPending;
|
|
|
|
bool mDidOnStartRequest;
|
2010-08-05 02:15:55 +00:00
|
|
|
PRTime mLastProgressUpdate;
|
|
|
|
nsCOMPtr<nsIAsyncVerifyRedirectCallback> mRedirectCallback;
|
|
|
|
nsCOMPtr<nsIChannel> mNewRedirectChannel;
|
2013-10-20 01:08:25 +00:00
|
|
|
nsCString mPartialValidator;
|
|
|
|
bool mCacheBust;
|
2005-06-01 18:05:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nsIncrementalDownload::nsIncrementalDownload()
|
|
|
|
: mChunkLen(0)
|
|
|
|
, mChunkSize(DEFAULT_CHUNK_SIZE)
|
|
|
|
, mInterval(DEFAULT_INTERVAL)
|
|
|
|
, mTotalSize(-1)
|
|
|
|
, mCurrentSize(-1)
|
|
|
|
, mLoadFlags(LOAD_NORMAL)
|
2005-10-20 21:45:13 +00:00
|
|
|
, mNonPartialCount(0)
|
2005-06-01 18:05:36 +00:00
|
|
|
, mStatus(NS_OK)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mIsPending(false)
|
|
|
|
, mDidOnStartRequest(false)
|
2009-06-16 08:46:40 +00:00
|
|
|
, mLastProgressUpdate(0)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mRedirectCallback(nullptr)
|
|
|
|
, mNewRedirectChannel(nullptr)
|
2013-10-20 01:08:25 +00:00
|
|
|
, mCacheBust(false)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIncrementalDownload::FlushChunk()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_ASSERTION(mTotalSize != int64_t(-1), "total size should be known");
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
if (mChunkLen == 0)
|
|
|
|
return NS_OK;
|
|
|
|
|
2015-11-09 02:28:05 +00:00
|
|
|
nsresult rv = AppendToFile(mDest, mChunk.get(), mChunkLen);
|
2005-06-01 18:05:36 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
mCurrentSize += int64_t(mChunkLen);
|
2005-06-01 18:05:36 +00:00
|
|
|
mChunkLen = 0;
|
|
|
|
|
2009-06-16 08:46:40 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIncrementalDownload::UpdateProgress()
|
|
|
|
{
|
|
|
|
mLastProgressUpdate = PR_Now();
|
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
if (mProgressSink)
|
|
|
|
mProgressSink->OnProgress(this, mObserverContext,
|
2015-01-08 19:48:52 +00:00
|
|
|
mCurrentSize + mChunkLen,
|
|
|
|
mTotalSize);
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIncrementalDownload::CallOnStartRequest()
|
|
|
|
{
|
|
|
|
if (!mObserver || mDidOnStartRequest)
|
|
|
|
return NS_OK;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mDidOnStartRequest = true;
|
2005-06-01 18:05:36 +00:00
|
|
|
return mObserver->OnStartRequest(this, mObserverContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsIncrementalDownload::CallOnStopRequest()
|
|
|
|
{
|
|
|
|
if (!mObserver)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Ensure that OnStartRequest is always called once before OnStopRequest.
|
|
|
|
nsresult rv = CallOnStartRequest();
|
|
|
|
if (NS_SUCCEEDED(mStatus))
|
|
|
|
mStatus = rv;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsPending = false;
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
mObserver->OnStopRequest(this, mObserverContext, mStatus);
|
2012-07-30 14:20:58 +00:00
|
|
|
mObserver = nullptr;
|
|
|
|
mObserverContext = nullptr;
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIncrementalDownload::StartTimer(int32_t interval)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
mTimer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return mTimer->Init(this, interval * 1000, nsITimer::TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsIncrementalDownload::ProcessTimeout()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mChannel, "how can we have a channel?");
|
|
|
|
|
|
|
|
// Handle existing error conditions
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
CallOnStopRequest();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch next chunk
|
2005-06-30 20:21:34 +00:00
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-09-21 16:40:12 +00:00
|
|
|
nsresult rv = NS_NewChannel(getter_AddRefs(channel),
|
|
|
|
mFinalURI,
|
|
|
|
nsContentUtils::GetSystemPrincipal(),
|
2015-09-12 01:52:08 +00:00
|
|
|
nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
|
2014-09-21 16:40:12 +00:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
nullptr, // loadGroup
|
|
|
|
this, // aCallbacks
|
|
|
|
mLoadFlags);
|
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(channel, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_ASSERTION(mCurrentSize != int64_t(-1),
|
2005-06-01 18:05:36 +00:00
|
|
|
"we should know the current file size by now");
|
|
|
|
|
2006-12-29 01:19:32 +00:00
|
|
|
rv = ClearRequestHeader(http);
|
2006-12-27 20:17:44 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2005-10-20 21:45:13 +00:00
|
|
|
// Don't bother making a range request if we are just going to fetch the
|
|
|
|
// entire document.
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mInterval || mCurrentSize != int64_t(0)) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString range;
|
2005-10-20 21:45:13 +00:00
|
|
|
MakeRangeSpec(mCurrentSize, mTotalSize, mChunkSize, mInterval == 0, range);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = http->SetRequestHeader(NS_LITERAL_CSTRING("Range"), range, false);
|
2005-10-20 21:45:13 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2013-10-20 01:08:25 +00:00
|
|
|
|
|
|
|
if (!mPartialValidator.IsEmpty())
|
|
|
|
http->SetRequestHeader(NS_LITERAL_CSTRING("If-Range"),
|
|
|
|
mPartialValidator, false);
|
|
|
|
|
|
|
|
if (mCacheBust) {
|
|
|
|
http->SetRequestHeader(NS_LITERAL_CSTRING("Cache-Control"),
|
|
|
|
NS_LITERAL_CSTRING("no-cache"), false);
|
|
|
|
http->SetRequestHeader(NS_LITERAL_CSTRING("Pragma"),
|
|
|
|
NS_LITERAL_CSTRING("no-cache"), false);
|
|
|
|
}
|
2005-10-20 21:45:13 +00:00
|
|
|
}
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2015-09-12 01:52:08 +00:00
|
|
|
rv = channel->AsyncOpen2(this);
|
2005-06-01 18:05:36 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Wait to assign mChannel when we know we are going to succeed. This is
|
|
|
|
// important because we don't want to introduce a reference cycle between
|
|
|
|
// mChannel and this until we know for a fact that AsyncOpen has succeeded,
|
|
|
|
// thus ensuring that our stream listener methods will be invoked.
|
|
|
|
mChannel = channel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reads the current file size and validates it.
|
|
|
|
nsresult
|
|
|
|
nsIncrementalDownload::ReadCurrentSize()
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t size;
|
|
|
|
nsresult rv = mDest->GetFileSize((int64_t *) &size);
|
2005-06-11 02:56:52 +00:00
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND ||
|
|
|
|
rv == NS_ERROR_FILE_TARGET_DOES_NOT_EXIST) {
|
2005-06-01 18:05:36 +00:00
|
|
|
mCurrentSize = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mCurrentSize = size;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsIncrementalDownload,
|
|
|
|
nsIIncrementalDownload,
|
|
|
|
nsIRequest,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIObserver,
|
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIChannelEventSink,
|
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsIAsyncVerifyRedirectCallback)
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
// nsIRequest
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetName(nsACString &name)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mURI, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
|
|
|
return mURI->GetSpec(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsIncrementalDownload::IsPending(bool *isPending)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
*isPending = mIsPending;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetStatus(nsresult *status)
|
|
|
|
{
|
|
|
|
*status = mStatus;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Cancel(nsresult status)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(NS_FAILED(status));
|
|
|
|
|
|
|
|
// Ignore this cancelation if we're already canceled.
|
|
|
|
if (NS_FAILED(mStatus))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
// Nothing more to do if callbacks aren't pending.
|
|
|
|
if (!mIsPending)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (mChannel) {
|
|
|
|
mChannel->Cancel(mStatus);
|
|
|
|
NS_ASSERTION(!mTimer, "what is this timer object doing here?");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// dispatch a timer callback event to drive invoking our listener's
|
|
|
|
// OnStopRequest.
|
|
|
|
if (mTimer)
|
|
|
|
mTimer->Cancel();
|
|
|
|
StartTimer(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Suspend()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Resume()
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetLoadFlags(nsLoadFlags *loadFlags)
|
|
|
|
{
|
|
|
|
*loadFlags = mLoadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::SetLoadFlags(nsLoadFlags loadFlags)
|
|
|
|
{
|
|
|
|
mLoadFlags = loadFlags;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetLoadGroup(nsILoadGroup **loadGroup)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::SetLoadGroup(nsILoadGroup *loadGroup)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIIncrementalDownload
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Init(nsIURI *uri, nsIFile *dest,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t chunkSize, int32_t interval)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
// Keep it simple: only allow initialization once
|
|
|
|
NS_ENSURE_FALSE(mURI, NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
|
|
|
|
mDest = do_QueryInterface(dest);
|
|
|
|
NS_ENSURE_ARG(mDest);
|
|
|
|
|
|
|
|
mURI = uri;
|
|
|
|
mFinalURI = uri;
|
|
|
|
|
2005-10-20 21:45:13 +00:00
|
|
|
if (chunkSize > 0)
|
2005-06-01 18:05:36 +00:00
|
|
|
mChunkSize = chunkSize;
|
2005-10-20 21:45:13 +00:00
|
|
|
if (interval >= 0)
|
2005-06-01 18:05:36 +00:00
|
|
|
mInterval = interval;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetURI(nsIURI **result)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*result = mURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetFinalURI(nsIURI **result)
|
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*result = mFinalURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetDestination(nsIFile **result)
|
|
|
|
{
|
2005-09-14 20:49:03 +00:00
|
|
|
if (!mDest) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*result = nullptr;
|
2005-09-14 20:49:03 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Return a clone of mDest so that callers may modify the resulting nsIFile
|
|
|
|
// without corrupting our internal object. This also works around the fact
|
|
|
|
// that some nsIFile impls may cache the result of stat'ing the filesystem.
|
|
|
|
return mDest->Clone(result);
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIncrementalDownload::GetTotalSize(int64_t *result)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
*result = mTotalSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIncrementalDownload::GetCurrentSize(int64_t *result)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
*result = mCurrentSize;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Start(nsIRequestObserver *observer,
|
|
|
|
nsISupports *context)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(observer);
|
|
|
|
NS_ENSURE_FALSE(mIsPending, NS_ERROR_IN_PROGRESS);
|
|
|
|
|
|
|
|
// Observe system shutdown so we can be sure to release any reference held
|
|
|
|
// between ourselves and the timer. We have the observer service hold a weak
|
|
|
|
// reference to us, so that we don't have to worry about calling
|
|
|
|
// RemoveObserver. XXX(darin): The timer code should do this for us.
|
Bug 560095 - Use mozilla::services::GetObserverService(). r=biesi,dveditz,gavin,josh,jst,mrbkap,roc,sdwilsh,shaver,sicking,smontagu,surkov
2010-04-29 16:59:13 +00:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2005-06-01 18:05:36 +00:00
|
|
|
if (obs)
|
2011-10-17 14:59:28 +00:00
|
|
|
obs->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, true);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
nsresult rv = ReadCurrentSize();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = StartTimer(0);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
mObserver = observer;
|
|
|
|
mObserverContext = context;
|
|
|
|
mProgressSink = do_QueryInterface(observer); // ok if null
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mIsPending = true;
|
2005-06-01 18:05:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIRequestObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::OnStartRequest(nsIRequest *request,
|
|
|
|
nsISupports *context)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(request, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Ensure that we are receiving a 206 response.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t code;
|
2005-06-01 18:05:36 +00:00
|
|
|
rv = http->GetResponseStatus(&code);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (code != 206) {
|
|
|
|
// We may already have the entire file downloaded, in which case
|
|
|
|
// our request for a range beyond the end of the file would have
|
|
|
|
// been met with an error response code.
|
2012-08-22 15:56:38 +00:00
|
|
|
if (code == 416 && mTotalSize == int64_t(-1)) {
|
2005-06-01 18:05:36 +00:00
|
|
|
mTotalSize = mCurrentSize;
|
|
|
|
// Return an error code here to suppress OnDataAvailable.
|
|
|
|
return NS_ERROR_DOWNLOAD_COMPLETE;
|
|
|
|
}
|
2005-10-20 21:45:13 +00:00
|
|
|
// The server may have decided to give us all of the data in one chunk. If
|
|
|
|
// we requested a partial range, then we don't want to download all of the
|
|
|
|
// data at once. So, we'll just try again, but if this keeps happening then
|
|
|
|
// we'll eventually give up.
|
|
|
|
if (code == 200) {
|
|
|
|
if (mInterval) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mChannel = nullptr;
|
2005-10-20 21:45:13 +00:00
|
|
|
if (++mNonPartialCount > MAX_RETRY_COUNT) {
|
|
|
|
NS_WARNING("unable to fetch a byte range; giving up");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// Increase delay with each failure.
|
|
|
|
StartTimer(mInterval * mNonPartialCount);
|
|
|
|
return NS_ERROR_DOWNLOAD_NOT_PARTIAL;
|
|
|
|
}
|
|
|
|
// Since we have been asked to download the rest of the file, we can deal
|
|
|
|
// with a 200 response. This may result in downloading the beginning of
|
|
|
|
// the file again, but that can't really be helped.
|
|
|
|
} else {
|
|
|
|
NS_WARNING("server response was unexpected");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We got a partial response, so clear this counter in case the next chunk
|
|
|
|
// results in a 200 response.
|
|
|
|
mNonPartialCount = 0;
|
2013-10-20 01:08:25 +00:00
|
|
|
|
|
|
|
// confirm that the content-range response header is consistent with
|
|
|
|
// expectations on each 206. If it is not then drop this response and
|
|
|
|
// retry with no-cache set.
|
|
|
|
if (!mCacheBust) {
|
|
|
|
nsAutoCString buf;
|
|
|
|
int64_t startByte = 0;
|
|
|
|
bool confirmedOK = false;
|
|
|
|
|
|
|
|
rv = http->GetResponseHeader(NS_LITERAL_CSTRING("Content-Range"), buf);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv; // it isn't a useful 206 without a CONTENT-RANGE of some sort
|
|
|
|
|
|
|
|
// Content-Range: bytes 0-299999/25604694
|
|
|
|
int32_t p = buf.Find("bytes ");
|
|
|
|
|
|
|
|
// first look for the starting point of the content-range
|
|
|
|
// to make sure it is what we expect
|
|
|
|
if (p != -1) {
|
|
|
|
char *endptr = nullptr;
|
|
|
|
const char *s = buf.get() + p + 6;
|
|
|
|
while (*s && *s == ' ')
|
|
|
|
s++;
|
|
|
|
startByte = strtol(s, &endptr, 10);
|
|
|
|
|
|
|
|
if (*s && endptr && (endptr != s) &&
|
|
|
|
(mCurrentSize == startByte)) {
|
|
|
|
|
|
|
|
// ok the starting point is confirmed. We still need to check the
|
|
|
|
// total size of the range for consistency if this isn't
|
|
|
|
// the first chunk
|
|
|
|
if (mTotalSize == int64_t(-1)) {
|
|
|
|
// first chunk
|
|
|
|
confirmedOK = true;
|
|
|
|
} else {
|
|
|
|
int32_t slash = buf.FindChar('/');
|
|
|
|
int64_t rangeSize = 0;
|
|
|
|
if (slash != kNotFound &&
|
|
|
|
(PR_sscanf(buf.get() + slash + 1, "%lld", (int64_t *) &rangeSize) == 1) &&
|
|
|
|
rangeSize == mTotalSize) {
|
|
|
|
confirmedOK = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!confirmedOK) {
|
|
|
|
NS_WARNING("unexpected content-range");
|
|
|
|
mCacheBust = true;
|
|
|
|
mChannel = nullptr;
|
|
|
|
if (++mNonPartialCount > MAX_RETRY_COUNT) {
|
|
|
|
NS_WARNING("unable to fetch a byte range; giving up");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// Increase delay with each failure.
|
|
|
|
StartTimer(mInterval * mNonPartialCount);
|
|
|
|
return NS_ERROR_DOWNLOAD_NOT_PARTIAL;
|
|
|
|
}
|
|
|
|
}
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do special processing after the first response.
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mTotalSize == int64_t(-1)) {
|
2005-06-01 18:05:36 +00:00
|
|
|
// Update knowledge of mFinalURI
|
|
|
|
rv = http->GetURI(getter_AddRefs(mFinalURI));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2013-10-20 01:08:25 +00:00
|
|
|
http->GetResponseHeader(NS_LITERAL_CSTRING("Etag"), mPartialValidator);
|
|
|
|
if (StringBeginsWith(mPartialValidator, NS_LITERAL_CSTRING("W/")))
|
|
|
|
mPartialValidator.Truncate(); // don't use weak validators
|
|
|
|
if (mPartialValidator.IsEmpty())
|
|
|
|
http->GetResponseHeader(NS_LITERAL_CSTRING("Last-Modified"), mPartialValidator);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2005-10-20 21:45:13 +00:00
|
|
|
if (code == 206) {
|
|
|
|
// OK, read the Content-Range header to determine the total size of this
|
|
|
|
// download file.
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString buf;
|
2005-10-20 21:45:13 +00:00
|
|
|
rv = http->GetResponseHeader(NS_LITERAL_CSTRING("Content-Range"), buf);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t slash = buf.FindChar('/');
|
2005-10-20 21:45:13 +00:00
|
|
|
if (slash == kNotFound) {
|
|
|
|
NS_WARNING("server returned invalid Content-Range header!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
if (PR_sscanf(buf.get() + slash + 1, "%lld", (int64_t *) &mTotalSize) != 1)
|
2005-10-20 21:45:13 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
} else {
|
2012-10-22 17:51:07 +00:00
|
|
|
rv = http->GetContentLength(&mTotalSize);
|
2010-08-30 20:20:39 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2005-10-20 21:45:13 +00:00
|
|
|
// We need to know the total size of the thing we're trying to download.
|
2012-08-22 15:56:38 +00:00
|
|
|
if (mTotalSize == int64_t(-1)) {
|
2005-10-20 21:45:13 +00:00
|
|
|
NS_WARNING("server returned no content-length header!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2006-06-22 00:22:53 +00:00
|
|
|
// Need to truncate (or create, if it doesn't exist) the file since we
|
|
|
|
// are downloading the whole thing.
|
2012-07-30 14:20:58 +00:00
|
|
|
WriteToFile(mDest, nullptr, 0, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE);
|
2006-06-22 00:22:53 +00:00
|
|
|
mCurrentSize = 0;
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notify observer that we are starting...
|
|
|
|
rv = CallOnStartRequest();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust mChunkSize accordingly if mCurrentSize is close to mTotalSize.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t diff = mTotalSize - mCurrentSize;
|
|
|
|
if (diff <= int64_t(0)) {
|
2006-12-27 20:17:44 +00:00
|
|
|
NS_WARNING("about to set a bogus chunk size; giving up");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
if (diff < int64_t(mChunkSize))
|
|
|
|
mChunkSize = uint32_t(diff);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2016-01-08 15:20:00 +00:00
|
|
|
mChunk = mozilla::MakeUniqueFallible<char[]>(mChunkSize);
|
2005-06-01 18:05:36 +00:00
|
|
|
if (!mChunk)
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::OnStopRequest(nsIRequest *request,
|
|
|
|
nsISupports *context,
|
|
|
|
nsresult status)
|
|
|
|
{
|
2005-10-20 21:45:13 +00:00
|
|
|
// Not a real error; just a trick to kill off the channel without our
|
|
|
|
// listener having to care.
|
|
|
|
if (status == NS_ERROR_DOWNLOAD_NOT_PARTIAL)
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
// Not a real error; just a trick used to suppress OnDataAvailable calls.
|
|
|
|
if (status == NS_ERROR_DOWNLOAD_COMPLETE)
|
|
|
|
status = NS_OK;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(mStatus))
|
|
|
|
mStatus = status;
|
|
|
|
|
|
|
|
if (mChunk) {
|
|
|
|
if (NS_SUCCEEDED(mStatus))
|
|
|
|
mStatus = FlushChunk();
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mChunk = nullptr; // deletes memory
|
2005-06-01 18:05:36 +00:00
|
|
|
mChunkLen = 0;
|
2009-06-16 08:46:40 +00:00
|
|
|
UpdateProgress();
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mChannel = nullptr;
|
2005-06-01 18:05:36 +00:00
|
|
|
|
|
|
|
// Notify listener if we hit an error or finished
|
|
|
|
if (NS_FAILED(mStatus) || mCurrentSize == mTotalSize) {
|
|
|
|
CallOnStopRequest();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StartTimer(mInterval); // Do next chunk
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIStreamListener
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::OnDataAvailable(nsIRequest *request,
|
|
|
|
nsISupports *context,
|
|
|
|
nsIInputStream *input,
|
2012-09-06 02:41:02 +00:00
|
|
|
uint64_t offset,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
while (count) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t space = mChunkSize - mChunkLen;
|
2013-01-15 12:22:03 +00:00
|
|
|
uint32_t n, len = std::min(space, count);
|
2005-06-01 18:05:36 +00:00
|
|
|
|
2015-11-09 02:28:05 +00:00
|
|
|
nsresult rv = input->Read(&mChunk[mChunkLen], len, &n);
|
2005-06-01 18:05:36 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (n != len)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
count -= n;
|
|
|
|
mChunkLen += n;
|
|
|
|
|
2013-10-22 03:22:57 +00:00
|
|
|
if (mChunkLen == mChunkSize) {
|
|
|
|
rv = FlushChunk();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2005-06-01 18:05:36 +00:00
|
|
|
}
|
|
|
|
|
2009-06-16 08:46:40 +00:00
|
|
|
if (PR_Now() > mLastProgressUpdate + UPDATE_PROGRESS_INTERVAL)
|
|
|
|
UpdateProgress();
|
|
|
|
|
2005-06-01 18:05:36 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::Observe(nsISupports *subject, const char *topic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *data)
|
2005-06-01 18:05:36 +00:00
|
|
|
{
|
|
|
|
if (strcmp(topic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Cancel(NS_ERROR_ABORT);
|
|
|
|
|
|
|
|
// Since the app is shutting down, we need to go ahead and notify our
|
|
|
|
// observer here. Otherwise, we would notify them after XPCOM has been
|
|
|
|
// shutdown or not at all.
|
|
|
|
CallOnStopRequest();
|
|
|
|
}
|
|
|
|
else if (strcmp(topic, NS_TIMER_CALLBACK_TOPIC) == 0) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mTimer = nullptr;
|
2005-06-01 18:05:36 +00:00
|
|
|
nsresult rv = ProcessTimeout();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
Cancel(rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-10 23:44:47 +00:00
|
|
|
// nsIInterfaceRequestor
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::GetInterface(const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
if (iid.Equals(NS_GET_IID(nsIChannelEventSink))) {
|
|
|
|
NS_ADDREF_THIS();
|
2007-07-08 07:08:04 +00:00
|
|
|
*result = static_cast<nsIChannelEventSink *>(this);
|
2005-10-10 23:44:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> ir = do_QueryInterface(mObserver);
|
|
|
|
if (ir)
|
|
|
|
return ir->GetInterface(iid, result);
|
|
|
|
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2006-12-27 20:17:44 +00:00
|
|
|
nsresult
|
2006-12-29 01:19:32 +00:00
|
|
|
nsIncrementalDownload::ClearRequestHeader(nsIHttpChannel *channel)
|
2006-12-27 20:17:44 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(channel);
|
2006-12-29 01:19:32 +00:00
|
|
|
|
|
|
|
// We don't support encodings -- they make the Content-Length not equal
|
|
|
|
// to the actual size of the data.
|
|
|
|
return channel->SetRequestHeader(NS_LITERAL_CSTRING("Accept-Encoding"),
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_LITERAL_CSTRING(""), false);
|
2006-12-27 20:17:44 +00:00
|
|
|
}
|
|
|
|
|
2005-10-10 23:44:47 +00:00
|
|
|
// nsIChannelEventSink
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-08-05 02:15:55 +00:00
|
|
|
nsIncrementalDownload::AsyncOnChannelRedirect(nsIChannel *oldChannel,
|
|
|
|
nsIChannel *newChannel,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags,
|
2010-08-05 02:15:55 +00:00
|
|
|
nsIAsyncVerifyRedirectCallback *cb)
|
2005-10-10 23:44:47 +00:00
|
|
|
{
|
2005-10-11 00:46:30 +00:00
|
|
|
// In response to a redirect, we need to propagate the Range header. See bug
|
2005-10-10 23:44:47 +00:00
|
|
|
// 311595. Any failure code returned from this function aborts the redirect.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(oldChannel);
|
|
|
|
NS_ENSURE_STATE(http);
|
|
|
|
|
2006-12-27 20:17:44 +00:00
|
|
|
nsCOMPtr<nsIHttpChannel> newHttpChannel = do_QueryInterface(newChannel);
|
|
|
|
NS_ENSURE_STATE(newHttpChannel);
|
|
|
|
|
2005-10-10 23:44:47 +00:00
|
|
|
NS_NAMED_LITERAL_CSTRING(rangeHdr, "Range");
|
|
|
|
|
2006-12-29 01:19:32 +00:00
|
|
|
nsresult rv = ClearRequestHeader(newHttpChannel);
|
2006-12-27 20:17:44 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2005-10-28 19:45:48 +00:00
|
|
|
|
|
|
|
// If we didn't have a Range header, then we must be doing a full download.
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString rangeVal;
|
2005-10-10 23:44:47 +00:00
|
|
|
http->GetRequestHeader(rangeHdr, rangeVal);
|
2005-10-28 19:45:48 +00:00
|
|
|
if (!rangeVal.IsEmpty()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = newHttpChannel->SetRequestHeader(rangeHdr, rangeVal, false);
|
2005-10-28 19:45:48 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2005-10-10 23:44:47 +00:00
|
|
|
|
2013-10-20 01:08:25 +00:00
|
|
|
// A redirection changes the validator
|
|
|
|
mPartialValidator.Truncate();
|
|
|
|
|
|
|
|
if (mCacheBust) {
|
|
|
|
newHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Cache-Control"),
|
|
|
|
NS_LITERAL_CSTRING("no-cache"), false);
|
|
|
|
newHttpChannel->SetRequestHeader(NS_LITERAL_CSTRING("Pragma"),
|
|
|
|
NS_LITERAL_CSTRING("no-cache"), false);
|
|
|
|
}
|
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
// Prepare to receive callback
|
|
|
|
mRedirectCallback = cb;
|
|
|
|
mNewRedirectChannel = newChannel;
|
|
|
|
|
2005-10-10 23:44:47 +00:00
|
|
|
// Give the observer a chance to see this redirect notification.
|
|
|
|
nsCOMPtr<nsIChannelEventSink> sink = do_GetInterface(mObserver);
|
2010-08-05 02:15:55 +00:00
|
|
|
if (sink) {
|
|
|
|
rv = sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags, this);
|
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
(void) OnRedirectVerifyCallback(NS_OK);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIncrementalDownload::OnRedirectVerifyCallback(nsresult result)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRedirectCallback, "mRedirectCallback not set in callback");
|
|
|
|
NS_ASSERTION(mNewRedirectChannel, "mNewRedirectChannel not set in callback");
|
2005-10-20 21:45:13 +00:00
|
|
|
|
|
|
|
// Update mChannel, so we can Cancel the new channel.
|
2010-08-05 02:15:55 +00:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
mChannel = mNewRedirectChannel;
|
2005-10-20 21:45:13 +00:00
|
|
|
|
2010-08-05 02:15:55 +00:00
|
|
|
mRedirectCallback->OnRedirectVerifyCallback(result);
|
2012-07-30 14:20:58 +00:00
|
|
|
mRedirectCallback = nullptr;
|
|
|
|
mNewRedirectChannel = nullptr;
|
2010-08-05 02:15:55 +00:00
|
|
|
return NS_OK;
|
2005-10-10 23:44:47 +00:00
|
|
|
}
|
|
|
|
|
2010-06-10 18:11:40 +00:00
|
|
|
extern nsresult
|
2005-06-01 18:05:36 +00:00
|
|
|
net_NewIncrementalDownload(nsISupports *outer, const nsIID &iid, void **result)
|
|
|
|
{
|
|
|
|
if (outer)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsIncrementalDownload *d = new nsIncrementalDownload();
|
|
|
|
if (!d)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(d);
|
|
|
|
nsresult rv = d->QueryInterface(iid, result);
|
|
|
|
NS_RELEASE(d);
|
|
|
|
return rv;
|
|
|
|
}
|