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/. */
|
|
|
|
|
|
|
|
#ifndef CDMProxy_h_
|
|
|
|
#define CDMProxy_h_
|
|
|
|
|
2015-07-16 18:52:43 +00:00
|
|
|
#include "mozilla/CDMCaps.h"
|
|
|
|
#include "mozilla/MozPromise.h"
|
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
#include "mozilla/dom/MediaKeyMessageEvent.h"
|
2015-07-16 18:52:43 +00:00
|
|
|
#include "mozilla/dom/MediaKeys.h"
|
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
#include "nsIThread.h"
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-04-09 11:14:55 +00:00
|
|
|
class MediaRawData;
|
2016-12-14 22:32:03 +00:00
|
|
|
class ChromiumCDMProxy;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
2016-07-18 20:12:00 +00:00
|
|
|
enum DecryptStatus {
|
|
|
|
Ok = 0,
|
|
|
|
GenericErr = 1,
|
|
|
|
NoKeyErr = 2,
|
|
|
|
AbortedErr = 3,
|
|
|
|
};
|
|
|
|
|
2015-07-03 19:04:04 +00:00
|
|
|
struct DecryptResult {
|
2016-07-18 20:12:00 +00:00
|
|
|
DecryptResult(DecryptStatus aStatus, MediaRawData* aSample)
|
2015-07-03 19:04:04 +00:00
|
|
|
: mStatus(aStatus)
|
|
|
|
, mSample(aSample)
|
|
|
|
{}
|
2016-07-18 20:12:00 +00:00
|
|
|
DecryptStatus mStatus;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MediaRawData> mSample;
|
2014-07-30 06:53:28 +00:00
|
|
|
};
|
|
|
|
|
2016-11-30 21:57:58 +00:00
|
|
|
typedef MozPromise<DecryptResult, DecryptResult, /* IsExclusive = */ true> DecryptPromise;
|
|
|
|
|
2016-10-04 09:18:46 +00:00
|
|
|
class CDMKeyInfo {
|
|
|
|
public:
|
|
|
|
explicit CDMKeyInfo(const nsTArray<uint8_t>& aKeyId)
|
|
|
|
: mKeyId(aKeyId)
|
|
|
|
, mStatus()
|
|
|
|
{}
|
|
|
|
|
|
|
|
CDMKeyInfo(const nsTArray<uint8_t>& aKeyId,
|
|
|
|
const dom::Optional<dom::MediaKeyStatus>& aStatus)
|
|
|
|
: mKeyId(aKeyId)
|
|
|
|
, mStatus(aStatus.Value())
|
|
|
|
{}
|
|
|
|
|
|
|
|
// The copy-ctor and copy-assignment operator for Optional<T> are declared as
|
|
|
|
// delete, so override CDMKeyInfo copy-ctor for nsTArray operations.
|
|
|
|
CDMKeyInfo(const CDMKeyInfo& aKeyInfo)
|
|
|
|
{
|
|
|
|
mKeyId = aKeyInfo.mKeyId;
|
|
|
|
if (aKeyInfo.mStatus.WasPassed()) {
|
|
|
|
mStatus.Construct(aKeyInfo.mStatus.Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<uint8_t> mKeyId;
|
|
|
|
dom::Optional<dom::MediaKeyStatus> mStatus;
|
|
|
|
};
|
|
|
|
|
2016-08-02 07:05:21 +00:00
|
|
|
typedef int64_t UnixTime;
|
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
// Proxies calls CDM, and proxies calls back.
|
2014-06-06 20:52:15 +00:00
|
|
|
// Note: Promises are passed in via a PromiseId, so that the ID can be
|
|
|
|
// passed via IPC to the CDM, which can then signal when to reject or
|
|
|
|
// resolve the promise using its PromiseId.
|
|
|
|
class CDMProxy {
|
2016-07-18 20:13:00 +00:00
|
|
|
protected:
|
2014-06-06 20:52:15 +00:00
|
|
|
typedef dom::PromiseId PromiseId;
|
2016-07-04 02:14:01 +00:00
|
|
|
typedef dom::MediaKeySessionType MediaKeySessionType;
|
2014-06-06 20:52:15 +00:00
|
|
|
public:
|
|
|
|
|
2017-01-25 19:51:34 +00:00
|
|
|
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-07 05:26:15 +00:00
|
|
|
CDMProxy(dom::MediaKeys* aKeys,
|
|
|
|
const nsAString& aKeySystem,
|
|
|
|
bool aDistinctiveIdentifierRequired,
|
2017-03-09 06:58:18 +00:00
|
|
|
bool aPersistentStateRequired,
|
|
|
|
nsIEventTarget* aMainThread)
|
2016-07-07 05:26:15 +00:00
|
|
|
: mKeys(aKeys)
|
|
|
|
, mKeySystem(aKeySystem)
|
|
|
|
, mDistinctiveIdentifierRequired(aDistinctiveIdentifierRequired)
|
|
|
|
, mPersistentStateRequired(aPersistentStateRequired)
|
2017-03-09 06:58:18 +00:00
|
|
|
, mMainThread(aMainThread)
|
2016-07-18 20:13:00 +00:00
|
|
|
{}
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Loads the CDM corresponding to mKeySystem.
|
|
|
|
// Calls MediaKeys::OnCDMCreated() when the CDM is created.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void Init(PromiseId aPromiseId,
|
|
|
|
const nsAString& aOrigin,
|
|
|
|
const nsAString& aTopLevelOrigin,
|
2017-03-09 06:58:18 +00:00
|
|
|
const nsAString& aName) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
2016-11-10 23:10:43 +00:00
|
|
|
virtual void OnSetDecryptorId(uint32_t aId) {}
|
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
// Main thread only.
|
|
|
|
// Uses the CDM to create a key session.
|
|
|
|
// Calls MediaKeys::OnSessionActivated() when session is created.
|
2014-08-27 08:46:56 +00:00
|
|
|
// Assumes ownership of (Move()s) aInitData's contents.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void CreateSession(uint32_t aCreateSessionToken,
|
2016-07-04 02:14:01 +00:00
|
|
|
MediaKeySessionType aSessionType,
|
2016-07-18 20:13:00 +00:00
|
|
|
PromiseId aPromiseId,
|
|
|
|
const nsAString& aInitDataType,
|
|
|
|
nsTArray<uint8_t>& aInitData) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Uses the CDM to load a presistent session stored on disk.
|
|
|
|
// Calls MediaKeys::OnSessionActivated() when session is loaded.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void LoadSession(PromiseId aPromiseId,
|
2017-03-09 06:09:43 +00:00
|
|
|
dom::MediaKeySessionType aSessionType,
|
2016-07-18 20:13:00 +00:00
|
|
|
const nsAString& aSessionId) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Sends a new certificate to the CDM.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
2014-08-27 08:46:56 +00:00
|
|
|
// Assumes ownership of (Move()s) aCert's contents.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void SetServerCertificate(PromiseId aPromiseId,
|
|
|
|
nsTArray<uint8_t>& aCert) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Sends an update to the CDM.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
2014-08-27 08:46:56 +00:00
|
|
|
// Assumes ownership of (Move()s) aResponse's contents.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void UpdateSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId,
|
|
|
|
nsTArray<uint8_t>& aResponse) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
|
|
|
// If processing this operation results in the session actually closing,
|
|
|
|
// we also call MediaKeySession::OnClosed(), which in turn calls
|
|
|
|
// MediaKeys::OnSessionClosed().
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void CloseSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Removes all data for a persisent session.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void RemoveSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId) = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void Shutdown() = 0;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
2014-12-17 01:01:00 +00:00
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void Terminated() = 0;
|
2014-12-17 01:01:00 +00:00
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
// Threadsafe.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual const nsCString& GetNodeId() const = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnSetSessionId(uint32_t aCreateSessionToken,
|
|
|
|
const nsAString& aSessionId) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2014-09-23 22:04:49 +00:00
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnResolveLoadSessionPromise(uint32_t aPromiseId,
|
|
|
|
bool aSuccess) = 0;
|
2014-09-23 22:04:49 +00:00
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnSessionMessage(const nsAString& aSessionId,
|
|
|
|
dom::MediaKeyMessageType aMessageType,
|
|
|
|
nsTArray<uint8_t>& aMessage) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnExpirationChange(const nsAString& aSessionId,
|
2016-08-02 07:05:21 +00:00
|
|
|
UnixTime aExpiryTime) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnSessionClosed(const nsAString& aSessionId) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnSessionError(const nsAString& aSessionId,
|
|
|
|
nsresult aException,
|
|
|
|
uint32_t aSystemCode,
|
|
|
|
const nsAString& aMsg) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnRejectPromise(uint32_t aPromiseId,
|
|
|
|
nsresult aDOMException,
|
|
|
|
const nsCString& aMsg) = 0;
|
|
|
|
|
|
|
|
virtual RefPtr<DecryptPromise> Decrypt(MediaRawData* aSample) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
// Owner thread only.
|
|
|
|
virtual void OnDecrypted(uint32_t aId,
|
2016-07-18 20:12:00 +00:00
|
|
|
DecryptStatus aResult,
|
2016-07-18 20:13:00 +00:00
|
|
|
const nsTArray<uint8_t>& aDecryptedData) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Reject promise with DOMException corresponding to aExceptionCode.
|
|
|
|
// Can be called from any thread.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void RejectPromise(PromiseId aId,
|
|
|
|
nsresult aExceptionCode,
|
|
|
|
const nsCString& aReason) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Resolves promise with "undefined".
|
|
|
|
// Can be called from any thread.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void ResolvePromise(PromiseId aId) = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
// Threadsafe.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual const nsString& KeySystem() const = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual CDMCaps& Capabilites() = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2014-10-15 08:33:18 +00:00
|
|
|
// Main thread only.
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void OnKeyStatusesChange(const nsAString& aSessionId) = 0;
|
2014-10-15 08:33:18 +00:00
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual void GetSessionIdsForKeyId(const nsTArray<uint8_t>& aKeyId,
|
|
|
|
nsTArray<nsCString>& aSessionIds) = 0;
|
2015-03-26 09:57:36 +00:00
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
#ifdef DEBUG
|
2016-07-18 20:13:00 +00:00
|
|
|
virtual bool IsOnOwnerThread() = 0;
|
2014-07-30 06:53:28 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-10 23:10:43 +00:00
|
|
|
virtual uint32_t GetDecryptorId() { return 0; }
|
|
|
|
|
2016-12-14 22:32:03 +00:00
|
|
|
virtual ChromiumCDMProxy* AsChromiumCDMProxy() { return nullptr; }
|
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
protected:
|
|
|
|
virtual ~CDMProxy() {}
|
2014-06-06 20:52:15 +00:00
|
|
|
|
|
|
|
// Helper to enforce that a raw pointer is only accessed on the main thread.
|
|
|
|
template<class Type>
|
|
|
|
class MainThreadOnlyRawPtr {
|
|
|
|
public:
|
2014-09-01 03:50:23 +00:00
|
|
|
explicit MainThreadOnlyRawPtr(Type* aPtr)
|
2014-06-06 20:52:15 +00:00
|
|
|
: mPtr(aPtr)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNull() const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return !mPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mPtr = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-25 20:18:38 +00:00
|
|
|
Type* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
|
2014-06-06 20:52:15 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mPtr;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Type* mPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Our reference back to the MediaKeys object.
|
|
|
|
// WARNING: This is a non-owning reference that is cleared by MediaKeys
|
|
|
|
// destructor. only use on main thread, and always nullcheck before using!
|
|
|
|
MainThreadOnlyRawPtr<dom::MediaKeys> mKeys;
|
|
|
|
|
2015-11-19 09:00:39 +00:00
|
|
|
const nsString mKeySystem;
|
2014-06-06 20:52:15 +00:00
|
|
|
|
2016-07-18 20:13:00 +00:00
|
|
|
// Onwer specified thread. e.g. Gecko Media Plugin thread.
|
|
|
|
// All interactions with the out-of-process EME plugin must come from this thread.
|
|
|
|
RefPtr<nsIThread> mOwnerThread;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2014-10-12 22:53:43 +00:00
|
|
|
nsCString mNodeId;
|
2014-07-30 06:53:28 +00:00
|
|
|
|
|
|
|
CDMCaps mCapabilites;
|
2016-07-07 05:26:15 +00:00
|
|
|
|
|
|
|
const bool mDistinctiveIdentifierRequired;
|
|
|
|
const bool mPersistentStateRequired;
|
2017-03-09 06:58:18 +00:00
|
|
|
|
|
|
|
// The main thread associated with the root document.
|
|
|
|
const nsCOMPtr<nsIEventTarget> mMainThread;
|
2014-06-06 20:52:15 +00:00
|
|
|
};
|
|
|
|
|
2014-07-30 06:53:28 +00:00
|
|
|
|
2014-06-06 20:52:15 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // CDMProxy_h_
|