Line data Source code
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "mozilla/dom/MediaKeyStatusMap.h"
8 : #include "nsPIDOMWindow.h"
9 : #include "mozilla/dom/UnionTypes.h"
10 : #include "mozilla/dom/ToJSValue.h"
11 : #include "mozilla/EMEUtils.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaKeyStatusMap)
17 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaKeyStatusMap)
18 0 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeyStatusMap)
19 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
20 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
21 0 : NS_INTERFACE_MAP_END
22 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaKeyStatusMap, mParent)
23 :
24 0 : MediaKeyStatusMap::MediaKeyStatusMap(nsPIDOMWindowInner* aParent)
25 0 : : mParent(aParent)
26 : {
27 0 : }
28 :
29 0 : MediaKeyStatusMap::~MediaKeyStatusMap()
30 : {
31 0 : }
32 :
33 : JSObject*
34 0 : MediaKeyStatusMap::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
35 : {
36 0 : return MediaKeyStatusMapBinding::Wrap(aCx, this, aGivenProto);
37 : }
38 :
39 : nsPIDOMWindowInner*
40 0 : MediaKeyStatusMap::GetParentObject() const
41 : {
42 0 : return mParent;
43 : }
44 :
45 : void
46 0 : MediaKeyStatusMap::Get(JSContext* aCx,
47 : const ArrayBufferViewOrArrayBuffer& aKey,
48 : JS::MutableHandle<JS::Value> aOutValue,
49 : ErrorResult& aOutRv) const
50 : {
51 0 : ArrayData keyId = GetArrayBufferViewOrArrayBufferData(aKey);
52 0 : if (!keyId.IsValid()) {
53 0 : aOutValue.setUndefined();
54 0 : return;
55 : }
56 0 : for (const KeyStatus& status : mStatuses) {
57 0 : if (keyId == status.mKeyId) {
58 0 : bool ok = ToJSValue(aCx, status.mStatus, aOutValue);
59 0 : if (!ok) {
60 0 : aOutRv.NoteJSContextException(aCx);
61 : }
62 0 : return;
63 : }
64 : }
65 0 : aOutValue.setUndefined();
66 : }
67 :
68 : bool
69 0 : MediaKeyStatusMap::Has(const ArrayBufferViewOrArrayBuffer& aKey) const
70 : {
71 0 : ArrayData keyId = GetArrayBufferViewOrArrayBufferData(aKey);
72 0 : if (!keyId.IsValid()) {
73 0 : return false;
74 : }
75 :
76 0 : for (const KeyStatus& status : mStatuses) {
77 0 : if (keyId == status.mKeyId) {
78 0 : return true;
79 : }
80 : }
81 :
82 0 : return false;
83 : }
84 :
85 : uint32_t
86 0 : MediaKeyStatusMap::GetIterableLength() const
87 : {
88 0 : return mStatuses.Length();
89 : }
90 :
91 : TypedArrayCreator<ArrayBuffer>
92 0 : MediaKeyStatusMap::GetKeyAtIndex(uint32_t aIndex) const
93 : {
94 0 : MOZ_ASSERT(aIndex < GetIterableLength());
95 0 : return TypedArrayCreator<ArrayBuffer>(mStatuses[aIndex].mKeyId);
96 : }
97 :
98 : nsString
99 0 : MediaKeyStatusMap::GetKeyIDAsHexString(uint32_t aIndex) const
100 : {
101 0 : MOZ_ASSERT(aIndex < GetIterableLength());
102 0 : return NS_ConvertUTF8toUTF16(ToHexString(mStatuses[aIndex].mKeyId));
103 : }
104 :
105 : MediaKeyStatus
106 0 : MediaKeyStatusMap::GetValueAtIndex(uint32_t aIndex) const
107 : {
108 0 : MOZ_ASSERT(aIndex < GetIterableLength());
109 0 : return mStatuses[aIndex].mStatus;
110 : }
111 :
112 : uint32_t
113 0 : MediaKeyStatusMap::Size() const
114 : {
115 0 : return mStatuses.Length();
116 : }
117 :
118 : void
119 0 : MediaKeyStatusMap::Update(const nsTArray<CDMCaps::KeyStatus>& aKeys)
120 : {
121 0 : mStatuses.Clear();
122 0 : for (const auto& key : aKeys) {
123 0 : mStatuses.InsertElementSorted(KeyStatus(key.mId, key.mStatus));
124 : }
125 0 : }
126 :
127 : } // namespace dom
128 : } // namespace mozilla
|