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 "ExceptionOr.h"
34#include "GenericTaskQueue.h"
35#include "JSDOMPromiseDeferred.h"
36#include "MediaKeySessionType.h"
37#include <wtf/Ref.h>
38#include <wtf/RefCounted.h>
39#include <wtf/WeakPtr.h>
40
41namespace WebCore {
42
43class CDM;
44class CDMClient;
45class CDMInstance;
46class BufferSource;
47class MediaKeySession;
48
49class MediaKeys : public RefCounted<MediaKeys>, public CanMakeWeakPtr<MediaKeys> {
50public:
51 using KeySessionType = MediaKeySessionType;
52
53 static Ref<MediaKeys> create(bool useDistinctiveIdentifier, bool persistentStateAllowed, const Vector<MediaKeySessionType>& supportedSessionTypes, Ref<CDM>&& implementation, Ref<CDMInstance>&& instance)
54 {
55 return adoptRef(*new MediaKeys(useDistinctiveIdentifier, persistentStateAllowed, supportedSessionTypes, WTFMove(implementation), WTFMove(instance)));
56 }
57
58 ~MediaKeys();
59
60 ExceptionOr<Ref<MediaKeySession>> createSession(ScriptExecutionContext&, MediaKeySessionType);
61 void setServerCertificate(const BufferSource&, Ref<DeferredPromise>&&);
62
63 void attachCDMClient(CDMClient&);
64 void detachCDMClient(CDMClient&);
65 void attemptToResumePlaybackOnClients();
66
67 bool hasOpenSessions() const;
68 CDMInstance& cdmInstance() { return m_instance; }
69 const CDMInstance& cdmInstance() const { return m_instance; }
70
71protected:
72 MediaKeys(bool useDistinctiveIdentifier, bool persistentStateAllowed, const Vector<MediaKeySessionType>&, Ref<CDM>&&, Ref<CDMInstance>&&);
73
74 bool m_useDistinctiveIdentifier;
75 bool m_persistentStateAllowed;
76 Vector<MediaKeySessionType> m_supportedSessionTypes;
77 Ref<CDM> m_implementation;
78 Ref<CDMInstance> m_instance;
79
80 Vector<Ref<MediaKeySession>> m_sessions;
81 Vector<CDMClient*> m_cdmClients;
82 GenericTaskQueue<Timer> m_taskQueue;
83};
84
85} // namespace WebCore
86
87#endif // ENABLE(ENCRYPTED_MEDIA)
88