OLD | NEW |
| (Empty) |
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 #include "MockCDM.h" | |
28 | |
29 #if ENABLE(ENCRYPTED_MEDIA_V2) | |
30 | |
31 #include "core/html/MediaKeyError.h" | |
32 #include "modules/encryptedmedia/CDM.h" | |
33 #include <wtf/Uint8Array.h> | |
34 | |
35 namespace WebCore { | |
36 | |
37 class MockCDMSession : public CDMSession { | |
38 public: | |
39 static PassOwnPtr<MockCDMSession> create() { return adoptPtr(new MockCDMSess
ion()); } | |
40 virtual ~MockCDMSession() { } | |
41 | |
42 virtual const String& sessionId() const OVERRIDE { return m_sessionId; } | |
43 virtual PassRefPtr<Uint8Array> generateKeyRequest(const String& mimeType, Ui
nt8Array* initData, String& destinationURL, unsigned short& errorCode, unsigned
long& systemCode) OVERRIDE; | |
44 virtual void releaseKeys() OVERRIDE; | |
45 virtual bool update(Uint8Array*, RefPtr<Uint8Array>& nextMessage, unsigned s
hort& errorCode, unsigned long& systemCode) OVERRIDE; | |
46 | |
47 protected: | |
48 MockCDMSession(); | |
49 | |
50 String m_sessionId; | |
51 }; | |
52 | |
53 bool MockCDM::supportsKeySytem(const String& keySystem) | |
54 { | |
55 return equalIgnoringCase(keySystem, "com.webcore.mock"); | |
56 } | |
57 | |
58 bool MockCDM::supportsMIMEType(const String& mimeType) | |
59 { | |
60 return equalIgnoringCase(mimeType, "video/mock"); | |
61 } | |
62 | |
63 PassOwnPtr<CDMSession> MockCDM::createSession() | |
64 { | |
65 return MockCDMSession::create(); | |
66 } | |
67 | |
68 static Uint8Array* initDataPrefix() | |
69 { | |
70 static const unsigned char prefixData[] = {'m', 'o', 'c', 'k'}; | |
71 DEFINE_STATIC_LOCAL(RefPtr<Uint8Array>, prefix, ()); | |
72 static bool initialized = false; | |
73 if (!initialized) { | |
74 initialized = true; | |
75 prefix = Uint8Array::create(prefixData, sizeof(prefixData) / sizeof(pref
ixData[0])); | |
76 } | |
77 return prefix.get(); | |
78 } | |
79 | |
80 static Uint8Array* keyPrefix() | |
81 { | |
82 static const unsigned char prefixData[] = {'k', 'e', 'y'}; | |
83 DEFINE_STATIC_LOCAL(RefPtr<WTF::Uint8Array>, prefix, ()); | |
84 static bool initialized = false; | |
85 if (!initialized) { | |
86 initialized = true; | |
87 prefix = Uint8Array::create(prefixData, sizeof(prefixData) / sizeof(pref
ixData[0])); | |
88 } | |
89 return prefix.get(); | |
90 } | |
91 | |
92 static Uint8Array* keyRequest() | |
93 { | |
94 static const unsigned char requestData[] = {'r', 'e', 'q', 'u', 'e', 's', 't
'}; | |
95 DEFINE_STATIC_LOCAL(RefPtr<WTF::Uint8Array>, request, ()); | |
96 static bool initialized = false; | |
97 if (!initialized) { | |
98 initialized = true; | |
99 request = Uint8Array::create(requestData, sizeof(requestData) / sizeof(r
equestData[0])); | |
100 } | |
101 return request.get(); | |
102 } | |
103 | |
104 static String generateSessionId() | |
105 { | |
106 static int monotonicallyIncreasingSessionId = 0; | |
107 return String::number(monotonicallyIncreasingSessionId++); | |
108 } | |
109 | |
110 MockCDMSession::MockCDMSession() | |
111 : m_sessionId(generateSessionId()) | |
112 { | |
113 } | |
114 | |
115 PassRefPtr<Uint8Array> MockCDMSession::generateKeyRequest(const String&, Uint8Ar
ray* initData, String&, unsigned short& errorCode, unsigned long&) | |
116 { | |
117 for (unsigned i = 0; i < initDataPrefix()->length(); ++i) { | |
118 if (!initData || i >= initData->length() || initData->item(i) != initDat
aPrefix()->item(i)) { | |
119 errorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN; | |
120 return 0; | |
121 } | |
122 } | |
123 return keyRequest(); | |
124 } | |
125 | |
126 void MockCDMSession::releaseKeys() | |
127 { | |
128 // no-op | |
129 } | |
130 | |
131 bool MockCDMSession::update(Uint8Array* key, RefPtr<Uint8Array>&, unsigned short
& errorCode, unsigned long&) | |
132 { | |
133 for (unsigned i = 0; i < keyPrefix()->length(); ++i) { | |
134 if (i >= key->length() || key->item(i) != keyPrefix()->item(i)) { | |
135 errorCode = MediaKeyError::MEDIA_KEYERR_CLIENT; | |
136 return false; | |
137 } | |
138 } | |
139 return true; | |
140 } | |
141 | |
142 } | |
143 | |
144 #endif // ENABLE(ENCRYPTED_MEDIA_V2) | |
OLD | NEW |