image/decoders/icon/android/nsIconChannel.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

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 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <stdlib.h>
michael@0 7 #include "mozilla/dom/ContentChild.h"
michael@0 8 #include "nsMimeTypes.h"
michael@0 9 #include "nsIURL.h"
michael@0 10 #include "nsXULAppAPI.h"
michael@0 11 #include "AndroidBridge.h"
michael@0 12 #include "nsIconChannel.h"
michael@0 13 #include "nsIStringStream.h"
michael@0 14 #include "nsNetUtil.h"
michael@0 15
michael@0 16 NS_IMPL_ISUPPORTS(nsIconChannel,
michael@0 17 nsIRequest,
michael@0 18 nsIChannel)
michael@0 19
michael@0 20 using namespace mozilla;
michael@0 21 using mozilla::dom::ContentChild;
michael@0 22
michael@0 23 static nsresult
michael@0 24 GetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf)
michael@0 25 {
michael@0 26 if (!AndroidBridge::Bridge())
michael@0 27 return NS_ERROR_FAILURE;
michael@0 28
michael@0 29 AndroidBridge::Bridge()->GetIconForExtension(aFileExt, aIconSize, aBuf);
michael@0 30
michael@0 31 return NS_OK;
michael@0 32 }
michael@0 33
michael@0 34 static nsresult
michael@0 35 CallRemoteGetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf)
michael@0 36 {
michael@0 37 NS_ENSURE_TRUE(aBuf != nullptr, NS_ERROR_NULL_POINTER);
michael@0 38
michael@0 39 // An array has to be used to get data from remote process
michael@0 40 InfallibleTArray<uint8_t> bits;
michael@0 41 uint32_t bufSize = aIconSize * aIconSize * 4;
michael@0 42
michael@0 43 if (!ContentChild::GetSingleton()->SendGetIconForExtension(PromiseFlatCString(aFileExt), aIconSize, &bits))
michael@0 44 return NS_ERROR_FAILURE;
michael@0 45
michael@0 46 NS_ASSERTION(bits.Length() == bufSize, "Pixels array is incomplete");
michael@0 47 if (bits.Length() != bufSize)
michael@0 48 return NS_ERROR_FAILURE;
michael@0 49
michael@0 50 memcpy(aBuf, bits.Elements(), bufSize);
michael@0 51
michael@0 52 return NS_OK;
michael@0 53 }
michael@0 54
michael@0 55 static nsresult
michael@0 56 moz_icon_to_channel(nsIURI *aURI, const nsACString& aFileExt, uint32_t aIconSize, nsIChannel **aChannel)
michael@0 57 {
michael@0 58 NS_ENSURE_TRUE(aIconSize < 256 && aIconSize > 0, NS_ERROR_UNEXPECTED);
michael@0 59
michael@0 60 int width = aIconSize;
michael@0 61 int height = aIconSize;
michael@0 62
michael@0 63 // moz-icon data should have two bytes for the size,
michael@0 64 // then the ARGB pixel values with pre-multiplied Alpha
michael@0 65 const int channels = 4;
michael@0 66 long int buf_size = 2 + channels * height * width;
michael@0 67 uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size);
michael@0 68 NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
michael@0 69 uint8_t * out = buf;
michael@0 70
michael@0 71 *(out++) = width;
michael@0 72 *(out++) = height;
michael@0 73
michael@0 74 nsresult rv;
michael@0 75 if (XRE_GetProcessType() == GeckoProcessType_Default)
michael@0 76 rv = GetIconForExtension(aFileExt, aIconSize, out);
michael@0 77 else
michael@0 78 rv = CallRemoteGetIconForExtension(aFileExt, aIconSize, out);
michael@0 79 NS_ENSURE_SUCCESS(rv, rv);
michael@0 80
michael@0 81 // Encode the RGBA data
michael@0 82 const uint8_t * in = out;
michael@0 83 for (int y = 0; y < height; ++y) {
michael@0 84 for (int x = 0; x < width; ++x) {
michael@0 85 uint8_t r = *(in++);
michael@0 86 uint8_t g = *(in++);
michael@0 87 uint8_t b = *(in++);
michael@0 88 uint8_t a = *(in++);
michael@0 89 #define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255))
michael@0 90 *(out++) = DO_PREMULTIPLY(b);
michael@0 91 *(out++) = DO_PREMULTIPLY(g);
michael@0 92 *(out++) = DO_PREMULTIPLY(r);
michael@0 93 *(out++) = a;
michael@0 94 #undef DO_PREMULTIPLY
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 nsCOMPtr<nsIStringInputStream> stream =
michael@0 99 do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
michael@0 100 NS_ENSURE_SUCCESS(rv, rv);
michael@0 101
michael@0 102 rv = stream->AdoptData((char*)buf, buf_size);
michael@0 103 NS_ENSURE_SUCCESS(rv, rv);
michael@0 104
michael@0 105 return NS_NewInputStreamChannel(aChannel, aURI, stream,
michael@0 106 NS_LITERAL_CSTRING(IMAGE_ICON_MS));
michael@0 107 }
michael@0 108
michael@0 109 nsresult
michael@0 110 nsIconChannel::Init(nsIURI* aURI)
michael@0 111 {
michael@0 112 nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI);
michael@0 113 NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI");
michael@0 114
michael@0 115 nsAutoCString stockIcon;
michael@0 116 iconURI->GetStockIcon(stockIcon);
michael@0 117
michael@0 118 uint32_t desiredImageSize;
michael@0 119 iconURI->GetImageSize(&desiredImageSize);
michael@0 120
michael@0 121 nsAutoCString iconFileExt;
michael@0 122 iconURI->GetFileExtension(iconFileExt);
michael@0 123
michael@0 124 return moz_icon_to_channel(iconURI, iconFileExt, desiredImageSize, getter_AddRefs(mRealChannel));
michael@0 125 }

mercurial