michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsIconChannel.h" michael@0: #include "mozilla/Endian.h" michael@0: #include "nsIIconURI.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIInterfaceRequestor.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsXPIDLString.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsMemory.h" michael@0: #include "nsIStringStream.h" michael@0: #include "nsIURL.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIMIMEService.h" michael@0: #include "nsCExternalHandlerService.h" michael@0: #include "nsILocalFileMac.h" michael@0: #include "nsIFileURL.h" michael@0: #include "nsTArray.h" michael@0: #include "nsObjCExceptions.h" michael@0: michael@0: #include michael@0: michael@0: // nsIconChannel methods michael@0: nsIconChannel::nsIconChannel() michael@0: { michael@0: } michael@0: michael@0: nsIconChannel::~nsIconChannel() michael@0: {} michael@0: michael@0: NS_IMPL_ISUPPORTS(nsIconChannel, michael@0: nsIChannel, michael@0: nsIRequest, michael@0: nsIRequestObserver, michael@0: nsIStreamListener) michael@0: michael@0: nsresult nsIconChannel::Init(nsIURI* uri) michael@0: { michael@0: NS_ASSERTION(uri, "no uri"); michael@0: mUrl = uri; michael@0: mOriginalURI = uri; michael@0: nsresult rv; michael@0: mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv); michael@0: return rv; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // nsIRequest methods: michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetName(nsACString &result) michael@0: { michael@0: return mUrl->GetSpec(result); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::IsPending(bool *result) michael@0: { michael@0: return mPump->IsPending(result); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status) michael@0: { michael@0: return mPump->GetStatus(status); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::Cancel(nsresult status) michael@0: { michael@0: return mPump->Cancel(status); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::Suspend(void) michael@0: { michael@0: return mPump->Suspend(); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::Resume(void) michael@0: { michael@0: return mPump->Resume(); michael@0: } michael@0: michael@0: // nsIRequestObserver methods michael@0: NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext) michael@0: { michael@0: if (mListener) michael@0: return mListener->OnStartRequest(this, aContext); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus) michael@0: { michael@0: if (mListener) { michael@0: mListener->OnStopRequest(this, aContext, aStatus); michael@0: mListener = nullptr; michael@0: } michael@0: michael@0: // Remove from load group michael@0: if (mLoadGroup) michael@0: mLoadGroup->RemoveRequest(this, nullptr, aStatus); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // nsIStreamListener methods michael@0: NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest, michael@0: nsISupports* aContext, michael@0: nsIInputStream* aStream, michael@0: uint64_t aOffset, michael@0: uint32_t aCount) michael@0: { michael@0: if (mListener) michael@0: return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: // nsIChannel methods: michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI) michael@0: { michael@0: *aURI = mOriginalURI; michael@0: NS_ADDREF(*aURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: mOriginalURI = aURI; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI) michael@0: { michael@0: *aURI = mUrl; michael@0: NS_IF_ADDREF(*aURI); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::Open(nsIInputStream **_retval) michael@0: { michael@0: return MakeInputStream(_retval, false); michael@0: } michael@0: michael@0: nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, uint32_t * aDesiredImageSize, nsACString &aContentType, nsACString &aFileExtension) michael@0: { michael@0: nsresult rv = NS_OK; michael@0: nsCOMPtr iconURI (do_QueryInterface(mUrl, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: iconURI->GetImageSize(aDesiredImageSize); michael@0: iconURI->GetContentType(aContentType); michael@0: iconURI->GetFileExtension(aFileExtension); michael@0: michael@0: nsCOMPtr url; michael@0: rv = iconURI->GetIconURL(getter_AddRefs(url)); michael@0: if (NS_FAILED(rv) || !url) return NS_OK; michael@0: michael@0: nsCOMPtr fileURL = do_QueryInterface(url, &rv); michael@0: if (NS_FAILED(rv) || !fileURL) return NS_OK; michael@0: michael@0: nsCOMPtr file; michael@0: rv = fileURL->GetFile(getter_AddRefs(file)); michael@0: if (NS_FAILED(rv) || !file) return NS_OK; michael@0: michael@0: nsCOMPtr localFileMac (do_QueryInterface(file, &rv)); michael@0: if (NS_FAILED(rv) || !localFileMac) return NS_OK; michael@0: michael@0: *aLocalFile = file; michael@0: NS_IF_ADDREF(*aLocalFile); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt) michael@0: { michael@0: nsCOMPtr inStream; michael@0: nsresult rv = MakeInputStream(getter_AddRefs(inStream), true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Init our stream pump michael@0: rv = mPump->Init(inStream, int64_t(-1), int64_t(-1), 0, 0, false); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = mPump->AsyncRead(this, ctxt); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: // Store our real listener michael@0: mListener = aListener; michael@0: // Add ourself to the load group, if available michael@0: if (mLoadGroup) michael@0: mLoadGroup->AddRequest(this, nullptr); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, bool nonBlocking) michael@0: { michael@0: NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; michael@0: michael@0: nsXPIDLCString contentType; michael@0: nsAutoCString fileExt; michael@0: nsCOMPtr fileloc; // file we want an icon for michael@0: uint32_t desiredImageSize; michael@0: nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(fileloc), &desiredImageSize, contentType, fileExt); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool fileExists = false; michael@0: if (fileloc) { michael@0: // ensure that we DO NOT resolve aliases, very important for file views michael@0: fileloc->SetFollowLinks(false); michael@0: fileloc->Exists(&fileExists); michael@0: } michael@0: michael@0: NSImage* iconImage = nil; michael@0: michael@0: // first try to get the icon from the file if it exists michael@0: if (fileExists) { michael@0: nsCOMPtr localFileMac(do_QueryInterface(fileloc, &rv)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: CFURLRef macURL; michael@0: if (NS_SUCCEEDED(localFileMac->GetCFURL(&macURL))) { michael@0: iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[(NSURL*)macURL path]]; michael@0: ::CFRelease(macURL); michael@0: } michael@0: } michael@0: michael@0: // if we don't have an icon yet try to get one by extension michael@0: if (!iconImage && !fileExt.IsEmpty()) { michael@0: NSString* fileExtension = [NSString stringWithUTF8String:fileExt.get()]; michael@0: iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:fileExtension]; michael@0: } michael@0: michael@0: // If we still don't have an icon, get the generic document icon. michael@0: if (!iconImage) michael@0: iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeUnknown]; michael@0: michael@0: if (!iconImage) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // we have an icon now, size it michael@0: NSRect desiredSizeRect = NSMakeRect(0, 0, desiredImageSize, desiredImageSize); michael@0: [iconImage setSize:desiredSizeRect.size]; michael@0: michael@0: [iconImage lockFocus]; michael@0: NSBitmapImageRep* bitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:desiredSizeRect] autorelease]; michael@0: [iconImage unlockFocus]; michael@0: michael@0: // we expect the following things to be true about our bitmapRep michael@0: NS_ENSURE_TRUE(![bitmapRep isPlanar] && michael@0: // Not necessarily: on a HiDPI-capable system, we'll get a 2x bitmap michael@0: // (unsigned int)[bitmapRep bytesPerPlane] == desiredImageSize * desiredImageSize * 4 && michael@0: [bitmapRep bitsPerPixel] == 32 && michael@0: [bitmapRep samplesPerPixel] == 4 && michael@0: [bitmapRep hasAlpha] == YES, michael@0: NS_ERROR_UNEXPECTED); michael@0: michael@0: // check what size we actually got, and ensure it isn't too big to return michael@0: uint32_t actualImageSize = [bitmapRep bytesPerRow] / 4; michael@0: NS_ENSURE_TRUE(actualImageSize < 256, NS_ERROR_UNEXPECTED); michael@0: michael@0: // now we can validate the amount of data michael@0: NS_ENSURE_TRUE((unsigned int)[bitmapRep bytesPerPlane] == actualImageSize * actualImageSize * 4, michael@0: NS_ERROR_UNEXPECTED); michael@0: michael@0: // rgba, pre-multiplied data michael@0: uint8_t* bitmapRepData = (uint8_t*)[bitmapRep bitmapData]; michael@0: michael@0: // create our buffer michael@0: int32_t bufferCapacity = 2 + [bitmapRep bytesPerPlane]; michael@0: nsAutoTArray iconBuffer; // initial size is for 16x16 michael@0: iconBuffer.SetLength(bufferCapacity); michael@0: michael@0: uint8_t* iconBufferPtr = iconBuffer.Elements(); michael@0: michael@0: // write header data into buffer michael@0: *iconBufferPtr++ = actualImageSize; michael@0: *iconBufferPtr++ = actualImageSize; michael@0: michael@0: uint32_t dataCount = [bitmapRep bytesPerPlane]; michael@0: uint32_t index = 0; michael@0: while (index < dataCount) { michael@0: // get data from the bitmap michael@0: uint8_t r = bitmapRepData[index++]; michael@0: uint8_t g = bitmapRepData[index++]; michael@0: uint8_t b = bitmapRepData[index++]; michael@0: uint8_t a = bitmapRepData[index++]; michael@0: michael@0: // write data out to our buffer michael@0: // non-cairo uses native image format, but the A channel is ignored. michael@0: // cairo uses ARGB (highest to lowest bits) michael@0: #if MOZ_LITTLE_ENDIAN michael@0: *iconBufferPtr++ = b; michael@0: *iconBufferPtr++ = g; michael@0: *iconBufferPtr++ = r; michael@0: *iconBufferPtr++ = a; michael@0: #else michael@0: *iconBufferPtr++ = a; michael@0: *iconBufferPtr++ = r; michael@0: *iconBufferPtr++ = g; michael@0: *iconBufferPtr++ = b; michael@0: #endif michael@0: } michael@0: michael@0: NS_ASSERTION(iconBufferPtr == iconBuffer.Elements() + bufferCapacity, michael@0: "buffer size miscalculation"); michael@0: michael@0: // Now, create a pipe and stuff our data into it michael@0: nsCOMPtr inStream; michael@0: nsCOMPtr outStream; michael@0: rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream), michael@0: bufferCapacity, bufferCapacity, nonBlocking); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: uint32_t written; michael@0: rv = outStream->Write((char*)iconBuffer.Elements(), bufferCapacity, &written); michael@0: if (NS_SUCCEEDED(rv)) michael@0: NS_IF_ADDREF(*_retval = inStream); michael@0: } michael@0: michael@0: // Drop notification callbacks to prevent cycles. michael@0: mCallbacks = nullptr; michael@0: michael@0: return NS_OK; michael@0: michael@0: NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetLoadFlags(uint32_t *aLoadAttributes) michael@0: { michael@0: return mPump->GetLoadFlags(aLoadAttributes); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetLoadFlags(uint32_t aLoadAttributes) michael@0: { michael@0: return mPump->SetLoadFlags(aLoadAttributes); michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType) michael@0: { michael@0: aContentType.AssignLiteral(IMAGE_ICON_MS); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::SetContentType(const nsACString &aContentType) michael@0: { michael@0: //It doesn't make sense to set the content-type on this type michael@0: // of channel... michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset) michael@0: { michael@0: aContentCharset.AssignLiteral(IMAGE_ICON_MS); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::SetContentCharset(const nsACString &aContentCharset) michael@0: { michael@0: //It doesn't make sense to set the content-type on this type michael@0: // of channel... michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::GetContentDisposition(uint32_t *aContentDisposition) michael@0: { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::SetContentDisposition(uint32_t aContentDisposition) michael@0: { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename) michael@0: { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::SetContentDispositionFilename(const nsAString &aContentDispositionFilename) michael@0: { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsIconChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader) michael@0: { michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetContentLength(int64_t *aContentLength) michael@0: { michael@0: *aContentLength = mContentLength; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetContentLength(int64_t aContentLength) michael@0: { michael@0: NS_NOTREACHED("nsIconChannel::SetContentLength"); michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup) michael@0: { michael@0: *aLoadGroup = mLoadGroup; michael@0: NS_IF_ADDREF(*aLoadGroup); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup) michael@0: { michael@0: mLoadGroup = aLoadGroup; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner) michael@0: { michael@0: *aOwner = mOwner.get(); michael@0: NS_IF_ADDREF(*aOwner); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner) michael@0: { michael@0: mOwner = aOwner; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks) michael@0: { michael@0: *aNotificationCallbacks = mCallbacks.get(); michael@0: NS_IF_ADDREF(*aNotificationCallbacks); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks) michael@0: { michael@0: mCallbacks = aNotificationCallbacks; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo) michael@0: { michael@0: *aSecurityInfo = nullptr; michael@0: return NS_OK; michael@0: } michael@0: