Bug 1843230 - Remove IsWin8OrLater checks from dom/geolocation/ r=emk
[gecko.git] / dom / file / BlobSet.cpp
blob6ae177429685f179cf5c460658702f4a22e4da28
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::AppendUTF8String(const nsACString& aUTF8String,
34 bool nativeEOL) {
35 nsCString utf8Str;
36 if (NS_WARN_IF(!utf8Str.Assign(aUTF8String, mozilla::fallible))) {
37 return NS_ERROR_OUT_OF_MEMORY;
40 if (nativeEOL) {
41 if (utf8Str.Contains('\r')) {
42 if (NS_WARN_IF(
43 !utf8Str.ReplaceSubstring("\r\n", "\n", mozilla::fallible) ||
44 !utf8Str.ReplaceSubstring("\r", "\n", mozilla::fallible))) {
45 return NS_ERROR_OUT_OF_MEMORY;
48 #ifdef XP_WIN
49 if (NS_WARN_IF(
50 !utf8Str.ReplaceSubstring("\n", "\r\n", mozilla::fallible))) {
51 return NS_ERROR_OUT_OF_MEMORY;
53 #endif
56 RefPtr<StringBlobImpl> blobImpl = StringBlobImpl::Create(utf8Str, u""_ns);
57 return AppendBlobImpl(blobImpl);
60 nsresult BlobSet::AppendBlobImpl(BlobImpl* aBlobImpl) {
61 NS_ENSURE_ARG_POINTER(aBlobImpl);
63 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
64 // instead.
65 const nsTArray<RefPtr<BlobImpl>>* subBlobs = aBlobImpl->GetSubBlobImpls();
66 if (subBlobs) {
67 for (BlobImpl* subBlob : *subBlobs) {
68 nsresult rv = AppendBlobImpl(subBlob);
69 if (NS_WARN_IF(NS_FAILED(rv))) {
70 return rv;
74 return NS_OK;
77 if (NS_WARN_IF(!mBlobImpls.AppendElement(aBlobImpl, fallible))) {
78 return NS_ERROR_OUT_OF_MEMORY;
80 return NS_OK;
83 } // namespace mozilla::dom