From b495532ca49d2c57c189f099c9e0888d8b85b011 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 24 Apr 2012 12:58:07 +0200 Subject: [PATCH] Bug 743615 - Handle ImageData in the main thread runtime callbacks. r=bent --- dom/base/StructuredCloneTags.h | 6 ++++ dom/base/nsJSEnvironment.cpp | 59 ++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/dom/base/StructuredCloneTags.h b/dom/base/StructuredCloneTags.h index 86c1acf712d8..07d8df6525b3 100644 --- a/dom/base/StructuredCloneTags.h +++ b/dom/base/StructuredCloneTags.h @@ -44,9 +44,15 @@ namespace dom { enum StructuredCloneTags { SCTAG_BASE = JS_SCTAG_USER_MIN, + + // These tags are used only for main thread structured clone. SCTAG_DOM_BLOB, SCTAG_DOM_FILE, SCTAG_DOM_FILELIST, + + // These tags are used for both main thread and workers. + SCTAG_DOM_IMAGEDATA, + SCTAG_DOM_MAX }; diff --git a/dom/base/nsJSEnvironment.cpp b/dom/base/nsJSEnvironment.cpp index 0db2ca195bc6..3406e5393d28 100644 --- a/dom/base/nsJSEnvironment.cpp +++ b/dom/base/nsJSEnvironment.cpp @@ -86,6 +86,8 @@ #include "WrapperFactory.h" #include "nsGlobalWindow.h" #include "nsScriptNameSpaceManager.h" +#include "StructuredCloneTags.h" +#include "mozilla/dom/ImageData.h" #include "nsJSPrincipals.h" @@ -113,6 +115,7 @@ #include "sampler.h" using namespace mozilla; +using namespace mozilla::dom; const size_t gStackSize = 8192; @@ -3610,7 +3613,36 @@ NS_DOMReadStructuredClone(JSContext* cx, uint32_t data, void* closure) { - // We don't currently support any extensions to structured cloning. + if (tag == SCTAG_DOM_IMAGEDATA) { + // Read the information out of the stream. + uint32_t width, height; + JS::Value dataArray; + if (!JS_ReadUint32Pair(reader, &width, &height) || + !JS_ReadTypedArray(reader, &dataArray)) { + return nsnull; + } + MOZ_ASSERT(dataArray.isObject()); + + // Construct the ImageData. + nsCOMPtr imageData = new ImageData(width, height, + dataArray.toObject()); + // Wrap it in a jsval. + JSObject* global = JS_GetGlobalForScopeChain(cx); + if (!global) { + return nsnull; + } + nsCOMPtr wrapper; + JS::Value val; + nsresult rv = + nsContentUtils::WrapNative(cx, global, imageData, &val, + getter_AddRefs(wrapper)); + if (NS_FAILED(rv)) { + return nsnull; + } + return val.toObjectOrNull(); + } + + // Don't know what this is. Bail. nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_DOM_DATA_CLONE_ERR); return nsnull; } @@ -3621,7 +3653,30 @@ NS_DOMWriteStructuredClone(JSContext* cx, JSObject* obj, void *closure) { - // We don't currently support any extensions to structured cloning. + nsCOMPtr wrappedNative; + nsContentUtils::XPConnect()-> + GetWrappedNativeOfJSObject(cx, obj, getter_AddRefs(wrappedNative)); + nsISupports *native = wrappedNative ? wrappedNative->Native() : nsnull; + + nsCOMPtr imageData = do_QueryInterface(native); + if (imageData) { + // Prepare the ImageData internals. + PRUint32 width, height; + JS::Value dataArray; + if (NS_FAILED(imageData->GetWidth(&width)) || + NS_FAILED(imageData->GetHeight(&height)) || + NS_FAILED(imageData->GetData(cx, &dataArray))) + { + return false; + } + + // Write the internals to the stream. + return JS_WriteUint32Pair(writer, SCTAG_DOM_IMAGEDATA, 0) && + JS_WriteUint32Pair(writer, width, height) && + JS_WriteTypedArray(writer, dataArray); + } + + // Don't know what this is. Bail. nsDOMClassInfo::ThrowJSException(cx, NS_ERROR_DOM_DATA_CLONE_ERR); return JS_FALSE; }