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