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); }
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; }