NS_IMETHODIMP nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt) { nsresult rv; if (!mLocation) { 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); if (NS_FAILED(rv)) return rv; location.swap(mLocation); mLocationIsTemp = true; } 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; }
NS_IMETHODIMP nsDownloader::OnStartRequest(nsIRequest *request, nsISupports *ctxt) { nsresult rv = NS_ERROR_FAILURE; if (!mLocation) { nsCOMPtr<nsICachingChannel> caching = do_QueryInterface(request, &rv); if (NS_SUCCEEDED(rv)) rv = caching->SetCacheAsFile(true); } if (NS_FAILED(rv)) { // OK, we will need to stream the data to disk ourselves. Make // sure mLocation exists. if (!mLocation) { rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mLocation)); if (NS_FAILED(rv)) return rv; char buf[13]; NS_MakeRandomString(buf, 8); memcpy(buf+8, ".tmp", 5); rv = mLocation->AppendNative(nsDependentCString(buf, 12)); if (NS_FAILED(rv)) return rv; rv = mLocation->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); if (NS_FAILED(rv)) return rv; mLocationIsTemp = true; } 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 rv; }