DocumentLoader::timing() should return a reference.
[WebKit-https.git] / Source / WebCore / Modules / encryptedmedia / CDM.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(ENCRYPTED_MEDIA_V2)
29
30 #include "CDM.h"
31
32 #include "CDMPrivateMediaPlayer.h"
33 #include "CDMSession.h"
34 #include "MediaKeyError.h"
35 #include "MediaKeys.h"
36 #include "MediaPlayer.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(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 NeverDestroyed<Vector<CDMFactory*>> cdms;
64     static bool queriedCDMs = false;
65     if (!queriedCDMs) {
66         queriedCDMs = true;
67
68         // FIXME: initialize specific UA CDMs. http://webkit.org/b/109318, http://webkit.org/b/109320
69         cdms.get().append(new CDMFactory(CDMPrivateMediaPlayer::create, CDMPrivateMediaPlayer::supportsKeySystem, CDMPrivateMediaPlayer::supportsKeySystemAndMimeType));
70
71 #if PLATFORM(MAC) && ENABLE(MEDIA_SOURCE)
72         cdms.get().append(new CDMFactory(CDMPrivateMediaSourceAVFObjC::create, CDMPrivateMediaSourceAVFObjC::supportsKeySystem, CDMPrivateMediaSourceAVFObjC::supportsKeySystemAndMimeType));
73 #endif
74     }
75
76     return cdms;
77 }
78
79 void CDM::registerCDMFactory(CreateCDM constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
80 {
81     installedCDMFactories().append(new CDMFactory(constructor, supportsKeySystem, supportsKeySystemAndMimeType));
82 }
83
84 static CDMFactory* CDMFactoryForKeySystem(const String& keySystem)
85 {
86     Vector<CDMFactory*>& cdmFactories = installedCDMFactories();
87     for (size_t i = 0; i < cdmFactories.size(); ++i) {
88         if (cdmFactories[i]->supportsKeySystem(keySystem))
89             return cdmFactories[i];
90     }
91     return 0;
92 }
93
94 bool CDM::supportsKeySystem(const String& keySystem)
95 {
96     return CDMFactoryForKeySystem(keySystem);
97 }
98
99 bool CDM::keySystemSupportsMimeType(const String& keySystem, const String& mimeType)
100 {
101     if (CDMFactory* factory = CDMFactoryForKeySystem(keySystem))
102         return factory->supportsKeySystemAndMimeType(keySystem, mimeType);
103     return false;
104 }
105
106 std::unique_ptr<CDM> CDM::create(const String& keySystem)
107 {
108     if (!supportsKeySystem(keySystem))
109         return nullptr;
110
111     return std::make_unique<CDM>(keySystem);
112 }
113
114 CDM::CDM(const String& keySystem)
115     : m_keySystem(keySystem)
116     , m_client(0)
117 {
118     m_private = CDMFactoryForKeySystem(keySystem)->constructor(this);
119 }
120
121 CDM::~CDM()
122 {
123 }
124
125 bool CDM::supportsMIMEType(const String& mimeType) const
126 {
127     return m_private->supportsMIMEType(mimeType);
128 }
129
130 std::unique_ptr<CDMSession> CDM::createSession()
131 {
132     std::unique_ptr<CDMSession> session = m_private->createSession();
133     if (mediaPlayer())
134         mediaPlayer()->setCDMSession(session.get());
135     return session;
136 }
137
138 MediaPlayer* CDM::mediaPlayer() const
139 {
140     if (!m_client)
141         return 0;
142     return m_client->cdmMediaPlayer(this);
143 }
144
145 }
146
147 #endif // ENABLE(ENCRYPTED_MEDIA_V2)