/*
 * Copyright (C) 2016 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CDM_h
#define CDM_h

#if ENABLE(ENCRYPTED_MEDIA)

#include "ContextDestructionObserver.h"
#include "MediaKeySystemConfiguration.h"
#include "SharedBuffer.h"
//#include <functional>
#include <wtf/HashSet.h>
#include <wtf/Optional.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/WeakPtr.h>
#include <wtf/text/WTFString.h>

namespace WebCore {

class CDM;
class CDMInstance;
class CDMPrivate;
class Document;
class ScriptExecutionContext;
class SharedBuffer;

struct MediaKeysRestrictions;

class CDMFactory {
public:
    virtual ~CDMFactory() { };
    virtual PassOwnPtr<CDMPrivate> createCDM(CDM&, const String& keySystem) = 0;
    virtual bool supportsKeySystem(const String&) = 0;
};

struct CDMKeyInfo
{
	Vector<char> kid;
	Vector<char> k;
};

struct CDMResponseInfo
{
	Vector<CDMKeyInfo> keys;
};

class CDM : public RefCounted<CDM>, public ContextDestructionObserver {
public:
    static void registerCDMFactory(CDMFactory&);
    static void unregisterCDMFactory(CDMFactory&);

    static bool supportsKeySystem(const String&);
    static bool isPersistentType(MediaKeySessionType);

    static PassRefPtr<CDM> create(ScriptExecutionContext*, const String& keySystem);
    ~CDM();

    enum ConfigurationStatus {
    	ConfigurationStatusSupported,
		ConfigurationStatusNotSupported,
		ConfigurationStatusConsentDenied,
    };

    enum AudioVideoType {
    	AudioVideoTypeAudio,
		AudioVideoTypeVideo,
    };

    enum ConsentStatus {
    	ConsentStatusConsentDenied,
		ConsentStatusInformUser,
		ConsentStatusAllowed,
    };

    class SupportedConfigurationCallback
	{
	public:
    	virtual ~SupportedConfigurationCallback() { }
    	virtual void call(Optional<MediaKeySystemConfiguration>) = 0;
	};

    class ConsentStatusCallback
	{
	public:
    	virtual ~ConsentStatusCallback() { }
    	virtual void call(ConsentStatus, const MediaKeySystemConfiguration&, const MediaKeysRestrictions&) = 0;
	};

    void doSupportedConfigurationStep(const MediaKeySystemConfiguration& candidateConfiguration, const MediaKeysRestrictions&, PassOwnPtr<SupportedConfigurationCallback>);

    void getSupportedConfigurationWithCallback(const MediaKeySystemConfiguration& candidateConfiguration, PassOwnPtr<SupportedConfigurationCallback>);

    const String& keySystem() const { return m_keySystem; }

    CDMPrivate* getPrivate() { return m_private.get(); }

    WeakPtr<CDM> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(this); }

    void loadAndInitialize();
    RefPtr<CDMInstance> createInstance();
    bool supportsServerCertificates() const;
    bool supportsSessions() const;

    bool supportsInitDataType(const AtomicString&) const;

    RefPtr<SharedBuffer> sanitizeInitData(const AtomicString& initDataType, const SharedBuffer&);
    bool supportsInitData(const AtomicString& initDataType, const SharedBuffer&);

    RefPtr<SharedBuffer> sanitizeResponse(RefPtr<SharedBuffer>, CDMResponseInfo&, bool isAesDecryptor);

    Optional<String> sanitizeSessionId(const String& sessionId);

private:
    CDM(ScriptExecutionContext*, const String& keySystem);

    Optional<MediaKeySystemConfiguration> getSupportedConfiguration(const MediaKeySystemConfiguration& candidateConfiguration, const MediaKeysRestrictions&);
    Optional<Vector<MediaKeySystemMediaCapability> > getSupportedCapabilitiesForAudioVideoType(AudioVideoType, const Vector<MediaKeySystemMediaCapability>& requestedCapabilities, const MediaKeySystemConfiguration& partialConfiguration, const MediaKeysRestrictions&);

    void getConsentStatus(const MediaKeySystemConfiguration& accumulatedConfiguration, const MediaKeysRestrictions&, PassOwnPtr<ConsentStatusCallback>);

    String m_keySystem;
    OwnPtr<CDMPrivate> m_private;
    WeakPtrFactory<CDM> m_weakPtrFactory;
};

}

#endif // ENABLE(ENCRYPTED_MEDIA)

#endif // CDM_h
