/src/mozilla-central/dom/push/PushUtil.cpp
Line | Count | Source (jump to first uncovered line) |
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/PushUtil.h" |
8 | | |
9 | | namespace mozilla { |
10 | | namespace dom { |
11 | | |
12 | | /* static */ bool |
13 | | PushUtil::CopyArrayBufferToArray(const ArrayBuffer& aBuffer, |
14 | | nsTArray<uint8_t>& aArray) |
15 | 0 | { |
16 | 0 | MOZ_ASSERT(aArray.IsEmpty()); |
17 | 0 | aBuffer.ComputeLengthAndData(); |
18 | 0 | return aArray.SetCapacity(aBuffer.Length(), fallible) && |
19 | 0 | aArray.InsertElementsAt(0, aBuffer.Data(), aBuffer.Length(), fallible); |
20 | 0 | } |
21 | | |
22 | | /* static */ bool |
23 | | PushUtil::CopyArrayBufferViewToArray(const ArrayBufferView& aView, |
24 | | nsTArray<uint8_t>& aArray) |
25 | 0 | { |
26 | 0 | MOZ_ASSERT(aArray.IsEmpty()); |
27 | 0 | aView.ComputeLengthAndData(); |
28 | 0 | return aArray.SetCapacity(aView.Length(), fallible) && |
29 | 0 | aArray.InsertElementsAt(0, aView.Data(), aView.Length(), fallible); |
30 | 0 | } |
31 | | |
32 | | /* static */ bool |
33 | | PushUtil::CopyBufferSourceToArray( |
34 | | const OwningArrayBufferViewOrArrayBuffer& aSource, nsTArray<uint8_t>& aArray) |
35 | 0 | { |
36 | 0 | if (aSource.IsArrayBuffer()) { |
37 | 0 | return CopyArrayBufferToArray(aSource.GetAsArrayBuffer(), aArray); |
38 | 0 | } |
39 | 0 | if (aSource.IsArrayBufferView()) { |
40 | 0 | return CopyArrayBufferViewToArray(aSource.GetAsArrayBufferView(), aArray); |
41 | 0 | } |
42 | 0 | MOZ_CRASH("Uninitialized union: expected buffer or view"); |
43 | 0 | } |
44 | | |
45 | | /* static */ void |
46 | | PushUtil::CopyArrayToArrayBuffer(JSContext* aCx, |
47 | | const nsTArray<uint8_t>& aArray, |
48 | | JS::MutableHandle<JSObject*> aValue, |
49 | | ErrorResult& aRv) |
50 | 0 | { |
51 | 0 | if (aArray.IsEmpty()) { |
52 | 0 | aValue.set(nullptr); |
53 | 0 | return; |
54 | 0 | } |
55 | 0 | JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx, |
56 | 0 | aArray.Length(), |
57 | 0 | aArray.Elements())); |
58 | 0 | if (NS_WARN_IF(!buffer)) { |
59 | 0 | aRv.Throw(NS_ERROR_OUT_OF_MEMORY); |
60 | 0 | return; |
61 | 0 | } |
62 | 0 | aValue.set(buffer); |
63 | 0 | } |
64 | | |
65 | | } // namespace dom |
66 | | } // namespace mozilla |