uint32_t GetContentSize(nsIRequest* aRequest) { nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); if (channel) { int64_t size; nsresult rv = channel->GetContentLength(&size); if (NS_SUCCEEDED(rv)) { return std::max(SaturateToInt32(size), 0); } } // Use the file size as a size hint for file channels. nsCOMPtr<nsIFileChannel> fileChannel(do_QueryInterface(aRequest)); if (fileChannel) { nsCOMPtr<nsIFile> file; nsresult rv = fileChannel->GetFile(getter_AddRefs(file)); if (NS_SUCCEEDED(rv)) { int64_t filesize; rv = file->GetFileSize(&filesize); if (NS_SUCCEEDED(rv)) { return std::max(SaturateToInt32(filesize), 0); } } } // Fallback - neither http nor file. We'll use dynamic allocation. return 0; }
uint32_t GetContentSize(nsIRequest* aRequest) { // Use content-length as a size hint for http channels. nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest)); if (httpChannel) { nsAutoCString contentLength; nsresult rv = httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("content-length"), contentLength); if (NS_SUCCEEDED(rv)) { return std::max(contentLength.ToInteger(&rv), 0); } } // Use the file size as a size hint for file channels. nsCOMPtr<nsIFileChannel> fileChannel(do_QueryInterface(aRequest)); if (fileChannel) { nsCOMPtr<nsIFile> file; nsresult rv = fileChannel->GetFile(getter_AddRefs(file)); if (NS_SUCCEEDED(rv)) { int64_t filesize; rv = file->GetFileSize(&filesize); if (NS_SUCCEEDED(rv)) { return std::max(SaturateToInt32(filesize), 0); } } } // Fallback - neither http nor file. We'll use dynamic allocation. return 0; }