2 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef MediaKeySession_h
27 #define MediaKeySession_h
29 #include "bindings/core/v8/ScriptPromiseProperty.h"
30 #include "core/dom/ActiveDOMObject.h"
31 #include "core/dom/DOMException.h"
32 #include "modules/EventTargetModules.h"
33 #include "platform/Timer.h"
34 #include "platform/heap/Handle.h"
35 #include "public/platform/WebContentDecryptionModuleSession.h"
36 #include "wtf/Forward.h"
42 class GenericEventQueue;
45 class WebContentDecryptionModule;
48 // References are held by JS only. However, even if all JS references are
49 // dropped, it won't be garbage collected until close event received or
50 // MediaKeys goes away (as determined by the validity of a WeakPtr). This allows
51 // the CDM to continue to fire events for this session, as long as the session
54 // WeakPtr<MediaKeys> is used instead of having MediaKeys and MediaKeySession
55 // keep references to each other, and then having to inform the other object
56 // when it gets destroyed.
58 // Because this object controls the lifetime of the WebContentDecryptionModuleSession,
59 // it may outlive any JavaScript references as long as the MediaKeys object is alive.
60 // The WebContentDecryptionModuleSession has the same lifetime as this object.
61 class MediaKeySession FINAL
62 : public RefCountedGarbageCollectedWillBeGarbageCollectedFinalized<MediaKeySession>, public ActiveDOMObject, public EventTargetWithInlineData
63 , private WebContentDecryptionModuleSession::Client {
64 DEFINE_EVENT_TARGET_REFCOUNTING_WILL_BE_REMOVED(RefCountedGarbageCollected<MediaKeySession>);
65 DEFINE_WRAPPERTYPEINFO();
66 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaKeySession);
68 static MediaKeySession* create(ScriptState*, MediaKeys*, const String& sessionType);
69 virtual ~MediaKeySession();
71 const String& keySystem() const { return m_keySystem; }
72 String sessionId() const;
73 ScriptPromise closed(ScriptState*);
75 ScriptPromise generateRequest(ScriptState*, const String& initDataType, ArrayBuffer* initData);
76 ScriptPromise generateRequest(ScriptState*, const String& initDataType, ArrayBufferView* initData);
78 void setError(MediaKeyError*);
79 MediaKeyError* error() { return m_error.get(); }
81 ScriptPromise update(ScriptState*, ArrayBuffer* response);
82 ScriptPromise update(ScriptState*, ArrayBufferView* response);
83 ScriptPromise release(ScriptState*);
85 void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
88 virtual const AtomicString& interfaceName() const OVERRIDE;
89 virtual ExecutionContext* executionContext() const OVERRIDE;
92 virtual bool hasPendingActivity() const OVERRIDE;
93 virtual void stop() OVERRIDE;
95 virtual void trace(Visitor*) OVERRIDE;
99 friend class NewSessionResult;
101 MediaKeySession(ScriptState*, MediaKeys*, const String& sessionType);
103 void actionTimerFired(Timer<MediaKeySession>*);
105 // WebContentDecryptionModuleSession::Client
106 virtual void message(const unsigned char* message, size_t messageLength, const WebURL& destinationURL) OVERRIDE;
107 virtual void ready() OVERRIDE;
108 virtual void close() OVERRIDE;
109 virtual void error(MediaKeyErrorCode, unsigned long systemCode) OVERRIDE;
110 virtual void error(WebContentDecryptionModuleException, unsigned long systemCode, const WebString& errorMessage) OVERRIDE;
112 ScriptPromise generateRequestInternal(ScriptState*, const String& initDataType, PassRefPtr<ArrayBuffer> initData);
113 ScriptPromise updateInternal(ScriptState*, PassRefPtr<ArrayBuffer> response);
115 // Called by NewSessionResult when the new sesison has been created.
116 void finishGenerateRequest();
119 RefPtrWillBeMember<MediaKeyError> m_error;
120 OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
121 OwnPtr<WebContentDecryptionModuleSession> m_session;
123 // Used to determine if MediaKeys is still active.
124 WeakMember<MediaKeys> m_mediaKeys;
126 // Session properties.
127 String m_sessionType;
130 bool m_isUninitialized;
132 bool m_isClosed; // Is the CDM finished with this session?
134 // Keep track of the closed promise.
135 typedef ScriptPromiseProperty<Member<MediaKeySession>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
136 Member<ClosedPromise> m_closedPromise;
138 HeapDeque<Member<PendingAction> > m_pendingActions;
139 Timer<MediaKeySession> m_actionTimer;
144 #endif // MediaKeySession_h