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-09-15 23:27:05 +04:00
|
|
|
|
|
|
|
#include "nsDownloader.h"
|
|
|
|
#include "nsIInputStream.h"
|
2016-01-09 04:20:50 +03:00
|
|
|
#include "nsIOutputStream.h"
|
2003-06-24 02:58:28 +04:00
|
|
|
#include "nsDirectoryServiceUtils.h"
|
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2000-09-15 23:27:05 +04:00
|
|
|
#include "nsNetUtil.h"
|
2012-04-05 20:50:55 +04:00
|
|
|
#include "nsCRTGlue.h"
|
2000-09-15 23:27:05 +04:00
|
|
|
|
2003-06-24 02:58:28 +04:00
|
|
|
nsDownloader::~nsDownloader()
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
2003-06-24 02:58:28 +04:00
|
|
|
if (mLocation && mLocationIsTemp) {
|
|
|
|
// release the sink first since it may still hold an open file
|
|
|
|
// descriptor to mLocation. this needs to happen before the
|
|
|
|
// file can be removed otherwise the Remove call will fail.
|
2007-08-25 04:26:18 +04:00
|
|
|
if (mSink) {
|
|
|
|
mSink->Close();
|
2012-07-30 18:20:58 +04:00
|
|
|
mSink = nullptr;
|
2007-08-25 04:26:18 +04:00
|
|
|
}
|
2003-06-24 02:58:28 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
nsresult rv = mLocation->Remove(false);
|
2003-06-24 02:58:28 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
NS_ERROR("unable to remove temp file");
|
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsDownloader,
|
|
|
|
nsIDownloader,
|
|
|
|
nsIStreamListener,
|
|
|
|
nsIRequestObserver)
|
2003-06-24 02:58:28 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDownloader::Init(nsIDownloadObserver *observer, nsIFile *location)
|
|
|
|
{
|
|
|
|
mObserver = observer;
|
|
|
|
mLocation = location;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt)
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
2013-01-04 03:30:48 +04:00
|
|
|
nsresult rv;
|
2003-06-24 02:58:28 +04:00
|
|
|
if (!mLocation) {
|
2013-01-04 03:30:48 +04:00
|
|
|
nsCOMPtr<nsIFile> location;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(location));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
char buf[13];
|
|
|
|
NS_MakeRandomString(buf, 8);
|
|
|
|
memcpy(buf+8, ".tmp", 5);
|
|
|
|
rv = location->AppendNative(nsDependentCString(buf, 12));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = location->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2003-06-24 02:58:28 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
location.swap(mLocation);
|
|
|
|
mLocationIsTemp = true;
|
2003-06-24 02:58:28 +04:00
|
|
|
}
|
2013-01-04 03:30:48 +04:00
|
|
|
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mSink), mLocation);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// we could wrap this output stream with a buffered output stream,
|
|
|
|
// but it shouldn't be necessary since we will be writing large
|
|
|
|
// chunks given to us via OnDataAvailable.
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-09-15 23:27:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-06-24 02:58:28 +04:00
|
|
|
nsDownloader::OnStopRequest(nsIRequest *request,
|
|
|
|
nsISupports *ctxt,
|
|
|
|
nsresult status)
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
2013-01-04 03:30:48 +04:00
|
|
|
if (mSink) {
|
2007-08-25 04:26:18 +04:00
|
|
|
mSink->Close();
|
2012-07-30 18:20:58 +04:00
|
|
|
mSink = nullptr;
|
2007-08-25 04:26:18 +04:00
|
|
|
}
|
2003-06-24 02:58:28 +04:00
|
|
|
|
2004-04-21 18:58:45 +04:00
|
|
|
mObserver->OnDownloadComplete(this, request, ctxt, status, mLocation);
|
2012-07-30 18:20:58 +04:00
|
|
|
mObserver = nullptr;
|
2003-06-24 02:58:28 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-09-15 23:27:05 +04:00
|
|
|
}
|
|
|
|
|
2016-08-12 10:36:22 +03:00
|
|
|
nsresult
|
2001-08-17 04:17:52 +04:00
|
|
|
nsDownloader::ConsumeData(nsIInputStream* in,
|
|
|
|
void* closure,
|
|
|
|
const char* fromRawSegment,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t toOffset,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t *writeCount)
|
2001-08-17 04:17:52 +04:00
|
|
|
{
|
2003-06-24 02:58:28 +04:00
|
|
|
nsDownloader *self = (nsDownloader *) closure;
|
|
|
|
if (self->mSink)
|
|
|
|
return self->mSink->Write(fromRawSegment, count, writeCount);
|
|
|
|
|
|
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
2001-08-17 04:17:52 +04:00
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
nsDownloader::OnDataAvailable(nsIRequest *request, nsISupports *ctxt,
|
2003-06-24 02:58:28 +04:00
|
|
|
nsIInputStream *inStr,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t sourceOffset, uint32_t count)
|
2000-09-15 23:27:05 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n;
|
2003-06-24 02:58:28 +04:00
|
|
|
return inStr->ReadSegments(ConsumeData, this, count, &n);
|
2000-09-15 23:27:05 +04:00
|
|
|
}
|