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/. */
|
|
|
|
|
|
|
|
#ifndef CDMProxy_h_
|
|
|
|
#define CDMProxy_h_
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "mozilla/dom/MediaKeys.h"
|
2014-07-30 10:53:28 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "GMPDecryptorProxy.h"
|
|
|
|
#include "mozilla/CDMCaps.h"
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2015-04-09 14:14:55 +03:00
|
|
|
class MediaRawData;
|
2014-07-30 10:53:28 +04:00
|
|
|
class CDMCallbackProxy;
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
namespace dom {
|
|
|
|
class MediaKeySession;
|
|
|
|
}
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
class DecryptionClient {
|
|
|
|
public:
|
|
|
|
virtual ~DecryptionClient() {}
|
2014-12-11 05:59:37 +03:00
|
|
|
virtual void Decrypted(GMPErr aResult,
|
2015-04-09 14:14:55 +03:00
|
|
|
MediaRawData* aSample) = 0;
|
2014-07-30 10:53:28 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Proxies calls GMP/CDM, and proxies calls back.
|
2014-06-07 00:52:15 +04: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 {
|
|
|
|
typedef dom::PromiseId PromiseId;
|
|
|
|
typedef dom::SessionType SessionType;
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CDMProxy)
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
CDMProxy(dom::MediaKeys* aKeys, const nsAString& aKeySystem);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Loads the CDM corresponding to mKeySystem.
|
|
|
|
// Calls MediaKeys::OnCDMCreated() when the CDM is created.
|
2014-10-13 02:53:43 +04:00
|
|
|
void Init(PromiseId aPromiseId,
|
|
|
|
const nsAString& aOrigin,
|
|
|
|
const nsAString& aTopLevelOrigin,
|
|
|
|
bool aInPrivateBrowsing);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Uses the CDM to create a key session.
|
|
|
|
// Calls MediaKeys::OnSessionActivated() when session is created.
|
2014-08-27 12:46:56 +04:00
|
|
|
// Assumes ownership of (Move()s) aInitData's contents.
|
2015-01-09 04:30:07 +03:00
|
|
|
void CreateSession(uint32_t aCreateSessionToken,
|
|
|
|
dom::SessionType aSessionType,
|
2014-06-07 00:52:15 +04:00
|
|
|
PromiseId aPromiseId,
|
|
|
|
const nsAString& aInitDataType,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aInitData);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Uses the CDM to load a presistent session stored on disk.
|
|
|
|
// Calls MediaKeys::OnSessionActivated() when session is loaded.
|
|
|
|
void LoadSession(PromiseId aPromiseId,
|
|
|
|
const nsAString& aSessionId);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Sends a new certificate to the CDM.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
2014-08-27 12:46:56 +04:00
|
|
|
// Assumes ownership of (Move()s) aCert's contents.
|
2014-06-07 00:52:15 +04:00
|
|
|
void SetServerCertificate(PromiseId aPromiseId,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aCert);
|
2014-06-07 00:52:15 +04:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Sends an update to the CDM.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
2014-08-27 12:46:56 +04:00
|
|
|
// Assumes ownership of (Move()s) aResponse's contents.
|
2014-06-07 00:52:15 +04:00
|
|
|
void UpdateSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId,
|
2014-08-27 12:46:56 +04:00
|
|
|
nsTArray<uint8_t>& aResponse);
|
2014-06-07 00:52:15 +04: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().
|
|
|
|
void CloseSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
// Removes all data for a persisent session.
|
|
|
|
// Calls MediaKeys->ResolvePromise(aPromiseId) after the CDM has
|
|
|
|
// processed the request.
|
|
|
|
void RemoveSession(const nsAString& aSessionId,
|
|
|
|
PromiseId aPromiseId);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
void Shutdown();
|
|
|
|
|
2014-12-17 04:01:00 +03:00
|
|
|
// Main thread only.
|
|
|
|
void Terminated();
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
// Threadsafe.
|
2014-10-13 02:53:43 +04:00
|
|
|
const nsCString& GetNodeId() const;
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
// Main thread only.
|
2015-01-09 04:30:07 +03:00
|
|
|
void OnSetSessionId(uint32_t aCreateSessionToken,
|
|
|
|
const nsAString& aSessionId);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
// Main thread only.
|
|
|
|
void OnResolveLoadSessionPromise(uint32_t aPromiseId, bool aSuccess);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
// Main thread only.
|
|
|
|
void OnSessionMessage(const nsAString& aSessionId,
|
2015-01-15 01:25:47 +03:00
|
|
|
GMPSessionMessageType aMessageType,
|
|
|
|
nsTArray<uint8_t>& aMessage);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
void OnExpirationChange(const nsAString& aSessionId,
|
|
|
|
GMPTimestamp aExpiryTime);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
void OnSessionClosed(const nsAString& aSessionId);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
void OnSessionError(const nsAString& aSessionId,
|
|
|
|
nsresult aException,
|
|
|
|
uint32_t aSystemCode,
|
|
|
|
const nsAString& aMsg);
|
|
|
|
|
|
|
|
// Main thread only.
|
|
|
|
void OnRejectPromise(uint32_t aPromiseId,
|
|
|
|
nsresult aDOMException,
|
|
|
|
const nsAString& aMsg);
|
|
|
|
|
|
|
|
// Threadsafe.
|
2015-04-15 03:14:49 +03:00
|
|
|
void Decrypt(MediaRawData* aSample,
|
|
|
|
DecryptionClient* aSink,
|
|
|
|
MediaTaskQueue* aTaskQueue);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
// Reject promise with DOMException corresponding to aExceptionCode.
|
|
|
|
// Can be called from any thread.
|
|
|
|
void RejectPromise(PromiseId aId, nsresult aExceptionCode);
|
|
|
|
|
|
|
|
// Resolves promise with "undefined".
|
|
|
|
// Can be called from any thread.
|
|
|
|
void ResolvePromise(PromiseId aId);
|
|
|
|
|
|
|
|
// Threadsafe.
|
|
|
|
const nsString& KeySystem() const;
|
|
|
|
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_Decrypted(uint32_t aId,
|
|
|
|
GMPErr aResult,
|
|
|
|
const nsTArray<uint8_t>& aDecryptedData);
|
|
|
|
|
|
|
|
CDMCaps& Capabilites();
|
|
|
|
|
2014-10-15 12:33:18 +04:00
|
|
|
// Main thread only.
|
2015-02-03 11:53:36 +03:00
|
|
|
void OnKeyStatusesChange(const nsAString& aSessionId);
|
2014-10-15 12:33:18 +04:00
|
|
|
|
2015-03-26 12:57:36 +03:00
|
|
|
void GetSessionIdsForKeyId(const nsTArray<uint8_t>& aKeyId,
|
|
|
|
nsTArray<nsCString>& aSessionIds);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool IsOnGMPThread();
|
|
|
|
#endif
|
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
private:
|
2015-02-10 13:48:29 +03:00
|
|
|
friend class gmp_InitDoneCallback;
|
2015-02-10 13:48:52 +03:00
|
|
|
friend class gmp_InitGetGMPDecryptorCallback;
|
2014-06-07 00:52:15 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
struct InitData {
|
|
|
|
uint32_t mPromiseId;
|
|
|
|
nsAutoString mOrigin;
|
|
|
|
nsAutoString mTopLevelOrigin;
|
|
|
|
bool mInPrivateBrowsing;
|
|
|
|
};
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
// GMP thread only.
|
2015-02-10 13:48:29 +03:00
|
|
|
void gmp_Init(nsAutoPtr<InitData>&& aData);
|
|
|
|
void gmp_InitDone(GMPDecryptorProxy* aCDM, nsAutoPtr<InitData>&& aData);
|
2015-02-10 13:48:52 +03:00
|
|
|
void gmp_InitGetGMPDecryptor(nsresult aResult,
|
|
|
|
const nsACString& aNodeId,
|
|
|
|
nsAutoPtr<InitData>&& aData);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-08-08 06:43:54 +04:00
|
|
|
// GMP thread only.
|
|
|
|
void gmp_Shutdown();
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
// Main thread only.
|
|
|
|
void OnCDMCreated(uint32_t aPromiseId);
|
|
|
|
|
|
|
|
struct CreateSessionData {
|
|
|
|
dom::SessionType mSessionType;
|
2015-01-09 04:30:07 +03:00
|
|
|
uint32_t mCreateSessionToken;
|
2014-07-30 10:53:28 +04:00
|
|
|
PromiseId mPromiseId;
|
|
|
|
nsAutoCString mInitDataType;
|
|
|
|
nsTArray<uint8_t> mInitData;
|
|
|
|
};
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_CreateSession(nsAutoPtr<CreateSessionData> aData);
|
|
|
|
|
|
|
|
struct SessionOpData {
|
|
|
|
PromiseId mPromiseId;
|
|
|
|
nsAutoCString mSessionId;
|
|
|
|
};
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_LoadSession(nsAutoPtr<SessionOpData> aData);
|
|
|
|
|
|
|
|
struct SetServerCertificateData {
|
|
|
|
PromiseId mPromiseId;
|
|
|
|
nsTArray<uint8_t> mCert;
|
|
|
|
};
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_SetServerCertificate(nsAutoPtr<SetServerCertificateData> aData);
|
|
|
|
|
|
|
|
struct UpdateSessionData {
|
|
|
|
PromiseId mPromiseId;
|
|
|
|
nsAutoCString mSessionId;
|
|
|
|
nsTArray<uint8_t> mResponse;
|
|
|
|
};
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_UpdateSession(nsAutoPtr<UpdateSessionData> aData);
|
|
|
|
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_CloseSession(nsAutoPtr<SessionOpData> aData);
|
|
|
|
|
|
|
|
// GMP thread only.
|
|
|
|
void gmp_RemoveSession(nsAutoPtr<SessionOpData> aData);
|
|
|
|
|
2015-04-15 03:14:49 +03:00
|
|
|
class DecryptJob : public nsRunnable {
|
|
|
|
public:
|
|
|
|
explicit DecryptJob(MediaRawData* aSample,
|
|
|
|
DecryptionClient* aClient,
|
|
|
|
MediaTaskQueue* aTaskQueue)
|
2014-07-30 10:53:28 +04:00
|
|
|
: mId(0)
|
|
|
|
, mSample(aSample)
|
|
|
|
, mClient(aClient)
|
2015-04-15 03:14:49 +03:00
|
|
|
, mResult(GMPGenericErr)
|
|
|
|
, mTaskQueue(aTaskQueue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mClient);
|
|
|
|
MOZ_ASSERT(mSample);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD Run() override;
|
|
|
|
void PostResult(GMPErr aResult, const nsTArray<uint8_t>& aDecryptedData);
|
|
|
|
void PostResult(GMPErr aResult);
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
uint32_t mId;
|
2015-04-09 14:14:55 +03:00
|
|
|
nsRefPtr<MediaRawData> mSample;
|
2015-04-15 03:14:49 +03:00
|
|
|
private:
|
|
|
|
~DecryptJob() {}
|
2014-07-30 10:53:28 +04:00
|
|
|
nsAutoPtr<DecryptionClient> mClient;
|
2015-04-15 03:14:49 +03:00
|
|
|
GMPErr mResult;
|
|
|
|
nsRefPtr<MediaTaskQueue> mTaskQueue;
|
2014-07-30 10:53:28 +04:00
|
|
|
};
|
|
|
|
// GMP thread only.
|
2015-04-15 03:14:49 +03:00
|
|
|
void gmp_Decrypt(nsRefPtr<DecryptJob> aJob);
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
class RejectPromiseTask : public nsRunnable {
|
|
|
|
public:
|
|
|
|
RejectPromiseTask(CDMProxy* aProxy,
|
|
|
|
PromiseId aId,
|
|
|
|
nsresult aCode)
|
|
|
|
: mProxy(aProxy)
|
|
|
|
, mId(aId)
|
|
|
|
, mCode(aCode)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_METHOD Run() {
|
|
|
|
mProxy->RejectPromise(mId, mCode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsRefPtr<CDMProxy> mProxy;
|
|
|
|
PromiseId mId;
|
|
|
|
nsresult mCode;
|
|
|
|
};
|
|
|
|
|
|
|
|
~CDMProxy();
|
|
|
|
|
|
|
|
// Helper to enforce that a raw pointer is only accessed on the main thread.
|
|
|
|
template<class Type>
|
|
|
|
class MainThreadOnlyRawPtr {
|
|
|
|
public:
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit MainThreadOnlyRawPtr(Type* aPtr)
|
2014-06-07 00:52:15 +04: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 23:18:38 +03:00
|
|
|
Type* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN {
|
2014-06-07 00:52:15 +04: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;
|
|
|
|
|
|
|
|
const nsAutoString mKeySystem;
|
|
|
|
|
|
|
|
// Gecko Media Plugin thread. All interactions with the out-of-process
|
|
|
|
// EME plugin must come from this thread.
|
|
|
|
nsRefPtr<nsIThread> mGMPThread;
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-10-13 02:53:43 +04:00
|
|
|
nsCString mNodeId;
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
GMPDecryptorProxy* mCDM;
|
|
|
|
CDMCaps mCapabilites;
|
|
|
|
nsAutoPtr<CDMCallbackProxy> mCallback;
|
|
|
|
|
|
|
|
// Decryption jobs sent to CDM, awaiting result.
|
|
|
|
// GMP thread only.
|
2015-04-15 03:14:49 +03:00
|
|
|
nsTArray<nsRefPtr<DecryptJob>> mDecryptionJobs;
|
2014-07-30 10:53:28 +04:00
|
|
|
|
|
|
|
// Number of buffers we've decrypted. Used to uniquely identify
|
|
|
|
// decryption jobs sent to CDM. Note we can't just use the length of
|
|
|
|
// mDecryptionJobs as that shrinks as jobs are completed and removed
|
|
|
|
// from it.
|
|
|
|
// GMP thread only.
|
|
|
|
uint32_t mDecryptionJobCount;
|
2015-02-10 13:48:29 +03:00
|
|
|
|
|
|
|
// True if CDMProxy::gmp_Shutdown was called.
|
|
|
|
// GMP thread only.
|
|
|
|
bool mShutdownCalled;
|
2014-06-07 00:52:15 +04:00
|
|
|
};
|
|
|
|
|
2014-07-30 10:53:28 +04:00
|
|
|
|
2014-06-07 00:52:15 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // CDMProxy_h_
|