Bug 1146365 - Add support of union type for FetchEvent.respondWith. r=nsm, r=ehsan

This commit is contained in:
Timur Valeev 2015-03-24 10:37:39 +03:00
parent d82e49937f
commit 19a398b233
3 changed files with 21 additions and 28 deletions

View File

@ -17,8 +17,8 @@ interface FetchEvent : Event {
readonly attribute Client? client; // The window issuing the request.
readonly attribute boolean isReload;
[Throws] void respondWith(Promise<Response> r);
[Throws] void respondWith(Response r);
[Throws]
void respondWith((Response or Promise<Response>) r);
};
dictionary FetchEventInit : EventInit {

View File

@ -282,37 +282,32 @@ RespondWithHandler::CancelRequest()
} // anonymous namespace
void
FetchEvent::RespondWith(Promise& aPromise, ErrorResult& aRv)
FetchEvent::RespondWith(const ResponseOrPromise& aArg, ErrorResult& aRv)
{
if (mWaitToRespond) {
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
return;
}
nsRefPtr<Promise> promise;
if (aArg.IsResponse()) {
nsRefPtr<Response> res = &aArg.GetAsResponse();
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(worker);
worker->AssertIsOnWorkerThread();
promise = Promise::Create(worker->GlobalScope(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
promise->MaybeResolve(res);
} else if (aArg.IsPromise()) {
promise = &aArg.GetAsPromise();
}
mWaitToRespond = true;
nsRefPtr<RespondWithHandler> handler =
new RespondWithHandler(mChannel, mServiceWorker, mRequest->Mode());
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);
promise->AppendNativeHandler(handler);
}
already_AddRefed<ServiceWorkerClient>

View File

@ -19,6 +19,7 @@ class nsIInterceptedChannel;
namespace mozilla {
namespace dom {
class Request;
class ResponseOrPromise;
} // namespace dom
} // namespace mozilla
@ -82,10 +83,7 @@ public:
}
void
RespondWith(Promise& aPromise, ErrorResult& aRv);
void
RespondWith(Response& aResponse, ErrorResult& aRv);
RespondWith(const ResponseOrPromise& aArg, ErrorResult& aRv);
already_AddRefed<Promise>
ForwardTo(const nsAString& aUrl);