Bug 1472324: Update Worklet: s/import/addModule. r=baku,karlt draft
authorArnaud Bienner <arnaud.bienner@gmail.com>
Tue, 10 Jul 2018 14:24:14 +0200
changeset 815988 64be88359f1ef0edc8ef2b7c893a9a2ae53a7d04
parent 815839 086667f4a18d7ef2588a769cab02b9b58b463548
push id115705
push userbmo:arnaud.bienner@gmail.com
push dateTue, 10 Jul 2018 12:24:35 +0000
reviewersbaku, karlt
bugs1472324
milestone63.0a1
Bug 1472324: Update Worklet: s/import/addModule. r=baku,karlt MozReview-Commit-ID: 4HNBeUyzgJS
dom/webidl/Worklet.webidl
dom/worklet/Worklet.cpp
dom/worklet/Worklet.h
dom/worklet/tests/test_audioWorklet.html
dom/worklet/tests/test_basic.html
dom/worklet/tests/test_console.html
dom/worklet/tests/test_dump.html
dom/worklet/tests/test_exception.html
dom/worklet/tests/test_import_with_cache.html
dom/worklet/tests/test_paintWorklet.html
--- a/dom/webidl/Worklet.webidl
+++ b/dom/webidl/Worklet.webidl
@@ -5,10 +5,14 @@
  *
  * The origin of this IDL file is
  * https://drafts.css-houdini.org/worklets/#idl-index
  */
 
 [Pref="dom.worklet.enabled"]
 interface Worklet {
   [NewObject, Throws, NeedsCallerType]
-  Promise<void> import(USVString moduleURL);
+  Promise<void> addModule(USVString moduleURL, optional WorkletOptions options);
 };
+
+dictionary WorkletOptions {
+  RequestCredentials credentials = "same-origin";
+};
--- a/dom/worklet/Worklet.cpp
+++ b/dom/worklet/Worklet.cpp
@@ -65,17 +65,18 @@ private:
 
 class WorkletFetchHandler final : public PromiseNativeHandler
                                 , public nsIStreamLoaderObserver
 {
 public:
   NS_DECL_THREADSAFE_ISUPPORTS
 
   static already_AddRefed<Promise>
-  Fetch(Worklet* aWorklet, const nsAString& aModuleURL, CallerType aCallerType,
+  Fetch(Worklet* aWorklet, const nsAString& aModuleURL,
+        const WorkletOptions& aOptions, CallerType aCallerType,
         ErrorResult& aRv)
   {
     MOZ_ASSERT(aWorklet);
     MOZ_ASSERT(NS_IsMainThread());
 
     nsCOMPtr<nsIGlobalObject> global =
       do_QueryInterface(aWorklet->GetParentObject());
     MOZ_ASSERT(global);
@@ -119,16 +120,17 @@ public:
         return promise.forget();
       }
     }
 
     RequestOrUSVString request;
     request.SetAsUSVString().Rebind(aModuleURL.Data(), aModuleURL.Length());
 
     RequestInit init;
+    init.mCredentials.Construct(aOptions.mCredentials);
 
     RefPtr<Promise> fetchPromise =
       FetchRequest(global, request, init, aCallerType, aRv);
     if (NS_WARN_IF(aRv.Failed())) {
       promise->MaybeReject(aRv);
       return promise.forget();
     }
 
@@ -512,21 +514,23 @@ Worklet::WrapObject(JSContext* aCx, JS::
   if (mWorkletType == eAudioWorklet) {
     return AudioWorklet_Binding::Wrap(aCx, this, aGivenProto);
   } else {
     return Worklet_Binding::Wrap(aCx, this, aGivenProto);
   }
 }
 
 already_AddRefed<Promise>
-Worklet::Import(const nsAString& aModuleURL, CallerType aCallerType,
-                ErrorResult& aRv)
+Worklet::AddModule(const nsAString& aModuleURL,
+                   const WorkletOptions& aOptions,
+                   CallerType aCallerType,
+                   ErrorResult& aRv)
 {
   MOZ_ASSERT(NS_IsMainThread());
-  return WorkletFetchHandler::Fetch(this, aModuleURL, aCallerType, aRv);
+  return WorkletFetchHandler::Fetch(this, aModuleURL, aOptions, aCallerType, aRv);
 }
 
 /* static */ already_AddRefed<WorkletGlobalScope>
 Worklet::CreateGlobalScope(JSContext* aCx, WorkletType aWorkletType)
 {
   WorkletThread::AssertIsOnWorkletThread();
 
   RefPtr<WorkletGlobalScope> scope;
--- a/dom/worklet/Worklet.h
+++ b/dom/worklet/Worklet.h
@@ -19,16 +19,17 @@ class nsIPrincipal;
 
 namespace mozilla {
 namespace dom {
 
 class Promise;
 class Worklet;
 class WorkletFetchHandler;
 class WorkletGlobalScope;
+struct WorkletOptions;
 class WorkletThread;
 enum class CallerType : uint32_t;
 
 class WorkletLoadInfo
 {
 public:
   WorkletLoadInfo();
   ~WorkletLoadInfo();
@@ -78,18 +79,19 @@ public:
   {
     return mWindow;
   }
 
   virtual JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   already_AddRefed<Promise>
-  Import(const nsAString& aModuleURL, CallerType aCallerType,
-         ErrorResult& aRv);
+  AddModule(const nsAString& aModuleURL,
+            const WorkletOptions& aOptions,
+            CallerType aCallerType, ErrorResult& aRv);
 
   WorkletType Type() const
   {
     return mWorkletType;
   }
 
   static already_AddRefed<WorkletGlobalScope>
   CreateGlobalScope(JSContext* aCx, WorkletType aWorkletType);
--- a/dom/worklet/tests/test_audioWorklet.html
+++ b/dom/worklet/tests/test_audioWorklet.html
@@ -38,13 +38,13 @@ function configureTest() {
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   ok(window.isSecureContext, "Test should run in secure context");
   var audioContext = new AudioContext();
   ok(audioContext.audioWorklet instanceof AudioWorklet,
      "AudioContext.audioWorklet should be an instance of AudioWorklet");
-  audioContext.audioWorklet.import("worklet_audioWorklet.js")
+  audioContext.audioWorklet.addModule("worklet_audioWorklet.js")
 }
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_basic.html
+++ b/dom/worklet/tests/test_basic.html
@@ -17,42 +17,42 @@ function configureTest() {
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   var audioContext = new AudioContext();
   ok(!!audioContext.audioWorklet, "audioContext.audioWorklet exists");
 
   // First loading
-  audioContext.audioWorklet.import("common.js")
+  audioContext.audioWorklet.addModule("common.js")
   .then(() => {
     ok(true, "Import should load a resource.");
   })
 
   // Second loading - same file
   .then(() => {
-    return audioContext.audioWorklet.import("common.js")
+    return audioContext.audioWorklet.addModule("common.js")
   })
   .then(() => {
     ok(true, "Import should load a resource.");
   })
 
   // 3rd loading - a network error
   .then(() => {
-    return audioContext.audioWorklet.import("404.js");
+    return audioContext.audioWorklet.addModule("404.js");
   })
   .then(() => {
     ok(false, "The loading should fail.");
   }, () => {
     ok(true, "The loading should fail.");
   })
 
   // 4th loading - a network error
   .then(() => {
-    return audioContext.audioWorklet.import("404.js");
+    return audioContext.audioWorklet.addModule("404.js");
   })
   .then(() => {
     ok(false, "The loading should fail.");
   }, () => {
     ok(true, "The loading should fail.");
   })
 
   // done
--- a/dom/worklet/tests/test_console.html
+++ b/dom/worklet/tests/test_console.html
@@ -35,14 +35,14 @@ function configureTest() {
   return SpecialPowers.pushPrefEnv(
     {"set": [["dom.audioworklet.enabled", true],
              ["dom.worklet.enabled", true]]});
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   var audioContext = new AudioContext();
-  audioContext.audioWorklet.import("worklet_console.js");
+  audioContext.audioWorklet.addModule("worklet_console.js");
 }
 
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_dump.html
+++ b/dom/worklet/tests/test_dump.html
@@ -14,17 +14,17 @@ function configureTest() {
   return SpecialPowers.pushPrefEnv(
     {"set": [["dom.audioworklet.enabled", true],
              ["dom.worklet.enabled", true]]});
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   var audioContext = new AudioContext();
-  audioContext.audioWorklet.import("worklet_dump.js")
+  audioContext.audioWorklet.addModule("worklet_dump.js")
   .then(() => {
     ok(true, "All good!");
     SimpleTest.finish();
   });
 }
 </script>
 </body>
 </html>
--- a/dom/worklet/tests/test_exception.html
+++ b/dom/worklet/tests/test_exception.html
@@ -15,26 +15,26 @@ function configureTest() {
     {"set": [["dom.audioworklet.enabled", true],
              ["dom.worklet.enabled", true]]});
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   // This loading should fail
   var audioContext = new AudioContext();
-  audioContext.audioWorklet.import("404.js")
+  audioContext.audioWorklet.addModule("404.js")
   .then(() => {
     ok(false, "We should not be called!");
   }, () => {
     ok(true, "The script thrown but we are still here.");
   })
 
   // This should throw from JS
   .then(() => {
-    return audioContext.audioWorklet.import("worklet_exception.js")
+    return audioContext.audioWorklet.addModule("worklet_exception.js")
   })
   .then(() => {
     ok(false, "We should not be called!");
   }, () => {
     ok(true, "The script thrown but we are still here.");
   })
 
   .then(() => {
--- a/dom/worklet/tests/test_import_with_cache.html
+++ b/dom/worklet/tests/test_import_with_cache.html
@@ -15,17 +15,17 @@ function configureTest() {
     {"set": [["dom.audioworklet.enabled", true],
              ["dom.worklet.enabled", true]]});
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
   var audioContext = new AudioContext();
   function loading() {
-    audioContext.audioWorklet.import("server_import_with_cache.sjs")
+    audioContext.audioWorklet.addModule("server_import_with_cache.sjs")
     .then(() => {
       ok(true, "Import should load a resource.");
     }, () => {
       ok(false, "Import should load a resource.");
     })
     .then(() => {
       done();
     });
--- a/dom/worklet/tests/test_paintWorklet.html
+++ b/dom/worklet/tests/test_paintWorklet.html
@@ -34,13 +34,13 @@ function configureTest() {
 
   return SpecialPowers.pushPrefEnv(
     {"set": [["dom.paintWorklet.enabled", true],
              ["dom.worklet.enabled", true]]});
 }
 
 // This function is called into an iframe.
 function runTestInIframe() {
-  paintWorklet.import("worklet_paintWorklet.js")
+  paintWorklet.addModule("worklet_paintWorklet.js")
 }
 </script>
 </body>
 </html>