From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/file/Blob.h | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 dom/file/Blob.h (limited to 'dom/file/Blob.h') diff --git a/dom/file/Blob.h b/dom/file/Blob.h new file mode 100644 index 0000000000..2314df32b3 --- /dev/null +++ b/dom/file/Blob.h @@ -0,0 +1,161 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_Blob_h +#define mozilla_dom_Blob_h + +#include "mozilla/dom/BodyConsumer.h" +#include "nsCycleCollectionParticipant.h" +#include "nsCOMPtr.h" +#include "nsWrapperCache.h" +#include "nsWeakReference.h" + +class nsIGlobalObject; +class nsIInputStream; + +namespace mozilla { +class ErrorResult; + +namespace dom { + +struct BlobPropertyBag; +class BlobImpl; +class File; +class GlobalObject; +class OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String; +class Promise; + +class ReadableStream; + +#define NS_DOM_BLOB_IID \ + { \ + 0x648c2a83, 0xbdb1, 0x4a7d, { \ + 0xb5, 0x0a, 0xca, 0xcd, 0x92, 0x87, 0x45, 0xc2 \ + } \ + } + +class Blob : public nsSupportsWeakReference, public nsWrapperCache { + public: + NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob) + NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID) + + using BlobPart = OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String; + + // This creates a Blob or a File based on the type of BlobImpl. + static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl); + + 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(nsIGlobalObject* aGlobal, + void* aMemoryBuffer, + uint64_t aLength, + const nsAString& aContentType); + + BlobImpl* Impl() const { return mImpl; } + + bool IsFile() const; + + const nsTArray>* GetSubBlobImpls() const; + + // This method returns null if this Blob is not a File; it returns + // the same object in case this Blob already implements the File interface; + // otherwise it returns a new File object with the same BlobImpl. + already_AddRefed ToFile(); + + // This method creates a new File object with the given name and the same + // BlobImpl. + already_AddRefed ToFile(const nsAString& aName, ErrorResult& aRv) const; + + already_AddRefed CreateSlice(uint64_t aStart, uint64_t aLength, + const nsAString& aContentType, + ErrorResult& aRv) const; + + void CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv) const; + + int64_t GetFileId() const; + + // A utility function that enforces the spec constraints on the type of a + // blob: no codepoints outside the ASCII range (otherwise type becomes empty) + // and lowercase ASCII only. We can't just use our existing nsContentUtils + // ASCII-related helpers because we need the "outside ASCII range" check, and + // we can't use NS_IsAscii because its definition of "ASCII" (chars all <= + // 0x7E) differs from the file API definition (which excludes control chars). + static void MakeValidBlobType(nsAString& aType); + + // WebIDL methods + nsIGlobalObject* GetParentObject() const { return mGlobal; } + + bool IsMemoryFile() const; + + // Blob constructor + static already_AddRefed Constructor( + const GlobalObject& aGlobal, const Optional>& aData, + const BlobPropertyBag& aBag, ErrorResult& aRv); + + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + uint64_t GetSize(ErrorResult& aRv); + + void GetType(nsAString& aType); + + void GetBlobImplType(nsAString& aBlobImplType); + + already_AddRefed Slice(const Optional& aStart, + const Optional& aEnd, + const Optional& aContentType, + ErrorResult& aRv); + + size_t GetAllocationSize() const; + + nsresult GetSendInfo(nsIInputStream** aBody, uint64_t* aContentLength, + nsACString& aContentType, nsACString& aCharset) const; + + already_AddRefed Stream(JSContext* aCx, + ErrorResult& aRv) const; + already_AddRefed Text(ErrorResult& aRv) const; + already_AddRefed ArrayBuffer(ErrorResult& aRv) const; + + protected: + // File constructor should never be used directly. Use Blob::Create instead. + Blob(nsIGlobalObject* aGlobal, BlobImpl* aImpl); + virtual ~Blob(); + + virtual bool HasFileInterface() const { return false; } + + already_AddRefed ConsumeBody(BodyConsumer::ConsumeType aConsumeType, + ErrorResult& aRv) const; + + // The member is the real backend implementation of this File/Blob. + // It's thread-safe and not CC-able and it's the only element that is moved + // between threads. + // Note: we should not store any other state in this class! + RefPtr mImpl; + + private: + nsCOMPtr mGlobal; +}; + +NS_DEFINE_STATIC_IID_ACCESSOR(Blob, NS_DOM_BLOB_IID) + +// Override BindingJSObjectMallocBytes for blobs to tell the JS GC how much +// memory is held live by the binding object. +size_t BindingJSObjectMallocBytes(Blob* aBlob); + +} // namespace dom +} // namespace mozilla + +inline nsISupports* ToSupports(mozilla::dom::Blob* aBlob) { + return static_cast(aBlob); +} + +#endif // mozilla_dom_Blob_h -- cgit v1.2.3