Use "= default" to denote default constructor or destructor
[WebKit-https.git] / Source / WebCore / Modules / encryptedmedia / legacy / LegacyCDM.cpp
1 /*
2  * Copyright (C) 2013 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 #include "config.h"
27
28 #if ENABLE(LEGACY_ENCRYPTED_MEDIA)
29
30 #include "LegacyCDM.h"
31
32 #include "LegacyCDMPrivateClearKey.h"
33 #include "LegacyCDMPrivateMediaPlayer.h"
34 #include "LegacyCDMSession.h"
35 #include "MediaPlayer.h"
36 #include "WebKitMediaKeys.h"
37 #include <wtf/NeverDestroyed.h>
38 #include <wtf/text/WTFString.h>
39
40 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
41 #include "CDMPrivateMediaSourceAVFObjC.h"
42 #endif
43
44 namespace WebCore {
45
46 struct CDMFactory {
47     WTF_MAKE_NONCOPYABLE(CDMFactory); WTF_MAKE_FAST_ALLOCATED;
48 public:
49     CDMFactory(CreateCDM&& constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
50         : constructor(WTFMove(constructor))
51         , supportsKeySystem(supportsKeySystem)
52         , supportsKeySystemAndMimeType(supportsKeySystemAndMimeType)
53     {
54     }
55
56     CreateCDM constructor;
57     CDMSupportsKeySystem supportsKeySystem;
58     CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType;
59 };
60
61 static Vector<CDMFactory*>& installedCDMFactories()
62 {
63     static auto cdms = makeNeverDestroyed(Vector<CDMFactory*> {
64         new CDMFactory([](LegacyCDM* cdm) { return std::make_unique<LegacyCDMPrivateClearKey>(cdm); },
65             LegacyCDMPrivateClearKey::supportsKeySystem, LegacyCDMPrivateClearKey::supportsKeySystemAndMimeType),
66
67         // FIXME: initialize specific UA CDMs. http://webkit.org/b/109318, http://webkit.org/b/109320
68         new CDMFactory([](LegacyCDM* cdm) { return std::make_unique<CDMPrivateMediaPlayer>(cdm); },
69             CDMPrivateMediaPlayer::supportsKeySystem, CDMPrivateMediaPlayer::supportsKeySystemAndMimeType),
70
71 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
72         new CDMFactory([](LegacyCDM* cdm) { return std::make_unique<CDMPrivateMediaSourceAVFObjC>(cdm); },
73             CDMPrivateMediaSourceAVFObjC::supportsKeySystem, CDMPrivateMediaSourceAVFObjC::supportsKeySystemAndMimeType),
74 #endif
75     });
76     return cdms;
77 }
78
79 void LegacyCDM::registerCDMFactory(CreateCDM&& constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
80 {
81     installedCDMFactories().append(new CDMFactory(WTFMove(constructor), supportsKeySystem, supportsKeySystemAndMimeType));
82 }
83
84 static CDMFactory* CDMFactoryForKeySystem(const String& keySystem)
85 {
86     for (auto& factory : installedCDMFactories()) {
87         if (factory->supportsKeySystem(keySystem))
88             return factory;
89     }
90     return nullptr;
91 }
92
93 bool LegacyCDM::supportsKeySystem(const String& keySystem)
94 {
95     return CDMFactoryForKeySystem(keySystem);
96 }
97
98 bool LegacyCDM::keySystemSupportsMimeType(const String& keySystem, const String& mimeType)
99 {
100     if (CDMFactory* factory = CDMFactoryForKeySystem(keySystem))
101         return factory->supportsKeySystemAndMimeType(keySystem, mimeType);
102     return false;
103 }
104
105 std::unique_ptr<LegacyCDM> LegacyCDM::create(const String& keySystem)
106 {
107     if (!supportsKeySystem(keySystem))
108         return nullptr;
109
110     return std::make_unique<LegacyCDM>(keySystem);
111 }
112
113 LegacyCDM::LegacyCDM(const String& keySystem)
114     : m_keySystem(keySystem)
115     , m_client(nullptr)
116 {
117     m_private = CDMFactoryForKeySystem(keySystem)->constructor(this);
118 }
119
120 LegacyCDM::~LegacyCDM() = default;
121
122 bool LegacyCDM::supportsMIMEType(const String& mimeType) const
123 {
124     return m_private->supportsMIMEType(mimeType);
125 }
126
127 std::unique_ptr<LegacyCDMSession> LegacyCDM::createSession(LegacyCDMSessionClient& client)
128 {
129     auto session = m_private->createSession(&client);
130     if (mediaPlayer())
131         mediaPlayer()->setCDMSession(session.get());
132     return session;
133 }
134
135 RefPtr<MediaPlayer> LegacyCDM::mediaPlayer() const
136 {
137     if (!m_client)
138         return nullptr;
139     return m_client->cdmMediaPlayer(this);
140 }
141
142 }
143
144 #endif // ENABLE(LEGACY_ENCRYPTED_MEDIA)