MediaKeySession.h (4731B)
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_MediaKeySession_h 8 #define mozilla_dom_MediaKeySession_h 9 10 #include "DecoderDoctorLogger.h" 11 #include "mozilla/DOMEventTargetHelper.h" 12 #include "mozilla/DetailedPromise.h" 13 #include "mozilla/Mutex.h" 14 #include "mozilla/dom/MediaKeyMessageEventBinding.h" 15 #include "mozilla/dom/MediaKeySessionBinding.h" 16 #include "mozilla/dom/MediaKeysBinding.h" 17 #include "mozilla/dom/Promise.h" 18 #include "mozilla/dom/TypedArray.h" 19 #include "nsCOMPtr.h" 20 #include "nsCycleCollectionParticipant.h" 21 22 struct JSContext; 23 24 namespace mozilla { 25 class ErrorResult; 26 27 namespace dom { 28 class MediaKeySession; 29 } // namespace dom 30 DDLoggedTypeName(dom::MediaKeySession); 31 32 namespace dom { 33 34 class ArrayBufferViewOrArrayBuffer; 35 class MediaKeyError; 36 class MediaKeyStatusMap; 37 38 nsString ToString(MediaKeySessionType aType); 39 40 class MediaKeySession final : public DOMEventTargetHelper, 41 public DecoderDoctorLifeLogger<MediaKeySession> { 42 public: 43 NS_DECL_ISUPPORTS_INHERITED 44 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaKeySession, 45 DOMEventTargetHelper) 46 public: 47 MediaKeySession(nsPIDOMWindowInner* aParent, MediaKeys* aKeys, 48 const nsAString& aKeySystem, MediaKeySessionType aSessionType, 49 bool aHardwareDecryption, ErrorResult& aRv); 50 51 void SetSessionId(const nsAString& aSessionId); 52 53 JSObject* WrapObject(JSContext* aCx, 54 JS::Handle<JSObject*> aGivenProto) override; 55 56 // Mark this as resultNotAddRefed to return raw pointers 57 MediaKeyError* GetError() const; 58 59 MediaKeyStatusMap* KeyStatuses() const; 60 61 void GetSessionId(nsString& aRetval) const; 62 63 const nsString& GetSessionId() const; 64 65 // Number of ms since epoch at which expiration occurs, or NaN if unknown. 66 // TODO: The type of this attribute is still under contention. 67 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=25902 68 double Expiration() const; 69 70 Promise* Closed() const; 71 72 already_AddRefed<Promise> GenerateRequest( 73 const nsAString& aInitDataType, 74 const ArrayBufferViewOrArrayBuffer& aInitData, ErrorResult& aRv); 75 76 already_AddRefed<Promise> Load(const nsAString& aSessionId, ErrorResult& aRv); 77 78 already_AddRefed<Promise> Update(const ArrayBufferViewOrArrayBuffer& response, 79 ErrorResult& aRv); 80 81 already_AddRefed<Promise> Close(ErrorResult& aRv); 82 83 already_AddRefed<Promise> Remove(ErrorResult& aRv); 84 85 void DispatchKeyMessage(MediaKeyMessageType aMessageType, 86 const nsTArray<uint8_t>& aMessage); 87 88 void DispatchKeyError(uint32_t system_code); 89 90 void DispatchKeyStatusesChange(); 91 92 void OnClosed(MediaKeySessionClosedReason aReason); 93 94 bool IsClosed() const; 95 96 void SetExpiration(double aExpiry); 97 98 mozilla::dom::EventHandlerNonNull* GetOnkeystatuseschange(); 99 void SetOnkeystatuseschange(mozilla::dom::EventHandlerNonNull* aCallback); 100 101 mozilla::dom::EventHandlerNonNull* GetOnmessage(); 102 void SetOnmessage(mozilla::dom::EventHandlerNonNull* aCallback); 103 104 // Process-unique identifier. 105 uint32_t Token() const; 106 107 private: 108 ~MediaKeySession(); 109 110 void UpdateKeyStatusMap(); 111 112 bool IsCallable() const { 113 // The EME spec sets the "callable value" to true whenever the CDM sets 114 // the sessionId. When the session is initialized, sessionId is empty and 115 // callable is thus false. 116 return !mSessionId.IsEmpty(); 117 } 118 119 already_AddRefed<DetailedPromise> MakePromise(ErrorResult& aRv, 120 const nsACString& aName); 121 122 // EME spec, starting from 6.6.2.7 123 // https://w3c.github.io/encrypted-media/ 124 void CompleteGenerateRequest(const nsString& aInitDataType, 125 nsTArray<uint8_t>& aData, 126 DetailedPromise* aPromise); 127 128 RefPtr<DetailedPromise> mClosed; 129 130 RefPtr<MediaKeyError> mMediaKeyError; 131 RefPtr<MediaKeys> mKeys; 132 const nsString mKeySystem; 133 nsString mSessionId; 134 const MediaKeySessionType mSessionType; 135 const uint32_t mToken; 136 bool mIsClosed; 137 bool mUninitialized; 138 RefPtr<MediaKeyStatusMap> mKeyStatusMap; 139 double mExpiration; 140 141 // True if this key session is related with hardware decryption. 142 bool mHardwareDecryption; 143 144 // True if this media key session is created under a private browsing mode. 145 const bool mIsPrivateBrowsing; 146 }; 147 148 } // namespace dom 149 } // namespace mozilla 150 151 #endif