2 * Copyright (C) 2016 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if ENABLE(ENCRYPTED_MEDIA)
31 #include "CDMFactory.h"
32 #include "CDMInstance.h"
33 #include "CDMPrivate.h"
34 #include "MediaKeysRequirement.h"
35 #include <wtf/HashMap.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/Vector.h>
38 #include <wtf/WeakPtr.h>
42 class MockCDMFactory : public RefCounted<MockCDMFactory>, private CDMFactory {
44 static Ref<MockCDMFactory> create() { return adoptRef(*new MockCDMFactory); }
47 const Vector<AtomicString>& supportedDataTypes() const { return m_supportedDataTypes; }
48 void setSupportedDataTypes(Vector<String>&&);
50 const Vector<MediaKeySessionType>& supportedSessionTypes() const { return m_supportedSessionTypes; }
51 void setSupportedSessionTypes(Vector<MediaKeySessionType>&& types) { m_supportedSessionTypes = WTFMove(types); }
53 const Vector<String>& supportedRobustness() const { return m_supportedRobustness; }
54 void setSupportedRobustness(Vector<String>&& robustness) { m_supportedRobustness = WTFMove(robustness); }
56 MediaKeysRequirement distinctiveIdentifiersRequirement() const { return m_distinctiveIdentifiersRequirement; }
57 void setDistinctiveIdentifiersRequirement(MediaKeysRequirement requirement) { m_distinctiveIdentifiersRequirement = requirement; }
59 MediaKeysRequirement persistentStateRequirement() const { return m_persistentStateRequirement; }
60 void setPersistentStateRequirement(MediaKeysRequirement requirement) { m_persistentStateRequirement = requirement; }
62 bool canCreateInstances() const { return m_canCreateInstances; }
63 void setCanCreateInstances(bool flag) { m_canCreateInstances = flag; }
65 bool supportsServerCertificates() const { return m_supportsServerCertificates; }
66 void setSupportsServerCertificates(bool flag) { m_supportsServerCertificates = flag; }
68 bool supportsSessions() const { return m_supportsSessions; }
69 void setSupportsSessions(bool flag) { m_supportsSessions = flag; }
73 bool hasSessionWithID(const String& id) { return m_sessions.contains(id); }
74 void removeSessionWithID(const String& id) { m_sessions.remove(id); }
75 void addKeysToSessionWithID(const String& id, Vector<Ref<SharedBuffer>>&&);
76 const Vector<Ref<SharedBuffer>>* keysForSessionWithID(const String& id) const;
77 Vector<Ref<SharedBuffer>> removeKeysFromSessionWithID(const String& id);
81 std::unique_ptr<CDMPrivate> createCDM(const String&) final;
82 bool supportsKeySystem(const String&) final;
84 MediaKeysRequirement m_distinctiveIdentifiersRequirement { MediaKeysRequirement::Optional };
85 MediaKeysRequirement m_persistentStateRequirement { MediaKeysRequirement::Optional };
86 Vector<AtomicString> m_supportedDataTypes;
87 Vector<MediaKeySessionType> m_supportedSessionTypes;
88 Vector<String> m_supportedRobustness;
89 bool m_registered { true };
90 bool m_canCreateInstances { true };
91 bool m_supportsServerCertificates { true };
92 bool m_supportsSessions { true };
93 WeakPtrFactory<MockCDMFactory> m_weakPtrFactory;
94 HashMap<String, Vector<Ref<SharedBuffer>>> m_sessions;
97 class MockCDM : public CDMPrivate {
99 MockCDM(WeakPtr<MockCDMFactory>);
101 MockCDMFactory* factory() { return m_factory.get(); }
104 friend class MockCDMInstance;
106 bool supportsInitDataType(const AtomicString&) const final;
107 bool supportsConfiguration(const MediaKeySystemConfiguration&) const final;
108 bool supportsConfigurationWithRestrictions(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
109 bool supportsSessionTypeWithConfiguration(MediaKeySessionType&, const MediaKeySystemConfiguration&) const final;
110 bool supportsRobustness(const String&) const final;
111 MediaKeysRequirement distinctiveIdentifiersRequirement(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
112 MediaKeysRequirement persistentStateRequirement(const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) const final;
113 bool distinctiveIdentifiersAreUniquePerOriginAndClearable(const MediaKeySystemConfiguration&) const final;
114 RefPtr<CDMInstance> createInstance() final;
115 void loadAndInitialize() final;
116 bool supportsServerCertificates() const final;
117 bool supportsSessions() const final;
118 bool supportsInitData(const AtomicString&, const SharedBuffer&) const final;
119 RefPtr<SharedBuffer> sanitizeResponse(const SharedBuffer&) const final;
120 std::optional<String> sanitizeSessionId(const String&) const final;
122 WeakPtr<MockCDMFactory> m_factory;
123 WeakPtrFactory<MockCDM> m_weakPtrFactory;
126 class MockCDMInstance : public CDMInstance {
128 MockCDMInstance(WeakPtr<MockCDM>);
131 ImplementationType implementationType() const final { return ImplementationType::Mock; }
132 SuccessValue initializeWithConfiguration(const MediaKeySystemConfiguration&) final;
133 SuccessValue setDistinctiveIdentifiersAllowed(bool) final;
134 SuccessValue setPersistentStateAllowed(bool) final;
135 SuccessValue setServerCertificate(Ref<SharedBuffer>&&) final;
136 SuccessValue setStorageDirectory(const String&) final;
137 void requestLicense(LicenseType, const AtomicString& initDataType, Ref<SharedBuffer>&& initData, LicenseCallback) final;
138 void updateLicense(const String&, LicenseType, const SharedBuffer&, LicenseUpdateCallback) final;
139 void loadSession(LicenseType, const String&, const String&, LoadSessionCallback) final;
140 void closeSession(const String&, CloseSessionCallback) final;
141 void removeSessionData(const String&, LicenseType, RemoveSessionDataCallback) final;
142 void storeRecordOfKeyUsage(const String&) final;
144 const String& keySystem() const final;
146 WeakPtr<MockCDM> m_cdm;
147 bool m_distinctiveIdentifiersAllowed { true };
148 bool m_persistentStateAllowed { true };