Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* vim:set ts=2 sw=2 sts=2 cin et: */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include <QIcon> |
michael@0 | 7 | |
michael@0 | 8 | #include <stdlib.h> |
michael@0 | 9 | #include <unistd.h> |
michael@0 | 10 | |
michael@0 | 11 | #include "mozilla/Endian.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "nsMimeTypes.h" |
michael@0 | 14 | #include "nsIMIMEService.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "nsIStringBundle.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "nsNetUtil.h" |
michael@0 | 19 | #include "nsIURL.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "nsIconChannel.h" |
michael@0 | 22 | #include "nsGtkQtIconsConverter.h" |
michael@0 | 23 | |
michael@0 | 24 | NS_IMPL_ISUPPORTS(nsIconChannel, |
michael@0 | 25 | nsIRequest, |
michael@0 | 26 | nsIChannel) |
michael@0 | 27 | |
michael@0 | 28 | static nsresult |
michael@0 | 29 | moz_qicon_to_channel(QImage *image, nsIURI *aURI, |
michael@0 | 30 | nsIChannel **aChannel) |
michael@0 | 31 | { |
michael@0 | 32 | NS_ENSURE_ARG_POINTER(image); |
michael@0 | 33 | |
michael@0 | 34 | int width = image->width(); |
michael@0 | 35 | int height = image->height(); |
michael@0 | 36 | |
michael@0 | 37 | NS_ENSURE_TRUE(height < 256 && width < 256 && height > 0 && width > 0, |
michael@0 | 38 | NS_ERROR_UNEXPECTED); |
michael@0 | 39 | |
michael@0 | 40 | const int n_channels = 4; |
michael@0 | 41 | long int buf_size = 2 + n_channels * height * width; |
michael@0 | 42 | uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size); |
michael@0 | 43 | NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 44 | uint8_t *out = buf; |
michael@0 | 45 | |
michael@0 | 46 | *(out++) = width; |
michael@0 | 47 | *(out++) = height; |
michael@0 | 48 | |
michael@0 | 49 | const uchar * const pixels = image->bits(); |
michael@0 | 50 | int rowextra = image->bytesPerLine() - width * n_channels; |
michael@0 | 51 | |
michael@0 | 52 | // encode the RGB data and the A data |
michael@0 | 53 | const uchar * in = pixels; |
michael@0 | 54 | for (int y = 0; y < height; ++y, in += rowextra) { |
michael@0 | 55 | for (int x = 0; x < width; ++x) { |
michael@0 | 56 | uint8_t r = *(in++); |
michael@0 | 57 | uint8_t g = *(in++); |
michael@0 | 58 | uint8_t b = *(in++); |
michael@0 | 59 | uint8_t a = *(in++); |
michael@0 | 60 | #define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255)) |
michael@0 | 61 | #if MOZ_LITTLE_ENDIAN |
michael@0 | 62 | *(out++) = DO_PREMULTIPLY(b); |
michael@0 | 63 | *(out++) = DO_PREMULTIPLY(g); |
michael@0 | 64 | *(out++) = DO_PREMULTIPLY(r); |
michael@0 | 65 | *(out++) = a; |
michael@0 | 66 | #else |
michael@0 | 67 | *(out++) = a; |
michael@0 | 68 | *(out++) = DO_PREMULTIPLY(r); |
michael@0 | 69 | *(out++) = DO_PREMULTIPLY(g); |
michael@0 | 70 | *(out++) = DO_PREMULTIPLY(b); |
michael@0 | 71 | #endif |
michael@0 | 72 | #undef DO_PREMULTIPLY |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | NS_ASSERTION(out == buf + buf_size, "size miscalculation"); |
michael@0 | 77 | |
michael@0 | 78 | nsresult rv; |
michael@0 | 79 | nsCOMPtr<nsIStringInputStream> stream = |
michael@0 | 80 | do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); |
michael@0 | 81 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 82 | |
michael@0 | 83 | rv = stream->AdoptData((char*)buf, buf_size); |
michael@0 | 84 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 85 | |
michael@0 | 86 | return NS_NewInputStreamChannel(aChannel, aURI, stream, |
michael@0 | 87 | NS_LITERAL_CSTRING(IMAGE_ICON_MS)); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | nsresult |
michael@0 | 91 | nsIconChannel::Init(nsIURI* aURI) |
michael@0 | 92 | { |
michael@0 | 93 | |
michael@0 | 94 | nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI); |
michael@0 | 95 | NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); |
michael@0 | 96 | |
michael@0 | 97 | nsAutoCString stockIcon; |
michael@0 | 98 | iconURI->GetStockIcon(stockIcon); |
michael@0 | 99 | |
michael@0 | 100 | nsAutoCString iconSizeString; |
michael@0 | 101 | iconURI->GetIconSize(iconSizeString); |
michael@0 | 102 | |
michael@0 | 103 | uint32_t desiredImageSize; |
michael@0 | 104 | iconURI->GetImageSize(&desiredImageSize); |
michael@0 | 105 | |
michael@0 | 106 | nsAutoCString iconStateString; |
michael@0 | 107 | iconURI->GetIconState(iconStateString); |
michael@0 | 108 | bool disabled = iconStateString.EqualsLiteral("disabled"); |
michael@0 | 109 | |
michael@0 | 110 | // This is a workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=662299 |
michael@0 | 111 | // Try to find corresponding freedesktop icon and fallback to empty QIcon if failed. |
michael@0 | 112 | QIcon icon = QIcon::fromTheme(QString(stockIcon.get()).replace("gtk-", "edit-")); |
michael@0 | 113 | QPixmap pixmap = icon.pixmap(desiredImageSize, desiredImageSize, disabled ? QIcon::Disabled : QIcon::Normal); |
michael@0 | 114 | |
michael@0 | 115 | QImage image = pixmap.toImage(); |
michael@0 | 116 | |
michael@0 | 117 | return moz_qicon_to_channel(&image, iconURI, |
michael@0 | 118 | getter_AddRefs(mRealChannel)); |
michael@0 | 119 | } |