MLSTypeUtils.cpp (3174B)
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/. */ 6 7 #include "mozilla/dom/MLSBinding.h" 8 #include "mozilla/dom/TypedArray.h" 9 #include "nsTArray.h" 10 11 namespace mozilla::dom { 12 13 nsTArray<uint8_t> ExtractMLSBytesOrUint8ArrayWithUnknownType( 14 const MLSBytesOrUint8Array& aArgument, ErrorResult& aRv) { 15 // The data can be in a Uint8Array or MLSBytes. 16 const Uint8Array* array = nullptr; 17 18 if (aArgument.IsMLSBytes()) { 19 array = &aArgument.GetAsMLSBytes().mContent; 20 } else { 21 MOZ_ASSERT(aArgument.IsUint8Array()); 22 array = &aArgument.GetAsUint8Array(); 23 } 24 25 // Append the data from the Uint8Array to the output array 26 nsTArray<uint8_t> bytes; 27 if (array && !array->AppendDataTo(bytes)) { 28 aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 29 return nsTArray<uint8_t>(); 30 } 31 return bytes; 32 } 33 34 nsTArray<uint8_t> ExtractMLSBytesOrUint8Array( 35 MLSObjectType aExpectedType, const MLSBytesOrUint8Array& aArgument, 36 ErrorResult& aRv) { 37 // The data can be in a Uint8Array or MLSBytes. 38 const Uint8Array* array = nullptr; 39 40 if (aArgument.IsMLSBytes()) { 41 // Check if the type of MLSBytes matches the expected type 42 if (aArgument.GetAsMLSBytes().mType != aExpectedType) { 43 aRv.ThrowTypeError("Input data has an invalid type"); 44 return nsTArray<uint8_t>(); 45 } 46 array = &aArgument.GetAsMLSBytes().mContent; 47 } else { 48 MOZ_ASSERT(aArgument.IsUint8Array()); 49 array = &aArgument.GetAsUint8Array(); 50 } 51 52 // Append the data from the Uint8Array to the output array 53 nsTArray<uint8_t> bytes; 54 if (array && !array->AppendDataTo(bytes)) { 55 aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 56 return nsTArray<uint8_t>(); 57 } 58 return bytes; 59 } 60 61 nsTArray<uint8_t> ExtractMLSBytesOrUint8ArrayOrUTF8String( 62 MLSObjectType aExpectedType, 63 const MLSBytesOrUint8ArrayOrUTF8String& aArgument, ErrorResult& aRv) { 64 // The data can be in a Uint8Array, MLSBytes, or UTF8String. 65 const Uint8Array* array = nullptr; 66 nsTArray<uint8_t> bytes; 67 if (aArgument.IsMLSBytes()) { 68 // Check if the type of MLSBytes matches the expected type 69 if (aArgument.GetAsMLSBytes().mType != aExpectedType) { 70 aRv.ThrowTypeError("Input data has an invalid type"); 71 return nsTArray<uint8_t>(); 72 } 73 array = &aArgument.GetAsMLSBytes().mContent; 74 } else if (aArgument.IsUint8Array()) { 75 array = &aArgument.GetAsUint8Array(); 76 } else { 77 MOZ_ASSERT(aArgument.IsUTF8String()); 78 const nsACString& string = aArgument.GetAsUTF8String(); 79 if (!bytes.AppendElements(string.BeginReading(), string.Length(), 80 fallible)) { 81 aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 82 return nsTArray<uint8_t>(); 83 } 84 return bytes; 85 } 86 87 // Append the data from the Uint8Array to the output array 88 if (array && !array->AppendDataTo(bytes)) { 89 aRv.Throw(NS_ERROR_OUT_OF_MEMORY); 90 return nsTArray<uint8_t>(); 91 } 92 return bytes; 93 } 94 95 } // namespace mozilla::dom