Bug 1135627 - Add direct Response overload for FetchEvent.respondWith(). r=jdm,nsm,ehsan
☠☠ backed out by 8deee34f9ea2 ☠ ☠
authorTimur Valeev <tvaleev@gmail.com>
Wed, 18 Mar 2015 06:17:11 +0300
changeset 234685 62b55b1f9b7e19be8a69d45076c391bc57439561
parent 234684 6abbe0f834790dd4580b23bfa40fbc0ec0cb5909
child 234686 f33123f726b6a127aa3065fa049823c25d58a36c
push id57210
push usernsm.nikhil@gmail.com
push dateFri, 20 Mar 2015 15:34:31 +0000
treeherdermozilla-inbound@62b55b1f9b7e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm, nsm, ehsan
bugs1135627
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1135627 - Add direct Response overload for FetchEvent.respondWith(). r=jdm,nsm,ehsan
dom/webidl/FetchEvent.webidl
dom/workers/ServiceWorkerEvents.cpp
dom/workers/ServiceWorkerEvents.h
dom/workers/test/serviceworkers/fetch/fetch_tests.js
dom/workers/test/serviceworkers/fetch_event_worker.js
--- a/dom/webidl/FetchEvent.webidl
+++ b/dom/webidl/FetchEvent.webidl
@@ -13,15 +13,16 @@
 interface FetchEvent : Event {
   readonly attribute Request request;
 
   // https://github.com/slightlyoff/ServiceWorker/issues/631
   readonly attribute Client? client; // The window issuing the request.
   readonly attribute boolean isReload;
 
   [Throws] void respondWith(Promise<Response> r);
+  [Throws] void respondWith(Response r);
 };
 
 dictionary FetchEventInit : EventInit {
   Request request;
   Client client;
   boolean isReload;
 };
--- a/dom/workers/ServiceWorkerEvents.cpp
+++ b/dom/workers/ServiceWorkerEvents.cpp
@@ -18,16 +18,18 @@
 
 #include "mozilla/dom/FetchEventBinding.h"
 #include "mozilla/dom/PromiseNativeHandler.h"
 #include "mozilla/dom/Request.h"
 #include "mozilla/dom/Response.h"
 #include "mozilla/dom/WorkerScope.h"
 #include "mozilla/dom/workers/bindings/ServiceWorker.h"
 
+#include "WorkerPrivate.h"
+
 using namespace mozilla::dom;
 
 BEGIN_WORKERS_NAMESPACE
 
 FetchEvent::FetchEvent(EventTarget* aOwner)
 : Event(aOwner, nullptr, nullptr)
 , mIsReload(false)
 , mWaitToRespond(false)
@@ -278,16 +280,36 @@ FetchEvent::RespondWith(Promise& aPromis
     return;
   }
 
   mWaitToRespond = true;
   nsRefPtr<RespondWithHandler> handler = new RespondWithHandler(mChannel, mServiceWorker);
   aPromise.AppendNativeHandler(handler);
 }
 
+void
+FetchEvent::RespondWith(Response& aResponse, ErrorResult& aRv)
+{
+  if (mWaitToRespond) {
+    aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
+    return;
+  }
+
+  WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
+  MOZ_ASSERT(worker);
+  worker->AssertIsOnWorkerThread();
+  nsRefPtr<Promise> promise = Promise::Create(worker->GlobalScope(), aRv);
+  if (NS_WARN_IF(aRv.Failed())) {
+    return;
+  }
+  promise->MaybeResolve(&aResponse);
+
+  RespondWith(*promise, aRv);
+}
+
 already_AddRefed<ServiceWorkerClient>
 FetchEvent::GetClient()
 {
   if (!mClient) {
     if (!mClientInfo) {
       return nullptr;
     }
 
--- a/dom/workers/ServiceWorkerEvents.h
+++ b/dom/workers/ServiceWorkerEvents.h
@@ -6,16 +6,17 @@
 #ifndef mozilla_dom_workers_serviceworkerevents_h__
 #define mozilla_dom_workers_serviceworkerevents_h__
 
 #include "mozilla/dom/Event.h"
 #include "mozilla/dom/ExtendableEventBinding.h"
 #include "mozilla/dom/FetchEventBinding.h"
 #include "mozilla/dom/InstallEventBinding.h"
 #include "mozilla/dom/Promise.h"
+#include "mozilla/dom/Response.h"
 #include "nsProxyRelease.h"
 
 class nsIInterceptedChannel;
 
 namespace mozilla {
 namespace dom {
   class Request;
 } // namespace dom
@@ -78,16 +79,19 @@ public:
   IsReload() const
   {
     return mIsReload;
   }
 
   void
   RespondWith(Promise& aPromise, ErrorResult& aRv);
 
+  void
+  RespondWith(Response& aResponse, ErrorResult& aRv);
+
   already_AddRefed<Promise>
   ForwardTo(const nsAString& aUrl);
 
   already_AddRefed<Promise>
   Default();
 };
 
 class ExtendableEvent : public Event
--- a/dom/workers/test/serviceworkers/fetch/fetch_tests.js
+++ b/dom/workers/test/serviceworkers/fetch/fetch_tests.js
@@ -22,16 +22,22 @@ function fetch(name, onload, onerror, he
 }
 
 fetch('synthesized.txt', function(xhr) {
   my_ok(xhr.status == 200, "load should be successful");
   my_ok(xhr.responseText == "synthesized response body", "load should have synthesized response");
   finish();
 });
 
+fetch('test-respondwith-response.txt', function(xhr) {
+  my_ok(xhr.status == 200, "test-respondwith-response load should be successful");
+  my_ok(xhr.responseText == "test-respondwith-response response body", "load should have response");
+  finish();
+});
+
 fetch('synthesized-404.txt', function(xhr) {
   my_ok(xhr.status == 404, "load should 404");
   my_ok(xhr.responseText == "synthesized response body", "404 load should have synthesized response");
   finish();
 });
 
 fetch('synthesized-headers.txt', function(xhr) {
   my_ok(xhr.status == 200, "load should be successful");
--- a/dom/workers/test/serviceworkers/fetch_event_worker.js
+++ b/dom/workers/test/serviceworkers/fetch_event_worker.js
@@ -16,16 +16,20 @@ onfetch = function(ev) {
       new Response("synthesized response body", {
         headers: {
           "X-Custom-Greeting": "Hello"
         }
       })
     ));
   }
 
+  else if (ev.request.url.contains("test-respondwith-response.txt")) {
+    ev.respondWith(new Response("test-respondwith-response response body", {}));
+  }
+
   else if (ev.request.url.contains("ignored.txt")) {
   }
 
   else if (ev.request.url.contains("rejected.txt")) {
     ev.respondWith(Promise.reject());
   }
 
   else if (ev.request.url.contains("nonresponse.txt")) {