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"
|
2015-03-26 09:55:30 +03:00
|
|
|
#include "GMPService.h"
|
|
|
|
#include "mozilla/EventDispatcher.h"
|
|
|
|
#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"
|
2015-03-26 09:55:30 +03:00
|
|
|
#include "mozilla/dom/PluginCrashedEvent.h"
|
2014-10-01 22:43:26 +04:00
|
|
|
#include "mozilla/dom/UnionTypes.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
#include "mozilla/CDMProxy.h"
|
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"
|
|
|
|
#include "mozilla/Preferences.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
|
|
|
|
|
|
|
|
MediaKeys::MediaKeys(nsPIDOMWindow* aParent, const nsAString& aKeySystem)
|
2014-07-30 10:53:28 +04:00
|
|
|
: mParent(aParent)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
, mCreatePromiseId(0)
|
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
|
|
|
}
|
|
|
|
|
2014-08-08 06:43:54 +04:00
|
|
|
static PLDHashOperator
|
|
|
|
RejectPromises(const uint32_t& aKey,
|
|
|
|
nsRefPtr<dom::Promise>& aPromise,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
aPromise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2015-02-12 07:30:00 +03:00
|
|
|
((MediaKeys*)aClosure)->Release();
|
2014-08-08 06:43:54 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static PLDHashOperator
|
|
|
|
CopySessions(const nsAString& aKey,
|
|
|
|
nsRefPtr<MediaKeySession>& aSession,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
KeySessionHashMap* p = static_cast<KeySessionHashMap*>(aClosure);
|
|
|
|
p->Put(aSession->GetSessionId(), aSession);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
CloseSessions(const nsAString& aKey,
|
|
|
|
nsRefPtr<MediaKeySession>& aSession,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
aSession->OnClosed();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
mKeySessions.Enumerate(&CopySessions, &keySessions);
|
|
|
|
keySessions.Enumerate(&CloseSessions, nullptr);
|
|
|
|
keySessions.Clear();
|
|
|
|
MOZ_ASSERT(mKeySessions.Count() == 0);
|
|
|
|
|
|
|
|
// Notify the element about that CDM has terminated.
|
|
|
|
if (mElement) {
|
|
|
|
mElement->DecodeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-11 05:11:54 +03:00
|
|
|
nsRefPtr<MediaKeys> kungFuDeathGrip = this;
|
|
|
|
|
|
|
|
mPromises.Enumerate(&RejectPromises, this);
|
2014-08-08 06:43:54 +04:00
|
|
|
mPromises.Clear();
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow*
|
|
|
|
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
|
|
|
|
MediaKeys::GetKeySystem(nsString& retval) const
|
|
|
|
{
|
|
|
|
retval = mKeySystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-08-27 12:46:56 +04:00
|
|
|
MediaKeys::SetServerCertificate(const ArrayBufferViewOrArrayBuffer& aCert, ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
2014-07-19 05:31:11 +04:00
|
|
|
nsRefPtr<Promise> promise(MakePromise(aRv));
|
|
|
|
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");
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
if (!CopyArrayBufferViewOrArrayBufferData(aCert, data)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
mProxy->SetServerCertificate(StorePromise(promise), data);
|
2014-06-07 00:52:15 +04:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2014-07-19 05:31:11 +04:00
|
|
|
MediaKeys::MakePromise(ErrorResult& aRv)
|
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;
|
|
|
|
}
|
2014-07-19 05:31:11 +04:00
|
|
|
return Promise::Create(global, aRv);
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PromiseId
|
|
|
|
MediaKeys::StorePromise(Promise* aPromise)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
mPromises.Put(id, aPromise);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
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;
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
nsRefPtr<Promise> promise;
|
|
|
|
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
|
|
|
|
MediaKeys::RejectPromise(PromiseId aId, nsresult aExceptionCode)
|
|
|
|
{
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::RejectPromise(%d, 0x%x)", this, aId, aExceptionCode);
|
2015-03-02 04:13:47 +03:00
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!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 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
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-30 10:53:28 +04: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-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
MediaKeys::Init(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(MakePromise(aRv));
|
2014-07-19 05:31:11 +04:00
|
|
|
if (aRv.Failed()) {
|
2014-06-07 00:52:15 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
mProxy = new CDMProxy(this, mKeySystem);
|
|
|
|
|
|
|
|
// Determine principal (at creation time) of the MediaKeys object.
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(GetParentObject());
|
|
|
|
if (!sop) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
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.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(GetParentObject());
|
|
|
|
if (!window) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMWindow> topWindow;
|
|
|
|
window->GetTop(getter_AddRefs(topWindow));
|
|
|
|
nsCOMPtr<nsPIDOMWindow> top = do_QueryInterface(topWindow);
|
|
|
|
if (!top || !top->GetExtantDoc()) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
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");
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString origin;
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(mPrincipal, origin);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
nsAutoString topLevelOrigin;
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(mTopLevelPrincipal, topLevelOrigin);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window) {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
|
|
|
const bool inPrivateBrowsing = nsContentUtils::IsInPrivateBrowsing(doc);
|
|
|
|
|
2015-03-26 09:55:30 +03:00
|
|
|
EME_LOG("MediaKeys[%p]::Create() (%s, %s), %s",
|
|
|
|
this,
|
2014-10-13 02:53:43 +04:00
|
|
|
NS_ConvertUTF16toUTF8(origin).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(topLevelOrigin).get(),
|
|
|
|
(inPrivateBrowsing ? "PrivateBrowsing" : "NonPrivateBrowsing"));
|
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,
|
|
|
|
origin,
|
|
|
|
topLevelOrigin,
|
|
|
|
inPrivateBrowsing);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-03-26 09:55:30 +03:00
|
|
|
class CrashHandler : public gmp::GeckoMediaPluginService::PluginCrashCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CrashHandler(const nsACString& aPluginId,
|
|
|
|
nsPIDOMWindow* aParentWindow,
|
|
|
|
nsIDocument* aDocument)
|
|
|
|
: gmp::GeckoMediaPluginService::PluginCrashCallback(aPluginId)
|
|
|
|
, mParentWindowWeakPtr(do_GetWeakReference(aParentWindow))
|
|
|
|
, mDocumentWeakPtr(do_GetWeakReference(aDocument))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Run(const nsACString& aPluginName, const nsAString& aPluginDumpId) override
|
|
|
|
{
|
|
|
|
PluginCrashedEventInit init;
|
|
|
|
init.mBubbles = true;
|
|
|
|
init.mCancelable = true;
|
|
|
|
init.mGmpPlugin = true;
|
|
|
|
init.mPluginDumpID = aPluginDumpId;
|
|
|
|
CopyUTF8toUTF16(aPluginName, init.mPluginName);
|
|
|
|
init.mSubmittedCrashReport = false;
|
|
|
|
|
|
|
|
// The following PluginCrashedEvent fields stay empty:
|
|
|
|
// init.mBrowserDumpID
|
|
|
|
// init.mPluginFilename
|
|
|
|
// TODO: Can/should we fill them?
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindow> parentWindow;
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
if (!GetParentWindowAndDocumentIfValid(parentWindow, document)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<PluginCrashedEvent> event =
|
|
|
|
PluginCrashedEvent::Constructor(document, NS_LITERAL_STRING("PluginCrashed"), init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
|
|
|
|
|
|
|
|
EventDispatcher::DispatchDOMEvent(parentWindow, nullptr, event, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-03-28 00:04:16 +03:00
|
|
|
virtual bool IsStillValid() override
|
2015-03-26 09:55:30 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsPIDOMWindow> parentWindow;
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
return GetParentWindowAndDocumentIfValid(parentWindow, document);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~CrashHandler()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
GetParentWindowAndDocumentIfValid(nsCOMPtr<nsPIDOMWindow>& parentWindow,
|
|
|
|
nsCOMPtr<nsIDocument>& document)
|
|
|
|
{
|
|
|
|
parentWindow = do_QueryReferent(mParentWindowWeakPtr);
|
|
|
|
if (!parentWindow) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
document = do_QueryReferent(mDocumentWeakPtr);
|
|
|
|
if (!document) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocument> parentWindowDocument = parentWindow->GetExtantDoc();
|
|
|
|
if (!parentWindowDocument || document.get() != parentWindowDocument.get()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWeakPtr mParentWindowWeakPtr;
|
|
|
|
nsWeakPtr mDocumentWeakPtr;
|
|
|
|
};
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
void
|
2015-03-26 09:55:30 +03:00
|
|
|
MediaKeys::OnCDMCreated(PromiseId aId, const nsACString& aNodeId, const nsACString& aPluginId)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
if (!promise) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-13 02:53:43 +04:00
|
|
|
mNodeId = aNodeId;
|
2014-06-07 00:52:15 +04:00
|
|
|
nsRefPtr<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);
|
2015-03-26 09:55:30 +03:00
|
|
|
|
|
|
|
if (!aPluginId.IsEmpty()) {
|
|
|
|
// Prepare plugin crash reporter.
|
|
|
|
nsRefPtr<gmp::GeckoMediaPluginService> service =
|
|
|
|
gmp::GeckoMediaPluginService::GetGeckoMediaPluginService();
|
|
|
|
if (NS_WARN_IF(!service)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (NS_WARN_IF(!mParent)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocument> doc = mParent->GetExtantDoc();
|
|
|
|
if (NS_WARN_IF(!doc)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
service->AddPluginCrashCallback(new CrashHandler(aPluginId, mParent, doc));
|
|
|
|
EME_LOG("MediaKeys[%p]::OnCDMCreated() registered crash handler for pluginId '%s'",
|
|
|
|
this, aPluginId.Data());
|
|
|
|
}
|
2014-06-07 00:52:15 +04:00
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
already_AddRefed<MediaKeySession>
|
2015-02-01 21:12:00 +03:00
|
|
|
MediaKeys::CreateSession(JSContext* aCx,
|
|
|
|
SessionType aSessionType,
|
2014-07-19 05:31:11 +04:00
|
|
|
ErrorResult& aRv)
|
2014-06-07 00:52:15 +04:00
|
|
|
{
|
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-02-01 21:12:00 +03:00
|
|
|
nsRefPtr<MediaKeySession> session = new MediaKeySession(aCx,
|
|
|
|
GetParentObject(),
|
2014-06-07 00:52:15 +04:00
|
|
|
this,
|
|
|
|
mKeySystem,
|
2014-08-27 12:46:56 +04:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
nsRefPtr<Promise> promise(RetrievePromise(aId));
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaKeySession> session;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
nsRefPtr<MediaKeySession> session;
|
|
|
|
mPendingSessions.Get(aToken, getter_AddRefs(session));
|
|
|
|
mPendingSessions.Remove(aToken);
|
|
|
|
return session.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
const nsCString&
|
2014-10-13 02:53:43 +04:00
|
|
|
MediaKeys::GetNodeId() const
|
2014-07-30 10:53:28 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-10-13 02:53:43 +04:00
|
|
|
return mNodeId;
|
|
|
|
}
|
2014-10-13 02:53:43 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-27 12:46:56 +04:00
|
|
|
bool
|
|
|
|
CopyArrayBufferViewOrArrayBufferData(const ArrayBufferViewOrArrayBuffer& aBufferOrView,
|
|
|
|
nsTArray<uint8_t>& aOutData)
|
|
|
|
{
|
|
|
|
if (aBufferOrView.IsArrayBuffer()) {
|
|
|
|
const ArrayBuffer& buffer = aBufferOrView.GetAsArrayBuffer();
|
|
|
|
buffer.ComputeLengthAndData();
|
|
|
|
aOutData.AppendElements(buffer.Data(), buffer.Length());
|
|
|
|
} else if (aBufferOrView.IsArrayBufferView()) {
|
|
|
|
const ArrayBufferView& bufferview = aBufferOrView.GetAsArrayBufferView();
|
|
|
|
bufferview.ComputeLengthAndData();
|
|
|
|
aOutData.AppendElements(bufferview.Data(), bufferview.Length());
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|