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/MediaKeys.h"
|
2016-11-16 01:35:36 +03:00
|
|
|
#include "GMPCrashHelper.h"
|
2015-03-26 09:55:30 +03:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
#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"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2016-09-21 01:45:02 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-07-18 23:13:00 +03:00
|
|
|
#include "GMPCDMProxy.h"
|
2016-11-07 09:03:44 +03:00
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
#include "mozilla/MediaDrmCDMProxy.h"
|
|
|
|
#endif
|
2015-02-20 04:37:55 +03:00
|
|
|
#include "mozilla/EMEUtils.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-07-30 10:53:28 +04:00
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
2014-10-14 04:52:56 +04:00
|
|
|
#include "nsContentTypeParser.h"
|
|
|
|
#ifdef MOZ_FMP4
|
|
|
|
#include "MP4Decoder.h"
|
|
|
|
#endif
|
2014-07-30 10:53:28 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "mozilla/WindowsVersion.h"
|
|
|
|
#endif
|
2014-10-14 04:52:56 +04:00
|
|
|
#include "nsContentCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2015-02-13 22:52:42 +03:00
|
|
|
#include "mozilla/dom/MediaKeySystemAccess.h"
|
2015-03-02 04:13:47 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(MediaKeys,
|
2014-10-13 02:53:43 +04:00
|
|
|
mElement,
|
2014-06-07 00:52:15 +04:00
|
|
|
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
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
MediaKeys::MediaKeys(nsPIDOMWindowInner* aParent,
|
2015-08-31 07:08:51 +03:00
|
|
|
const nsAString& aKeySystem,
|
2016-10-28 01:22:11 +03:00
|
|
|
const MediaKeySystemConfiguration& aConfig)
|
2014-07-30 10:53:28 +04:00
|
|
|
: mParent(aParent)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
, mCreatePromiseId(0)
|
2016-10-28 01:22:11 +03:00
|
|
|
, mConfig(aConfig)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2015-03-11 07:02:08 +03:00
|
|
|
EME_LOG("MediaKeys[%p] constructed keySystem=%s",
|
|
|
|
this, NS_ConvertUTF16toUTF8(mKeySystem).get());
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaKeys::~MediaKeys()
|
2014-08-18 01:41:50 +04:00
|
|
|
{
|
|
|
|
Shutdown();
|
2015-03-11 07:02:08 +03:00
|
|
|
EME_LOG("MediaKeys[%p] destroyed", this);
|
2014-08-18 01:41:50 +04:00
|
|
|
}
|
|
|
|
|
2014-12-17 04:01:00 +03:00
|
|
|
void
|
|
|
|
MediaKeys::Terminated()
|
|
|
|
{
|
2015-03-11 07:02:08 +03:00
|
|
|
EME_LOG("MediaKeys[%p] CDM crashed unexpectedly", this);
|
|
|
|
|
2014-12-17 04:01:00 +03:00
|
|
|
KeySessionHashMap keySessions;
|
|
|
|
// Remove entries during iteration will screw it. Make a copy first.
|
2015-09-25 04:48:25 +03:00
|
|
|
for (auto iter = mKeySessions.Iter(); !iter.Done(); iter.Next()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeySession>& session = iter.Data();
|
2015-09-25 04:48:25 +03:00
|
|
|
keySessions.Put(session->GetSessionId(), session);
|
|
|
|
}
|
|
|
|
for (auto iter = keySessions.Iter(); !iter.Done(); iter.Next()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeySession>& session = iter.Data();
|
2015-09-25 04:48:25 +03:00
|
|
|
session->OnClosed();
|
|
|
|
}
|
2014-12-17 04:01:00 +03:00
|
|
|
keySessions.Clear();
|
|
|
|
MOZ_ASSERT(mKeySessions.Count() == 0);
|
|
|
|
|
|
|
|
// Notify the element about that CDM has terminated.
|
|
|
|
if (mElement) {
|
2016-09-10 17:56:09 +03:00
|
|
|
mElement->DecodeError(NS_ERROR_DOM_MEDIA_CDM_ERR);
|
2014-12-17 04:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
2014-08-18 01:41:50 +04:00
|
|
|
void
|
|
|
|
MediaKeys::Shutdown()
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
if (mProxy) {
|
|
|
|
mProxy->Shutdown();
|
|
|
|
mProxy = nullptr;
|
|
|
|
}
|
2014-08-08 06:43:54 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeys> kungFuDeathGrip = this;
|
2015-02-11 05:11:54 +03:00
|
|
|
|
2015-09-25 04:48:25 +03:00
|
|
|
for (auto iter = mPromises.Iter(); !iter.Done(); iter.Next()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<dom::DetailedPromise>& promise = iter.Data();
|
2015-09-25 04:48:25 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Promise still outstanding at MediaKeys shutdown"));
|
|
|
|
Release();
|
|
|
|
}
|
2014-08-08 06:43:54 +04:00
|
|
|
mPromises.Clear();
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner*
|
2014-06-07 00:52:15 +04:00
|
|
|
MediaKeys::GetParentObject() const
|
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
MediaKeys::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return MediaKeysBinding::Wrap(aCx, this, aGivenProto);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-08-31 07:08:51 +03:00
|
|
|
MediaKeys::GetKeySystem(nsString& aOutKeySystem) const
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2015-12-04 07:35:40 +03:00
|
|
|
aOutKeySystem.Assign(mKeySystem);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2015-06-03 04:42:50 +03:00
|
|
|
already_AddRefed<DetailedPromise>
|
2014-08-27 12:46:56 +04:00
|
|
|
MediaKeys::SetServerCertificate(const ArrayBufferViewOrArrayBuffer& aCert, ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(MakePromise(aRv,
|
2015-07-27 02:52:19 +03:00
|
|
|
NS_LITERAL_CSTRING("MediaKeys.setServerCertificate")));
|
2014-07-19 05:31:11 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-08-27 12:46:56 +04:00
|
|
|
|
2015-04-01 10:48:43 +03:00
|
|
|
if (!mProxy) {
|
|
|
|
NS_WARNING("Tried to use a MediaKeys without a CDM");
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Null CDM in MediaKeys.setServerCertificate()"));
|
2015-04-01 10:48:43 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t> data;
|
2015-11-24 03:13:11 +03:00
|
|
|
CopyArrayBufferViewOrArrayBufferData(aCert, data);
|
|
|
|
if (data.IsEmpty()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_TYPE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Empty certificate passed to MediaKeys.setServerCertificate()"));
|
2014-08-27 12:46:56 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->SetServerCertificate(StorePromise(promise), data);
|
2014-06-07 00:52:15 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:42:50 +03:00
|
|
|
already_AddRefed<DetailedPromise>
|
2015-07-27 02:52:19 +03:00
|
|
|
MediaKeys::MakePromise(ErrorResult& aRv, const nsACString& aName)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
|
|
|
|
if (!global) {
|
|
|
|
NS_WARNING("Passed non-global to MediaKeys ctor!");
|
2014-07-19 05:31:11 +04:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
2014-06-07 00:52:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-27 02:52:19 +03:00
|
|
|
return DetailedPromise::Create(global, aRv, aName);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PromiseId
|
2015-06-03 04:42:50 +03:00
|
|
|
MediaKeys::StorePromise(DetailedPromise* aPromise)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
static uint32_t sEMEPromiseCount = 1;
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
uint32_t id = sEMEPromiseCount++;
|
2015-02-11 01:43:49 +03:00
|
|
|
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::StorePromise() id=%d", this, id);
|
2015-03-02 04:13:47 +03:00
|
|
|
|
2015-02-11 01:43:49 +03:00
|
|
|
// Keep MediaKeys alive for the lifetime of its promises. Any still-pending
|
|
|
|
// promises are rejected in Shutdown().
|
|
|
|
AddRef();
|
|
|
|
|
2015-10-14 09:42:24 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
// We should not have already stored this promise!
|
|
|
|
for (auto iter = mPromises.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
MOZ_ASSERT(iter.Data() != aPromise);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
mPromises.Put(id, aPromise);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2016-10-20 10:28:09 +03:00
|
|
|
void
|
|
|
|
MediaKeys::ConnectPendingPromiseIdWithToken(PromiseId aId, uint32_t aToken)
|
|
|
|
{
|
2016-10-21 00:52:26 +03:00
|
|
|
// Should only be called from MediaKeySession::GenerateRequest.
|
2016-10-20 10:28:09 +03:00
|
|
|
mPromiseIdToken.Put(aId, aToken);
|
|
|
|
EME_LOG("MediaKeys[%p]::ConnectPendingPromiseIdWithToken() id=%u => token(%u)",
|
|
|
|
this, aId, aToken);
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:42:50 +03:00
|
|
|
already_AddRefed<DetailedPromise>
|
2014-06-07 00:52:15 +04:00
|
|
|
MediaKeys::RetrievePromise(PromiseId aId)
|
|
|
|
{
|
2015-03-02 04:13:47 +03:00
|
|
|
if (!mPromises.Contains(aId)) {
|
|
|
|
NS_WARNING(nsPrintfCString("Tried to retrieve a non-existent promise id=%d", aId).get());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise;
|
2014-06-07 00:52:15 +04:00
|
|
|
mPromises.Remove(aId, getter_AddRefs(promise));
|
2015-02-11 01:43:49 +03:00
|
|
|
Release();
|
2014-06-07 00:52:15 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-06-03 04:42:50 +03:00
|
|
|
MediaKeys::RejectPromise(PromiseId aId, nsresult aExceptionCode,
|
|
|
|
const nsCString& aReason)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2016-12-16 06:16:31 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::RejectPromise(%d, 0x%" PRIx32 ")",
|
|
|
|
this, aId, static_cast<uint32_t>(aExceptionCode));
|
2015-03-02 04:13:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(RetrievePromise(aId));
|
2014-06-07 00:52:15 +04:00
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-20 10:28:09 +03:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
uint32_t token = 0;
|
|
|
|
if (mPromiseIdToken.Get(aId, &token)) {
|
|
|
|
MOZ_ASSERT(mPendingSessions.Contains(token));
|
|
|
|
mPendingSessions.Remove(token);
|
|
|
|
mPromiseIdToken.Remove(aId);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_FAILED(aExceptionCode));
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(aExceptionCode, aReason);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
if (mCreatePromiseId == aId) {
|
|
|
|
// Note: This will probably destroy the MediaKeys object!
|
|
|
|
Release();
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
void
|
|
|
|
MediaKeys::OnSessionIdReady(MediaKeySession* aSession)
|
|
|
|
{
|
|
|
|
if (!aSession) {
|
|
|
|
NS_WARNING("Invalid MediaKeySession passed to OnSessionIdReady()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mKeySessions.Contains(aSession->GetSessionId())) {
|
|
|
|
NS_WARNING("MediaKeySession's made ready multiple times!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mPendingSessions.Contains(aSession->Token())) {
|
|
|
|
NS_WARNING("MediaKeySession made ready when it wasn't waiting to be ready!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aSession->GetSessionId().IsEmpty()) {
|
|
|
|
NS_WARNING("MediaKeySession with invalid sessionId passed to OnSessionIdReady()");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mKeySessions.Put(aSession->GetSessionId(), aSession);
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
void
|
|
|
|
MediaKeys::ResolvePromise(PromiseId aId)
|
|
|
|
{
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::ResolvePromise(%d)", this, aId);
|
2015-03-02 04:13:47 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(RetrievePromise(aId));
|
2016-10-20 10:28:09 +03:00
|
|
|
MOZ_ASSERT(!mPromises.Contains(aId));
|
2014-06-07 00:52:15 +04:00
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-20 10:28:09 +03:00
|
|
|
|
|
|
|
uint32_t token = 0;
|
|
|
|
if (!mPromiseIdToken.Get(aId, &token)) {
|
2016-08-09 12:15:13 +03:00
|
|
|
promise->MaybeResolveWithUndefined();
|
2016-10-20 10:28:09 +03:00
|
|
|
return;
|
|
|
|
} else if (!mPendingSessions.Contains(token)) {
|
|
|
|
// Pending session for CreateSession() should be removed when sessionId
|
|
|
|
// is ready.
|
|
|
|
promise->MaybeResolveWithUndefined();
|
|
|
|
mPromiseIdToken.Remove(aId);
|
|
|
|
return;
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
2016-10-20 10:28:09 +03:00
|
|
|
mPromiseIdToken.Remove(aId);
|
|
|
|
|
|
|
|
// We should only resolve LoadSession calls via this path,
|
|
|
|
// not CreateSession() promises.
|
|
|
|
RefPtr<MediaKeySession> session;
|
|
|
|
mPendingSessions.Remove(token, getter_AddRefs(session));
|
|
|
|
if (!session || session->GetSessionId().IsEmpty()) {
|
|
|
|
NS_WARNING("Received activation for non-existent session!");
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR,
|
|
|
|
NS_LITERAL_CSTRING("CDM LoadSession() returned a different session ID than requested"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mKeySessions.Put(session->GetSessionId(), session);
|
|
|
|
promise->MaybeResolve(session);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2016-06-29 02:42:04 +03:00
|
|
|
class MediaKeysGMPCrashHelper : public GMPCrashHelper
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit MediaKeysGMPCrashHelper(MediaKeys* aMediaKeys)
|
|
|
|
: mMediaKeys(aMediaKeys)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread()); // WeakPtr isn't thread safe.
|
|
|
|
}
|
|
|
|
already_AddRefed<nsPIDOMWindowInner>
|
|
|
|
GetPluginCrashedEventTarget() override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread()); // WeakPtr isn't thread safe.
|
|
|
|
EME_LOG("MediaKeysGMPCrashHelper::GetPluginCrashedEventTarget()");
|
|
|
|
return (mMediaKeys && mMediaKeys->GetParentObject()) ?
|
|
|
|
do_AddRef(mMediaKeys->GetParentObject()) : nullptr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
WeakPtr<MediaKeys> mMediaKeys;
|
|
|
|
};
|
|
|
|
|
2016-11-07 09:03:44 +03:00
|
|
|
already_AddRefed<CDMProxy>
|
|
|
|
MediaKeys::CreateCDMProxy()
|
|
|
|
{
|
|
|
|
RefPtr<CDMProxy> proxy;
|
|
|
|
#ifdef MOZ_WIDGET_ANDROID
|
|
|
|
if (IsWidevineKeySystem(mKeySystem)) {
|
|
|
|
proxy = new MediaDrmCDMProxy(this,
|
|
|
|
mKeySystem,
|
|
|
|
mConfig.mDistinctiveIdentifier == MediaKeysRequirement::Required,
|
|
|
|
mConfig.mPersistentState == MediaKeysRequirement::Required);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
proxy = new GMPCDMProxy(this,
|
|
|
|
mKeySystem,
|
|
|
|
new MediaKeysGMPCrashHelper(this),
|
|
|
|
mConfig.mDistinctiveIdentifier == MediaKeysRequirement::Required,
|
|
|
|
mConfig.mPersistentState == MediaKeysRequirement::Required);
|
|
|
|
}
|
|
|
|
return proxy.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:42:50 +03:00
|
|
|
already_AddRefed<DetailedPromise>
|
2014-10-13 02:53:43 +04:00
|
|
|
MediaKeys::Init(ErrorResult& aRv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(MakePromise(aRv,
|
2015-07-27 02:52:19 +03:00
|
|
|
NS_LITERAL_CSTRING("MediaKeys::Init()")));
|
2014-07-19 05:31:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2014-06-07 00:52:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-07 09:03:44 +03:00
|
|
|
mProxy = CreateCDMProxy();
|
2014-10-13 02:53:43 +04:00
|
|
|
|
|
|
|
// Determine principal (at creation time) of the MediaKeys object.
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(GetParentObject());
|
|
|
|
if (!sop) {
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get script principal in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
2014-10-13 02:53:43 +04:00
|
|
|
mPrincipal = sop->GetPrincipal();
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
// Determine principal of the "top-level" window; the principal of the
|
|
|
|
// page that will display in the URL bar.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = GetParentObject();
|
2014-10-13 02:53:43 +04:00
|
|
|
if (!window) {
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get top-level window in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> top = window->GetOuterWindow()->GetTop();
|
2014-10-13 02:53:43 +04:00
|
|
|
if (!top || !top->GetExtantDoc()) {
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get document in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
2014-10-14 04:52:56 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
mTopLevelPrincipal = top->GetExtantDoc()->NodePrincipal();
|
|
|
|
|
|
|
|
if (!mPrincipal || !mTopLevelPrincipal) {
|
|
|
|
NS_WARNING("Failed to get principals when creating MediaKeys");
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get principal(s) in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2016-07-22 09:40:00 +03:00
|
|
|
nsAutoCString origin;
|
|
|
|
nsresult rv = mPrincipal->GetOrigin(origin);
|
2014-10-13 02:53:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get principal origin string in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
2016-07-22 09:40:00 +03:00
|
|
|
nsAutoCString topLevelOrigin;
|
|
|
|
rv = mTopLevelPrincipal->GetOrigin(topLevelOrigin);
|
2014-10-13 02:53:43 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-06-03 04:42:50 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR,
|
|
|
|
NS_LITERAL_CSTRING("Couldn't get top-level principal origin string in MediaKeys::Init"));
|
2014-10-13 02:53:43 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2016-12-07 20:07:09 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::Create() (%s, %s)",
|
2015-03-26 09:55:30 +03:00
|
|
|
this,
|
2016-07-22 09:40:00 +03:00
|
|
|
origin.get(),
|
2016-12-07 20:07:09 +03:00
|
|
|
topLevelOrigin.get());
|
2014-07-30 10:53:28 +04: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.
|
2014-10-13 02:53:43 +04:00
|
|
|
MOZ_ASSERT(!mCreatePromiseId, "Should only be created once!");
|
|
|
|
mCreatePromiseId = StorePromise(promise);
|
|
|
|
AddRef();
|
|
|
|
mProxy->Init(mCreatePromiseId,
|
2016-07-22 09:40:00 +03:00
|
|
|
NS_ConvertUTF8toUTF16(origin),
|
|
|
|
NS_ConvertUTF8toUTF16(topLevelOrigin),
|
2017-03-07 10:32:10 +03:00
|
|
|
KeySystemToGMPName(mKeySystem),
|
|
|
|
top->GetExtantDoc()->EventTargetFor(TaskCategory::Other));
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-28 02:12:34 +03:00
|
|
|
MediaKeys::OnCDMCreated(PromiseId aId, const uint32_t aPluginId)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(RetrievePromise(aId));
|
2014-06-07 00:52:15 +04:00
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeys> keys(this);
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::OnCDMCreated() resolve promise id=%d", this, aId);
|
2014-06-07 00:52:15 +04:00
|
|
|
promise->MaybeResolve(keys);
|
2014-07-30 10:53:28 +04:00
|
|
|
if (mCreatePromiseId == aId) {
|
|
|
|
Release();
|
|
|
|
}
|
2015-02-13 22:52:42 +03:00
|
|
|
|
2015-02-17 00:25:11 +03:00
|
|
|
MediaKeySystemAccess::NotifyObservers(mParent,
|
|
|
|
mKeySystem,
|
2015-02-13 22:52:42 +03:00
|
|
|
MediaKeySystemStatus::Cdm_created);
|
2016-09-21 01:45:02 +03:00
|
|
|
|
2016-09-27 05:01:06 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::VIDEO_CDM_CREATED, ToCDMTypeTelemetryEnum(mKeySystem));
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2016-10-28 01:43:26 +03:00
|
|
|
static bool
|
|
|
|
IsSessionTypeSupported(const MediaKeySessionType aSessionType,
|
|
|
|
const MediaKeySystemConfiguration& aConfig)
|
|
|
|
{
|
|
|
|
if (aSessionType == MediaKeySessionType::Temporary) {
|
|
|
|
// Temporary is always supported.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!aConfig.mSessionTypes.WasPassed()) {
|
|
|
|
// No other session types supported.
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-09 02:35:33 +03:00
|
|
|
return aConfig.mSessionTypes.Value().Contains(ToString(aSessionType));
|
2016-10-28 01:43:26 +03:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
already_AddRefed<MediaKeySession>
|
2015-02-01 21:12:00 +03:00
|
|
|
MediaKeys::CreateSession(JSContext* aCx,
|
2016-07-04 05:14:01 +03:00
|
|
|
MediaKeySessionType aSessionType,
|
2014-07-19 05:31:11 +04:00
|
|
|
ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2016-10-28 01:43:26 +03:00
|
|
|
if (!IsSessionTypeSupported(aSessionType, mConfig)) {
|
2016-12-16 06:16:31 +03:00
|
|
|
EME_LOG("MediaKeys[%p] CreateSession() failed, unsupported session type", this);
|
2016-10-28 01:43:26 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-01 10:48:43 +03:00
|
|
|
if (!mProxy) {
|
|
|
|
NS_WARNING("Tried to use a MediaKeys which lost its CDM");
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-03-11 07:02:08 +03:00
|
|
|
EME_LOG("MediaKeys[%p] Creating session", this);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeySession> session = new MediaKeySession(aCx,
|
2016-11-02 01:56:30 +03:00
|
|
|
GetParentObject(),
|
|
|
|
this,
|
|
|
|
mKeySystem,
|
|
|
|
aSessionType,
|
|
|
|
aRv);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2015-02-01 21:12:00 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
// Add session to the set of sessions awaiting their sessionId being ready.
|
|
|
|
mPendingSessions.Put(session->Token(), session);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
return session.forget();
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
void
|
|
|
|
MediaKeys::OnSessionLoaded(PromiseId aId, bool aSuccess)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DetailedPromise> promise(RetrievePromise(aId));
|
2014-09-24 02:04:49 +04:00
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::OnSessionLoaded() resolve promise id=%d", this, aId);
|
2015-03-02 04:13:47 +03:00
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
promise->MaybeResolve(aSuccess);
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
void
|
|
|
|
MediaKeys::OnSessionClosed(MediaKeySession* aSession)
|
|
|
|
{
|
|
|
|
nsAutoString id;
|
|
|
|
aSession->GetSessionId(id);
|
|
|
|
mKeySessions.Remove(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MediaKeySession>
|
|
|
|
MediaKeys::GetSession(const nsAString& aSessionId)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeySession> session;
|
2014-06-07 00:52:15 +04:00
|
|
|
mKeySessions.Get(aSessionId, getter_AddRefs(session));
|
|
|
|
return session.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-09 04:30:07 +03:00
|
|
|
already_AddRefed<MediaKeySession>
|
|
|
|
MediaKeys::GetPendingSession(uint32_t aToken)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaKeySession> session;
|
2015-01-09 04:30:07 +03:00
|
|
|
mPendingSessions.Get(aToken, getter_AddRefs(session));
|
|
|
|
mPendingSessions.Remove(aToken);
|
|
|
|
return session.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
bool
|
|
|
|
MediaKeys::IsBoundToMediaElement() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mElement != nullptr;
|
|
|
|
}
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
nsresult
|
|
|
|
MediaKeys::Bind(HTMLMediaElement* aElement)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (IsBoundToMediaElement()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
2014-10-13 02:53:43 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
mElement = aElement;
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-07-30 10:53:28 +04:00
|
|
|
}
|
|
|
|
|
2016-01-06 03:28:43 +03:00
|
|
|
void
|
|
|
|
MediaKeys::Unbind()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mElement = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-01 02:56:29 +03:00
|
|
|
void
|
|
|
|
MediaKeys::GetSessionsInfo(nsString& sessionsInfo)
|
|
|
|
{
|
2017-03-03 00:08:29 +03:00
|
|
|
for (KeySessionHashMap::Iterator it = mKeySessions.Iter();
|
|
|
|
!it.Done();
|
|
|
|
it.Next()) {
|
|
|
|
MediaKeySession* keySession = it.Data();
|
2017-03-01 02:56:29 +03:00
|
|
|
nsString sessionID;
|
|
|
|
keySession->GetSessionId(sessionID);
|
2017-03-01 06:03:01 +03:00
|
|
|
sessionsInfo.AppendLiteral("(sid=");
|
2017-03-01 02:56:29 +03:00
|
|
|
sessionsInfo.Append(sessionID);
|
|
|
|
MediaKeyStatusMap* keyStatusMap = keySession->KeyStatuses();
|
|
|
|
for (uint32_t i = 0; i < keyStatusMap->GetIterableLength(); i++) {
|
|
|
|
nsString keyID = keyStatusMap->GetKeyIDAsHexString(i);
|
2017-03-01 06:03:01 +03:00
|
|
|
sessionsInfo.AppendLiteral("(kid=");
|
2017-03-01 02:56:29 +03:00
|
|
|
sessionsInfo.Append(keyID);
|
|
|
|
using IntegerType = typename std::underlying_type<MediaKeyStatus>::type;
|
|
|
|
auto idx = static_cast<IntegerType>(keyStatusMap->GetValueAtIndex(i));
|
|
|
|
const char* keyStatus = MediaKeyStatusValues::strings[idx].value;
|
2017-03-01 06:03:01 +03:00
|
|
|
sessionsInfo.AppendLiteral(" status=");
|
2017-03-01 02:56:29 +03:00
|
|
|
sessionsInfo.Append(
|
|
|
|
NS_ConvertUTF8toUTF16((nsDependentCString(keyStatus))));
|
|
|
|
sessionsInfo.AppendLiteral(")");
|
|
|
|
}
|
|
|
|
sessionsInfo.AppendLiteral(")");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|