2014-06-06 20:52:15 +00: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/dom/HTMLMediaElement.h"
|
|
|
|
#include "mozilla/dom/MediaKeys.h"
|
|
|
|
#include "mozilla/dom/MediaKeysBinding.h"
|
|
|
|
#include "mozilla/dom/MediaKeyMessageEvent.h"
|
|
|
|
#include "mozilla/dom/MediaKeyError.h"
|
|
|
|
#include "mozilla/dom/MediaKeySession.h"
|
|
|
|
#include "mozilla/dom/DOMException.h"
|
|
|
|
#include "mozilla/CDMProxy.h"
|
2014-07-30 06:53:28 +00:00
|
|
|
#include "mozilla/EMELog.h"
|
2014-06-06 20:52:15 +00:00
|
|
|
#include "nsContentUtils.h"
|
2014-07-30 06:53:28 +00:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "mozilla/WindowsVersion.h"
|
|
|
|
#endif
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaKeys,
|
|
|
|
mParent,
|
|
|
|
mKeySessions,
|
|
|
|
mPromises,
|
|
|
|
mPendingSessions);
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaKeys)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaKeys)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaKeys)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
MediaKeys::MediaKeys(nsPIDOMWindow* aParent, const nsAString& aKeySystem)
|
2014-07-30 06:53:28 +00:00
|
|
|
: mParent(aParent)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
, mCreatePromiseId(0)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2014-08-08 02:43:54 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
RejectPromises(const uint32_t& aKey,
|
|
|
|
nsRefPtr<dom::Promise>& aPromise,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
aPromise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
MediaKeys::~MediaKeys()
|
|
|
|
{
|
|
|
|
if (mProxy) {
|
|
|
|
mProxy->Shutdown();
|
|
|
|
mProxy = nullptr;
|
|
|
|
}
|
2014-08-08 02:43:54 +00:00
|
|
|
|
|
|
|
mPromises.Enumerate(&RejectPromises, nullptr);
|
|
|
|
mPromises.Clear();
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow*
|
|
|
|
MediaKeys::GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaKeys::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return MediaKeysBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeys::GetKeySystem(nsString& retval) const
|
|
|
|
{
|
|
|
|
retval = mKeySystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 01:31:11 +00:00
|
|
|
MediaKeys::SetServerCertificate(const Uint8Array& aCert, ErrorResult& aRv)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
|
|
|
aCert.ComputeLengthAndData();
|
2014-07-19 01:31:11 +00:00
|
|
|
nsRefPtr<Promise> promise(MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
mProxy->SetServerCertificate(StorePromise(promise), aCert);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
static bool
|
|
|
|
IsSupportedKeySystem(const nsAString& aKeySystem)
|
|
|
|
{
|
|
|
|
return aKeySystem.EqualsASCII("org.w3.clearkey") ||
|
|
|
|
#ifdef XP_WIN
|
|
|
|
(aKeySystem.EqualsASCII("com.adobe.access") &&
|
|
|
|
IsVistaOrLater() &&
|
|
|
|
Preferences::GetBool("media.eme.adobe-access.enabled", false)) ||
|
|
|
|
#endif
|
|
|
|
false;
|
|
|
|
}
|
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
/* static */
|
|
|
|
IsTypeSupportedResult
|
|
|
|
MediaKeys::IsTypeSupported(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aKeySystem,
|
|
|
|
const Optional<nsAString>& aInitDataType,
|
|
|
|
const Optional<nsAString>& aContentType,
|
|
|
|
const Optional<nsAString>& aCapability)
|
|
|
|
{
|
|
|
|
// TODO: Should really get spec changed to this is async, so we can wait
|
|
|
|
// for user to consent to running plugin.
|
2014-07-30 06:53:28 +00:00
|
|
|
return IsSupportedKeySystem(aKeySystem) ? IsTypeSupportedResult::Maybe
|
|
|
|
: IsTypeSupportedResult::_empty;
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 01:31:11 +00:00
|
|
|
MediaKeys::MakePromise(ErrorResult& aRv)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
|
|
|
|
if (!global) {
|
|
|
|
NS_WARNING("Passed non-global to MediaKeys ctor!");
|
2014-07-19 01:31:11 +00:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2014-06-06 20:52:15 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-19 01:31:11 +00:00
|
|
|
return Promise::Create(global, aRv);
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PromiseId
|
|
|
|
MediaKeys::StorePromise(Promise* aPromise)
|
|
|
|
{
|
|
|
|
static uint32_t sEMEPromiseCount = 1;
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
uint32_t id = sEMEPromiseCount++;
|
|
|
|
mPromises.Put(id, aPromise);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeys::RetrievePromise(PromiseId aId)
|
|
|
|
{
|
2014-07-30 06:53:28 +00:00
|
|
|
MOZ_ASSERT(mPromises.Contains(aId));
|
2014-06-06 20:52:15 +00:00
|
|
|
nsRefPtr<Promise> promise;
|
|
|
|
mPromises.Remove(aId, getter_AddRefs(promise));
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeys::RejectPromise(PromiseId aId, nsresult aExceptionCode)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
NS_WARNING("MediaKeys tried to reject a non-existent promise");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mPendingSessions.Contains(aId)) {
|
|
|
|
// This promise could be a createSession or loadSession promise,
|
|
|
|
// so we might have a pending session waiting to be resolved into
|
|
|
|
// the promise on success. We've been directed to reject to promise,
|
|
|
|
// so we can throw away the corresponding session object.
|
|
|
|
mPendingSessions.Remove(aId);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_FAILED(aExceptionCode));
|
|
|
|
promise->MaybeReject(aExceptionCode);
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
if (mCreatePromiseId == aId) {
|
|
|
|
// Note: This will probably destroy the MediaKeys object!
|
|
|
|
Release();
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeys::ResolvePromise(PromiseId aId)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
NS_WARNING("MediaKeys tried to resolve a non-existent promise");
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 06:53:28 +00:00
|
|
|
if (mPendingSessions.Contains(aId)) {
|
|
|
|
// We should only resolve LoadSession calls via this path,
|
|
|
|
// not CreateSession() promises.
|
|
|
|
nsRefPtr<MediaKeySession> session;
|
|
|
|
if (!mPendingSessions.Get(aId, getter_AddRefs(session)) ||
|
|
|
|
!session ||
|
|
|
|
session->GetSessionId().IsEmpty()) {
|
|
|
|
NS_WARNING("Received activation for non-existent session!");
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
mPendingSessions.Remove(aId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPendingSessions.Remove(aId);
|
|
|
|
mKeySessions.Put(session->GetSessionId(), session);
|
|
|
|
promise->MaybeResolve(session);
|
|
|
|
} else {
|
|
|
|
promise->MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeys::Create(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aKeySystem,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// CDMProxy keeps MediaKeys alive until it resolves the promise and thus
|
|
|
|
// returns the MediaKeys object to JS.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<MediaKeys> keys = new MediaKeys(window, aKeySystem);
|
2014-07-19 01:31:11 +00:00
|
|
|
nsRefPtr<Promise> promise(keys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
2014-06-06 20:52:15 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
if (!IsSupportedKeySystem(aKeySystem)) {
|
2014-06-06 20:52:15 +00:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
keys->mProxy = new CDMProxy(keys, aKeySystem);
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// The CDMProxy's initialization is asynchronous. The MediaKeys is
|
|
|
|
// refcounted, and its instance is returned to JS by promise once
|
|
|
|
// it's been initialized. No external refs exist to the MediaKeys while
|
|
|
|
// we're waiting for the promise to be resolved, so we must hold a
|
|
|
|
// reference to the new MediaKeys object until it's been created,
|
|
|
|
// or its creation has failed. Store the id of the promise returned
|
|
|
|
// here, and hold a self-reference until that promise is resolved or
|
|
|
|
// rejected.
|
|
|
|
MOZ_ASSERT(!keys->mCreatePromiseId, "Should only be created once!");
|
|
|
|
keys->mCreatePromiseId = keys->StorePromise(promise);
|
|
|
|
keys->AddRef();
|
|
|
|
keys->mProxy->Init(keys->mCreatePromiseId);
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeys::OnCDMCreated(PromiseId aId)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
NS_WARNING("MediaKeys tried to resolve a non-existent promise");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsRefPtr<MediaKeys> keys(this);
|
|
|
|
promise->MaybeResolve(keys);
|
2014-07-30 06:53:28 +00:00
|
|
|
if (mCreatePromiseId == aId) {
|
|
|
|
Release();
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 01:31:11 +00:00
|
|
|
MediaKeys::LoadSession(const nsAString& aSessionId, ErrorResult& aRv)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
2014-07-19 01:31:11 +00:00
|
|
|
nsRefPtr<Promise> promise(MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
if (aSessionId.IsEmpty()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
// "The sessionId parameter is empty."
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: The spec doesn't specify what to do in this case...
|
|
|
|
if (mKeySessions.Contains(aSessionId)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create session.
|
|
|
|
nsRefPtr<MediaKeySession> session(
|
2014-07-19 01:31:11 +00:00
|
|
|
new MediaKeySession(GetParentObject(), this, mKeySystem, SessionType::Persistent, aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
session->Init(aSessionId);
|
|
|
|
auto pid = StorePromise(promise);
|
|
|
|
mPendingSessions.Put(pid, session);
|
|
|
|
mProxy->LoadSession(pid, aSessionId);
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeys::CreateSession(const nsAString& initDataType,
|
|
|
|
const Uint8Array& aInitData,
|
2014-07-19 01:31:11 +00:00
|
|
|
SessionType aSessionType,
|
|
|
|
ErrorResult& aRv)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
|
|
|
aInitData.ComputeLengthAndData();
|
2014-07-19 01:31:11 +00:00
|
|
|
nsRefPtr<Promise> promise(MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-06 20:52:15 +00:00
|
|
|
nsRefPtr<MediaKeySession> session = new MediaKeySession(GetParentObject(),
|
|
|
|
this,
|
|
|
|
mKeySystem,
|
2014-07-19 01:31:11 +00:00
|
|
|
aSessionType, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
auto pid = StorePromise(promise);
|
|
|
|
// Hang onto session until the CDM has finished setting it up.
|
|
|
|
mPendingSessions.Put(pid, session);
|
|
|
|
mProxy->CreateSession(aSessionType,
|
|
|
|
pid,
|
|
|
|
initDataType,
|
|
|
|
aInitData);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-30 06:53:28 +00:00
|
|
|
MediaKeys::OnSessionCreated(PromiseId aId, const nsAString& aSessionId)
|
2014-06-06 20:52:15 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
NS_WARNING("MediaKeys tried to resolve a non-existent promise");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mPendingSessions.Contains(aId));
|
|
|
|
|
|
|
|
nsRefPtr<MediaKeySession> session;
|
2014-07-30 06:53:28 +00:00
|
|
|
bool gotSession = mPendingSessions.Get(aId, getter_AddRefs(session));
|
|
|
|
// Session has completed creation/loading, remove it from mPendingSessions,
|
|
|
|
// and resolve the promise with it. We store it in mKeySessions, so we can
|
|
|
|
// find it again if we need to send messages to it etc.
|
|
|
|
mPendingSessions.Remove(aId);
|
|
|
|
if (!gotSession || !session) {
|
2014-06-06 20:52:15 +00:00
|
|
|
NS_WARNING("Received activation for non-existent session!");
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->Init(aSessionId);
|
|
|
|
mKeySessions.Put(aSessionId, session);
|
|
|
|
promise->MaybeResolve(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeys::OnSessionClosed(MediaKeySession* aSession)
|
|
|
|
{
|
|
|
|
nsAutoString id;
|
|
|
|
aSession->GetSessionId(id);
|
|
|
|
mKeySessions.Remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MediaKeySession>
|
|
|
|
MediaKeys::GetSession(const nsAString& aSessionId)
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaKeySession> session;
|
|
|
|
mKeySessions.Get(aSessionId, getter_AddRefs(session));
|
|
|
|
return session.forget();
|
|
|
|
}
|
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
nsresult
|
|
|
|
MediaKeys::GetOrigin(nsString& aOutOrigin)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// TODO: Bug 1035637, return a combination of origin and URL bar origin.
|
|
|
|
|
|
|
|
nsIPrincipal* principal = nullptr;
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(GetParentObject());
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptPrincipal =
|
|
|
|
do_QueryInterface(pWindow);
|
|
|
|
if (scriptPrincipal) {
|
|
|
|
principal = scriptPrincipal->GetPrincipal();
|
|
|
|
}
|
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsresult res = nsContentUtils::GetUTFOrigin(principal, aOutOrigin);
|
|
|
|
|
|
|
|
EME_LOG("EME Origin = '%s'", NS_ConvertUTF16toUTF8(aOutOrigin).get());
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|