Bug 795542 - Part 2: Implement StringEncoding API objects in Workers. r=bent
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Sat, 22 Dec 2012 09:16:14 +0900
changeset 116846 ac143af3f698c4dc79710b724180c160864ae11a
parent 116845 905b2cfe26aeda9da0d1519fdb0b303a4a64e60c
child 116847 847d24f0a8da938a961afeb8639a98d51faad2a2
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbent
bugs795542
milestone20.0a1
Bug 795542 - Part 2: Implement StringEncoding API objects in Workers. r=bent
dom/bindings/Bindings.conf
dom/encoding/test/Makefile.in
dom/encoding/test/test_TextDecoder.html
dom/encoding/test/test_TextEncoder.html
dom/encoding/test/worker_helper.js
dom/workers/DOMBindingInlines.h
dom/workers/Makefile.in
dom/workers/TextDecoder.cpp
dom/workers/TextDecoder.h
dom/workers/TextEncoder.cpp
dom/workers/TextEncoder.h
dom/workers/WorkerScope.cpp
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -441,19 +441,29 @@ DOMInterfaces = {
 },
 
 'SVGTransformList': {
     'nativeType': 'mozilla::DOMSVGTransformList',
     'headerFile': 'DOMSVGTransformList.h',
     'resultNotAddRefed': [ 'getItem' ]
 },
 
-'TextEncoder': {
+'TextDecoder': [
+{
+    'workers': True,
+}],
+
+'TextEncoder': [
+{
     'implicitJSContext': [ 'encode' ],
 },
+{
+    'workers': True,
+    'implicitJSContext': [ 'encode' ],
+}],
 
 'URL' : {
     'concrete': False,
 },
 
 'WebGLActiveInfo': {
    'nativeType': 'mozilla::WebGLActiveInfo',
    'headerFile': 'WebGLContext.h',
--- a/dom/encoding/test/Makefile.in
+++ b/dom/encoding/test/Makefile.in
@@ -25,16 +25,17 @@ MOCHITEST_FILES = \
   test_BOMEncoding.js \
   test_TextDecoder.html \
   test_TextDecoder.js \
   test_TextEncoder.html \
   test_TextEncoder.js \
   test_stringencoding.html \
   test_submit_euckr.html \
   test_utf16_files.html \
+  worker_helper.js \
   $(NULL)
 
 MOCHITEST_CHROME_FILES = \
   file_stringencoding.jsm \
   test_stringencoding.xul \
   $(NULL)
 
 include $(topsrcdir)/config/rules.mk
--- a/dom/encoding/test/test_TextDecoder.html
+++ b/dom/encoding/test/test_TextDecoder.html
@@ -1,25 +1,29 @@
 <!DOCTYPE HTML>
 <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()
 {
   runTextDecoderOptions();
   runTextDecoderBOMEnoding();
 }
 
+runTestInWorker(["test_TextDecoder.js", "test_BOMEncoding.js"]);
+
 </script>
 </body>
 </html>
--- a/dom/encoding/test/test_TextEncoder.html
+++ b/dom/encoding/test/test_TextEncoder.html
@@ -1,23 +1,27 @@
 <!DOCTYPE HTML>
 <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()
 {
   runTextEncoderTests();
 }
 
+runTestInWorker(["test_TextEncoder.js"]);
+
 </script>
 </body>
 </html>
new file mode 100644
--- /dev/null
+++ b/dom/encoding/test/worker_helper.js
@@ -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();
+  };
+}
--- a/dom/workers/DOMBindingInlines.h
+++ b/dom/workers/DOMBindingInlines.h
@@ -2,22 +2,26 @@
 /* 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_dombindinginlines_h__
 #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;
 
 namespace {
 
 template <class T>
 struct WrapPrototypeTraits
 { };
@@ -39,16 +43,18 @@ struct WrapPrototypeTraits
     GetProtoObject(JSContext* aCx, JSObject* aGlobal)                          \
     {                                                                          \
       using namespace mozilla::dom;                                            \
       return _class##Binding_workers::GetProtoObject(aCx, aGlobal);            \
     }                                                                          \
   };
 
 SPECIALIZE_PROTO_TRAITS(FileReaderSync)
+SPECIALIZE_PROTO_TRAITS(TextDecoder)
+SPECIALIZE_PROTO_TRAITS(TextEncoder)
 SPECIALIZE_PROTO_TRAITS(XMLHttpRequest)
 SPECIALIZE_PROTO_TRAITS(XMLHttpRequestUpload)
 
 #undef SPECIALIZE_PROTO_TRAITS
 
 } // anonymous namespace
 
 template <class T>
--- a/dom/workers/Makefile.in
+++ b/dom/workers/Makefile.in
@@ -25,16 +25,18 @@ CPPSRCS = \
   File.cpp \
   FileReaderSync.cpp \
   ImageData.cpp \
   Location.cpp \
   Navigator.cpp \
   Principal.cpp \
   RuntimeService.cpp \
   ScriptLoader.cpp \
+  TextDecoder.cpp \
+  TextEncoder.cpp \
   Worker.cpp \
   WorkerPrivate.cpp \
   WorkerScope.cpp \
   XMLHttpRequestEventTarget.cpp \
   XMLHttpRequestUpload.cpp \
   XMLHttpRequest.cpp \
   $(NULL)
 
@@ -47,16 +49,18 @@ EXPORTS_NAMESPACES = \
 EXPORTS_mozilla/dom/workers = Workers.h
 
 # Stuff needed for the bindings, not really public though.
 EXPORTS_mozilla/dom/workers/bindings = \
   DOMBindingBase.h \
   EventListenerManager.h \
   EventTarget.h \
   FileReaderSync.h \
+  TextDecoder.h \
+  TextEncoder.h \
   WorkerFeature.h \
   XMLHttpRequestEventTarget.h \
   XMLHttpRequestUpload.h \
   XMLHttpRequest.h \
   $(NULL)
 
 LOCAL_INCLUDES = \
   -I$(topsrcdir)/content/base/src \
new file mode 100644
--- /dev/null
+++ b/dom/workers/TextDecoder.cpp
@@ -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;
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/TextDecoder.h
@@ -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_
new file mode 100644
--- /dev/null
+++ b/dom/workers/TextEncoder.cpp
@@ -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;
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/TextEncoder.h
@@ -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_
--- a/dom/workers/WorkerScope.cpp
+++ b/dom/workers/WorkerScope.cpp
@@ -8,16 +8,18 @@
 
 #include "jsapi.h"
 #include "jsdbgapi.h"
 #include "mozilla/Util.h"
 #include "mozilla/dom/DOMJSClass.h"
 #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"
 #include "nsTraceRefcnt.h"
 #include "xpcpublic.h"
 
 #ifdef ANDROID
 #include <android/log.h>
@@ -978,16 +980,18 @@ CreateDedicatedWorkerGlobalScope(JSConte
       !location::InitClass(aCx, global) ||
       !imagedata::InitClass(aCx, global) ||
       !navigator::InitClass(aCx, global)) {
     return NULL;
   }
 
   // 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;
   }
 
   if (!JS_DefineProfilingFunctions(aCx, global)) {
     return NULL;
   }