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-10 19:08:22 +00:00
Родитель 9d7785625f
Коммит e4595e9483
8 изменённых файлов: 41 добавлений и 49 удалений

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

@ -25,7 +25,7 @@ struct BlobPropertyBag;
class BlobImpl;
class File;
class GlobalObject;
class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString;
class OwningArrayBufferViewOrArrayBufferOrBlobOrUTF8String;
class Promise;
class ReadableStream;
@ -43,7 +43,7 @@ class Blob : public nsSupportsWeakReference, public nsWrapperCache {
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Blob)
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.
static Blob* Create(nsIGlobalObject* aGlobal, BlobImpl* aImpl);

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

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

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

@ -20,7 +20,8 @@ class BlobSet final {
public:
[[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);

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

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

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

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

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

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

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

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

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

@ -14,10 +14,10 @@ dictionary WriteParams {
required WriteCommandType type;
unsigned long long? size;
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"]
interface FileSystemWritableFileStream : WritableStream {