michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "mozilla/dom/ContentChild.h" michael@0: #include "nsMimeTypes.h" michael@0: #include "nsIURL.h" michael@0: #include "nsXULAppAPI.h" michael@0: #include "AndroidBridge.h" michael@0: #include "nsIconChannel.h" michael@0: #include "nsIStringStream.h" michael@0: #include "nsNetUtil.h" michael@0: michael@0: NS_IMPL_ISUPPORTS(nsIconChannel, michael@0: nsIRequest, michael@0: nsIChannel) michael@0: michael@0: using namespace mozilla; michael@0: using mozilla::dom::ContentChild; michael@0: michael@0: static nsresult michael@0: GetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf) michael@0: { michael@0: if (!AndroidBridge::Bridge()) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: AndroidBridge::Bridge()->GetIconForExtension(aFileExt, aIconSize, aBuf); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: CallRemoteGetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf) michael@0: { michael@0: NS_ENSURE_TRUE(aBuf != nullptr, NS_ERROR_NULL_POINTER); michael@0: michael@0: // An array has to be used to get data from remote process michael@0: InfallibleTArray bits; michael@0: uint32_t bufSize = aIconSize * aIconSize * 4; michael@0: michael@0: if (!ContentChild::GetSingleton()->SendGetIconForExtension(PromiseFlatCString(aFileExt), aIconSize, &bits)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: NS_ASSERTION(bits.Length() == bufSize, "Pixels array is incomplete"); michael@0: if (bits.Length() != bufSize) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: memcpy(aBuf, bits.Elements(), bufSize); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: moz_icon_to_channel(nsIURI *aURI, const nsACString& aFileExt, uint32_t aIconSize, nsIChannel **aChannel) michael@0: { michael@0: NS_ENSURE_TRUE(aIconSize < 256 && aIconSize > 0, NS_ERROR_UNEXPECTED); michael@0: michael@0: int width = aIconSize; michael@0: int height = aIconSize; michael@0: michael@0: // moz-icon data should have two bytes for the size, michael@0: // then the ARGB pixel values with pre-multiplied Alpha michael@0: const int channels = 4; michael@0: long int buf_size = 2 + channels * height * width; michael@0: uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size); michael@0: NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY); michael@0: uint8_t * out = buf; michael@0: michael@0: *(out++) = width; michael@0: *(out++) = height; michael@0: michael@0: nsresult rv; michael@0: if (XRE_GetProcessType() == GeckoProcessType_Default) michael@0: rv = GetIconForExtension(aFileExt, aIconSize, out); michael@0: else michael@0: rv = CallRemoteGetIconForExtension(aFileExt, aIconSize, out); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Encode the RGBA data michael@0: const uint8_t * in = out; michael@0: for (int y = 0; y < height; ++y) { michael@0: for (int x = 0; x < width; ++x) { michael@0: uint8_t r = *(in++); michael@0: uint8_t g = *(in++); michael@0: uint8_t b = *(in++); michael@0: uint8_t a = *(in++); michael@0: #define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255)) michael@0: *(out++) = DO_PREMULTIPLY(b); michael@0: *(out++) = DO_PREMULTIPLY(g); michael@0: *(out++) = DO_PREMULTIPLY(r); michael@0: *(out++) = a; michael@0: #undef DO_PREMULTIPLY michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr stream = michael@0: do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = stream->AdoptData((char*)buf, buf_size); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return NS_NewInputStreamChannel(aChannel, aURI, stream, michael@0: NS_LITERAL_CSTRING(IMAGE_ICON_MS)); michael@0: } michael@0: michael@0: nsresult michael@0: nsIconChannel::Init(nsIURI* aURI) michael@0: { michael@0: nsCOMPtr iconURI = do_QueryInterface(aURI); michael@0: NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI"); michael@0: michael@0: nsAutoCString stockIcon; michael@0: iconURI->GetStockIcon(stockIcon); michael@0: michael@0: uint32_t desiredImageSize; michael@0: iconURI->GetImageSize(&desiredImageSize); michael@0: michael@0: nsAutoCString iconFileExt; michael@0: iconURI->GetFileExtension(iconFileExt); michael@0: michael@0: return moz_icon_to_channel(iconURI, iconFileExt, desiredImageSize, getter_AddRefs(mRealChannel)); michael@0: }