Bug 736686 - Part 2: Implement Blob constructor in Worker. r=jonas, bent

This commit is contained in:
Masatoshi Kimura 2012-03-24 12:33:59 +01:00
parent 827b7b59cf
commit 4d93b5343a
5 changed files with 108 additions and 5 deletions

View File

@ -179,12 +179,28 @@ nsDOMMultipartFile::NewBlob(nsISupports* *aNewObject)
return NS_OK;
}
static nsIDOMBlob*
GetXPConnectNative(JSContext* aCx, JSObject* aObj) {
nsCOMPtr<nsIDOMBlob> 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<nsIDOMBlob> blob = do_QueryInterface(
nsContentUtils::XPConnect()->GetNativeOfWrapper(aCx, &obj));
nsCOMPtr<nsIDOMBlob> blob = aUnwrapFunc(aCx, &obj);
if (blob) {
// Flatten so that multipart blobs will never nest
nsDOMFileBase* file = static_cast<nsDOMFileBase*>(

View File

@ -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<nsIDOMBlob>
CreateSlice(PRUint64 aStart, PRUint64 aLength, const nsAString& aContentType);

View File

@ -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<nsDOMMultipartFile> 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

View File

@ -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 = \

View File

@ -0,0 +1,59 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE html>
<html>
<!--
Tests of DOM Worker Blob constructor
-->
<head>
<title>Test for DOM Worker Blob constructor</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
(function() {
onerror = function(e) {
ok(false, "Main Thread had an error: " + event.data);
SimpleTest.finish();
};
function f() {
onmessage = function(e) {
var b = new Blob([e.data, "World"],{});
var fr = new FileReaderSync();
postMessage(fr.readAsText(b));
};
}
var b = new Blob([f.toString(),"f();"]);
var u = URL.createObjectURL(b);
var w = new Worker(u);
w.onmessage = function(e) {
URL.revokeObjectURL(u);
is(e.data, fr.result);
SimpleTest.finish();
};
w.onerror = function(e) {
is(e.target, w);
ok(false, "Worker had an error: " + e.data);
SimpleTest.finish();
};
b = new Blob(["Hello, "]);
var fr = new FileReader();
fr.readAsText(new Blob([b, "World"],{}));
fr.onload = function() {
w.postMessage(b);
};
SimpleTest.waitForExplicitFinish();
})();
</script>
</pre>
</body>
</html>