already_AddRefed<Promise> Cache::AddAll(const GlobalObject& aGlobal, nsTArray<RefPtr<Request>>&& aRequestList, CallerType aCallerType, ErrorResult& aRv) { MOZ_DIAGNOSTIC_ASSERT(mActor); // If there is no work to do, then resolve immediately if (aRequestList.IsEmpty()) { RefPtr<Promise> promise = Promise::Create(mGlobal, aRv); if (NS_WARN_IF(!promise)) { return nullptr; } promise->MaybeResolveWithUndefined(); return promise.forget(); } AutoTArray<RefPtr<Promise>, 256> fetchList; fetchList.SetCapacity(aRequestList.Length()); // Begin fetching each request in parallel. For now, if an error occurs just // abandon our previous fetch calls. In theory we could cancel them in the // future once fetch supports it. for (uint32_t i = 0; i < aRequestList.Length(); ++i) { RequestOrUSVString requestOrString; requestOrString.SetAsRequest() = aRequestList[i]; RefPtr<Promise> fetch = FetchRequest(mGlobal, requestOrString, RequestInit(), aCallerType, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } fetchList.AppendElement(std::move(fetch)); } RefPtr<Promise> promise = Promise::Create(mGlobal, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } RefPtr<FetchHandler> handler = new FetchHandler(mActor->GetWorkerHolder(), this, std::move(aRequestList), promise); RefPtr<Promise> fetchPromise = Promise::All(aGlobal.Context(), fetchList, aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } fetchPromise->AppendNativeHandler(handler); return promise.forget(); }
already_AddRefed<Promise> Cache::AddAll(JSContext* aContext, const Sequence<OwningRequestOrUSVString>& aRequestList, CallerType aCallerType, ErrorResult& aRv) { if (NS_WARN_IF(!mActor)) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } CacheChild::AutoLock actorLock(mActor); GlobalObject global(aContext, mGlobal->GetGlobalJSObject()); MOZ_DIAGNOSTIC_ASSERT(!global.Failed()); nsTArray<RefPtr<Request>> requestList(aRequestList.Length()); for (uint32_t i = 0; i < aRequestList.Length(); ++i) { RequestOrUSVString requestOrString; if (aRequestList[i].IsRequest()) { requestOrString.SetAsRequest() = aRequestList[i].GetAsRequest(); if (NS_WARN_IF(!IsValidPutRequestMethod(requestOrString.GetAsRequest(), aRv))) { return nullptr; } } else { requestOrString.SetAsUSVString().Rebind( aRequestList[i].GetAsUSVString().Data(), aRequestList[i].GetAsUSVString().Length()); } RefPtr<Request> request = Request::Constructor(global, requestOrString, RequestInit(), aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } nsAutoString url; request->GetUrl(url); if (NS_WARN_IF(!IsValidPutRequestURL(url, aRv))) { return nullptr; } requestList.AppendElement(std::move(request)); } return AddAll(global, std::move(requestList), aCallerType, aRv); }
already_AddRefed<InternalRequest> TypeUtils::ToInternalRequest(const RequestOrUSVString& aIn, BodyAction aBodyAction, ErrorResult& aRv) { if (aIn.IsRequest()) { Request& request = aIn.GetAsRequest(); // Check and set bodyUsed flag immediately because its on Request // instead of InternalRequest. CheckAndSetBodyUsed(&request, aBodyAction, aRv); if (aRv.Failed()) { return nullptr; } return request.GetInternalRequest(); } return ToInternalRequest(aIn.GetAsUSVString(), aRv); }
void WriteToCache(Cache* aCache) { AssertIsOnMainThread(); MOZ_ASSERT(aCache); MOZ_ASSERT(mState == WaitingForOpen); ErrorResult result; nsCOMPtr<nsIInputStream> body; result = NS_NewCStringInputStream(getter_AddRefs(body), NS_ConvertUTF16toUTF8(mCN->Buffer())); if (NS_WARN_IF(result.Failed())) { MOZ_ASSERT(!result.IsErrorWithMessage()); Fail(result.StealNSResult()); return; } RefPtr<InternalResponse> ir = new InternalResponse(200, NS_LITERAL_CSTRING("OK")); ir->SetBody(body, mCN->Buffer().Length()); ir->InitChannelInfo(mChannelInfo); if (mPrincipalInfo) { ir->SetPrincipalInfo(Move(mPrincipalInfo)); } RefPtr<Response> response = new Response(aCache->GetGlobalObject(), ir); RequestOrUSVString request; request.SetAsUSVString().Rebind(URL().Data(), URL().Length()); // 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<Promise> cachePromise = aCache->Put(request, *response, result); if (NS_WARN_IF(result.Failed())) { MOZ_ASSERT(!result.IsErrorWithMessage()); Fail(result.StealNSResult()); return; } mState = WaitingForPut; cachePromise->AppendNativeHandler(this); }
already_AddRefed<InternalRequest> TypeUtils::ToInternalRequest(const nsAString& aIn, ErrorResult& aRv) { RequestOrUSVString requestOrString; requestOrString.SetAsUSVString().Rebind(aIn.Data(), aIn.Length()); // Re-create a GlobalObject stack object so we can use webidl Constructors. AutoJSAPI jsapi; if (NS_WARN_IF(!jsapi.Init(GetGlobalObject()))) { aRv.Throw(NS_ERROR_UNEXPECTED); return nullptr; } JSContext* cx = jsapi.cx(); GlobalObject global(cx, GetGlobalObject()->GetGlobalJSObject()); MOZ_ASSERT(!global.Failed()); nsRefPtr<Request> request = Request::Constructor(global, requestOrString, RequestInit(), aRv); if (NS_WARN_IF(aRv.Failed())) { return nullptr; } return request->GetInternalRequest(); }
void CompareCache::ManageCacheResult(JSContext* aCx, JS::Handle<JS::Value> aValue) { AssertIsOnMainThread(); if (NS_WARN_IF(!aValue.isObject())) { mManager->CacheFinished(NS_ERROR_FAILURE, false); return; } JS::Rooted<JSObject*> obj(aCx, &aValue.toObject()); if (NS_WARN_IF(!obj)) { mManager->CacheFinished(NS_ERROR_FAILURE, false); return; } Cache* cache = nullptr; nsresult rv = UNWRAP_OBJECT(Cache, obj, cache); if (NS_WARN_IF(NS_FAILED(rv))) { mManager->CacheFinished(rv, false); return; } RequestOrUSVString request; request.SetAsUSVString().Rebind(mURL.Data(), mURL.Length()); ErrorResult error; CacheQueryOptions params; RefPtr<Promise> promise = cache->Match(request, params, error); if (NS_WARN_IF(error.Failed())) { mManager->CacheFinished(error.StealNSResult(), false); return; } promise->AppendNativeHandler(this); mState = WaitingForValue; }