Back to index

lightning-sunbird  0.9+nobinonly
nsIconChannelCocoa.mm
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Brian Ryner.
00020  * Portions created by the Initial Developer are Copyright (C) 2000
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Scott MacGregor          <mscott@mozilla.org>
00025  *   Robert John Churchill    <rjc@netscape.com>
00026  *   Josh Aas                 <josh@mozilla.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 
00043 #include "nsIconChannel.h"
00044 #include "nsIIconURI.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsIInterfaceRequestor.h"
00047 #include "nsIInterfaceRequestorUtils.h"
00048 #include "nsXPIDLString.h"
00049 #include "nsMimeTypes.h"
00050 #include "nsMemory.h"
00051 #include "nsIStringStream.h"
00052 #include "nsIURL.h"
00053 #include "nsNetUtil.h"
00054 #include "nsIMIMEService.h"
00055 #include "nsCExternalHandlerService.h"
00056 #include "plstr.h"
00057 #include "nsILocalFileMac.h"
00058 #include "nsIFileURL.h"
00059 #include "nsInt64.h"
00060 #include "nsAutoBuffer.h"
00061 
00062 #include <Cocoa/Cocoa.h>
00063 
00064 // nsIconChannel methods
00065 nsIconChannel::nsIconChannel()
00066 {
00067 }
00068 
00069 nsIconChannel::~nsIconChannel() 
00070 {}
00071 
00072 NS_IMPL_THREADSAFE_ISUPPORTS4(nsIconChannel, 
00073                               nsIChannel, 
00074                               nsIRequest,
00075                             nsIRequestObserver,
00076                             nsIStreamListener)
00077 
00078 nsresult nsIconChannel::Init(nsIURI* uri)
00079 {
00080   NS_ASSERTION(uri, "no uri");
00081   mUrl = uri;
00082   
00083   nsresult rv;
00084   mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
00085   return rv;
00086 }
00087 
00089 // nsIRequest methods:
00090 
00091 NS_IMETHODIMP nsIconChannel::GetName(nsACString &result)
00092 {
00093   return mUrl->GetSpec(result);
00094 }
00095 
00096 NS_IMETHODIMP nsIconChannel::IsPending(PRBool *result)
00097 {
00098   return mPump->IsPending(result);
00099 }
00100 
00101 NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status)
00102 {
00103   return mPump->GetStatus(status);
00104 }
00105 
00106 NS_IMETHODIMP nsIconChannel::Cancel(nsresult status)
00107 {
00108   return mPump->Cancel(status);
00109 }
00110 
00111 NS_IMETHODIMP nsIconChannel::Suspend(void)
00112 {
00113   return mPump->Suspend();
00114 }
00115 
00116 NS_IMETHODIMP nsIconChannel::Resume(void)
00117 {
00118   return mPump->Resume();
00119 }
00120 
00121 // nsIRequestObserver methods
00122 NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
00123 {
00124   if (mListener)
00125     return mListener->OnStartRequest(this, aContext);
00126   return NS_OK;
00127 }
00128 
00129 NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
00130 {
00131   if (mListener) {
00132     mListener->OnStopRequest(this, aContext, aStatus);
00133     mListener = nsnull;
00134   }
00135 
00136   // Remove from load group
00137   if (mLoadGroup)
00138     mLoadGroup->RemoveRequest(this, nsnull, aStatus);
00139 
00140   return NS_OK;
00141 }
00142 
00143 // nsIStreamListener methods
00144 NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest,
00145                                              nsISupports* aContext,
00146                                              nsIInputStream* aStream,
00147                                              PRUint32 aOffset,
00148                                              PRUint32 aCount)
00149 {
00150   if (mListener)
00151     return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount);
00152   return NS_OK;
00153 }
00154 
00156 // nsIChannel methods:
00157 
00158 NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI)
00159 {
00160   *aURI = mOriginalURI ? mOriginalURI : mUrl;
00161   NS_ADDREF(*aURI);
00162   return NS_OK;
00163 }
00164 
00165 NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI)
00166 {
00167   mOriginalURI = aURI;
00168   return NS_OK;
00169 }
00170 
00171 NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI)
00172 {
00173   *aURI = mUrl;
00174   NS_IF_ADDREF(*aURI);
00175   return NS_OK;
00176 }
00177 
00178 NS_IMETHODIMP
00179 nsIconChannel::Open(nsIInputStream **_retval)
00180 {
00181   return MakeInputStream(_retval, PR_FALSE);
00182 }
00183 
00184 nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, PRUint32 * aDesiredImageSize, nsACString &aContentType, nsACString &aFileExtension)
00185 {
00186   nsresult rv = NS_OK;
00187   nsCOMPtr<nsIMozIconURI> iconURI (do_QueryInterface(mUrl, &rv));
00188   NS_ENSURE_SUCCESS(rv, rv);
00189 
00190   iconURI->GetImageSize(aDesiredImageSize);
00191   iconURI->GetContentType(aContentType);
00192   iconURI->GetFileExtension(aFileExtension);
00193   
00194   nsCOMPtr<nsIURI> fileURI;
00195   rv = iconURI->GetIconFile(getter_AddRefs(fileURI));
00196   if (NS_FAILED(rv) || !fileURI) return NS_OK;
00197 
00198   nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(fileURI, &rv);
00199   if (NS_FAILED(rv) || !fileURL) return NS_OK;
00200 
00201   nsCOMPtr<nsIFile> file;
00202   rv = fileURL->GetFile(getter_AddRefs(file));
00203   if (NS_FAILED(rv) || !file) return NS_OK;
00204   
00205   nsCOMPtr<nsILocalFileMac> localFileMac (do_QueryInterface(file, &rv));
00206   if (NS_FAILED(rv) || !localFileMac) return NS_OK;
00207   
00208   *aLocalFile = file;
00209   NS_IF_ADDREF(*aLocalFile);
00210 
00211   return NS_OK;
00212 }
00213 
00214 NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
00215 {
00216   nsCOMPtr<nsIInputStream> inStream;
00217   nsresult rv = MakeInputStream(getter_AddRefs(inStream), PR_TRUE);
00218   NS_ENSURE_SUCCESS(rv, rv);
00219 
00220   // Init our stream pump
00221   rv = mPump->Init(inStream, nsInt64(-1), nsInt64(-1), 0, 0, PR_FALSE);
00222   NS_ENSURE_SUCCESS(rv, rv);
00223   
00224   rv = mPump->AsyncRead(this, ctxt);
00225   if (NS_SUCCEEDED(rv)) {
00226     // Store our real listener
00227     mListener = aListener;
00228     // Add ourself to the load group, if available
00229     if (mLoadGroup)
00230       mLoadGroup->AddRequest(this, nsnull);
00231   }
00232 
00233   return rv;
00234 }
00235 
00236 nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, PRBool nonBlocking)
00237 {
00238   nsXPIDLCString contentType;
00239   nsCAutoString fileExt;
00240   nsCOMPtr<nsIFile> fileloc; // file we want an icon for
00241   PRUint32 desiredImageSize;
00242   nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(fileloc), &desiredImageSize, contentType, fileExt);
00243   NS_ENSURE_SUCCESS(rv, rv);
00244 
00245   // ensure that we DO NOT resolve aliases, very important for file views
00246   nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(fileloc);
00247   if (localFile)
00248     localFile->SetFollowLinks(PR_FALSE);
00249 
00250   PRBool fileExists = PR_FALSE;
00251   if (fileloc)
00252     localFile->Exists(&fileExists);
00253 
00254   NSImage* iconImage = nil;
00255   
00256   // first try to get the icon from the file if it exists
00257   if (fileExists) {
00258     nsCOMPtr<nsILocalFileMac> localFileMac(do_QueryInterface(fileloc, &rv));
00259     NS_ENSURE_SUCCESS(rv, rv);
00260     
00261     CFURLRef macURL;
00262     if (NS_SUCCEEDED(localFileMac->GetCFURL(&macURL))) {
00263       iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[(NSURL*)macURL path]];
00264       ::CFRelease(macURL);
00265     }
00266   }
00267 
00268   // try by HFS type if we don't have an icon yet
00269   if (!iconImage) {
00270     nsCOMPtr<nsIMIMEService> mimeService (do_GetService(NS_MIMESERVICE_CONTRACTID, &rv));
00271     NS_ENSURE_SUCCESS(rv, rv);
00272 
00273     // if we were given an explicit content type, use it....
00274     nsCOMPtr<nsIMIMEInfo> mimeInfo;
00275     if (mimeService && (!contentType.IsEmpty() || !fileExt.IsEmpty()))
00276       mimeService->GetFromTypeAndExtension(contentType, fileExt, getter_AddRefs(mimeInfo));
00277 
00278     if (mimeInfo) {
00279       // get the icon by HFS type
00280       PRUint32 macType;
00281       if (NS_SUCCEEDED(mimeInfo->GetMacType(&macType)))
00282         iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeForHFSTypeCode(macType)];
00283     }
00284   }
00285   
00286   // if we still don't have an icon, try to get one by extension
00287   if (!iconImage && !fileExt.IsEmpty()) {
00288     NSString* fileExtension = [NSString stringWithUTF8String:PromiseFlatCString(fileExt).get()];
00289     iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:fileExtension];
00290   }
00291   
00292   if (!iconImage)
00293     return NS_ERROR_FAILURE;
00294   
00295   // we have an icon now, size it
00296   NSRect desiredSizeRect = NSMakeRect(0, 0, desiredImageSize, desiredImageSize);
00297   [iconImage setSize:desiredSizeRect.size];
00298   
00299   [iconImage lockFocus];
00300   NSBitmapImageRep* bitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:desiredSizeRect] autorelease];
00301   [iconImage unlockFocus];
00302   
00303   // we expect the following things to be true about our bitmapRep
00304   NS_ENSURE_TRUE(![bitmapRep isPlanar] &&
00305                  (unsigned int)[bitmapRep bytesPerPlane] == desiredImageSize * desiredImageSize * 4 &&
00306                  [bitmapRep bitsPerPixel] == 32 &&
00307                  [bitmapRep samplesPerPixel] == 4 &&
00308                  [bitmapRep hasAlpha] == YES,
00309                  NS_ERROR_UNEXPECTED);
00310   
00311   // rgba, pre-multiplied data
00312   PRUint8* bitmapRepData = (PRUint8*)[bitmapRep bitmapData];
00313   
00314   // create our buffer
00315   PRInt32 bufferCapacity = 3 + desiredImageSize * desiredImageSize * 5;
00316   nsAutoBuffer<PRUint8, 3 + 16 * 16 * 5> iconBuffer; // initial size is for 16x16
00317   if (!iconBuffer.EnsureElemCapacity(bufferCapacity))
00318     return NS_ERROR_OUT_OF_MEMORY;
00319   
00320   PRUint8* iconBufferPtr = iconBuffer.get();
00321   
00322   // write header data into buffer
00323   *iconBufferPtr++ = desiredImageSize;
00324   *iconBufferPtr++ = desiredImageSize;
00325   *iconBufferPtr++ = 8; // alpha bits per pixel
00326   
00327   PRUint32 dataCount = (desiredImageSize * desiredImageSize) * 4;
00328   PRUint32 index = 0;
00329   while (index < dataCount) {
00330     // get data from the bitmap
00331     PRUint8 r = bitmapRepData[index++];
00332     PRUint8 g = bitmapRepData[index++];
00333     PRUint8 b = bitmapRepData[index++];
00334     PRUint8 a = bitmapRepData[index++];
00335     
00336     // reverse premultiplication
00337     if (a == 0) {
00338       r = g = b = 0;
00339     }
00340     else {
00341       r = ((PRUint32) r) * 255 / a;
00342       g = ((PRUint32) g) * 255 / a;
00343       b = ((PRUint32) b) * 255 / a;
00344     }
00345     
00346     // write data out to our buffer - the real alpha data is appended to the
00347     // end of the stream, the alpha here is just an unused extra channel
00348     *iconBufferPtr++ = a;
00349     *iconBufferPtr++ = r;
00350     *iconBufferPtr++ = g;
00351     *iconBufferPtr++ = b;
00352   }
00353   
00354   // add the alpha to the buffer
00355   index = 3;
00356   while (index < dataCount) {
00357     *iconBufferPtr++ = bitmapRepData[index];
00358     index += 4;
00359   }
00360   
00361   // Now, create a pipe and stuff our data into it
00362   nsCOMPtr<nsIInputStream> inStream;
00363   nsCOMPtr<nsIOutputStream> outStream;
00364   rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream), bufferCapacity, bufferCapacity, nonBlocking);  
00365 
00366   if (NS_SUCCEEDED(rv)) {
00367     PRUint32 written;
00368     rv = outStream->Write((char*)iconBuffer.get(), bufferCapacity, &written);
00369     if (NS_SUCCEEDED(rv))
00370       NS_IF_ADDREF(*_retval = inStream);
00371   }
00372 
00373   // Drop notification callbacks to prevent cycles.
00374   mCallbacks = nsnull;
00375 
00376   return NS_OK;
00377 }
00378 
00379 NS_IMETHODIMP nsIconChannel::GetLoadFlags(PRUint32 *aLoadAttributes)
00380 {
00381   return mPump->GetLoadFlags(aLoadAttributes);
00382 }
00383 
00384 NS_IMETHODIMP nsIconChannel::SetLoadFlags(PRUint32 aLoadAttributes)
00385 {
00386   return mPump->SetLoadFlags(aLoadAttributes);
00387 }
00388 
00389 NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType) 
00390 {
00391   aContentType.AssignLiteral("image/icon");
00392   return NS_OK;
00393 }
00394 
00395 NS_IMETHODIMP
00396 nsIconChannel::SetContentType(const nsACString &aContentType)
00397 {
00398   //It doesn't make sense to set the content-type on this type
00399   // of channel...
00400   return NS_ERROR_FAILURE;
00401 }
00402 
00403 NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset) 
00404 {
00405   aContentCharset.AssignLiteral("image/icon");
00406   return NS_OK;
00407 }
00408 
00409 NS_IMETHODIMP
00410 nsIconChannel::SetContentCharset(const nsACString &aContentCharset)
00411 {
00412   //It doesn't make sense to set the content-type on this type
00413   // of channel...
00414   return NS_ERROR_FAILURE;
00415 }
00416 
00417 NS_IMETHODIMP nsIconChannel::GetContentLength(PRInt32 *aContentLength)
00418 {
00419   *aContentLength = mContentLength;
00420   return NS_OK;
00421 }
00422 
00423 NS_IMETHODIMP nsIconChannel::SetContentLength(PRInt32 aContentLength)
00424 {
00425   NS_NOTREACHED("nsIconChannel::SetContentLength");
00426   return NS_ERROR_NOT_IMPLEMENTED;
00427 }
00428 
00429 NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
00430 {
00431   *aLoadGroup = mLoadGroup;
00432   NS_IF_ADDREF(*aLoadGroup);
00433   return NS_OK;
00434 }
00435 
00436 NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
00437 {
00438   mLoadGroup = aLoadGroup;
00439   return NS_OK;
00440 }
00441 
00442 NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner)
00443 {
00444   *aOwner = mOwner.get();
00445   NS_IF_ADDREF(*aOwner);
00446   return NS_OK;
00447 }
00448 
00449 NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner)
00450 {
00451   mOwner = aOwner;
00452   return NS_OK;
00453 }
00454 
00455 NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
00456 {
00457   *aNotificationCallbacks = mCallbacks.get();
00458   NS_IF_ADDREF(*aNotificationCallbacks);
00459   return NS_OK;
00460 }
00461 
00462 NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
00463 {
00464   mCallbacks = aNotificationCallbacks;
00465   return NS_OK;
00466 }
00467 
00468 NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
00469 {
00470   *aSecurityInfo = nsnull;
00471   return NS_OK;
00472 }
00473