Bug 1135627 - Add direct Response overload for FetchEvent.respondWith(). r=jdm,nsm,ehsan

--HG--
extra : rebase_source : 1df0b695586ff6273e7e5ded032c2daa3d5e1e31
This commit is contained in:
Timur Valeev 2015-03-18 06:17:11 +03:00
Родитель 962b231116
Коммит 4a14abec30
5 изменённых файлов: 37 добавлений и 0 удалений

Просмотреть файл

@ -18,6 +18,7 @@ interface FetchEvent : Event {
readonly attribute boolean isReload;
[Throws] void respondWith(Promise<Response> r);
[Throws] void respondWith(Response r);
};
dictionary FetchEventInit : EventInit {

Просмотреть файл

@ -23,6 +23,8 @@
#include "mozilla/dom/WorkerScope.h"
#include "mozilla/dom/workers/bindings/ServiceWorker.h"
#include "WorkerPrivate.h"
using namespace mozilla::dom;
BEGIN_WORKERS_NAMESPACE
@ -283,6 +285,26 @@ FetchEvent::RespondWith(Promise& aPromise, ErrorResult& aRv)
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()
{

Просмотреть файл

@ -11,6 +11,7 @@
#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;
@ -83,6 +84,9 @@ public:
void
RespondWith(Promise& aPromise, ErrorResult& aRv);
void
RespondWith(Response& aResponse, ErrorResult& aRv);
already_AddRefed<Promise>
ForwardTo(const nsAString& aUrl);

Просмотреть файл

@ -27,6 +27,12 @@ fetch('synthesized.txt', function(xhr) {
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");

Просмотреть файл

@ -21,6 +21,10 @@ onfetch = function(ev) {
));
}
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")) {
}