summaryrefslogtreecommitdiff
path: root/dom/media/eme/mediadrm/MediaDrmCDMCallbackProxy.cpp
blob: 300093b844078312fb585b36dc0a3bc9a0dc7145 (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
136
137
138
139
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "MediaDrmCDMCallbackProxy.h"
#include "mozilla/CDMProxy.h"
#include "nsString.h"
#include "mozilla/dom/MediaKeys.h"
#include "mozilla/dom/MediaKeySession.h"
#include "mozIGeckoMediaPluginService.h"
#include "nsContentCID.h"
#include "nsServiceManagerUtils.h"
#include "MainThreadUtils.h"
#include "mozilla/EMEUtils.h"

namespace mozilla {

MediaDrmCDMCallbackProxy::MediaDrmCDMCallbackProxy(CDMProxy* aProxy)
  : mProxy(aProxy)
{

}

void
MediaDrmCDMCallbackProxy::SetSessionId(uint32_t aToken,
                                       const nsCString& aSessionId)
{
  MOZ_ASSERT(NS_IsMainThread());
  mProxy->OnSetSessionId(aToken, NS_ConvertUTF8toUTF16(aSessionId));
}

void
MediaDrmCDMCallbackProxy::ResolveLoadSessionPromise(uint32_t aPromiseId,
                                                    bool aSuccess)
{
  MOZ_ASSERT(NS_IsMainThread());
  mProxy->OnResolveLoadSessionPromise(aPromiseId, aSuccess);
}

void
MediaDrmCDMCallbackProxy::ResolvePromise(uint32_t aPromiseId)
{
  // Note: CDMProxy proxies this from non-main threads to main thread.
  mProxy->ResolvePromise(aPromiseId);
}

void
MediaDrmCDMCallbackProxy::RejectPromise(uint32_t aPromiseId,
                                        nsresult aException,
                                        const nsCString& aMessage)
{
  MOZ_ASSERT(NS_IsMainThread());
  mProxy->OnRejectPromise(aPromiseId, aException, aMessage);
}

void
MediaDrmCDMCallbackProxy::SessionMessage(const nsCString& aSessionId,
                                         dom::MediaKeyMessageType aMessageType,
                                         const nsTArray<uint8_t>& aMessage)
{
  MOZ_ASSERT(NS_IsMainThread());
  // For removing constness
  nsTArray<uint8_t> message(aMessage);
  mProxy->OnSessionMessage(NS_ConvertUTF8toUTF16(aSessionId), aMessageType, message);
}

void
MediaDrmCDMCallbackProxy::ExpirationChange(const nsCString& aSessionId,
                                           UnixTime aExpiryTime)
{
  MOZ_ASSERT(NS_IsMainThread());
  mProxy->OnExpirationChange(NS_ConvertUTF8toUTF16(aSessionId), aExpiryTime);
}

void
MediaDrmCDMCallbackProxy::SessionClosed(const nsCString& aSessionId)
{
  MOZ_ASSERT(NS_IsMainThread());
  bool keyStatusesChange = false;
  {
    CDMCaps::AutoLock caps(mProxy->Capabilites());
    keyStatusesChange = caps.RemoveKeysForSession(NS_ConvertUTF8toUTF16(aSessionId));
  }
  if (keyStatusesChange) {
    mProxy->OnKeyStatusesChange(NS_ConvertUTF8toUTF16(aSessionId));
  }
  mProxy->OnSessionClosed(NS_ConvertUTF8toUTF16(aSessionId));
}

void
MediaDrmCDMCallbackProxy::SessionError(const nsCString& aSessionId,
                                       nsresult aException,
                                       uint32_t aSystemCode,
                                       const nsCString& aMessage)
{
  MOZ_ASSERT(NS_IsMainThread());
  mProxy->OnSessionError(NS_ConvertUTF8toUTF16(aSessionId),
                         aException,
                         aSystemCode,
                         NS_ConvertUTF8toUTF16(aMessage));
}

void
MediaDrmCDMCallbackProxy::BatchedKeyStatusChanged(const nsCString& aSessionId,
                                                  const nsTArray<CDMKeyInfo>& aKeyInfos)
{
  MOZ_ASSERT(NS_IsMainThread());
  BatchedKeyStatusChangedInternal(aSessionId, aKeyInfos);
}

void
MediaDrmCDMCallbackProxy::BatchedKeyStatusChangedInternal(const nsCString& aSessionId,
                                                          const nsTArray<CDMKeyInfo>& aKeyInfos)
{
  bool keyStatusesChange = false;
  {
    CDMCaps::AutoLock caps(mProxy->Capabilites());
    for (size_t i = 0; i < aKeyInfos.Length(); i++) {
      keyStatusesChange |=
        caps.SetKeyStatus(aKeyInfos[i].mKeyId,
                          NS_ConvertUTF8toUTF16(aSessionId),
                          aKeyInfos[i].mStatus);
    }
  }
  if (keyStatusesChange) {
    mProxy->OnKeyStatusesChange(NS_ConvertUTF8toUTF16(aSessionId));
  }
}

void
MediaDrmCDMCallbackProxy::Decrypted(uint32_t aId,
                                    DecryptStatus aResult,
                                    const nsTArray<uint8_t>& aDecryptedData)
{
  MOZ_ASSERT_UNREACHABLE("Fennec could not handle decrypted event");
}

} // namespace mozilla