/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. #include "nsIconChannel.h"
  42. #include "nsIIconURI.h"
  43. #include "nsIServiceManager.h"
  44. #include "nsIInterfaceRequestor.h"
  45. #include "nsIInterfaceRequestorUtils.h"
  46. #include "nsXPIDLString.h"
  47. #include "nsMimeTypes.h"
  48. #include "nsMemory.h"
  49. #include "nsIStringStream.h"
  50. #include "nsIURL.h"
  51. #include "nsNetUtil.h"
  52. #include "nsIMIMEService.h"
  53. #include "nsCExternalHandlerService.h"
  54. #include "plstr.h"
  55. #include "nsILocalFileMac.h"
  56. #include "nsIFileURL.h"
  57. #include "nsTArray.h"
  58. #include "nsObjCExceptions.h"
  59. #include <Cocoa/Cocoa.h>
  60. // nsIconChannel methods
  61. nsIconChannel::nsIconChannel()
  62. {
  63. }
  64. nsIconChannel::~nsIconChannel()
  65. {}
  66. NS_IMPL_THREADSAFE_ISUPPORTS4(nsIconChannel,
  67. nsIChannel,
  68. nsIRequest,
  69. nsIRequestObserver,
  70. nsIStreamListener)
  71. nsresult nsIconChannel::Init(nsIURI* uri)
  72. {
  73. NS_ASSERTION(uri, "no uri");
  74. mUrl = uri;
  75. mOriginalURI = uri;
  76. nsresult rv;
  77. mPump = do_CreateInstance(NS_INPUTSTREAMPUMP_CONTRACTID, &rv);
  78. return rv;
  79. }
  80. ////////////////////////////////////////////////////////////////////////////////
  81. // nsIRequest methods:
  82. NS_IMETHODIMP nsIconChannel::GetName(nsACString &result)
  83. {
  84. return mUrl->GetSpec(result);
  85. }
  86. NS_IMETHODIMP nsIconChannel::IsPending(bool *result)
  87. {
  88. return mPump->IsPending(result);
  89. }
  90. NS_IMETHODIMP nsIconChannel::GetStatus(nsresult *status)
  91. {
  92. return mPump->GetStatus(status);
  93. }
  94. NS_IMETHODIMP nsIconChannel::Cancel(nsresult status)
  95. {
  96. return mPump->Cancel(status);
  97. }
  98. NS_IMETHODIMP nsIconChannel::Suspend(void)
  99. {
  100. return mPump->Suspend();
  101. }
  102. NS_IMETHODIMP nsIconChannel::Resume(void)
  103. {
  104. return mPump->Resume();
  105. }
  106. // nsIRequestObserver methods
  107. NS_IMETHODIMP nsIconChannel::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
  108. {
  109. if (mListener)
  110. return mListener->OnStartRequest(this, aContext);
  111. return NS_OK;
  112. }
  113. NS_IMETHODIMP nsIconChannel::OnStopRequest(nsIRequest* aRequest, nsISupports* aContext, nsresult aStatus)
  114. {
  115. if (mListener) {
  116. mListener->OnStopRequest(this, aContext, aStatus);
  117. mListener = nsnull;
  118. }
  119. // Remove from load group
  120. if (mLoadGroup)
  121. mLoadGroup->RemoveRequest(this, nsnull, aStatus);
  122. return NS_OK;
  123. }
  124. // nsIStreamListener methods
  125. NS_IMETHODIMP nsIconChannel::OnDataAvailable(nsIRequest* aRequest,
  126. nsISupports* aContext,
  127. nsIInputStream* aStream,
  128. PRUint32 aOffset,
  129. PRUint32 aCount)
  130. {
  131. if (mListener)
  132. return mListener->OnDataAvailable(this, aContext, aStream, aOffset, aCount);
  133. return NS_OK;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////
  136. // nsIChannel methods:
  137. NS_IMETHODIMP nsIconChannel::GetOriginalURI(nsIURI* *aURI)
  138. {
  139. *aURI = mOriginalURI;
  140. NS_ADDREF(*aURI);
  141. return NS_OK;
  142. }
  143. NS_IMETHODIMP nsIconChannel::SetOriginalURI(nsIURI* aURI)
  144. {
  145. NS_ENSURE_ARG_POINTER(aURI);
  146. mOriginalURI = aURI;
  147. return NS_OK;
  148. }
  149. NS_IMETHODIMP nsIconChannel::GetURI(nsIURI* *aURI)
  150. {
  151. *aURI = mUrl;
  152. NS_IF_ADDREF(*aURI);
  153. return NS_OK;
  154. }
  155. NS_IMETHODIMP
  156. nsIconChannel::Open(nsIInputStream **_retval)
  157. {
  158. return MakeInputStream(_retval, false);
  159. }
  160. nsresult nsIconChannel::ExtractIconInfoFromUrl(nsIFile ** aLocalFile, PRUint32 * aDesiredImageSize, nsACString &aContentType, nsACString &aFileExtension)
  161. {
  162. nsresult rv = NS_OK;
  163. nsCOMPtr<nsIMozIconURI> iconURI (do_QueryInterface(mUrl, &rv));
  164. NS_ENSURE_SUCCESS(rv, rv);
  165. iconURI->GetImageSize(aDesiredImageSize);
  166. iconURI->GetContentType(aContentType);
  167. iconURI->GetFileExtension(aFileExtension);
  168. nsCOMPtr<nsIURL> url;
  169. rv = iconURI->GetIconURL(getter_AddRefs(url));
  170. if (NS_FAILED(rv) || !url) return NS_OK;
  171. nsCOMPtr<nsIFileURL> fileURL = do_QueryInterface(url, &rv);
  172. if (NS_FAILED(rv) || !fileURL) return NS_OK;
  173. nsCOMPtr<nsIFile> file;
  174. rv = fileURL->GetFile(getter_AddRefs(file));
  175. if (NS_FAILED(rv) || !file) return NS_OK;
  176. nsCOMPtr<nsILocalFileMac> localFileMac (do_QueryInterface(file, &rv));
  177. if (NS_FAILED(rv) || !localFileMac) return NS_OK;
  178. *aLocalFile = file;
  179. NS_IF_ADDREF(*aLocalFile);
  180. return NS_OK;
  181. }
  182. NS_IMETHODIMP nsIconChannel::AsyncOpen(nsIStreamListener *aListener, nsISupports *ctxt)
  183. {
  184. nsCOMPtr<nsIInputStream> inStream;
  185. nsresult rv = MakeInputStream(getter_AddRefs(inStream), true);
  186. NS_ENSURE_SUCCESS(rv, rv);
  187. // Init our stream pump
  188. rv = mPump->Init(inStream, PRInt64(-1), PRInt64(-1), 0, 0, false);
  189. NS_ENSURE_SUCCESS(rv, rv);
  190. rv = mPump->AsyncRead(this, ctxt);
  191. if (NS_SUCCEEDED(rv)) {
  192. // Store our real listener
  193. mListener = aListener;
  194. // Add ourself to the load group, if available
  195. if (mLoadGroup)
  196. mLoadGroup->AddRequest(this, nsnull);
  197. }
  198. return rv;
  199. }
  200. nsresult nsIconChannel::MakeInputStream(nsIInputStream** _retval, bool nonBlocking)
  201. {
  202. NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
  203. nsXPIDLCString contentType;
  204. nsCAutoString fileExt;
  205. nsCOMPtr<nsIFile> fileloc; // file we want an icon for
  206. PRUint32 desiredImageSize;
  207. nsresult rv = ExtractIconInfoFromUrl(getter_AddRefs(fileloc), &desiredImageSize, contentType, fileExt);
  208. NS_ENSURE_SUCCESS(rv, rv);
  209. // ensure that we DO NOT resolve aliases, very important for file views
  210. nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(fileloc);
  211. if (localFile)
  212. localFile->SetFollowLinks(false);
  213. bool fileExists = false;
  214. if (fileloc)
  215. localFile->Exists(&fileExists);
  216. NSImage* iconImage = nil;
  217. // first try to get the icon from the file if it exists
  218. if (fileExists) {
  219. nsCOMPtr<nsILocalFileMac> localFileMac(do_QueryInterface(fileloc, &rv));
  220. NS_ENSURE_SUCCESS(rv, rv);
  221. CFURLRef macURL;
  222. if (NS_SUCCEEDED(localFileMac->GetCFURL(&macURL))) {
  223. iconImage = [[NSWorkspace sharedWorkspace] iconForFile:[(NSURL*)macURL path]];
  224. ::CFRelease(macURL);
  225. }
  226. }
  227. // if we don't have an icon yet try to get one by extension
  228. if (!iconImage && !fileExt.IsEmpty()) {
  229. NSString* fileExtension = [NSString stringWithUTF8String:PromiseFlatCString(fileExt).get()];
  230. iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:fileExtension];
  231. }
  232. // If we still don't have an icon, get the generic document icon.
  233. if (!iconImage)
  234. iconImage = [[NSWorkspace sharedWorkspace] iconForFileType:NSFileTypeUnknown];
  235. if (!iconImage)
  236. return NS_ERROR_FAILURE;
  237. // we have an icon now, size it
  238. NSRect desiredSizeRect = NSMakeRect(0, 0, desiredImageSize, desiredImageSize);
  239. [iconImage setSize:desiredSizeRect.size];
  240. [iconImage lockFocus];
  241. NSBitmapImageRep* bitmapRep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:desiredSizeRect] autorelease];
  242. [iconImage unlockFocus];
  243. // we expect the following things to be true about our bitmapRep
  244. NS_ENSURE_TRUE(![bitmapRep isPlanar] &&
  245. (unsigned int)[bitmapRep bytesPerPlane] == desiredImageSize * desiredImageSize * 4 &&
  246. [bitmapRep bitsPerPixel] == 32 &&
  247. [bitmapRep samplesPerPixel] == 4 &&
  248. [bitmapRep hasAlpha] == YES,
  249. NS_ERROR_UNEXPECTED);
  250. // rgba, pre-multiplied data
  251. PRUint8* bitmapRepData = (PRUint8*)[bitmapRep bitmapData];
  252. // create our buffer
  253. PRInt32 bufferCapacity = 2 + desiredImageSize * desiredImageSize * 4;
  254. nsAutoTArray<PRUint8, 3 + 16 * 16 * 5> iconBuffer; // initial size is for 16x16
  255. if (!iconBuffer.SetLength(bufferCapacity))
  256. return NS_ERROR_OUT_OF_MEMORY;
  257. PRUint8* iconBufferPtr = iconBuffer.Elements();
  258. // write header data into buffer
  259. *iconBufferPtr++ = desiredImageSize;
  260. *iconBufferPtr++ = desiredImageSize;
  261. PRUint32 dataCount = (desiredImageSize * desiredImageSize) * 4;
  262. PRUint32 index = 0;
  263. while (index < dataCount) {
  264. // get data from the bitmap
  265. PRUint8 r = bitmapRepData[index++];
  266. PRUint8 g = bitmapRepData[index++];
  267. PRUint8 b = bitmapRepData[index++];
  268. PRUint8 a = bitmapRepData[index++];
  269. // write data out to our buffer
  270. // non-cairo uses native image format, but the A channel is ignored.
  271. // cairo uses ARGB (highest to lowest bits)
  272. #if defined(IS_LITTLE_ENDIAN)
  273. *iconBufferPtr++ = b;
  274. *iconBufferPtr++ = g;
  275. *iconBufferPtr++ = r;
  276. *iconBufferPtr++ = a;
  277. #else
  278. *iconBufferPtr++ = a;
  279. *iconBufferPtr++ = r;
  280. *iconBufferPtr++ = g;
  281. *iconBufferPtr++ = b;
  282. #endif
  283. }
  284. NS_ASSERTION(iconBufferPtr == iconBuffer.Elements() + bufferCapacity,
  285. "buffer size miscalculation");
  286. // Now, create a pipe and stuff our data into it
  287. nsCOMPtr<nsIInputStream> inStream;
  288. nsCOMPtr<nsIOutputStream> outStream;
  289. rv = NS_NewPipe(getter_AddRefs(inStream), getter_AddRefs(outStream), bufferCapacity, bufferCapacity, nonBlocking);
  290. if (NS_SUCCEEDED(rv)) {
  291. PRUint32 written;
  292. rv = outStream->Write((char*)iconBuffer.Elements(), bufferCapacity, &written);
  293. if (NS_SUCCEEDED(rv))
  294. NS_IF_ADDREF(*_retval = inStream);
  295. }
  296. // Drop notification callbacks to prevent cycles.
  297. mCallbacks = nsnull;
  298. return NS_OK;
  299. NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
  300. }
  301. NS_IMETHODIMP nsIconChannel::GetLoadFlags(PRUint32 *aLoadAttributes)
  302. {
  303. return mPump->GetLoadFlags(aLoadAttributes);
  304. }
  305. NS_IMETHODIMP nsIconChannel::SetLoadFlags(PRUint32 aLoadAttributes)
  306. {
  307. return mPump->SetLoadFlags(aLoadAttributes);
  308. }
  309. NS_IMETHODIMP nsIconChannel::GetContentType(nsACString &aContentType)
  310. {
  311. aContentType.AssignLiteral("image/icon");
  312. return NS_OK;
  313. }
  314. NS_IMETHODIMP
  315. nsIconChannel::SetContentType(const nsACString &aContentType)
  316. {
  317. //It doesn't make sense to set the content-type on this type
  318. // of channel...
  319. return NS_ERROR_FAILURE;
  320. }
  321. NS_IMETHODIMP nsIconChannel::GetContentCharset(nsACString &aContentCharset)
  322. {
  323. aContentCharset.AssignLiteral("image/icon");
  324. return NS_OK;
  325. }
  326. NS_IMETHODIMP
  327. nsIconChannel::SetContentCharset(const nsACString &aContentCharset)
  328. {
  329. //It doesn't make sense to set the content-type on this type
  330. // of channel...
  331. return NS_ERROR_FAILURE;
  332. }
  333. NS_IMETHODIMP
  334. nsIconChannel::GetContentDisposition(PRUint32 *aContentDisposition)
  335. {
  336. return NS_ERROR_NOT_AVAILABLE;
  337. }
  338. NS_IMETHODIMP
  339. nsIconChannel::GetContentDispositionFilename(nsAString &aContentDispositionFilename)
  340. {
  341. return NS_ERROR_NOT_AVAILABLE;
  342. }
  343. NS_IMETHODIMP
  344. nsIconChannel::GetContentDispositionHeader(nsACString &aContentDispositionHeader)
  345. {
  346. return NS_ERROR_NOT_AVAILABLE;
  347. }
  348. NS_IMETHODIMP nsIconChannel::GetContentLength(PRInt32 *aContentLength)
  349. {
  350. *aContentLength = mContentLength;
  351. return NS_OK;
  352. }
  353. NS_IMETHODIMP nsIconChannel::SetContentLength(PRInt32 aContentLength)
  354. {
  355. NS_NOTREACHED("nsIconChannel::SetContentLength");
  356. return NS_ERROR_NOT_IMPLEMENTED;
  357. }
  358. NS_IMETHODIMP nsIconChannel::GetLoadGroup(nsILoadGroup* *aLoadGroup)
  359. {
  360. *aLoadGroup = mLoadGroup;
  361. NS_IF_ADDREF(*aLoadGroup);
  362. return NS_OK;
  363. }
  364. NS_IMETHODIMP nsIconChannel::SetLoadGroup(nsILoadGroup* aLoadGroup)
  365. {
  366. mLoadGroup = aLoadGroup;
  367. return NS_OK;
  368. }
  369. NS_IMETHODIMP nsIconChannel::GetOwner(nsISupports* *aOwner)
  370. {
  371. *aOwner = mOwner.get();
  372. NS_IF_ADDREF(*aOwner);
  373. return NS_OK;
  374. }
  375. NS_IMETHODIMP nsIconChannel::SetOwner(nsISupports* aOwner)
  376. {
  377. mOwner = aOwner;
  378. return NS_OK;
  379. }
  380. NS_IMETHODIMP nsIconChannel::GetNotificationCallbacks(nsIInterfaceRequestor* *aNotificationCallbacks)
  381. {
  382. *aNotificationCallbacks = mCallbacks.get();
  383. NS_IF_ADDREF(*aNotificationCallbacks);
  384. return NS_OK;
  385. }
  386. NS_IMETHODIMP nsIconChannel::SetNotificationCallbacks(nsIInterfaceRequestor* aNotificationCallbacks)
  387. {
  388. mCallbacks = aNotificationCallbacks;
  389. return NS_OK;
  390. }
  391. NS_IMETHODIMP nsIconChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
  392. {
  393. *aSecurityInfo = nsnull;
  394. return NS_OK;
  395. }