diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index 0907a0faf7..fcd84e28b6 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -116,11 +116,13 @@ DOMInterfaces = { }, 'Cache': { - 'implicitJSContext': [ 'add', 'addAll' ], + 'implicitJSContext': [ 'add', 'addAll', 'match', 'matchAll', 'put', + 'delete', 'keys' ], 'nativeType': 'mozilla::dom::cache::Cache', }, 'CacheStorage': { + 'implicitJSContext': [ 'match' ], 'nativeType': 'mozilla::dom::cache::CacheStorage', }, diff --git a/dom/cache/AutoUtils.cpp b/dom/cache/AutoUtils.cpp index d1f354336e..bfa4cffd23 100644 --- a/dom/cache/AutoUtils.cpp +++ b/dom/cache/AutoUtils.cpp @@ -289,9 +289,9 @@ MatchInPutList(InternalRequest* aRequest, } // namespace void -AutoChildOpArgs::Add(InternalRequest* aRequest, BodyAction aBodyAction, - SchemeAction aSchemeAction, Response& aResponse, - ErrorResult& aRv) +AutoChildOpArgs::Add(JSContext* aCx, InternalRequest* aRequest, + BodyAction aBodyAction, SchemeAction aSchemeAction, + Response& aResponse, ErrorResult& aRv) { MOZ_DIAGNOSTIC_ASSERT(!mSent); @@ -329,7 +329,7 @@ AutoChildOpArgs::Add(InternalRequest* aRequest, BodyAction aBodyAction, mTypeUtils->ToCacheRequest(pair.request(), aRequest, aBodyAction, aSchemeAction, mStreamCleanupList, aRv); if (!aRv.Failed()) { - mTypeUtils->ToCacheResponse(pair.response(), aResponse, + mTypeUtils->ToCacheResponse(aCx, pair.response(), aResponse, mStreamCleanupList, aRv); } diff --git a/dom/cache/AutoUtils.h b/dom/cache/AutoUtils.h index 244639f7c0..188c9d74e3 100644 --- a/dom/cache/AutoUtils.h +++ b/dom/cache/AutoUtils.h @@ -55,7 +55,7 @@ public: void Add(InternalRequest* aRequest, BodyAction aBodyAction, SchemeAction aSchemeAction, ErrorResult& aRv); - void Add(InternalRequest* aRequest, BodyAction aBodyAction, + void Add(JSContext* aCx, InternalRequest* aRequest, BodyAction aBodyAction, SchemeAction aSchemeAction, Response& aResponse, ErrorResult& aRv); const CacheOpArgs& SendAsOpArgs(); diff --git a/dom/cache/Cache.cpp b/dom/cache/Cache.cpp index 60e4f76b92..dd142881ec 100644 --- a/dom/cache/Cache.cpp +++ b/dom/cache/Cache.cpp @@ -184,7 +184,10 @@ public: // Now store the unwrapped Response list in the Cache. ErrorResult result; - RefPtr put = mCache->PutAll(mRequestList, responseList, result); + // TODO: Here we use the JSContext as received by the ResolvedCallback, and + // its state could be the wrong one. The spec doesn't say anything + // about it, yet (bug 1384006) + RefPtr put = mCache->PutAll(aCx, mRequestList, responseList, result); if (NS_WARN_IF(result.Failed())) { // TODO: abort the fetch requests we have running (bug 1157434) mPromise->MaybeReject(result); @@ -245,7 +248,7 @@ Cache::Cache(nsIGlobalObject* aGlobal, CacheChild* aActor) } already_AddRefed -Cache::Match(const RequestOrUSVString& aRequest, +Cache::Match(JSContext* aCx, const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { @@ -255,7 +258,8 @@ Cache::Match(const RequestOrUSVString& aRequest, CacheChild::AutoLock actorLock(mActor); - RefPtr ir = ToInternalRequest(aRequest, IgnoreBody, aRv); + RefPtr ir = + ToInternalRequest(aCx, aRequest, IgnoreBody, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } @@ -274,7 +278,7 @@ Cache::Match(const RequestOrUSVString& aRequest, } already_AddRefed -Cache::MatchAll(const Optional& aRequest, +Cache::MatchAll(JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { @@ -290,8 +294,8 @@ Cache::MatchAll(const Optional& aRequest, AutoChildOpArgs args(this, CacheMatchAllArgs(void_t(), params), 1); if (aRequest.WasPassed()) { - RefPtr ir = ToInternalRequest(aRequest.Value(), - IgnoreBody, aRv); + RefPtr ir = ToInternalRequest(aCx, aRequest.Value(), + IgnoreBody, aRv); if (aRv.Failed()) { return nullptr; } @@ -390,8 +394,8 @@ Cache::AddAll(JSContext* aContext, } already_AddRefed -Cache::Put(const RequestOrUSVString& aRequest, Response& aResponse, - ErrorResult& aRv) +Cache::Put(JSContext* aCx, const RequestOrUSVString& aRequest, + Response& aResponse, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { aRv.Throw(NS_ERROR_UNEXPECTED); @@ -404,14 +408,14 @@ Cache::Put(const RequestOrUSVString& aRequest, Response& aResponse, return nullptr; } - RefPtr ir = ToInternalRequest(aRequest, ReadBody, aRv); + RefPtr ir = ToInternalRequest(aCx, aRequest, ReadBody, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } AutoChildOpArgs args(this, CachePutAllArgs(), 1); - args.Add(ir, ReadBody, TypeErrorOnInvalidScheme, + args.Add(aCx, ir, ReadBody, TypeErrorOnInvalidScheme, aResponse, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; @@ -421,7 +425,7 @@ Cache::Put(const RequestOrUSVString& aRequest, Response& aResponse, } already_AddRefed -Cache::Delete(const RequestOrUSVString& aRequest, +Cache::Delete(JSContext* aCx, const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { @@ -431,7 +435,8 @@ Cache::Delete(const RequestOrUSVString& aRequest, CacheChild::AutoLock actorLock(mActor); - RefPtr ir = ToInternalRequest(aRequest, IgnoreBody, aRv); + RefPtr ir = + ToInternalRequest(aCx, aRequest, IgnoreBody, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } @@ -450,7 +455,7 @@ Cache::Delete(const RequestOrUSVString& aRequest, } already_AddRefed -Cache::Keys(const Optional& aRequest, +Cache::Keys(JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { @@ -466,8 +471,8 @@ Cache::Keys(const Optional& aRequest, AutoChildOpArgs args(this, CacheKeysArgs(void_t(), params), 1); if (aRequest.WasPassed()) { - RefPtr ir = ToInternalRequest(aRequest.Value(), - IgnoreBody, aRv); + RefPtr ir = + ToInternalRequest(aCx, aRequest.Value(), IgnoreBody, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } @@ -627,7 +632,7 @@ Cache::AddAll(const GlobalObject& aGlobal, } already_AddRefed -Cache::PutAll(const nsTArray>& aRequestList, +Cache::PutAll(JSContext* aCx, const nsTArray>& aRequestList, const nsTArray>& aResponseList, ErrorResult& aRv) { @@ -644,7 +649,8 @@ Cache::PutAll(const nsTArray>& aRequestList, for (uint32_t i = 0; i < aRequestList.Length(); ++i) { RefPtr ir = aRequestList[i]->GetInternalRequest(); - args.Add(ir, ReadBody, TypeErrorOnInvalidScheme, *aResponseList[i], aRv); + args.Add(aCx, ir, ReadBody, TypeErrorOnInvalidScheme, *aResponseList[i], + aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } diff --git a/dom/cache/Cache.h b/dom/cache/Cache.h index 04f891dca5..6fea3d36a0 100644 --- a/dom/cache/Cache.h +++ b/dom/cache/Cache.h @@ -43,10 +43,10 @@ public: // webidl interface methods already_AddRefed - Match(const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, - ErrorResult& aRv); + Match(JSContext* aCx, const RequestOrUSVString& aRequest, + const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed - MatchAll(const Optional& aRequest, + MatchAll(JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed Add(JSContext* aContext, const RequestOrUSVString& aRequest, @@ -55,13 +55,13 @@ public: AddAll(JSContext* aContext, const Sequence& aRequests, ErrorResult& aRv); already_AddRefed - Put(const RequestOrUSVString& aRequest, Response& aResponse, + Put(JSContext* aCx, const RequestOrUSVString& aRequest, Response& aResponse, ErrorResult& aRv); already_AddRefed - Delete(const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, - ErrorResult& aRv); + Delete(JSContext* aCx, const RequestOrUSVString& aRequest, + const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed - Keys(const Optional& aRequest, + Keys(JSContext* aCx, const Optional& aRequest, const CacheQueryOptions& aParams, ErrorResult& aRv); // binding methods @@ -100,7 +100,7 @@ private: ErrorResult& aRv); already_AddRefed - PutAll(const nsTArray>& aRequestList, + PutAll(JSContext* aCx, const nsTArray>& aRequestList, const nsTArray>& aResponseList, ErrorResult& aRv); diff --git a/dom/cache/CacheStorage.cpp b/dom/cache/CacheStorage.cpp index 3ace28c156..05df8ddc69 100644 --- a/dom/cache/CacheStorage.cpp +++ b/dom/cache/CacheStorage.cpp @@ -316,7 +316,7 @@ CacheStorage::CacheStorage(nsresult aFailureResult) } already_AddRefed -CacheStorage::Match(const RequestOrUSVString& aRequest, +CacheStorage::Match(JSContext* aCx, const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, ErrorResult& aRv) { NS_ASSERT_OWNINGTHREAD(CacheStorage); @@ -326,8 +326,8 @@ CacheStorage::Match(const RequestOrUSVString& aRequest, return nullptr; } - RefPtr request = ToInternalRequest(aRequest, IgnoreBody, - aRv); + RefPtr request = + ToInternalRequest(aCx, aRequest, IgnoreBody, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } diff --git a/dom/cache/CacheStorage.h b/dom/cache/CacheStorage.h index 04a2fa0dd9..c1cef4b428 100644 --- a/dom/cache/CacheStorage.h +++ b/dom/cache/CacheStorage.h @@ -59,9 +59,9 @@ public: DefineCaches(JSContext* aCx, JS::Handle aGlobal); // webidl interface methods - already_AddRefed Match(const RequestOrUSVString& aRequest, - const CacheQueryOptions& aOptions, - ErrorResult& aRv); + already_AddRefed + Match(JSContext* aCx, const RequestOrUSVString& aRequest, + const CacheQueryOptions& aOptions, ErrorResult& aRv); already_AddRefed Has(const nsAString& aKey, ErrorResult& aRv); already_AddRefed Open(const nsAString& aKey, ErrorResult& aRv); already_AddRefed Delete(const nsAString& aKey, ErrorResult& aRv); diff --git a/dom/cache/TypeUtils.cpp b/dom/cache/TypeUtils.cpp index f849f18874..1af5ee9458 100644 --- a/dom/cache/TypeUtils.cpp +++ b/dom/cache/TypeUtils.cpp @@ -79,7 +79,7 @@ ToHeadersEntryList(nsTArray& aOut, InternalHeaders* aHeaders) } // namespace already_AddRefed -TypeUtils::ToInternalRequest(const RequestOrUSVString& aIn, +TypeUtils::ToInternalRequest(JSContext* aCx, const RequestOrUSVString& aIn, BodyAction aBodyAction, ErrorResult& aRv) { if (aIn.IsRequest()) { @@ -87,7 +87,7 @@ TypeUtils::ToInternalRequest(const RequestOrUSVString& aIn, // Check and set bodyUsed flag immediately because its on Request // instead of InternalRequest. - CheckAndSetBodyUsed(&request, aBodyAction, aRv); + CheckAndSetBodyUsed(aCx, &request, aBodyAction, aRv); if (aRv.Failed()) { return nullptr; } return request.GetInternalRequest(); @@ -97,7 +97,8 @@ TypeUtils::ToInternalRequest(const RequestOrUSVString& aIn, } already_AddRefed -TypeUtils::ToInternalRequest(const OwningRequestOrUSVString& aIn, +TypeUtils::ToInternalRequest(JSContext* aCx, + const OwningRequestOrUSVString& aIn, BodyAction aBodyAction, ErrorResult& aRv) { @@ -106,7 +107,7 @@ TypeUtils::ToInternalRequest(const OwningRequestOrUSVString& aIn, // Check and set bodyUsed flag immediately because its on Request // instead of InternalRequest. - CheckAndSetBodyUsed(request, aBodyAction, aRv); + CheckAndSetBodyUsed(aCx, request, aBodyAction, aRv); if (aRv.Failed()) { return nullptr; } return request->GetInternalRequest(); @@ -204,7 +205,7 @@ TypeUtils::ToCacheResponseWithoutBody(CacheResponse& aOut, } void -TypeUtils::ToCacheResponse(CacheResponse& aOut, Response& aIn, +TypeUtils::ToCacheResponse(JSContext* aCx, CacheResponse& aOut, Response& aIn, nsTArray>& aStreamCleanupList, ErrorResult& aRv) { @@ -222,7 +223,10 @@ TypeUtils::ToCacheResponse(CacheResponse& aOut, Response& aIn, nsCOMPtr stream; ir->GetUnfilteredBody(getter_AddRefs(stream)); if (stream) { - aIn.SetBodyUsed(); + aIn.SetBodyUsed(aCx, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } } SerializeCacheStream(stream, &aOut.body(), aStreamCleanupList, aRv); @@ -426,8 +430,8 @@ TypeUtils::ProcessURL(nsACString& aUrl, bool* aSchemeValidOut, } void -TypeUtils::CheckAndSetBodyUsed(Request* aRequest, BodyAction aBodyAction, - ErrorResult& aRv) +TypeUtils::CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest, + BodyAction aBodyAction, ErrorResult& aRv) { MOZ_DIAGNOSTIC_ASSERT(aRequest); @@ -443,7 +447,10 @@ TypeUtils::CheckAndSetBodyUsed(Request* aRequest, BodyAction aBodyAction, nsCOMPtr stream; aRequest->GetBody(getter_AddRefs(stream)); if (stream) { - aRequest->SetBodyUsed(); + aRequest->SetBodyUsed(aCx, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } } } diff --git a/dom/cache/TypeUtils.h b/dom/cache/TypeUtils.h index 274586e3f7..ff5816db3e 100644 --- a/dom/cache/TypeUtils.h +++ b/dom/cache/TypeUtils.h @@ -73,12 +73,12 @@ public: GetIPCManager() = 0; already_AddRefed - ToInternalRequest(const RequestOrUSVString& aIn, BodyAction aBodyAction, - ErrorResult& aRv); + ToInternalRequest(JSContext* aCx, const RequestOrUSVString& aIn, + BodyAction aBodyAction, ErrorResult& aRv); already_AddRefed - ToInternalRequest(const OwningRequestOrUSVString& aIn, BodyAction aBodyAction, - ErrorResult& aRv); + ToInternalRequest(JSContext* aCx, const OwningRequestOrUSVString& aIn, + BodyAction aBodyAction, ErrorResult& aRv); void ToCacheRequest(CacheRequest& aOut, InternalRequest* aIn, @@ -91,7 +91,7 @@ public: ErrorResult& aRv); void - ToCacheResponse(CacheResponse& aOut, Response& aIn, + ToCacheResponse(JSContext* aCx, CacheResponse& aOut, Response& aIn, nsTArray>& aStreamCleanupList, ErrorResult& aRv); @@ -133,7 +133,7 @@ public: private: void - CheckAndSetBodyUsed(Request* aRequest, BodyAction aBodyAction, + CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest, BodyAction aBodyAction, ErrorResult& aRv); already_AddRefed diff --git a/dom/fetch/Fetch.cpp b/dom/fetch/Fetch.cpp index ce21cd391a..53ada9378b 100644 --- a/dom/fetch/Fetch.cpp +++ b/dom/fetch/Fetch.cpp @@ -948,6 +948,52 @@ template bool FetchBody::BodyUsed() const; +template +void +FetchBody::SetBodyUsed(JSContext* aCx, ErrorResult& aRv) +{ + MOZ_ASSERT(aCx); + MOZ_ASSERT(mOwner->EventTargetFor(TaskCategory::Other)->IsOnCurrentThread()); + + if (mBodyUsed) { + return; + } + + mBodyUsed = true; + + // If we already have a ReadableStreamBody and it has been created by DOM, we + // have to lock it now because it can have been shared with other objects. + if (mReadableStreamBody) { + JS::Rooted readableStreamObj(aCx, mReadableStreamBody); + if (JS::ReadableStreamGetMode(readableStreamObj) == + JS::ReadableStreamMode::ExternalSource) { + LockStream(aCx, readableStreamObj, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + } else { + // If this is not a native ReadableStream, let's activate the + // FetchStreamReader. + MOZ_ASSERT(mFetchStreamReader); + JS::Rooted reader(aCx); + mFetchStreamReader->StartConsuming(aCx, readableStreamObj, &reader, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return; + } + + mReadableStreamReader = reader; + } + } +} + +template +void +FetchBody::SetBodyUsed(JSContext* aCx, ErrorResult& aRv); + +template +void +FetchBody::SetBodyUsed(JSContext* aCx, ErrorResult& aRv); + template already_AddRefed FetchBody::ConsumeBody(JSContext* aCx, FetchConsumeType aType, ErrorResult& aRv) @@ -963,30 +1009,9 @@ FetchBody::ConsumeBody(JSContext* aCx, FetchConsumeType aType, ErrorRes return nullptr; } - SetBodyUsed(); - - // If we already have a ReadableStreamBody and it has been created by DOM, we - // have to lock it now because it can have been shared with other objects. - if (mReadableStreamBody) { - JS::Rooted readableStreamObj(aCx, mReadableStreamBody); - if (JS::ReadableStreamGetMode(readableStreamObj) == - JS::ReadableStreamMode::ExternalSource) { - LockStream(aCx, readableStreamObj, aRv); - if (NS_WARN_IF(aRv.Failed())) { - return nullptr; - } - } else { - // If this is not a native ReadableStream, let's activate the - // FetchStreamReader. - MOZ_ASSERT(mFetchStreamReader); - JS::Rooted reader(aCx); - mFetchStreamReader->StartConsuming(aCx, readableStreamObj, &reader, aRv); - if (NS_WARN_IF(aRv.Failed())) { - return nullptr; - } - - mReadableStreamReader = reader; - } + SetBodyUsed(aCx, aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; } nsCOMPtr global = DerivedClass()->GetParentObject(); diff --git a/dom/fetch/Fetch.h b/dom/fetch/Fetch.h index d65b66b883..01f0ab4ace 100644 --- a/dom/fetch/Fetch.h +++ b/dom/fetch/Fetch.h @@ -190,11 +190,28 @@ public: // Utility public methods accessed by various runnables. + // This method _must_ be called in order to set the body as used. If the body + // is a ReadableStream, this method will start reading the stream. + // More in details, this method does: + // 1) It uses an internal flag to track if the body is used. This is tracked + // separately from the ReadableStream disturbed state due to purely native + // streams. + // 2) If there is a ReadableStream reflector for the native stream it is + // Locked. + // 3) If there is a JS ReadableStream then we begin pumping it into the native + // body stream. This effectively locks and disturbs the stream. + // + // Note that JSContext is used only if there is a ReadableStream (this can + // happen because the body is a ReadableStream or because attribute body has + // already been used by content). If something goes wrong using + // ReadableStream, errors will be reported via ErrorResult and not as JS + // exceptions in JSContext. This is done in order to have a centralized error + // reporting way. + // + // Exceptions generated when reading from the ReadableStream are directly sent + // to the Console (NOTE FOR THE REVIEWER: this is part of patch 16) void - SetBodyUsed() - { - mBodyUsed = true; - } + SetBodyUsed(JSContext* aCx, ErrorResult& aRv); const nsCString& MimeType() const diff --git a/dom/fetch/Request.cpp b/dom/fetch/Request.cpp index ec140a22d4..b79b539772 100644 --- a/dom/fetch/Request.cpp +++ b/dom/fetch/Request.cpp @@ -614,7 +614,10 @@ Request::Constructor(const GlobalObject& aGlobal, inputReq->GetBody(getter_AddRefs(body)); if (body) { inputReq->SetBody(nullptr); - inputReq->SetBodyUsed(); + inputReq->SetBodyUsed(aGlobal.Context(), aRv); + if (NS_WARN_IF(aRv.Failed())) { + return nullptr; + } } } return domRequest.forget(); diff --git a/dom/workers/ScriptLoader.cpp b/dom/workers/ScriptLoader.cpp index 308ef94e38..123a46526a 100644 --- a/dom/workers/ScriptLoader.cpp +++ b/dom/workers/ScriptLoader.cpp @@ -701,9 +701,14 @@ private: request.SetAsUSVString().Rebind(loadInfo.mFullURL.Data(), loadInfo.mFullURL.Length()); + // This JSContext will not end up executing JS code because here there are + // no ReadableStreams involved. + AutoJSAPI jsapi; + jsapi.Init(); + ErrorResult error; RefPtr cachePromise = - mCacheCreator->Cache_()->Put(request, *response, error); + mCacheCreator->Cache_()->Put(jsapi.cx(), request, *response, error); if (NS_WARN_IF(error.Failed())) { nsresult rv = error.StealNSResult(); channel->Cancel(rv); @@ -1668,8 +1673,13 @@ CacheScriptLoader::Load(Cache* aCache) mozilla::dom::CacheQueryOptions params; + // This JSContext will not end up executing JS code because here there are + // no ReadableStreams involved. + AutoJSAPI jsapi; + jsapi.Init(); + ErrorResult error; - RefPtr promise = aCache->Match(request, params, error); + RefPtr promise = aCache->Match(jsapi.cx(), request, params, error); if (NS_WARN_IF(error.Failed())) { Fail(error.StealNSResult()); return; diff --git a/dom/workers/ServiceWorkerEvents.cpp b/dom/workers/ServiceWorkerEvents.cpp index ce2e5e7aae..7c9add4060 100644 --- a/dom/workers/ServiceWorkerEvents.cpp +++ b/dom/workers/ServiceWorkerEvents.cpp @@ -505,6 +505,44 @@ public: } } + // This function steals the error message from a ErrorResult. + void + SetCancelErrorResult(JSContext* aCx, ErrorResult& aRv) + { + MOZ_DIAGNOSTIC_ASSERT(aRv.Failed()); + MOZ_DIAGNOSTIC_ASSERT(!JS_IsExceptionPending(aCx)); + + // Storing the error as exception in the JSContext. + if (!aRv.MaybeSetPendingException(aCx)) { + return; + } + + MOZ_ASSERT(!aRv.Failed()); + + // Let's take the pending exception. + JS::Rooted exn(aCx); + if (!JS_GetPendingException(aCx, &exn)) { + return; + } + + JS_ClearPendingException(aCx); + + // Converting the exception in a js::ErrorReport. + js::ErrorReport report(aCx); + if (!report.init(aCx, exn, js::ErrorReport::WithSideEffects)) { + JS_ClearPendingException(aCx); + return; + } + + MOZ_ASSERT(mOwner); + MOZ_ASSERT(mMessageName.EqualsLiteral("InterceptionFailedWithURL")); + MOZ_ASSERT(mParams.Length() == 1); + + // Let's store the error message here. + mMessageName.Assign(report.toStringResult().c_str()); + mParams.Clear(); + } + template void SetCancelMessage(const nsACString& aMessageName, Params&&... aParams) { @@ -659,7 +697,12 @@ RespondWithHandler::ResolvedCallback(JSContext* aCx, JS::Handle aValu ir->GetUnfilteredBody(getter_AddRefs(body)); // Errors and redirects may not have a body. if (body) { - response->SetBodyUsed(); + IgnoredErrorResult error; + response->SetBodyUsed(aCx, error); + if (NS_WARN_IF(error.Failed())) { + autoCancel.SetCancelErrorResult(aCx, error); + return; + } nsCOMPtr responseBody; rv = mInterceptedChannel->GetResponseBody(getter_AddRefs(responseBody)); diff --git a/dom/workers/ServiceWorkerScriptCache.cpp b/dom/workers/ServiceWorkerScriptCache.cpp index 05be250da8..f343c35586 100644 --- a/dom/workers/ServiceWorkerScriptCache.cpp +++ b/dom/workers/ServiceWorkerScriptCache.cpp @@ -395,7 +395,7 @@ public: return; } - WriteToCache(cache); + WriteToCache(aCx, cache); return; } @@ -528,7 +528,7 @@ private: } void - WriteToCache(Cache* aCache) + WriteToCache(JSContext* aCx, Cache* aCache) { AssertIsOnMainThread(); MOZ_ASSERT(aCache); @@ -561,8 +561,10 @@ private: // For now we have to wait until the Put Promise is fulfilled before we can // continue since Cache does not yet support starting a read that is being // written to. - RefPtr cachePromise = aCache->Put(request, *response, result); + RefPtr cachePromise = aCache->Put(aCx, request, *response, result); if (NS_WARN_IF(result.Failed())) { + // No exception here because there are no ReadableStreams involved here. + MOZ_ASSERT(!result.IsJSException()); MOZ_ASSERT(!result.IsErrorWithMessage()); Fail(result.StealNSResult()); return; @@ -903,7 +905,7 @@ CompareCache::ManageCacheResult(JSContext* aCx, JS::Handle aValue) request.SetAsUSVString().Rebind(mURL.Data(), mURL.Length()); ErrorResult error; CacheQueryOptions params; - RefPtr promise = cache->Match(request, params, error); + RefPtr promise = cache->Match(aCx, request, params, error); if (NS_WARN_IF(error.Failed())) { mManager->CacheFinished(error.StealNSResult(), false); return;