2014-06-07 00:52:15 +04:00
|
|
|
/* -*- 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/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"
|
2014-07-30 10:53:28 +04:00
|
|
|
#include "mozilla/EMELog.h"
|
|
|
|
#include "nsIConsoleService.h"
|
|
|
|
#include "prenv.h"
|
|
|
|
#include "mozilla/PodOperations.h"
|
|
|
|
#include "mozilla/CDMCallbackProxy.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
CDMProxy::CDMProxy(dom::MediaKeys* aKeys, const nsAString& aKeySystem)
|
|
|
|
: mKeys(aKeys)
|
|
|
|
, mKeySystem(aKeySystem)
|
2014-07-30 10:53:28 +04:00
|
|
|
, mCDM(nullptr)
|
|
|
|
, mDecryptionJobCount(0)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_CTOR(CDMProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
CDMProxy::~CDMProxy()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(CDMProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-10-13 02:53:43 +04:00
|
|
|
CDMProxy::Init(PromiseId aPromiseId,
|
|
|
|
const nsAString& aOrigin,
|
|
|
|
const nsAString& aTopLevelOrigin,
|
|
|
|
bool aInPrivateBrowsing)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-18 01:41:50 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(!mKeys.IsNull());
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
EME_LOG("CDMProxy::Init (%s, %s) %s",
|
|
|
|
NS_ConvertUTF16toUTF8(aOrigin).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aTopLevelOrigin).get(),
|
|
|
|
(aInPrivateBrowsing ? "PrivateBrowsing" : "NonPrivateBrowsing"));
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
if (!mGMPThread) {
|
|
|
|
nsCOMPtr<mozIGeckoMediaPluginService> mps =
|
|
|
|
do_GetService("@mozilla.org/gecko-media-plugin-service;1");
|
|
|
|
if (!mps) {
|
|
|
|
RejectPromise(aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mps->GetThread(getter_AddRefs(mGMPThread));
|
|
|
|
if (!mGMPThread) {
|
|
|
|
RejectPromise(aPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 02:53:43 +04:00
|
|
|
nsAutoPtr<InitData> data(new InitData());
|
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mOrigin = aOrigin;
|
|
|
|
data->mTopLevelOrigin = aTopLevelOrigin;
|
|
|
|
data->mInPrivateBrowsing = aInPrivateBrowsing;
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<InitData>>(this,
|
|
|
|
&CDMProxy::gmp_Init,
|
|
|
|
data));
|
2014-07-30 10:53:28 +04:00
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
bool
|
|
|
|
CDMProxy::IsOnGMPThread()
|
|
|
|
{
|
|
|
|
return NS_GetCurrentThread() == mGMPThread;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2014-10-13 02:53:43 +04:00
|
|
|
CDMProxy::gmp_Init(nsAutoPtr<InitData> aData)
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
|
|
|
|
nsCOMPtr<mozIGeckoMediaPluginService> mps =
|
|
|
|
do_GetService("@mozilla.org/gecko-media-plugin-service;1");
|
|
|
|
if (!mps) {
|
2014-10-13 02:53:43 +04:00
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = mps->GetNodeId(aData->mOrigin,
|
|
|
|
aData->mTopLevelOrigin,
|
|
|
|
aData->mInPrivateBrowsing,
|
|
|
|
mNodeId);
|
|
|
|
MOZ_ASSERT(!GetNodeId().IsEmpty());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
2014-07-30 10:53:28 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
EME_LOG("CDMProxy::gmp_Init (%s, %s) %s NodeId=%s",
|
|
|
|
NS_ConvertUTF16toUTF8(aData->mOrigin).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aData->mTopLevelOrigin).get(),
|
|
|
|
(aData->mInPrivateBrowsing ? "PrivateBrowsing" : "NonPrivateBrowsing"),
|
|
|
|
GetNodeId().get());
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsTArray<nsCString> tags;
|
|
|
|
tags.AppendElement(NS_ConvertUTF16toUTF8(mKeySystem));
|
2014-10-13 02:53:43 +04:00
|
|
|
rv = mps->GetGMPDecryptor(&tags, GetNodeId(), &mCDM);
|
2014-07-30 10:53:28 +04:00
|
|
|
if (NS_FAILED(rv) || !mCDM) {
|
2014-10-13 02:53:43 +04:00
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
2014-07-30 10:53:28 +04:00
|
|
|
} else {
|
|
|
|
mCallback = new CDMCallbackProxy(this);
|
|
|
|
mCDM->Init(mCallback);
|
2014-10-13 02:53:43 +04:00
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<uint32_t>(this,
|
|
|
|
&CDMProxy::OnCDMCreated,
|
|
|
|
aData->mPromiseId));
|
2014-07-30 10:53:28 +04:00
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::OnCDMCreated(uint32_t aPromiseId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-18 01:41:50 +04:00
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
2014-10-13 02:53:43 +04:00
|
|
|
MOZ_ASSERT(!GetNodeId().IsEmpty());
|
|
|
|
mKeys->OnCDMCreated(aPromiseId, GetNodeId());
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
void
|
2015-01-09 04:30:07 +03:00
|
|
|
CDMProxy::CreateSession(uint32_t aCreateSessionToken,
|
|
|
|
dom::SessionType aSessionType,
|
2014-06-07 00:52:15 +04:00
|
|
|
PromiseId aPromiseId,
|
|
|
|
const nsAString& aInitDataType,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aInitData)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mGMPThread);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<CreateSessionData> data(new CreateSessionData());
|
|
|
|
data->mSessionType = aSessionType;
|
2015-01-09 04:30:07 +03:00
|
|
|
data->mCreateSessionToken = aCreateSessionToken;
|
2014-07-30 10:53:28 +04:00
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mInitDataType = NS_ConvertUTF16toUTF8(aInitDataType);
|
2014-08-27 12:46:56 +04:00
|
|
|
data->mInitData = Move(aInitData);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<CreateSessionData>>(this, &CDMProxy::gmp_CreateSession, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
GMPSessionType
|
|
|
|
ToGMPSessionType(dom::SessionType aSessionType) {
|
|
|
|
switch (aSessionType) {
|
|
|
|
case dom::SessionType::Temporary: return kGMPTemporySession;
|
|
|
|
case dom::SessionType::Persistent: return kGMPPersistentSession;
|
|
|
|
default: return kGMPTemporySession;
|
|
|
|
};
|
|
|
|
};
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::gmp_CreateSession(nsAutoPtr<CreateSessionData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2015-01-09 04:30:07 +03:00
|
|
|
mCDM->CreateSession(aData->mCreateSessionToken,
|
|
|
|
aData->mPromiseId,
|
2014-07-30 10:53:28 +04:00
|
|
|
aData->mInitDataType,
|
|
|
|
aData->mInitData,
|
|
|
|
ToGMPSessionType(aData->mSessionType));
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::LoadSession(PromiseId aPromiseId,
|
|
|
|
const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mGMPThread);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<SessionOpData> data(new SessionOpData());
|
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mSessionId = NS_ConvertUTF16toUTF8(aSessionId);
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<SessionOpData>>(this, &CDMProxy::gmp_LoadSession, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::gmp_LoadSession(nsAutoPtr<SessionOpData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCDM->LoadSession(aData->mPromiseId, aData->mSessionId);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::SetServerCertificate(PromiseId aPromiseId,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aCert)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mGMPThread);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<SetServerCertificateData> data;
|
|
|
|
data->mPromiseId = aPromiseId;
|
2014-08-27 12:46:56 +04:00
|
|
|
data->mCert = Move(aCert);
|
2014-07-30 10:53:28 +04:00
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<SetServerCertificateData>>(this, &CDMProxy::gmp_SetServerCertificate, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::gmp_SetServerCertificate(nsAutoPtr<SetServerCertificateData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCDM->SetServerCertificate(aData->mPromiseId, aData->mCert);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::UpdateSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aResponse)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mGMPThread);
|
|
|
|
NS_ENSURE_TRUE_VOID(!mKeys.IsNull());
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<UpdateSessionData> data(new UpdateSessionData());
|
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mSessionId = NS_ConvertUTF16toUTF8(aSessionId);
|
2014-08-27 12:46:56 +04:00
|
|
|
data->mResponse = Move(aResponse);
|
2014-07-30 10:53:28 +04:00
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<UpdateSessionData>>(this, &CDMProxy::gmp_UpdateSession, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::gmp_UpdateSession(nsAutoPtr<UpdateSessionData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCDM->UpdateSession(aData->mPromiseId,
|
|
|
|
aData->mSessionId,
|
|
|
|
aData->mResponse);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::CloseSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_TRUE_VOID(!mKeys.IsNull());
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<SessionOpData> data(new SessionOpData());
|
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mSessionId = NS_ConvertUTF16toUTF8(aSessionId);
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<SessionOpData>>(this, &CDMProxy::gmp_CloseSession, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::gmp_CloseSession(nsAutoPtr<SessionOpData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCDM->CloseSession(aData->mPromiseId, aData->mSessionId);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::RemoveSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-30 10:53:28 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(!mKeys.IsNull());
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<SessionOpData> data(new SessionOpData());
|
|
|
|
data->mPromiseId = aPromiseId;
|
|
|
|
data->mSessionId = NS_ConvertUTF16toUTF8(aSessionId);
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<SessionOpData>>(this, &CDMProxy::gmp_RemoveSession, data));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::gmp_RemoveSession(nsAutoPtr<SessionOpData> aData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
if (!mCDM) {
|
|
|
|
RejectPromise(aData->mPromiseId, NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCDM->RemoveSession(aData->mPromiseId, aData->mSessionId);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mKeys.Clear();
|
2014-08-08 06:43:54 +04:00
|
|
|
// Note: This may end up being the last owning reference to the CDMProxy.
|
|
|
|
nsRefPtr<nsIRunnable> task(NS_NewRunnableMethod(this, &CDMProxy::gmp_Shutdown));
|
2014-12-11 05:59:37 +03:00
|
|
|
if (mGMPThread) {
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
2014-08-08 06:43:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::gmp_Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
2014-08-21 08:10:11 +04:00
|
|
|
|
|
|
|
// Abort any pending decrypt jobs, to awaken any clients waiting on a job.
|
|
|
|
for (size_t i = 0; i < mDecryptionJobs.Length(); i++) {
|
|
|
|
DecryptJob* job = mDecryptionJobs[i];
|
2014-12-11 05:59:37 +03:00
|
|
|
job->mClient->Decrypted(GMPAbortedErr, nullptr);
|
2014-08-21 08:10:11 +04:00
|
|
|
}
|
|
|
|
mDecryptionJobs.Clear();
|
|
|
|
|
2014-08-08 06:43:54 +04:00
|
|
|
if (mCDM) {
|
|
|
|
mCDM->Close();
|
|
|
|
mCDM = nullptr;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::RejectPromise(PromiseId aId, nsresult aCode)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (!mKeys.IsNull()) {
|
|
|
|
mKeys->RejectPromise(aId, aCode);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsRefPtr<nsIRunnable> task(new RejectPromiseTask(this, aId, aCode));
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::ResolvePromise(PromiseId aId)
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
if (!mKeys.IsNull()) {
|
|
|
|
mKeys->ResolvePromise(aId);
|
|
|
|
} else {
|
|
|
|
NS_WARNING("CDMProxy unable to resolve promise!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nsRefPtr<nsIRunnable> task;
|
|
|
|
task = NS_NewRunnableMethodWithArg<PromiseId>(this,
|
|
|
|
&CDMProxy::ResolvePromise,
|
|
|
|
aId);
|
|
|
|
NS_DispatchToMainThread(task);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
const nsCString&
|
|
|
|
CDMProxy::GetNodeId() const
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
2014-10-13 02:53:43 +04:00
|
|
|
return mNodeId;
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-09 04:30:07 +03:00
|
|
|
CDMProxy::OnSetSessionId(uint32_t aCreateSessionToken,
|
|
|
|
const nsAString& aSessionId)
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-18 01:41:50 +04:00
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-09 04:30:07 +03:00
|
|
|
|
|
|
|
nsRefPtr<dom::MediaKeySession> session(mKeys->GetPendingSession(aCreateSessionToken));
|
|
|
|
session->SetSessionId(aSessionId);
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
void
|
|
|
|
CDMProxy::OnResolveLoadSessionPromise(uint32_t aPromiseId, bool aSuccess)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mKeys->OnSessionLoaded(aPromiseId, aSuccess);
|
|
|
|
}
|
|
|
|
|
2015-01-15 01:25:47 +03:00
|
|
|
static dom::MediaKeyMessageType
|
|
|
|
ToMediaKeyMessageType(GMPSessionMessageType aMessageType) {
|
|
|
|
switch (aMessageType) {
|
|
|
|
case kGMPLicenseRequest: return dom::MediaKeyMessageType::License_request;
|
|
|
|
case kGMPLicenseRenewal: return dom::MediaKeyMessageType::License_renewal;
|
|
|
|
case kGMPLicenseRelease: return dom::MediaKeyMessageType::License_release;
|
2015-01-26 22:08:00 +03:00
|
|
|
case kGMPIndividualizationRequest: return dom::MediaKeyMessageType::Individualization_request;
|
2015-01-15 01:25:47 +03:00
|
|
|
default: return dom::MediaKeyMessageType::License_request;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::OnSessionMessage(const nsAString& aSessionId,
|
2015-01-15 01:25:47 +03:00
|
|
|
GMPSessionMessageType aMessageType,
|
|
|
|
nsTArray<uint8_t>& aMessage)
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-18 01:41:50 +04:00
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
nsRefPtr<dom::MediaKeySession> session(mKeys->GetSession(aSessionId));
|
|
|
|
if (session) {
|
2015-01-15 01:25:47 +03:00
|
|
|
session->DispatchKeyMessage(ToMediaKeyMessageType(aMessageType), aMessage);
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 12:33:18 +04:00
|
|
|
void
|
|
|
|
CDMProxy::OnKeysChange(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsRefPtr<dom::MediaKeySession> session(mKeys->GetSession(aSessionId));
|
|
|
|
if (session) {
|
|
|
|
session->DispatchKeysChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
void
|
|
|
|
CDMProxy::OnExpirationChange(const nsAString& aSessionId,
|
|
|
|
GMPTimestamp aExpiryTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_WARNING("CDMProxy::OnExpirationChange() not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::OnSessionClosed(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-12-11 05:59:37 +03:00
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsRefPtr<dom::MediaKeySession> session(mKeys->GetSession(aSessionId));
|
|
|
|
if (session) {
|
|
|
|
session->OnClosed();
|
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
LogToConsole(const nsAString& aMsg)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIConsoleService> console(
|
|
|
|
do_GetService("@mozilla.org/consoleservice;1"));
|
|
|
|
if (!console) {
|
|
|
|
NS_WARNING("Failed to log message to console.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsAutoString msg(aMsg);
|
|
|
|
console->LogStringMessage(msg.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::OnSessionError(const nsAString& aSessionId,
|
|
|
|
nsresult aException,
|
|
|
|
uint32_t aSystemCode,
|
|
|
|
const nsAString& aMsg)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-08-18 01:41:50 +04:00
|
|
|
if (mKeys.IsNull()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
nsRefPtr<dom::MediaKeySession> session(mKeys->GetSession(aSessionId));
|
|
|
|
if (session) {
|
|
|
|
session->DispatchKeyError(aSystemCode);
|
|
|
|
}
|
|
|
|
LogToConsole(aMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::OnRejectPromise(uint32_t aPromiseId,
|
|
|
|
nsresult aDOMException,
|
|
|
|
const nsAString& aMsg)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RejectPromise(aPromiseId, aDOMException);
|
|
|
|
LogToConsole(aMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsString&
|
|
|
|
CDMProxy::KeySystem() const
|
|
|
|
{
|
|
|
|
return mKeySystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDMCaps&
|
|
|
|
CDMProxy::Capabilites() {
|
|
|
|
return mCapabilites;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::Decrypt(mp4_demuxer::MP4Sample* aSample,
|
|
|
|
DecryptionClient* aClient)
|
|
|
|
{
|
|
|
|
nsAutoPtr<DecryptJob> job(new DecryptJob(aSample, aClient));
|
|
|
|
nsRefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethodWithArg<nsAutoPtr<DecryptJob>>(this, &CDMProxy::gmp_Decrypt, job));
|
|
|
|
mGMPThread->Dispatch(task, NS_DISPATCH_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::gmp_Decrypt(nsAutoPtr<DecryptJob> aJob)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
MOZ_ASSERT(aJob->mClient);
|
|
|
|
MOZ_ASSERT(aJob->mSample);
|
|
|
|
|
|
|
|
if (!mCDM) {
|
2014-12-11 05:59:37 +03:00
|
|
|
aJob->mClient->Decrypted(GMPAbortedErr, nullptr);
|
2014-07-30 10:53:28 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aJob->mId = ++mDecryptionJobCount;
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
data.AppendElements(aJob->mSample->data, aJob->mSample->size);
|
|
|
|
mCDM->Decrypt(aJob->mId, aJob->mSample->crypto, data);
|
|
|
|
mDecryptionJobs.AppendElement(aJob.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CDMProxy::gmp_Decrypted(uint32_t aId,
|
|
|
|
GMPErr aResult,
|
|
|
|
const nsTArray<uint8_t>& aDecryptedData)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(IsOnGMPThread());
|
|
|
|
for (size_t i = 0; i < mDecryptionJobs.Length(); i++) {
|
|
|
|
DecryptJob* job = mDecryptionJobs[i];
|
|
|
|
if (job->mId == aId) {
|
|
|
|
if (aDecryptedData.Length() != job->mSample->size) {
|
|
|
|
NS_WARNING("CDM returned incorrect number of decrypted bytes");
|
|
|
|
}
|
2014-08-21 08:10:11 +04:00
|
|
|
if (GMP_SUCCEEDED(aResult)) {
|
|
|
|
PodCopy(job->mSample->data,
|
|
|
|
aDecryptedData.Elements(),
|
|
|
|
std::min<size_t>(aDecryptedData.Length(), job->mSample->size));
|
2014-12-11 05:59:37 +03:00
|
|
|
job->mClient->Decrypted(GMPNoErr, job->mSample.forget());
|
|
|
|
} else if (aResult == GMPNoKeyErr) {
|
|
|
|
NS_WARNING("CDM returned GMPNoKeyErr");
|
|
|
|
// We still have the encrypted sample, so we can re-enqueue it to be
|
|
|
|
// decrypted again once the key is usable again.
|
|
|
|
job->mClient->Decrypted(GMPNoKeyErr, job->mSample.forget());
|
2014-08-21 08:10:11 +04:00
|
|
|
} else {
|
2014-12-11 05:59:37 +03:00
|
|
|
nsAutoCString str("CDM returned decode failure GMPErr=");
|
|
|
|
str.AppendInt(aResult);
|
|
|
|
NS_WARNING(str.get());
|
|
|
|
job->mClient->Decrypted(aResult, nullptr);
|
2014-08-21 08:10:11 +04:00
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
mDecryptionJobs.RemoveElementAt(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 05:59:37 +03:00
|
|
|
NS_WARNING("GMPDecryptorChild returned incorrect job ID");
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-17 04:01:00 +03:00
|
|
|
CDMProxy::Terminated()
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
2014-12-17 04:01:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-12-11 05:59:37 +03:00
|
|
|
NS_WARNING("CDM terminated");
|
2014-12-17 04:01:00 +03:00
|
|
|
if (!mKeys.IsNull()) {
|
|
|
|
mKeys->Terminated();
|
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
} // namespace mozilla
|