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 "ActiveDOMObject.h"
32#include "CDMSession.h"
33#include "EventTarget.h"
34#include "ExceptionCode.h"
35#include "GenericEventQueue.h"
36#include "Timer.h"
37#include <runtime/Uint8Array.h>
38#include <wtf/Deque.h>
39#include <wtf/PassRefPtr.h>
40#include <wtf/RefCounted.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class MediaKeyError;
46class MediaKeys;
47
48class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTargetWithInlineData, public ActiveDOMObject, public CDMSessionClient {
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
65    bool isClosed() const { return !m_session; }
66    void close();
67
68    using RefCounted<MediaKeySession>::ref;
69    using RefCounted<MediaKeySession>::deref;
70
71    void enqueueEvent(PassRefPtr<Event>);
72
73    // ActiveDOMObject
74    virtual bool hasPendingActivity() const override { return (m_keys && !isClosed()) || m_asyncEventQueue.hasPendingEvents(); }
75
76    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyadded);
77    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeyerror);
78    DEFINE_ATTRIBUTE_EVENT_LISTENER(webkitkeymessage);
79
80    virtual EventTargetInterface eventTargetInterface() const override { return MediaKeySessionEventTargetInterfaceType; }
81    virtual ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
82
83protected:
84    MediaKeySession(ScriptExecutionContext*, MediaKeys*, const String& keySystem);
85    void keyRequestTimerFired(Timer<MediaKeySession>&);
86    void addKeyTimerFired(Timer<MediaKeySession>&);
87
88    // CDMSessionClient
89    virtual void sendMessage(Uint8Array*, String destinationURL);
90    virtual void sendError(MediaKeyErrorCode, unsigned long systemCode);
91
92    MediaKeys* m_keys;
93    String m_keySystem;
94    String m_sessionId;
95    RefPtr<MediaKeyError> m_error;
96    GenericEventQueue m_asyncEventQueue;
97    std::unique_ptr<CDMSession> m_session;
98
99    struct PendingKeyRequest {
100        PendingKeyRequest(const String& mimeType, Uint8Array* initData) : mimeType(mimeType), initData(initData) { }
101        String mimeType;
102        RefPtr<Uint8Array> initData;
103    };
104    Deque<PendingKeyRequest> m_pendingKeyRequests;
105    Timer<MediaKeySession> m_keyRequestTimer;
106
107    Deque<RefPtr<Uint8Array>> m_pendingKeys;
108    Timer<MediaKeySession> m_addKeyTimer;
109
110private:
111    virtual void refEventTarget() override { ref(); }
112    virtual void derefEventTarget() override { deref(); }
113};
114
115}
116
117#endif // ENABLE(ENCRYPTED_MEDIA_V2)
118
119#endif // MediaKeySession_h
120