Line data Source code
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 mozilla_dom_mediakeys_h__
8 : #define mozilla_dom_mediakeys_h__
9 :
10 : #include "nsWrapperCache.h"
11 : #include "nsISupports.h"
12 : #include "mozilla/Attributes.h"
13 : #include "mozilla/RefPtr.h"
14 : #include "nsCOMPtr.h"
15 : #include "nsCycleCollectionParticipant.h"
16 : #include "nsRefPtrHashtable.h"
17 : #include "mozilla/dom/Promise.h"
18 : #include "mozilla/dom/MediaKeysBinding.h"
19 : #include "mozilla/dom/MediaKeySystemAccessBinding.h"
20 : #include "mozIGeckoMediaPluginService.h"
21 : #include "mozilla/DetailedPromise.h"
22 : #include "mozilla/WeakPtr.h"
23 :
24 : namespace mozilla {
25 :
26 : class CDMProxy;
27 :
28 : namespace dom {
29 :
30 : class ArrayBufferViewOrArrayBuffer;
31 : class MediaKeySession;
32 : class HTMLMediaElement;
33 :
34 : typedef nsRefPtrHashtable<nsStringHashKey, MediaKeySession> KeySessionHashMap;
35 : typedef nsRefPtrHashtable<nsUint32HashKey, dom::DetailedPromise> PromiseHashMap;
36 : typedef nsRefPtrHashtable<nsUint32HashKey, MediaKeySession> PendingKeySessionsHashMap;
37 : typedef nsDataHashtable<nsUint32HashKey, uint32_t> PendingPromiseIdTokenHashMap;
38 : typedef uint32_t PromiseId;
39 :
40 : // This class is used on the main thread only.
41 : // Note: its addref/release is not (and can't be) thread safe!
42 : class MediaKeys final : public nsISupports,
43 : public nsWrapperCache,
44 : public SupportsWeakPtr<MediaKeys>
45 : {
46 : ~MediaKeys();
47 :
48 : public:
49 : NS_DECL_CYCLE_COLLECTING_ISUPPORTS
50 0 : NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MediaKeys)
51 0 : MOZ_DECLARE_WEAKREFERENCE_TYPENAME(MediaKeys)
52 :
53 : MediaKeys(nsPIDOMWindowInner* aParentWindow,
54 : const nsAString& aKeySystem,
55 : const MediaKeySystemConfiguration& aConfig);
56 :
57 : already_AddRefed<DetailedPromise> Init(ErrorResult& aRv);
58 :
59 : nsPIDOMWindowInner* GetParentObject() const;
60 :
61 : JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
62 :
63 : nsresult Bind(HTMLMediaElement* aElement);
64 : void Unbind();
65 :
66 : // Javascript: readonly attribute DOMString keySystem;
67 : void GetKeySystem(nsString& retval) const;
68 :
69 : // JavaScript: MediaKeys.createSession()
70 : already_AddRefed<MediaKeySession> CreateSession(JSContext* aCx,
71 : MediaKeySessionType aSessionType,
72 : ErrorResult& aRv);
73 :
74 : // JavaScript: MediaKeys.SetServerCertificate()
75 : already_AddRefed<DetailedPromise>
76 : SetServerCertificate(const ArrayBufferViewOrArrayBuffer& aServerCertificate,
77 : ErrorResult& aRv);
78 :
79 : already_AddRefed<MediaKeySession> GetSession(const nsAString& aSessionId);
80 :
81 : // Removes and returns MediaKeySession from the set of sessions awaiting
82 : // their sessionId to be assigned.
83 : already_AddRefed<MediaKeySession> GetPendingSession(uint32_t aToken);
84 :
85 : // Called once a Init() operation succeeds.
86 : void OnCDMCreated(PromiseId aId, const uint32_t aPluginId);
87 :
88 : // Called once the CDM generates a sessionId while servicing a
89 : // MediaKeySession.generateRequest() or MediaKeySession.load() call,
90 : // once the sessionId of a MediaKeySession is known.
91 : void OnSessionIdReady(MediaKeySession* aSession);
92 :
93 : // Called once a LoadSession succeeds.
94 : void OnSessionLoaded(PromiseId aId, bool aSuccess);
95 :
96 : // Called once a session has closed.
97 : void OnSessionClosed(MediaKeySession* aSession);
98 :
99 0 : CDMProxy* GetCDMProxy() { return mProxy; }
100 :
101 : // Makes a new promise, or nullptr on failure.
102 : already_AddRefed<DetailedPromise> MakePromise(ErrorResult& aRv,
103 : const nsACString& aName);
104 : // Stores promise in mPromises, returning an ID that can be used to retrieve
105 : // it later. The ID is passed to the CDM, so that it can signal specific
106 : // promises to be resolved.
107 : PromiseId StorePromise(DetailedPromise* aPromise);
108 :
109 : // Stores a map from promise id to pending session token. Using this
110 : // mapping, when a promise is rejected via its ID, we can check if the
111 : // promise corresponds to a pending session and retrieve that session
112 : // via the mapped-to token, and remove the pending session from the
113 : // list of sessions awaiting a session id.
114 : void ConnectPendingPromiseIdWithToken(PromiseId aId, uint32_t aToken);
115 :
116 : // Reject promise with DOMException corresponding to aExceptionCode.
117 : void RejectPromise(PromiseId aId, nsresult aExceptionCode,
118 : const nsCString& aReason);
119 : // Resolves promise with "undefined".
120 : void ResolvePromise(PromiseId aId);
121 :
122 : void Shutdown();
123 :
124 : // Called by CDMProxy when CDM crashes or shuts down. It is different from
125 : // Shutdown which is called from the script/dom side.
126 : void Terminated();
127 :
128 : // Returns true if this MediaKeys has been bound to a media element.
129 : bool IsBoundToMediaElement() const;
130 :
131 : void GetSessionsInfo(nsString& sessionsInfo);
132 :
133 : private:
134 :
135 : // Instantiate CDMProxy instance.
136 : // It could be MediaDrmCDMProxy (Widevine on Fennec) or GMPCDMProxy (the rest).
137 : already_AddRefed<CDMProxy> CreateCDMProxy(nsIEventTarget* aMainThread);
138 :
139 : // Removes promise from mPromises, and returns it.
140 : already_AddRefed<DetailedPromise> RetrievePromise(PromiseId aId);
141 :
142 : // Owning ref to proxy. The proxy has a weak reference back to the MediaKeys,
143 : // and the MediaKeys destructor clears the proxy's reference to the MediaKeys.
144 : RefPtr<CDMProxy> mProxy;
145 :
146 : RefPtr<HTMLMediaElement> mElement;
147 :
148 : nsCOMPtr<nsPIDOMWindowInner> mParent;
149 : const nsString mKeySystem;
150 : KeySessionHashMap mKeySessions;
151 : PromiseHashMap mPromises;
152 : PendingKeySessionsHashMap mPendingSessions;
153 : PromiseId mCreatePromiseId;
154 :
155 : RefPtr<nsIPrincipal> mPrincipal;
156 : RefPtr<nsIPrincipal> mTopLevelPrincipal;
157 :
158 : const MediaKeySystemConfiguration mConfig;
159 :
160 : PendingPromiseIdTokenHashMap mPromiseIdToken;
161 : };
162 :
163 : } // namespace dom
164 : } // namespace mozilla
165 :
166 : #endif // mozilla_dom_mediakeys_h__
|