summaryrefslogtreecommitdiff
path: root/dom/media/eme/MediaKeyStatusMap.cpp
blob: d2dbe00bb8b2c5b0a5606b6556dc552f92f194ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/MediaKeyStatusMap.h"
#include "nsPIDOMWindow.h"
#include "mozilla/dom/UnionTypes.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/EMEUtils.h"

namespace mozilla {
namespace dom {

NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaKeyStatusMap)
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaKeyStatusMap)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeyStatusMap)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaKeyStatusMap, mParent)

MediaKeyStatusMap::MediaKeyStatusMap(nsPIDOMWindowInner* aParent)
  : mParent(aParent)
{
}

MediaKeyStatusMap::~MediaKeyStatusMap()
{
}

JSObject*
MediaKeyStatusMap::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
{
  return MediaKeyStatusMapBinding::Wrap(aCx, this, aGivenProto);
}

nsPIDOMWindowInner*
MediaKeyStatusMap::GetParentObject() const
{
  return mParent;
}

void
MediaKeyStatusMap::Get(JSContext* aCx,
                       const ArrayBufferViewOrArrayBuffer& aKey,
                       JS::MutableHandle<JS::Value> aOutValue,
                       ErrorResult& aOutRv) const
{
  ArrayData keyId = GetArrayBufferViewOrArrayBufferData(aKey);
  if (!keyId.IsValid()) {
    aOutValue.setUndefined();
    return;
  }
  for (const KeyStatus& status : mStatuses) {
    if (keyId == status.mKeyId) {
      bool ok = ToJSValue(aCx, status.mStatus, aOutValue);
      if (!ok) {
        aOutRv.NoteJSContextException(aCx);
      }
      return;
    }
  }
  aOutValue.setUndefined();
}

bool
MediaKeyStatusMap::Has(const ArrayBufferViewOrArrayBuffer& aKey) const
{
  ArrayData keyId = GetArrayBufferViewOrArrayBufferData(aKey);
  if (!keyId.IsValid()) {
    return false;
  }

  for (const KeyStatus& status : mStatuses) {
    if (keyId == status.mKeyId) {
      return true;
    }
  }

  return false;
}

uint32_t
MediaKeyStatusMap::GetIterableLength() const
{
  return mStatuses.Length();
}

TypedArrayCreator<ArrayBuffer>
MediaKeyStatusMap::GetKeyAtIndex(uint32_t aIndex) const
{
  MOZ_ASSERT(aIndex < GetIterableLength());
  return TypedArrayCreator<ArrayBuffer>(mStatuses[aIndex].mKeyId);
}

MediaKeyStatus
MediaKeyStatusMap::GetValueAtIndex(uint32_t aIndex) const
{
  MOZ_ASSERT(aIndex < GetIterableLength());
  return mStatuses[aIndex].mStatus;
}

uint32_t
MediaKeyStatusMap::Size() const
{
  return mStatuses.Length();
}

static MediaKeyStatus
ToMediaKeyStatus(GMPMediaKeyStatus aStatus) {
  switch (aStatus) {
    case kGMPUsable: return MediaKeyStatus::Usable;
    case kGMPExpired: return MediaKeyStatus::Expired;
    case kGMPOutputDownscaled: return MediaKeyStatus::Output_downscaled;
    case kGMPOutputRestricted: return MediaKeyStatus::Output_restricted;
    case kGMPInternalError: return MediaKeyStatus::Internal_error;
    case kGMPReleased: return MediaKeyStatus::Released;
    case kGMPStatusPending: return MediaKeyStatus::Status_pending;
    default: return MediaKeyStatus::Internal_error;
  }
}

void
MediaKeyStatusMap::Update(const nsTArray<CDMCaps::KeyStatus>& aKeys)
{
  mStatuses.Clear();
  for (const auto& key : aKeys) {
    mStatuses.InsertElementSorted(KeyStatus(key.mId, ToMediaKeyStatus(key.mStatus)));
  }
}

} // namespace dom
} // namespace mozilla