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/dom/HTMLMediaElement.h"
|
|
|
|
#include "mozilla/dom/MediaKeySession.h"
|
|
|
|
#include "mozilla/dom/MediaKeyError.h"
|
|
|
|
#include "mozilla/dom/MediaKeyMessageEvent.h"
|
2014-08-27 12:46:56 +04:00
|
|
|
#include "mozilla/dom/MediaEncryptedEvent.h"
|
2015-01-31 03:23:07 +03:00
|
|
|
#include "mozilla/dom/MediaKeyStatusMap.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "mozilla/CDMProxy.h"
|
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2014-08-27 12:46:56 +04:00
|
|
|
#include "mozilla/Move.h"
|
2014-10-15 12:33:18 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(MediaKeySession,
|
|
|
|
DOMEventTargetHelper,
|
|
|
|
mMediaKeyError,
|
|
|
|
mKeys,
|
2015-01-31 03:23:07 +03:00
|
|
|
mKeyStatusMap,
|
2014-06-07 00:52:15 +04:00
|
|
|
mClosed)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaKeySession)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MediaKeySession, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MediaKeySession, DOMEventTargetHelper)
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
// Count of number of instances. Used to give each instance a
|
|
|
|
// unique token.
|
|
|
|
static uint32_t sMediaKeySessionNum = 0;
|
|
|
|
|
2015-02-01 21:12:00 +03:00
|
|
|
MediaKeySession::MediaKeySession(JSContext* aCx,
|
|
|
|
nsPIDOMWindow* aParent,
|
2014-06-07 00:52:15 +04:00
|
|
|
MediaKeys* aKeys,
|
|
|
|
const nsAString& aKeySystem,
|
2014-07-19 05:31:11 +04:00
|
|
|
SessionType aSessionType,
|
|
|
|
ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
: DOMEventTargetHelper(aParent)
|
|
|
|
, mKeys(aKeys)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
, mSessionType(aSessionType)
|
2015-01-09 04:30:07 +03:00
|
|
|
, mToken(sMediaKeySessionNum++)
|
2014-06-07 00:52:15 +04:00
|
|
|
, mIsClosed(false)
|
2014-09-24 02:04:49 +04:00
|
|
|
, mUninitialized(true)
|
2015-02-01 21:12:00 +03:00
|
|
|
, mKeyStatusMap(new MediaKeyStatusMap(aCx, aParent, aRv))
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
2015-02-01 21:12:00 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-19 05:31:11 +04:00
|
|
|
mClosed = mKeys->MakePromise(aRv);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
void MediaKeySession::SetSessionId(const nsAString& aSessionId)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2015-01-09 04:30:07 +03:00
|
|
|
if (NS_WARN_IF(!mSessionId.IsEmpty())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
mSessionId = aSessionId;
|
2015-01-09 04:30:07 +03:00
|
|
|
mKeys->OnSessionIdReady(this);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaKeySession::~MediaKeySession()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaKeyError*
|
|
|
|
MediaKeySession::GetError() const
|
|
|
|
{
|
|
|
|
return mMediaKeyError;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySession::GetKeySystem(nsString& aKeySystem) const
|
|
|
|
{
|
|
|
|
aKeySystem = mKeySystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySession::GetSessionId(nsString& aSessionId) const
|
|
|
|
{
|
2014-07-30 10:53:28 +04:00
|
|
|
aSessionId = GetSessionId();
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsString&
|
|
|
|
MediaKeySession::GetSessionId() const
|
|
|
|
{
|
|
|
|
return mSessionId;
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaKeySession::WrapObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
return MediaKeySessionBinding::Wrap(aCx, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
MediaKeySession::Expiration() const
|
|
|
|
{
|
|
|
|
return JS::GenericNaN();
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
|
|
|
MediaKeySession::Closed() const
|
|
|
|
{
|
|
|
|
return mClosed;
|
|
|
|
}
|
|
|
|
|
2015-01-31 03:23:07 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySession::UpdateKeyStatusMap()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!IsClosed());
|
|
|
|
if (!mKeys->GetCDMProxy()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<CDMCaps::KeyStatus> keyStatuses;
|
|
|
|
{
|
|
|
|
CDMCaps::AutoLock caps(mKeys->GetCDMProxy()->Capabilites());
|
|
|
|
caps.GetKeyStatusesForSession(mSessionId, keyStatuses);
|
|
|
|
}
|
|
|
|
|
|
|
|
mKeyStatusMap->Update(keyStatuses);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaKeyStatusMap*
|
|
|
|
MediaKeySession::KeyStatuses() const
|
|
|
|
{
|
|
|
|
return mKeyStatusMap;
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeySession::GenerateRequest(const nsAString& aInitDataType,
|
|
|
|
const ArrayBufferViewOrArrayBuffer& aInitData,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUninitialized) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mUninitialized = false;
|
|
|
|
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
if (aInitDataType.IsEmpty() ||
|
|
|
|
!CopyArrayBufferViewOrArrayBufferData(aInitData, data)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseId pid = mKeys->StorePromise(promise);
|
2015-01-09 04:30:07 +03:00
|
|
|
mKeys->GetCDMProxy()->CreateSession(Token(),
|
|
|
|
mSessionType,
|
2014-09-24 02:04:49 +04:00
|
|
|
pid,
|
|
|
|
aInitDataType, data);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeySession::Load(const nsAString& aSessionId, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSessionId.IsEmpty()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
// "The sessionId parameter is empty."
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mUninitialized) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mUninitialized = false;
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
// We now know the sessionId being loaded into this session. Remove the
|
|
|
|
// session from its owning MediaKey's set of sessions awaiting a sessionId.
|
|
|
|
nsRefPtr<MediaKeySession> session(mKeys->GetPendingSession(Token()));
|
|
|
|
MOZ_ASSERT(session == this, "Session should be awaiting id on its own token");
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
// Associate with the known sessionId.
|
|
|
|
SetSessionId(aSessionId);
|
|
|
|
|
|
|
|
mKeys->GetCDMProxy()->LoadSession(mKeys->StorePromise(promise), aSessionId);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-08-27 12:46:56 +04:00
|
|
|
MediaKeySession::Update(const ArrayBufferViewOrArrayBuffer& aResponse, ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t> data;
|
2014-06-07 00:52:15 +04:00
|
|
|
if (IsClosed() ||
|
|
|
|
!mKeys->GetCDMProxy() ||
|
2014-08-27 12:46:56 +04:00
|
|
|
!CopyArrayBufferViewOrArrayBufferData(aResponse, data)) {
|
2014-06-07 00:52:15 +04:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
mKeys->GetCDMProxy()->UpdateSession(mSessionId,
|
|
|
|
mKeys->StorePromise(promise),
|
2014-08-27 12:46:56 +04:00
|
|
|
data);
|
2014-06-07 00:52:15 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
MediaKeySession::Close(ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
if (IsClosed() || !mKeys->GetCDMProxy()) {
|
|
|
|
promise->MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
mKeys->GetCDMProxy()->CloseSession(mSessionId, mKeys->StorePromise(promise));
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySession::OnClosed()
|
|
|
|
{
|
|
|
|
if (IsClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsClosed = true;
|
|
|
|
mKeys->OnSessionClosed(this);
|
|
|
|
mKeys = nullptr;
|
|
|
|
mClosed->MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaKeySession::IsClosed() const
|
|
|
|
{
|
|
|
|
return mIsClosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
MediaKeySession::Remove(ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
nsRefPtr<Promise> promise(mKeys->MakePromise(aRv));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
if (mSessionType != SessionType::Persistent) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
// "The operation is not supported on session type sessions."
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
if (IsClosed() || !mKeys->GetCDMProxy()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
// "The session is closed."
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
mKeys->GetCDMProxy()->RemoveSession(mSessionId, mKeys->StorePromise(promise));
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-15 01:25:47 +03:00
|
|
|
MediaKeySession::DispatchKeyMessage(MediaKeyMessageType aMessageType,
|
|
|
|
const nsTArray<uint8_t>& aMessage)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<MediaKeyMessageEvent> event(
|
2015-01-15 01:25:47 +03:00
|
|
|
MediaKeyMessageEvent::Constructor(this, aMessageType, aMessage));
|
2014-06-07 00:52:15 +04:00
|
|
|
nsRefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, event);
|
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaKeySession::DispatchKeyError(uint32_t aSystemCode)
|
|
|
|
{
|
|
|
|
RefPtr<MediaKeyError> event(new MediaKeyError(this, aSystemCode));
|
|
|
|
nsRefPtr<AsyncEventDispatcher> asyncDispatcher =
|
|
|
|
new AsyncEventDispatcher(this, event);
|
|
|
|
asyncDispatcher->PostDOMEvent();
|
|
|
|
}
|
|
|
|
|
2014-10-15 12:33:18 +04:00
|
|
|
void
|
2015-02-03 11:53:36 +03:00
|
|
|
MediaKeySession::DispatchKeyStatusesChange()
|
2014-10-15 12:33:18 +04:00
|
|
|
{
|
|
|
|
if (IsClosed()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-31 03:23:07 +03:00
|
|
|
|
|
|
|
UpdateKeyStatusMap();
|
|
|
|
|
2014-12-11 05:59:37 +03:00
|
|
|
nsRefPtr<AsyncEventDispatcher> asyncDispatcher =
|
2015-02-03 11:53:36 +03:00
|
|
|
new AsyncEventDispatcher(this, NS_LITERAL_STRING("keystatuseschange"), false);
|
2014-12-11 05:59:37 +03:00
|
|
|
asyncDispatcher->PostDOMEvent();
|
2014-10-15 12:33:18 +04:00
|
|
|
}
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
uint32_t
|
|
|
|
MediaKeySession::Token() const
|
|
|
|
{
|
|
|
|
return mToken;
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|