Back to index

lightning-sunbird  0.9+nobinonly
nsIconChannel.cpp
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@netscape.com>
00025  *   Neil Rashbrook <neil@parkwaycc.co.uk>
00026  *   Ben Goodger <ben@mozilla.org>
00027  *   Simon Taylor <simontaylor2@gawab.com>
00028  *
00029  * Alternatively, the contents of this file may be used under the terms of
00030  * either the GNU General Public License Version 2 or later (the "GPL"), or
00031  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00032  * in which case the provisions of the GPL or the LGPL are applicable instead
00033  * of those above. If you wish to allow use of your version of this file only
00034  * under the terms of either the GPL or the LGPL, and not to allow others to
00035  * use your version of this file under the terms of the MPL, indicate your
00036  * decision by deleting the provisions above and replace them with the notice
00037  * and other provisions required by the GPL or the LGPL. If you do not delete
00038  * the provisions above, a recipient may use your version of this file under
00039  * the terms of any one of the MPL, the GPL or the LGPL.
00040  *
00041  * ***** END LICENSE BLOCK ***** */
00042 
00043 
00044 #include "nsIconChannel.h"
00045 #include "nsIIconURI.h"
00046 #include "nsIServiceManager.h"
00047 #include "nsIInterfaceRequestor.h"
00048 #include "nsIInterfaceRequestorUtils.h"
00049 #include "nsXPIDLString.h"
00050 #include "nsReadableUtils.h"
00051 #include "nsMimeTypes.h"
00052 #include "nsMemory.h"
00053 #include "nsIStringStream.h"
00054 #include "nsIURL.h"
00055 #include "nsNetUtil.h"
00056 #include "nsInt64.h"
00057 #include "nsIFile.h"
00058 #include "nsIFileURL.h"
00059 #include "nsIMIMEService.h"
00060 #include "nsDirectoryServiceDefs.h"
00061 
00062 #include <Mime.h>
00063 #include <Bitmap.h>
00064 #include <Screen.h>
00065 #include <Node.h>
00066 #include <NodeInfo.h>
00067 
00068 // nsIconChannel methods
00069 nsIconChannel::nsIconChannel()
00070 {
00071 }
00072 
00073 nsIconChannel::~nsIconChannel() 
00074 {}
00075 
00076 NS_IMPL_THREADSAFE_ISUPPORTS4(nsIconChannel, 
00077                               nsIChannel, 
00078                               nsIRequest,
00079                               nsIRequestObserver,
00080                               nsIStreamListener)
00081 
00082 nsresult nsIconChannel::Init(nsIURI* uri)
00083 {
00084   NS_ASSERTION(uri, "no uri");
00085   mUrl = uri;
00086 
00087   nsresult rv;
00088   mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
00089   return rv;
00090 }
00091 
00093 // nsIRequest methods:
00094 
00095 NS_IMETHODIMP nsIconChannel::GetName(nsACString &result)
00096 {
00097   return mUrl->GetSpec(result);
00098 }
00099 
00100 NS_IMETHODIMP nsIconChannel::IsPending(PRBool *result)
00101 {
00102   return mPump->IsPending(result);
00103 }
00104 
00105 NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status)
00106 {
00107   return mPump->GetStatus(status);
00108 }
00109 
00110 NS_IMETHODIMP nsIconChannel::Cancel(nsresult status)
00111 {
00112   return mPump->Cancel(status);
00113 }
00114 
00115 NS_IMETHODIMP nsIconChannel::Suspend(void)
00116 {
00117   return mPump->Suspend();
00118 }
00119 
00120 NS_IMETHODIMP nsIconChannel::Resume(void)
00121 {
00122   return mPump->Resume();
00123 }
00124 NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
00125 {
00126   *aLoadGroup = mLoadGroup;
00127   NS_IF_ADDREF(*aLoadGroup);
00128   return NS_OK;
00129 }
00130 
00131 NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
00132 {
00133   mLoadGroup = aLoadGroup;
00134   return NS_OK;
00135 }
00136 
00137 NS_IMETHODIMP nsIconChannel::GetLoadFlags(PRUint32 *aLoadAttributes)
00138 {
00139   return mPump->GetLoadFlags(aLoadAttributes);
00140 }
00141 
00142 NS_IMETHODIMP nsIconChannel::SetLoadFlags(PRUint32 aLoadAttributes)
00143 {
00144   return mPump->SetLoadFlags(aLoadAttributes);
00145 }
00146 
00148 // nsIChannel methods:
00149 
00150 NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI)
00151 {
00152   *aURI = mOriginalURI ? mOriginalURI : mUrl;
00153   NS_ADDREF(*aURI);
00154   return NS_OK;
00155 }
00156 
00157 NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI)
00158 {
00159   mOriginalURI = aURI;
00160   return NS_OK;
00161 }
00162 
00163 NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI)
00164 {
00165   *aURI = mUrl;
00166   NS_IF_ADDREF(*aURI);
00167   return NS_OK;
00168 }
00169 
00170 NS_IMETHODIMP
00171 nsIconChannel::Open(nsIInputStream **_retval)
00172 {
00173   return MakeInputStream(_retval, PR_FALSE);
00174 }
00175 
00176 nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, PRUint32 * aDesiredImageSize, nsACString &aContentType, nsACString &aFileExtension)
00177 {
00178   nsresult rv = NS_OK;
00179   nsCOMPtr<nsIMozIconURI> iconURI (do_QueryInterface(mUrl, &rv));
00180   NS_ENSURE_SUCCESS(rv, rv);
00181 
00182   iconURI->GetImageSize(aDesiredImageSize);
00183   iconURI->GetContentType(aContentType);
00184   iconURI->GetFileExtension(aFileExtension);
00185 
00186   nsCOMPtr<nsIURI> fileURI;
00187   rv = iconURI->GetIconFile(getter_AddRefs(fileURI));
00188   if (NS_FAILED(rv) || !fileURI) return NS_OK;
00189 
00190   nsCOMPtr<nsIFileURL>    fileURL = do_QueryInterface(fileURI, &rv);
00191   if (NS_FAILED(rv) || !fileURL) return NS_OK;
00192 
00193   nsCOMPtr<nsIFile> file;
00194   rv = fileURL->GetFile(getter_AddRefs(file));
00195   if (NS_FAILED(rv) || !file) return NS_OK;
00196 
00197   *aLocalFile = file;
00198   NS_IF_ADDREF(*aLocalFile);
00199   return NS_OK;
00200 }
00201 
00202 NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
00203 {
00204   nsCOMPtr<nsIInputStream> inStream;
00205   nsresult rv = MakeInputStream(getter_AddRefs(inStream), PR_TRUE);
00206   if (NS_FAILED(rv))
00207     return rv;
00208 
00209   // Init our streampump
00210   rv = mPump->Init(inStream, nsInt64(-1), nsInt64(-1), 0, 0, PR_FALSE);
00211   if (NS_FAILED(rv))
00212     return rv;
00213 
00214   rv = mPump->AsyncRead(this, ctxt);
00215   if (NS_SUCCEEDED(rv)) {
00216     // Store our real listener
00217     mListener = aListener;
00218     // Add ourself to the load group, if available
00219     if (mLoadGroup)
00220       mLoadGroup->AddRequest(this, nsnull);
00221   }
00222   return rv;
00223 }
00224 
00225 nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, PRBool nonBlocking)
00226 {
00227   nsXPIDLCString contentType;
00228   nsCAutoString filePath;
00229   nsCAutoString fileExtension;
00230   nsCOMPtr<nsIFile> localFile; // File we want an icon for
00231   PRUint32 desiredImageSize;
00232   nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(localFile), &desiredImageSize, contentType, fileExtension);
00233   NS_ENSURE_SUCCESS(rv, rv);
00234 
00235   PRUint32 iconSize = 16;
00236   if (desiredImageSize > 16)
00237     iconSize = 32;
00238 
00239   PRUint32 alphaBytesPerRow = (iconSize / 8);
00240   if (iconSize % 32 != 0)
00241     alphaBytesPerRow = ((iconSize / 32) + 1) * 4;
00242     
00243   PRBool fileExists = PR_FALSE;
00244   if (localFile)
00245   {
00246     localFile->GetNativePath(filePath);
00247     localFile->Exists(&fileExists);
00248   }
00249   
00250   // Get the native icon.
00251   // 1) If it is for an actual local file, BNodeInfo::GetTrackerIcon.
00252   // 2) If the local file does not exist, use the content type 
00253   //    and BMimeType::GetIcon
00254   BBitmap nativeIcon(BRect(0, 0, iconSize - 1, iconSize - 1), B_CMAP8);
00255   if (!nativeIcon.IsValid())
00256     return NS_ERROR_OUT_OF_MEMORY;
00257 
00258   PRBool gotBitmap = PR_FALSE;
00259   if (fileExists)
00260   {
00261     BNode localNode(filePath.get());
00262     // BeOS doesn't MIME type foreign files immediately - 
00263     // If there is no type attribute then we can force an identify
00264     if (localNode.ReadAttr("BEOS:TYPE", B_STRING_TYPE, 0, NULL, 0) != B_OK)
00265        update_mime_info(filePath.get(), 0, 1, 1);
00266 
00267     BNodeInfo localNodeInfo(&localNode);
00268     if (iconSize == 16)
00269     {
00270       if (localNodeInfo.GetTrackerIcon(&nativeIcon, B_MINI_ICON) == B_OK)
00271         gotBitmap = PR_TRUE;
00272     }
00273     else
00274     {
00275       if (localNodeInfo.GetTrackerIcon(&nativeIcon, B_LARGE_ICON) == B_OK)
00276         gotBitmap = PR_TRUE;
00277     }
00278   }
00279 
00280   // If we haven't got a bitmap yet, use the content type
00281   if (!gotBitmap)    
00282   {
00283     // If no content type specified, use mozilla's mime service to guess a mime type
00284     if (contentType.IsEmpty())
00285     {
00286       nsCOMPtr<nsIMIMEService> mimeService (do_GetService("@mozilla.org/mime;1", &rv));
00287       if (NS_SUCCEEDED(rv))
00288         mimeService->GetTypeFromExtension(fileExtension, contentType);
00289       // If unrecognised extension - set to generic file
00290       if (contentType.IsEmpty())
00291         contentType = "application/octet-stream";
00292     }
00293     // Create BeOS-Native MIME type info - if unheard of, set to generic file
00294     BMimeType mimeType(contentType.get());
00295     if (!mimeType.IsInstalled())
00296        mimeType.SetTo("application/octet-stream");
00297     if (iconSize == 16)
00298     {
00299       if (mimeType.GetIcon(&nativeIcon, B_MINI_ICON) == B_OK)
00300         gotBitmap = PR_TRUE;
00301     }
00302     else
00303     {
00304       if (mimeType.GetIcon(&nativeIcon, B_LARGE_ICON) == B_OK)
00305         gotBitmap = PR_TRUE;
00306     }
00307   }
00308   
00309   if (!gotBitmap)
00310     return NS_ERROR_NOT_AVAILABLE;
00311 
00312   BScreen mainScreen(B_MAIN_SCREEN_ID);
00313   if (!mainScreen.IsValid())
00314     return NS_ERROR_NOT_AVAILABLE;
00315 
00316   // Got a bitmap and color space info - convert data to mozilla's icon format
00317   PRUint32 iconLength = 3 + iconSize * (iconSize * 3 + alphaBytesPerRow);
00318   uint8 *buffer = new uint8[iconLength];
00319   if (!buffer)
00320     return NS_ERROR_OUT_OF_MEMORY;
00321 
00322   uint8* destByte = buffer;
00323   *(destByte++) = iconSize;
00324   *(destByte++) = iconSize;
00325   *(destByte++) = 1; // alpha bits per pixel
00326 
00327   // RGB data
00328   uint8* sourceByte = (uint8*)nativeIcon.Bits();
00329   for(PRUint32 iconRow = 0; iconRow < iconSize; iconRow++)
00330   {
00331     sourceByte = (uint8*)nativeIcon.Bits() + nativeIcon.BytesPerRow() * iconRow;
00332     for(PRUint32 iconCol = 0; iconCol < iconSize; iconCol++)
00333     {
00334       if (*sourceByte != B_TRANSPARENT_MAGIC_CMAP8)
00335       {
00336         rgb_color colorVal = mainScreen.ColorForIndex(*sourceByte);
00337         *(destByte++) = colorVal.blue;
00338         *(destByte++) = colorVal.green;
00339         *(destByte++) = colorVal.red;
00340       }
00341       else
00342       {
00343         *destByte++ = 0;
00344         *destByte++ = 0;
00345         *destByte++ = 0;
00346       }
00347       // original code had a conditional here:
00348       // if (iconCol < iconSize - 1) 
00349       // Leaving this comment in case complications arise later
00350       sourceByte++;
00351     }
00352   }
00353   // Alpha data - bitmask, with rows aligned on 32-bit boundaries
00354   for(PRUint32 iconRow = 0; iconRow < iconSize; iconRow++)
00355   {
00356     destByte = buffer + 3 + iconSize * iconSize * 3 + iconRow * alphaBytesPerRow;
00357     sourceByte = (uint8*)nativeIcon.Bits() + nativeIcon.BytesPerRow() * iconRow;
00358     int bitNo = 0;
00359     for(PRUint32 iconCol = 0; iconCol < iconSize; iconCol++)
00360     {
00361       if (*sourceByte == B_TRANSPARENT_MAGIC_CMAP8)
00362         (*destByte) &= (~(128 >> (bitNo % 8)));
00363       else
00364         (*destByte) |= (128 >> (bitNo % 8));
00365       
00366       //original code had a conditional here:
00367       //if (iconCol < iconSize - 1) 
00368       //Leaving this comment in case complications arise later
00369       bitNo++;
00370       sourceByte++;
00371       if ((bitNo%8) == 0)
00372         destByte++;
00373     }
00374   }
00375   
00376   // Now, create a pipe and stuff our data into it
00377   nsCOMPtr<nsIInputStream> inStream;
00378   nsCOMPtr<nsIOutputStream> outStream;
00379   rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream),
00380                   iconLength, iconLength, nonBlocking);
00381   if (NS_SUCCEEDED(rv))
00382   {
00383     PRUint32 written;
00384     rv = outStream->Write((char*)buffer, iconLength, &written);
00385     if (NS_SUCCEEDED(rv))
00386       NS_ADDREF(*_retval = inStream);
00387   }
00388   delete [] buffer;
00389   
00390   return rv;
00391 }
00392 
00393 NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType) 
00394 {
00395   aContentType.AssignLiteral("image/icon");
00396   return NS_OK;
00397 }
00398 
00399 NS_IMETHODIMP
00400 nsIconChannel::SetContentType(const nsACString &aContentType)
00401 {
00402   // It doesn't make sense to set the content-type on this type
00403   // of channel...
00404   return NS_ERROR_FAILURE;
00405 }
00406 
00407 NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset) 
00408 {
00409   aContentCharset.Truncate();
00410   return NS_OK;
00411 }
00412 
00413 NS_IMETHODIMP
00414 nsIconChannel::SetContentCharset(const nsACString &aContentCharset)
00415 {
00416   // It doesn't make sense to set the content-charset on this type
00417   // of channel...
00418   return NS_ERROR_FAILURE;
00419 }
00420 
00421 NS_IMETHODIMP nsIconChannel::GetContentLength(PRInt32 *aContentLength)
00422 {
00423   *aContentLength = mContentLength;
00424   return NS_OK;
00425 }
00426 
00427 NS_IMETHODIMP nsIconChannel::SetContentLength(PRInt32 aContentLength)
00428 {
00429   NS_NOTREACHED("nsIconChannel::SetContentLength");
00430   return NS_ERROR_NOT_IMPLEMENTED;
00431 }
00432 
00433 NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner)
00434 {
00435   *aOwner = mOwner.get();
00436   NS_IF_ADDREF(*aOwner);
00437   return NS_OK;
00438 }
00439 
00440 NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner)
00441 {
00442   mOwner = aOwner;
00443   return NS_OK;
00444 }
00445 
00446 NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
00447 {
00448   *aNotificationCallbacks = mCallbacks.get();
00449   NS_IF_ADDREF(*aNotificationCallbacks);
00450   return NS_OK;
00451 }
00452 
00453 NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
00454 {
00455   mCallbacks = aNotificationCallbacks;
00456   return NS_OK;
00457 }
00458 
00459 NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
00460 {
00461   *aSecurityInfo = nsnull;
00462   return NS_OK;
00463 }
00464 
00465 // nsIRequestObserver methods
00466 NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
00467 {
00468   if (mListener)
00469     return mListener->OnStartRequest(this, aContext);
00470   return NS_OK;
00471 }
00472 
00473 NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
00474 {
00475   if (mListener) {
00476     mListener->OnStopRequest(this, aContext, aStatus);
00477     mListener = nsnull;
00478   }
00479 
00480   // Remove from load group
00481   if (mLoadGroup)
00482     mLoadGroup->RemoveRequest(this, nsnull, aStatus);
00483 
00484   return NS_OK;
00485 }
00486 
00487 // nsIStreamListener methods
00488 NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest,
00489                                              nsISupports* aContext,
00490                                              nsIInputStream* aStream,
00491                                              PRUint32 aOffset,
00492                                              PRUint32 aCount)
00493 {
00494   if (mListener)
00495     return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount);
00496   return NS_OK;
00497 }