Bug 1798513 - Replace USVString with UTF8String in union types with Blob getter. r=nika

Depends on D165730

Differential Revision: https://phabricator.services.mozilla.com/D165731
This commit is contained in:
Jari Jalkanen 2023-01-11 08:33:47 +00:00
Родитель 72f77002b1
Коммит 64a6072483
8 изменённых файлов: 41 добавлений и 49 удалений

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

@ -25,7 +25,7 @@ struct BlobPropertyBag;
class BlobImpl; class BlobImpl;
class File; class File;
class GlobalObject; class GlobalObject;
class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString; class OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String;
class Promise; class Promise;
class ReadableStream; class ReadableStream;
@ -43,7 +43,7 @@ class Blob : public nsSupportsWeakReference, public nsWrapperCache {
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob) NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob)
NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID) NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID)
using BlobPart = OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString; using BlobPart = OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String;
// This creates a Blob or a File based on the type of BlobImpl. // This creates a Blob or a File based on the type of BlobImpl.
static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl); static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl);

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

@ -30,19 +30,26 @@ nsresult BlobSet::AppendVoidPtr(const void* aData, uint32_t aLength) {
return AppendBlobImpl(blobImpl); return AppendBlobImpl(blobImpl);
} }
nsresult BlobSet::AppendString(const nsAString& aString, bool nativeEOL) { nsresult BlobSet::AppendUTF8String(const nsACString& aUTF8String,
bool nativeEOL) {
nsCString utf8Str; nsCString utf8Str;
if (NS_WARN_IF(!AppendUTF16toUTF8(aString, utf8Str, mozilla::fallible))) { if (NS_WARN_IF(!utf8Str.Assign(aUTF8String, mozilla::fallible))) {
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
} }
if (nativeEOL) { if (nativeEOL) {
if (utf8Str.Contains('\r')) { if (utf8Str.Contains('\r')) {
utf8Str.ReplaceSubstring("\r\n", "\n"); if (NS_WARN_IF(
utf8Str.ReplaceSubstring("\r", "\n"); !utf8Str.ReplaceSubstring("\r\n", "\n", mozilla::fallible) ||
!utf8Str.ReplaceSubstring("\r", "\n", mozilla::fallible))) {
return NS_ERROR_OUT_OF_MEMORY;
}
} }
#ifdef XP_WIN #ifdef XP_WIN
utf8Str.ReplaceSubstring("\n", "\r\n"); if (NS_WARN_IF(
!utf8Str.ReplaceSubstring("\n", "\r\n", mozilla::fallible))) {
return NS_ERROR_OUT_OF_MEMORY;
}
#endif #endif
} }

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

@ -20,7 +20,8 @@ class BlobSet final {
public: public:
[[nodiscard]] nsresult AppendVoidPtr(const void* aData, uint32_t aLength); [[nodiscard]] nsresult AppendVoidPtr(const void* aData, uint32_t aLength);
[[nodiscard]] nsresult AppendString(const nsAString& aString, bool nativeEOL); [[nodiscard]] nsresult AppendUTF8String(const nsACString& aUTF8String,
bool nativeEOL);
[[nodiscard]] nsresult AppendBlobImpl(BlobImpl* aBlobImpl); [[nodiscard]] nsresult AppendBlobImpl(BlobImpl* aBlobImpl);

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

@ -203,8 +203,8 @@ void MultipartBlobImpl::InitializeBlob(const Sequence<Blob::BlobPart>& aData,
} }
} }
else if (data.IsUSVString()) { else if (data.IsUTF8String()) {
aRv = blobSet.AppendString(data.GetAsUSVString(), aNativeEOL); aRv = blobSet.AppendUTF8String(data.GetAsUTF8String(), aNativeEOL);
if (aRv.Failed()) { if (aRv.Failed()) {
return; return;
} }

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

@ -253,7 +253,7 @@ already_AddRefed<Promise> FileSystemWritableFileStream::Write(
aError.MightThrowJSException(); aError.MightThrowJSException();
ArrayBufferViewOrArrayBufferOrBlobOrUSVStringOrWriteParams data; ArrayBufferViewOrArrayBufferOrBlobOrUTF8StringOrWriteParams data;
if (!data.Init(aCx, aChunk)) { if (!data.Init(aCx, aChunk)) {
aError.StealExceptionFromJSContext(aCx); aError.StealExceptionFromJSContext(aCx);
return nullptr; return nullptr;
@ -353,7 +353,7 @@ JSObject* FileSystemWritableFileStream::WrapObject(
// WebIDL Interface // WebIDL Interface
already_AddRefed<Promise> FileSystemWritableFileStream::Write( already_AddRefed<Promise> FileSystemWritableFileStream::Write(
const ArrayBufferViewOrArrayBufferOrBlobOrUSVStringOrWriteParams& aData, const ArrayBufferViewOrArrayBufferOrBlobOrUTF8StringOrWriteParams& aData,
ErrorResult& aError) { ErrorResult& aError) {
// https://fs.spec.whatwg.org/#dom-filesystemwritablefilestream-write // https://fs.spec.whatwg.org/#dom-filesystemwritablefilestream-write
// Step 1. Let writer be the result of getting a writer for this. // Step 1. Let writer be the result of getting a writer for this.
@ -502,11 +502,11 @@ void FileSystemWritableFileStream::Write(const T& aData,
return Span{buffer.Data(), buffer.Length()}; return Span{buffer.Data(), buffer.Length()};
}(); }();
auto maybeBuffer = Buffer<char>::CopyFrom(AsChars(dataSpan)); nsCString dataBuffer;
QM_TRY(MOZ_TO_RESULT(maybeBuffer.isSome()), rejectAndReturn); QM_TRY(MOZ_TO_RESULT(dataBuffer.Assign(
AsChars(dataSpan).data(), dataSpan.Length(), mozilla::fallible)),
QM_TRY_INSPECT(const auto& written, rejectAndReturn);
WriteBuffer(maybeBuffer.extract(), aPosition), QM_TRY_INSPECT(const auto& written, WriteBuffer(dataBuffer, aPosition),
rejectAndReturn); rejectAndReturn);
LOG_VERBOSE(("WritableFileStream: Wrote %" PRId64, written)); LOG_VERBOSE(("WritableFileStream: Wrote %" PRId64, written));
@ -535,19 +535,10 @@ void FileSystemWritableFileStream::Write(const T& aData,
} }
// Step 3.4.8 Otherwise ... // Step 3.4.8 Otherwise ...
MOZ_ASSERT(aData.IsUSVString()); MOZ_ASSERT(aData.IsUTF8String());
uint32_t count; QM_TRY_INSPECT(const auto& written,
UniquePtr<char[]> string( WriteBuffer(aData.GetAsUTF8String(), aPosition),
ToNewUTF8String(aData.GetAsUSVString(), &count, fallible));
QM_TRY((MOZ_TO_RESULT(string.get()).mapErr([](const nsresult) {
return NS_ERROR_OUT_OF_MEMORY;
})),
rejectAndReturn);
Buffer<char> buffer(std::move(string), count);
QM_TRY_INSPECT(const auto& written, WriteBuffer(std::move(buffer), aPosition),
rejectAndReturn); rejectAndReturn);
LOG_VERBOSE(("WritableFileStream: Wrote %" PRId64, written)); LOG_VERBOSE(("WritableFileStream: Wrote %" PRId64, written));
@ -599,19 +590,17 @@ void FileSystemWritableFileStream::Truncate(uint64_t aSize,
} }
Result<uint64_t, nsresult> FileSystemWritableFileStream::WriteBuffer( Result<uint64_t, nsresult> FileSystemWritableFileStream::WriteBuffer(
Buffer<char>&& aBuffer, const Maybe<uint64_t> aPosition) { const nsACString& aBuffer, const Maybe<uint64_t> aPosition) {
MOZ_ASSERT(!mClosed); MOZ_ASSERT(!mClosed);
Buffer<char> buffer = std::move(aBuffer); const auto checkedLength = CheckedInt<PRInt32>(aBuffer.Length());
const auto checkedLength = CheckedInt<PRInt32>(buffer.Length());
QM_TRY(MOZ_TO_RESULT(checkedLength.isValid())); QM_TRY(MOZ_TO_RESULT(checkedLength.isValid()));
if (aPosition) { if (aPosition) {
QM_TRY(SeekPosition(*aPosition)); QM_TRY(SeekPosition(*aPosition));
} }
return PR_Write(mFileDesc, buffer.Elements(), checkedLength.value()); return PR_Write(mFileDesc, aBuffer.BeginReading(), checkedLength.value());
} }
Result<uint64_t, nsresult> FileSystemWritableFileStream::WriteStream( Result<uint64_t, nsresult> FileSystemWritableFileStream::WriteStream(
@ -619,15 +608,9 @@ Result<uint64_t, nsresult> FileSystemWritableFileStream::WriteStream(
MOZ_ASSERT(aStream); MOZ_ASSERT(aStream);
MOZ_ASSERT(!mClosed); MOZ_ASSERT(!mClosed);
void* rawBuffer = nullptr; nsCString rawBuffer;
uint64_t length; QM_TRY(MOZ_TO_RESULT(NS_ReadInputStreamToString(aStream, rawBuffer, -1)));
QM_TRY(MOZ_TO_RESULT( QM_TRY_RETURN(WriteBuffer(rawBuffer, aPosition));
NS_ReadInputStreamToBuffer(aStream, &rawBuffer, -1, &length)));
Buffer<char> buffer(UniquePtr<char[]>(reinterpret_cast<char*>(rawBuffer)),
length);
QM_TRY_RETURN(WriteBuffer(std::move(buffer), aPosition));
} }
Result<Ok, nsresult> FileSystemWritableFileStream::SeekPosition( Result<Ok, nsresult> FileSystemWritableFileStream::SeekPosition(

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

@ -20,10 +20,11 @@ class ErrorResult;
namespace dom { namespace dom {
class ArrayBufferViewOrArrayBufferOrBlobOrUSVStringOrWriteParams; class ArrayBufferViewOrArrayBufferOrBlobOrUTF8StringOrWriteParams;
class Blob; class Blob;
class FileSystemManager; class FileSystemManager;
class FileSystemWritableFileStreamChild; class FileSystemWritableFileStreamChild;
class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString;
class Promise; class Promise;
class FileSystemWritableFileStream final : public WritableStream { class FileSystemWritableFileStream final : public WritableStream {
@ -55,7 +56,7 @@ class FileSystemWritableFileStream final : public WritableStream {
// WebIDL Interface // WebIDL Interface
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Write( MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Write(
const ArrayBufferViewOrArrayBufferOrBlobOrUSVStringOrWriteParams& aData, const ArrayBufferViewOrArrayBufferOrBlobOrUTF8StringOrWriteParams& aData,
ErrorResult& aError); ErrorResult& aError);
MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Seek(uint64_t aPosition, MOZ_CAN_RUN_SCRIPT already_AddRefed<Promise> Seek(uint64_t aPosition,
@ -81,7 +82,7 @@ class FileSystemWritableFileStream final : public WritableStream {
void Truncate(uint64_t aSize, RefPtr<Promise> aPromise); void Truncate(uint64_t aSize, RefPtr<Promise> aPromise);
Result<uint64_t, nsresult> WriteBuffer(Buffer<char>&& aBuffer, Result<uint64_t, nsresult> WriteBuffer(const nsACString& aBuffer,
const Maybe<uint64_t> aPosition); const Maybe<uint64_t> aPosition);
Result<uint64_t, nsresult> WriteStream(nsCOMPtr<nsIInputStream> aStream, Result<uint64_t, nsresult> WriteStream(nsCOMPtr<nsIInputStream> aStream,

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

@ -10,7 +10,7 @@
* liability, trademark and document use rules apply. * liability, trademark and document use rules apply.
*/ */
typedef (BufferSource or Blob or USVString) BlobPart; typedef (BufferSource or Blob or UTF8String) BlobPart;
[Exposed=(Window,Worker)] [Exposed=(Window,Worker)]
interface Blob { interface Blob {

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

@ -14,10 +14,10 @@ dictionary WriteParams {
required WriteCommandType type; required WriteCommandType type;
unsigned long long? size; unsigned long long? size;
unsigned long long? position; unsigned long long? position;
(BufferSource or Blob or USVString)? data; (BufferSource or Blob or UTF8String)? data;
}; };
typedef (BufferSource or Blob or USVString or WriteParams) FileSystemWriteChunkType; typedef (BufferSource or Blob or UTF8String or WriteParams) FileSystemWriteChunkType;
[Exposed=(Window,Worker), SecureContext, Pref="dom.fs.enabled"] [Exposed=(Window,Worker), SecureContext, Pref="dom.fs.enabled"]
interface FileSystemWritableFileStream : WritableStream { interface FileSystemWritableFileStream : WritableStream {