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 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MediaKeySession);
67 static ScriptPromise create(ScriptState*, MediaKeys*, const String& initDataType, PassRefPtr<ArrayBuffer> initData, const String& sessionType);
68 virtual ~MediaKeySession();
70 const String& keySystem() const { return m_keySystem; }
71 String sessionId() const;
72 ScriptPromise closed(ScriptState*);
74 void setError(MediaKeyError*);
75 MediaKeyError* error() { return m_error.get(); }
77 ScriptPromise update(ScriptState*, ArrayBuffer* response);
78 ScriptPromise update(ScriptState*, ArrayBufferView* response);
79 ScriptPromise release(ScriptState*);
81 void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
84 virtual const AtomicString& interfaceName() const OVERRIDE;
85 virtual ExecutionContext* executionContext() const OVERRIDE;
88 virtual bool hasPendingActivity() const OVERRIDE;
89 virtual void stop() OVERRIDE;
91 virtual void trace(Visitor*) OVERRIDE;
95 friend class MediaKeySessionInitializer;
97 MediaKeySession(ExecutionContext*, MediaKeys*, PassOwnPtr<WebContentDecryptionModuleSession>);
98 void actionTimerFired(Timer<MediaKeySession>*);
100 // WebContentDecryptionModuleSession::Client
101 virtual void message(const unsigned char* message, size_t messageLength, const WebURL& destinationURL) OVERRIDE;
102 virtual void ready() OVERRIDE;
103 virtual void close() OVERRIDE;
104 virtual void error(MediaKeyErrorCode, unsigned long systemCode) OVERRIDE;
105 virtual void error(WebContentDecryptionModuleException, unsigned long systemCode, const WebString& errorMessage) OVERRIDE;
107 ScriptPromise updateInternal(ScriptState*, PassRefPtr<ArrayBuffer> response);
110 RefPtrWillBeMember<MediaKeyError> m_error;
111 OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
112 OwnPtr<WebContentDecryptionModuleSession> m_session;
114 // Used to determine if MediaKeys is still active.
115 WeakMember<MediaKeys> m_keys;
117 // Is the CDM finished with this session?
120 // Keep track of the closed promise.
121 typedef ScriptPromiseProperty<Member<MediaKeySession>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
122 Member<ClosedPromise> m_closedPromise;
124 HeapDeque<Member<PendingAction> > m_pendingActions;
125 Timer<MediaKeySession> m_actionTimer;
130 #endif // MediaKeySession_h