Improve use of NeverDestroyed
[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([](CDM* cdm) { return std::make_unique<CDMPrivateClearKey>(cdm); },
65             CDMPrivateClearKey::supportsKeySystem, CDMPrivateClearKey::supportsKeySystemAndMimeType),
66
67         // FIXME: initialize specific UA CDMs. http://webkit.org/b/109318, http://webkit.org/b/109320
68         new CDMFactory([](CDM* cdm) { return std::make_unique<CDMPrivateMediaPlayer>(cdm); },
69             CDMPrivateMediaPlayer::supportsKeySystem, CDMPrivateMediaPlayer::supportsKeySystemAndMimeType),
70
71 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
72         new CDMFactory([](CDM* cdm) { return std::make_unique<CDMPrivateMediaSourceAVFObjC>(cdm); },
73             CDMPrivateMediaSourceAVFObjC::supportsKeySystem, CDMPrivateMediaSourceAVFObjC::supportsKeySystemAndMimeType),
74 #endif
75     });
76     return cdms;
77 }
78
79 void CDM::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 CDM::supportsKeySystem(const String& keySystem)
94 {
95     return CDMFactoryForKeySystem(keySystem);
96 }
97
98 bool CDM::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<CDM> CDM::create(const String& keySystem)
106 {
107     if (!supportsKeySystem(keySystem))
108         return nullptr;
109
110     return std::make_unique<CDM>(keySystem);
111 }
112
113 CDM::CDM(const String& keySystem)
114     : m_keySystem(keySystem)
115     , m_client(nullptr)
116 {
117     m_private = CDMFactoryForKeySystem(keySystem)->constructor(this);
118 }
119
120 CDM::~CDM()
121 {
122 }
123
124 bool CDM::supportsMIMEType(const String& mimeType) const
125 {
126     return m_private->supportsMIMEType(mimeType);
127 }
128
129 std::unique_ptr<CDMSession> CDM::createSession(CDMSessionClient& client)
130 {
131     auto session = m_private->createSession(&client);
132     if (mediaPlayer())
133         mediaPlayer()->setCDMSession(session.get());
134     return session;
135 }
136
137 MediaPlayer* CDM::mediaPlayer() const
138 {
139     if (!m_client)
140         return 0;
141     return m_client->cdmMediaPlayer(this);
142 }
143
144 }
145
146 #endif // ENABLE(LEGACY_ENCRYPTED_MEDIA)