Bug 1136757 - Add direct Response overload for FetchEvent.respondWith(). r=jdm,nsm,ehsan
authorTimur Valeev <tvaleev@gmail.com>
Fri, 20 Mar 2015 08:44:20 -0700
changeset 263614 0f7f9fc972cf8f1b33ea6b9bded9b9329762fe33
parent 263613 8deee34f9ea2bbe1b241345e0f21f62b72694a88
child 263615 4113f4e7e3e3ee8f9dd079001f44dc9c9f72630f
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm, nsm, ehsan
bugs1136757
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 1136757 - Add direct Response overload for FetchEvent.respondWith(). r=jdm,nsm,ehsan ON CLOSED TREE Fix bug number
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")) {