Bug 795542 - Part 2: Implement StringEncoding API objects in Workers. r=bent

This commit is contained in:
Masatoshi Kimura 2012-12-22 09:16:14 +09:00
parent e9c46e4563
commit 139459454e
12 changed files with 279 additions and 3 deletions

View File

@ -446,9 +446,19 @@ DOMInterfaces = {
'resultNotAddRefed': [ 'getItem' ]
},
'TextEncoder': {
'TextDecoder': [
{
'workers': True,
}],
'TextEncoder': [
{
'implicitJSContext': [ 'encode' ],
},
{
'workers': True,
'implicitJSContext': [ 'encode' ],
}],
'URL' : {
'concrete': False,

View File

@ -30,6 +30,7 @@ MOCHITEST_FILES = \
test_stringencoding.html \
test_submit_euckr.html \
test_utf16_files.html \
worker_helper.js \
$(NULL)
MOCHITEST_CHROME_FILES = \

View File

@ -2,16 +2,18 @@
<html>
<head>
<meta charset=utf-8>
<title>Test for Bug 764234</title>
<title>Test for TextDecoder</title>
<script type="text/javascript" src="/resources/testharness.js"></script>
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
<script type="text/javascript" src="test_TextDecoder.js"></script>
<script type="text/javascript" src="test_BOMEncoding.js"></script>
<script type="text/javascript" src="worker_helper.js"></script>
</head>
<body>
<div id="log"></div>
<script>
setup({explicit_done: true});
runTest();
function runTest()
@ -20,6 +22,8 @@ function runTest()
runTextDecoderBOMEnoding();
}
runTestInWorker(["test_TextDecoder.js", "test_BOMEncoding.js"]);
</script>
</body>
</html>

View File

@ -2,15 +2,17 @@
<html>
<head>
<meta charset=utf-8>
<title>Test for Bug 764234</title>
<title>Test for TextEncoder</title>
<script type="text/javascript" src="/resources/testharness.js"></script>
<script type="text/javascript" src="/resources/testharnessreport.js"></script>
<script type="text/javascript" src="test_TextEncoder.js"></script>
<script type="text/javascript" src="worker_helper.js"></script>
</head>
<body>
<div id="log"></div>
<script>
setup({explicit_done: true});
runTest();
function runTest()
@ -18,6 +20,8 @@ function runTest()
runTextEncoderTests();
}
runTestInWorker(["test_TextEncoder.js"]);
</script>
</body>
</html>

View File

@ -0,0 +1,48 @@
/*
* worker_helper.js
* bug 764234 tests
*/
function runTestInWorker(files) {
function workerRun() {
var tests = [];
var asserts;
test = function(func, msg) {
asserts = [];
tests.push({asserts: asserts, msg: msg});
}
assert_equals = function(result, expected, msg) {
asserts.push(["assert_equals", result, expected, msg]);
};
assert_true = function(condition, msg) {
asserts.push(["assert_true", condition, msg]);
};
assert_unreached = function(condition, msg) {
asserts.push(["assert_unreached", condition, msg]);
};
onmessage = function(event) {
importScripts.apply(self, event.data);
runTest();
postMessage(tests);
};
}
var url = URL.createObjectURL(new Blob([
runTest.toString(), "\n\n",
"(", workerRun.toString(), ")();"
]));
var worker = new Worker(url);
var base = location.toString().replace(/\/[^\/]*$/,"/");
worker.postMessage(files.map(function(f) { return base + f; }));
worker.onmessage = function(event) {
URL.revokeObjectURL(url);
event.data.forEach(function(t) {
test(function() {
t.asserts.forEach(function(a) {
func = a.shift();
self[func].apply(self, a);
});
}, "worker " + t.msg);
});
done();
};
}

View File

@ -7,12 +7,16 @@
#define mozilla_dom_workers_dombindinginlines_h__
#include "mozilla/dom/FileReaderSyncBinding.h"
#include "mozilla/dom/TextDecoderBinding.h"
#include "mozilla/dom/TextEncoderBinding.h"
#include "mozilla/dom/XMLHttpRequestBinding.h"
#include "mozilla/dom/XMLHttpRequestUploadBinding.h"
BEGIN_WORKERS_NAMESPACE
class FileReaderSync;
class TextDecoder;
class TextEncoder;
class XMLHttpRequest;
class XMLHttpRequestUpload;
@ -44,6 +48,8 @@ struct WrapPrototypeTraits
};
SPECIALIZE_PROTO_TRAITS(FileReaderSync)
SPECIALIZE_PROTO_TRAITS(TextDecoder)
SPECIALIZE_PROTO_TRAITS(TextEncoder)
SPECIALIZE_PROTO_TRAITS(XMLHttpRequest)
SPECIALIZE_PROTO_TRAITS(XMLHttpRequestUpload)

View File

@ -30,6 +30,8 @@ CPPSRCS = \
Principal.cpp \
RuntimeService.cpp \
ScriptLoader.cpp \
TextDecoder.cpp \
TextEncoder.cpp \
Worker.cpp \
WorkerPrivate.cpp \
WorkerScope.cpp \
@ -52,6 +54,8 @@ EXPORTS_mozilla/dom/workers/bindings = \
EventListenerManager.h \
EventTarget.h \
FileReaderSync.h \
TextDecoder.h \
TextEncoder.h \
WorkerFeature.h \
XMLHttpRequestEventTarget.h \
XMLHttpRequestUpload.h \

View File

@ -0,0 +1,44 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TextDecoder.h"
#include "DOMBindingInlines.h"
USING_WORKERS_NAMESPACE
using mozilla::ErrorResult;
using mozilla::dom::TextDecoderOptionsWorkers;
void
TextDecoder::_trace(JSTracer* aTrc)
{
DOMBindingBase::_trace(aTrc);
}
void
TextDecoder::_finalize(JSFreeOp* aFop)
{
DOMBindingBase::_finalize(aFop);
}
// static
TextDecoder*
TextDecoder::Constructor(JSContext* aCx, JSObject* aObj,
const nsAString& aEncoding,
const TextDecoderOptionsWorkers& aOptions,
ErrorResult& aRv)
{
nsRefPtr<TextDecoder> txtDecoder = new TextDecoder(aCx);
txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (!Wrap(aCx, aObj, txtDecoder)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
return txtDecoder;
}

52
dom/workers/TextDecoder.h Normal file
View File

@ -0,0 +1,52 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_workers_textdecoder_h_
#define mozilla_dom_workers_textdecoder_h_
#include "mozilla/dom/TextDecoderBase.h"
#include "mozilla/dom/workers/bindings/DOMBindingBase.h"
#include "mozilla/dom/TextDecoderBinding.h"
BEGIN_WORKERS_NAMESPACE
class TextDecoder MOZ_FINAL : public DOMBindingBase,
public TextDecoderBase
{
protected:
TextDecoder(JSContext* aCx)
: DOMBindingBase(aCx)
{}
virtual
~TextDecoder()
{}
public:
virtual void
_trace(JSTracer* aTrc) MOZ_OVERRIDE;
virtual void
_finalize(JSFreeOp* aFop) MOZ_OVERRIDE;
static TextDecoder*
Constructor(JSContext* aCx, JSObject* aObj,
const nsAString& aEncoding,
const TextDecoderOptionsWorkers& aOptions,
ErrorResult& aRv);
void
Decode(const ArrayBufferView* aView,
const TextDecodeOptionsWorkers& aOptions,
nsAString& aOutDecodedString,
ErrorResult& aRv) {
return TextDecoderBase::Decode(aView, aOptions.mStream,
aOutDecodedString, aRv);
}
};
END_WORKERS_NAMESPACE
#endif // mozilla_dom_workers_textdecoder_h_

View File

@ -0,0 +1,42 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "TextEncoder.h"
#include "DOMBindingInlines.h"
USING_WORKERS_NAMESPACE
using mozilla::ErrorResult;
void
TextEncoder::_trace(JSTracer* aTrc)
{
DOMBindingBase::_trace(aTrc);
}
void
TextEncoder::_finalize(JSFreeOp* aFop)
{
DOMBindingBase::_finalize(aFop);
}
// static
TextEncoder*
TextEncoder::Constructor(JSContext* aCx, JSObject* aObj,
const nsAString& aEncoding,
ErrorResult& aRv)
{
nsRefPtr<TextEncoder> txtEncoder = new TextEncoder(aCx);
txtEncoder->Init(aEncoding, aRv);
if (aRv.Failed()) {
return nullptr;
}
if (!Wrap(aCx, aObj, txtEncoder)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
return txtEncoder;
}

57
dom/workers/TextEncoder.h Normal file
View File

@ -0,0 +1,57 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_workers_textencoder_h_
#define mozilla_dom_workers_textencoder_h_
#include "mozilla/dom/TextEncoderBase.h"
#include "mozilla/dom/workers/bindings/DOMBindingBase.h"
#include "mozilla/dom/TextEncoderBinding.h"
BEGIN_WORKERS_NAMESPACE
class TextEncoder MOZ_FINAL : public DOMBindingBase,
public TextEncoderBase
{
protected:
TextEncoder(JSContext* aCx)
: DOMBindingBase(aCx)
{}
virtual
~TextEncoder()
{}
virtual JSObject*
CreateUint8Array(JSContext* aCx, char* aBuf, uint32_t aLen) MOZ_OVERRIDE
{
return Uint8Array::Create(aCx, this, aLen,
reinterpret_cast<uint8_t*>(aBuf));
}
public:
virtual void
_trace(JSTracer* aTrc) MOZ_OVERRIDE;
virtual void
_finalize(JSFreeOp* aFop) MOZ_OVERRIDE;
static TextEncoder*
Constructor(JSContext* aCx, JSObject* aObj,
const nsAString& aEncoding,
ErrorResult& aRv);
JSObject*
Encode(JSContext* aCx,
const nsAString& aString,
const TextEncodeOptionsWorkers& aOptions,
ErrorResult& aRv) {
return TextEncoderBase::Encode(aCx, aString, aOptions.mStream, aRv);
}
};
END_WORKERS_NAMESPACE
#endif // mozilla_dom_workers_textencoder_h_

View File

@ -13,6 +13,8 @@
#include "mozilla/dom/EventTargetBinding.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/FileReaderSyncBinding.h"
#include "mozilla/dom/TextDecoderBinding.h"
#include "mozilla/dom/TextEncoderBinding.h"
#include "mozilla/dom/XMLHttpRequestBinding.h"
#include "mozilla/dom/XMLHttpRequestUploadBinding.h"
#include "mozilla/OSFileConstants.h"
@ -983,6 +985,8 @@ CreateDedicatedWorkerGlobalScope(JSContext* aCx)
// Init other paris-bindings.
if (!FileReaderSyncBinding_workers::GetConstructorObject(aCx, global) ||
!TextDecoderBinding_workers::GetConstructorObject(aCx, global) ||
!TextEncoderBinding_workers::GetConstructorObject(aCx, global) ||
!XMLHttpRequestBinding_workers::GetConstructorObject(aCx, global) ||
!XMLHttpRequestUploadBinding_workers::GetConstructorObject(aCx, global)) {
return NULL;