1/*
2 * Copyright (C) 2016 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#pragma once
27
28#if ENABLE(ENCRYPTED_MEDIA)
29
30#include "CDM.h"
31#include "CDMFactory.h"
32#include "CDMInstance.h"
33#include "CDMInstanceSession.h"
34#include "CDMPrivate.h"
35#include "MediaKeyEncryptionScheme.h"
36#include "MediaKeysRequirement.h"
37#include <wtf/HashMap.h>
38#include <wtf/RefCounted.h>
39#include <wtf/Vector.h>
40#include <wtf/WeakPtr.h>
41
42namespace WebCore {
43
44class MockCDMFactory : public RefCounted<MockCDMFactory>, public CanMakeWeakPtr<MockCDMFactory>, private CDMFactory {
45public:
46 static Ref<MockCDMFactory> create() { return adoptRef(*new MockCDMFactory); }
47 ~MockCDMFactory();
48
49 const Vector<AtomString>& supportedDataTypes() const { return m_supportedDataTypes; }
50 void setSupportedDataTypes(Vector<String>&&);
51
52 const Vector<MediaKeySessionType>& supportedSessionTypes() const { return m_supportedSessionTypes; }
53 void setSupportedSessionTypes(Vector<MediaKeySessionType>&& types) { m_supportedSessionTypes = WTFMove(types); }
54
55 const Vector<String>& supportedRobustness() const { return m_supportedRobustness; }
56 void setSupportedRobustness(Vector<String>&& robustness) { m_supportedRobustness = WTFMove(robustness); }
57
58 MediaKeysRequirement distinctiveIdentifiersRequirement() const { return m_distinctiveIdentifiersRequirement; }
59 void setDistinctiveIdentifiersRequirement(MediaKeysRequirement requirement) { m_distinctiveIdentifiersRequirement = requirement; }
60
61 MediaKeysRequirement persistentStateRequirement() const { return m_persistentStateRequirement; }
62 void setPersistentStateRequirement(MediaKeysRequirement requirement) { m_persistentStateRequirement = requirement; }
63
64 bool canCreateInstances() const { return m_canCreateInstances; }
65 void setCanCreateInstances(bool flag) { m_canCreateInstances = flag; }
66
67 bool supportsServerCertificates() const { return m_supportsServerCertificates; }
68 void setSupportsServerCertificates(bool flag) { m_supportsServerCertificates = flag; }
69
70 bool supportsSessions() const { return m_supportsSessions; }
71 void setSupportsSessions(bool flag) { m_supportsSessions = flag; }
72
73 const Vector<MediaKeyEncryptionScheme>& supportedEncryptionSchemes() const { return m_supportedEncryptionSchemes; }
74 void setSupportedEncryptionSchemes(Vector<MediaKeyEncryptionScheme>&& schemes) { m_supportedEncryptionSchemes = WTFMove(schemes); }
75
76 void unregister();
77
78 bool hasSessionWithID(const String& id) { return m_sessions.contains(id); }
79 void removeSessionWithID(const String& id) { m_sessions.remove(id); }
80 void addKeysToSessionWithID(const String& id, Vector<Ref<SharedBuffer>>&&);
81 const Vector<Ref<SharedBuffer>>* keysForSessionWithID(const String& id) const;
82 Vector<Ref<SharedBuffer>> removeKeysFromSessionWithID(const String& id);
83
84private:
85 MockCDMFactory();
86 std::unique_ptr<CDMPrivate> createCDM(const String&) final;
87 bool supportsKeySystem(const String&) final;
88
89 MediaKeysRequirement m_distinctiveIdentifiersRequirement { MediaKeysRequirement::Optional };
90 MediaKeysRequirement m_persistentStateRequirement { MediaKeysRequirement::Optional };
91 Vector<AtomString> m_supportedDataTypes;
92 Vector<MediaKeySessionType> m_supportedSessionTypes;
93 Vector<String> m_supportedRobustness;
94 Vector<MediaKeyEncryptionScheme> m_supportedEncryptionSchemes;
95 bool m_registered { true };
96 bool m_canCreateInstances { true };
97 bool m_supportsServerCertificates { true };
98 bool m_supportsSessions { true };
99 HashMap<String, Vector<Ref<SharedBuffer>>> m_sessions;
100};
101
102class MockCDM : public CDMPrivate, public CanMakeWeakPtr<MockCDM> {
103public:
104 MockCDM(WeakPtr<MockCDMFactory>);
105
106 MockCDMFactory* factory() { return m_factory.get(); }
107
108private:
109 friend class MockCDMInstance;
110
111 bool supportsInitDataType(const AtomString&) const final;
112 bool supportsConfiguration(const MediaKeySystemConfiguration&) const final;
113 bool supportsConfigurationWithRestrictions(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
114 bool supportsSessionTypeWithConfiguration(MediaKeySessionType&, const MediaKeySystemConfiguration&) const final;
115 bool supportsRobustness(const String&) const final;
116 MediaKeysRequirement distinctiveIdentifiersRequirement(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
117 MediaKeysRequirement persistentStateRequirement(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
118 bool distinctiveIdentifiersAreUniquePerOriginAndClearable(const MediaKeySystemConfiguration&) const final;
119 RefPtr<CDMInstance> createInstance() final;
120 void loadAndInitialize() final;
121 bool supportsServerCertificates() const final;
122 bool supportsSessions() const final;
123 bool supportsInitData(const AtomString&, const SharedBuffer&) const final;
124 RefPtr<SharedBuffer> sanitizeResponse(const SharedBuffer&) const final;
125 Optional<String> sanitizeSessionId(const String&) const final;
126
127 WeakPtr<MockCDMFactory> m_factory;
128};
129
130class MockCDMInstance : public CDMInstance, public CanMakeWeakPtr<MockCDMInstance> {
131public:
132 MockCDMInstance(WeakPtr<MockCDM>);
133
134 MockCDMFactory* factory() const { return m_cdm ? m_cdm->factory() : nullptr; }
135 bool distinctiveIdentifiersAllowed() const { return m_distinctiveIdentifiersAllowed; }
136 bool persistentStateAllowed() const { return m_persistentStateAllowed; }
137
138private:
139 ImplementationType implementationType() const final { return ImplementationType::Mock; }
140 SuccessValue initializeWithConfiguration(const MediaKeySystemConfiguration&) final;
141 SuccessValue setDistinctiveIdentifiersAllowed(bool) final;
142 SuccessValue setPersistentStateAllowed(bool) final;
143 SuccessValue setServerCertificate(Ref<SharedBuffer>&&) final;
144 SuccessValue setStorageDirectory(const String&) final;
145 const String& keySystem() const final;
146 RefPtr<CDMInstanceSession> createSession() final;
147
148 WeakPtr<MockCDM> m_cdm;
149 bool m_distinctiveIdentifiersAllowed { true };
150 bool m_persistentStateAllowed { true };
151};
152
153class MockCDMInstanceSession : public CDMInstanceSession {
154public:
155 MockCDMInstanceSession(WeakPtr<MockCDMInstance>&&);
156
157private:
158 void requestLicense(LicenseType, const AtomString& initDataType, Ref<SharedBuffer>&& initData, LicenseCallback&&) final;
159 void updateLicense(const String&, LicenseType, const SharedBuffer&, LicenseUpdateCallback&&) final;
160 void loadSession(LicenseType, const String&, const String&, LoadSessionCallback&&) final;
161 void closeSession(const String&, CloseSessionCallback&&) final;
162 void removeSessionData(const String&, LicenseType, RemoveSessionDataCallback&&) final;
163 void storeRecordOfKeyUsage(const String&) final;
164
165 WeakPtr<MockCDMInstance> m_instance;
166};
167
168}
169
170#endif
171