1/*
2 * Copyright (C) 2016 Metrological Group B.V.
3 * Copyright (C) 2016 Igalia S.L.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#if ENABLE(ENCRYPTED_MEDIA)
32
33#include "ActiveDOMObject.h"
34#include "CDMInstanceSession.h"
35#include "DOMPromiseProxy.h"
36#include "EventTarget.h"
37#include "GenericEventQueue.h"
38#include "GenericTaskQueue.h"
39#include "MediaKeyMessageType.h"
40#include "MediaKeySessionType.h"
41#include "MediaKeyStatus.h"
42#include <wtf/RefCounted.h>
43#include <wtf/Vector.h>
44#include <wtf/WeakPtr.h>
45#include <wtf/text/WTFString.h>
46
47namespace WebCore {
48
49class BufferSource;
50class CDM;
51class MediaKeyStatusMap;
52class MediaKeys;
53class SharedBuffer;
54
55class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTargetWithInlineData, public ActiveDOMObject, public CanMakeWeakPtr<MediaKeySession>, public CDMInstanceSessionClient {
56 WTF_MAKE_ISO_ALLOCATED(MediaKeySession);
57public:
58 static Ref<MediaKeySession> create(ScriptExecutionContext&, WeakPtr<MediaKeys>&&, MediaKeySessionType, bool useDistinctiveIdentifier, Ref<CDM>&&, Ref<CDMInstanceSession>&&);
59 virtual ~MediaKeySession();
60
61 using RefCounted<MediaKeySession>::ref;
62 using RefCounted<MediaKeySession>::deref;
63
64 bool isClosed() const { return m_closed; }
65
66 const String& sessionId() const;
67 double expiration() const;
68 Ref<MediaKeyStatusMap> keyStatuses() const;
69
70 void generateRequest(const AtomicString&, const BufferSource&, Ref<DeferredPromise>&&);
71 void load(const String&, Ref<DeferredPromise>&&);
72 void update(const BufferSource&, Ref<DeferredPromise>&&);
73 void close(Ref<DeferredPromise>&&);
74 void remove(Ref<DeferredPromise>&&);
75
76 using ClosedPromise = DOMPromiseProxy<IDLVoid>;
77 ClosedPromise& closed() { return m_closedPromise; }
78
79 const Vector<std::pair<Ref<SharedBuffer>, MediaKeyStatus>>& statuses() const { return m_statuses; }
80
81private:
82 MediaKeySession(ScriptExecutionContext&, WeakPtr<MediaKeys>&&, MediaKeySessionType, bool useDistinctiveIdentifier, Ref<CDM>&&, Ref<CDMInstanceSession>&&);
83 void enqueueMessage(MediaKeyMessageType, const SharedBuffer&);
84 void updateExpiration(double);
85 void sessionClosed();
86 String mediaKeysStorageDirectory() const;
87
88 // CDMInstanceSessionClient
89 void updateKeyStatuses(CDMInstanceSessionClient::KeyStatusVector&&) override;
90 void sendMessage(CDMMessageType, Ref<SharedBuffer>&& message) final;
91 void sessionIdChanged(const String&) final;
92
93 // EventTarget
94 EventTargetInterface eventTargetInterface() const override { return MediaKeySessionEventTargetInterfaceType; }
95 ScriptExecutionContext* scriptExecutionContext() const override { return ActiveDOMObject::scriptExecutionContext(); }
96 void refEventTarget() override { ref(); }
97 void derefEventTarget() override { deref(); }
98
99 // ActiveDOMObject
100 bool hasPendingActivity() const override;
101 const char* activeDOMObjectName() const override;
102 bool canSuspendForDocumentSuspension() const override;
103 void stop() override;
104
105 WeakPtr<MediaKeys> m_keys;
106 String m_sessionId;
107 double m_expiration;
108 ClosedPromise m_closedPromise;
109 Ref<MediaKeyStatusMap> m_keyStatuses;
110 bool m_closed { false };
111 bool m_uninitialized { true };
112 bool m_callable { false };
113 bool m_useDistinctiveIdentifier;
114 MediaKeySessionType m_sessionType;
115 Ref<CDM> m_implementation;
116 Ref<CDMInstanceSession> m_instanceSession;
117 GenericEventQueue m_eventQueue;
118 GenericTaskQueue<Timer> m_taskQueue;
119 Vector<Ref<SharedBuffer>> m_recordOfKeyUsage;
120 double m_firstDecryptTime { 0 };
121 double m_latestDecryptTime { 0 };
122 Vector<std::pair<Ref<SharedBuffer>, MediaKeyStatus>> m_statuses;
123 WeakPtrFactory<CDMInstanceSessionClient> m_cdmInstanceSessionClientWeakPtrFactory;
124};
125
126} // namespace WebCore
127
128#endif // ENABLE(ENCRYPTED_MEDIA)
129