MediaDrmCDMCallbackProxy.cpp (4453B)
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 "MediaDrmCDMCallbackProxy.h" 8 9 #include "MainThreadUtils.h" 10 #include "MediaDrmCDMProxy.h" 11 #include "mozilla/EMEUtils.h" 12 #include "mozilla/dom/MediaKeySession.h" 13 #include "mozilla/dom/MediaKeys.h" 14 #include "nsServiceManagerUtils.h" 15 #include "nsString.h" 16 17 namespace mozilla { 18 19 MediaDrmCDMCallbackProxy::MediaDrmCDMCallbackProxy(MediaDrmCDMProxy* aProxy) 20 : mProxy(aProxy) {} 21 22 MediaDrmCDMCallbackProxy::~MediaDrmCDMCallbackProxy() = default; 23 24 void MediaDrmCDMCallbackProxy::SetSessionId(uint32_t aToken, 25 const nsCString& aSessionId) { 26 MOZ_ASSERT(NS_IsMainThread()); 27 mProxy->OnSetSessionId(aToken, NS_ConvertUTF8toUTF16(aSessionId)); 28 } 29 30 void MediaDrmCDMCallbackProxy::ResolveLoadSessionPromise(uint32_t aPromiseId, 31 bool aSuccess) { 32 MOZ_ASSERT(NS_IsMainThread()); 33 mProxy->OnResolveLoadSessionPromise(aPromiseId, aSuccess); 34 } 35 36 void MediaDrmCDMCallbackProxy::ResolvePromise(uint32_t aPromiseId) { 37 // Note: CDMProxy proxies this from non-main threads to main thread. 38 mProxy->ResolvePromise(aPromiseId); 39 } 40 41 void MediaDrmCDMCallbackProxy::RejectPromise(uint32_t aPromiseId, 42 ErrorResult&& aException, 43 const nsCString& aMessage) { 44 MOZ_ASSERT(NS_IsMainThread()); 45 mProxy->OnRejectPromise(aPromiseId, std::move(aException), aMessage); 46 } 47 48 void MediaDrmCDMCallbackProxy::SessionMessage( 49 const nsCString& aSessionId, dom::MediaKeyMessageType aMessageType, 50 const nsTArray<uint8_t>& aMessage) { 51 MOZ_ASSERT(NS_IsMainThread()); 52 // For removing constness 53 nsTArray<uint8_t> message(aMessage.Clone()); 54 mProxy->OnSessionMessage(NS_ConvertUTF8toUTF16(aSessionId), aMessageType, 55 message); 56 } 57 58 void MediaDrmCDMCallbackProxy::ExpirationChange(const nsCString& aSessionId, 59 UnixTime aExpiryTime) { 60 MOZ_ASSERT(NS_IsMainThread()); 61 mProxy->OnExpirationChange(NS_ConvertUTF8toUTF16(aSessionId), aExpiryTime); 62 } 63 64 void MediaDrmCDMCallbackProxy::SessionClosed(const nsCString& aSessionId) { 65 MOZ_ASSERT(NS_IsMainThread()); 66 bool keyStatusesChange = false; 67 { 68 auto caps = mProxy->Capabilites().Lock(); 69 keyStatusesChange = 70 caps->RemoveKeysForSession(NS_ConvertUTF8toUTF16(aSessionId)); 71 } 72 if (keyStatusesChange) { 73 mProxy->OnKeyStatusesChange(NS_ConvertUTF8toUTF16(aSessionId)); 74 } 75 mProxy->OnSessionClosed( 76 NS_ConvertUTF8toUTF16(aSessionId), 77 dom::MediaKeySessionClosedReason::Closed_by_application); 78 } 79 80 void MediaDrmCDMCallbackProxy::SessionError(const nsCString& aSessionId, 81 nsresult aException, 82 uint32_t aSystemCode, 83 const nsCString& aMessage) { 84 MOZ_ASSERT(NS_IsMainThread()); 85 mProxy->OnSessionError(NS_ConvertUTF8toUTF16(aSessionId), aException, 86 aSystemCode, NS_ConvertUTF8toUTF16(aMessage)); 87 } 88 89 void MediaDrmCDMCallbackProxy::BatchedKeyStatusChanged( 90 const nsCString& aSessionId, const nsTArray<CDMKeyInfo>& aKeyInfos) { 91 MOZ_ASSERT(NS_IsMainThread()); 92 BatchedKeyStatusChangedInternal(aSessionId, aKeyInfos); 93 } 94 95 void MediaDrmCDMCallbackProxy::BatchedKeyStatusChangedInternal( 96 const nsCString& aSessionId, const nsTArray<CDMKeyInfo>& aKeyInfos) { 97 bool keyStatusesChange = false; 98 { 99 auto caps = mProxy->Capabilites().Lock(); 100 for (size_t i = 0; i < aKeyInfos.Length(); i++) { 101 keyStatusesChange |= caps->SetKeyStatus(aKeyInfos[i].mKeyId, 102 NS_ConvertUTF8toUTF16(aSessionId), 103 aKeyInfos[i].mStatus); 104 } 105 } 106 if (keyStatusesChange) { 107 mProxy->OnKeyStatusesChange(NS_ConvertUTF8toUTF16(aSessionId)); 108 } 109 } 110 111 void MediaDrmCDMCallbackProxy::Decrypted( 112 uint32_t aId, DecryptStatus aResult, 113 const nsTArray<uint8_t>& aDecryptedData) { 114 MOZ_ASSERT_UNREACHABLE("Fennec could not handle decrypted event"); 115 } 116 117 } // namespace mozilla