Bug 1421655 - Implementation of imgITools::decodeImageFromArrayBuffer, r=aosmond
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 30 Nov 2017 14:48:47 +0100
changeset 448662 807297ac24456964acc0efcba2d642c28191109a
parent 448661 d2dff894a6dd435d32186ff8ad09b7b3a728bb59
child 448663 75d2ae4d07c4a88a1fa5d695278c7e106a2e20c1
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaosmond
bugs1421655
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1421655 - Implementation of imgITools::decodeImageFromArrayBuffer, r=aosmond
image/imgITools.idl
image/imgTools.cpp
--- a/image/imgITools.idl
+++ b/image/imgITools.idl
@@ -15,36 +15,48 @@ interface nsIDOMDocument;
 interface imgIScriptedNotificationObserver;
 interface imgINotificationObserver;
 interface imgIContainerCallback;
 
 [scriptable, builtinclass, uuid(4c2383a4-931c-484d-8c4a-973590f66e3f)]
 interface imgITools : nsISupports
 {
     /**
-     * decodeImage
      * decodeImageBuffer
      * Caller provides an buffer, a buffer size and a mimetype. We read from
      * the stream and decompress it (according to the specified mime type) and
      * return the resulting imgIContainer.
      *
-     * @param aStream
-     *        An input stream for an encoded image file.
      * @param aBuffer
      *        Data in memory.
      * @param aSize
      *        Buffer size.
      * @param aMimeType
      *        Type of image in the stream.
      */
     imgIContainer decodeImageBuffer(in string aBuffer,
                                     in unsigned long aSize,
                                     in ACString aMimeType);
 
     /**
+     * decodeImageFromArrayBuffer
+     * Caller provides an ArrayBuffer and a mimetype. We read from
+     * the stream and decompress it (according to the specified mime type) and
+     * return the resulting imgIContainer.
+     *
+     * @param aArrayBuffer
+     *        An ArrayBuffer.
+     * @param aMimeType
+     *        Type of image in the stream.
+     */
+    [implicit_jscontext]
+    imgIContainer decodeImageFromArrayBuffer(in jsval aArrayBuffer,
+                                             in ACString aMimeType);
+
+    /**
      * decodeImageAsync
      * See decodeImage. The main difference between this method and decodeImage
      * is that here the operation is done async on a thread from the decode
      * pool. When the operation is completed, the callback is executed with the
      * result.
      *
      * @param aStream
      *        An input stream for an encoded image file.
--- a/image/imgTools.cpp
+++ b/image/imgTools.cpp
@@ -23,16 +23,17 @@
 #include "nsStringStream.h"
 #include "nsContentUtils.h"
 #include "nsProxyRelease.h"
 #include "ImageFactory.h"
 #include "Image.h"
 #include "ScriptedNotificationObserver.h"
 #include "imgIScriptedNotificationObserver.h"
 #include "gfxPlatform.h"
+#include "jsfriendapi.h"
 
 using namespace mozilla::gfx;
 
 namespace mozilla {
 namespace image {
 
 namespace {
 
@@ -176,16 +177,42 @@ imgTools::imgTools()
 }
 
 imgTools::~imgTools()
 {
   /* destructor code */
 }
 
 NS_IMETHODIMP
+imgTools::DecodeImageFromArrayBuffer(JS::HandleValue aArrayBuffer,
+                                     const nsACString& aMimeType,
+                                     JSContext* aCx,
+                                     imgIContainer** aContainer)
+{
+  if (!aArrayBuffer.isObject()) {
+    return NS_ERROR_FAILURE;
+  }
+
+  JS::Rooted<JSObject*> obj(aCx,
+                            js::UnwrapArrayBuffer(&aArrayBuffer.toObject()));
+  if (!obj) {
+    return NS_ERROR_FAILURE;
+  }
+
+  uint8_t* bufferData = nullptr;
+  uint32_t bufferLength = 0;
+  bool isSharedMemory = false;
+
+  js::GetArrayBufferLengthAndData(obj, &bufferLength, &isSharedMemory,
+                                  &bufferData);
+  return DecodeImageBuffer((char*)bufferData, bufferLength, aMimeType,
+                           aContainer);
+}
+
+NS_IMETHODIMP
 imgTools::DecodeImageBuffer(const char* aBuffer, uint32_t aSize,
                             const nsACString& aMimeType,
                             imgIContainer** aContainer)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   NS_ENSURE_ARG_POINTER(aBuffer);