From e5c5bd45309704d3fa0dd1d8e95e8f5413161287 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 21 Oct 2019 05:33:33 +0000 Subject: [PATCH] Bug 1585284 - Use nsIGlobalObject in any Blob/File CTOR, r=smaug Differential Revision: https://phabricator.services.mozilla.com/D49392 --- dom/base/BodyUtil.cpp | 2 +- dom/base/BodyUtil.h | 2 +- dom/base/PostMessageEvent.cpp | 2 +- dom/base/StructuredCloneHolder.cpp | 54 +++++++++--------- dom/base/StructuredCloneHolder.h | 14 ++--- dom/base/nsDOMDataChannel.cpp | 2 +- dom/console/Console.cpp | 37 +++++++----- dom/events/DataTransferItem.cpp | 62 +++++++++++++-------- dom/events/DataTransferItem.h | 2 + dom/file/Blob.cpp | 50 +++++++++-------- dom/file/Blob.h | 15 ++--- dom/file/File.cpp | 27 +++++---- dom/file/File.h | 12 ++-- dom/file/MutableBlobStorage.cpp | 8 ++- dom/filesystem/Directory.cpp | 28 ++++++---- dom/filesystem/Directory.h | 8 +-- dom/filesystem/FileSystemBase.cpp | 2 +- dom/filesystem/FileSystemBase.h | 2 +- dom/filesystem/GetDirectoryListingTask.cpp | 14 ++--- dom/filesystem/GetFileOrDirectoryTask.cpp | 8 ++- dom/filesystem/GetFilesTask.cpp | 8 ++- dom/filesystem/OSFileSystem.cpp | 25 ++++----- dom/filesystem/OSFileSystem.h | 8 +-- dom/html/HTMLCanvasElement.cpp | 4 +- dom/html/HTMLInputElement.cpp | 7 ++- dom/media/MediaRecorder.cpp | 2 +- dom/media/imagecapture/CaptureTask.cpp | 2 +- dom/presentation/PresentationConnection.cpp | 2 +- dom/serviceworkers/ServiceWorkerEvents.cpp | 4 +- dom/serviceworkers/ServiceWorkerEvents.h | 6 +- dom/xhr/XMLHttpRequestMainThread.cpp | 4 +- layout/forms/nsFileControlFrame.cpp | 2 +- widget/nsBaseFilePicker.cpp | 14 ++++- widget/nsFilePickerProxy.cpp | 14 +++-- 34 files changed, 254 insertions(+), 199 deletions(-) diff --git a/dom/base/BodyUtil.cpp b/dom/base/BodyUtil.cpp index 8869c0226a81..0a31e6162587 100644 --- a/dom/base/BodyUtil.cpp +++ b/dom/base/BodyUtil.cpp @@ -398,7 +398,7 @@ void BodyUtil::ConsumeArrayBuffer(JSContext* aCx, } // static -already_AddRefed BodyUtil::ConsumeBlob(nsISupports* aParent, +already_AddRefed BodyUtil::ConsumeBlob(nsIGlobalObject* aParent, const nsString& aMimeType, uint32_t aInputLength, uint8_t* aInput, diff --git a/dom/base/BodyUtil.h b/dom/base/BodyUtil.h index 07b49392c5d7..3e56ad93a1a4 100644 --- a/dom/base/BodyUtil.h +++ b/dom/base/BodyUtil.h @@ -36,7 +36,7 @@ class BodyUtil final { * Creates an in-memory blob from an array. The blob takes ownership of * |aInput|, which must be allocated by |malloc|. */ - static already_AddRefed ConsumeBlob(nsISupports* aParent, + static already_AddRefed ConsumeBlob(nsIGlobalObject* aParent, const nsString& aMimeType, uint32_t aInputLength, uint8_t* aInput, ErrorResult& aRv); diff --git a/dom/base/PostMessageEvent.cpp b/dom/base/PostMessageEvent.cpp index 79bca33744f9..e85a61c35667 100644 --- a/dom/base/PostMessageEvent.cpp +++ b/dom/base/PostMessageEvent.cpp @@ -164,7 +164,7 @@ PostMessageEvent::Run() { StructuredCloneHolder* holder; if (mHolder.constructed()) { - mHolder.ref().Read(ToSupports(targetWindow), cx, + mHolder.ref().Read(targetWindow->AsGlobal(), cx, &messageData, rv); holder = &mHolder.ref(); } else { diff --git a/dom/base/StructuredCloneHolder.cpp b/dom/base/StructuredCloneHolder.cpp index cbbe01f62213..3a7cbf70d276 100644 --- a/dom/base/StructuredCloneHolder.cpp +++ b/dom/base/StructuredCloneHolder.cpp @@ -233,7 +233,7 @@ StructuredCloneHolder::StructuredCloneHolder( : StructuredCloneHolderBase(aScope), mSupportsCloning(aSupportsCloning == CloningSupported), mSupportsTransferring(aSupportsTransferring == TransferringSupported), - mParent(nullptr) + mGlobal(nullptr) #ifdef DEBUG , mCreationEventTarget(GetCurrentThreadEventTarget()) @@ -266,16 +266,16 @@ void StructuredCloneHolder::Write(JSContext* aCx, JS::Handle aValue, } } -void StructuredCloneHolder::Read(nsISupports* aParent, JSContext* aCx, +void StructuredCloneHolder::Read(nsIGlobalObject* aGlobal, JSContext* aCx, JS::MutableHandle aValue, ErrorResult& aRv) { MOZ_ASSERT_IF( mStructuredCloneScope == StructuredCloneScope::SameProcessSameThread, mCreationEventTarget->IsOnCurrentThread()); - MOZ_ASSERT(aParent); + MOZ_ASSERT(aGlobal); - mozilla::AutoRestore guard(mParent); - mParent = aParent; + mozilla::AutoRestore guard(mGlobal); + mGlobal = aGlobal; if (!StructuredCloneHolderBase::Read(aCx, aValue)) { JS_ClearPendingException(aCx); @@ -293,15 +293,17 @@ void StructuredCloneHolder::Read(nsISupports* aParent, JSContext* aCx, } } -void StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent, JSContext* aCx, +void StructuredCloneHolder::ReadFromBuffer(nsIGlobalObject* aGlobal, + JSContext* aCx, JSStructuredCloneData& aBuffer, JS::MutableHandle aValue, ErrorResult& aRv) { - ReadFromBuffer(aParent, aCx, aBuffer, JS_STRUCTURED_CLONE_VERSION, aValue, + ReadFromBuffer(aGlobal, aCx, aBuffer, JS_STRUCTURED_CLONE_VERSION, aValue, aRv); } -void StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent, JSContext* aCx, +void StructuredCloneHolder::ReadFromBuffer(nsIGlobalObject* aGlobal, + JSContext* aCx, JSStructuredCloneData& aBuffer, uint32_t aAlgorithmVersion, JS::MutableHandle aValue, @@ -312,8 +314,8 @@ void StructuredCloneHolder::ReadFromBuffer(nsISupports* aParent, JSContext* aCx, MOZ_ASSERT(!mBuffer, "ReadFromBuffer() must be called without a Write()."); - mozilla::AutoRestore guard(mParent); - mParent = aParent; + mozilla::AutoRestore guard(mGlobal); + mGlobal = aGlobal; if (!JS_ReadStructuredClone(aCx, aBuffer, aAlgorithmVersion, mStructuredCloneScope, aValue, &sCallbacks, @@ -353,10 +355,10 @@ JSObject* StructuredCloneHolder::ReadFullySerializableObjects( // the casting between JSPrincipals* and nsIPrincipal* we can't use // getter_AddRefs above and have to already_AddRefed here. nsCOMPtr principal = - already_AddRefed(nsJSPrincipals::get(prin)); + already_AddRefed(nsJSPrincipals::get(prin)); nsresult rv = nsContentUtils::WrapNative( - aCx, principal, &NS_GET_IID(nsIPrincipal), &result); + aCx, principal, &NS_GET_IID(nsIPrincipal), &result); if (NS_FAILED(rv)) { xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR); return nullptr; @@ -449,7 +451,7 @@ JSObject* ReadBlob(JSContext* aCx, uint32_t aIndex, MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false)); - RefPtr blob = Blob::Create(aHolder->ParentDuringRead(), blobImpl); + RefPtr blob = Blob::Create(aHolder->GlobalDuringRead(), blobImpl); if (!ToJSValue(aCx, blob, &val)) { return nullptr; } @@ -521,7 +523,7 @@ already_AddRefed ReadDirectoryInternal( } RefPtr directory = - Directory::Create(aHolder->ParentDuringRead(), file); + Directory::Create(aHolder->GlobalDuringRead(), file); return directory.forget(); } @@ -560,7 +562,7 @@ JSObject* ReadFileList(JSContext* aCx, JSStructuredCloneReader* aReader, JS::Rooted val(aCx); { - RefPtr fileList = new FileList(aHolder->ParentDuringRead()); + RefPtr fileList = new FileList(aHolder->GlobalDuringRead()); uint32_t zero, index; // |index| is the index of the first blobImpl. @@ -585,7 +587,7 @@ JSObject* ReadFileList(JSContext* aCx, JSStructuredCloneReader* aReader, MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false)); - RefPtr file = File::Create(aHolder->ParentDuringRead(), blobImpl); + RefPtr file = File::Create(aHolder->GlobalDuringRead(), blobImpl); if (!fileList->Append(file)) { return nullptr; } @@ -638,7 +640,7 @@ JSObject* ReadFormData(JSContext* aCx, JSStructuredCloneReader* aReader, // See the serialization of the FormData for the format. JS::Rooted val(aCx); { - RefPtr formData = new FormData(aHolder->ParentDuringRead()); + RefPtr formData = new FormData(aHolder->GlobalDuringRead()); Optional thirdArg; for (uint32_t i = 0; i < aCount; ++i) { @@ -663,7 +665,7 @@ JSObject* ReadFormData(JSContext* aCx, JSStructuredCloneReader* aReader, RefPtr blobImpl = aHolder->BlobImpls()[indexOrLengthOfString]; MOZ_ALWAYS_SUCCEEDS(blobImpl->SetMutable(false)); - RefPtr blob = Blob::Create(aHolder->ParentDuringRead(), blobImpl); + RefPtr blob = Blob::Create(aHolder->GlobalDuringRead(), blobImpl); MOZ_ASSERT(blob); ErrorResult rv; @@ -838,7 +840,7 @@ JSObject* ReadInputStream(JSContext* aCx, uint32_t aIndex, nsCOMPtr inputStream = aHolder->InputStreams()[aIndex]; nsresult rv = nsContentUtils::WrapNative( - aCx, inputStream, &NS_GET_IID(nsIInputStream), &result); + aCx, inputStream, &NS_GET_IID(nsIInputStream), &result); if (NS_FAILED(rv)) { return nullptr; } @@ -895,9 +897,8 @@ JSObject* StructuredCloneHolder::CustomReadHandler( // This can be null. JS::RootedObject result(aCx); { - nsCOMPtr parent = do_QueryInterface(mParent); // aIndex is the index of the cloned image. - result = ImageBitmap::ReadStructuredClone(aCx, aReader, parent, + result = ImageBitmap::ReadStructuredClone(aCx, aReader, mGlobal, GetSurfaces(), aIndex); } return result; @@ -1032,10 +1033,8 @@ bool StructuredCloneHolder::CustomReadTransferHandler( MOZ_ASSERT(aExtraData < mPortIdentifiers.Length()); const MessagePortIdentifier& portIdentifier = mPortIdentifiers[aExtraData]; - nsCOMPtr global = do_QueryInterface(mParent); - ErrorResult rv; - RefPtr port = MessagePort::Create(global, portIdentifier, rv); + RefPtr port = MessagePort::Create(mGlobal, portIdentifier, rv); if (NS_WARN_IF(rv.Failed())) { rv.SuppressException(); return false; @@ -1060,9 +1059,8 @@ bool StructuredCloneHolder::CustomReadTransferHandler( MOZ_ASSERT(aContent); OffscreenCanvasCloneData* data = static_cast(aContent); - nsCOMPtr parent = do_QueryInterface(mParent); RefPtr canvas = - OffscreenCanvas::CreateFromCloneData(parent, data); + OffscreenCanvas::CreateFromCloneData(mGlobal, data); delete data; JS::Rooted value(aCx); @@ -1081,8 +1079,8 @@ bool StructuredCloneHolder::CustomReadTransferHandler( StructuredCloneScope::SameProcessDifferentThread)) { MOZ_ASSERT(aContent); ImageBitmapCloneData* data = static_cast(aContent); - nsCOMPtr parent = do_QueryInterface(mParent); - RefPtr bitmap = ImageBitmap::CreateFromCloneData(parent, data); + RefPtr bitmap = + ImageBitmap::CreateFromCloneData(mGlobal, data); delete data; JS::Rooted value(aCx); diff --git a/dom/base/StructuredCloneHolder.h b/dom/base/StructuredCloneHolder.h index 3b1b99ef8262..3a7d52ee5136 100644 --- a/dom/base/StructuredCloneHolder.h +++ b/dom/base/StructuredCloneHolder.h @@ -13,13 +13,13 @@ #include "mozilla/Move.h" #include "mozilla/UniquePtr.h" #include "mozilla/dom/BindingDeclarations.h" -#include "nsISupports.h" #include "nsTArray.h" #ifdef DEBUG # include "nsIThread.h" #endif +class nsIGlobalObject; class nsIInputStream; namespace mozilla { @@ -162,7 +162,7 @@ class StructuredCloneHolder : public StructuredCloneHolderBase { JS::Handle aTransfer, JS::CloneDataPolicy cloneDataPolicy, ErrorResult& aRv); - void Read(nsISupports* aParent, JSContext* aCx, + void Read(nsIGlobalObject* aGlobal, JSContext* aCx, JS::MutableHandle aValue, ErrorResult& aRv); // Call this method to know if this object is keeping some DOM object alive. @@ -191,9 +191,9 @@ class StructuredCloneHolder : public StructuredCloneHolderBase { StructuredCloneScope CloneScope() const { return mStructuredCloneScope; } - // The parent object is set internally just during the Read(). This method + // The global object is set internally just during the Read(). This method // can be used by read functions to retrieve it. - nsISupports* ParentDuringRead() const { return mParent; } + nsIGlobalObject* GlobalDuringRead() const { return mGlobal; } // This must be called if the transferring has ports generated by Read(). // MessagePorts are not thread-safe and they must be retrieved in the thread @@ -270,11 +270,11 @@ class StructuredCloneHolder : public StructuredCloneHolderBase { // If you receive a buffer from IPC, you can use this method to retrieve a // JS::Value. It can happen that you want to pre-populate the array of Blobs // and/or the PortIdentifiers. - void ReadFromBuffer(nsISupports* aParent, JSContext* aCx, + void ReadFromBuffer(nsIGlobalObject* aGlobal, JSContext* aCx, JSStructuredCloneData& aBuffer, JS::MutableHandle aValue, ErrorResult& aRv); - void ReadFromBuffer(nsISupports* aParent, JSContext* aCx, + void ReadFromBuffer(nsIGlobalObject* aGlobal, JSContext* aCx, JSStructuredCloneData& aBuffer, uint32_t aAlgorithmVersion, JS::MutableHandle aValue, ErrorResult& aRv); @@ -304,7 +304,7 @@ class StructuredCloneHolder : public StructuredCloneHolderBase { nsTArray> mClonedSurfaces; // This raw pointer is only set within ::Read() and is unset by the end. - nsISupports* MOZ_NON_OWNING_REF mParent; + nsIGlobalObject* MOZ_NON_OWNING_REF mGlobal; // This array contains the ports once we've finished the reading. It's // generated from the mPortIdentifiers array. diff --git a/dom/base/nsDOMDataChannel.cpp b/dom/base/nsDOMDataChannel.cpp index b5aa434de84f..65fa5c7c86e8 100644 --- a/dom/base/nsDOMDataChannel.cpp +++ b/dom/base/nsDOMDataChannel.cpp @@ -284,7 +284,7 @@ nsresult nsDOMDataChannel::DoOnMessageAvailable(const nsACString& aData, if (aBinary) { if (mBinaryType == DC_BINARY_TYPE_BLOB) { RefPtr blob = - Blob::CreateStringBlob(GetOwner(), aData, EmptyString()); + Blob::CreateStringBlob(GetOwnerGlobal(), aData, EmptyString()); MOZ_ASSERT(blob); if (!ToJSValue(cx, blob, &jsData)) { diff --git a/dom/console/Console.cpp b/dom/console/Console.cpp index 30f611cdc694..14976e07ff72 100644 --- a/dom/console/Console.cpp +++ b/dom/console/Console.cpp @@ -72,7 +72,7 @@ namespace mozilla { namespace dom { struct ConsoleStructuredCloneData { - nsCOMPtr mParent; + nsCOMPtr mGlobal; nsTArray> mBlobs; }; @@ -291,7 +291,7 @@ class ConsoleRunnable : public StructuredCloneHolderBase { JS::Rooted val(aCx); { - RefPtr blob = Blob::Create(mClonedData.mParent, + RefPtr blob = Blob::Create(mClonedData.mGlobal, mClonedData.mBlobs.ElementAt(aIndex)); if (!ToJSValue(aCx, blob, &val)) { return nullptr; @@ -451,7 +451,7 @@ class ConsoleRunnable : public StructuredCloneHolderBase { JS::Rooted argumentsValue(aCx); bool ok = Read(aCx, &argumentsValue); - mClonedData.mParent = nullptr; + mClonedData.mGlobal = nullptr; if (!ok) { return; @@ -657,7 +657,8 @@ class ConsoleWorkerRunnable : public WorkerProxyToMainThreadRunnable, return; } - RunConsole(jsapi.cx(), aWorkerPrivate, outerWindow, aWindow); + RunConsole(jsapi.cx(), aWindow->AsGlobal(), aWorkerPrivate, outerWindow, + aWindow); } void RunWindowless(WorkerPrivate* aWorkerPrivate) { @@ -688,7 +689,12 @@ class ConsoleWorkerRunnable : public WorkerProxyToMainThreadRunnable, JSAutoRealm ar(cx, global); - RunConsole(cx, aWorkerPrivate, nullptr, nullptr); + nsCOMPtr globalObject = xpc::NativeGlobal(global); + if (NS_WARN_IF(!globalObject)) { + return; + } + + RunConsole(cx, globalObject, aWorkerPrivate, nullptr, nullptr); } void RunBackOnWorkerThreadForCleanup(WorkerPrivate* aWorkerPrivate) override { @@ -702,7 +708,8 @@ class ConsoleWorkerRunnable : public WorkerProxyToMainThreadRunnable, virtual bool PreDispatch(JSContext* aCx) = 0; // This method is called in the main-thread. - virtual void RunConsole(JSContext* aCx, WorkerPrivate* aWorkerPrivate, + virtual void RunConsole(JSContext* aCx, nsIGlobalObject* aGlobal, + WorkerPrivate* aWorkerPrivate, nsPIDOMWindowOuter* aOuterWindow, nsPIDOMWindowInner* aInnerWindow) = 0; @@ -735,9 +742,11 @@ class ConsoleCallDataWorkerRunnable final : public ConsoleWorkerRunnable { return StoreConsoleData(aCx, mCallData); } - void RunConsole(JSContext* aCx, WorkerPrivate* aWorkerPrivate, + void RunConsole(JSContext* aCx, nsIGlobalObject* aGlobal, + WorkerPrivate* aWorkerPrivate, nsPIDOMWindowOuter* aOuterWindow, nsPIDOMWindowInner* aInnerWindow) override { + MOZ_ASSERT(aGlobal); MOZ_ASSERT(aWorkerPrivate); AssertIsOnMainThread(); @@ -768,12 +777,11 @@ class ConsoleCallDataWorkerRunnable final : public ConsoleWorkerRunnable { mCallData->SetIDs(id, innerID); } - // Now we could have the correct window (if we are not window-less). - mClonedData.mParent = aInnerWindow; + mClonedData.mGlobal = aGlobal; ProcessCallData(aCx, mConsole, mCallData); - mClonedData.mParent = nullptr; + mClonedData.mGlobal = nullptr; } virtual void ReleaseData() override { @@ -856,17 +864,18 @@ class ConsoleProfileWorkerRunnable final : public ConsoleWorkerRunnable { return StoreProfileData(aCx, mArguments); } - void RunConsole(JSContext* aCx, WorkerPrivate* aWorkerPrivate, + void RunConsole(JSContext* aCx, nsIGlobalObject* aGlobal, + WorkerPrivate* aWorkerPrivate, nsPIDOMWindowOuter* aOuterWindow, nsPIDOMWindowInner* aInnerWindow) override { AssertIsOnMainThread(); + MOZ_ASSERT(aGlobal); - // Now we could have the correct window (if we are not window-less). - mClonedData.mParent = aInnerWindow; + mClonedData.mGlobal = aGlobal; ProcessProfileData(aCx, mName, mAction); - mClonedData.mParent = nullptr; + mClonedData.mGlobal = nullptr; } virtual void ReleaseData() override {} diff --git a/dom/events/DataTransferItem.cpp b/dom/events/DataTransferItem.cpp index 76edc073f3cc..6cfe8a062310 100644 --- a/dom/events/DataTransferItem.cpp +++ b/dom/events/DataTransferItem.cpp @@ -286,14 +286,21 @@ already_AddRefed DataTransferItem::GetAsFile( if (RefPtr blob = do_QueryObject(supports)) { mCachedFile = blob->ToFile(); - } else if (nsCOMPtr blobImpl = do_QueryInterface(supports)) { - MOZ_ASSERT(blobImpl->IsFile()); - mCachedFile = File::Create(mDataTransfer, blobImpl); - } else if (nsCOMPtr ifile = do_QueryInterface(supports)) { - mCachedFile = File::CreateFromFile(mDataTransfer, ifile); } else { - MOZ_ASSERT(false, "One of the above code paths should be taken"); - return nullptr; + nsCOMPtr global = GetGlobalFromDataTransfer(); + if (NS_WARN_IF(!global)) { + return nullptr; + } + + if (nsCOMPtr blobImpl = do_QueryInterface(supports)) { + MOZ_ASSERT(blobImpl->IsFile()); + mCachedFile = File::Create(global, blobImpl); + } else if (nsCOMPtr ifile = do_QueryInterface(supports)) { + mCachedFile = File::CreateFromFile(global, ifile); + } else { + MOZ_ASSERT(false, "One of the above code paths should be taken"); + return nullptr; + } } } @@ -308,20 +315,8 @@ already_AddRefed DataTransferItem::GetAsEntry( return nullptr; } - nsCOMPtr global; - // This is annoying, but DataTransfer may have various things as parent. - nsCOMPtr target = - do_QueryInterface(mDataTransfer->GetParentObject()); - if (target) { - global = target->GetOwnerGlobal(); - } else { - RefPtr event = do_QueryObject(mDataTransfer->GetParentObject()); - if (event) { - global = event->GetParentObject(); - } - } - - if (!global) { + nsCOMPtr global = GetGlobalFromDataTransfer(); + if (NS_WARN_IF(!global)) { return nullptr; } @@ -390,7 +385,12 @@ already_AddRefed DataTransferItem::CreateFileFromInputStream( return nullptr; } - return File::CreateMemoryFile(mDataTransfer, data, available, fileName, mType, + nsCOMPtr global = GetGlobalFromDataTransfer(); + if (NS_WARN_IF(!global)) { + return nullptr; + } + + return File::CreateMemoryFile(global, data, available, fileName, mType, PR_Now()); } @@ -551,5 +551,23 @@ already_AddRefed DataTransferItem::Data(nsIPrincipal* aPrincipal, return variant.forget(); } +already_AddRefed +DataTransferItem::GetGlobalFromDataTransfer() { + nsCOMPtr global; + // This is annoying, but DataTransfer may have various things as parent. + nsCOMPtr target = + do_QueryInterface(mDataTransfer->GetParentObject()); + if (target) { + global = target->GetOwnerGlobal(); + } else { + RefPtr event = do_QueryObject(mDataTransfer->GetParentObject()); + if (event) { + global = event->GetParentObject(); + } + } + + return global.forget(); +} + } // namespace dom } // namespace mozilla diff --git a/dom/events/DataTransferItem.h b/dom/events/DataTransferItem.h index 1f73a0e4f0af..a34973122935 100644 --- a/dom/events/DataTransferItem.h +++ b/dom/events/DataTransferItem.h @@ -105,6 +105,8 @@ class DataTransferItem final : public nsISupports, public nsWrapperCache { ~DataTransferItem() {} already_AddRefed CreateFileFromInputStream(nsIInputStream* aStream); + already_AddRefed GetGlobalFromDataTransfer(); + // The index in the 2d mIndexedItems array uint32_t mIndex; diff --git a/dom/file/Blob.cpp b/dom/file/Blob.cpp index 37077521c3bb..66d7188dfeca 100644 --- a/dom/file/Blob.cpp +++ b/dom/file/Blob.cpp @@ -13,6 +13,7 @@ #include "mozilla/dom/WorkerCommon.h" #include "mozilla/dom/WorkerPrivate.h" #include "MultipartBlobImpl.h" +#include "nsIGlobalObject.h" #include "nsIInputStream.h" #include "nsPIDOMWindow.h" #include "StreamBlobImpl.h" @@ -25,12 +26,12 @@ namespace dom { NS_IMPL_CYCLE_COLLECTION_CLASS(Blob) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Blob) - NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal) NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Blob) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Blob) @@ -67,43 +68,43 @@ void Blob::MakeValidBlobType(nsAString& aType) { } /* static */ -Blob* Blob::Create(nsISupports* aParent, BlobImpl* aImpl) { +Blob* Blob::Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl) { MOZ_ASSERT(aImpl); - return aImpl->IsFile() ? new File(aParent, aImpl) : new Blob(aParent, aImpl); + return aImpl->IsFile() ? new File(aGlobal, aImpl) : new Blob(aGlobal, aImpl); } /* static */ -already_AddRefed Blob::CreateEmptyBlob(nsISupports* aParent, +already_AddRefed Blob::CreateEmptyBlob(nsIGlobalObject* aGlobal, const nsAString& aContentType) { - RefPtr blob = Blob::Create(aParent, new EmptyBlobImpl(aContentType)); + RefPtr blob = Blob::Create(aGlobal, new EmptyBlobImpl(aContentType)); MOZ_ASSERT(!blob->mImpl->IsFile()); return blob.forget(); } /* static */ -already_AddRefed Blob::CreateStringBlob(nsISupports* aParent, +already_AddRefed Blob::CreateStringBlob(nsIGlobalObject* aGlobal, const nsACString& aData, const nsAString& aContentType) { RefPtr blobImpl = StringBlobImpl::Create(aData, aContentType); - RefPtr blob = Blob::Create(aParent, blobImpl); + RefPtr blob = Blob::Create(aGlobal, blobImpl); MOZ_ASSERT(!blob->mImpl->IsFile()); return blob.forget(); } /* static */ -already_AddRefed Blob::CreateMemoryBlob(nsISupports* aParent, +already_AddRefed Blob::CreateMemoryBlob(nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength, const nsAString& aContentType) { RefPtr blob = Blob::Create( - aParent, new MemoryBlobImpl(aMemoryBuffer, aLength, aContentType)); + aGlobal, new MemoryBlobImpl(aMemoryBuffer, aLength, aContentType)); MOZ_ASSERT(!blob->mImpl->IsFile()); return blob.forget(); } -Blob::Blob(nsISupports* aParent, BlobImpl* aImpl) - : mImpl(aImpl), mParent(aParent) { +Blob::Blob(nsIGlobalObject* aGlobal, BlobImpl* aImpl) + : mImpl(aImpl), mGlobal(aGlobal) { MOZ_ASSERT(mImpl); } @@ -124,7 +125,7 @@ already_AddRefed Blob::ToFile() { if (HasFileInterface()) { file = static_cast(this); } else { - file = new File(mParent, mImpl); + file = new File(mGlobal, mImpl); } return file.forget(); @@ -143,7 +144,7 @@ already_AddRefed Blob::ToFile(const nsAString& aName, return nullptr; } - RefPtr file = new File(mParent, impl); + RefPtr file = new File(mGlobal, impl); return file.forget(); } @@ -156,7 +157,7 @@ already_AddRefed Blob::CreateSlice(uint64_t aStart, uint64_t aLength, return nullptr; } - RefPtr blob = Blob::Create(mParent, impl); + RefPtr blob = Blob::Create(mGlobal, impl); return blob.forget(); } @@ -182,7 +183,7 @@ already_AddRefed Blob::Slice(const Optional& aStart, return nullptr; } - RefPtr blob = Blob::Create(mParent, impl); + RefPtr blob = Blob::Create(mGlobal, impl); return blob.forget(); } @@ -228,7 +229,10 @@ already_AddRefed Blob::Constructor( MOZ_ASSERT(!impl->IsFile()); - RefPtr blob = Blob::Create(aGlobal.GetAsSupports(), impl); + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + MOZ_ASSERT(global); + + RefPtr blob = Blob::Create(global, impl); return blob.forget(); } @@ -261,8 +265,7 @@ already_AddRefed Blob::ArrayBuffer(ErrorResult& aRv) { already_AddRefed Blob::ConsumeBody( BodyConsumer::ConsumeType aConsumeType, ErrorResult& aRv) { - nsCOMPtr global = do_QueryInterface(mParent); - if (NS_WARN_IF(!global)) { + if (NS_WARN_IF(!mGlobal)) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; } @@ -273,7 +276,7 @@ already_AddRefed Blob::ConsumeBody( MOZ_ASSERT(workerPrivate); mainThreadEventTarget = workerPrivate->MainThreadEventTarget(); } else { - mainThreadEventTarget = global->EventTargetFor(TaskCategory::Other); + mainThreadEventTarget = mGlobal->EventTargetFor(TaskCategory::Other); } MOZ_ASSERT(mainThreadEventTarget); @@ -284,7 +287,7 @@ already_AddRefed Blob::ConsumeBody( return nullptr; } - return BodyConsumer::Create(global, mainThreadEventTarget, inputStream, + return BodyConsumer::Create(mGlobal, mainThreadEventTarget, inputStream, nullptr, aConsumeType, VoidCString(), VoidString(), VoidCString(), MutableBlobStorage::eOnlyInMemory, aRv); @@ -353,15 +356,14 @@ void Blob::Stream(JSContext* aCx, JS::MutableHandle aStream, return; } - nsCOMPtr global = do_QueryInterface(GetParentObject()); - if (NS_WARN_IF(!global)) { + if (NS_WARN_IF(!mGlobal)) { aRv.Throw(NS_ERROR_FAILURE); return; } RefPtr holder = new BlobBodyStreamHolder(); - BodyStream::Create(aCx, holder, global, stream, aRv); + BodyStream::Create(aCx, holder, mGlobal, stream, aRv); if (NS_WARN_IF(aRv.Failed())) { return; } diff --git a/dom/file/Blob.h b/dom/file/Blob.h index cd7846a315f8..dcf0c5373f6d 100644 --- a/dom/file/Blob.h +++ b/dom/file/Blob.h @@ -18,6 +18,7 @@ #include "nsWrapperCache.h" #include "nsWeakReference.h" +class nsIGlobalObject; class nsIInputStream; namespace mozilla { @@ -48,19 +49,19 @@ class Blob : public nsIMutable, typedef OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString BlobPart; // This creates a Blob or a File based on the type of BlobImpl. - static Blob* Create(nsISupports* aParent, BlobImpl* aImpl); + static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl); - static already_AddRefed CreateEmptyBlob(nsISupports* aParent, + static already_AddRefed CreateEmptyBlob(nsIGlobalObject* aGlobal, const nsAString& aContentType); - static already_AddRefed CreateStringBlob(nsISupports* aParent, + static already_AddRefed CreateStringBlob(nsIGlobalObject* aGlobal, const nsACString& aData, const nsAString& aContentType); // The returned Blob takes ownership of aMemoryBuffer. aMemoryBuffer will be // freed by free so it must be allocated by malloc or something // compatible with it. - static already_AddRefed CreateMemoryBlob(nsISupports* aParent, + static already_AddRefed CreateMemoryBlob(nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength, const nsAString& aContentType); @@ -97,7 +98,7 @@ class Blob : public nsIMutable, static void MakeValidBlobType(nsAString& aType); // WebIDL methods - nsISupports* GetParentObject() const { return mParent; } + nsIGlobalObject* GetParentObject() const { return mGlobal; } bool IsMemoryFile() const; @@ -132,7 +133,7 @@ class Blob : public nsIMutable, protected: // File constructor should never be used directly. Use Blob::Create instead. - Blob(nsISupports* aParent, BlobImpl* aImpl); + Blob(nsIGlobalObject* aGlobal, BlobImpl* aImpl); virtual ~Blob(); virtual bool HasFileInterface() const { return false; } @@ -147,7 +148,7 @@ class Blob : public nsIMutable, RefPtr mImpl; private: - nsCOMPtr mParent; + nsCOMPtr mGlobal; }; NS_DEFINE_STATIC_IID_ACCESSOR(Blob, NS_DOM_BLOB_IID) diff --git a/dom/file/File.cpp b/dom/file/File.cpp index b529feabff0a..ac6ffefd5c3a 100644 --- a/dom/file/File.cpp +++ b/dom/file/File.cpp @@ -18,61 +18,61 @@ namespace mozilla { namespace dom { -File::File(nsISupports* aParent, BlobImpl* aImpl) : Blob(aParent, aImpl) { +File::File(nsIGlobalObject* aGlobal, BlobImpl* aImpl) : Blob(aGlobal, aImpl) { MOZ_ASSERT(aImpl->IsFile()); } File::~File() {} /* static */ -File* File::Create(nsISupports* aParent, BlobImpl* aImpl) { +File* File::Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl) { MOZ_ASSERT(aImpl); MOZ_ASSERT(aImpl->IsFile()); - return new File(aParent, aImpl); + return new File(aGlobal, aImpl); } /* static */ -already_AddRefed File::Create(nsISupports* aParent, +already_AddRefed File::Create(nsIGlobalObject* aGlobal, const nsAString& aName, const nsAString& aContentType, uint64_t aLength, int64_t aLastModifiedDate) { RefPtr file = new File( - aParent, new BaseBlobImpl(NS_LITERAL_STRING("BaseBlobImpl"), aName, + aGlobal, new BaseBlobImpl(NS_LITERAL_STRING("BaseBlobImpl"), aName, aContentType, aLength, aLastModifiedDate)); return file.forget(); } /* static */ -already_AddRefed File::CreateMemoryFile(nsISupports* aParent, +already_AddRefed File::CreateMemoryFile(nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength, const nsAString& aName, const nsAString& aContentType, int64_t aLastModifiedDate) { RefPtr file = - new File(aParent, new MemoryBlobImpl(aMemoryBuffer, aLength, aName, + new File(aGlobal, new MemoryBlobImpl(aMemoryBuffer, aLength, aName, aContentType, aLastModifiedDate)); return file.forget(); } /* static */ -already_AddRefed File::CreateFromFile(nsISupports* aParent, +already_AddRefed File::CreateFromFile(nsIGlobalObject* aGlobal, nsIFile* aFile) { MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess()); - RefPtr file = new File(aParent, new FileBlobImpl(aFile)); + RefPtr file = new File(aGlobal, new FileBlobImpl(aFile)); return file.forget(); } /* static */ -already_AddRefed File::CreateFromFile(nsISupports* aParent, +already_AddRefed File::CreateFromFile(nsIGlobalObject* aGlobal, nsIFile* aFile, const nsAString& aName, const nsAString& aContentType) { MOZ_DIAGNOSTIC_ASSERT(XRE_IsParentProcess()); RefPtr file = - new File(aParent, new FileBlobImpl(aFile, aName, aContentType)); + new File(aGlobal, new FileBlobImpl(aFile, aName, aContentType)); return file.forget(); } @@ -132,7 +132,10 @@ already_AddRefed File::Constructor(const GlobalObject& aGlobal, impl->SetLastModified(aBag.mLastModified.Value()); } - RefPtr file = new File(aGlobal.GetAsSupports(), impl); + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + MOZ_ASSERT(global); + + RefPtr file = new File(global, impl); return file.forget(); } diff --git a/dom/file/File.h b/dom/file/File.h index ad656aa3cd59..1ad6d0fbdff5 100644 --- a/dom/file/File.h +++ b/dom/file/File.h @@ -25,9 +25,9 @@ class File final : public Blob { public: // Note: BlobImpl must be a File in order to use this method. // Check impl->IsFile(). - static File* Create(nsISupports* aParent, BlobImpl* aImpl); + static File* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl); - static already_AddRefed Create(nsISupports* aParent, + static already_AddRefed Create(nsIGlobalObject* aGlobal, const nsAString& aName, const nsAString& aContentType, uint64_t aLength, @@ -36,7 +36,7 @@ class File final : public Blob { // The returned File takes ownership of aMemoryBuffer. aMemoryBuffer will be // freed by free so it must be allocated by malloc or something // compatible with it. - static already_AddRefed CreateMemoryFile(nsISupports* aParent, + static already_AddRefed CreateMemoryFile(nsIGlobalObject* aGlobal, void* aMemoryBuffer, uint64_t aLength, const nsAString& aName, @@ -49,10 +49,10 @@ class File final : public Blob { // order to use nsIMIMEService. // Would be nice if we try to avoid to use this method outside the // main-thread to avoid extra runnables. - static already_AddRefed CreateFromFile(nsISupports* aParent, + static already_AddRefed CreateFromFile(nsIGlobalObject* aGlobal, nsIFile* aFile); - static already_AddRefed CreateFromFile(nsISupports* aParent, + static already_AddRefed CreateFromFile(nsIGlobalObject* aGlobal, nsIFile* aFile, const nsAString& aName, const nsAString& aContentType); @@ -98,7 +98,7 @@ class File final : public Blob { private: // File constructor should never be used directly. Use Blob::Create or // File::Create. - File(nsISupports* aParent, BlobImpl* aImpl); + File(nsIGlobalObject* aGlobal, BlobImpl* aImpl); ~File(); }; diff --git a/dom/file/MutableBlobStorage.cpp b/dom/file/MutableBlobStorage.cpp index 7e0fd9ce60bd..0246f9fd3f8c 100644 --- a/dom/file/MutableBlobStorage.cpp +++ b/dom/file/MutableBlobStorage.cpp @@ -215,7 +215,9 @@ class CreateBlobRunnable final : public Runnable, nsCOMPtr parent(std::move(mParent)); RefPtr callback(std::move(mCallback)); - RefPtr blob = Blob::Create(parent, aBlobImpl); + nsCOMPtr global = do_QueryInterface(parent); + + RefPtr blob = Blob::Create(global, aBlobImpl); callback->BlobStoreCompleted(mBlobStorage, blob, NS_OK); } @@ -391,7 +393,9 @@ void MutableBlobStorage::GetBlobWhenReady( blobImpl = new EmptyBlobImpl(NS_ConvertUTF8toUTF16(aContentType)); } - RefPtr blob = Blob::Create(aParent, blobImpl); + nsCOMPtr global = do_QueryInterface(aParent); + + RefPtr blob = Blob::Create(global, blobImpl); RefPtr runnable = new BlobCreationDoneRunnable(this, aCallback, blob, NS_OK); diff --git a/dom/filesystem/Directory.cpp b/dom/filesystem/Directory.cpp index d8c74e49a762..c41178ebcaaa 100644 --- a/dom/filesystem/Directory.cpp +++ b/dom/filesystem/Directory.cpp @@ -27,7 +27,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Directory) tmp->mFileSystem->Unlink(); tmp->mFileSystem = nullptr; } - NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal) NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER NS_IMPL_CYCLE_COLLECTION_UNLINK_END @@ -35,7 +35,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Directory) if (tmp->mFileSystem) { tmp->mFileSystem->Traverse(cb); } - NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Directory) @@ -57,29 +57,35 @@ already_AddRefed Directory::Constructor(const GlobalObject& aGlobal, return nullptr; } - return Create(aGlobal.GetAsSupports(), path); + nsCOMPtr global = do_QueryInterface(aGlobal.GetAsSupports()); + if (NS_WARN_IF(!global)) { + aRv.Throw(NS_ERROR_FAILURE); + return nullptr; + } + + return Create(global, path); } /* static */ -already_AddRefed Directory::Create(nsISupports* aParent, +already_AddRefed Directory::Create(nsIGlobalObject* aGlobal, nsIFile* aFile, FileSystemBase* aFileSystem) { - MOZ_ASSERT(aParent); + MOZ_ASSERT(aGlobal); MOZ_ASSERT(aFile); - RefPtr directory = new Directory(aParent, aFile, aFileSystem); + RefPtr directory = new Directory(aGlobal, aFile, aFileSystem); return directory.forget(); } -Directory::Directory(nsISupports* aParent, nsIFile* aFile, +Directory::Directory(nsIGlobalObject* aGlobal, nsIFile* aFile, FileSystemBase* aFileSystem) - : mParent(aParent), mFile(aFile) { + : mGlobal(aGlobal), mFile(aFile) { MOZ_ASSERT(aFile); // aFileSystem can be null. In this case we create a OSFileSystem when needed. if (aFileSystem) { // More likely, this is a OSFileSystem. This object keeps a reference of - // mParent but it's not cycle collectable and to avoid manual + // mGlobal but it's not cycle collectable and to avoid manual // addref/release, it's better to have 1 object per directory. For this // reason we clone it here. mFileSystem = aFileSystem->Clone(); @@ -88,7 +94,7 @@ Directory::Directory(nsISupports* aParent, nsIFile* aFile, Directory::~Directory() {} -nsISupports* Directory::GetParentObject() const { return mParent; } +nsIGlobalObject* Directory::GetParentObject() const { return mGlobal; } JSObject* Directory::WrapObject(JSContext* aCx, JS::Handle aGivenProto) { @@ -183,7 +189,7 @@ FileSystemBase* Directory::GetFileSystem(ErrorResult& aRv) { } RefPtr fs = new OSFileSystem(path); - fs->Init(mParent); + fs->Init(mGlobal); mFileSystem = fs; } diff --git a/dom/filesystem/Directory.h b/dom/filesystem/Directory.h index 1f4c508a3476..61a5667f127b 100644 --- a/dom/filesystem/Directory.h +++ b/dom/filesystem/Directory.h @@ -30,13 +30,13 @@ class Directory final : public nsISupports, public nsWrapperCache { const nsAString& aRealPath, ErrorResult& aRv); - static already_AddRefed Create(nsISupports* aParent, + static already_AddRefed Create(nsIGlobalObject* aGlobal, nsIFile* aDirectory, FileSystemBase* aFileSystem = 0); // ========= Begin WebIDL bindings. =========== - nsISupports* GetParentObject() const; + nsIGlobalObject* GetParentObject() const; virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; @@ -86,7 +86,7 @@ class Directory final : public nsISupports, public nsWrapperCache { nsIFile* GetInternalNsIFile() const { return mFile; } private: - Directory(nsISupports* aParent, nsIFile* aFile, + Directory(nsIGlobalObject* aGlobal, nsIFile* aFile, FileSystemBase* aFileSystem = nullptr); ~Directory(); @@ -95,7 +95,7 @@ class Directory final : public nsISupports, public nsWrapperCache { */ nsresult DOMPathToRealPath(const nsAString& aPath, nsIFile** aFile) const; - nsCOMPtr mParent; + nsCOMPtr mGlobal; RefPtr mFileSystem; nsCOMPtr mFile; diff --git a/dom/filesystem/FileSystemBase.cpp b/dom/filesystem/FileSystemBase.cpp index 630bacfa2636..744d6cd48259 100644 --- a/dom/filesystem/FileSystemBase.cpp +++ b/dom/filesystem/FileSystemBase.cpp @@ -21,7 +21,7 @@ void FileSystemBase::Shutdown() { mShutdown = true; } -nsISupports* FileSystemBase::GetParentObject() const { +nsIGlobalObject* FileSystemBase::GetParentObject() const { AssertIsOnOwningThread(); return nullptr; } diff --git a/dom/filesystem/FileSystemBase.h b/dom/filesystem/FileSystemBase.h index 90b72b84221e..4337693ce051 100644 --- a/dom/filesystem/FileSystemBase.h +++ b/dom/filesystem/FileSystemBase.h @@ -30,7 +30,7 @@ class FileSystemBase { virtual bool ShouldCreateDirectory() = 0; - virtual nsISupports* GetParentObject() const; + virtual nsIGlobalObject* GetParentObject() const; virtual void GetDirectoryName(nsIFile* aFile, nsAString& aRetval, ErrorResult& aRv) const; diff --git a/dom/filesystem/GetDirectoryListingTask.cpp b/dom/filesystem/GetDirectoryListingTask.cpp index 1f1e6450e621..b09a7e1bcbb0 100644 --- a/dom/filesystem/GetDirectoryListingTask.cpp +++ b/dom/filesystem/GetDirectoryListingTask.cpp @@ -37,12 +37,8 @@ GetDirectoryListingTaskChild::Create(FileSystemBase* aFileSystem, MOZ_ASSERT(aDirectory); aFileSystem->AssertIsOnOwningThread(); - nsCOMPtr globalObject = - do_QueryInterface(aFileSystem->GetParentObject()); - if (NS_WARN_IF(!globalObject)) { - aRv.Throw(NS_ERROR_FAILURE); - return nullptr; - } + nsCOMPtr globalObject = aFileSystem->GetParentObject(); + MOZ_ASSERT(globalObject); RefPtr task = new GetDirectoryListingTaskChild( globalObject, aFileSystem, aDirectory, aTargetPath, aFilters); @@ -123,8 +119,10 @@ void GetDirectoryListingTaskChild::SetSuccessRequestResult( RefPtr blobImpl = IPCBlobUtils::Deserialize(d.blob()); MOZ_ASSERT(blobImpl); - RefPtr file = - File::Create(mFileSystem->GetParentObject(), blobImpl); + nsCOMPtr globalObject = mFileSystem->GetParentObject(); + MOZ_ASSERT(globalObject); + + RefPtr file = File::Create(globalObject, blobImpl); MOZ_ASSERT(file); ofd->SetAsFile() = file; diff --git a/dom/filesystem/GetFileOrDirectoryTask.cpp b/dom/filesystem/GetFileOrDirectoryTask.cpp index ae78ae4b5b67..2373a568c119 100644 --- a/dom/filesystem/GetFileOrDirectoryTask.cpp +++ b/dom/filesystem/GetFileOrDirectoryTask.cpp @@ -30,8 +30,7 @@ GetFileOrDirectoryTaskChild::Create(FileSystemBase* aFileSystem, MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread!"); MOZ_ASSERT(aFileSystem); - nsCOMPtr globalObject = - do_QueryInterface(aFileSystem->GetParentObject()); + nsCOMPtr globalObject = aFileSystem->GetParentObject(); if (NS_WARN_IF(!globalObject)) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; @@ -91,7 +90,10 @@ void GetFileOrDirectoryTaskChild::SetSuccessRequestResult( RefPtr blobImpl = IPCBlobUtils::Deserialize(r.blob()); MOZ_ASSERT(blobImpl); - mResultFile = File::Create(mFileSystem->GetParentObject(), blobImpl); + nsCOMPtr globalObject = mFileSystem->GetParentObject(); + MOZ_ASSERT(globalObject); + + mResultFile = File::Create(globalObject, blobImpl); MOZ_ASSERT(mResultFile); break; } diff --git a/dom/filesystem/GetFilesTask.cpp b/dom/filesystem/GetFilesTask.cpp index dbfc7ce5d8a5..5881469bb3e7 100644 --- a/dom/filesystem/GetFilesTask.cpp +++ b/dom/filesystem/GetFilesTask.cpp @@ -32,8 +32,7 @@ already_AddRefed GetFilesTaskChild::Create( MOZ_ASSERT(aDirectory); aFileSystem->AssertIsOnOwningThread(); - nsCOMPtr globalObject = - do_QueryInterface(aFileSystem->GetParentObject()); + nsCOMPtr globalObject = aFileSystem->GetParentObject(); if (NS_WARN_IF(!globalObject)) { aRv.Throw(NS_ERROR_FAILURE); return nullptr; @@ -107,12 +106,15 @@ void GetFilesTaskChild::SetSuccessRequestResult( return; } + nsCOMPtr globalObject = mFileSystem->GetParentObject(); + MOZ_ASSERT(globalObject); + for (uint32_t i = 0; i < r.data().Length(); ++i) { const FileSystemFileResponse& data = r.data()[i]; RefPtr blobImpl = IPCBlobUtils::Deserialize(data.blob()); MOZ_ASSERT(blobImpl); - mTargetData[i] = File::Create(mFileSystem->GetParentObject(), blobImpl); + mTargetData[i] = File::Create(globalObject, blobImpl); } } diff --git a/dom/filesystem/OSFileSystem.cpp b/dom/filesystem/OSFileSystem.cpp index 88f79e7b8cea..4125511ded43 100644 --- a/dom/filesystem/OSFileSystem.cpp +++ b/dom/filesystem/OSFileSystem.cpp @@ -25,29 +25,24 @@ already_AddRefed OSFileSystem::Clone() { AssertIsOnOwningThread(); RefPtr fs = new OSFileSystem(mLocalRootPath); - if (mParent) { - fs->Init(mParent); + if (mGlobal) { + fs->Init(mGlobal); } return fs.forget(); } -void OSFileSystem::Init(nsISupports* aParent) { +void OSFileSystem::Init(nsIGlobalObject* aGlobal) { AssertIsOnOwningThread(); - MOZ_ASSERT(!mParent, "No duple Init() calls"); - MOZ_ASSERT(aParent); + MOZ_ASSERT(!mGlobal, "No duple Init() calls"); + MOZ_ASSERT(aGlobal); - mParent = aParent; - -#ifdef DEBUG - nsCOMPtr obj = do_QueryInterface(aParent); - MOZ_ASSERT(obj); -#endif + mGlobal = aGlobal; } -nsISupports* OSFileSystem::GetParentObject() const { +nsIGlobalObject* OSFileSystem::GetParentObject() const { AssertIsOnOwningThread(); - return mParent; + return mGlobal; } bool OSFileSystem::IsSafeFile(nsIFile* aFile) const { @@ -68,14 +63,14 @@ bool OSFileSystem::IsSafeDirectory(Directory* aDir) const { void OSFileSystem::Unlink() { AssertIsOnOwningThread(); - mParent = nullptr; + mGlobal = nullptr; } void OSFileSystem::Traverse(nsCycleCollectionTraversalCallback& cb) { AssertIsOnOwningThread(); OSFileSystem* tmp = this; - NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent); + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal); } void OSFileSystem::SerializeDOMPath(nsAString& aOutput) const { diff --git a/dom/filesystem/OSFileSystem.h b/dom/filesystem/OSFileSystem.h index 6b4332073449..6d327ec0380d 100644 --- a/dom/filesystem/OSFileSystem.h +++ b/dom/filesystem/OSFileSystem.h @@ -16,7 +16,7 @@ class OSFileSystem final : public FileSystemBase { public: explicit OSFileSystem(const nsAString& aRootDir); - void Init(nsISupports* aParent); + void Init(nsIGlobalObject* aGlobal); // Overrides FileSystemBase @@ -29,7 +29,7 @@ class OSFileSystem final : public FileSystemBase { return false; } - virtual nsISupports* GetParentObject() const override; + virtual nsIGlobalObject* GetParentObject() const override; virtual bool IsSafeFile(nsIFile* aFile) const override; @@ -44,7 +44,7 @@ class OSFileSystem final : public FileSystemBase { private: virtual ~OSFileSystem() {} - nsCOMPtr mParent; + nsCOMPtr mGlobal; }; class OSFileSystemParent final : public FileSystemBase { @@ -60,7 +60,7 @@ class OSFileSystemParent final : public FileSystemBase { virtual bool ShouldCreateDirectory() override { return false; } - virtual nsISupports* GetParentObject() const override { + virtual nsIGlobalObject* GetParentObject() const override { MOZ_CRASH("This should not be called on the PBackground thread."); return nullptr; } diff --git a/dom/html/HTMLCanvasElement.cpp b/dom/html/HTMLCanvasElement.cpp index 05a1fcabd7b7..4ab21e3d902d 100644 --- a/dom/html/HTMLCanvasElement.cpp +++ b/dom/html/HTMLCanvasElement.cpp @@ -883,8 +883,8 @@ nsresult HTMLCanvasElement::MozGetAsFileImpl(const nsAString& aName, do_QueryInterface(OwnerDoc()->GetScopeObject()); // The File takes ownership of the buffer - RefPtr file = - File::CreateMemoryFile(win, imgData, imgSize, aName, type, PR_Now()); + RefPtr file = File::CreateMemoryFile(win->AsGlobal(), imgData, imgSize, + aName, type, PR_Now()); file.forget(aResult); return NS_OK; diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp index 4d04c1dc45e8..db28c0d59d0a 100644 --- a/dom/html/HTMLInputElement.cpp +++ b/dom/html/HTMLInputElement.cpp @@ -2076,7 +2076,7 @@ void HTMLInputElement::MozSetDirectory(const nsAString& aDirectoryPath, return; } - RefPtr directory = Directory::Create(window, file); + RefPtr directory = Directory::Create(window->AsGlobal(), file); MOZ_ASSERT(directory); nsTArray array; @@ -6112,7 +6112,7 @@ static nsTArray RestoreFileContentData( continue; } - RefPtr file = File::Create(aWindow, it.get_BlobImpl()); + RefPtr file = File::Create(aWindow->AsGlobal(), it.get_BlobImpl()); MOZ_ASSERT(file); OwningFileOrDirectory* element = res.AppendElement(); @@ -6126,7 +6126,8 @@ static nsTArray RestoreFileContentData( continue; } - RefPtr directory = Directory::Create(aWindow, file); + RefPtr directory = + Directory::Create(aWindow->AsGlobal(), file); MOZ_ASSERT(directory); OwningFileOrDirectory* element = res.AppendElement(); diff --git a/dom/media/MediaRecorder.cpp b/dom/media/MediaRecorder.cpp index 367f5663391a..62cbef7b9094 100644 --- a/dom/media/MediaRecorder.cpp +++ b/dom/media/MediaRecorder.cpp @@ -874,7 +874,7 @@ class MediaRecorder::Session : public PrincipalChangeObserver, RefPtr storer = MakeAndAddRef(); MaybeCreateMutableBlobStorage(); mMutableBlobStorage->GetBlobWhenReady( - mRecorder->GetOwner(), NS_ConvertUTF16toUTF8(mMimeType), storer); + mRecorder->GetOwnerGlobal(), NS_ConvertUTF16toUTF8(mMimeType), storer); mMutableBlobStorage = nullptr; storer->Promise()->Then( diff --git a/dom/media/imagecapture/CaptureTask.cpp b/dom/media/imagecapture/CaptureTask.cpp index aa8383115744..1d169d84f813 100644 --- a/dom/media/imagecapture/CaptureTask.cpp +++ b/dom/media/imagecapture/CaptureTask.cpp @@ -48,7 +48,7 @@ nsresult CaptureTask::TaskComplete(already_AddRefed aBlob, // We have to set the parent because the blob has been generated with a valid // one. if (blob) { - blob = dom::Blob::Create(mImageCapture->GetParentObject(), blob->Impl()); + blob = dom::Blob::Create(mImageCapture->GetOwnerGlobal(), blob->Impl()); } if (mPrincipalChanged) { diff --git a/dom/presentation/PresentationConnection.cpp b/dom/presentation/PresentationConnection.cpp index 5d3c75a06e56..1e7c53f1a8f8 100644 --- a/dom/presentation/PresentationConnection.cpp +++ b/dom/presentation/PresentationConnection.cpp @@ -512,7 +512,7 @@ nsresult PresentationConnection::DoReceiveMessage(const nsACString& aData, if (aIsBinary) { if (mBinaryType == PresentationConnectionBinaryType::Blob) { RefPtr blob = - Blob::CreateStringBlob(GetOwner(), aData, EmptyString()); + Blob::CreateStringBlob(GetOwnerGlobal(), aData, EmptyString()); MOZ_ASSERT(blob); if (!ToJSValue(cx, blob, &jsData)) { diff --git a/dom/serviceworkers/ServiceWorkerEvents.cpp b/dom/serviceworkers/ServiceWorkerEvents.cpp index 22f55c625ca7..508cf1a6ecee 100644 --- a/dom/serviceworkers/ServiceWorkerEvents.cpp +++ b/dom/serviceworkers/ServiceWorkerEvents.cpp @@ -1051,7 +1051,7 @@ nsresult ExtractBytesFromData( } } // namespace -PushMessageData::PushMessageData(nsISupports* aOwner, +PushMessageData::PushMessageData(nsIGlobalObject* aOwner, nsTArray&& aBytes) : mOwner(aOwner), mBytes(std::move(aBytes)) {} @@ -1149,7 +1149,7 @@ already_AddRefed PushEvent::Constructor( aRv.Throw(rv); return nullptr; } - e->mData = new PushMessageData(aOwner, std::move(bytes)); + e->mData = new PushMessageData(aOwner->GetOwnerGlobal(), std::move(bytes)); } return e.forget(); } diff --git a/dom/serviceworkers/ServiceWorkerEvents.h b/dom/serviceworkers/ServiceWorkerEvents.h index ebdae877f2ab..84d84da66bec 100644 --- a/dom/serviceworkers/ServiceWorkerEvents.h +++ b/dom/serviceworkers/ServiceWorkerEvents.h @@ -183,7 +183,7 @@ class PushMessageData final : public nsISupports, public nsWrapperCache { virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - nsISupports* GetParentObject() const { return mOwner; } + nsIGlobalObject* GetParentObject() const { return mOwner; } void Json(JSContext* cx, JS::MutableHandle aRetval, ErrorResult& aRv); @@ -192,10 +192,10 @@ class PushMessageData final : public nsISupports, public nsWrapperCache { ErrorResult& aRv); already_AddRefed Blob(ErrorResult& aRv); - PushMessageData(nsISupports* aOwner, nsTArray&& aBytes); + PushMessageData(nsIGlobalObject* aOwner, nsTArray&& aBytes); private: - nsCOMPtr mOwner; + nsCOMPtr mOwner; nsTArray mBytes; nsString mDecodedText; ~PushMessageData(); diff --git a/dom/xhr/XMLHttpRequestMainThread.cpp b/dom/xhr/XMLHttpRequestMainThread.cpp index 2dcd8152fae5..a180eccbf0d7 100644 --- a/dom/xhr/XMLHttpRequestMainThread.cpp +++ b/dom/xhr/XMLHttpRequestMainThread.cpp @@ -1701,7 +1701,7 @@ XMLHttpRequestMainThread::OnDataAvailable(nsIRequest* request, rv = NS_GetBlobForBlobURI(blobURI, getter_AddRefs(blobImpl)); if (NS_SUCCEEDED(rv)) { if (blobImpl) { - mResponseBlob = Blob::Create(GetOwner(), blobImpl); + mResponseBlob = Blob::Create(GetOwnerGlobal(), blobImpl); } if (!mResponseBlob) { rv = NS_ERROR_FILE_NOT_FOUND; @@ -2143,7 +2143,7 @@ XMLHttpRequestMainThread::OnStopRequest(nsIRequest* request, nsresult status) { // mBlobStorage can be null if the channel is non-file non-cacheable // and if the response length is zero. MaybeCreateBlobStorage(); - mBlobStorage->GetBlobWhenReady(GetOwner(), contentType, this); + mBlobStorage->GetBlobWhenReady(GetOwnerGlobal(), contentType, this); waitingForBlobCreation = true; NS_ASSERTION(mResponseBody.IsEmpty(), "mResponseBody should be empty"); diff --git a/layout/forms/nsFileControlFrame.cpp b/layout/forms/nsFileControlFrame.cpp index 6711904d380a..a58ce4d72e78 100644 --- a/layout/forms/nsFileControlFrame.cpp +++ b/layout/forms/nsFileControlFrame.cpp @@ -331,7 +331,7 @@ static void AppendBlobImplAsDirectory(nsTArray& aArray, return; } - RefPtr directory = Directory::Create(inner, file); + RefPtr directory = Directory::Create(inner->AsGlobal(), file); MOZ_ASSERT(directory); OwningFileOrDirectory* element = aArray.AppendElement(); diff --git a/widget/nsBaseFilePicker.cpp b/widget/nsBaseFilePicker.cpp index 0df12a9115ec..1c983d743629 100644 --- a/widget/nsBaseFilePicker.cpp +++ b/widget/nsBaseFilePicker.cpp @@ -37,6 +37,8 @@ namespace { nsresult LocalFileToDirectoryOrBlob(nsPIDOMWindowInner* aWindow, bool aIsDirectory, nsIFile* aFile, nsISupports** aResult) { + MOZ_ASSERT(aWindow); + if (aIsDirectory) { #ifdef DEBUG bool isDir; @@ -44,14 +46,14 @@ nsresult LocalFileToDirectoryOrBlob(nsPIDOMWindowInner* aWindow, MOZ_ASSERT(isDir); #endif - RefPtr directory = Directory::Create(aWindow, aFile); + RefPtr directory = Directory::Create(aWindow->AsGlobal(), aFile); MOZ_ASSERT(directory); directory.forget(aResult); return NS_OK; } - RefPtr file = File::CreateFromFile(aWindow, aFile); + RefPtr file = File::CreateFromFile(aWindow->AsGlobal(), aFile); file.forget(aResult); return NS_OK; } @@ -119,6 +121,10 @@ class nsBaseFilePickerEnumerator : public nsSimpleEnumerator { return NS_ERROR_FAILURE; } + if (!mParent) { + return NS_ERROR_FAILURE; + } + return LocalFileToDirectoryOrBlob( mParent, mMode == nsIFilePicker::modeGetFolder, localFile, aResult); } @@ -378,6 +384,10 @@ nsBaseFilePicker::GetDomFileOrDirectory(nsISupports** aValue) { auto* innerParent = mParent ? mParent->GetCurrentInnerWindow() : nullptr; + if (!innerParent) { + return NS_ERROR_FAILURE; + } + return LocalFileToDirectoryOrBlob( innerParent, mMode == nsIFilePicker::modeGetFolder, localFile, aValue); } diff --git a/widget/nsFilePickerProxy.cpp b/widget/nsFilePickerProxy.cpp index 9229edd6e653..d784ebd2e8d3 100644 --- a/widget/nsFilePickerProxy.cpp +++ b/widget/nsFilePickerProxy.cpp @@ -146,6 +146,13 @@ nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback) { mozilla::ipc::IPCResult nsFilePickerProxy::Recv__delete__( const MaybeInputData& aData, const int16_t& aResult) { + nsPIDOMWindowInner* inner = + mParent ? mParent->GetCurrentInnerWindow() : nullptr; + + if (NS_WARN_IF(!inner)) { + return IPC_OK(); + } + if (aData.type() == MaybeInputData::TInputBlobs) { const nsTArray& blobs = aData.get_InputBlobs().blobs(); for (uint32_t i = 0; i < blobs.Length(); ++i) { @@ -156,9 +163,7 @@ mozilla::ipc::IPCResult nsFilePickerProxy::Recv__delete__( return IPC_OK(); } - nsPIDOMWindowInner* inner = - mParent ? mParent->GetCurrentInnerWindow() : nullptr; - RefPtr file = File::Create(inner, blobImpl); + RefPtr file = File::Create(inner->AsGlobal(), blobImpl); MOZ_ASSERT(file); OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement(); @@ -172,8 +177,7 @@ mozilla::ipc::IPCResult nsFilePickerProxy::Recv__delete__( return IPC_OK(); } - RefPtr directory = - Directory::Create(mParent->GetCurrentInnerWindow(), file); + RefPtr directory = Directory::Create(inner->AsGlobal(), file); MOZ_ASSERT(directory); OwningFileOrDirectory* element = mFilesOrDirectories.AppendElement();