PageRenderTime 85ms CodeModel.GetById 73ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/image/decoders/icon/mac/nsIconChannelCocoa.mm

http://github.com/zpao/v8monkey
Objective C++ | 475 lines | 325 code | 80 blank | 70 comment | 31 complexity | 4e15b99331768609885841100eb12247 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2 *
  3 * ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is
 19 * Brian Ryner.
 20 * Portions created by the Initial Developer are Copyright (C) 2000
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *   Scott MacGregor          <mscott@mozilla.org>
 25 *   Robert John Churchill    <rjc@netscape.com>
 26 *   Josh Aas                 <josh@mozilla.com>
 27 *
 28 * Alternatively, the contents of this file may be used under the terms of
 29 * either the GNU General Public License Version 2 or later (the "GPL"), or
 30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 31 * in which case the provisions of the GPL or the LGPL are applicable instead
 32 * of those above. If you wish to allow use of your version of this file only
 33 * under the terms of either the GPL or the LGPL, and not to allow others to
 34 * use your version of this file under the terms of the MPL, indicate your
 35 * decision by deleting the provisions above and replace them with the notice
 36 * and other provisions required by the GPL or the LGPL. If you do not delete
 37 * the provisions above, a recipient may use your version of this file under
 38 * the terms of any one of the MPL, the GPL or the LGPL.
 39 *
 40 * ***** END LICENSE BLOCK ***** */
 41
 42
 43#include "nsIconChannel.h"
 44#include "nsIIconURI.h"
 45#include "nsIServiceManager.h"
 46#include "nsIInterfaceRequestor.h"
 47#include "nsIInterfaceRequestorUtils.h"
 48#include "nsXPIDLString.h"
 49#include "nsMimeTypes.h"
 50#include "nsMemory.h"
 51#include "nsIStringStream.h"
 52#include "nsIURL.h"
 53#include "nsNetUtil.h"
 54#include "nsIMIMEService.h"
 55#include "nsCExternalHandlerService.h"
 56#include "plstr.h"
 57#include "nsILocalFileMac.h"
 58#include "nsIFileURL.h"
 59#include "nsTArray.h"
 60#include "nsObjCExceptions.h"
 61
 62#include <Cocoa/Cocoa.h>
 63
 64// nsIconChannel methods
 65nsIconChannel::nsIconChannel()
 66{
 67}
 68
 69nsIconChannel::~nsIconChannel() 
 70{}
 71
 72NS_IMPL_THREADSAFE_ISUPPORTS4(nsIconChannel, 
 73                              nsIChannel, 
 74                              nsIRequest,
 75			       nsIRequestObserver,
 76			       nsIStreamListener)
 77
 78nsresult nsIconChannel::Init(nsIURI* uri)
 79{
 80  NS_ASSERTION(uri, "no uri");
 81  mUrl = uri;
 82  mOriginalURI = uri;
 83  nsresult rv;
 84  mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
 85  return rv;
 86}
 87
 88////////////////////////////////////////////////////////////////////////////////
 89// nsIRequest methods:
 90
 91NS_IMETHODIMP nsIconChannel::GetName(nsACString &result)
 92{
 93  return mUrl->GetSpec(result);
 94}
 95
 96NS_IMETHODIMP nsIconChannel::IsPending(bool *result)
 97{
 98  return mPump->IsPending(result);
 99}
100
101NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status)
102{
103  return mPump->GetStatus(status);
104}
105
106NS_IMETHODIMP nsIconChannel::Cancel(nsresult status)
107{
108  return mPump->Cancel(status);
109}
110
111NS_IMETHODIMP nsIconChannel::Suspend(void)
112{
113  return mPump->Suspend();
114}
115
116NS_IMETHODIMP nsIconChannel::Resume(void)
117{
118  return mPump->Resume();
119}
120
121// nsIRequestObserver methods
122NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
123{
124  if (mListener)
125    return mListener->OnStartRequest(this, aContext);
126  return NS_OK;
127}
128
129NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
130{
131  if (mListener) {
132    mListener->OnStopRequest(this, aContext, aStatus);
133    mListener = nsnull;
134  }
135
136  // Remove from load group
137  if (mLoadGroup)
138    mLoadGroup->RemoveRequest(this, nsnull, aStatus);
139
140  return NS_OK;
141}
142
143// nsIStreamListener methods
144NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest,
145                                             nsISupports* aContext,
146                                             nsIInputStream* aStream,
147                                             PRUint32 aOffset,
148                                             PRUint32 aCount)
149{
150  if (mListener)
151    return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount);
152  return NS_OK;
153}
154
155////////////////////////////////////////////////////////////////////////////////
156// nsIChannel methods:
157
158NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI)
159{
160  *aURI = mOriginalURI;
161  NS_ADDREF(*aURI);
162  return NS_OK;
163}
164
165NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI)
166{
167  NS_ENSURE_ARG_POINTER(aURI);
168  mOriginalURI = aURI;
169  return NS_OK;
170}
171
172NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI)
173{
174  *aURI = mUrl;
175  NS_IF_ADDREF(*aURI);
176  return NS_OK;
177}
178
179NS_IMETHODIMP
180nsIconChannel::Open(nsIInputStream **_retval)
181{
182  return MakeInputStream(_retval, false);
183}
184
185nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, PRUint32 * aDesiredImageSize, nsACString &aContentType, nsACString &aFileExtension)
186{
187  nsresult rv = NS_OK;
188  nsCOMPtr<nsIMozIconURI> iconURI (do_QueryInterface(mUrl, &rv));
189  NS_ENSURE_SUCCESS(rv, rv);
190
191  iconURI->GetImageSize(aDesiredImageSize);
192  iconURI->GetContentType(aContentType);
193  iconURI->GetFileExtension(aFileExtension);
194  
195  nsCOMPtr<nsIURL> url;
196  rv = iconURI->GetIconURL(getter_AddRefs(url));
197  if (NS_FAILED(rv) || !url) return NS_OK;
198
199  nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(url, &rv);
200  if (NS_FAILED(rv) || !fileURL) return NS_OK;
201
202  nsCOMPtr<nsIFile> file;
203  rv = fileURL->GetFile(getter_AddRefs(file));
204  if (NS_FAILED(rv) || !file) return NS_OK;
205  
206  nsCOMPtr<nsILocalFileMac> localFileMac (do_QueryInterface(file, &rv));
207  if (NS_FAILED(rv) || !localFileMac) return NS_OK;
208  
209  *aLocalFile = file;
210  NS_IF_ADDREF(*aLocalFile);
211
212  return NS_OK;
213}
214
215NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
216{
217  nsCOMPtr<nsIInputStream> inStream;
218  nsresult rv = MakeInputStream(getter_AddRefs(inStream), true);
219  NS_ENSURE_SUCCESS(rv, rv);
220
221  // Init our stream pump
222  rv = mPump->Init(inStream, PRInt64(-1), PRInt64(-1), 0, 0, false);
223  NS_ENSURE_SUCCESS(rv, rv);
224  
225  rv = mPump->AsyncRead(this, ctxt);
226  if (NS_SUCCEEDED(rv)) {
227    // Store our real listener
228    mListener = aListener;
229    // Add ourself to the load group, if available
230    if (mLoadGroup)
231      mLoadGroup->AddRequest(this, nsnull);
232  }
233
234  return rv;
235}
236
237nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, bool nonBlocking)
238{
239  NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
240
241  nsXPIDLCString contentType;
242  nsCAutoString fileExt;
243  nsCOMPtr<nsIFile> fileloc; // file we want an icon for
244  PRUint32 desiredImageSize;
245  nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(fileloc), &desiredImageSize, contentType, fileExt);
246  NS_ENSURE_SUCCESS(rv, rv);
247
248  // ensure that we DO NOT resolve aliases, very important for file views
249  nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(fileloc);
250  if (localFile)
251    localFile->SetFollowLinks(false);
252
253  bool fileExists = false;
254  if (fileloc)
255    localFile->Exists(&fileExists);
256
257  NSImage* iconImage = nil;
258  
259  // first try to get the icon from the file if it exists
260  if (fileExists) {
261    nsCOMPtr<nsILocalFileMac> localFileMac(do_QueryInterface(fileloc, &rv));
262    NS_ENSURE_SUCCESS(rv, rv);
263    
264    CFURLRef macURL;
265    if (NS_SUCCEEDED(localFileMac->GetCFURL(&macURL))) {
266      iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[(NSURL*)macURL path]];
267      ::CFRelease(macURL);
268    }
269  }
270
271  // if we don't have an icon yet try to get one by extension
272  if (!iconImage && !fileExt.IsEmpty()) {
273    NSString* fileExtension = [NSString stringWithUTF8String:PromiseFlatCString(fileExt).get()];
274    iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:fileExtension];
275  }
276
277  // If we still don't have an icon, get the generic document icon.
278  if (!iconImage)
279    iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeUnknown];
280
281  if (!iconImage)
282    return NS_ERROR_FAILURE;
283  
284  // we have an icon now, size it
285  NSRect desiredSizeRect = NSMakeRect(0, 0, desiredImageSize, desiredImageSize);
286  [iconImage setSize:desiredSizeRect.size];
287  
288  [iconImage lockFocus];
289  NSBitmapImageRep* bitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:desiredSizeRect] autorelease];
290  [iconImage unlockFocus];
291  
292  // we expect the following things to be true about our bitmapRep
293  NS_ENSURE_TRUE(![bitmapRep isPlanar] &&
294                 (unsigned int)[bitmapRep bytesPerPlane] == desiredImageSize * desiredImageSize * 4 &&
295                 [bitmapRep bitsPerPixel] == 32 &&
296                 [bitmapRep samplesPerPixel] == 4 &&
297                 [bitmapRep hasAlpha] == YES,
298                 NS_ERROR_UNEXPECTED);
299  
300  // rgba, pre-multiplied data
301  PRUint8* bitmapRepData = (PRUint8*)[bitmapRep bitmapData];
302  
303  // create our buffer
304  PRInt32 bufferCapacity = 2 + desiredImageSize * desiredImageSize * 4;
305  nsAutoTArray<PRUint8, 3 + 16 * 16 * 5> iconBuffer; // initial size is for 16x16
306  if (!iconBuffer.SetLength(bufferCapacity))
307    return NS_ERROR_OUT_OF_MEMORY;
308  
309  PRUint8* iconBufferPtr = iconBuffer.Elements();
310  
311  // write header data into buffer
312  *iconBufferPtr++ = desiredImageSize;
313  *iconBufferPtr++ = desiredImageSize;
314
315  PRUint32 dataCount = (desiredImageSize * desiredImageSize) * 4;
316  PRUint32 index = 0;
317  while (index < dataCount) {
318    // get data from the bitmap
319    PRUint8 r = bitmapRepData[index++];
320    PRUint8 g = bitmapRepData[index++];
321    PRUint8 b = bitmapRepData[index++];
322    PRUint8 a = bitmapRepData[index++];
323
324    // write data out to our buffer
325    // non-cairo uses native image format, but the A channel is ignored.
326    // cairo uses ARGB (highest to lowest bits)
327#if defined(IS_LITTLE_ENDIAN)
328    *iconBufferPtr++ = b;
329    *iconBufferPtr++ = g;
330    *iconBufferPtr++ = r;
331    *iconBufferPtr++ = a;
332#else
333    *iconBufferPtr++ = a;
334    *iconBufferPtr++ = r;
335    *iconBufferPtr++ = g;
336    *iconBufferPtr++ = b;
337#endif
338  }
339
340  NS_ASSERTION(iconBufferPtr == iconBuffer.Elements() + bufferCapacity,
341               "buffer size miscalculation");
342  
343  // Now, create a pipe and stuff our data into it
344  nsCOMPtr<nsIInputStream> inStream;
345  nsCOMPtr<nsIOutputStream> outStream;
346  rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream), bufferCapacity, bufferCapacity, nonBlocking);  
347
348  if (NS_SUCCEEDED(rv)) {
349    PRUint32 written;
350    rv = outStream->Write((char*)iconBuffer.Elements(), bufferCapacity, &written);
351    if (NS_SUCCEEDED(rv))
352      NS_IF_ADDREF(*_retval = inStream);
353  }
354
355  // Drop notification callbacks to prevent cycles.
356  mCallbacks = nsnull;
357
358  return NS_OK;
359
360  NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
361}
362
363NS_IMETHODIMP nsIconChannel::GetLoadFlags(PRUint32 *aLoadAttributes)
364{
365  return mPump->GetLoadFlags(aLoadAttributes);
366}
367
368NS_IMETHODIMP nsIconChannel::SetLoadFlags(PRUint32 aLoadAttributes)
369{
370  return mPump->SetLoadFlags(aLoadAttributes);
371}
372
373NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType) 
374{
375  aContentType.AssignLiteral("image/icon");
376  return NS_OK;
377}
378
379NS_IMETHODIMP
380nsIconChannel::SetContentType(const nsACString &aContentType)
381{
382  //It doesn't make sense to set the content-type on this type
383  // of channel...
384  return NS_ERROR_FAILURE;
385}
386
387NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset) 
388{
389  aContentCharset.AssignLiteral("image/icon");
390  return NS_OK;
391}
392
393NS_IMETHODIMP
394nsIconChannel::SetContentCharset(const nsACString &aContentCharset)
395{
396  //It doesn't make sense to set the content-type on this type
397  // of channel...
398  return NS_ERROR_FAILURE;
399}
400
401NS_IMETHODIMP
402nsIconChannel::GetContentDisposition(PRUint32 *aContentDisposition)
403{
404  return NS_ERROR_NOT_AVAILABLE;
405}
406
407NS_IMETHODIMP
408nsIconChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
409{
410  return NS_ERROR_NOT_AVAILABLE;
411}
412
413NS_IMETHODIMP
414nsIconChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
415{
416  return NS_ERROR_NOT_AVAILABLE;
417}
418
419NS_IMETHODIMP nsIconChannel::GetContentLength(PRInt32 *aContentLength)
420{
421  *aContentLength = mContentLength;
422  return NS_OK;
423}
424
425NS_IMETHODIMP nsIconChannel::SetContentLength(PRInt32 aContentLength)
426{
427  NS_NOTREACHED("nsIconChannel::SetContentLength");
428  return NS_ERROR_NOT_IMPLEMENTED;
429}
430
431NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
432{
433  *aLoadGroup = mLoadGroup;
434  NS_IF_ADDREF(*aLoadGroup);
435  return NS_OK;
436}
437
438NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
439{
440  mLoadGroup = aLoadGroup;
441  return NS_OK;
442}
443
444NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner)
445{
446  *aOwner = mOwner.get();
447  NS_IF_ADDREF(*aOwner);
448  return NS_OK;
449}
450
451NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner)
452{
453  mOwner = aOwner;
454  return NS_OK;
455}
456
457NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
458{
459  *aNotificationCallbacks = mCallbacks.get();
460  NS_IF_ADDREF(*aNotificationCallbacks);
461  return NS_OK;
462}
463
464NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
465{
466  mCallbacks = aNotificationCallbacks;
467  return NS_OK;
468}
469
470NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
471{
472  *aSecurityInfo = nsnull;
473  return NS_OK;
474}
475