From 4d93b5343a094125214e68afe4be8736332ac6ec Mon Sep 17 00:00:00 2001 From: Masatoshi Kimura Date: Sat, 24 Mar 2012 12:33:59 +0100 Subject: [PATCH] Bug 736686 - Part 2: Implement Blob constructor in Worker. r=jonas, bent --- content/base/src/nsDOMBlobBuilder.cpp | 19 ++++++- content/base/src/nsDOMBlobBuilder.h | 6 +++ dom/workers/File.cpp | 28 ++++++++-- dom/workers/test/Makefile.in | 1 + dom/workers/test/test_blobConstructor.html | 59 ++++++++++++++++++++++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 dom/workers/test/test_blobConstructor.html diff --git a/content/base/src/nsDOMBlobBuilder.cpp b/content/base/src/nsDOMBlobBuilder.cpp index 280156422c0a..f28cd31afced 100644 --- a/content/base/src/nsDOMBlobBuilder.cpp +++ b/content/base/src/nsDOMBlobBuilder.cpp @@ -179,12 +179,28 @@ nsDOMMultipartFile::NewBlob(nsISupports* *aNewObject) return NS_OK; } +static nsIDOMBlob* +GetXPConnectNative(JSContext* aCx, JSObject* aObj) { + nsCOMPtr blob = do_QueryInterface( + nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, aObj)); + return blob; +} + NS_IMETHODIMP nsDOMMultipartFile::Initialize(nsISupports* aOwner, JSContext* aCx, JSObject* aObj, PRUint32 aArgc, jsval* aArgv) +{ + return InitInternal(aCx, aArgc, aArgv, GetXPConnectNative); +} + +nsresult +nsDOMMultipartFile::InitInternal(JSContext* aCx, + PRUint32 aArgc, + jsval* aArgv, + UnwrapFuncPtr aUnwrapFunc) { bool nativeEOL = false; if (aArgc > 1) { @@ -221,8 +237,7 @@ nsDOMMultipartFile::Initialize(nsISupports* aOwner, if (element.isObject()) { JSObject& obj = element.toObject(); - nsCOMPtr blob = do_QueryInterface( - nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, &obj)); + nsCOMPtr blob = aUnwrapFunc(aCx, &obj); if (blob) { // Flatten so that multipart blobs will never nest nsDOMFileBase* file = static_cast( diff --git a/content/base/src/nsDOMBlobBuilder.h b/content/base/src/nsDOMBlobBuilder.h index ce70d6d8dbad..8cf5cad623db 100644 --- a/content/base/src/nsDOMBlobBuilder.h +++ b/content/base/src/nsDOMBlobBuilder.h @@ -81,6 +81,12 @@ public: PRUint32 aArgc, jsval* aArgv); + typedef nsIDOMBlob* (*UnwrapFuncPtr)(JSContext*, JSObject*); + nsresult InitInternal(JSContext* aCx, + PRUint32 aArgc, + jsval* aArgv, + UnwrapFuncPtr aUnwrapFunc); + already_AddRefed CreateSlice(PRUint64 aStart, PRUint64 aLength, const nsAString& aContentType); diff --git a/dom/workers/File.cpp b/dom/workers/File.cpp index 785cd3fbe53f..e6fc6cc55080 100644 --- a/dom/workers/File.cpp +++ b/dom/workers/File.cpp @@ -40,6 +40,7 @@ #include "File.h" #include "nsIDOMFile.h" +#include "nsDOMBlobBuilder.h" #include "jsapi.h" #include "jsatom.h" @@ -57,6 +58,7 @@ USING_WORKERS_NAMESPACE +using mozilla::dom::workers::exceptions::ThrowDOMExceptionForCode; using mozilla::dom::workers::exceptions::ThrowFileExceptionForCode; namespace { @@ -110,12 +112,32 @@ private: return NULL; } + static nsIDOMBlob* + Unwrap(JSContext* aCx, JSObject* aObj) + { + return GetPrivate(aObj); + } + static JSBool Construct(JSContext* aCx, unsigned aArgc, jsval* aVp) { - JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR, - sClass.name); - return false; + nsRefPtr file = new nsDOMMultipartFile(); + nsresult rv = file->InitInternal(aCx, aArgc, JS_ARGV(aCx, aVp), + Unwrap); + if (NS_FAILED(rv)) { + ThrowDOMExceptionForCode(aCx, + NS_ERROR_GET_MODULE(rv) == NS_ERROR_MODULE_DOM ? + NS_ERROR_GET_CODE(rv) : UNKNOWN_ERR); + return false; + } + + JSObject* obj = file::CreateBlob(aCx, file); + if (!obj) { + return false; + } + + JS_SET_RVAL(aCx, aVp, OBJECT_TO_JSVAL(obj)); + return true; } static void diff --git a/dom/workers/test/Makefile.in b/dom/workers/test/Makefile.in index 7dd748206124..efe31ede1d38 100644 --- a/dom/workers/test/Makefile.in +++ b/dom/workers/test/Makefile.in @@ -123,6 +123,7 @@ _TEST_FILES = \ test_xhr_implicit_cancel.html \ xhr_implicit_cancel_worker.js \ test_xhr_timeout.html \ + test_blobConstructor.html \ $(NULL) _SUBDIR_TEST_FILES = \ diff --git a/dom/workers/test/test_blobConstructor.html b/dom/workers/test/test_blobConstructor.html new file mode 100644 index 000000000000..8daca8469201 --- /dev/null +++ b/dom/workers/test/test_blobConstructor.html @@ -0,0 +1,59 @@ + + + + + + Test for DOM Worker Blob constructor + + + + +

+ +
+
+
+ +