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#if ENABLE(ENCRYPTED_MEDIA_V2)
30
31#include "ContextDestructionObserver.h"
32#include "EventTarget.h"
33#include "ExceptionCode.h"
34#include "Timer.h"
35#include <wtf/Deque.h>
36#include <wtf/PassRefPtr.h>
37#include <wtf/RefCounted.h>
38#include <wtf/Uint8Array.h>
39#include <wtf/text/WTFString.h>
40
41namespace WebCore {
42
43class GenericEventQueue;
44class MediaKeyError;
45class MediaKeys;
46class CDMSession;
47
48class MediaKeySession : public RefCounted<MediaKeySession>, public EventTarget, public ContextDestructionObserver {
49public:
50    static PassRefPtr<MediaKeySession> create(ScriptExecutionContext*, MediaKeys*, const String& keySystem);
51    ~MediaKeySession();
52
53    const String& keySystem() const { return m_keySystem; }
54    const String& sessionId() const;
55
56    void setError(MediaKeyError*);
57    MediaKeyError* error() { return m_error.get(); }
58
59    void setKeys(MediaKeys* keys) { m_keys = keys; }
60    MediaKeys* keys() const { return m_keys; }
61
62    void generateKeyRequest(const String& mimeType, Uint8Array* initData);
63    void update(Uint8Array* key, ExceptionCode&);
64    void close();
65
66    using RefCounted<MediaKeySession>::ref;
67    using RefCounted<MediaKeySession>::deref;
68
69    void enqueueEvent(PassRefPtr<Event>);
70
71    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyadded);
72    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyerror);
73    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeymessage);
74
75    virtual const AtomicString& interfaceName() const OVERRIDE;
76    virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
77
78protected:
79    MediaKeySession(ScriptExecutionContext*, MediaKeys*, const String& keySystem);
80    void keyRequestTimerFired(Timer<MediaKeySession>*);
81    void addKeyTimerFired(Timer<MediaKeySession>*);
82
83    MediaKeys* m_keys;
84    String m_keySystem;
85    String m_sessionId;
86    RefPtr<MediaKeyError> m_error;
87    OwnPtr<GenericEventQueue> m_asyncEventQueue;
88    OwnPtr<CDMSession> m_session;
89
90    struct PendingKeyRequest {
91        PendingKeyRequest(const String& mimeType, Uint8Array* initData) : mimeType(mimeType), initData(initData) { }
92        String mimeType;
93        RefPtr<Uint8Array> initData;
94    };
95    Deque<PendingKeyRequest> m_pendingKeyRequests;
96    Timer<MediaKeySession> m_keyRequestTimer;
97
98    Deque<RefPtr<Uint8Array> > m_pendingKeys;
99    Timer<MediaKeySession> m_addKeyTimer;
100
101private:
102    virtual void refEventTarget() OVERRIDE { ref(); }
103    virtual void derefEventTarget() OVERRIDE { deref(); }
104
105    virtual EventTargetData* eventTargetData() OVERRIDE { return &m_eventTargetData; }
106    virtual EventTargetData* ensureEventTargetData() OVERRIDE { return &m_eventTargetData; }
107
108    EventTargetData m_eventTargetData;
109};
110
111}
112
113#endif // ENABLE(ENCRYPTED_MEDIA_V2)
114
115#endif // MediaKeySession_h
116