Backed out changeset b09d48d2b473 (bug 1655101) for causing mochitest webgl failures...
[gecko.git] / dom / file / BlobSet.cpp
blob8a688d05dfec055126542c5cde5b15f0bdcf13c8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/BlobSet.h"
8 #include "mozilla/CheckedInt.h"
9 #include "mozilla/dom/File.h"
10 #include "MemoryBlobImpl.h"
11 #include "MultipartBlobImpl.h"
12 #include "StringBlobImpl.h"
14 namespace mozilla::dom {
16 nsresult BlobSet::AppendVoidPtr(const void* aData, uint32_t aLength) {
17 NS_ENSURE_ARG_POINTER(aData);
18 if (!aLength) {
19 return NS_OK;
22 void* data = malloc(aLength);
23 if (!data) {
24 return NS_ERROR_OUT_OF_MEMORY;
27 memcpy((char*)data, aData, aLength);
29 RefPtr<BlobImpl> blobImpl = new MemoryBlobImpl(data, aLength, u""_ns);
30 return AppendBlobImpl(blobImpl);
33 nsresult BlobSet::AppendVector(Vector<uint8_t>&& aData) {
34 size_t length = aData.length();
35 RefPtr<BlobImpl> blobImpl =
36 new MemoryBlobImpl(aData.extractOrCopyRawBuffer(), length, u""_ns);
37 return AppendBlobImpl(blobImpl);
40 nsresult BlobSet::AppendUTF8String(const nsACString& aUTF8String,
41 bool nativeEOL) {
42 nsCString utf8Str;
43 if (NS_WARN_IF(!utf8Str.Assign(aUTF8String, mozilla::fallible))) {
44 return NS_ERROR_OUT_OF_MEMORY;
47 if (nativeEOL) {
48 if (utf8Str.Contains('\r')) {
49 if (NS_WARN_IF(
50 !utf8Str.ReplaceSubstring("\r\n", "\n", mozilla::fallible) ||
51 !utf8Str.ReplaceSubstring("\r", "\n", mozilla::fallible))) {
52 return NS_ERROR_OUT_OF_MEMORY;
55 #ifdef XP_WIN
56 if (NS_WARN_IF(
57 !utf8Str.ReplaceSubstring("\n", "\r\n", mozilla::fallible))) {
58 return NS_ERROR_OUT_OF_MEMORY;
60 #endif
63 RefPtr<StringBlobImpl> blobImpl = StringBlobImpl::Create(utf8Str, u""_ns);
64 return AppendBlobImpl(blobImpl);
67 nsresult BlobSet::AppendBlobImpl(BlobImpl* aBlobImpl) {
68 NS_ENSURE_ARG_POINTER(aBlobImpl);
70 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
71 // instead.
72 const nsTArray<RefPtr<BlobImpl>>* subBlobs = aBlobImpl->GetSubBlobImpls();
73 if (subBlobs) {
74 for (BlobImpl* subBlob : *subBlobs) {
75 nsresult rv = AppendBlobImpl(subBlob);
76 if (NS_WARN_IF(NS_FAILED(rv))) {
77 return rv;
81 return NS_OK;
84 if (NS_WARN_IF(!mBlobImpls.AppendElement(aBlobImpl, fallible))) {
85 return NS_ERROR_OUT_OF_MEMORY;
87 return NS_OK;
90 } // namespace mozilla::dom