image/decoders/icon/mac/nsIconChannelCocoa.mm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial