Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsDownloader.h" |
michael@0 | 6 | #include "nsIInputStream.h" |
michael@0 | 7 | #include "nsDirectoryServiceUtils.h" |
michael@0 | 8 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 9 | #include "nsNetUtil.h" |
michael@0 | 10 | #include "nsCRTGlue.h" |
michael@0 | 11 | |
michael@0 | 12 | nsDownloader::~nsDownloader() |
michael@0 | 13 | { |
michael@0 | 14 | if (mLocation && mLocationIsTemp) { |
michael@0 | 15 | // release the sink first since it may still hold an open file |
michael@0 | 16 | // descriptor to mLocation. this needs to happen before the |
michael@0 | 17 | // file can be removed otherwise the Remove call will fail. |
michael@0 | 18 | if (mSink) { |
michael@0 | 19 | mSink->Close(); |
michael@0 | 20 | mSink = nullptr; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | nsresult rv = mLocation->Remove(false); |
michael@0 | 24 | if (NS_FAILED(rv)) |
michael@0 | 25 | NS_ERROR("unable to remove temp file"); |
michael@0 | 26 | } |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | NS_IMPL_ISUPPORTS(nsDownloader, |
michael@0 | 30 | nsIDownloader, |
michael@0 | 31 | nsIStreamListener, |
michael@0 | 32 | nsIRequestObserver) |
michael@0 | 33 | |
michael@0 | 34 | NS_IMETHODIMP |
michael@0 | 35 | nsDownloader::Init(nsIDownloadObserver *observer, nsIFile *location) |
michael@0 | 36 | { |
michael@0 | 37 | mObserver = observer; |
michael@0 | 38 | mLocation = location; |
michael@0 | 39 | return NS_OK; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | NS_IMETHODIMP |
michael@0 | 43 | nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt) |
michael@0 | 44 | { |
michael@0 | 45 | nsresult rv; |
michael@0 | 46 | if (!mLocation) { |
michael@0 | 47 | nsCOMPtr<nsIFile> location; |
michael@0 | 48 | rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(location)); |
michael@0 | 49 | if (NS_FAILED(rv)) return rv; |
michael@0 | 50 | |
michael@0 | 51 | char buf[13]; |
michael@0 | 52 | NS_MakeRandomString(buf, 8); |
michael@0 | 53 | memcpy(buf+8, ".tmp", 5); |
michael@0 | 54 | rv = location->AppendNative(nsDependentCString(buf, 12)); |
michael@0 | 55 | if (NS_FAILED(rv)) return rv; |
michael@0 | 56 | |
michael@0 | 57 | rv = location->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); |
michael@0 | 58 | if (NS_FAILED(rv)) return rv; |
michael@0 | 59 | |
michael@0 | 60 | location.swap(mLocation); |
michael@0 | 61 | mLocationIsTemp = true; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | rv = NS_NewLocalFileOutputStream(getter_AddRefs(mSink), mLocation); |
michael@0 | 65 | if (NS_FAILED(rv)) return rv; |
michael@0 | 66 | |
michael@0 | 67 | // we could wrap this output stream with a buffered output stream, |
michael@0 | 68 | // but it shouldn't be necessary since we will be writing large |
michael@0 | 69 | // chunks given to us via OnDataAvailable. |
michael@0 | 70 | |
michael@0 | 71 | return NS_OK; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | NS_IMETHODIMP |
michael@0 | 75 | nsDownloader::OnStopRequest(nsIRequest *request, |
michael@0 | 76 | nsISupports *ctxt, |
michael@0 | 77 | nsresult status) |
michael@0 | 78 | { |
michael@0 | 79 | if (mSink) { |
michael@0 | 80 | mSink->Close(); |
michael@0 | 81 | mSink = nullptr; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | mObserver->OnDownloadComplete(this, request, ctxt, status, mLocation); |
michael@0 | 85 | mObserver = nullptr; |
michael@0 | 86 | |
michael@0 | 87 | return NS_OK; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | NS_METHOD |
michael@0 | 91 | nsDownloader::ConsumeData(nsIInputStream* in, |
michael@0 | 92 | void* closure, |
michael@0 | 93 | const char* fromRawSegment, |
michael@0 | 94 | uint32_t toOffset, |
michael@0 | 95 | uint32_t count, |
michael@0 | 96 | uint32_t *writeCount) |
michael@0 | 97 | { |
michael@0 | 98 | nsDownloader *self = (nsDownloader *) closure; |
michael@0 | 99 | if (self->mSink) |
michael@0 | 100 | return self->mSink->Write(fromRawSegment, count, writeCount); |
michael@0 | 101 | |
michael@0 | 102 | *writeCount = count; |
michael@0 | 103 | return NS_OK; |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | NS_IMETHODIMP |
michael@0 | 107 | nsDownloader::OnDataAvailable(nsIRequest *request, nsISupports *ctxt, |
michael@0 | 108 | nsIInputStream *inStr, |
michael@0 | 109 | uint64_t sourceOffset, uint32_t count) |
michael@0 | 110 | { |
michael@0 | 111 | uint32_t n; |
michael@0 | 112 | return inStr->ReadSegments(ConsumeData, this, count, &n); |
michael@0 | 113 | } |