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 <wtf/NeverDestroyed.h>
37#include <wtf/text/WTFString.h>
38
39namespace WebCore {
40
41struct CDMFactory {
42    WTF_MAKE_NONCOPYABLE(CDMFactory); WTF_MAKE_FAST_ALLOCATED;
43public:
44    CDMFactory(CreateCDM constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
45        : constructor(constructor)
46        , supportsKeySystem(supportsKeySystem)
47        , supportsKeySystemAndMimeType(supportsKeySystemAndMimeType)
48    {
49    }
50
51    CreateCDM constructor;
52    CDMSupportsKeySystem supportsKeySystem;
53    CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType;
54};
55
56static Vector<CDMFactory*>& installedCDMFactories()
57{
58    static NeverDestroyed<Vector<CDMFactory*>> cdms;
59    static bool queriedCDMs = false;
60    if (!queriedCDMs) {
61        queriedCDMs = true;
62
63        // FIXME: initialize specific UA CDMs. http://webkit.org/b/109318, http://webkit.org/b/109320
64        cdms.get().append(new CDMFactory(CDMPrivateMediaPlayer::create, CDMPrivateMediaPlayer::supportsKeySystem, CDMPrivateMediaPlayer::supportsKeySystemAndMimeType));
65    }
66
67    return cdms;
68}
69
70void CDM::registerCDMFactory(CreateCDM constructor, CDMSupportsKeySystem supportsKeySystem, CDMSupportsKeySystemAndMimeType supportsKeySystemAndMimeType)
71{
72    installedCDMFactories().append(new CDMFactory(constructor, supportsKeySystem, supportsKeySystemAndMimeType));
73}
74
75static CDMFactory* CDMFactoryForKeySystem(const String& keySystem)
76{
77    Vector<CDMFactory*>& cdmFactories = installedCDMFactories();
78    for (size_t i = 0; i < cdmFactories.size(); ++i) {
79        if (cdmFactories[i]->supportsKeySystem(keySystem))
80            return cdmFactories[i];
81    }
82    return 0;
83}
84
85bool CDM::supportsKeySystem(const String& keySystem)
86{
87    return CDMFactoryForKeySystem(keySystem);
88}
89
90bool CDM::keySystemSupportsMimeType(const String& keySystem, const String& mimeType)
91{
92    if (CDMFactory* factory = CDMFactoryForKeySystem(keySystem))
93        return factory->supportsKeySystemAndMimeType(keySystem, mimeType);
94    return false;
95}
96
97std::unique_ptr<CDM> CDM::create(const String& keySystem)
98{
99    if (!supportsKeySystem(keySystem))
100        return nullptr;
101
102    return std::make_unique<CDM>(keySystem);
103}
104
105CDM::CDM(const String& keySystem)
106    : m_keySystem(keySystem)
107    , m_client(0)
108{
109    m_private = CDMFactoryForKeySystem(keySystem)->constructor(this);
110}
111
112CDM::~CDM()
113{
114}
115
116bool CDM::supportsMIMEType(const String& mimeType) const
117{
118    return m_private->supportsMIMEType(mimeType);
119}
120
121std::unique_ptr<CDMSession> CDM::createSession()
122{
123    return m_private->createSession();
124}
125
126MediaPlayer* CDM::mediaPlayer() const
127{
128    if (!m_client)
129        return 0;
130    return m_client->cdmMediaPlayer(this);
131}
132
133}
134
135#endif // ENABLE(ENCRYPTED_MEDIA_V2)
136