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"
|
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() {
|
|
|
|
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;
|
2003-06-24 02:58:28 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03: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");
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
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
|
2019-05-01 11:47:10 +03:00
|
|
|
nsDownloader::Init(nsIDownloadObserver* observer, nsIFile* location) {
|
2003-06-24 02:58:28 +04:00
|
|
|
mObserver = observer;
|
|
|
|
mLocation = location;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsDownloader::OnStartRequest(nsIRequest* request) {
|
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;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
char buf[13];
|
|
|
|
NS_MakeRandomString(buf, 8);
|
|
|
|
memcpy(buf + 8, ".tmp", 5);
|
|
|
|
rv = location->AppendNative(nsDependentCString(buf, 12));
|
2003-06-24 02:58:28 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2013-01-04 03:30:48 +04:00
|
|
|
|
|
|
|
rv = location->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
location.swap(mLocation);
|
|
|
|
mLocationIsTemp = true;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-01-04 03:30:48 +04:00
|
|
|
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mSink), mLocation);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2013-01-04 03:30:48 +04:00
|
|
|
return NS_OK;
|
2000-09-15 23:27:05 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsDownloader::OnStopRequest(nsIRequest* request, nsresult status) {
|
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
|
|
|
|
2019-02-28 02:41:31 +03:00
|
|
|
mObserver->OnDownloadComplete(this, request, nullptr, 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
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult nsDownloader::ConsumeData(nsIInputStream* in, void* closure,
|
|
|
|
const char* fromRawSegment,
|
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) {
|
|
|
|
nsDownloader* self = (nsDownloader*)closure;
|
2003-06-24 02:58:28 +04:00
|
|
|
if (self->mSink) return self->mSink->Write(fromRawSegment, count, writeCount);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2003-06-24 02:58:28 +04:00
|
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
2001-08-17 04:17:52 +04:00
|
|
|
}
|
2000-09-15 23:27:05 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsDownloader::OnDataAvailable(nsIRequest* request, nsIInputStream* inStr,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t sourceOffset, uint32_t count) {
|
2017-07-06 15:00:35 +03: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
|
|
|
}
|