image/decoders/icon/android/nsIconChannel.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/decoders/icon/android/nsIconChannel.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include <stdlib.h>
    1.10 +#include "mozilla/dom/ContentChild.h"
    1.11 +#include "nsMimeTypes.h"
    1.12 +#include "nsIURL.h"
    1.13 +#include "nsXULAppAPI.h"
    1.14 +#include "AndroidBridge.h"
    1.15 +#include "nsIconChannel.h"
    1.16 +#include "nsIStringStream.h"
    1.17 +#include "nsNetUtil.h"
    1.18 +
    1.19 +NS_IMPL_ISUPPORTS(nsIconChannel,
    1.20 +                  nsIRequest,
    1.21 +                  nsIChannel)
    1.22 +
    1.23 +using namespace mozilla;
    1.24 +using mozilla::dom::ContentChild;
    1.25 +
    1.26 +static nsresult
    1.27 +GetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf)
    1.28 +{
    1.29 +    if (!AndroidBridge::Bridge())
    1.30 +        return NS_ERROR_FAILURE;
    1.31 +
    1.32 +    AndroidBridge::Bridge()->GetIconForExtension(aFileExt, aIconSize, aBuf);
    1.33 +
    1.34 +    return NS_OK;
    1.35 +}
    1.36 +
    1.37 +static nsresult
    1.38 +CallRemoteGetIconForExtension(const nsACString& aFileExt, uint32_t aIconSize, uint8_t * const aBuf)
    1.39 +{
    1.40 +  NS_ENSURE_TRUE(aBuf != nullptr, NS_ERROR_NULL_POINTER);
    1.41 +
    1.42 +  // An array has to be used to get data from remote process
    1.43 +  InfallibleTArray<uint8_t> bits;
    1.44 +  uint32_t bufSize = aIconSize * aIconSize * 4;
    1.45 +
    1.46 +  if (!ContentChild::GetSingleton()->SendGetIconForExtension(PromiseFlatCString(aFileExt), aIconSize, &bits))
    1.47 +    return NS_ERROR_FAILURE;
    1.48 +
    1.49 +  NS_ASSERTION(bits.Length() == bufSize, "Pixels array is incomplete");
    1.50 +  if (bits.Length() != bufSize)
    1.51 +    return NS_ERROR_FAILURE;
    1.52 +
    1.53 +  memcpy(aBuf, bits.Elements(), bufSize);
    1.54 +
    1.55 +  return NS_OK;
    1.56 +}
    1.57 +
    1.58 +static nsresult
    1.59 +moz_icon_to_channel(nsIURI *aURI, const nsACString& aFileExt, uint32_t aIconSize, nsIChannel **aChannel)
    1.60 +{
    1.61 +  NS_ENSURE_TRUE(aIconSize < 256 && aIconSize > 0, NS_ERROR_UNEXPECTED);
    1.62 +
    1.63 +  int width = aIconSize;
    1.64 +  int height = aIconSize;
    1.65 +
    1.66 +  // moz-icon data should have two bytes for the size,
    1.67 +  // then the ARGB pixel values with pre-multiplied Alpha
    1.68 +  const int channels = 4;
    1.69 +  long int buf_size = 2 + channels * height * width;
    1.70 +  uint8_t * const buf = (uint8_t*)NS_Alloc(buf_size);
    1.71 +  NS_ENSURE_TRUE(buf, NS_ERROR_OUT_OF_MEMORY);
    1.72 +  uint8_t * out = buf;
    1.73 +
    1.74 +  *(out++) = width;
    1.75 +  *(out++) = height;
    1.76 +
    1.77 +  nsresult rv;
    1.78 +  if (XRE_GetProcessType() == GeckoProcessType_Default)
    1.79 +    rv = GetIconForExtension(aFileExt, aIconSize, out);
    1.80 +  else
    1.81 +    rv = CallRemoteGetIconForExtension(aFileExt, aIconSize, out);
    1.82 +  NS_ENSURE_SUCCESS(rv, rv);
    1.83 +
    1.84 +  // Encode the RGBA data
    1.85 +  const uint8_t * in = out;
    1.86 +  for (int y = 0; y < height; ++y) {
    1.87 +    for (int x = 0; x < width; ++x) {
    1.88 +      uint8_t r = *(in++);
    1.89 +      uint8_t g = *(in++);
    1.90 +      uint8_t b = *(in++);
    1.91 +      uint8_t a = *(in++);
    1.92 +#define DO_PREMULTIPLY(c_) uint8_t(uint16_t(c_) * uint16_t(a) / uint16_t(255))
    1.93 +      *(out++) = DO_PREMULTIPLY(b);
    1.94 +      *(out++) = DO_PREMULTIPLY(g);
    1.95 +      *(out++) = DO_PREMULTIPLY(r);
    1.96 +      *(out++) = a;
    1.97 +#undef DO_PREMULTIPLY
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +  nsCOMPtr<nsIStringInputStream> stream =
   1.102 +    do_CreateInstance("@mozilla.org/io/string-input-stream;1", &rv);
   1.103 +  NS_ENSURE_SUCCESS(rv, rv);
   1.104 +
   1.105 +  rv = stream->AdoptData((char*)buf, buf_size);
   1.106 +  NS_ENSURE_SUCCESS(rv, rv);
   1.107 +
   1.108 +  return NS_NewInputStreamChannel(aChannel, aURI, stream,
   1.109 +                                  NS_LITERAL_CSTRING(IMAGE_ICON_MS));
   1.110 +}
   1.111 +
   1.112 +nsresult
   1.113 +nsIconChannel::Init(nsIURI* aURI)
   1.114 +{
   1.115 +  nsCOMPtr<nsIMozIconURI> iconURI = do_QueryInterface(aURI);
   1.116 +  NS_ASSERTION(iconURI, "URI is not an nsIMozIconURI");
   1.117 +
   1.118 +  nsAutoCString stockIcon;
   1.119 +  iconURI->GetStockIcon(stockIcon);
   1.120 +
   1.121 +  uint32_t desiredImageSize;
   1.122 +  iconURI->GetImageSize(&desiredImageSize);
   1.123 +
   1.124 +  nsAutoCString iconFileExt;
   1.125 +  iconURI->GetFileExtension(iconFileExt);
   1.126 +
   1.127 +  return moz_icon_to_channel(iconURI, iconFileExt, desiredImageSize, getter_AddRefs(mRealChannel));
   1.128 +}

mercurial