Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "mozilla/dom/PushUtil.h"
6 :
7 : namespace mozilla {
8 : namespace dom {
9 :
10 : /* static */ bool
11 0 : PushUtil::CopyArrayBufferToArray(const ArrayBuffer& aBuffer,
12 : nsTArray<uint8_t>& aArray)
13 : {
14 0 : MOZ_ASSERT(aArray.IsEmpty());
15 0 : aBuffer.ComputeLengthAndData();
16 0 : return aArray.SetCapacity(aBuffer.Length(), fallible) &&
17 0 : aArray.InsertElementsAt(0, aBuffer.Data(), aBuffer.Length(), fallible);
18 : }
19 :
20 : /* static */ bool
21 0 : PushUtil::CopyArrayBufferViewToArray(const ArrayBufferView& aView,
22 : nsTArray<uint8_t>& aArray)
23 : {
24 0 : MOZ_ASSERT(aArray.IsEmpty());
25 0 : aView.ComputeLengthAndData();
26 0 : return aArray.SetCapacity(aView.Length(), fallible) &&
27 0 : aArray.InsertElementsAt(0, aView.Data(), aView.Length(), fallible);
28 : }
29 :
30 : /* static */ bool
31 0 : PushUtil::CopyBufferSourceToArray(
32 : const OwningArrayBufferViewOrArrayBuffer& aSource, nsTArray<uint8_t>& aArray)
33 : {
34 0 : if (aSource.IsArrayBuffer()) {
35 0 : return CopyArrayBufferToArray(aSource.GetAsArrayBuffer(), aArray);
36 : }
37 0 : if (aSource.IsArrayBufferView()) {
38 0 : return CopyArrayBufferViewToArray(aSource.GetAsArrayBufferView(), aArray);
39 : }
40 0 : MOZ_CRASH("Uninitialized union: expected buffer or view");
41 : }
42 :
43 : /* static */ void
44 0 : PushUtil::CopyArrayToArrayBuffer(JSContext* aCx,
45 : const nsTArray<uint8_t>& aArray,
46 : JS::MutableHandle<JSObject*> aValue,
47 : ErrorResult& aRv)
48 : {
49 0 : if (aArray.IsEmpty()) {
50 0 : aValue.set(nullptr);
51 0 : return;
52 : }
53 0 : JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx,
54 0 : aArray.Length(),
55 0 : aArray.Elements()));
56 0 : if (NS_WARN_IF(!buffer)) {
57 0 : aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
58 0 : return;
59 : }
60 0 : aValue.set(buffer);
61 : }
62 :
63 : } // namespace dom
64 : } // namespace mozilla
|