Bug 1643721: part 11) Privatize member of `RangeContextSerializer`. r=masayuki
[gecko.git] / dom / file / BlobSet.cpp
blob76f0ce19988d06836969061b9d8765dcd8cebc03
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"
13 namespace mozilla {
14 namespace 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, EmptyString());
30 return AppendBlobImpl(blobImpl);
33 nsresult BlobSet::AppendString(const nsAString& aString, bool nativeEOL) {
34 nsCString utf8Str;
35 if (NS_WARN_IF(!AppendUTF16toUTF8(aString, utf8Str, mozilla::fallible))) {
36 return NS_ERROR_OUT_OF_MEMORY;
39 if (nativeEOL) {
40 if (utf8Str.Contains('\r')) {
41 utf8Str.ReplaceSubstring("\r\n", "\n");
42 utf8Str.ReplaceSubstring("\r", "\n");
44 #ifdef XP_WIN
45 utf8Str.ReplaceSubstring("\n", "\r\n");
46 #endif
49 RefPtr<StringBlobImpl> blobImpl =
50 StringBlobImpl::Create(utf8Str, EmptyString());
51 return AppendBlobImpl(blobImpl);
54 nsresult BlobSet::AppendBlobImpl(BlobImpl* aBlobImpl) {
55 NS_ENSURE_ARG_POINTER(aBlobImpl);
57 // If aBlobImpl is a MultipartBlobImpl, let's append the sub-blobImpls
58 // instead.
59 const nsTArray<RefPtr<BlobImpl>>* subBlobs = aBlobImpl->GetSubBlobImpls();
60 if (subBlobs) {
61 for (BlobImpl* subBlob : *subBlobs) {
62 nsresult rv = AppendBlobImpl(subBlob);
63 if (NS_WARN_IF(NS_FAILED(rv))) {
64 return rv;
68 return NS_OK;
71 if (NS_WARN_IF(!mBlobImpls.AppendElement(aBlobImpl, fallible))) {
72 return NS_ERROR_OUT_OF_MEMORY;
74 return NS_OK;
77 } // namespace dom
78 } // namespace mozilla