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