Bug 1627892 - Use SafeRefPtr for Request. r=dom-workers-and-storage-reviewers,perry

Differential Revision: https://phabricator.services.mozilla.com/D70390
This commit is contained in:
Simon Giesecke 2020-05-11 12:13:48 +00:00
Родитель bec9351c13
Коммит 75c988ec13
10 изменённых файлов: 69 добавлений и 57 удалений

18
dom/cache/Cache.cpp поставляемый
Просмотреть файл

@ -103,7 +103,7 @@ static bool IsValidPutResponseStatus(Response& aResponse,
class Cache::FetchHandler final : public PromiseNativeHandler {
public:
FetchHandler(SafeRefPtr<CacheWorkerRef> aWorkerRef, Cache* aCache,
nsTArray<RefPtr<Request>>&& aRequestList, Promise* aPromise)
nsTArray<SafeRefPtr<Request>>&& aRequestList, Promise* aPromise)
: mWorkerRef(std::move(aWorkerRef)),
mCache(aCache),
mRequestList(std::move(aRequestList)),
@ -217,7 +217,7 @@ class Cache::FetchHandler final : public PromiseNativeHandler {
SafeRefPtr<CacheWorkerRef> mWorkerRef;
RefPtr<Cache> mCache;
nsTArray<RefPtr<Request>> mRequestList;
nsTArray<SafeRefPtr<Request>> mRequestList;
RefPtr<Promise> mPromise;
NS_DECL_ISUPPORTS
@ -322,8 +322,8 @@ already_AddRefed<Promise> Cache::Add(JSContext* aContext,
GlobalObject global(aContext, mGlobal->GetGlobalJSObject());
MOZ_DIAGNOSTIC_ASSERT(!global.Failed());
nsTArray<RefPtr<Request>> requestList(1);
RefPtr<Request> request =
nsTArray<SafeRefPtr<Request>> requestList(1);
SafeRefPtr<Request> request =
Request::Constructor(global, aRequest, RequestInit(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
@ -352,7 +352,7 @@ already_AddRefed<Promise> Cache::AddAll(
GlobalObject global(aContext, mGlobal->GetGlobalJSObject());
MOZ_DIAGNOSTIC_ASSERT(!global.Failed());
nsTArray<RefPtr<Request>> requestList(aRequestList.Length());
nsTArray<SafeRefPtr<Request>> requestList(aRequestList.Length());
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
RequestOrUSVString requestOrString;
@ -367,7 +367,7 @@ already_AddRefed<Promise> Cache::AddAll(
aRequestList[i].GetAsUSVString());
}
RefPtr<Request> request =
SafeRefPtr<Request> request =
Request::Constructor(global, requestOrString, RequestInit(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
@ -531,7 +531,7 @@ already_AddRefed<Promise> Cache::ExecuteOp(AutoChildOpArgs& aOpArgs,
}
already_AddRefed<Promise> Cache::AddAll(
const GlobalObject& aGlobal, nsTArray<RefPtr<Request>>&& aRequestList,
const GlobalObject& aGlobal, nsTArray<SafeRefPtr<Request>>&& aRequestList,
CallerType aCallerType, ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(mActor);
@ -555,7 +555,7 @@ already_AddRefed<Promise> Cache::AddAll(
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {
RequestOrUSVString requestOrString;
requestOrString.SetAsRequest() = aRequestList[i];
requestOrString.SetAsRequest() = aRequestList[i].unsafeGetRawPtr();
RefPtr<Promise> fetch =
FetchRequest(mGlobal, requestOrString, RequestInit(), aCallerType, aRv);
if (NS_WARN_IF(aRv.Failed())) {
@ -585,7 +585,7 @@ already_AddRefed<Promise> Cache::AddAll(
}
already_AddRefed<Promise> Cache::PutAll(
JSContext* aCx, const nsTArray<RefPtr<Request>>& aRequestList,
JSContext* aCx, const nsTArray<SafeRefPtr<Request>>& aRequestList,
const nsTArray<RefPtr<Response>>& aResponseList, ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(aRequestList.Length() == aResponseList.Length());

4
dom/cache/Cache.h поставляемый
Просмотреть файл

@ -99,11 +99,11 @@ class Cache final : public nsISupports,
ErrorResult& aRv);
already_AddRefed<Promise> AddAll(const GlobalObject& aGlobal,
nsTArray<RefPtr<Request>>&& aRequestList,
nsTArray<SafeRefPtr<Request>>&& aRequestList,
CallerType aCallerType, ErrorResult& aRv);
already_AddRefed<Promise> PutAll(
JSContext* aCx, const nsTArray<RefPtr<Request>>& aRequestList,
JSContext* aCx, const nsTArray<SafeRefPtr<Request>>& aRequestList,
const nsTArray<RefPtr<Response>>& aResponseList, ErrorResult& aRv);
OpenMode GetOpenMode() const;

9
dom/cache/CacheOpChild.cpp поставляемый
Просмотреть файл

@ -16,6 +16,13 @@
namespace mozilla {
namespace dom {
// XXX Move this to ToJSValue.h
template <typename T>
MOZ_MUST_USE bool ToJSValue(JSContext* aCx, const SafeRefPtr<T>& aArgument,
JS::MutableHandle<JS::Value> aValue) {
return ToJSValue(aCx, *aArgument.unsafeGetRawPtr(), aValue);
}
namespace cache {
using mozilla::ipc::PBackgroundChild;
@ -212,7 +219,7 @@ void CacheOpChild::HandleResponseList(
void CacheOpChild::HandleRequestList(
const nsTArray<CacheRequest>& aRequestList) {
AutoTArray<RefPtr<Request>, 256> requests;
AutoTArray<SafeRefPtr<Request>, 256> requests;
requests.SetCapacity(aRequestList.Length());
for (uint32_t i = 0; i < aRequestList.Length(); ++i) {

25
dom/cache/TypeUtils.cpp поставляемый
Просмотреть файл

@ -82,7 +82,7 @@ SafeRefPtr<InternalRequest> TypeUtils::ToInternalRequest(
// Check and set bodyUsed flag immediately because its on Request
// instead of InternalRequest.
CheckAndSetBodyUsed(aCx, &request, aBodyAction, aRv);
CheckAndSetBodyUsed(aCx, request, aBodyAction, aRv);
if (aRv.Failed()) {
return nullptr;
}
@ -97,7 +97,7 @@ SafeRefPtr<InternalRequest> TypeUtils::ToInternalRequest(
JSContext* aCx, const OwningRequestOrUSVString& aIn, BodyAction aBodyAction,
ErrorResult& aRv) {
if (aIn.IsRequest()) {
RefPtr<Request> request = aIn.GetAsRequest().get();
Request& request = aIn.GetAsRequest();
// Check and set bodyUsed flag immediately because its on Request
// instead of InternalRequest.
@ -106,7 +106,7 @@ SafeRefPtr<InternalRequest> TypeUtils::ToInternalRequest(
return nullptr;
}
return request->GetInternalRequest();
return request.GetInternalRequest();
}
return ToInternalRequest(aIn.GetAsUSVString(), aRv);
@ -338,10 +338,9 @@ SafeRefPtr<InternalRequest> TypeUtils::ToInternalRequest(
return internalRequest;
}
already_AddRefed<Request> TypeUtils::ToRequest(const CacheRequest& aIn) {
RefPtr<Request> request =
new Request(GetGlobalObject(), ToInternalRequest(aIn), nullptr);
return request.forget();
SafeRefPtr<Request> TypeUtils::ToRequest(const CacheRequest& aIn) {
return MakeSafeRefPtr<Request>(GetGlobalObject(), ToInternalRequest(aIn),
nullptr);
}
// static
@ -418,15 +417,13 @@ void TypeUtils::ProcessURL(nsACString& aUrl, bool* aSchemeValidOut,
*aUrlQueryOut = Substring(aUrl, queryPos - 1, queryLen + 1);
}
void TypeUtils::CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest,
void TypeUtils::CheckAndSetBodyUsed(JSContext* aCx, Request& aRequest,
BodyAction aBodyAction, ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(aRequest);
if (aBodyAction == IgnoreBody) {
return;
}
bool bodyUsed = aRequest->GetBodyUsed(aRv);
bool bodyUsed = aRequest.GetBodyUsed(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
@ -436,9 +433,9 @@ void TypeUtils::CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest,
}
nsCOMPtr<nsIInputStream> stream;
aRequest->GetBody(getter_AddRefs(stream));
aRequest.GetBody(getter_AddRefs(stream));
if (stream) {
aRequest->SetBodyUsed(aCx, aRv);
aRequest.SetBodyUsed(aCx, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return;
}
@ -460,7 +457,7 @@ SafeRefPtr<InternalRequest> TypeUtils::ToInternalRequest(const nsAString& aIn,
GlobalObject global(cx, GetGlobalObject()->GetGlobalJSObject());
MOZ_DIAGNOSTIC_ASSERT(!global.Failed());
RefPtr<Request> request =
SafeRefPtr<Request> request =
Request::Constructor(global, requestOrString, RequestInit(), aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;

4
dom/cache/TypeUtils.h поставляемый
Просмотреть файл

@ -91,7 +91,7 @@ class TypeUtils {
SafeRefPtr<InternalRequest> ToInternalRequest(const CacheRequest& aIn);
already_AddRefed<Request> ToRequest(const CacheRequest& aIn);
SafeRefPtr<Request> ToRequest(const CacheRequest& aIn);
// static methods
static already_AddRefed<InternalHeaders> ToInternalHeaders(
@ -117,7 +117,7 @@ class TypeUtils {
nsACString* aUrlQueryOut, ErrorResult& aRv);
private:
void CheckAndSetBodyUsed(JSContext* aCx, Request* aRequest,
void CheckAndSetBodyUsed(JSContext* aCx, Request& aRequest,
BodyAction aBodyAction, ErrorResult& aRv);
SafeRefPtr<InternalRequest> ToInternalRequest(const nsAString& aIn,

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

@ -450,7 +450,8 @@ already_AddRefed<Promise> FetchRequest(nsIGlobalObject* aGlobal,
JS::Rooted<JSObject*> jsGlobal(cx, aGlobal->GetGlobalJSObject());
GlobalObject global(cx, jsGlobal);
RefPtr<Request> request = Request::Constructor(global, aInput, aInit, aRv);
SafeRefPtr<Request> request =
Request::Constructor(global, aInput, aInit, aRv);
if (aRv.Failed()) {
return nullptr;
}

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

@ -260,20 +260,20 @@ class ReferrerSameOriginChecker final : public WorkerMainThreadRunnable {
} // namespace
/*static*/
already_AddRefed<Request> Request::Constructor(const GlobalObject& aGlobal,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& aRv) {
SafeRefPtr<Request> Request::Constructor(const GlobalObject& aGlobal,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& aRv) {
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
return Constructor(global, aGlobal.Context(), aInput, aInit, aRv);
}
/*static*/
already_AddRefed<Request> Request::Constructor(nsIGlobalObject* aGlobal,
JSContext* aCx,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& aRv) {
SafeRefPtr<Request> Request::Constructor(nsIGlobalObject* aGlobal,
JSContext* aCx,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& aRv) {
bool hasCopiedBody = false;
SafeRefPtr<InternalRequest> request;
@ -593,7 +593,8 @@ already_AddRefed<Request> Request::Constructor(nsIGlobalObject* aGlobal,
}
}
RefPtr<Request> domRequest = new Request(aGlobal, std::move(request), signal);
auto domRequest =
MakeSafeRefPtr<Request>(aGlobal, std::move(request), signal);
domRequest->SetMimeType();
if (aInput.IsRequest()) {
@ -608,10 +609,10 @@ already_AddRefed<Request> Request::Constructor(nsIGlobalObject* aGlobal,
}
}
}
return domRequest.forget();
return domRequest;
}
already_AddRefed<Request> Request::Clone(ErrorResult& aRv) {
SafeRefPtr<Request> Request::Clone(ErrorResult& aRv) {
bool used = GetBodyUsed(aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
@ -627,10 +628,7 @@ already_AddRefed<Request> Request::Clone(ErrorResult& aRv) {
return nullptr;
}
RefPtr<Request> request =
new Request(mOwner, std::move(ir), GetOrCreateSignal());
return request.forget();
return MakeSafeRefPtr<Request>(mOwner, std::move(ir), GetOrCreateSignal());
}
Headers* Request::Headers_() {

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

@ -100,20 +100,20 @@ class Request final : public FetchBody<Request>, public nsWrapperCache {
const nsAString& BodyLocalPath() const { return mRequest->BodyLocalPath(); }
static already_AddRefed<Request> Constructor(const GlobalObject& aGlobal,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& rv);
static SafeRefPtr<Request> Constructor(const GlobalObject& aGlobal,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& rv);
static already_AddRefed<Request> Constructor(nsIGlobalObject* aGlobal,
JSContext* aCx,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& rv);
static SafeRefPtr<Request> Constructor(nsIGlobalObject* aGlobal,
JSContext* aCx,
const RequestOrUSVString& aInput,
const RequestInit& aInit,
ErrorResult& rv);
nsIGlobalObject* GetParentObject() const { return mOwner; }
already_AddRefed<Request> Clone(ErrorResult& aRv);
SafeRefPtr<Request> Clone(ErrorResult& aRv);
SafeRefPtr<InternalRequest> GetInternalRequest();

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

@ -420,6 +420,15 @@ struct CopyablePtr<SafeRefPtr<T>> {
} // namespace detail
namespace dom {
/// XXX Move this to BindingUtils.h later on
template <class T, class S>
inline RefPtr<T> StrongOrRawPtr(SafeRefPtr<S>&& aPtr) {
return AsRefPtr(std::move(aPtr));
}
} // namespace dom
} // namespace mozilla
// Use MOZ_INLINE_DECL_SAFEREFCOUNTING_INHERITED in a 'Class' derived from a

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

@ -127,7 +127,7 @@ class WorkletFetchHandler final : public PromiseNativeHandler,
RequestInit requestInit;
requestInit.mCredentials.Construct(aOptions.mCredentials);
RefPtr<Request> request =
SafeRefPtr<Request> request =
Request::Constructor(global, aCx, requestInput, requestInit, aRv);
if (aRv.Failed()) {
return nullptr;
@ -136,7 +136,7 @@ class WorkletFetchHandler final : public PromiseNativeHandler,
request->OverrideContentPolicyType(aWorklet->Impl()->ContentPolicyType());
RequestOrUSVString finalRequestInput;
finalRequestInput.SetAsRequest() = request;
finalRequestInput.SetAsRequest() = request.unsafeGetRawPtr();
RefPtr<Promise> fetchPromise = FetchRequest(
global, finalRequestInput, requestInit, CallerType::System, aRv);