1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef MediaDrmCDMProxy_h_
8 #define MediaDrmCDMProxy_h_
9 
10 #include <jni.h>
11 #include "mozilla/jni/Types.h"
12 #include "GeneratedJNINatives.h"
13 #include "mozilla/CDMProxy.h"
14 #include "mozilla/CDMCaps.h"
15 #include "mozilla/dom/MediaKeys.h"
16 #include "mozilla/dom/MediaKeySession.h"
17 #include "mozilla/MediaDrmProxySupport.h"
18 #include "mozilla/UniquePtr.h"
19 
20 #include "MediaCodec.h"
21 #include "nsString.h"
22 
23 using namespace mozilla::java;
24 
25 namespace mozilla {
26 
27 class MediaDrmCDMCallbackProxy;
28 class MediaDrmCDMProxy : public CDMProxy {
29  public:
30   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaDrmCDMProxy, override)
31 
32   MediaDrmCDMProxy(dom::MediaKeys* aKeys, const nsAString& aKeySystem,
33                    bool aDistinctiveIdentifierRequired,
34                    bool aPersistentStateRequired,
35                    nsISerialEventTarget* aMainThread);
36 
37   void Init(PromiseId aPromiseId, const nsAString& aOrigin,
38             const nsAString& aTopLevelOrigin,
39             const nsAString& aGMPName) override;
40 
41   void CreateSession(uint32_t aCreateSessionToken,
42                      MediaKeySessionType aSessionType, PromiseId aPromiseId,
43                      const nsAString& aInitDataType,
44                      nsTArray<uint8_t>& aInitData) override;
45 
46   void LoadSession(PromiseId aPromiseId, dom::MediaKeySessionType aSessionType,
47                    const nsAString& aSessionId) override;
48 
49   void SetServerCertificate(PromiseId aPromiseId,
50                             nsTArray<uint8_t>& aCert) override;
51 
52   void UpdateSession(const nsAString& aSessionId, PromiseId aPromiseId,
53                      nsTArray<uint8_t>& aResponse) override;
54 
55   void CloseSession(const nsAString& aSessionId, PromiseId aPromiseId) override;
56 
57   void RemoveSession(const nsAString& aSessionId,
58                      PromiseId aPromiseId) override;
59 
60   void Shutdown() override;
61 
62   void Terminated() override;
63 
64   const nsCString& GetNodeId() const override;
65 
66   void OnSetSessionId(uint32_t aCreateSessionToken,
67                       const nsAString& aSessionId) override;
68 
69   void OnResolveLoadSessionPromise(uint32_t aPromiseId, bool aSuccess) override;
70 
71   void OnSessionMessage(const nsAString& aSessionId,
72                         dom::MediaKeyMessageType aMessageType,
73                         const nsTArray<uint8_t>& aMessage) override;
74 
75   void OnExpirationChange(const nsAString& aSessionId,
76                           UnixTime aExpiryTime) override;
77 
78   void OnSessionClosed(const nsAString& aSessionId) override;
79 
80   void OnSessionError(const nsAString& aSessionId, nsresult aException,
81                       uint32_t aSystemCode, const nsAString& aMsg) override;
82 
83   void OnRejectPromise(uint32_t aPromiseId, nsresult aCode,
84                        const nsCString& aMsg) override;
85 
86   RefPtr<DecryptPromise> Decrypt(MediaRawData* aSample) override;
87   void OnDecrypted(uint32_t aId, DecryptStatus aResult,
88                    const nsTArray<uint8_t>& aDecryptedData) override;
89 
90   void RejectPromise(PromiseId aId, nsresult aCode,
91                      const nsCString& aReason) override;
92 
93   // Resolves promise with "undefined".
94   // Can be called from any thread.
95   void ResolvePromise(PromiseId aId) override;
96 
97   // Threadsafe.
98   const nsString& KeySystem() const override;
99 
100   DataMutex<CDMCaps>& Capabilites() override;
101 
102   void OnKeyStatusesChange(const nsAString& aSessionId) override;
103 
104   void GetStatusForPolicy(PromiseId aPromiseId,
105                           const nsAString& aMinHdcpVersion) override;
106 
107 #ifdef DEBUG
108   bool IsOnOwnerThread() override;
109 #endif
110 
111   const nsString& GetMediaDrmStubId() const;
112 
113  private:
114   virtual ~MediaDrmCDMProxy();
115 
116   void OnCDMCreated(uint32_t aPromiseId);
117 
118   struct CreateSessionData {
119     MediaKeySessionType mSessionType;
120     uint32_t mCreateSessionToken;
121     PromiseId mPromiseId;
122     nsCString mInitDataType;
123     nsTArray<uint8_t> mInitData;
124   };
125 
126   struct UpdateSessionData {
127     PromiseId mPromiseId;
128     nsCString mSessionId;
129     nsTArray<uint8_t> mResponse;
130   };
131 
132   struct SessionOpData {
133     PromiseId mPromiseId;
134     nsCString mSessionId;
135   };
136 
137   class RejectPromiseTask : public Runnable {
138    public:
RejectPromiseTask(MediaDrmCDMProxy * aProxy,PromiseId aId,nsresult aCode,const nsCString & aReason)139     RejectPromiseTask(MediaDrmCDMProxy* aProxy, PromiseId aId, nsresult aCode,
140                       const nsCString& aReason)
141         : Runnable("RejectPromiseTask"),
142           mProxy(aProxy),
143           mId(aId),
144           mCode(aCode),
145           mReason(aReason) {}
Run()146     NS_IMETHOD Run() override {
147       mProxy->RejectPromise(mId, mCode, mReason);
148       return NS_OK;
149     }
150 
151    private:
152     RefPtr<MediaDrmCDMProxy> mProxy;
153     PromiseId mId;
154     nsresult mCode;
155     nsCString mReason;
156   };
157 
158   nsCString mNodeId;
159   UniquePtr<MediaDrmProxySupport> mCDM;
160   UniquePtr<MediaDrmCDMCallbackProxy> mCallback;
161   bool mShutdownCalled;
162 
163   // =====================================================================
164   // For MediaDrmProxySupport
165   void md_Init(uint32_t aPromiseId);
166   void md_CreateSession(UniquePtr<CreateSessionData>&& aData);
167   void md_UpdateSession(UniquePtr<UpdateSessionData>&& aData);
168   void md_CloseSession(UniquePtr<SessionOpData>&& aData);
169   void md_Shutdown();
170   // =====================================================================
171 };
172 
173 }  // namespace mozilla
174 
175 #endif  // MediaDrmCDMProxy_h_
176