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#include "config.h"
30#include "MediaKeySystemAccess.h"
31
32#if ENABLE(ENCRYPTED_MEDIA)
33
34#include "CDM.h"
35#include "CDMInstance.h"
36#include "JSMediaKeys.h"
37#include "MediaKeys.h"
38#include "MediaKeysRequirement.h"
39
40namespace WebCore {
41
42Ref<MediaKeySystemAccess> MediaKeySystemAccess::create(const String& keySystem, MediaKeySystemConfiguration&& configuration, Ref<CDM>&& implementation)
43{
44 return adoptRef(*new MediaKeySystemAccess(keySystem, WTFMove(configuration), WTFMove(implementation)));
45}
46
47MediaKeySystemAccess::MediaKeySystemAccess(const String& keySystem, MediaKeySystemConfiguration&& configuration, Ref<CDM>&& implementation)
48 : m_keySystem(keySystem)
49 , m_configuration(new MediaKeySystemConfiguration(WTFMove(configuration)))
50 , m_implementation(WTFMove(implementation))
51{
52}
53
54MediaKeySystemAccess::~MediaKeySystemAccess() = default;
55
56void MediaKeySystemAccess::createMediaKeys(Ref<DeferredPromise>&& promise)
57{
58 // https://w3c.github.io/encrypted-media/#dom-mediakeysystemaccess-createmediakeys
59 // W3C Editor's Draft 09 November 2016
60
61 // When this method is invoked, the user agent must run the following steps:
62 // 1. Let promise be a new promise.
63 // 2. Run the following steps in parallel:
64 m_taskQueue.enqueueTask([this, promise = WTFMove(promise)] () mutable {
65 // 2.1. Let configuration be the value of this object's configuration value.
66 // 2.2. Let use distinctive identifier be true if the value of configuration's distinctiveIdentifier member is "required" and false otherwise.
67 bool useDistinctiveIdentifier = m_configuration->distinctiveIdentifier == MediaKeysRequirement::Required;
68
69 // 2.3. Let persistent state allowed be true if the value of configuration's persistentState member is "required" and false otherwise.
70 bool persistentStateAllowed = m_configuration->persistentState == MediaKeysRequirement::Required;
71
72 // 2.4. Load and initialize the Key System implementation represented by this object's cdm implementation value if necessary.
73 m_implementation->loadAndInitialize();
74
75 // 2.5. Let instance be a new instance of the Key System implementation represented by this object's cdm implementation value.
76 auto instance = m_implementation->createInstance();
77 if (!instance) {
78 promise->reject(InvalidStateError);
79 return;
80 }
81
82 // 2.6. Initialize instance to enable, disable and/or select Key System features using configuration.
83 if (instance->initializeWithConfiguration(*m_configuration) == CDMInstance::Failed) {
84 promise->reject(NotAllowedError);
85 return;
86 }
87
88 // 2.7. If use distinctive identifier is false, prevent instance from using Distinctive Identifier(s) and Distinctive Permanent Identifier(s).
89 if (!useDistinctiveIdentifier && instance->setDistinctiveIdentifiersAllowed(false) == CDMInstance::Failed) {
90 promise->reject(NotAllowedError);
91 return;
92 }
93
94 // 2.8. If persistent state allowed is false, prevent instance from persisting any state related to the application or origin of this object's Document.
95 if (!persistentStateAllowed && instance->setPersistentStateAllowed(false) == CDMInstance::Failed) {
96 promise->reject(NotAllowedError);
97 return;
98 }
99
100 // 2.9. If any of the preceding steps failed, reject promise with a new DOMException whose name is the appropriate error name.
101 // 2.10. Let media keys be a new MediaKeys object, and initialize it as follows:
102 // 2.10.1. Let the use distinctive identifier value be use distinctive identifier.
103 // 2.10.2. Let the persistent state allowed value be persistent state allowed.
104 // 2.10.3. Let the supported session types value be be the value of configuration's sessionTypes member.
105 // 2.10.4. Let the cdm implementation value be this object's cdm implementation value.
106 // 2.10.5. Let the cdm instance value be instance.
107 auto mediaKeys = MediaKeys::create(useDistinctiveIdentifier, persistentStateAllowed, m_configuration->sessionTypes, m_implementation.copyRef(), instance.releaseNonNull());
108
109 // 2.11. Resolve promise with media keys.
110 promise->resolveWithNewlyCreated<IDLInterface<MediaKeys>>(WTFMove(mediaKeys));
111 });
112
113 // 3. Return promise.
114}
115
116} // namespace WebCore
117
118#endif // ENABLE(ENCRYPTED_MEDIA)
119