Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / encryptedmedia / MediaKeySession.h
1 /*
2  * Copyright (C) 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25
26 #ifndef MediaKeySession_h
27 #define MediaKeySession_h
28
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"
37
38 namespace blink {
39
40 class ScriptPromise;
41 class ScriptState;
42 class GenericEventQueue;
43 class MediaKeyError;
44 class MediaKeys;
45 class WebContentDecryptionModule;
46 class WebString;
47
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
52 // is open.
53 //
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.
57 //
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);
66 public:
67     static ScriptPromise create(ScriptState*, MediaKeys*, const String& initDataType, PassRefPtr<ArrayBuffer> initData, const String& sessionType);
68     virtual ~MediaKeySession();
69
70     const String& keySystem() const { return m_keySystem; }
71     String sessionId() const;
72     ScriptPromise closed(ScriptState*);
73
74     void setError(MediaKeyError*);
75     MediaKeyError* error() { return m_error.get(); }
76
77     ScriptPromise update(ScriptState*, ArrayBuffer* response);
78     ScriptPromise update(ScriptState*, ArrayBufferView* response);
79     ScriptPromise release(ScriptState*);
80
81     void enqueueEvent(PassRefPtrWillBeRawPtr<Event>);
82
83     // EventTarget
84     virtual const AtomicString& interfaceName() const OVERRIDE;
85     virtual ExecutionContext* executionContext() const OVERRIDE;
86
87     // ActiveDOMObject
88     virtual bool hasPendingActivity() const OVERRIDE;
89     virtual void stop() OVERRIDE;
90
91     virtual void trace(Visitor*) OVERRIDE;
92
93 private:
94     class PendingAction;
95     friend class MediaKeySessionInitializer;
96
97     MediaKeySession(ExecutionContext*, MediaKeys*, PassOwnPtr<WebContentDecryptionModuleSession>);
98     void actionTimerFired(Timer<MediaKeySession>*);
99
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;
106
107     ScriptPromise updateInternal(ScriptState*, PassRefPtr<ArrayBuffer> response);
108
109     String m_keySystem;
110     RefPtrWillBeMember<MediaKeyError> m_error;
111     OwnPtrWillBeMember<GenericEventQueue> m_asyncEventQueue;
112     OwnPtr<WebContentDecryptionModuleSession> m_session;
113
114     // Used to determine if MediaKeys is still active.
115     WeakMember<MediaKeys> m_keys;
116
117     // Is the CDM finished with this session?
118     bool m_isClosed;
119
120     // Keep track of the closed promise.
121     typedef ScriptPromiseProperty<Member<MediaKeySession>, V8UndefinedType, RefPtrWillBeMember<DOMException> > ClosedPromise;
122     Member<ClosedPromise> m_closedPromise;
123
124     HeapDeque<Member<PendingAction> > m_pendingActions;
125     Timer<MediaKeySession> m_actionTimer;
126 };
127
128 } // namespace blink
129
130 #endif // MediaKeySession_h